Avoid using addresses of static variables as unique keys

The addresses of static variables may be different for different
instances of Lua, making these instances incompatible if they use
these addresses as unique keys in the registry (or other tables).
This commit is contained in:
Roberto Ierusalimschy 2019-07-19 13:14:06 -03:00
parent 440a5ee78c
commit 3c0d3c6fbe
3 changed files with 17 additions and 16 deletions

View File

@ -21,10 +21,10 @@
/* /*
** The hook table at registry[&HOOKKEY] maps threads to their current ** The hook table at registry[HOOKKEY] maps threads to their current
** hook function. (We only need the unique address of 'HOOKKEY'.) ** hook function.
*/ */
static const int HOOKKEY = 0; static const char* HOOKKEY = "_HOOKKEY";
/* /*
@ -314,7 +314,7 @@ static int db_upvaluejoin (lua_State *L) {
static void hookf (lua_State *L, lua_Debug *ar) { static void hookf (lua_State *L, lua_Debug *ar) {
static const char *const hooknames[] = static const char *const hooknames[] =
{"call", "return", "line", "count", "tail call"}; {"call", "return", "line", "count", "tail call"};
lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
lua_pushthread(L); lua_pushthread(L);
if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
@ -367,14 +367,12 @@ static int db_sethook (lua_State *L) {
count = (int)luaL_optinteger(L, arg + 3, 0); count = (int)luaL_optinteger(L, arg + 3, 0);
func = hookf; mask = makemask(smask, count); func = hookf; mask = makemask(smask, count);
} }
if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) { if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) {
lua_createtable(L, 0, 2); /* create a hook table */ /* table just created; initialize it */
lua_pushvalue(L, -1);
lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */
lua_pushstring(L, "k"); lua_pushstring(L, "k");
lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
lua_pushvalue(L, -1); lua_pushvalue(L, -1);
lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */
} }
checkstack(L, L1, 1); checkstack(L, L1, 1);
lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */ lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
@ -396,7 +394,7 @@ static int db_gethook (lua_State *L) {
else if (hook != hookf) /* external hook? */ else if (hook != hookf) /* external hook? */
lua_pushliteral(L, "external hook"); lua_pushliteral(L, "external hook");
else { /* hook table must exist */ else { /* hook table must exist */
lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
checkstack(L, L1, 1); checkstack(L, L1, 1);
lua_pushthread(L1); lua_xmove(L1, L, 1); lua_pushthread(L1); lua_xmove(L1, L, 1);
lua_rawget(L, -2); /* 1st result = hooktable[L1] */ lua_rawget(L, -2); /* 1st result = hooktable[L1] */

View File

@ -56,10 +56,10 @@
/* /*
** unique key for table in the registry that keeps handles ** key for table in the registry that keeps handles
** for all loaded C libraries ** for all loaded C libraries
*/ */
static const int CLIBS = 0; static const char *CLIBS = "_CLIBS";
#define LIB_FAIL "open" #define LIB_FAIL "open"
@ -327,7 +327,7 @@ static void setpath (lua_State *L, const char *fieldname,
*/ */
static void *checkclib (lua_State *L, const char *path) { static void *checkclib (lua_State *L, const char *path) {
void *plib; void *plib;
lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS); lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
lua_getfield(L, -1, path); lua_getfield(L, -1, path);
plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */ plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
lua_pop(L, 2); /* pop CLIBS table and 'plib' */ lua_pop(L, 2); /* pop CLIBS table and 'plib' */
@ -340,7 +340,7 @@ static void *checkclib (lua_State *L, const char *path) {
** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries ** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries
*/ */
static void addtoclib (lua_State *L, const char *path, void *plib) { static void addtoclib (lua_State *L, const char *path, void *plib) {
lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS); lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
lua_pushlightuserdata(L, plib); lua_pushlightuserdata(L, plib);
lua_pushvalue(L, -1); lua_pushvalue(L, -1);
lua_setfield(L, -3, path); /* CLIBS[path] = plib */ lua_setfield(L, -3, path); /* CLIBS[path] = plib */
@ -716,12 +716,11 @@ static void createsearcherstable (lua_State *L) {
** setting a finalizer to close all libraries when closing state. ** setting a finalizer to close all libraries when closing state.
*/ */
static void createclibstable (lua_State *L) { static void createclibstable (lua_State *L) {
lua_newtable(L); /* create CLIBS table */ luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */
lua_createtable(L, 0, 1); /* create metatable for CLIBS */ lua_createtable(L, 0, 1); /* create metatable for CLIBS */
lua_pushcfunction(L, gctm); lua_pushcfunction(L, gctm);
lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */ lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS); /* set CLIBS table in registry */
} }

View File

@ -255,6 +255,10 @@ do -- test hook presence in debug info
end end
-- hook table has weak keys
assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k')
a = {}; L = nil a = {}; L = nil
local glob = 1 local glob = 1
local oldglob = glob local oldglob = glob