lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I have been having a problem using LuaJIT with userdata metatable
methods that raise an error at any point, such as when an argument is
invalid, being called in protected mode with debug.traceback as the
error handler. Below is an example of the program. With vanilla Lua,
the program operates as I expect. However, with LuaJIT, it crashes
unexpectedly at lj_err.c:126. I'm thinking that I'm doing something
wrong, but after futile searches I can't figure out what that
something is. So my question is, what is wrong with the code below?

Program:

#include <luajit/lua.h>
#include <luajit/lauxlib.h>
#include <luajit/lualib.h>

int func(lua_State * L)
{
	luaL_error(L, "foo");
}

int main(void)
{
	lua_State * L = luaL_newstate();
	
	lua_newuserdata(L, 1);
	
	lua_newtable(L);
	lua_pushcfunction(L, &func);
	lua_setfield(L, -2, "__add");
	
	lua_setmetatable(L, -2);
	lua_setglobal(L, "foo");
	
	luaL_openlibs(L);
	
	while(1)
	{
		luaL_dostring(L, "xpcall(function() return foo + foo end, debug.traceback)");
	}
	
	return 0;
}

GDB information when it crashes:

Program received signal SIGSEGV, Segmentation fault.
0x66d85b29 in currentline (L=0x346da8, fn=0x3438b8, nextframe=0x3411e8)
    at lj_err.c:126
126         return proto_line(pt, pc);
(gdb) bt
#0  0x66d85b29 in currentline (L=0x346da8, fn=0x3438b8, nextframe=0x3411e8)
    at lj_err.c:126
#1  0x003411a8 in ?? ()
#2  0x05010662 in ?? ()
#3  0x003401f0 in ?? ()
#4  0x00000c02 in ?? ()
#5  0x003412f0 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb) print pt
$1 = (GCproto *) 0x346da8
(gdb) print pc
$2 = 4294108315
(gdb) print *pt
$3 = {nextgc = {gcptr32 = 1}, marked = 1 '\001', gct = 0 '\000',
  numparams = 0 '\000', framesize = 0 '\000', sizebc = 1, gclist = {
    gcptr32 = 1}, k = {ptr32 = 1}, uv = {ptr32 = 3472648}, sizekgc = 120,
  sizekn = 43, sizept = 0, sizeuv = 2 '\002', flags = 4 '\004', trace = 0,
  chunkname = {gcptr32 = 3971933946}, lastlinedefined = 16,
  sizevarinfo = 1667331187, varinfo = {ptr32 = 1920213099}, uvname = {
    ptr32 = 1650811745}, lineinfo = {ptr32 = 980116321}}