Skip to content

Commit

Permalink
Preventing use of lightuserdata() due to the VA Bits limitation in Lu…
Browse files Browse the repository at this point in the history
…aJIT

Issue seen on ARM64. Refer Bug openresty#1152
  • Loading branch information
debayang committed May 4, 2018
1 parent 5a3c48d commit 543a722
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 149 deletions.
35 changes: 26 additions & 9 deletions src/ngx_http_lua_bodyfilterby.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ static void ngx_http_lua_body_filter_by_lua_env(lua_State *L,
ngx_http_request_t *r, ngx_chain_t *in);
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;

/* Wrapper for ngx_chain_t ptr */
typedef struct {
ngx_chain_t *wr;
} ngx_chain_ptr_wrap_t;

/* key for the ngx_chain_t *in pointer in the Lua thread */
#define ngx_http_lua_chain_key "__ngx_cl"
Expand All @@ -54,7 +58,9 @@ ngx_http_lua_body_filter_by_lua_env(lua_State *L, ngx_http_request_t *r,
/* set nginx request pointer to current lua thread's globals table */
ngx_http_lua_set_req(L, r);

lua_pushlightuserdata(L, in);
ngx_chain_ptr_wrap_t* w = (ngx_chain_ptr_wrap_t*) lua_newuserdata (L, sizeof(void*));
w->wr = in;

lua_setglobal(L, ngx_http_lua_chain_key);

/**
Expand Down Expand Up @@ -302,7 +308,9 @@ ngx_http_lua_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
L = ngx_http_lua_get_lua_vm(r, ctx);

lua_getglobal(L, ngx_http_lua_chain_key);
out = lua_touserdata(L, -1);
ngx_chain_ptr_wrap_t* w = (ngx_chain_ptr_wrap_t*) lua_touserdata(L, -1);
out = w->wr;

lua_pop(L, 1);

if (in == out) {
Expand Down Expand Up @@ -364,7 +372,8 @@ ngx_http_lua_body_filter_param_get(lua_State *L)
}

lua_getglobal(L, ngx_http_lua_chain_key);
in = lua_touserdata(L, -1);
ngx_chain_ptr_wrap_t* w = (ngx_chain_ptr_wrap_t*) lua_touserdata(L, -1);
in = w->wr;

if (idx == 2) {
/* asking for the eof argument */
Expand Down Expand Up @@ -455,8 +464,10 @@ ngx_http_lua_body_filter_param_set(lua_State *L, ngx_http_request_t *r,
last = lua_toboolean(L, 3);

lua_getglobal(L, ngx_http_lua_chain_key);
in = lua_touserdata(L, -1);
lua_pop(L, 1);
ngx_chain_ptr_wrap_t* w = (ngx_chain_ptr_wrap_t*) lua_touserdata(L, -1);
in = w->wr;

lua_pop(L, 1);

if (last) {
ctx->seen_last_in_filter = 1;
Expand Down Expand Up @@ -522,8 +533,10 @@ ngx_http_lua_body_filter_param_set(lua_State *L, ngx_http_request_t *r,
/* discard the buffers */

lua_getglobal(L, ngx_http_lua_chain_key); /* key val */
in = lua_touserdata(L, -1);
lua_pop(L, 1);
ngx_chain_ptr_wrap_t* w = (ngx_chain_ptr_wrap_t*) lua_touserdata(L, -1);
in = w->wr;

lua_pop(L, 1);

last = 0;

Expand Down Expand Up @@ -558,7 +571,9 @@ ngx_http_lua_body_filter_param_set(lua_State *L, ngx_http_request_t *r,
}

lua_getglobal(L, ngx_http_lua_chain_key);
in = lua_touserdata(L, -1);
ngx_chain_ptr_wrap_t* w = (ngx_chain_ptr_wrap_t*) lua_touserdata(L, -1);
in = w->wr;

lua_pop(L, 1);

last = 0;
Expand Down Expand Up @@ -625,7 +640,9 @@ ngx_http_lua_body_filter_param_set(lua_State *L, ngx_http_request_t *r,
}
}

lua_pushlightuserdata(L, cl);

w = (ngx_chain_ptr_wrap_t*) lua_newuserdata (L, sizeof(void*));
w->wr = cl;
lua_setglobal(L, ngx_http_lua_chain_key);
return 0;
}
Expand Down
6 changes: 2 additions & 4 deletions src/ngx_http_lua_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ ngx_http_lua_cache_load_code(ngx_log_t *log, lua_State *L,
u_char *err;

/* get code cache table */
lua_pushlightuserdata(L, &ngx_http_lua_code_cache_key);
lua_rawget(L, LUA_REGISTRYINDEX); /* sp++ */
lua_rawgeti(L, LUA_REGISTRYINDEX, ngx_http_lua_code_cache_key_ref);

dd("Code cache table to load: %p", lua_topointer(L, -1));

Expand Down Expand Up @@ -105,8 +104,7 @@ ngx_http_lua_cache_store_code(lua_State *L, const char *key)
int rc;

/* get code cache table */
lua_pushlightuserdata(L, &ngx_http_lua_code_cache_key);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_rawgeti(L, LUA_REGISTRYINDEX, ngx_http_lua_code_cache_key_ref);

dd("Code cache table to store: %p", lua_topointer(L, -1));

Expand Down
4 changes: 1 addition & 3 deletions src/ngx_http_lua_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,7 @@ ngx_http_lua_on_abort(lua_State *L)
}

ngx_http_lua_coroutine_create_helper(L, r, ctx, &coctx);

lua_pushlightuserdata(L, &ngx_http_lua_coroutines_key);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_rawgeti(L, LUA_REGISTRYINDEX, ngx_http_lua_coroutines_key_ref);
lua_pushvalue(L, -2);

dd("on_wait thread 1: %p", lua_tothread(L, -1));
Expand Down
10 changes: 3 additions & 7 deletions src/ngx_http_lua_headers.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,7 @@ ngx_http_lua_ngx_req_get_headers(lua_State *L)
lua_createtable(L, 0, count);

if (!raw) {
lua_pushlightuserdata(L, &ngx_http_lua_headers_metatable_key);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_rawgeti(L, LUA_REGISTRYINDEX, ngx_http_lua_headers_metatable_key_ref);
lua_setmetatable(L, -2);
}

Expand Down Expand Up @@ -566,8 +565,7 @@ ngx_http_lua_ngx_resp_get_headers(lua_State *L)
lua_createtable(L, 0, count + 2);

if (!raw) {
lua_pushlightuserdata(L, &ngx_http_lua_headers_metatable_key);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_rawgeti(L, LUA_REGISTRYINDEX, ngx_http_lua_headers_metatable_key_ref);
lua_setmetatable(L, -2);
}

Expand Down Expand Up @@ -1081,11 +1079,10 @@ ngx_http_lua_create_headers_metatable(ngx_log_t *log, lua_State *L)
"local new_key = string.gsub(string.lower(key), '_', '-')\n"
"if new_key ~= key then return tb[new_key] else return nil end";

lua_pushlightuserdata(L, &ngx_http_lua_headers_metatable_key);

/* metatable for ngx.req.get_headers(_, true) and
* ngx.resp.get_headers(_, true) */
lua_createtable(L, 0, 1);
ngx_http_lua_headers_metatable_key_ref = luaL_ref(L, LUA_REGISTRYINDEX);

rc = luaL_loadbuffer(L, buf, sizeof(buf) - 1, "=headers metatable");
if (rc != 0) {
Expand All @@ -1098,7 +1095,6 @@ ngx_http_lua_create_headers_metatable(ngx_log_t *log, lua_State *L)
}

lua_setfield(L, -2, "__index");
lua_rawset(L, LUA_REGISTRYINDEX);
}


Expand Down
9 changes: 7 additions & 2 deletions src/ngx_http_lua_ndk.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ static int ngx_http_lua_ndk_set_var_get(lua_State *L);
static int ngx_http_lua_ndk_set_var_set(lua_State *L);
static int ngx_http_lua_run_set_var_directive(lua_State *L);

typedef struct {
ndk_set_var_value_pt wr;
} ndk_set_var_value_ptr_wrap_t;

int
ngx_http_lua_ndk_set_var_get(lua_State *L)
Expand All @@ -44,7 +47,8 @@ ngx_http_lua_ndk_set_var_get(lua_State *L)

lua_pushvalue(L, -1); /* table key key */
lua_pushvalue(L, -1); /* table key key key */
lua_pushlightuserdata(L, (void *) func); /* table key key key func */
ndk_set_var_value_ptr_wrap_t *w = (ndk_set_var_value_ptr_wrap_t*) lua_newuserdata (L, sizeof(void*));
w->wr = (void*) func;
lua_pushcclosure(L, ngx_http_lua_run_set_var_directive, 2);
/* table key key closure */
lua_rawset(L, 1); /* table key */
Expand Down Expand Up @@ -94,7 +98,8 @@ ngx_http_lua_run_set_var_directive(lua_State *L)

dd("calling set_var func for %s", p);

func = (ndk_set_var_value_pt) lua_touserdata(L, lua_upvalueindex(2));
ndk_set_var_value_ptr_wrap_t *w = (ndk_set_var_value_ptr_wrap_t*) lua_touserdata(L, lua_upvalueindex(2));
func = w->wr;

rc = func(r, &res, &arg);

Expand Down
33 changes: 21 additions & 12 deletions src/ngx_http_lua_regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ typedef struct {
uint8_t flags;
} ngx_http_lua_regex_ctx_t;

typedef struct {
ngx_http_lua_regex_t* wr;
} ngx_http_lua_regex_ptr_wrap_t;

static int ngx_http_lua_ngx_re_gmatch_iterator(lua_State *L);
static ngx_uint_t ngx_http_lua_ngx_re_parse_opts(lua_State *L,
Expand Down Expand Up @@ -249,8 +252,7 @@ ngx_http_lua_ngx_re_match_helper(lua_State *L, int wantcaps)

dd("server pool %p", lmcf->pool);

lua_pushlightuserdata(L, &ngx_http_lua_regex_cache_key);
lua_rawget(L, LUA_REGISTRYINDEX); /* table */
lua_rawgeti(L, LUA_REGISTRYINDEX, ngx_http_lua_regex_cache_key_ref);

lua_pushliteral(L, "m");
lua_pushvalue(L, 2); /* table regex */
Expand All @@ -267,7 +269,8 @@ ngx_http_lua_ngx_re_match_helper(lua_State *L, int wantcaps)
lua_tostring(L, -1));

lua_rawget(L, -3); /* table key re */
re = lua_touserdata(L, -1);
ngx_http_lua_regex_ptr_wrap_t *w = (ngx_http_lua_regex_ptr_wrap_t*) lua_touserdata(L, -1);
re = w->wr;

lua_pop(L, 1); /* table key */

Expand Down Expand Up @@ -462,7 +465,9 @@ ngx_http_lua_ngx_re_match_helper(lua_State *L, int wantcaps)
re->captures = cap;
re->replace = NULL;

lua_pushlightuserdata(L, re); /* table key value */
ngx_http_lua_regex_ptr_wrap_t *w = (ngx_http_lua_regex_ptr_wrap_t*) lua_newuserdata (L, sizeof(void*));
w->wr = re;

lua_rawset(L, -3); /* table */
lua_pop(L, 1);

Expand Down Expand Up @@ -720,8 +725,7 @@ ngx_http_lua_ngx_re_gmatch(lua_State *L)

dd("server pool %p", lmcf->pool);

lua_pushlightuserdata(L, &ngx_http_lua_regex_cache_key);
lua_rawget(L, LUA_REGISTRYINDEX); /* table */
lua_rawgeti(L, LUA_REGISTRYINDEX, ngx_http_lua_regex_cache_key_ref );

lua_pushliteral(L, "m");
lua_pushvalue(L, 2); /* table regex */
Expand All @@ -738,7 +742,8 @@ ngx_http_lua_ngx_re_gmatch(lua_State *L)
lua_tostring(L, -1));

