Dibyendu Majumdar
2014-08-24 16:37:26 UTC
Hi,
On 32-bit Mac OS X (10.6.8) I am getting segmentation fault (details
below). I can try to create a test case, but was wondering if anyone would
recognise this as an issue with my code.
The stack trace is:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0xffffffff0000000b
0x00000000000940d9 in mainposition ()
(gdb) where
#0 0x00000000000940d9 in mainposition ()
#1 0x00000000000946c6 in luaH_get ()
#2 0x000000000008751a in lua_rawget ()
#3 0x000000000022df52 in my_newmetatable ()
Both 2.0.3 and 2.1.0 get the same fault. I used versions built by HomeBrew
as well as ones I built myself using standard Makefile.
I do not get this fault with LuaJIT 2.1 on Win64.
I do not get this fault in PUC Lua.
The code that is leading to segmentation fault is given below.
Many thanks
// The normal Lua metatable functions in C use string
// keys - these are expensive as the key needs to be
// converted to Lua string, hash code computed etc.
// Following implementations are taken from a post in
// Lua mailing list (http://lua-users.org/lists/lua-l/2010-11/msg00151.html)
// They use lightuserdata instead of strings to speed
// things up
// meta_key is the key assigned to the meta table of the userdata
static int my_newmetatable(lua_State *L, void *meta_key) {
lua_pushlightuserdata(L,meta_key);
lua_rawget(L, LUA_REGISTRYINDEX);
if (!lua_isnil(L, -1)) // name already in use?
return 0; // leave previous value on top, but
return 0
lua_pop(L, 1); // pop the nil value
lua_newtable(L); // create metatable
lua_pushlightuserdata(L, meta_key); // meta_key
lua_pushvalue(L, -2); // table
lua_rawset(L, LUA_REGISTRYINDEX); // assign table to meta_key in the
registry
return 1;
}
// meta_key is the key assigned to the meta table of the userdata
static void my_getmetatable (lua_State *L, void *meta_key) {
lua_pushlightuserdata(L, meta_key); // meta_key
lua_rawget(L, LUA_REGISTRYINDEX); // obtain the value associated with
meta_key from registry
}
// arg_index is the position of userdata argument on the stack
// meta_key is the key assigned to the meta table of the userdata
static void *my_testudata (lua_State *L, int arg_index, void *meta_key) {
void *p = lua_touserdata(L, arg_index);
if (p != NULL) { // value is a userdata?
if (lua_getmetatable(L, arg_index)) { // does it have a metatable?
lua_pushlightuserdata(L, meta_key); // meta_key
lua_rawget(L, LUA_REGISTRYINDEX); // get correct metatable
associated with meta_key
if (lua_rawequal(L, -1, -2)) { // compare: does it have the
correct mt?
lua_pop(L, 2); // remove both metatables
return p; // test ok
}
}
}
return NULL; /* to avoid warnings */
}
// arg_index is the position of userdata argument on the stack
// meta_key is the key assigned to the meta table of the userdata
static void *my_checkudata(lua_State *L, int arg_index, void *meta_key)
{
void *p = my_testudata(L, arg_index, meta_key);
if (p == nullptr)
luaL_argerror(L, arg_index, (const char *)meta_key);
return p;
}
On 32-bit Mac OS X (10.6.8) I am getting segmentation fault (details
below). I can try to create a test case, but was wondering if anyone would
recognise this as an issue with my code.
The stack trace is:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0xffffffff0000000b
0x00000000000940d9 in mainposition ()
(gdb) where
#0 0x00000000000940d9 in mainposition ()
#1 0x00000000000946c6 in luaH_get ()
#2 0x000000000008751a in lua_rawget ()
#3 0x000000000022df52 in my_newmetatable ()
Both 2.0.3 and 2.1.0 get the same fault. I used versions built by HomeBrew
as well as ones I built myself using standard Makefile.
I do not get this fault with LuaJIT 2.1 on Win64.
I do not get this fault in PUC Lua.
The code that is leading to segmentation fault is given below.
Many thanks
// The normal Lua metatable functions in C use string
// keys - these are expensive as the key needs to be
// converted to Lua string, hash code computed etc.
// Following implementations are taken from a post in
// Lua mailing list (http://lua-users.org/lists/lua-l/2010-11/msg00151.html)
// They use lightuserdata instead of strings to speed
// things up
// meta_key is the key assigned to the meta table of the userdata
static int my_newmetatable(lua_State *L, void *meta_key) {
lua_pushlightuserdata(L,meta_key);
lua_rawget(L, LUA_REGISTRYINDEX);
if (!lua_isnil(L, -1)) // name already in use?
return 0; // leave previous value on top, but
return 0
lua_pop(L, 1); // pop the nil value
lua_newtable(L); // create metatable
lua_pushlightuserdata(L, meta_key); // meta_key
lua_pushvalue(L, -2); // table
lua_rawset(L, LUA_REGISTRYINDEX); // assign table to meta_key in the
registry
return 1;
}
// meta_key is the key assigned to the meta table of the userdata
static void my_getmetatable (lua_State *L, void *meta_key) {
lua_pushlightuserdata(L, meta_key); // meta_key
lua_rawget(L, LUA_REGISTRYINDEX); // obtain the value associated with
meta_key from registry
}
// arg_index is the position of userdata argument on the stack
// meta_key is the key assigned to the meta table of the userdata
static void *my_testudata (lua_State *L, int arg_index, void *meta_key) {
void *p = lua_touserdata(L, arg_index);
if (p != NULL) { // value is a userdata?
if (lua_getmetatable(L, arg_index)) { // does it have a metatable?
lua_pushlightuserdata(L, meta_key); // meta_key
lua_rawget(L, LUA_REGISTRYINDEX); // get correct metatable
associated with meta_key
if (lua_rawequal(L, -1, -2)) { // compare: does it have the
correct mt?
lua_pop(L, 2); // remove both metatables
return p; // test ok
}
}
}
return NULL; /* to avoid warnings */
}
// arg_index is the position of userdata argument on the stack
// meta_key is the key assigned to the meta table of the userdata
static void *my_checkudata(lua_State *L, int arg_index, void *meta_key)
{
void *p = my_testudata(L, arg_index, meta_key);
if (p == nullptr)
luaL_argerror(L, arg_index, (const char *)meta_key);
return p;
}