Skip to content

Commit

Permalink
Fix command-line argv handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Pall authored and mkokryashkin committed Dec 15, 2023
1 parent 3c8dbe2 commit 84b74f6
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/exotic-builds-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
24 changes: 12 additions & 12 deletions src/luajit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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. */

Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions test/tarantool-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions test/tarantool-tests/fix-argv-handling.test.lua
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions test/tarantool-tests/fix-argv-handling/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BuildTestCLib(mynewstate mynewstate.c)
7 changes: 7 additions & 0 deletions test/tarantool-tests/fix-argv-handling/mynewstate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stddef.h>

struct lua_State;

struct lua_State *luaL_newstate(void) {
return NULL;
}
2 changes: 2 additions & 0 deletions test/tarantool-tests/fix-argv-handling/script.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Needed for test to exit on static build.
os.exit()

0 comments on commit 84b74f6

Please sign in to comment.