Skip to content

Commit 59877a2

Browse files
Buristanligurio
authored andcommitted
luzer: initialize FDP metatable only once
The FDP metatable is recreating each time when we initialize the FDP object. It increases the GC consumption and makes the code less JIT-friendly since traces are exited by the guard on the FDP methods objects that checked the exact pointer of the `GCobj` which is new for any new FDP provider. This patch fixes that by initializing the metatable only once on the luzer library loading.
1 parent ac580ef commit 59877a2

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2828
- Stack overflow due to recursive traceback calls.
2929
- Fix a crash due to incorrect `argv` building (#13).
3030
- Fix parsing command-line flags (#23).
31+
- Multiple initialization of the FDP metatable.

luzer/fuzzed_data_provider.cc

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,14 @@ const luaL_Reg methods[] =
261261
{ NULL, NULL }
262262
};
263263

264-
int
265-
luaL_fuzzed_data_provider(lua_State *L)
264+
/*
265+
* Create the metatable once on the luzer loading to be more GC and JIT
266+
* friendly. `luaL_fuzzed_data_provider()` is called in the loop inside
267+
* `LLVMFuzzerRunDriver()`.
268+
*/
269+
void
270+
fdp_metatable_init(lua_State *L)
266271
{
267-
int index = lua_gettop(L);
268-
if (index != 1)
269-
luaL_error(L, "Usage: luzer.FuzzedDataProvider(string)");
270-
271-
const char *data = luaL_checkstring(L, 1);
272-
size_t size = strlen(data);
273-
274272
luaL_newmetatable(L, FDP_LUA_UDATA_NAME);
275273
lua_pushvalue(L, -1);
276274
lua_setfield(L, -2, "__index");
@@ -279,6 +277,18 @@ luaL_fuzzed_data_provider(lua_State *L)
279277
#else
280278
luaL_setfuncs(L, methods, 0);
281279
#endif
280+
lua_pop(L, 1); /* Remove the metatable from the stack. */
281+
}
282+
283+
int
284+
luaL_fuzzed_data_provider(lua_State *L)
285+
{
286+
int index = lua_gettop(L);
287+
if (index != 1)
288+
luaL_error(L, "Usage: luzer.FuzzedDataProvider(string)");
289+
290+
const char *data = luaL_checkstring(L, 1);
291+
size_t size = strlen(data);
282292

283293
lua_userdata_t *lfdp;
284294
lfdp = (lua_userdata_t*)lua_newuserdata(L, sizeof(*lfdp));

luzer/fuzzed_data_provider.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#ifdef __cplusplus
55
extern "C" {
66
#endif
7+
void fdp_metatable_init(lua_State *L);
78
int luaL_fuzzed_data_provider(lua_State *L);
89
#ifdef __cplusplus
910
} /* extern "C" */

luzer/luzer.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,5 +462,7 @@ int luaopen_luzer_impl(lua_State *L)
462462
lua_pushstring(L, LUA_RELEASE);
463463
lua_rawset(L, -3);
464464

465+
fdp_metatable_init(L);
466+
465467
return 1;
466468
}

0 commit comments

Comments
 (0)