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 CA903404481; Thu, 9 Nov 2023 15:31:04 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org CA903404481 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1699533064; bh=GTsaWE5HkHLJQmxgiCp3O0D/+q11rvylOo+9dGBPZYE=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=VCXMDsyDb6J1JjDzmDk9oAfmJAN979w0Cs6jR5PUbbLq0d84EfoU1eT6CExhbV420 CGV66bIOcgtlC0uh9FFyAPTXrtE9apJV15BAlGWdpbNzcR1wHNPQ9hpEIyAziuLWbp SsqwV8cVfmtEX3T9gl2Gj7lOrat3uACeV8nmgEiU= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 58D4E404481 for ; Thu, 9 Nov 2023 15:31:03 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 58D4E404481 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1r14Ba-0003hZ-2Z; Thu, 09 Nov 2023 15:31:02 +0300 To: Maxim Kokryashkin , Sergey Bronnikov Date: Thu, 9 Nov 2023 15:26:28 +0300 Message-ID: <20231109122628.19853-1-skaplun@tarantool.org> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-7564579A: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD9562B317E0136C8D346679895B712F137819A527523E4D068182A05F5380850404C228DA9ACA6FE273022949B5558D1517E1061DF8EEC926BBB6D6E2DBFA61B8D6D14C43A5CD8ED2D X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE721AF84DC1D70954DEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006373255E97C90679D468638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D866735C424E45F8DFBB91F22C32562D30117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC60CDF180582EB8FBA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD1828451B159A507268D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269176DF2183F8FC7C007B7C037059AA88D68655334FD4449CB9ECD01F8117BC8BEAAAE862A0553A39223F8577A6DFFEA7C3B6C7E47A292E8D043847C11F186F3C59DAA53EE0834AAEE X-C1DE0DAB: 0D63561A33F958A5987B15888104D6781CD97707E95E7ADEFAAE2A2F63348014F87CCE6106E1FC07E67D4AC08A07B9B065B78C30F681404D9C5DF10A05D560A950611B66E3DA6D700B0A020F03D25A0997E3FB2386030E77 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF77DD89D51EBB7742D3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CFA8B7F669D1D23B469B5BD37F5F1528295D5B16034DEA2EA6C8CA0FC6AE216E4C25059624982F7461D0DB820CB2E38D8B8149E5EE8FE2A5F11B50B76340C6C0BDA74DFFEFA5DC0E7F02C26D483E81D6BE5EF9655DD6DEA7D65774BB76CC95456EEC5B5AD62611EEC62B5AFB4261A09AF0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojbL9S8ysBdXgV7r44UsqoY1CMabJi38zX X-DA7885C5: 89D8694F2752D676C4482E5D2D9FC3A1C8DCE612991C9973042B69A50FB5E1ED262E2D401490A4A0DB037EFA58388B346E8BC1A9835FDE71 X-Mailru-Sender: 689FA8AB762F7393590D8C940224AE3340D7C8723E4A3809325F67EC2938EFCF0FBE9A32752B8C9C2AA642CC12EC09F1FB559BB5D741EB962F61BD320559CF1EFD657A8799238ED55FEEDEB644C299C0ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit] Fix HREFK forwarding vs. table.clear(). 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 Kaplun via Tarantool-patches Reply-To: Sergey Kaplun Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Mike Pall Reported by XmiliaH. (cherry-picked from commit d5a237eae03d2ad346f82390836371a952e9a286) When performing HREFK (and also ALOAD, HLOAD) forwarding optimization, the `table.clear()` function call may be performed on the table operand from HREFK between table creation and IR, from which value is forwarded. This call isn't taken in the account, so it may lead to too optimistic value-forwarding from NEWREF (and also ASTORE, HSTORE), or the omitted type guard for HREFK operation. Therefore, this leads to incorrect trace behaviour (for example, taking a non-nil value from the cleared table). This patch adds necessary checks for `table.clear()` calls. Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#9145 --- Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-792-hrefk-table-clear Tarantool PR: https://github.com/tarantool/tarantool/pull/9351 Relate issues: * https://github.com/LuaJIT/LuaJIT/issues/792 * https://github.com/tarantool/tarantool/issues/9145 src/lj_opt_mem.c | 63 +++--- .../lj-792-hrefk-table-clear.test.lua | 181 ++++++++++++++++++ 2 files changed, 213 insertions(+), 31 deletions(-) create mode 100644 test/tarantool-tests/lj-792-hrefk-table-clear.test.lua diff --git a/src/lj_opt_mem.c b/src/lj_opt_mem.c index a83558ac..24a490d5 100644 --- a/src/lj_opt_mem.c +++ b/src/lj_opt_mem.c @@ -72,6 +72,34 @@ static AliasRet aa_table(jit_State *J, IRRef ta, IRRef tb) return aa_escape(J, taba, tabb); } +/* Check whether there's no aliasing table.clear. */ +static int fwd_aa_tab_clear(jit_State *J, IRRef lim, IRRef ta) +{ + IRRef ref = J->chain[IR_CALLS]; + while (ref > lim) { + IRIns *calls = IR(ref); + if (calls->op2 == IRCALL_lj_tab_clear && + (ta == calls->op1 || aa_table(J, ta, calls->op1) != ALIAS_NO)) + return 0; /* Conflict. */ + ref = calls->prev; + } + return 1; /* No conflict. Can safely FOLD/CSE. */ +} + +/* Check whether there's no aliasing NEWREF/table.clear for the left operand. */ +int LJ_FASTCALL lj_opt_fwd_tptr(jit_State *J, IRRef lim) +{ + IRRef ta = fins->op1; + IRRef ref = J->chain[IR_NEWREF]; + while (ref > lim) { + IRIns *newref = IR(ref); + if (ta == newref->op1 || aa_table(J, ta, newref->op1) != ALIAS_NO) + return 0; /* Conflict. */ + ref = newref->prev; + } + return fwd_aa_tab_clear(J, lim, ta); +} + /* Alias analysis for array and hash access using key-based disambiguation. */ static AliasRet aa_ahref(jit_State *J, IRIns *refa, IRIns *refb) { @@ -154,7 +182,8 @@ static TRef fwd_ahload(jit_State *J, IRRef xref) IRIns *ir = (xr->o == IR_HREFK || xr->o == IR_AREF) ? IR(xr->op1) : xr; IRRef tab = ir->op1; ir = IR(tab); - if (ir->o == IR_TNEW || (ir->o == IR_TDUP && irref_isk(xr->op2))) { + if ((ir->o == IR_TNEW || (ir->o == IR_TDUP && irref_isk(xr->op2))) && + fwd_aa_tab_clear(J, tab, tab)) { /* A NEWREF with a number key may end up pointing to the array part. ** But it's referenced from HSTORE and not found in the ASTORE chain. ** Or a NEWREF may rehash the table and move unrelated number keys. @@ -275,7 +304,7 @@ TRef LJ_FASTCALL lj_opt_fwd_hrefk(jit_State *J) while (ref > tab) { IRIns *newref = IR(ref); if (tab == newref->op1) { - if (fright->op1 == newref->op2) + if (fright->op1 == newref->op2 && fwd_aa_tab_clear(J, ref, tab)) return ref; /* Forward from NEWREF. */ else goto docse; @@ -285,7 +314,7 @@ TRef LJ_FASTCALL lj_opt_fwd_hrefk(jit_State *J) ref = newref->prev; } /* No conflicting NEWREF: key location unchanged for HREFK of TDUP. */ - if (IR(tab)->o == IR_TDUP) + if (IR(tab)->o == IR_TDUP && fwd_aa_tab_clear(J, tab, tab)) fins->t.irt &= ~IRT_GUARD; /* Drop HREFK guard. */ docse: return CSEFOLD; @@ -319,34 +348,6 @@ int LJ_FASTCALL lj_opt_fwd_href_nokey(jit_State *J) return 1; /* No conflict. Can fold to niltv. */ } -/* Check whether there's no aliasing table.clear. */ -static int fwd_aa_tab_clear(jit_State *J, IRRef lim, IRRef ta) -{ - IRRef ref = J->chain[IR_CALLS]; - while (ref > lim) { - IRIns *calls = IR(ref); - if (calls->op2 == IRCALL_lj_tab_clear && - (ta == calls->op1 || aa_table(J, ta, calls->op1) != ALIAS_NO)) - return 0; /* Conflict. */ - ref = calls->prev; - } - return 1; /* No conflict. Can safely FOLD/CSE. */ -} - -/* Check whether there's no aliasing NEWREF/table.clear for the left operand. */ -int LJ_FASTCALL lj_opt_fwd_tptr(jit_State *J, IRRef lim) -{ - IRRef ta = fins->op1; - IRRef ref = J->chain[IR_NEWREF]; - while (ref > lim) { - IRIns *newref = IR(ref); - if (ta == newref->op1 || aa_table(J, ta, newref->op1) != ALIAS_NO) - return 0; /* Conflict. */ - ref = newref->prev; - } - return fwd_aa_tab_clear(J, lim, ta); -} - /* ASTORE/HSTORE elimination. */ TRef LJ_FASTCALL lj_opt_dse_ahstore(jit_State *J) { diff --git a/test/tarantool-tests/lj-792-hrefk-table-clear.test.lua b/test/tarantool-tests/lj-792-hrefk-table-clear.test.lua new file mode 100644 index 00000000..e662e0cc --- /dev/null +++ b/test/tarantool-tests/lj-792-hrefk-table-clear.test.lua @@ -0,0 +1,181 @@ +local tap = require('tap') + +-- Test file to demonstrate LuaJIT incorrect optimizations across +-- the `table.clear()` call. +-- See also: https://github.com/LuaJIT/LuaJIT/issues/792. + +local test = tap.test('lj-792-hrefk-table-clear'):skipcond({ + ['Test requires JIT enabled'] = not jit.status(), +}) +local table_clear = require('table.clear') + +test:plan(7) + +local NITERATIONS = 4 +local MAGIC = 42 + +local function test_aref_fwd_tnew(tab_number) + local field_value_after_clear + for i = 1, NITERATIONS do + -- Create a table on trace to make the optimization work. + -- Initialize the first field to work with the array part. + local tab = {i} + -- Use an additional table to alias the created table with the + -- `1` key. + local tab_array = {tab, {0}} + -- AREF to be forwarded. + tab[1] = MAGIC + table_clear(tab_array[tab_number]) + -- It should be `nil`, since table is cleared. + field_value_after_clear = tab[1] + end + return field_value_after_clear +end + +local function test_aref_fwd_tdup(tab_number) + local field_value_after_clear + for _ = 1, NITERATIONS do + -- Create a table on trace to make the optimization work. + local tab = {nil} + -- Use an additional table to alias the created table with the + -- `1` key. + local tab_array = {tab, {0}} + -- AREF to be forwarded. + tab[1] = MAGIC + table_clear(tab_array[tab_number]) + -- It should be `nil`, since table is cleared. + field_value_after_clear = tab[1] + end + return field_value_after_clear +end + +local function test_href_fwd_tnew(tab_number) + local field_value_after_clear + for _ = 1, NITERATIONS do + -- Create a table on trace to make the optimization work. + local tab = {} + -- Use an additional table to alias the created table with the + -- `8` key. + local tab_array = {tab, {0}} + -- HREF to be forwarded. Use 8 to be in the hash part. + tab[8] = MAGIC + table_clear(tab_array[tab_number]) + -- It should be `nil`, since table is cleared. + field_value_after_clear = tab[8] + end + return field_value_after_clear +end + +local function test_href_fwd_tdup(tab_number) + local field_value_after_clear + for _ = 1, NITERATIONS do + -- Create a table on trace to make the optimization work. + local tab = {nil} + -- Use an additional table to alias the created table with the + -- `8` key. + local tab_array = {tab, {0}} + -- HREF to be forwarded. Use 8 to be in the hash part. + tab[8] = MAGIC + table_clear(tab_array[tab_number]) + -- It should be `nil`, since table is cleared. + field_value_after_clear = tab[8] + end + return field_value_after_clear +end + +jit.opt.start('hotloop=1') + +-- First compile the trace with a clearing not-interesting table. +test_aref_fwd_tnew(2) +-- Now run the trace with the clearing table, from which we take +-- AREF. +test:is(test_aref_fwd_tnew(1), nil, 'AREF forward from TNEW') + +-- XXX: Reset hotcounters to avoid collisions. +jit.opt.start('hotloop=1') + +-- First compile the trace with a clearing not-interesting table. +test_aref_fwd_tdup(2) +-- Now run the trace with the clearing table, from which we take +-- AREF. +test:is(test_aref_fwd_tdup(1), nil, 'AREF forward from TDUP') + +-- XXX: Reset hotcounters to avoid collisions. +jit.opt.start('hotloop=1') + +-- First compile the trace with a clearing not-interesting table. +test_href_fwd_tnew(2) +-- Now run the trace with the clearing table, from which we take +-- HREF. +test:is(test_href_fwd_tnew(1), nil, 'HREF forward from TNEW') + +-- XXX: Reset hotcounters to avoid collisions. +jit.opt.start('hotloop=1') + +-- First compile the trace with a clearing not-interesting table. +test_href_fwd_tdup(2) +-- Now run the trace with the clearing table, from which we take +-- HREF. +test:is(test_href_fwd_tdup(1), nil, 'HREF forward from TDUP') + +local function test_not_forwarded_hrefk_val_from_newref(tab_number) + local field_value_after_clear + for _ = 1, NITERATIONS do + -- Create a table on trace to make the optimization work. + local tab = {} + -- NEWREF to be forwarded. + tab.hrefk = MAGIC + -- Use an additional table to alias the created table with the + -- `hrefk` key. + local tab_array = {tab, {hrefk = 0}} + table_clear(tab_array[tab_number]) + -- It should be `nil`, since it is cleared. + field_value_after_clear = tab.hrefk + end + return field_value_after_clear +end + +-- XXX: Reset hotcounters to avoid collisions. +jit.opt.start('hotloop=1') + +-- First compile the trace with a clearing not-interesting table. +test_not_forwarded_hrefk_val_from_newref(2) +-- Now run the trace with the clearing table, from which we take +-- HREFK. +local value_from_cleared_tab = test_not_forwarded_hrefk_val_from_newref(1) + +test:is(value_from_cleared_tab, nil, + 'not forward the field value across table.clear') + +local function test_not_dropped_guard_on_hrefk(tab_number) + local tab, field_value_after_clear + for _ = 1, NITERATIONS do + -- Create a table on trace to make the optimization work. + tab = {hrefk = MAGIC} + -- Use an additional table to alias the created table with the + -- `hrefk` key. + local tab_array = {tab, {hrefk = 0}} + table_clear(tab_array[tab_number]) + -- It should be `nil`, since it is cleared. + -- If the guard is dropped for HREFK, the value from the TDUP + -- table is taken instead, without the type check. This leads + -- to incorrect (swapped) returned values. + field_value_after_clear = tab.hrefk + tab.hrefk = MAGIC + end + return field_value_after_clear, tab.hrefk +end + +-- XXX: Reset hotcounters to avoid collisions. +jit.opt.start('hotloop=1') + +-- First compile the trace with clearing not interesting table. +test_not_dropped_guard_on_hrefk(2) +-- Now run the trace with the clearing table, from which we take +-- HREFK. +local field_value_after_clear, tab_hrefk = test_not_dropped_guard_on_hrefk(1) + +test:is(field_value_after_clear, nil, 'correct field value after table.clear') +test:is(tab_hrefk, MAGIC, 'correct value set in the table that was cleared') + +test:done(true) -- 2.42.0