[Tarantool-patches] [PATCH luajit 3/3][v2] Fix C file generation in jit.bcsave.
Sergey Bronnikov
estetus at gmail.com
Thu Nov 9 16:08:52 MSK 2023
From: Mike Pall <mike>
Thanks to codicodi.
(cherry picked from commit 62903bacf4abbb1c6df0f395553eeaf1f68352c6)
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 commit
a9dd47b7fcde95bbca06485c50326b7b90d930a8 ("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 bytecode, and the
resulted object file has missed a symbol with bytecode.
The patch fixes the broken macro.
Note, test test/lua-Harness-tests/411-luajit.t checks the precense 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
---
src/jit/bcsave.lua | 2 +-
test/tarantool-tests/CMakeLists.txt | 1 +
.../lj-551-bytecode-c-broken-macro.test.lua | 16 +++++++
.../CMakeLists.txt | 47 +++++++++++++++++++
.../bcsaved.lua | 3 ++
5 files changed, 68 insertions(+), 1 deletion(-)
create mode 100644 test/tarantool-tests/lj-551-bytecode-c-broken-macro.test.lua
create mode 100644 test/tarantool-tests/lj-551-bytecode-c-broken-macro/CMakeLists.txt
create mode 100644 test/tarantool-tests/lj-551-bytecode-c-broken-macro/bcsaved.lua
diff --git a/src/jit/bcsave.lua b/src/jit/bcsave.lua
index 48b2329c..a287d675 100644
--- a/src/jit/bcsave.lua
+++ b/src/jit/bcsave.lua
@@ -133,7 +133,7 @@ local function bcsave_c(ctx, output, s)
local fp = savefile(output, "w")
if ctx.type == "c" then
fp:write(format([[
-#ifdef _cplusplus
+#ifdef __cplusplus
extern "C"
#endif
#ifdef _WIN32
diff --git a/test/tarantool-tests/CMakeLists.txt b/test/tarantool-tests/CMakeLists.txt
index d46e69a0..6f29c827 100644
--- a/test/tarantool-tests/CMakeLists.txt
+++ b/test/tarantool-tests/CMakeLists.txt
@@ -62,6 +62,7 @@ add_subdirectory(gh-5813-resolving-of-c-symbols/stripped)
add_subdirectory(gh-6098-fix-side-exit-patching-on-arm64)
add_subdirectory(gh-6189-cur_L)
add_subdirectory(lj-416-xor-before-jcc)
+add_subdirectory(lj-551-bytecode-c-broken-macro)
add_subdirectory(lj-601-fix-gc-finderrfunc)
add_subdirectory(lj-727-lightuserdata-itern)
add_subdirectory(lj-802-panic-at-mcode-protfail)
diff --git a/test/tarantool-tests/lj-551-bytecode-c-broken-macro.test.lua b/test/tarantool-tests/lj-551-bytecode-c-broken-macro.test.lua
new file mode 100644
index 00000000..1cfcb4b1
--- /dev/null
+++ b/test/tarantool-tests/lj-551-bytecode-c-broken-macro.test.lua
@@ -0,0 +1,16 @@
+local tap = require('tap')
+local test = tap.test('lj-551-bytecode-c-broken-macro')
+
+test:plan(4)
+
+local function check_module(t, module_name)
+ local ok, module = pcall(require, module_name)
+ local message = ('symbol "%s" is available in a library'):format(module_name)
+ t:ok(ok, message)
+ t:is(module.a, 0)
+end
+
+check_module(test, 'bcsaved_lib_c')
+check_module(test, 'bcsaved_lib_cc')
+
+test:done(true)
diff --git a/test/tarantool-tests/lj-551-bytecode-c-broken-macro/CMakeLists.txt b/test/tarantool-tests/lj-551-bytecode-c-broken-macro/CMakeLists.txt
new file mode 100644
index 00000000..732a7e4d
--- /dev/null
+++ b/test/tarantool-tests/lj-551-bytecode-c-broken-macro/CMakeLists.txt
@@ -0,0 +1,47 @@
+enable_language(CXX)
+
+set(LUA_FILE_ORIG ${CMAKE_CURRENT_SOURCE_DIR}/bcsaved.lua)
+
+make_lua_path(LUA_PATH
+ PATHS
+ ${PROJECT_SOURCE_DIR}/src/?.lua
+ ${PROJECT_SOURCE_DIR}/src/jit/?.lua
+)
+
+macro(BuildBCLib file_ext)
+ set(LIB_NAME "bcsaved_lib_${file_ext}")
+ set(LUA_FILE "${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}.lua")
+ set(GENERATED_FILE "${LIB_NAME}.${file_ext}")
+
+ add_custom_target(copy_lua_${file_ext}
+ COMMAND ${CMAKE_COMMAND} -E
+ copy
+ ${LUA_FILE_ORIG}
+ ${LUA_FILE}
+ DEPENDS ${LUA_FILE_ORIG}
+ BYPRODUCTS ${LUA_FILE}
+ COMMENT "Copying Lua module ${LIB_NAME} to a build directory"
+ VERBATIM
+ )
+
+ add_custom_target(export_bc_${file_ext}
+ COMMAND ${CMAKE_COMMAND} -E
+ env
+ LUA_PATH=${LUA_PATH}
+ ${LUAJIT_BINARY} -b ${LUA_FILE} ${GENERATED_FILE}
+ COMMAND ${CMAKE_COMMAND} -E
+ remove ${LUA_FILE}
+ DEPENDS luajit-main ${LUA_FILE}
+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
+ BYPRODUCTS ${GENERATED_FILE}
+ COMMENT "Exporting bytecode to a ${file_ext} file"
+ VERBATIM
+ )
+
+ BuildTestCLib(${LIB_NAME} ${GENERATED_FILE})
+ add_dependencies(${LIB_NAME} export_bc_${file_ext})
+ add_dependencies(tarantool-tests ${LIB_NAME})
+endmacro()
+
+BuildBCLib("c")
+BuildBCLib("cc")
diff --git a/test/tarantool-tests/lj-551-bytecode-c-broken-macro/bcsaved.lua b/test/tarantool-tests/lj-551-bytecode-c-broken-macro/bcsaved.lua
new file mode 100644
index 00000000..ac5ef1c0
--- /dev/null
+++ b/test/tarantool-tests/lj-551-bytecode-c-broken-macro/bcsaved.lua
@@ -0,0 +1,3 @@
+return {
+ a = 0,
+}
--
2.34.1
More information about the Tarantool-patches
mailing list