|
| 1 | +--[[ |
| 2 | +SPDX-License-Identifier: ISC |
| 3 | +Copyright (c) 2023-2025, Sergey Bronnikov. |
| 4 | +
|
| 5 | +String Buffer Library, |
| 6 | +https://luajit.org/ext_buffer.html |
| 7 | +
|
| 8 | +Recording of buffer:set can anchor wrong object, |
| 9 | +https://github.com/LuaJIT/LuaJIT/issues/1125 |
| 10 | +
|
| 11 | +String buffer methods may be called one extra time after loop, |
| 12 | +https://github.com/LuaJIT/LuaJIT/issues/755 |
| 13 | +
|
| 14 | +Traceexit in recff_buffer_method_put and recff_buffer_method_get |
| 15 | +might redo work, https://github.com/LuaJIT/LuaJIT/issues/798 |
| 16 | +
|
| 17 | +Invalid bufput_bufstr fold over lj_serialize_encode, |
| 18 | +https://github.com/LuaJIT/LuaJIT/issues/799 |
| 19 | +
|
| 20 | +COW buffer might not copy, |
| 21 | +https://github.com/LuaJIT/LuaJIT/issues/816 |
| 22 | +
|
| 23 | +String buffer API, |
| 24 | +https://github.com/LuaJIT/LuaJIT/issues/14 |
| 25 | +
|
| 26 | +Add missing GC steps to string buffer methods, |
| 27 | +https://github.com/LuaJIT/LuaJIT/commit/9c3df68a |
| 28 | +]] |
| 29 | + |
| 30 | +local luzer = require("luzer") |
| 31 | +local test_lib = require("lib") |
| 32 | + |
| 33 | +-- LuaJIT only. |
| 34 | +if test_lib.lua_version() ~= "LuaJIT" then |
| 35 | + print("Unsupported version.") |
| 36 | + os.exit(0) |
| 37 | +end |
| 38 | + |
| 39 | +local ffi = require("ffi") |
| 40 | +local string_buf = require("string.buffer") |
| 41 | +local unpack = unpack or table.unpack |
| 42 | + |
| 43 | +local MAX_N = 1e2 |
| 44 | + |
| 45 | +local function random_objects(self) |
| 46 | + local obj_type = self.fdp:oneof({ |
| 47 | + "number", |
| 48 | + "string", |
| 49 | + }) |
| 50 | + -- `count` must be less than UINT_MAX and there are at least |
| 51 | + -- extra free stack slots in the stack, otherwise an error |
| 52 | + -- "too many results to unpack" is raised, see <ltablib.c>. |
| 53 | + local count = self.fdp:consume_integer(1, 1024) |
| 54 | + local objects |
| 55 | + if obj_type == "string" then |
| 56 | + objects = self.fdp:consume_strings(self.MAX_N, count) |
| 57 | + elseif obj_type == "number" then |
| 58 | + objects = self.fdp:consume_numbers( |
| 59 | + test_lib.MIN_INT64, test_lib.MAX_INT64, count) |
| 60 | + else |
| 61 | + assert(nil, "object type is unsupported") |
| 62 | + end |
| 63 | + |
| 64 | + return objects |
| 65 | +end |
| 66 | + |
| 67 | +-- Reset (empty) the buffer. The allocated buffer space is not |
| 68 | +-- freed and may be reused. |
| 69 | +-- Usage: buf = buf:reset() |
| 70 | +local function buffer_reset(self) |
| 71 | + self.buf:reset() |
| 72 | +end |
| 73 | + |
| 74 | +-- Appends the formatted arguments to the buffer. The format |
| 75 | +-- string supports the same options as `string.format()`. |
| 76 | +-- Usage: buf = buf:putf(format, ...) |
| 77 | +local function buffer_putf(self) |
| 78 | + local str = self.fdp:consume_string(self.MAX_N) |
| 79 | + self.buf:putf("%s", str) |
| 80 | +end |
| 81 | + |
| 82 | +-- Appends the given `len` number of bytes from the memory pointed |
| 83 | +-- to by the FFI cdata object to the buffer. The object needs to |
| 84 | +-- be convertible to a (constant) pointer. |
| 85 | +-- Usage: buf = buf:putcdata(cdata, len) |
| 86 | +local function buffer_putcdata(self) |
| 87 | + local n = self.fdp:consume_integer(1, 255) |
| 88 | + local cdata = ffi.new("uint8_t[?]", 1, n) |
| 89 | + self.buf:putcdata(cdata, ffi.sizeof(cdata)) |
| 90 | +end |
| 91 | + |
| 92 | +-- This method allows zero-copy consumption of a string or an FFI |
| 93 | +-- cdata object as a buffer. It stores a reference to the passed |
| 94 | +-- string `str` or the FFI cdata object in the buffer. Any buffer |
| 95 | +-- space originally allocated is freed. This is not an append |
| 96 | +-- operation, unlike the buf:put*() methods. |
| 97 | +local function buffer_set(self) |
| 98 | + local str = self.fdp:consume_string(self.MAX_N) |
| 99 | + self.buf:set(str) |
| 100 | +end |
| 101 | + |
| 102 | +-- Appends a string str, a number num or any object obj with |
| 103 | +-- a `__tostring` metamethod to the buffer. Multiple arguments are |
| 104 | +-- appended in the given order. Appending a buffer to a buffer is |
| 105 | +-- possible and short-circuited internally. But it still involves |
| 106 | +-- a copy. Better combine the buffer writes to use a single buffer. |
| 107 | +-- Usage: buf = buf:put([str | num | obj] [, ...]) |
| 108 | +local function buffer_put(self) |
| 109 | + local objects = self:random_objects() |
| 110 | + local buf = self.buf:put(unpack(objects)) |
| 111 | + assert(type(buf) == "userdata") |
| 112 | +end |
| 113 | + |
| 114 | +-- Consumes the buffer data and returns one or more strings. If |
| 115 | +-- called without arguments, the whole buffer data is consumed. |
| 116 | +-- If called with a number, up to len bytes are consumed. A `nil` |
| 117 | +-- argument consumes the remaining buffer space (this only makes |
| 118 | +-- sense as the last argument). Multiple arguments consume the |
| 119 | +-- buffer data in the given order. |
| 120 | +-- Note: a zero length or no remaining buffer data returns an |
| 121 | +-- empty string and not nil. |
| 122 | +-- Usage: str, ... = buf:get([ len|nil ] [,...]) |
| 123 | +local function buffer_get(self) |
| 124 | + local len = self.fdp:consume_integer(0, self.MAX_N) |
| 125 | + local str = self.buf:get(len) |
| 126 | + assert(type(str) == "string") |
| 127 | +end |
| 128 | + |
| 129 | +local function buffer_tostring(self) |
| 130 | + local str = self.buf:tostring() |
| 131 | + assert(type(str) == "string") |
| 132 | +end |
| 133 | + |
| 134 | +-- The commit method appends the `used` bytes of the previously |
| 135 | +-- returned write space to the buffer data. |
| 136 | +-- Usage: buf = buf:commit(used) |
| 137 | +local function buffer_commit(self) |
| 138 | + local used = self.fdp:consume_integer(0, self.MAX_N) |
| 139 | + -- The function may throw an error "number out of range". |
| 140 | + local _, _ = pcall(self.buf.commit, self.buf, used) |
| 141 | +end |
| 142 | + |
| 143 | +-- The reserve method reserves at least `size` bytes of write |
| 144 | +-- space in the buffer. It returns an `uint8_t *` FFI cdata |
| 145 | +-- pointer `ptr` that points to this space. The space returned by |
| 146 | +-- `buf:reserve()` starts at the returned pointer and ends before |
| 147 | +-- len bytes after that. |
| 148 | +-- Usage: ptr, len = buf:reserve(size) |
| 149 | +local function buffer_reserve(self) |
| 150 | + local size = self.fdp:consume_integer(0, self.MAX_N) |
| 151 | + local ptr, len = self.buf:reserve(size) |
| 152 | + assert(type(ptr) == "cdata") |
| 153 | + assert(ffi.typeof(ptr) == ffi.typeof("uint8_t *")) |
| 154 | + assert(type(len) == "number") |
| 155 | +end |
| 156 | + |
| 157 | +-- Skips (consumes) `len` bytes from the buffer up to the current |
| 158 | +-- length of the buffer data. |
| 159 | +-- Usage: buf = buf:skip(len) |
| 160 | +local function buffer_skip(self) |
| 161 | + local len = self.fdp:consume_integer(0, self.MAX_N) |
| 162 | + local buf = self.buf:skip(len) |
| 163 | + assert(type(buf) == "userdata") |
| 164 | +end |
| 165 | + |
| 166 | +-- Returns an uint8_t * FFI cdata pointer ptr that points to the |
| 167 | +-- buffer data. The length of the buffer data in bytes is returned |
| 168 | +-- in `len`. The space returned by `buf:ref()` starts at the |
| 169 | +-- returned pointer and ends before len bytes after that. |
| 170 | +-- Synopsis: ptr, len = buf:ref() |
| 171 | +local function buffer_ref(self) |
| 172 | + local ptr, len = self.buf:ref() |
| 173 | + assert(type(ptr) == "cdata") |
| 174 | + assert(ffi.typeof(ptr) == ffi.typeof("uint8_t *")) |
| 175 | + assert(type(len) == "number") |
| 176 | +end |
| 177 | + |
| 178 | +-- Returns the current length of the buffer data in bytes. |
| 179 | +local function buffer_len(self) |
| 180 | + return #self.buf |
| 181 | +end |
| 182 | + |
| 183 | +-- The Lua concatenation operator `..` also accepts buffers, just |
| 184 | +-- like strings or numbers. It always returns a string and not |
| 185 | +-- a buffer. |
| 186 | +local function buffer_concat(self) |
| 187 | + local str = self.fdp:consume_string(1, self.MAX_N) |
| 188 | + local buf = self.buf .. str |
| 189 | + assert(type(buf) == "string") |
| 190 | +end |
| 191 | + |
| 192 | +-- Serializes (encodes) the Lua object `obj`. The stand-alone |
| 193 | +-- function returns a string `str`. The buffer method appends the |
| 194 | +-- encoding to the buffer. `obj` can be any of the supported Lua |
| 195 | +-- types - it doesn't need to be a Lua table. |
| 196 | +-- This function may throw an error when attempting to serialize |
| 197 | +-- unsupported object types, circular references or deeply nested |
| 198 | +-- tables. |
| 199 | +-- Usage: |
| 200 | +-- str = buffer.encode(obj) |
| 201 | +-- buf = buf:encode(obj) |
| 202 | +local function buffer_encode(self) |
| 203 | + local objects = self:random_objects() |
| 204 | + local ptr = self.buf:encode(objects) |
| 205 | + assert(type(ptr) == "userdata") |
| 206 | +end |
| 207 | + |
| 208 | +-- The stand-alone function deserializes (decodes) the string |
| 209 | +-- `str`, the buffer method deserializes one object from the |
| 210 | +-- buffer. Both return a Lua object `obj`. |
| 211 | +-- The returned object may be any of the supported Lua types - |
| 212 | +-- even `nil`. This function may throw an error when fed with |
| 213 | +-- malformed or incomplete encoded data. The stand-alone function |
| 214 | +-- throws when there's left-over data after decoding a single |
| 215 | +-- top-level object. The buffer method leaves any left-over data |
| 216 | +-- in the buffer. |
| 217 | +-- Usage: |
| 218 | +-- obj = buffer.decode(str) |
| 219 | +-- obj = buf:decode() |
| 220 | +local function buffer_decode(self) |
| 221 | + local str = self.fdp:consume_string(0, self.MAX_N) |
| 222 | + -- The function may throw an error "unexpected end of buffer". |
| 223 | + local _, _ = pcall(self.buf.decode, self.buf, str) |
| 224 | +end |
| 225 | + |
| 226 | +-- The buffer space of the buffer object is freed. The object |
| 227 | +-- itself remains intact, empty and may be reused. |
| 228 | +local function buffer_free(self) |
| 229 | + self.buf:free() |
| 230 | + assert(#self.buf == 0) |
| 231 | +end |
| 232 | + |
| 233 | +local buffer_methods = { |
| 234 | + buffer_commit, |
| 235 | + buffer_concat, |
| 236 | + buffer_decode, |
| 237 | + buffer_encode, |
| 238 | + buffer_get, |
| 239 | + buffer_len, |
| 240 | + buffer_put, |
| 241 | + buffer_putcdata, |
| 242 | + buffer_putf, |
| 243 | + buffer_ref, |
| 244 | + buffer_reserve, |
| 245 | + buffer_reset, |
| 246 | + buffer_set, |
| 247 | + buffer_skip, |
| 248 | + buffer_tostring, |
| 249 | +} |
| 250 | + |
| 251 | +local function buffer_random_op(self) |
| 252 | + local buffer_method = self.fdp:oneof(buffer_methods) |
| 253 | + buffer_method(self) |
| 254 | +end |
| 255 | + |
| 256 | +local function buffer_new(fdp) |
| 257 | + local buf_size = fdp:consume_integer(1, MAX_N) |
| 258 | + local b = string_buf.new(buf_size) |
| 259 | + return { |
| 260 | + buf = b, |
| 261 | + fdp = fdp, |
| 262 | + free = buffer_free, |
| 263 | + random_objects = random_objects, |
| 264 | + random_operation = buffer_random_op, |
| 265 | + MAX_N = MAX_N, |
| 266 | + } |
| 267 | +end |
| 268 | + |
| 269 | +local function TestOneInput(buf, _size) |
| 270 | + local fdp = luzer.FuzzedDataProvider(buf) |
| 271 | + local nops = fdp:consume_number(1, MAX_N) |
| 272 | + local b = buffer_new(fdp) |
| 273 | + for _ = 1, nops do |
| 274 | + b:random_operation() |
| 275 | + end |
| 276 | + b:free() |
| 277 | +end |
| 278 | + |
| 279 | +local args = { |
| 280 | + artifact_prefix = "string_buffer_torture_", |
| 281 | +} |
| 282 | +luzer.Fuzz(TestOneInput, nil, args) |
0 commit comments