From 84b74f62231242873dadd984c677bf3651d8151e Mon Sep 17 00:00:00 2001 From: Mike Pall Date: Thu, 27 Jan 2022 22:18:39 +0100 Subject: [PATCH] Fix command-line argv handling. --- .github/workflows/exotic-builds-testing.yml | 4 +++- src/luajit.c | 24 +++++++++---------- test/tarantool-tests/CMakeLists.txt | 6 +++++ .../fix-argv-handling.test.lua | 18 ++++++++++++++ .../fix-argv-handling/CMakeLists.txt | 1 + .../fix-argv-handling/mynewstate.c | 7 ++++++ .../fix-argv-handling/script.lua | 2 ++ 7 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 test/tarantool-tests/fix-argv-handling.test.lua create mode 100644 test/tarantool-tests/fix-argv-handling/CMakeLists.txt create mode 100644 test/tarantool-tests/fix-argv-handling/mynewstate.c create mode 100644 test/tarantool-tests/fix-argv-handling/script.lua diff --git a/.github/workflows/exotic-builds-testing.yml b/.github/workflows/exotic-builds-testing.yml index 7c57d23815..74a2ae55db 100644 --- a/.github/workflows/exotic-builds-testing.yml +++ b/.github/workflows/exotic-builds-testing.yml @@ -34,7 +34,7 @@ jobs: BUILDTYPE: [Debug, Release] ARCH: [ARM64, x86_64] GC64: [ON, OFF] - FLAVOR: [checkhook, dualnum, gdbjit, nojit] + FLAVOR: [checkhook, dualnum, gdbjit, nojit, dynamic_build] include: - BUILDTYPE: Debug CMAKEFLAGS: -DCMAKE_BUILD_TYPE=Debug -DLUA_USE_ASSERT=ON -DLUA_USE_APICHECK=ON @@ -48,6 +48,8 @@ jobs: FLAVORFLAGS: -DLUAJIT_DISABLE_JIT=ON - FLAVOR: gdbjit FLAVORFLAGS: -DLUAJIT_USE_GDBJIT=ON + - FLAVOR: dynamic_build + FLAVORFLAGS: -DBUILDMODE="dynamic" exclude: - ARCH: ARM64 GC64: OFF diff --git a/src/luajit.c b/src/luajit.c index b63c92d18e..dc142684b2 100644 --- a/src/luajit.c +++ b/src/luajit.c @@ -39,6 +39,7 @@ static lua_State *globalL = NULL; static const char *progname = LUA_PROGNAME; +static char *empty_argv[2] = { NULL, NULL }; #if !LJ_TARGET_CONSOLE static void lstop(lua_State *L, lua_Debug *ar) @@ -90,9 +91,9 @@ static void print_tools_usage(void) fflush(stderr); } -static void l_message(const char *pname, const char *msg) +static void l_message(const char *msg) { - if (pname) { fputs(pname, stderr); fputc(':', stderr); fputc(' ', stderr); } + if (progname) { fputs(progname, stderr); fputc(':', stderr); fputc(' ', stderr); } fputs(msg, stderr); fputc('\n', stderr); fflush(stderr); } @@ -102,7 +103,7 @@ static int report(lua_State *L, int status) if (status && !lua_isnil(L, -1)) { const char *msg = lua_tostring(L, -1); if (msg == NULL) msg = "(error object is not a string)"; - l_message(progname, msg); + l_message(msg); lua_pop(L, 1); } return status; @@ -267,9 +268,8 @@ static void dotty(lua_State *L) lua_getglobal(L, "print"); lua_insert(L, 1); if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0) - l_message(progname, - lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)", - lua_tostring(L, -1))); + l_message(lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)", + lua_tostring(L, -1))); } } lua_settop(L, 0); /* clear stack */ @@ -321,8 +321,7 @@ static int loadjitmodule(lua_State *L) lua_getfield(L, -1, "start"); if (lua_isnil(L, -1)) { nomodule: - l_message(progname, - "unknown luaJIT command or jit.* modules not installed"); + l_message("unknown luaJIT command or jit.* modules not installed"); return 1; } lua_remove(L, -2); /* Drop module table. */ @@ -382,7 +381,7 @@ static int runtoolcmd(lua_State *L, const char *tool_name) if (msg) { if (!strncmp(msg, "module ", 7)) msg = "unknown luaJIT command or tools not installed"; - l_message(progname, msg); + l_message(msg); } return 1; } @@ -566,7 +565,6 @@ static int pmain(lua_State *L) int argn; int flags = 0; globalL = L; - if (argv[0] && argv[0][0]) progname = argv[0]; LUAJIT_VERSION_SYM(); /* Linker-enforced version check. */ @@ -622,9 +620,11 @@ static int pmain(lua_State *L) int main(int argc, char **argv) { int status; - lua_State *L = lua_open(); + lua_State *L; + if (!argv[0]) argv = empty_argv; else if (argv[0][0]) progname = argv[0]; + L = lua_open(); /* create state */ if (L == NULL) { - l_message(argv[0], "cannot create state: not enough memory"); + l_message("cannot create state: not enough memory"); return EXIT_FAILURE; } smain.argc = argc; diff --git a/test/tarantool-tests/CMakeLists.txt b/test/tarantool-tests/CMakeLists.txt index 6d6868764b..aae3c9e3c6 100644 --- a/test/tarantool-tests/CMakeLists.txt +++ b/test/tarantool-tests/CMakeLists.txt @@ -70,6 +70,12 @@ add_subdirectory(lj-flush-on-trace) add_subdirectory(lj-1004-oom-error-frame) add_subdirectory(lj-1066-fix-cur_L-after-coroutine-resume) +# We need to override the `luaL_newstate` with `LD_PRELOAD` for +# this test, which is possible only with `dynamic` build mode. +if(BUILDMODE STREQUAL "dynamic") + add_subdirectory(fix-argv-handling) +endif() + # The part of the memory profiler toolchain is located in tools # directory, jit, profiler, and bytecode toolchains are located # in src/ directory, jit/vmdef.lua is autogenerated file also diff --git a/test/tarantool-tests/fix-argv-handling.test.lua b/test/tarantool-tests/fix-argv-handling.test.lua new file mode 100644 index 0000000000..17e63d03a5 --- /dev/null +++ b/test/tarantool-tests/fix-argv-handling.test.lua @@ -0,0 +1,18 @@ +local tap = require('tap') +local test = tap.test('fix-argv-handling') + +test:plan(1) + +local script = require('utils').exec.makecmd(arg, { + env = { LD_PRELOAD = 'mynewstate.so' }, + redirect = '2>&1', +}) +local output = script() + +if output:match('cannot be preloaded') then + test:ok(true, 'static build -- can not perform test') +else + test:like(output, 'cannot create state', 'correct argv handling') +end + +test:done(true) diff --git a/test/tarantool-tests/fix-argv-handling/CMakeLists.txt b/test/tarantool-tests/fix-argv-handling/CMakeLists.txt new file mode 100644 index 0000000000..2aca36d5d0 --- /dev/null +++ b/test/tarantool-tests/fix-argv-handling/CMakeLists.txt @@ -0,0 +1 @@ +BuildTestCLib(mynewstate mynewstate.c) diff --git a/test/tarantool-tests/fix-argv-handling/mynewstate.c b/test/tarantool-tests/fix-argv-handling/mynewstate.c new file mode 100644 index 0000000000..c31a47e2f2 --- /dev/null +++ b/test/tarantool-tests/fix-argv-handling/mynewstate.c @@ -0,0 +1,7 @@ +#include + +struct lua_State; + +struct lua_State *luaL_newstate(void) { + return NULL; +} diff --git a/test/tarantool-tests/fix-argv-handling/script.lua b/test/tarantool-tests/fix-argv-handling/script.lua new file mode 100644 index 0000000000..59f4476023 --- /dev/null +++ b/test/tarantool-tests/fix-argv-handling/script.lua @@ -0,0 +1,2 @@ +-- Needed for test to exit on static build. +os.exit()