Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workaround for LuaJIT not supporting >47bit pointers #225

Merged
merged 1 commit into from Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/cqueues.c
Expand Up @@ -1875,7 +1875,7 @@ static void thread_add(lua_State *L, struct cqueue *Q, struct callinfo *I, int i
/* anchor thread context to cqueue object */
cqs_getuservalue(L, I->self);
lua_pushvalue(L, -2);
lua_rawsetp(L, -2, T);
lua_rawsetp(L, -2, CQS_UNIQUE_LIGHTUSERDATA_MASK(T));
lua_pop(L, 2);

LIST_INSERT_HEAD(&Q->thread.pending, T, le);
Expand Down Expand Up @@ -1906,15 +1906,15 @@ static void thread_del(lua_State *L, struct cqueue *Q, struct callinfo *I, struc
cqs_getuservalue(L, I->self);

/* set thread's uservalue (it's thread) to nil */
lua_rawgetp(L, -1, T);
lua_rawgetp(L, -1, CQS_UNIQUE_LIGHTUSERDATA_MASK(T));
lua_pushnil(L);
cqs_setuservalue(L, -2);
lua_pop(L, 1);
T->L = NULL;

/* remove thread from cqueues's thread table */
lua_pushnil(L);
lua_rawsetp(L, -2, T);
lua_rawsetp(L, -2, CQS_UNIQUE_LIGHTUSERDATA_MASK(T));
lua_pop(L, 1);
} /* thread_del() */

Expand Down Expand Up @@ -2717,7 +2717,7 @@ static struct cstack *cstack_self(lua_State *L) {
static const int index = 47;
struct cstack *CS;

lua_rawgetp(L, LUA_REGISTRYINDEX, &index);
lua_rawgetp(L, LUA_REGISTRYINDEX, CQS_UNIQUE_LIGHTUSERDATA_MASK(&index));

CS = lua_touserdata(L, -1);

Expand All @@ -2731,7 +2731,7 @@ static struct cstack *cstack_self(lua_State *L) {

LIST_INIT(&CS->cqueues);

lua_rawsetp(L, LUA_REGISTRYINDEX, &index);
lua_rawsetp(L, LUA_REGISTRYINDEX, CQS_UNIQUE_LIGHTUSERDATA_MASK(&index));

return CS;
} /* cstack_self() */
Expand Down
18 changes: 17 additions & 1 deletion src/cqueues.h
Expand Up @@ -118,7 +118,23 @@
#define CQS_NOTIFY "CQS Notify"
#define CQS_CONDITION "CQS Condition"

#define CQUEUE__POLL ((void *)&cqueue__poll)
#ifndef CQS_USE_47BIT_LIGHTUSERDATA_HACK
/* LuaJIT only supports pointers with the low 47 bits set */
#if defined(LUA_JITLIBNAME) && (defined(_LP64) || defined(_LLP64) || defined(__arch64__) || defined (__arm64__) || defined (__aarch64__) || defined(_WIN64))
#define CQS_USE_47BIT_LIGHTUSERDATA_HACK 1
#else
#define CQS_USE_47BIT_LIGHTUSERDATA_HACK 0
#endif
#endif

#if CQS_USE_47BIT_LIGHTUSERDATA_HACK
#define CQS_UNIQUE_LIGHTUSERDATA_MASK(p) ((void *)((intptr_t)(p) & ((1UL<<47)-1)))
#else
#define CQS_UNIQUE_LIGHTUSERDATA_MASK(p) ((void *)(p))
#endif

#define CQUEUE__POLL CQS_UNIQUE_LIGHTUSERDATA_MASK(&cqueue__poll)

const char *cqueue__poll; // signals multilevel yield

cqs_nargs_t luaopen__cqueues(lua_State *);
Expand Down
6 changes: 3 additions & 3 deletions src/socket.c
Expand Up @@ -615,11 +615,11 @@ static struct so_options lso_checkopts(lua_State *L, int index) {
luaL_argcheck(L, path != NULL || addr != NULL, index, "no bind address specified");

if (path) {
sa = lso_singleton(L, &regindex, NULL, sizeof(struct sockaddr_un));
sa = lso_singleton(L, CQS_UNIQUE_LIGHTUSERDATA_MASK(&regindex), NULL, sizeof(struct sockaddr_un));
sa->sa_family = AF_UNIX;
memcpy(((struct sockaddr_un*)sa)->sun_path, path, MIN(plen, sizeof(((struct sockaddr_un*)sa)->sun_path)));
} else {
sa = lso_singleton(L, &regindex, NULL, sizeof(struct sockaddr_storage));
sa = lso_singleton(L, CQS_UNIQUE_LIGHTUSERDATA_MASK(&regindex), NULL, sizeof(struct sockaddr_storage));
if (!sa_pton(sa, sizeof(struct sockaddr_storage), addr, NULL, &error))
luaL_argerror(L, index, lua_pushfstring(L, "%s: unable to parse bind address (%s)", addr, cqs_strerror(error)));

Expand Down Expand Up @@ -810,7 +810,7 @@ static void lso_pushmode(lua_State *L, int mode, int mask, _Bool libc) {
static struct luasocket *lso_prototype(lua_State *L) {
static const int regindex;

return lso_singleton(L, &regindex, &lso_initializer, sizeof lso_initializer);
return lso_singleton(L, CQS_UNIQUE_LIGHTUSERDATA_MASK(&regindex), &lso_initializer, sizeof lso_initializer);
} /* lso_prototype() */


Expand Down
4 changes: 2 additions & 2 deletions src/thread.c
Expand Up @@ -370,7 +370,7 @@ static void *ct_enter(void *arg) {
*ud = ct;

lua_pushvalue(L, -1);
lua_rawsetp(L, LUA_REGISTRYINDEX, &selfindex);
lua_rawsetp(L, LUA_REGISTRYINDEX, CQS_UNIQUE_LIGHTUSERDATA_MASK(&selfindex));

if ((error = cqs_socket_fdopen(L, ct->tmp.fd[1], NULL)))
goto error;
Expand Down Expand Up @@ -797,7 +797,7 @@ static int ct_interpose(lua_State *L) {


static int ct_self(lua_State *L) {
lua_rawgetp(L, LUA_REGISTRYINDEX, &selfindex);
lua_rawgetp(L, LUA_REGISTRYINDEX, CQS_UNIQUE_LIGHTUSERDATA_MASK(&selfindex));

return 1;
} /* ct_self() */
Expand Down