Skip to content

Commit 36e5ef8

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 bfac9d7 commit 36e5ef8

File tree

2 files changed

+339
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)