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 B379D63F4CC; Tue, 26 Sep 2023 10:00:09 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org B379D63F4CC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1695711609; bh=8OuhXE7WeGrILQjxZmUDvQkZb3YTJzIQSQZnB8CYS7w=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=egTtCCfMTA1PwEbQggippkUHDyIxVrkjQ8JhSnZQ4xQzGCQcmHFPGc//fP6t7SfZE jHU2qIU8R9IVjBHB4NmthuWdjwZBKep7d9OE2/NdpSiEum/kXs0Hkrt6abvpSja1qK cNXzW+B2mBHQmHDINb0Y41DQ6JU6BIDTjph+PDmQ= Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.51]) (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 CD30041A6AB for ; Tue, 26 Sep 2023 10:00:08 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org CD30041A6AB Received: by mail-ed1-f51.google.com with SMTP id 4fb4d7f45d1cf-523100882f2so9262959a12.2 for ; Tue, 26 Sep 2023 00:00:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695711608; x=1696316408; 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=jmG9oqqunnwC2MjKIfrT10XDGdV1I1vEHd57hoAb+rs=; b=QtEdrSdrGU4Fu1oXRrl3P3TkKPzaCr9/XPQ6ErllBm99VXHaqdJAcvyj/CodhzCUgU oSVAcpNqEgJg1NnSyy8QS19EIuX0qySGpqXIzV12cHmE0quKlRyNa3o6+Z/QbxOr2abM eSeAqqsNOrEgmTxt2qq8e3BRd0KpRG7SXjdGJhnelTLSDXziu6+tqLepi+hrt/GgTFTm sNraumQ3G5kv8TzbP+aS/Se9CyhCw0ApsuHgVMXl1RTelk4FAXIyH1N7wSbihJJP4cJm c10RY7sGty/ohuC3uEbEr6p8obM4AKGZslCxmYv1K0lIL5qTGQhRLwSGWj6qiTyVyPuv 1bpg== X-Gm-Message-State: AOJu0Yzv49W6p6LXsVNPzZVp3FeZW1PQy4L4BxchGTjzYWi2vjd6dKzM QEHP6oeaDau91ji3saUrgYjcF2BpI08= X-Google-Smtp-Source: AGHT+IGeN3sHO/VKxXbCnkdXGM14JGWoHJhMLgu9/Qc+uxmmduB5slCgTwndfeRXPqO82oSkCa/bww== X-Received: by 2002:aa7:da4f:0:b0:532:eb5f:c8de with SMTP id w15-20020aa7da4f000000b00532eb5fc8demr7709130eds.24.1695711607321; Tue, 26 Sep 2023 00:00:07 -0700 (PDT) Received: from pony.. ([185.6.247.97]) by smtp.gmail.com with ESMTPSA id p5-20020a05640243c500b00533e915923asm3019336edc.49.2023.09.26.00.00.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 00:00:06 -0700 (PDT) To: tarantool-patches@dev.tarantool.org, Sergey Kaplun , max.kokryashkin@gmail.com Date: Tue, 26 Sep 2023 09:56:31 +0300 Message-Id: <26636e3dcb8ade3bdc23d9544f9a7e2ffe4322b2.1695711374.git.sergeyb@tarantool.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit] LJ_GC64: Fix lua_concat(). 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 Reported by Mathias Westerdahl. (cherry picked from commit 633f265f67f322cbe2c5fd11d3e46d968ac220f7) Lua 5.1 Reference Manual [1] defines a function `lua_concat`, that: > void lua_concat (lua_State *L, int n); > > Concatenates the n values at the top of the stack, pops them, and leaves > the result at the top. Without the patch `lua_concat()` behaved incorrectly with userdata with defined `__concat` metamethod. The problem is GC64-specific. Assuming we have three literals and a userdata with defined "__concat" metamethod on top of the Lua stack: 1 [string] 2 [string] 3 [string] 4 [string] 5 [userdata] <--- top On attempt to concatenate *two* items on top Lua stack, `lua_concat()` concatenates *four* items and leaves result at the top: 1 [string] 2 [string][string][string][userdata] <--- top The problem is in incorrect calculation of `n` counter in a loop in implementation of function `lua_concat`. Without a fix `n` is equal to 3 at the end of the first iteration and therefore it goes to the next iteration of concatenation. In a fixed implementation of `lua_concat()` `n` is equal to 1 at the end of the first loop iteration, decremented in a loop postcondition and breaks a loop. The patch fixes incorrect behaviour. 1. https://www.lua.org/manual/5.1/manual.html Sergey Bronnikov: * added the description and the test for the problem Part of tarantool/tarantool#8825 --- PR: https://github.com/tarantool/tarantool/pull/9176 Branch: https://github.com/tarantool/luajit/commits/ligurio/lj-881-fix-concat Issues: - https://github.com/LuaJIT/LuaJIT/issues/881 - https://github.com/tarantool/tarantool/issues/8825 src/lj_api.c | 2 +- .../lj-881-fix-lua-concat.test.c | 116 ++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 test/tarantool-c-tests/lj-881-fix-lua-concat.test.c diff --git a/src/lj_api.c b/src/lj_api.c index 05e02029..3bacad33 100644 --- a/src/lj_api.c +++ b/src/lj_api.c @@ -801,7 +801,7 @@ LUA_API void lua_concat(lua_State *L, int n) L->top -= n; break; } - n -= (int)(L->top - top); + n -= (int)(L->top - (top - 2*LJ_FR2)); L->top = top+2; jit_secure_call(L, top, 1+1); L->top -= 1+LJ_FR2; diff --git a/test/tarantool-c-tests/lj-881-fix-lua-concat.test.c b/test/tarantool-c-tests/lj-881-fix-lua-concat.test.c new file mode 100644 index 00000000..464ed27e --- /dev/null +++ b/test/tarantool-c-tests/lj-881-fix-lua-concat.test.c @@ -0,0 +1,116 @@ +#include + +#include "lua.h" +#include "lauxlib.h" + +#include "test.h" +#include "utils.h" + +/* + * This test demonstrates LuaJIT's incorrect behaviour, when + * calling `lua_concat()` with userdata with __concat metamethod. + * See https://github.com/LuaJIT/LuaJIT/issues/881 for details. + */ + +#define TYPE_NAME_INFO "Info" + +typedef struct Info +{ + int value; +} Info; + +static void lua_pushinfo(lua_State *L, Info *info) +{ + Info *infop = (Info *)lua_newuserdata(L, sizeof(Info)); + *infop = *info; + + luaL_getmetatable(L, TYPE_NAME_INFO); + lua_setmetatable(L, -2); +} + +static int INFO_concat(lua_State *L) +{ + const char *s = luaL_checkstring(L, 1); + Info *info = (Info*)luaL_checkudata(L, 2, TYPE_NAME_INFO); + lua_pushfstring(L, "%s[%s.value=%d]", s, TYPE_NAME_INFO, info->value); + return 1; +} + +static const luaL_Reg INFO_methods[] = +{ + {0, 0} +}; + +static const luaL_Reg INFO_meta[] = +{ + {"__concat", INFO_concat}, + {0, 0} +}; + +static int lua_concat_testcase(void *test_state) +{ + lua_State *L = test_state; + + /* Create methods table, add it to the globals. */ + luaL_register(L, TYPE_NAME_INFO, INFO_methods); + int methods_idx = lua_gettop(L); + /* Create metatable and add it to the Lua registry. */ + luaL_newmetatable(L, TYPE_NAME_INFO); + + int metatable_idx = lua_gettop(L); + /* Fill metatable. */ + luaL_register(L, 0, INFO_meta); + + lua_pushliteral(L, "__metatable"); + /* Duplicate methods table. */ + lua_pushvalue(L, methods_idx); + lua_settable(L, metatable_idx); + lua_pop(L, 2); + + assert_true(lua_gettop(L) == 0); + + Info info; + info.value = 7; + + lua_pushliteral(L, "C"); + lua_pushliteral(L, "B"); + lua_pushliteral(L, "A"); + lua_pushinfo(L, &info); + + int top = 4; + assert_true(lua_gettop(L) == top); + + /** + * void lua_concat (lua_State *L, int n); + * + * Concatenates the n values at the top of the stack, + * pops them, and leaves the result at the top. If n is 1, + * the result is the single value on the stack; if n is 0, + * the result is the empty string [1]. + * + * 1. https://www.lua.org/manual/5.1/manual.html + */ + int n = 2; + lua_concat(L, n); + + const char *str = lua_tostring(L, -1); + assert_true(top - n + 1 == lua_gettop(L)); + assert_true(strcmp(str, "A[Info.value=7]") == 0); + + /* Cleanup. */ + lua_settop(L, 0); + + return TEST_EXIT_SUCCESS; +} + +int main(void) +{ + lua_State *L = utils_lua_init(); + const struct test_unit tgroup[] = { + test_unit_def(lua_concat_testcase), + }; + const int test_result = test_run_group(tgroup, L); + utils_lua_close(L); + + return test_result; +} -- 2.34.1