lua-users home
lua-l archive

Hacking Lua - Calling luaL_openlib to register additional functions

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


Hi!
[sorry if this email comes twice but I did not get the last one I sent 6 hours ago] I've managed to get control about what's happening as Lua is initializing it's baselib. What I do is I hook up in base_open (in lbaselib.c) and call one of my functions before base_open it returns.
This is what's happening if I translate my actions in plain C code:
static void base_open (lua_State *L) {
 lua_pushliteral(L, "_G");
 lua_pushvalue(L, LUA_GLOBALSINDEX);
 luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */
 // some more calls
 lua_rawset(L, -3); /* set global `newproxy' */
 lua_rawset(L, -1); /* set global _G */
// The above lines are "original" as is in Lua 5.0.1; Here comes the hack: // Essentially what I do is stealing the pointer to L, run a function in a DLL // which I injected first and then use this pointer to call luaL_openlib again // but this time with extensions_funcs which is just a list of "name/address" pairs
 // terminated by {NULL,NULL} as requested which I want to be registered
 luaL_openlib(L, NULL, extension_funcs, 0);
}
I uploaded an image [1] to show you what this part in my DLL looks like.
The problem I am currently facing is that this is crashing in "markroot" saying:
Exception thrown: read access violation.
L->l_G->_defaultmeta.value.gc was nullptr.
If you look at the screenshot you see that the luaL_state object actually does look a bit strange which might suggest that I push the wrong pointer onto the stack for my "initialize" function, however, I've checked it multiple times and I am very certain that I am pushing the same address that for L that is getting pushed for the "original" calls as well. I don't know if anybody can help me here. Looking at the values of L, can anybody tell me if this struct is looking "healthy"? To me it does not but I have no idea what I should expect. An answer to my question could be, that I am actually not allowed to call luaL_openlib() at this point and that I might break stuff. If that is so: where should I call it? Or maybe: Am I doing it wrong anyway? All I want to do is register new built-in functions for Lua - am I doing it right actually?
I really hope somebody can give some advice - thanks in advance!
[1] https://s24.postimg.org/dr837uead/Capture.png

AltStyle によって変換されたページ (->オリジナル) /