Tim Caswell
2014-09-12 19:06:28 UTC
I'm updating my lua libuv bindings and was wondering if it was possible to
get a userdata from nothing more than the payload address. Looking at the
source to GCudata this should be possible using a pointer offset trick like
the container_of macro in the linux kernel sources. I just need to
subtract the size of GCudata from my payload address to get the address of
GCudata. (assuming my pointer was allocated using newuserdata).
Basically I want the inverse of luaL_checkudata. Let's call it pushudata.
int pushudata(lua_State* L, void* payload, const char* tname)
It takes the payload, subtracts the size of GCudata to get the possible
start of GCudata, then it checks against the tname.
What I'm unsure about is how to defend against payload addresses that don't
belong to live GCudata instances Would it just segfault in this case?
get a userdata from nothing more than the payload address. Looking at the
source to GCudata this should be possible using a pointer offset trick like
the container_of macro in the linux kernel sources. I just need to
subtract the size of GCudata from my payload address to get the address of
GCudata. (assuming my pointer was allocated using newuserdata).
Basically I want the inverse of luaL_checkudata. Let's call it pushudata.
int pushudata(lua_State* L, void* payload, const char* tname)
It takes the payload, subtracts the size of GCudata to get the possible
start of GCudata, then it checks against the tname.
What I'm unsure about is how to defend against payload addresses that don't
belong to live GCudata instances Would it just segfault in this case?