lua-users home
lua-l archive

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

On 18 June 2011 18:53, Rebel Neurofog <> wrote:
>> There's a way to attach a pointer (possibly pointing to structure of
>> several things you will actually use) to lua_State
>> by passing it to lua_newstate () or by setting it with lua_setallocf ().
> Actually I think the correct method for this is to set LUAI_EXTRASPACE in
> luaconf.h

2. The feature is per-coroutine rather than per global_State.
So using LUAI_EXTRASPACE would lead to unnecessary allocations and
access to main thread's state like ((void*) (G(L)->mainthread) -
sizeof (void*)).
Looks like Spartaaaaaa!!!!!!!!!!111...

It is still possible to do this at runtime although I would not recommend it, rather I will just give the evil information. From looking at the source we know that when the allocator is called for the creation of an LG then the state pointer in the allocator is NULL so you can set an allocator and identify this situation adding add extra bytes to the front or back of LG. When the state is closing the L and ptr will be the same.
Here is some output of a runtime hooked version of a Lua program.

inside executable ./lua_hookme
[HOOOKED INFO] lua_newstate_mem_hook
[HOOOKED INFO] Lua 0x0 alloc 0x1001000b0 size 616 byte count: 616
before lua_close
[HOOOKED INFO] lua_close_hook       state: 0x1001000b0
[HOOOKED INFO] state 0x1001000b0 cleaned and no leaks
[HOOOKED INFO] Lua 0x1001000b0 free 0x1001000b0 size 616 byte count: 0
[HOOOKED INFO] after hooked function lua_close_hook
after lua_close