Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Bronnikov via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: tarantool-patches@dev.tarantool.org,
	Sergey Kaplun <skaplun@tarantool.org>,
	max.kokryashkin@gmail.com
Subject: [Tarantool-patches] [PATCH luajit 3/3][v2] Fix C file generation in jit.bcsave.
Date: Thu,  9 Nov 2023 16:08:52 +0300	[thread overview]
Message-ID: <83a6a1ef28e94bb1e3eb14bab434d18d7bf2b06b.1699534835.git.sergeyb@tarantool.org> (raw)
In-Reply-To: <cover.1699534835.git.sergeyb@tarantool.org>

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


  parent reply	other threads:[~2023-11-09 13:09 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-09 13:06 [Tarantool-patches] [PATCH luajit 0/3][v2] " Sergey Bronnikov via Tarantool-patches
2023-11-09 13:08 ` [Tarantool-patches] [PATCH luajit 1/3][v2] Add 'cc' file type for saving bytecode Sergey Bronnikov via Tarantool-patches
2023-11-13  7:04   ` Sergey Kaplun via Tarantool-patches
2023-11-15  9:39     ` Sergey Bronnikov via Tarantool-patches
2023-11-09 13:08 ` [Tarantool-patches] [PATCH luajit 2/3][v2] test: set dependencies to tarantool-tests explicitly Sergey Bronnikov via Tarantool-patches
2023-11-13  7:10   ` Sergey Kaplun via Tarantool-patches
2023-11-15 11:34     ` Sergey Bronnikov via Tarantool-patches
2023-11-09 13:08 ` Sergey Bronnikov via Tarantool-patches [this message]
2023-11-13  7:10   ` [Tarantool-patches] [PATCH luajit 3/3][v2] Fix C file generation in jit.bcsave Sergey Kaplun via Tarantool-patches
2024-02-29  7:40 ` [Tarantool-patches] [PATCH luajit 0/3][v2] " Igor Munkin via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=83a6a1ef28e94bb1e3eb14bab434d18d7bf2b06b.1699534835.git.sergeyb@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=estetus@gmail.com \
    --cc=max.kokryashkin@gmail.com \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit 3/3][v2] Fix C file generation in jit.bcsave.' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox