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 657F09B9004; Tue, 12 Mar 2024 10:43:47 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 657F09B9004 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1710229427; bh=Zhvyl35K2PR7bBtRIn4M0MZEETCeciVcG4vGirlwvfs=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=TvLw80lF+qAGyuxuWy0I1AnIz3LnI9UbCuR8tp0Wg5gS1msqo4Gr6IXeaOYoHU7+z 09IffPf/PiI/xGJ54soDJMivyqXHoiIEybJXv3Ky0T9OfE1wrYCmEMtua342/Q/t9g kJz/zvAI2uTbAg5/ExtCbGwDikLOIZ4NLpsnVnMI= Received: from smtp30.i.mail.ru (smtp30.i.mail.ru [95.163.41.71]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 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 A455B9B9004 for ; Tue, 12 Mar 2024 10:43:45 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A455B9B9004 Received: by smtp30.i.mail.ru with esmtpa (envelope-from ) id 1rjwnX-0000000CUK4-2ith; Tue, 12 Mar 2024 10:43:44 +0300 Message-ID: <9ace95fc-99ee-428e-8c74-bef56a42508b@tarantool.org> Date: Tue, 12 Mar 2024 10:43:43 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: Sergey Kaplun , Maxim Kokryashkin Cc: tarantool-patches@dev.tarantool.org References: <20240311123007.25384-1-skaplun@tarantool.org> In-Reply-To: <20240311123007.25384-1-skaplun@tarantool.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD987C0EE6E7F0A597D77DAD88DB19A0E871D0E83388B9166C4182A05F538085040EAE567F3829457900578E6996F383413B35E84FE39291E65018471CBC2935FBE2D7423A3730B9CE4 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7CB9C0C663C74145DEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063790A3F9E01CE65FDC8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8B96E6A45E0691C3DF91C837445B88EFC8520708E66121CE9CC7F00164DA146DAFE8445B8C89999728AA50765F79006372A3B24BF85B2E607389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC8D2DCF9CF1F528DBCF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE33AC447995A7AD181150BA43C84913FC3A03B725D353964B2FFDA4F57982C5F435872C767BF85DA227C277FBC8AE2E8B4E4EA94BC0D253D575ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: 0D63561A33F958A52EBA0497824F3BBA5002B1117B3ED6960817072F2E9E944F8B25839F35DFE037823CB91A9FED034534781492E4B8EEADF12279BA039A6965BDAD6C7F3747799A X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF219D058A2C5A5C2D451CF85D0B7D1DB89D2CD1A6054DEE0D909CF1E036CA446C7E72D8710CBEBF0E748ED927723DE83FB9970905ED55EAC006F90A3FBC13508515A18D5515D1A6B15F4332CA8FE04980913E6812662D5F2AB9AF64DB4688768036DF5FE9C0001AF333F2C28C22F508233FCF178C6DD14203 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojWDseZ7hBpnhA/G/GGju+Gw== X-DA7885C5: 0375083DD3665445F255D290C0D534F96479DDF2AB71CC1537FF7CC8F88C27D6A3035956F11E8D395B1A4C17EAA7BC4BEF2421ABFA55128DAF83EF9164C44C7E X-Mailru-Sender: 689FA8AB762F7393590D8C940224AE339A72EF229DD03D4A55B7F9E70BAF6E0477F821901A89717EEF86D5F70DA33880E41E8EF7A07863ECB274557F927329BE2DDF8182D28ACDB545BD1C3CC395C826B4A721A3011E896F X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit] Fix recording of __concat metamethod. 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" Hi, Sergey thanks for the patch! LGTM On 3/11/24 15:30, Sergey Kaplun wrote: > From: Mike Pall > > Thanks to Sergey Kaplun. > > (cherry picked from commit bcc5125a9188179d23c223b8865ed94951fb91b3) > > `lj_record_ret()` uses `J->L->base` as a base for recording > concatenation with remaining operands, even if there is a vararg frame > or pcall frame (so it uses the wrong Lua stack slots from a frame > above). This leads to the corresponding assertion failure in the > `lj_record_mm_lookup()`. > > This patch adds the calculation of the base adjusment necessary for > correct frame usage. > > Sergey Kaplun: > * added the description and the test for the problem > > Part of tarantool/tarantool#9595 > --- > > Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-1164-record-meta-concat-varg-pcall > Tarantool PR: https://github.com/tarantool/tarantool/pull/9788 > Related issues: > * https://github.com/tarantool/tarantool/issues/9595 > * https://github.com/LuaJIT/LuaJIT/issues/1164 > > src/lj_record.c | 8 ++- > ...164-record-meta-concat-varg-pcall.test.lua | 49 +++++++++++++++++++ > 2 files changed, 55 insertions(+), 2 deletions(-) > create mode 100644 test/tarantool-tests/lj-1164-record-meta-concat-varg-pcall.test.lua > > diff --git a/src/lj_record.c b/src/lj_record.c > index c01c1f0b..77818fec 100644 > --- a/src/lj_record.c > +++ b/src/lj_record.c > @@ -819,6 +819,7 @@ void lj_record_ret(jit_State *J, BCReg rbase, ptrdiff_t gotresults) > { > TValue *frame = J->L->base - 1; > ptrdiff_t i; > + BCReg baseadj = 0; > for (i = 0; i < gotresults; i++) > (void)getslot(J, rbase+i); /* Ensure all results have a reference. */ > while (frame_ispcall(frame)) { /* Immediately resolve pcall() returns. */ > @@ -827,6 +828,7 @@ void lj_record_ret(jit_State *J, BCReg rbase, ptrdiff_t gotresults) > lj_trace_err(J, LJ_TRERR_NYIRETL); > lj_assertJ(J->baseslot > 1+LJ_FR2, "bad baseslot for return"); > gotresults++; > + baseadj += cbase; > rbase += cbase; > J->baseslot -= (BCReg)cbase; > J->base -= cbase; > @@ -851,6 +853,7 @@ void lj_record_ret(jit_State *J, BCReg rbase, ptrdiff_t gotresults) > if (--J->framedepth < 0) /* NYI: return of vararg func to lower frame. */ > lj_trace_err(J, LJ_TRERR_NYIRETL); > lj_assertJ(J->baseslot > 1+LJ_FR2, "bad baseslot for return"); > + baseadj += cbase; > rbase += cbase; > J->baseslot -= (BCReg)cbase; > J->base -= cbase; > @@ -918,7 +921,8 @@ void lj_record_ret(jit_State *J, BCReg rbase, ptrdiff_t gotresults) > BCReg bslot = bc_b(*(frame_contpc(frame)-1)); > TRef tr = gotresults ? J->base[cbase+rbase] : TREF_NIL; > if (bslot != J->maxslot) { /* Concatenate the remainder. */ > - TValue *b = J->L->base, save; /* Simulate lower frame and result. */ > + /* Simulate lower frame and result. */ > + TValue *b = J->L->base - baseadj, save; > /* Can't handle MM_concat + CALLT + fast func side-effects. */ > if (J->postproc != LJ_POST_NONE) > lj_trace_err(J, LJ_TRERR_NYIRETL); > @@ -931,7 +935,7 @@ void lj_record_ret(jit_State *J, BCReg rbase, ptrdiff_t gotresults) > J->L->base = b - cbase; > tr = rec_cat(J, bslot, cbase-(2< b = J->L->base + cbase; /* Undo. */ > - J->L->base = b; > + J->L->base = b + baseadj; > copyTV(J->L, b-(2< } > if (tr) { /* Store final result. */ > diff --git a/test/tarantool-tests/lj-1164-record-meta-concat-varg-pcall.test.lua b/test/tarantool-tests/lj-1164-record-meta-concat-varg-pcall.test.lua > new file mode 100644 > index 00000000..987e49a0 > --- /dev/null > +++ b/test/tarantool-tests/lj-1164-record-meta-concat-varg-pcall.test.lua > @@ -0,0 +1,49 @@ > +local tap = require('tap') > + > +-- Test file to demonstrate the incorrect recording of the > +-- `__concat` metamethod with the VARG or protected frame for the > +-- function used in the `__concat`, when we have to record more > +-- than 2 objects. > +-- See also: https://github.com/LuaJIT/LuaJIT/issues/1164. > + > +local test = tap.test('lj-1164-record-meta-concat-varg-pcall'):skipcond({ > + ['Test requires JIT enabled'] = not jit.status(), > +}) > + > +test:plan(2) > + > +jit.opt.start('hotloop=1') > + > +-- Table with the simplest varg metamethod to call. > +local tab_varg = setmetatable({}, { > + __concat = function(...) > + local _, tab_obj = ... > + return tab_obj > + end, > +}) > + > +local result > +for _ = 1, 4 do > + -- The concatenation has right to left associativity. > + result = '' .. '' .. tab_varg > +end > + > +test:is(result, tab_varg, 'varg frame is recorded correctly') > + > +-- Table with the simplest protected metamethod. > +local tab_pcall = setmetatable({}, { > + -- Need to use `__call` metamethod to make the object callable. > + __call = function(tab_obj) > + return tab_obj > + end, > + __concat = pcall, > +}) > + > +for _ = 1, 4 do > + -- The concatenation has right to left associativity. > + result = tab_pcall .. tab_pcall .. '' > +end > + > +test:is(result, true, 'protected frame is recorded correctly') > + > +test:done(true)