Skip to content

Commit ddee305

Browse files
committed
tests/lapi: add string.buffer tests
The patch add tests for LuaJIT's string buffer library [1]. Note, as it is stated in documentation [1] this serialization format is designed for internal use by LuaJIT applications, and this format is explicitly not intended to be a 'public standard' for structured data interchange across computer languages (like JSON or MessagePack). The purpose of the proposed tests is testing the library because other LuaJIT components relies on it and also the proposed tests indirectly tests FFI library. 1. https://luajit.org/ext_buffer.html
1 parent ec022d6 commit ddee305

File tree

2 files changed

+345
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)