Skip to content

Commit 0a0d9b0

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 0a0d9b0

File tree

2 files changed

+335
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)