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 06818BC9EB1; Wed, 24 Apr 2024 13:41:29 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 06818BC9EB1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1713955289; bh=IulFDE9dCPMUpYTwtvpBTYnsOPDIsR/jGWvx3P2mXXc=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=x85ookw3O1qDYzMyOClSHcuHMDpzzXEpyvGvagU9JcvvZxZ3g4cvJKxeDTNS1ruyp Kcw5oCj+fi/sYLlrp2f/qjMaOfNh1fe359MrCnKXmWKYiWPodWHGHzDsHqV/WKllyT OlQzAujd0VnUizKomTziGTNz3xKNTXsJJxiwiJro= Received: from smtp16.i.mail.ru (smtp16.i.mail.ru [95.163.41.69]) (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 DD2DBBC9EB4 for ; Wed, 24 Apr 2024 13:41:26 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org DD2DBBC9EB4 Received: by smtp16.i.mail.ru with esmtpa (envelope-from ) id 1rza46-00000009G4O-0JBD; Wed, 24 Apr 2024 13:41:26 +0300 To: Maxim Kokryashkin , Sergey Bronnikov Date: Wed, 24 Apr 2024 13:37:20 +0300 Message-ID: <20240424103720.9464-1-skaplun@tarantool.org> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9CB0CA91FF61D77F075DB05CF72E457A865F07370B97DAE6A182A05F538085040FE5DB4A7E05C992A5D1BE6A8D71B10A51FC3CFB85E65ECA81356FFDEACC632FEC94F5D657025D6F1 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7922D113DFDC6D5A3EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063736901D4B68B3D4FF8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D845950E667F00B1F62AC12D29DBA020F0CE4475F1AD024150CC7F00164DA146DAFE8445B8C89999728AA50765F7900637D0FEED2715E18529389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC8062BEEFFB5F8EA3EF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA73AA81AA40904B5D9A18204E546F3947C2D01283D1ACF37BA6136E347CC761E074AD6D5ED66289B523666184CF4C3C14F6136E347CC761E07725E5C173C3A84C3BB75CFADA5C32A90BA3038C0950A5D36B5C8C57E37DE458B330BD67F2E7D9AF16D1867E19FE14079C09775C1D3CA48CF17B107DEF921CE791DD303D21008E298D5E8D9A59859A8B6B372FE9A2E580EFC725E5C173C3A84C3CE9959E2676FD87735872C767BF85DA2F004C90652538430E4A6367B16DE6309 X-C1DE0DAB: 0D63561A33F958A5CE748D0CE6B1E7565002B1117B3ED696E5DD320BD0AA593A3E67C18142C611B7823CB91A9FED034534781492E4B8EEAD6A17C1D737525568C79554A2A72441328621D336A7BC284946AD531847A6065A535571D14F44ED41 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF77DD89D51EBB7742D3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF383DF44B53C42D03251294B6F4E25783EBF5FE5DE4992D277930ED9D97C088FC8657CCA151A12AF5930DB2EDDBD5B4A181CC4932F6810268DE560F695B7FF6E6F2C5DEE2C21E6DFBC226CC413062362A913E6812662D5F2A5EAB5682573093F7837F15F2B5E4A70B33F2C28C22F508233FCF178C6DD14203 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojzz1Jr2ms4lVF0dQ7/8/Pug== X-Mailru-Sender: 520A125C2F17F0B1A9638AD358559B5990771A143E9EA4C35D1BE6A8D71B10A51FC3CFB85E65ECA8B7CBEF92542CD7C88B0A2698F12F5C9EC77752E0C033A69E86920BD37369036789A8C6A0E60D2BB63A5DB60FBEB33A8A0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit] Check for IR_HREF vs. IR_HREFK aliasing in non-nil store check. 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 Thanks to Peter Cawley. (cherry picked from commit 658530562c2ac7ffa8e4ca5d18856857471244e9) The `lj_opt_fwd_wasnonnil()` skips the check for HREF and HREFK that may alias. Hence, the guard for the non-nil value may be skipped, and the `__newindex` metamethod call is omitted too. This patch adds the aforementioned check for different reference types (HREF vs. HREFK), which were not detected by the previous analysis. Also, the helper macro `irt_isp32()` is introduced to check that the IR type is `IRT_P32` (KSLOT type). Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#9924 --- Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-1133-fwd-href-hrefk-alias Related issues: * https://github.com/tarantool/tarantool/issues/9924 * https://github.com/LuaJIT/LuaJIT/issues/1133 src/lj_ir.h | 1 + src/lj_opt_mem.c | 2 + .../lj-1133-fwd-href-hrefk-alias.test.lua | 94 +++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 test/tarantool-tests/lj-1133-fwd-href-hrefk-alias.test.lua diff --git a/src/lj_ir.h b/src/lj_ir.h index e9b8328e..27c66f63 100644 --- a/src/lj_ir.h +++ b/src/lj_ir.h @@ -367,6 +367,7 @@ typedef struct IRType1 { uint8_t irt; } IRType1; #define irt_isu32(t) (irt_type(t) == IRT_U32) #define irt_isi64(t) (irt_type(t) == IRT_I64) #define irt_isu64(t) (irt_type(t) == IRT_U64) +#define irt_isp32(t) (irt_type(t) == IRT_P32) #define irt_isfp(t) (irt_isnum(t) || irt_isfloat(t)) #define irt_isinteger(t) (irt_typerange((t), IRT_I8, IRT_INT)) diff --git a/src/lj_opt_mem.c b/src/lj_opt_mem.c index 9044f09a..c9f1216c 100644 --- a/src/lj_opt_mem.c +++ b/src/lj_opt_mem.c @@ -916,6 +916,8 @@ int lj_opt_fwd_wasnonnil(jit_State *J, IROpT loadop, IRRef xref) if (skref == xkref || !irref_isk(skref) || !irref_isk(xkref)) return 0; /* A nil store with same const key or var key MAY alias. */ /* Different const keys CANNOT alias. */ + } else if (irt_isp32(IR(skref)->t) != irt_isp32(IR(xkref)->t)) { + return 0; /* HREF and HREFK MAY alias. */ } /* Different key types CANNOT alias. */ } /* Other non-nil stores MAY alias. */ ref = store->prev; diff --git a/test/tarantool-tests/lj-1133-fwd-href-hrefk-alias.test.lua b/test/tarantool-tests/lj-1133-fwd-href-hrefk-alias.test.lua new file mode 100644 index 00000000..6b72c97a --- /dev/null +++ b/test/tarantool-tests/lj-1133-fwd-href-hrefk-alias.test.lua @@ -0,0 +1,94 @@ +local tap = require('tap') + +-- Test file to demonstrate the LuaJIT's incorrect aliasing check +-- for HREFK and HREF IRs during the non-nil check. +-- See also: https://github.com/LuaJIT/LuaJIT/issues/1133. + +local test = tap.test('lj-1133-fwd-href-hrefk-alias'):skipcond({ + ['Test requires JIT enabled'] = not jit.status(), +}) +test:plan(1) + +local rawset = rawset + +-- The maximum value that can be stored in a 16-bit `op2` +-- field in HREFK IR. +local HASH_NODES = 65535 + +-- Amount of iteration to compile and execute the trace. +local LOOP_LIMIT = 4 + +-- Function to be called twice to emit the trace and take the side +-- exit. +local function trace_aliased_tables(t1, t2) + -- The criteria is the number of new index creations. + local count = 0 + local mt = {__newindex = function(t, k, v) + count = count + 1 + rawset(t, k, v) + end} + setmetatable(t1, mt) + setmetatable(t2, mt) + + for _ = 1, LOOP_LIMIT do + -- XXX: Keys values have no special meaning here, just be sure + -- that they are HREF/HREFK and not in the array table part. + -- `t1` is empty, emitting HREFK. + t1[10] = 1 + -- `t2` on recording has more than `HASH_NODES` table nodes, + -- so this emits HREF. + t2[10] = nil + -- Resolve `__newindex` if t1 == t2. + -- `lj_opt_fwd_wasnonnil()` missed the check that HREFK and + -- HREF may alias before the patch, so the guarded HLOAD IR + -- with the corresponding snapshot is skipped. + -- The difference in the emitted IR before and afterthe patch + -- is the following: + -- | 0004 > tab SLOAD #1 T + -- | ... + -- | 0009 p32 FLOAD 0004 tab.node + -- | 0010 > p32 HREFK 0009 +10 @0 + -- | 0011 > num HLOAD 0010 + -- | 0012 num HSTORE 0010 +1 + -- | .... SNAP #1 + -- | 0013 > tab SLOAD #2 T + -- | 0014 int FLOAD 0013 tab.asize + -- | 0015 > int ULE 0014 +10 + -- | 0016 p32 HREF 0013 +10 + -- | 0017 > p32 NE 0016 [0x415554e8] + -- | 0018 > num HLOAD 0016 + -- | 0019 nil HSTORE 0016 nil + -- | -0020 num HSTORE 0010 +30 + -- | .... SNAP #2 + -- | +0020 > num HLOAD 0010 + -- | +0021 num HSTORE 0010 +30 + -- | +.... SNAP #3 + -- + -- Hence, the taken exit is not resolving `__newindex` before + -- the patch. + t1[10] = 1 + -- The exit 2 of the trace is here. + -- Resolve `__newindex` if t1 ~= t2. + t2[10] = 1 + end + -- `__newindex` is called twice on the first iteration and once + -- on each other. + return count == LOOP_LIMIT + 1 +end + +-- Create a big table to emit HREF IR (not HREFK) to trick +-- the alias checking. +local bigt = {} +for i = 1, HASH_NODES + 1 do + bigt[-i] = true +end + +jit.opt.start('hotloop=1') + +trace_aliased_tables({}, bigt) + +-- Now use tables that are aliased. +local smallt = {} +test:ok(trace_aliased_tables(smallt, smallt), 'aliasing check is correct') + +test:done(true) -- 2.44.0