Skip to content

Commit 01df6ae

Browse files
Mike Palligormunkin
authored andcommitted
Fix C file generation in jit.bcsave.
Thanks to codicodi. (cherry picked from commit 62903ba) LuaJIT allows exporting bytecode to a C file using the option `-b`, see [1]. For building a generated C file in C++ projects, C file uses a macro `__cplusplus` [2], but this macro was broken by the commit a9dd47b ("Extend -b to generate c/h/obj/o files with embedded bytecode."). With this breakage, C/C++ compiler removes the definition of an array with the bytecode, and the resulted object file misses a symbol with the bytecode. The patch fixes the broken macro. Note, test test/lua-Harness-tests/411-luajit.t checks the presence of the macro `__cplusplus` in the generated C file, however, it doesn't catch the bug. Sergey Bronnikov: * added the description and the test for the problem Part of tarantool/tarantool#9145 1. https://luajit.org/running.html 2. https://en.cppreference.com/w/cpp/preprocessor/replace
1 parent 08e1890 commit 01df6ae

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

src/jit/bcsave.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ local function bcsave_c(ctx, output, s)
133133
local fp = savefile(output, "w")
134134
if ctx.type == "c" then
135135
fp:write(format([[
136-
#ifdef _cplusplus
136+
#ifdef __cplusplus
137137
extern "C"
138138
#endif
139139
#ifdef _WIN32

test/tarantool-tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ add_subdirectory(gh-6098-fix-side-exit-patching-on-arm64)
3838
add_subdirectory(gh-6189-cur_L)
3939
add_subdirectory(lj-416-xor-before-jcc)
4040
add_subdirectory(lj-549-bytecode-loader)
41+
add_subdirectory(lj-551-bytecode-c-broken-macro)
4142
add_subdirectory(lj-601-fix-gc-finderrfunc)
4243
add_subdirectory(lj-727-lightuserdata-itern)
4344
add_subdirectory(lj-802-panic-at-mcode-protfail)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
local tap = require('tap')
2+
local test = tap.test('lj-551-bytecode-c-broken-macro'):skipcond({
3+
-- XXX: Tarantool doesn't use default LuaJIT loaders, and Lua
4+
-- bytecode can't be loaded from the shared library. For more
5+
-- info: https://github.com/tarantool/tarantool/issues/9671.
6+
-- luacheck: no global
7+
['Test uses exotic type of loaders (see #9671)'] = _TARANTOOL,
8+
})
9+
10+
test:plan(4)
11+
12+
local function check_module(t, module_name)
13+
local ok, module = pcall(require, module_name)
14+
local message = ('symbol %q is available in a library'):format(module_name)
15+
t:ok(ok, message)
16+
t:is(module.msg, 'Lango team')
17+
end
18+
19+
check_module(test, 'script_c')
20+
check_module(test, 'script_cc')
21+
22+
test:done(true)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
enable_language(CXX)
2+
3+
set(LUA_FILE ${CMAKE_CURRENT_SOURCE_DIR}/script.lua)
4+
5+
make_lua_path(LUA_PATH
6+
PATHS
7+
${LUAJIT_SOURCE_DIR}/?.lua
8+
${LUAJIT_SOURCE_DIR}/jit/?.lua
9+
)
10+
11+
macro(BuildTestBCLib file_ext)
12+
set(LIB_NAME "script_${file_ext}")
13+
set(LUA_SOURCE ${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}.lua)
14+
set(EXT_SOURCE ${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}.${file_ext})
15+
16+
# XXX: Module has to be copied, since bytecode dumper uses its
17+
# name within the exported symbol.
18+
add_custom_target(export_bc_${file_ext}
19+
COMMAND ${CMAKE_COMMAND} -E copy ${LUA_FILE} ${LUA_SOURCE}
20+
COMMAND ${CMAKE_COMMAND} -E env
21+
LUA_PATH=${LUA_PATH} ${LUAJIT_TEST_BINARY} -b ${LUA_SOURCE} ${EXT_SOURCE}
22+
COMMAND ${CMAKE_COMMAND} -E remove ${LUA_SOURCE}
23+
DEPENDS ${LUAJIT_TEST_BINARY} ${LUA_FILE}
24+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
25+
BYPRODUCTS ${EXT_SOURCE}
26+
COMMENT "Exporting bytecode to a ${file_ext} file"
27+
VERBATIM
28+
)
29+
30+
BuildTestCLib(${LIB_NAME} ${EXT_SOURCE})
31+
add_dependencies(${LIB_NAME} export_bc_${file_ext})
32+
endmacro()
33+
34+
BuildTestBCLib("c")
35+
BuildTestBCLib("cc")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
return {
2+
msg = 'Lango team',
3+
}

0 commit comments

Comments
 (0)