From: Vladimír Čunát Date: Mon, 3 Aug 2020 17:32:23 +0000 (+0200) Subject: daemon/lua: get rid of __engine symbol in lua X-Git-Tag: v5.1.3~18^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b7629c55dcc1850d2f60ae14fa0856fab92848b8;p=thirdparty%2Fknot-resolver.git daemon/lua: get rid of __engine symbol in lua In particular this gets rid of last light user data inside kresd. It was still causing problems on some systems, for example Debian Sid. The error was the same: "bad light userdata pointer" from luajit, but note that the problem can still be triggered by lua libraries, e.g. cqueues. --- diff --git a/.luacheckrc b/.luacheckrc index 2901cef06..7ff622489 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -40,7 +40,6 @@ new_read_globals = { 'libknot_SONAME', 'libzscanner_SONAME', 'table_print', - '__engine', '_ENV', } diff --git a/NEWS b/NEWS index 9cef9085e..1eb4da5d4 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,7 @@ Improvements ------------ - capabilities are no longer constrained when running as root (!1012) - cache: add percentage usage to cache.stats() (!1025) +- aarch64 support again, as some systems still didn't work (!1033) Bugfixes -------- diff --git a/daemon/engine.c b/daemon/engine.c index ebf25555d..f52f91e39 100644 --- a/daemon/engine.c +++ b/daemon/engine.c @@ -21,6 +21,7 @@ #include "kresconfig.h" #include "daemon/engine.h" #include "daemon/ffimodule.h" +#include "daemon/worker.h" #include "lib/nsrep.h" #include "lib/cache/api.h" #include "lib/defines.h" @@ -498,8 +499,6 @@ static int init_state(struct engine *engine) lua_setglobal(engine->L, "fromjson"); lua_pushcfunction(engine->L, l_map); lua_setglobal(engine->L, "map"); - lua_pushlightuserdata(engine->L, engine); - lua_setglobal(engine->L, "__engine"); /* Random number generator */ lua_getfield(engine->L, LUA_GLOBALSINDEX, "math"); lua_getfield(engine->L, -1, "randomseed"); @@ -860,9 +859,5 @@ int engine_unregister(struct engine *engine, const char *name) struct engine *engine_luaget(lua_State *L) { - lua_getglobal(L, "__engine"); - struct engine *engine = lua_touserdata(L, -1); - if (!engine) luaL_error(L, "internal error, empty engine pointer"); - lua_pop(L, 1); - return engine; + return the_worker->engine; } diff --git a/daemon/engine.h b/daemon/engine.h index ad32cee5e..0a0561067 100644 --- a/daemon/engine.h +++ b/daemon/engine.h @@ -52,7 +52,7 @@ void engine_stop(struct engine *engine); int engine_register(struct engine *engine, const char *name, const char *precedence, const char* ref); int engine_unregister(struct engine *engine, const char *name); -/** Return engine light userdata. */ +/** Return engine light userdata; TODO: remove this function? */ struct engine *engine_luaget(struct lua_State *L); /** Set/get the per engine hostname */ diff --git a/daemon/lua/kres-gen.lua b/daemon/lua/kres-gen.lua index c77ffc0eb..8d02db78e 100644 --- a/daemon/lua/kres-gen.lua +++ b/daemon/lua/kres-gen.lua @@ -442,6 +442,15 @@ struct qr_task { int worker_resolve_exec(struct qr_task *, knot_pkt_t *); knot_pkt_t *worker_resolve_mk_pkt(const char *, uint16_t, uint16_t, const struct kr_qflags *); struct qr_task *worker_resolve_start(knot_pkt_t *, struct kr_qflags); +struct engine { + struct kr_context resolver; + char _stub[]; +}; +struct worker_ctx { + struct engine *engine; + char _stub[]; +}; +struct worker_ctx *the_worker; typedef struct { uint8_t bitmap[32]; uint8_t length; diff --git a/daemon/lua/kres-gen.sh b/daemon/lua/kres-gen.sh index be7aa3670..309b7f1d7 100755 --- a/daemon/lua/kres-gen.sh +++ b/daemon/lua/kres-gen.sh @@ -278,6 +278,14 @@ ${CDEFS} ${KRESD} functions <<-EOF worker_resolve_start EOF +echo "struct engine" | ${CDEFS} ${KRESD} types | sed '/struct network/,$ d' +printf "\tchar _stub[];\n};\n" + +echo "struct worker_ctx" | ${CDEFS} ${KRESD} types | sed '/uv_loop_t/,$ d' +printf "\tchar _stub[];\n};\n" + +echo "struct worker_ctx *the_worker;" + ## libzscanner API for ./zonefile.lua ${CDEFS} libzscanner types <<-EOF diff --git a/daemon/lua/kres.lua b/daemon/lua/kres.lua index 1bdc30a31..d859fda96 100644 --- a/daemon/lua/kres.lua +++ b/daemon/lua/kres.lua @@ -1061,7 +1061,7 @@ kres = { if ret ~= 1 then return nil end return ffi.string(addr_buf, C.kr_family_len(family)) end, - context = function () return ffi.cast('struct kr_context *', __engine) end, + context = function () return ffi.C.the_worker.engine.resolver end, knot_pkt_rr = knot_pkt_rr, } diff --git a/daemon/lua/sandbox.lua.in b/daemon/lua/sandbox.lua.in index 3eb9377fa..b20068194 100644 --- a/daemon/lua/sandbox.lua.in +++ b/daemon/lua/sandbox.lua.in @@ -199,7 +199,7 @@ function modules_create_table_for_c(kr_module_ud) elseif arg ~= nil then arg_conv = tostring(arg) end - local ret_cstr = cb(__engine, kr_module, arg_conv) + local ret_cstr = cb(ffi.C.the_worker.engine, kr_module, arg_conv) if ret_cstr == nil then return nil end diff --git a/daemon/main.c b/daemon/main.c index 3295cd0d6..7fbae458a 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -602,8 +602,6 @@ int main(int argc, char **argv) .ctx = mp_new (4096), .alloc = (knot_mm_alloc_t) mp_alloc }; - /** Static to work around lua_pushlightuserdata() limitations. - * TODO: convert to a proper singleton like worker, most likely. */ static struct engine engine; ret = engine_init(&engine, &pool); if (ret != 0) {