Discussion:
Getting userdata from payload address
Tim Caswell
2014-09-12 19:06:28 UTC
Permalink
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?
Tim Caswell
2014-09-12 20:32:18 UTC
Permalink
Nevermind. I need something portable across lua engines and just found the
weak table idea at http://www.lua.org/pil/28.5.html. That looks much
safer. Sorry for the noise.
Post by Tim Caswell
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?
Loading...