lua_rawget(L, -3); /* table key re */
re = lua_touserdata(L, -1);
ngx_http_lua_regex_ptr_wrap_t *w = (ngx_http_lua_regex_ptr_wrap_t*) lua_touserdata(L, -1);
re = w->wr;

lua_pop(L, 1); /* table key */

Expand Down Expand Up @@ -926,7 +931,9 @@ ngx_http_lua_ngx_re_gmatch(lua_State *L)
re->captures = cap;
re->replace = NULL;

lua_pushlightuserdata(L, re); /* table key value */
ngx_http_lua_regex_ptr_wrap_t *w = (ngx_http_lua_regex_ptr_wrap_t*) lua_newuserdata (L, sizeof(void*));
w->wr = re;

lua_rawset(L, -3); /* table */
lua_pop(L, 1);

Expand Down Expand Up @@ -1388,8 +1395,7 @@ ngx_http_lua_ngx_re_sub_helper(lua_State *L, unsigned global)

dd("server pool %p", lmcf->pool);

lua_pushlightuserdata(L, &ngx_http_lua_regex_cache_key);
lua_rawget(L, LUA_REGISTRYINDEX); /* table */
lua_rawgeti(L, LUA_REGISTRYINDEX, ngx_http_lua_regex_cache_key_ref );

