Skip to content

Commit

Permalink
xxx
Browse files Browse the repository at this point in the history
  • Loading branch information
ligurio committed Feb 5, 2025
1 parent 527e894 commit 317f8a5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 35 deletions.
4 changes: 0 additions & 4 deletions test/tarantool-c-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ add_test_suite_target(tarantool-c-tests
DEPENDS libluajit libtest tarantool-c-tests-build
)

if(NOT LUAJIT_DISABLE_JIT)
AppendFlags(TESTS_C_FLAGS "-DLJ_HASJIT")
endif(NOT LUAJIT_DISABLE_JIT)

set(CTEST_SRC_SUFFIX ".test.c")
file(GLOB tests "${CMAKE_CURRENT_SOURCE_DIR}/*${CTEST_SRC_SUFFIX}")
foreach(test_source ${tests})
Expand Down
76 changes: 45 additions & 31 deletions test/tarantool-c-tests/lj-1087-vm-handler-call.test.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include <assert.h>
#include <stdbool.h>

#include "lua.h"
#include "lauxlib.h"

#include "test.h"
#include "utils.h"

/* Need for skipcond for BSD and JIT. */
#include "lj_arch.h"

/*
* Test file to demonstrate a segmentation fault under
* AddressSanitizer, when C function is used as a VM handler
Expand All @@ -20,35 +22,35 @@
* See details in https://github.com/LuaJIT/LuaJIT/issues/1087.
*/

#define UNUSED(x) ((void)(x))
static int nop(lua_State *L)
{
UNUSED(L);
return 0;
}

/* `event == NULL` disables the corresponding handler. */
static void jit_attach(lua_State *L, void *cb, const char *event)
{
lua_getglobal(L, "jit");
lua_getfield(L, -1, "attach");
lua_pushcfunction(L, (lua_CFunction)cb);
if (event != NULL) {
if (event != NULL)
lua_pushstring(L, event);
} else {
else
lua_pushnil(L);
}
lua_pcall(L, 2, 0, 0);
}

static int trace_cb(lua_State *L) {
UNUSED(L);
return 0;
}

static int handle_luafunc_frame(void *test_state)
{
/* Setup. */
lua_State *L = test_state;
jit_attach(L, (void *)trace_cb, "trace");
jit_attach(L, (void *)nop, "trace");

/* Loading and executing of a broken Lua code. */
int rc = luaL_dostring(L, "repeat until nil > 1");
assert(rc == 1);
UNUSED(rc);

/* The Lua chunk generates a Lua frame. */
rc = luaL_dostring(L, "return function() end");
Expand All @@ -60,16 +62,13 @@ static int handle_luafunc_frame(void *test_state)
return TEST_EXIT_SUCCESS;
}

static int nop(lua_State *L)
{
return 0;
}

static int cframe(lua_State *L)
{
int rc = luaL_dostring(L, "repeat until nil > 1");
assert(rc == 1);
lua_pop(L, 1); /* Remove errmsg. */
UNUSED(rc);
/* Remove errmsg. */
lua_pop(L, 1);

lua_pushcfunction(L, nop);
lua_call(L, 0, 0);
Expand All @@ -81,7 +80,7 @@ static int handle_c_frame(void *test_state)
{
/* Setup. */
lua_State *L = test_state;
jit_attach(L, (void *)trace_cb, "trace");
jit_attach(L, (void *)nop, "trace");

lua_pushcfunction(L, cframe);
lua_call(L, 0, 0);
Expand All @@ -92,15 +91,17 @@ static int handle_c_frame(void *test_state)
return TEST_EXIT_SUCCESS;
}

/*
static int global_f(lua_State* L)
{
lua_pushstring(L, "М");
lua_pushstring(L, "И");
lua_pushstring(L, "Р");
lua_pushstring(L, "L");
lua_pushstring(L, "U");
lua_pushstring(L, "A");
lua_concat(L, 3);
return 1;
}
*/

static int handle_cont_frame(void *test_state)
{
Expand All @@ -112,10 +113,17 @@ static int handle_cont_frame(void *test_state)

/* Setup. */
lua_State *L = test_state;
jit_attach(L, (void *)trace_cb, "trace");
int res = luaL_dostring(L, "jit.opt.start('minstitch=16')");
jit_attach(L, (void *)nop, "trace");

/*
* The number 32767 is `REF_DROP - REF_BIAS`. See the commit
* 0fdf06b456e6 ("test: relax JIT setup in misc.getmetrics
* test") for the details.
*/
int res = luaL_dostring(L, "jit.opt.start('minstitch=32767')");
UNUSED(res);
assert(res == 0);
lua_pushcfunction(L, global_f);
lua_pushcfunction(L, nop);
lua_setglobal(L, "global_f");

res = luaL_loadstring(L, lua_chunk);
Expand All @@ -124,6 +132,9 @@ static int handle_cont_frame(void *test_state)

/* Teardown. */
lua_settop(L, 0);
res = luaL_dostring(L, "jit.opt.start('minstitch=0')");
UNUSED(res);
assert(res == 0);

return TEST_EXIT_SUCCESS;
}
Expand All @@ -133,14 +144,15 @@ static int handle_bottom_frame(void *test_state)
lua_State *L = test_state;

/* Attach VM call handler. */
jit_attach(L, (void *)trace_cb, "trace");
jit_attach(L, (void *)nop, "trace");

/* Load a Lua code that generate a trace abort. */
int rc = luaL_dostring(L, "repeat until nil > 1");
UNUSED(rc);
assert(rc == 1);

/* Triggers segmentation fault. */
jit_attach(L, (void *)trace_cb, NULL);
jit_attach(L, (void *)nop, NULL);

/* Teardown. */
lua_settop(L, 0);
Expand All @@ -150,21 +162,23 @@ static int handle_bottom_frame(void *test_state)

int main(void)
{
#if !defined(LJ_HASJIT)
return skip_all("JIT is disabled");
#else /* LJ_HASJIT */
if (!LJ_HASJIT)
return skip_all("JIT is disabled");

if (LUAJIT_OS == LUAJIT_OS_BSD)
return skip_all("Disabled on *BSD due to #4819");

lua_State *L = utils_lua_init();
const struct test_unit tgroup[] = {
test_unit_def(handle_luafunc_frame),
test_unit_def(handle_bottom_frame),
test_unit_def(handle_cont_frame),
test_unit_def(handle_c_frame),
};
luaL_openlibs(L);
int res = luaL_dostring(L, "jit.opt.start('hotloop=1')");
UNUSED(res);
assert(res == 0);
const int test_result = test_run_group(tgroup, L);
utils_lua_close(L);
return test_result;
#endif /* LJ_HASJIT */
}

0 comments on commit 317f8a5

Please sign in to comment.