Skip to content

Commit 84b74f6

Browse files
Mike Pallmkokryashkin
authored andcommitted
Fix command-line argv handling.
1 parent 3c8dbe2 commit 84b74f6

File tree

7 files changed

+49
-13
lines changed

7 files changed

+49
-13
lines changed

.github/workflows/exotic-builds-testing.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
BUILDTYPE: [Debug, Release]
3535
ARCH: [ARM64, x86_64]
3636
GC64: [ON, OFF]
37-
FLAVOR: [checkhook, dualnum, gdbjit, nojit]
37+
FLAVOR: [checkhook, dualnum, gdbjit, nojit, dynamic_build]
3838
include:
3939
- BUILDTYPE: Debug
4040
CMAKEFLAGS: -DCMAKE_BUILD_TYPE=Debug -DLUA_USE_ASSERT=ON -DLUA_USE_APICHECK=ON
@@ -48,6 +48,8 @@ jobs:
4848
FLAVORFLAGS: -DLUAJIT_DISABLE_JIT=ON
4949
- FLAVOR: gdbjit
5050
FLAVORFLAGS: -DLUAJIT_USE_GDBJIT=ON
51+
- FLAVOR: dynamic_build
52+
FLAVORFLAGS: -DBUILDMODE="dynamic"
5153
exclude:
5254
- ARCH: ARM64
5355
GC64: OFF

src/luajit.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
static lua_State *globalL = NULL;
4141
static const char *progname = LUA_PROGNAME;
42+
static char *empty_argv[2] = { NULL, NULL };
4243

4344
#if !LJ_TARGET_CONSOLE
4445
static void lstop(lua_State *L, lua_Debug *ar)
@@ -90,9 +91,9 @@ static void print_tools_usage(void)
9091
fflush(stderr);
9192
}
9293

93-
static void l_message(const char *pname, const char *msg)
94+
static void l_message(const char *msg)
9495
{
95-
if (pname) { fputs(pname, stderr); fputc(':', stderr); fputc(' ', stderr); }
96+
if (progname) { fputs(progname, stderr); fputc(':', stderr); fputc(' ', stderr); }
9697
fputs(msg, stderr); fputc('\n', stderr);
9798
fflush(stderr);
9899
}
@@ -102,7 +103,7 @@ static int report(lua_State *L, int status)
102103
if (status && !lua_isnil(L, -1)) {
103104
const char *msg = lua_tostring(L, -1);
104105
if (msg == NULL) msg = "(error object is not a string)";
105-
l_message(progname, msg);
106+
l_message(msg);
106107
lua_pop(L, 1);
107108
}
108109
return status;
@@ -267,9 +268,8 @@ static void dotty(lua_State *L)
267268
lua_getglobal(L, "print");
268269
lua_insert(L, 1);
269270
if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
270-
l_message(progname,
271-
lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)",
272-
lua_tostring(L, -1)));
271+
l_message(lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)",
272+
lua_tostring(L, -1)));
273273
}
274274
}
275275
lua_settop(L, 0); /* clear stack */
@@ -321,8 +321,7 @@ static int loadjitmodule(lua_State *L)
321321
lua_getfield(L, -1, "start");
322322
if (lua_isnil(L, -1)) {
323323
nomodule:
324-
l_message(progname,
325-
"unknown luaJIT command or jit.* modules not installed");
324+
l_message("unknown luaJIT command or jit.* modules not installed");
326325
return 1;
327326
}
328327
lua_remove(L, -2); /* Drop module table. */
@@ -382,7 +381,7 @@ static int runtoolcmd(lua_State *L, const char *tool_name)
382381
if (msg) {
383382
if (!strncmp(msg, "module ", 7))
384383
msg = "unknown luaJIT command or tools not installed";
385-
l_message(progname, msg);
384+
l_message(msg);
386385
}
387386
return 1;
388387
}
@@ -566,7 +565,6 @@ static int pmain(lua_State *L)
566565
int argn;
567566
int flags = 0;
568567
globalL = L;
569-
if (argv[0] && argv[0][0]) progname = argv[0];
570568

571569
LUAJIT_VERSION_SYM(); /* Linker-enforced version check. */
572570

@@ -622,9 +620,11 @@ static int pmain(lua_State *L)
622620
int main(int argc, char **argv)
623621
{
624622
int status;
625-
lua_State *L = lua_open();
623+
lua_State *L;
624+
if (!argv[0]) argv = empty_argv; else if (argv[0][0]) progname = argv[0];
625+
L = lua_open(); /* create state */
626626
if (L == NULL) {
627-
l_message(argv[0], "cannot create state: not enough memory");
627+
l_message("cannot create state: not enough memory");
628628
return EXIT_FAILURE;
629629
}
630630
smain.argc = argc;

test/tarantool-tests/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ add_subdirectory(lj-flush-on-trace)
7070
add_subdirectory(lj-1004-oom-error-frame)
7171
add_subdirectory(lj-1066-fix-cur_L-after-coroutine-resume)
7272

73+
# We need to override the `luaL_newstate` with `LD_PRELOAD` for
74+
# this test, which is possible only with `dynamic` build mode.
75+
if(BUILDMODE STREQUAL "dynamic")
76+
add_subdirectory(fix-argv-handling)
77+
endif()
78+
7379
# The part of the memory profiler toolchain is located in tools
7480
# directory, jit, profiler, and bytecode toolchains are located
7581
# in src/ directory, jit/vmdef.lua is autogenerated file also
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
local tap = require('tap')
2+
local test = tap.test('fix-argv-handling')
3+
4+
test:plan(1)
5+
6+
local script = require('utils').exec.makecmd(arg, {
7+
env = { LD_PRELOAD = 'mynewstate.so' },
8+
redirect = '2>&1',
9+
})
10+
local output = script()
11+
12+
if output:match('cannot be preloaded') then
13+
test:ok(true, 'static build -- can not perform test')
14+
else
15+
test:like(output, 'cannot create state', 'correct argv handling')
16+
end
17+
18+
test:done(true)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BuildTestCLib(mynewstate mynewstate.c)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <stddef.h>
2+
3+
struct lua_State;
4+
5+
struct lua_State *luaL_newstate(void) {
6+
return NULL;
7+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Needed for test to exit on static build.
2+
os.exit()

0 commit comments

Comments
 (0)