lua_pushliteral(L, "s");
lua_pushinteger(L, tpl.len);
Expand Down Expand Up @@ -1418,7 +1424,8 @@ ngx_http_lua_ngx_re_sub_helper(lua_State *L, unsigned global)
lua_tostring(L, -1));

lua_rawget(L, -3); /* table key re */
re = lua_touserdata(L, -1);
ngx_http_lua_regex_ptr_wrap_t *w = (ngx_http_lua_regex_ptr_wrap_t*) lua_touserdata(L, -1);
re = w->wr;

lua_pop(L, 1); /* table key */

Expand Down Expand Up @@ -1663,7 +1670,9 @@ ngx_http_lua_ngx_re_sub_helper(lua_State *L, unsigned global)
re->captures = cap;
re->replace = ctpl;

lua_pushlightuserdata(L, re); /* table key value */
ngx_http_lua_regex_ptr_wrap_t *w = (ngx_http_lua_regex_ptr_wrap_t*) lua_newuserdata (L, sizeof(void*));
w->wr = re;

lua_rawset(L, -3); /* table */
lua_pop(L, 1);

Expand Down
9 changes: 7 additions & 2 deletions src/ngx_http_lua_setby.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include "ngx_http_lua_shdict.h"
#include "ngx_http_lua_util.h"

typedef struct{
ngx_http_variable_value_t *wr;
} ngx_http_variable_value_wrap_ptr_t;

static void ngx_http_lua_set_by_lua_env(lua_State *L, ngx_http_request_t *r,
size_t nargs, ngx_http_variable_value_t *args);
Expand Down Expand Up @@ -150,7 +153,8 @@ ngx_http_lua_setby_param_get(lua_State *L)

/* get args from globals */
lua_getglobal(L, ngx_http_lua_args_key);
v = lua_touserdata(L, -1);
ngx_http_variable_value_wrap_ptr_t* w = (ngx_http_variable_value_wrap_ptr_t*) lua_touserdata(L, -1);
v = w->wr;

if (idx < 0 || idx > n - 1) {
lua_pushnil(L);
Expand Down Expand Up @@ -183,8 +187,9 @@ ngx_http_lua_set_by_lua_env(lua_State *L, ngx_http_request_t *r, size_t nargs,

lua_pushinteger(L, nargs);
lua_setglobal(L, ngx_http_lua_nargs_key);
ngx_http_variable_value_wrap_ptr_t* w = (ngx_http_variable_value_wrap_ptr_t*) lua_newuserdata (L, sizeof(void*));
w->wr = args;

lua_pushlightuserdata(L, args);
lua_setglobal(L, ngx_http_lua_args_key);

/**
Expand Down
10 changes: 7 additions & 3 deletions src/ngx_http_lua_shdict.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ static int ngx_http_lua_shdict_llen(lua_State *L);

static ngx_inline ngx_shm_zone_t *ngx_http_lua_shdict_get_zone(lua_State *L,
int index);

typedef struct {
ngx_shm_zone_t *wr;
} ngx_shm_zone_ptr_wrap_t;

#define NGX_HTTP_LUA_SHDICT_ADD 0x0001
#define NGX_HTTP_LUA_SHDICT_REPLACE 0x0002
Expand Down Expand Up @@ -396,7 +398,8 @@ ngx_http_lua_inject_shdict_api(ngx_http_lua_main_conf_t *lmcf, lua_State *L)

lua_createtable(L, 1 /* narr */, 0 /* nrec */);
/* table of zone[i] */
lua_pushlightuserdata(L, zone[i]); /* shared mt key ud */
ngx_shm_zone_ptr_wrap_t* w = (ngx_shm_zone_ptr_wrap_t*) lua_newuserdata (L, sizeof(void*));
w->wr = zone[i];
lua_rawseti(L, -2, SHDICT_USERDATA_INDEX); /* {zone[i]} */
lua_pushvalue(L, -3); /* shared mt key ud mt */
lua_setmetatable(L, -2); /* shared mt key ud */
Expand Down Expand Up @@ -433,7 +436,8 @@ ngx_http_lua_shdict_get_zone(lua_State *L, int index)
ngx_shm_zone_t *zone;

lua_rawgeti(L, index, SHDICT_USERDATA_INDEX);
zone = lua_touserdata(L, -1);
ngx_shm_zone_ptr_wrap_t *w = (ngx_shm_zone_ptr_wrap_t*) lua_touserdata(L, -1);
zone = w->wr;
lua_pop(L, 1);

return zone;
Expand Down
Loading

0 comments on commit 543a722

Please sign in to comment.