From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id DD0686CF5E5; Thu, 19 Oct 2023 11:57:07 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org DD0686CF5E5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1697705828; bh=fHBJFl86pjt+dcxTn6Z5uhkGfWkJXG2MC7w1b0B81NU=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=hPUnlVyPhLvSdl2obR1m5JCadZCgmi3HmPodFBAwPmx68j7Z2NaYnDdVIkiVuIwOx qHovjbpISL7P4M4GBKF2n5MiyxJhfRVz7ZXlKZFA2evEtvS0QTQ8Q0SwDmmB0jYLYi x70bDIj/qaGwB76QYitKreKlBr8yFODo1TmsNTEU= Received: from mail-ed1-f44.google.com (mail-ed1-f44.google.com [209.85.208.44]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id C15666CF5E0 for ; Thu, 19 Oct 2023 11:57:06 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C15666CF5E0 Received: by mail-ed1-f44.google.com with SMTP id 4fb4d7f45d1cf-53e16f076b3so1180799a12.0 for ; Thu, 19 Oct 2023 01:57:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697705825; x=1698310625; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=R0+S4D0BW8c/tTLKbfVGM8EoFoxZylkqSWb9aHzANI8=; b=I7WyByevS5/qcj5M0YlXR1xs+Jper9a0EltSPKdJA3xpSXRGSVbQOO6BhLGKiq+8+h ValWnH/DBpK3KhyXjXs2JZvc20jX9+jjtX72+D61BVyH8URgGwKOiHwO1OT2ybVEeY+H ZMKLezGV5P1647+vYcg6bX6pSCMWYx+Nj2HB1z9T11HbZO5KqpPBQIbKvrRh58kb4vSc BnPGenC5ELOQZahDdNM01TWWtg8QRbAuaLuv79xMkwAa4/UGuflgv03xiuB5+Afa0lE6 ZWHyKkjuws1rhyjvZsU4gHM6KVY1YE0l1WpkvL7BufTtfxXv89Yg+nKsBjNII7lq80iC nG8g== X-Gm-Message-State: AOJu0Yxz+yPgYE8yz27mlyLrya3Q7lFe+9DZAqyWIhz0lzRd3bEjHgWp APNvqJytvxdgk28kRBBt0j08bekCMnXUAA== X-Google-Smtp-Source: AGHT+IHjMXZz5akjvKPH0yFz9L7N5JM1PwKe+sY2SKnJFbybQ+IcWORh/jtU9N21/dfBuqz8uEmgAQ== X-Received: by 2002:aa7:de1a:0:b0:529:d147:e9f3 with SMTP id h26-20020aa7de1a000000b00529d147e9f3mr1164094edv.5.1697705825392; Thu, 19 Oct 2023 01:57:05 -0700 (PDT) Received: from pony.. ([185.6.247.97]) by smtp.gmail.com with ESMTPSA id a93-20020a509ee6000000b00536159c6c45sm4091116edf.15.2023.10.19.01.57.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 01:57:04 -0700 (PDT) To: tarantool-patches@dev.tarantool.org, Sergey Kaplun , max.kokryashkin@gmail.com Date: Thu, 19 Oct 2023 11:56:21 +0300 Message-Id: X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit] Fix C file generation in jit.bcsave. X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Sergey Bronnikov Thanks to codicodi. (cherry picked from commit 62903bacf4abbb1c6df0f395553eeaf1f68352c6) LuaJIT allows exporting bytecode to a C file using option `-b`, see [1]. For building generated C file in C++ projects C file uses a macro `__cplusplus` [2], but this macro was broken by commit a9dd47b7fcde ("Extend -b to generate c/h/obj/o files with embedded bytecode."). With this breakage C/C++ compiler removes definition of array with bytecode and resulted object file has missed a symbol with bytecode. The patch fixes broken macro. Note, test test/lua-Harness-tests/411-luajit.t checks a precense of macro `__cplusplus` in 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 --- PR: https://github.com/tarantool/tarantool/pull/9276 Epic: https://github.com/tarantool/tarantool/issues/9145 Issue: none src/jit/bcsave.lua | 2 +- test/tarantool-tests/CMakeLists.txt | 1 + .../lj-bytecode-c-broken-macro.test.lua | 13 ++++++++++++ .../lj-bytecode-c-broken-macro/CMakeLists.txt | 21 +++++++++++++++++++ .../lj-bytecode-c-broken-macro/lj_lib_xxx.lua | 4 ++++ 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 test/tarantool-tests/lj-bytecode-c-broken-macro.test.lua create mode 100644 test/tarantool-tests/lj-bytecode-c-broken-macro/CMakeLists.txt create mode 100644 test/tarantool-tests/lj-bytecode-c-broken-macro/lj_lib_xxx.lua diff --git a/src/jit/bcsave.lua b/src/jit/bcsave.lua index 41081184..5a12c7d0 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 c15d6037..838e359a 100644 --- a/test/tarantool-tests/CMakeLists.txt +++ b/test/tarantool-tests/CMakeLists.txt @@ -68,6 +68,7 @@ add_subdirectory(lj-727-lightuserdata-itern) add_subdirectory(lj-802-panic-at-mcode-protfail) add_subdirectory(lj-flush-on-trace) add_subdirectory(lj-1004-oom-error-frame) +add_subdirectory(lj-bytecode-c-broken-macro) # The part of the memory profiler toolchain is located in tools # directory, jit, profiler, and bytecode toolchains are located diff --git a/test/tarantool-tests/lj-bytecode-c-broken-macro.test.lua b/test/tarantool-tests/lj-bytecode-c-broken-macro.test.lua new file mode 100644 index 00000000..2047adac --- /dev/null +++ b/test/tarantool-tests/lj-bytecode-c-broken-macro.test.lua @@ -0,0 +1,13 @@ +local tap = require('tap') +local test = tap.test('lj-jit-bcsave-c-generation') + +test:plan(3) + +local module_name = 'lj_lib_xxx' +local ok, module = pcall(require, module_name) +local message = ('symbol "%s" is available in a library'):format(module_name) +test:ok(ok == true, message) +test:is(module.a, 1) +test:is(module.b, 2) + +test:done(true) diff --git a/test/tarantool-tests/lj-bytecode-c-broken-macro/CMakeLists.txt b/test/tarantool-tests/lj-bytecode-c-broken-macro/CMakeLists.txt new file mode 100644 index 00000000..aa715067 --- /dev/null +++ b/test/tarantool-tests/lj-bytecode-c-broken-macro/CMakeLists.txt @@ -0,0 +1,21 @@ +set(LIB_NAME "lj_lib_xxx") +set(LUA_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${LIB_NAME}.lua) +set(C_FILE ${LIB_NAME}.c) +set(CXX_FILE ${LIB_NAME}.cc) + +make_lua_path(LUA_PATH + PATHS + ${PROJECT_SOURCE_DIR}/src/?.lua + ${PROJECT_SOURCE_DIR}/src/jit/?.lua +) + +add_custom_target(export_bc + COMMAND ${CMAKE_COMMAND} -E env LUA_PATH=${LUA_PATH} ${LUAJIT_BINARY} -b ${LUA_FILE} ${C_FILE} + DEPENDS luajit-main ${LUA_FILE} + BYPRODUCTS ${C_FILE} + COMMENT "Exporting bytecode to a C file" + VERBATIM +) + +BuildTestCLib(${LIB_NAME} ${C_FILE}) +add_dependencies(${LIB_NAME} export_bc) diff --git a/test/tarantool-tests/lj-bytecode-c-broken-macro/lj_lib_xxx.lua b/test/tarantool-tests/lj-bytecode-c-broken-macro/lj_lib_xxx.lua new file mode 100644 index 00000000..591dfc8b --- /dev/null +++ b/test/tarantool-tests/lj-bytecode-c-broken-macro/lj_lib_xxx.lua @@ -0,0 +1,4 @@ +return { + a = 1, + b = 2, +} -- 2.34.1