|
| 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 formats = { -- luacheck: no unused |
| 44 | + "complex", |
| 45 | + "false", |
| 46 | + "int", |
| 47 | + "int64", |
| 48 | + "lightud32", |
| 49 | + "lightud64", |
| 50 | + "nil", |
| 51 | + "null", |
| 52 | + "num", |
| 53 | + "string", |
| 54 | + "tab", |
| 55 | + "tab_mt", |
| 56 | + "true", |
| 57 | + "uint64", |
| 58 | +} |
| 59 | + |
| 60 | +-- Reset (empty) the buffer. The allocated buffer space is not |
| 61 | +-- freed and may be reused. |
| 62 | +-- Usage: buf = buf:reset() |
| 63 | +local function buffer_reset(self) |
| 64 | + self.buf:reset() |
| 65 | +end |
| 66 | + |
| 67 | +-- Appends the formatted arguments to the buffer. The format |
| 68 | +-- string supports the same options as `string.format()`. |
| 69 | +-- Usage: buf = buf:putf(format, ...) |
| 70 | +local function buffer_putf(self) -- luacheck: no unused |
| 71 | + local MAX_N = 1000 |
| 72 | + local str = self.fdp:consume_string(MAX_N) |
| 73 | + self.buf:putf("%s", str) |
| 74 | +end |
| 75 | + |
| 76 | +-- Appends the given len number of bytes from the memory pointed |
| 77 | +-- to by the FFI cdata object to the buffer. The object needs to |
| 78 | +-- be convertible to a (constant) pointer. |
| 79 | +-- Usage: buf = buf:putcdata(cdata, len) |
| 80 | +local function buffer_putcdata(self) -- luacheck: no unused |
| 81 | + local cdata = ffi.new("uint8_t[?]", 1) |
| 82 | + self.buf:putcdata(cdata, ffi.sizeof(cdata)) |
| 83 | +end |
| 84 | + |
| 85 | +-- This method allows zero-copy consumption of a string or an FFI |
| 86 | +-- cdata object as a buffer. It stores a reference to the passed |
| 87 | +-- string `str` or the FFI cdata object in the buffer. Any buffer |
| 88 | +-- space originally allocated is freed. This is not an append |
| 89 | +-- operation, unlike the buf:put*() methods. |
| 90 | +local function buffer_set(self) |
| 91 | + local MAX_N = 1000 |
| 92 | + local str = self.fdp:consume_string(MAX_N) |
| 93 | + self.buf:set(str) |
| 94 | +end |
| 95 | + |
| 96 | +-- Appends a string str, a number num or any object obj with |
| 97 | +-- a `__tostring` metamethod to the buffer. Multiple arguments are |
| 98 | +-- appended in the given order. Appending a buffer to a buffer is |
| 99 | +-- possible and short-circuited internally. But it still involves |
| 100 | +-- a copy. Better combine the buffer writes to use a single buffer. |
| 101 | +-- Usage: buf = buf:put([str | num | obj] [, ...]) |
| 102 | +local function buffer_put(self) |
| 103 | + local obj_type = self.fdp:oneof({ "string", "number" }) |
| 104 | + local MAX_COUNT = 10 |
| 105 | + local MAX_N = 1000 |
| 106 | + local count = self.fdp:consume_integer(0, MAX_COUNT) |
| 107 | + local objects |
| 108 | + if obj_type == "string" then |
| 109 | + objects = self.fdp:consume_strings(MAX_N, count) |
| 110 | + elseif obj_type == "number" then |
| 111 | + objects = self.fdp:consume_numbers( |
| 112 | + test_lib.MIN_INT64, test_lib.MAX_INT64, count) |
| 113 | + else |
| 114 | + assert(nil, "object type is unsupported") |
| 115 | + end |
| 116 | + local buf = self.buf:put(unpack(objects)) |
| 117 | + assert(type(buf) == "cdata") |
| 118 | +end |
| 119 | + |
| 120 | +-- Consumes the buffer data and returns one or more strings. If |
| 121 | +-- called without arguments, the whole buffer data is consumed. |
| 122 | +-- If called with a number, up to len bytes are consumed. A `nil` |
| 123 | +-- argument consumes the remaining buffer space (this only makes |
| 124 | +-- sense as the last argument). Multiple arguments consume the |
| 125 | +-- buffer data in the given order. |
| 126 | +-- Note: a zero length or no remaining buffer data returns an |
| 127 | +-- empty string and not nil. |
| 128 | +-- Usage: str, ... = buf:get([ len|nil ] [,...]) |
| 129 | +local function buffer_get(self) |
| 130 | + local MAX_N = 1000 |
| 131 | + local len = self.fdp:consume_integer(0, MAX_N) |
| 132 | + local str = self.buf:get(len) |
| 133 | + assert(type(str) == "string") |
| 134 | +end |
| 135 | + |
| 136 | +local function buffer_tostring(self) |
| 137 | + local str = self.buf:tostring() |
| 138 | + assert(type(str) == "string") |
| 139 | +end |
| 140 | + |
| 141 | +-- The commit method appends the `used` bytes of the previously |
| 142 | +-- returned write space to the buffer data. |
| 143 | +-- Usage: buf = buf:commit(used) |
| 144 | +local function buffer_commit(self) |
| 145 | + local MAX_N = 1000 |
| 146 | + local used = self.fdp:consume_integer(0, MAX_N) |
| 147 | + local _ = self.buf:commit(used) -- luacheck: no unused |
| 148 | +end |
| 149 | + |
| 150 | +-- The reserve method reserves at least `size` bytes of write |
| 151 | +-- space in the buffer. It returns an `uint8_t *` FFI cdata |
| 152 | +-- pointer `ptr` that points to this space. The space returned by |
| 153 | +-- `buf:reserve()` starts at the returned pointer and ends before |
| 154 | +-- len bytes after that. |
| 155 | +-- Usage: ptr, len = buf:reserve(size) |
| 156 | +local function buffer_reserve(self) |
| 157 | + local size = self.fdp:consume_integer(0, test_lib.MAX_INT) |
| 158 | + local ptr, len = self.buf:reserve(size) |
| 159 | + assert(type(ptr) == "number") |
| 160 | + assert(type(len) == "number") |
| 161 | +end |
| 162 | + |
| 163 | +-- Skips (consumes) `len` bytes from the buffer up to the current |
| 164 | +-- length of the buffer data. |
| 165 | +-- Usage: buf = buf:skip(len) |
| 166 | +local function buffer_skip(self) |
| 167 | + local len = self.fdp:consume_integer(0, test_lib.MAX_INT) |
| 168 | + local buf = self.buf:skip(len) |
| 169 | + assert(type(buf) == "cdata") |
| 170 | +end |
| 171 | + |
| 172 | +-- Returns an uint8_t * FFI cdata pointer ptr that points to the |
| 173 | +-- buffer data. The length of the buffer data in bytes is returned |
| 174 | +-- in `len`. The space returned by `buf:ref()` starts at the |
| 175 | +-- returned pointer and ends before len bytes after that. |
| 176 | +-- Synopsis: ptr, len = buf:ref() |
| 177 | +local function buffer_ref(self) |
| 178 | + local ptr, len = self.buf:ref() |
| 179 | + assert(type(ptr) == "number") |
| 180 | + assert(type(len) == "number") |
| 181 | +end |
| 182 | + |
| 183 | +-- Returns the current length of the buffer data in bytes. |
| 184 | +local function buffer_len(self) |
| 185 | + local len = #self.buf |
| 186 | + assert(type(len) == "number") |
| 187 | +end |
| 188 | + |
| 189 | +-- The Lua concatenation operator `..` also accepts buffers, just |
| 190 | +-- like strings or numbers. It always returns a string and not |
| 191 | +-- a buffer. |
| 192 | +local function buffer_concat(self) |
| 193 | + local MAX_N = 1000 |
| 194 | + local str = self.fdp:consume_string(0, MAX_N) |
| 195 | + local _ = self.buf .. str |
| 196 | +end |
| 197 | + |
| 198 | +-- Serializes (encodes) the Lua object `obj`. The stand-alone |
| 199 | +-- function returns a string `str`. The buffer method appends the |
| 200 | +-- encoding to the buffer. `obj` can be any of the supported Lua |
| 201 | +-- types - it doesn't need to be a Lua table. |
| 202 | +-- This function may throw an error when attempting to serialize |
| 203 | +-- unsupported object types, circular references or deeply nested |
| 204 | +-- tables. |
| 205 | +-- Usage: |
| 206 | +-- str = buffer.encode(obj) |
| 207 | +-- buf = buf:encode(obj) |
| 208 | +local function buffer_encode(self) |
| 209 | + local str = self.buf:encode() |
| 210 | + assert(type(str) == "string") |
| 211 | +end |
| 212 | + |
| 213 | +-- The stand-alone function deserializes (decodes) the string |
| 214 | +-- `str`, the buffer method deserializes one object from the |
| 215 | +-- buffer. Both return a Lua object `obj`. |
| 216 | +-- The returned object may be any of the supported Lua types - |
| 217 | +-- even nil. This function may throw an error when fed with |
| 218 | +-- malformed or incomplete encoded data. The stand-alone function |
| 219 | +-- throws when there's left-over data after decoding a single |
| 220 | +-- top-level object. The buffer method leaves any left-over data |
| 221 | +-- in the buffer. |
| 222 | +-- Attempting to deserialize an FFI type will throw an error, if |
| 223 | +-- the FFI library is not built-in or has not been loaded, yet. |
| 224 | +-- Usage: |
| 225 | +-- obj = buffer.decode(str) |
| 226 | +-- obj = buf:decode() |
| 227 | +local function buffer_decode(self) |
| 228 | + local MAX_N = 1000 |
| 229 | + local str = self.fdp:consume_string(0, MAX_N) |
| 230 | + local obj = self.buf:decode(str) |
| 231 | + assert(type(obj) == "cdata") |
| 232 | +end |
| 233 | + |
| 234 | +-- The buffer space of the buffer object is freed. The object |
| 235 | +-- itself remains intact, empty and may be reused. |
| 236 | +local function buffer_free(self) |
| 237 | + self.buf:free() |
| 238 | + assert(#self.buf == 0) |
| 239 | +end |
| 240 | + |
| 241 | +local buffer_methods = { |
| 242 | + buffer_commit, |
| 243 | + buffer_concat, |
| 244 | + buffer_decode, |
| 245 | + buffer_encode, |
| 246 | + buffer_get, |
| 247 | + buffer_len, |
| 248 | + buffer_put, |
| 249 | + buffer_putcdata, |
| 250 | + buffer_putf, |
| 251 | + buffer_ref, |
| 252 | + buffer_reserve, |
| 253 | + buffer_reset, |
| 254 | + buffer_set, |
| 255 | + buffer_skip, |
| 256 | + buffer_tostring, |
| 257 | +} |
| 258 | + |
| 259 | +local function buffer_random_op(self) |
| 260 | + local buffer_method= self.fdp:oneof(buffer_methods) |
| 261 | + buffer_method(self) |
| 262 | +end |
| 263 | + |
| 264 | +local function buffer_new(fdp) |
| 265 | + local buf_size = fdp:consume_number(1, test_lib.MAX_INT) |
| 266 | + local b = string_buf.new(buf_size) |
| 267 | + return { |
| 268 | + buf = b, |
| 269 | + fdp = fdp, |
| 270 | + free = buffer_free, |
| 271 | + random_operation = buffer_random_op, |
| 272 | + } |
| 273 | +end |
| 274 | + |
| 275 | +local function TestOneInput(buf, _size) |
| 276 | + local fdp = luzer.FuzzedDataProvider(buf) |
| 277 | + local nops = fdp:consume_number(1, test_lib.MAX_INT) |
| 278 | + local b = buffer_new(fdp) |
| 279 | + for _ = 1, nops do |
| 280 | + b:random_operation() |
| 281 | + end |
| 282 | + b:free() |
| 283 | +end |
| 284 | + |
| 285 | +local args = { |
| 286 | + artifact_prefix = "string_buffer_torture_", |
| 287 | +} |
| 288 | +luzer.Fuzz(TestOneInput, nil, args) |
0 commit comments