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 1081264ED07; Thu, 9 Nov 2023 16:09:31 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 1081264ED07 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1699535371; bh=XykXZs+p7lXCqs1J4bNFliJe7GZRlq147U4fIpnRvUU=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=dSnT5yhgveb9iHOYd5ONtkhsihNU0NQf4KZDvmXqrLdNnqG1BhrS1Eh98HswuHLSe CYfn0M4ZA7DwBQAwsGYnMl8yqJwDXkKFRRURleqxJKTFYB6ZmmbOjWZNe7pOoXCghe eZhihacUsHP0W3W63lo2WnJQD1W6OIvJUXDMUyK8= Received: from mail-ej1-f44.google.com (mail-ej1-f44.google.com [209.85.218.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 BD62164ED07 for ; Thu, 9 Nov 2023 16:08:59 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org BD62164ED07 Received: by mail-ej1-f44.google.com with SMTP id a640c23a62f3a-9d0b4dfd60dso132340866b.1 for ; Thu, 09 Nov 2023 05:08:59 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699535339; x=1700140139; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rA/ozl8zIuTuEZQ506Sn1ngpMx7BRVPS2pKNriJ3EXY=; b=VwNUvbJS+RdkO3/CNIm8PSIvtO3urlg05FTETR9zT21RrnmvzL3TEK+q4zQIq4KZKt S765BQQf75oBXDfvpwLkv1T5gEyu4iCwO7q3jcgE26yLgHncCxSFMST6NX6SRcCPWDlF frMkuzpssFMyNyUoYicJ2Lv+JUWfj/7wEA1mLrY26guRIpXYD2XEElxPOdfQg5Mgi/Py LiG4pTlXyNzXDbaC4g6kEZDknP2b+ZvJWz1V6xNCQScMq71Xx4CV1hUhawLiNlzY/8J/ fcZv8gNgqKMrG0PeUEAwnwFULtQ6tRbXzlhMDMWkPYyKJfausyQMGRrcjcz1yftaL01R 54JA== X-Gm-Message-State: AOJu0YyQlJ5DbFKTqyKSEX6aZAKipPUwuGXEmQoS9yt4fH6igQDxKbLb PD/YefIhVeLeGbD+Y7hpDgsV8Ab5J9Y= X-Google-Smtp-Source: AGHT+IGVa4MfhhxZi7mXKfJ8qsMufCPqYLziiHVOiN/lpexC8U6dTw4CQMDnBH6k42xtyJfqtc8lvg== X-Received: by 2002:a17:906:7309:b0:9d3:8d1e:ceb with SMTP id di9-20020a170906730900b009d38d1e0cebmr4575478ejc.23.1699535338360; Thu, 09 Nov 2023 05:08:58 -0800 (PST) Received: from pony.. ([185.6.247.97]) by smtp.gmail.com with ESMTPSA id h16-20020a170906585000b009ad8acac02asm2571050ejs.172.2023.11.09.05.08.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 05:08:57 -0800 (PST) To: tarantool-patches@dev.tarantool.org, Sergey Kaplun , max.kokryashkin@gmail.com Date: Thu, 9 Nov 2023 16:08:52 +0300 Message-Id: <83a6a1ef28e94bb1e3eb14bab434d18d7bf2b06b.1699534835.git.sergeyb@tarantool.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit 3/3][v2] 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: Mike Pall 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