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 F3989480007; Fri, 6 Oct 2023 17:16:24 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org F3989480007 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1696601785; bh=1VyFWAagLD7xOKKt6uONE5n88JS3tFGHjj8QxfPZlKQ=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=vc3NtzSVgRzo9ShUSmfk9/KqGCtsW0uv5zZAKThMj5hAh5aa/b0AKKypgLlXwhMlr m1Bt/Ug8XT7Sq60MImM8He+2joQ6mngIJiLZw5if8cvPIufbaoDmY9/j6e6E1WrhH2 z4sK20F8neYAnbmCXNDp0IZeJj12CzOvuYnYLu+g= Received: from mail-ed1-f52.google.com (mail-ed1-f52.google.com [209.85.208.52]) (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 7418F480007 for ; Fri, 6 Oct 2023 17:16:23 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 7418F480007 Received: by mail-ed1-f52.google.com with SMTP id 4fb4d7f45d1cf-5363227cc80so3622688a12.3 for ; Fri, 06 Oct 2023 07:16:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696601782; x=1697206582; 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=arJGkagcLO24oeOfWIUpMCiV90hEfslB9BfW0MnxmT8=; b=earHMQV2xPSPCofGUJjbwx9IQ62RIPHXXbcwLulAevWXanyuLxIczcNuYeQe0zdzPP q11RMk52QbdlyQSQKtR1EePkHNVk8noYXeMJ1f1Ih68HMspyj6yzSQ/chWRMsCoagk4Y IEkcl0t/FNWgSm8FGdhviGEzs4fHkwiuEFJRhjSp9OQCOmMlwjb/zzIIjs7x1AV0k1vE JHlrZjTuNDWe4d5J5s1mKwB2MgoRFmvK/uPo1OQPQxBMFK8KqgKL7l46Pp1jUxzaw3N/ KML/NoqCxsfqHITBjJv20MrwbxYx5SWlVjLHyMVQ8ABn96P/QUDxkPNN6PlgwBYUTLUT yDGA== X-Gm-Message-State: AOJu0Yw+CvDCF9h1Rwc4bC5Ogd73npBOWBKUJSP2sYqudX01xD6xmA48 P/pBv6XFUG7DS1G6XvnMmL8rDSp6Ayg= X-Google-Smtp-Source: AGHT+IGhycEvefFnDT8bOVPWq87icrYhGkFrni7j8DmE+zEkswdBhrleAa056Ufvuel5dIiJoHlRvg== X-Received: by 2002:aa7:d947:0:b0:531:9c1:8271 with SMTP id l7-20020aa7d947000000b0053109c18271mr7707539eds.14.1696601781773; Fri, 06 Oct 2023 07:16:21 -0700 (PDT) Received: from pony.. ([185.6.247.97]) by smtp.gmail.com with ESMTPSA id h14-20020aa7de0e000000b00532eba07773sm2660393edv.25.2023.10.06.07.16.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Oct 2023 07:16:21 -0700 (PDT) To: tarantool-patches@dev.tarantool.org, Sergey Kaplun , max.kokryashkin@gmail.com Date: Fri, 6 Oct 2023 17:15:47 +0300 Message-Id: X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit][v2] 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 of the Lua stack, `lua_concat()` concatenates *four* items and leaves result on the top: 1 [string] 2 [string][string][string][userdata] <--- top The problem is in incorrect calculation of `n` counter in the loop in implementation of function `lua_concat`. Without the fix `n` is equal to 3 at the end of the first iteration and therefore it goes to the next iteration of concatenation. In the 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 the 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 Changes: - fixed typos - updated assertions (bool asserts replaced with int assertions) - replaced userdata with struct with userdata with integer per Max's request that's all, folks src/lj_api.c | 2 +- .../lj-881-fix-lua-concat.test.c | 95 +++++++++++++++++++ 2 files changed, 96 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..1b1f146f --- /dev/null +++ b/test/tarantool-c-tests/lj-881-fix-lua-concat.test.c @@ -0,0 +1,95 @@ +#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 MT_NAME "int" + +static int INFO_concat(lua_State *L) +{ + const char *s = luaL_checkstring(L, 1); + int *n = (int *)luaL_checkudata(L, 2, MT_NAME); + /* Do non-default concatenation. */ + lua_pushfstring(L, "%s + %d", s, *n); + return 1; +} + +static const luaL_Reg mt[] = +{ + {"__concat", INFO_concat}, + {NULL, NULL} +}; + +static int lua_concat_testcase(void *test_state) +{ + /* Setup. */ + lua_State *L = test_state; + const int top = 4; + + /* Create metatable and put it to the Lua registry. */ + luaL_newmetatable(L, MT_NAME); + /* Fill metatable. */ + luaL_register(L, 0, mt); + lua_pop(L, 1); + + assert_int_equal(lua_gettop(L), 0); + + lua_pushliteral(L, "C"); + lua_pushliteral(L, "B"); + lua_pushliteral(L, "A"); + + int *n = (int *)lua_newuserdata(L, sizeof(int)); + *n = 100; + + luaL_getmetatable(L, MT_NAME); + lua_setmetatable(L, -2); + + assert_int_equal(lua_gettop(L), top); + + /* Test body. */ + + /* + * 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 + */ + + /* Concatenate two elements on top. */ + lua_concat(L, 2); + + const char *str = lua_tostring(L, -1); + assert_int_equal(lua_gettop(L), top - 2 + 1); + const char expected_str[] = "A + 100"; + assert_str_equal(str, expected_str, strlen(expected_str)); + + /* Teardown. */ + 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