Skip to content

Commit 3914471

Browse files
committed
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 3914471

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-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: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,13 @@ 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 fdp_metatable_init(lua_State *L)
266270
{
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-
274271
luaL_newmetatable(L, FDP_LUA_UDATA_NAME);
275272
lua_pushvalue(L, -1);
276273
lua_setfield(L, -2, "__index");
@@ -279,6 +276,18 @@ luaL_fuzzed_data_provider(lua_State *L)
279276
#else
280277
luaL_setfuncs(L, methods, 0);
281278
#endif
279+
lua_pop(L, 1); /* Remove the metatable from the stack. */
280+
}
281+
282+
int
283+
luaL_fuzzed_data_provider(lua_State *L)
284+
{
285+
int index = lua_gettop(L);
286+
if (index != 1)
287+
luaL_error(L, "Usage: luzer.FuzzedDataProvider(string)");
288+
289+
const char *data = luaL_checkstring(L, 1);
290+
size_t size = strlen(data);
282291

283292
lua_userdata_t *lfdp;
284293
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)