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 160E9A62481; Tue, 26 Mar 2024 19:04:23 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 160E9A62481 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1711469063; bh=NDw0TKrXmHSgotrn0VlmYpCQZak8eaNxpCN0mfFRs/s=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=IsNRBOxvk90Im9JbZThI8rSa1bWf3V0hM1789/lbLBLlc3BBKd053iq9ox1lwWKA7 9L3Ht9ZMft0bA7GApojJZoTd1d6kBXVvq4jNhRWGareDWwNu5co0YpJ5RrIMB/1G3b sqIkcYdi86aGjhMqW0iQQww2RY/pQf6FA4mBmIAg= Received: from smtp44.i.mail.ru (smtp44.i.mail.ru [95.163.41.82]) (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 6644CA62481 for ; Tue, 26 Mar 2024 19:04:21 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 6644CA62481 Received: by smtp44.i.mail.ru with esmtpa (envelope-from ) id 1rp9Hg-00000002ZE0-030l; Tue, 26 Mar 2024 19:04:20 +0300 Date: Tue, 26 Mar 2024 19:04:18 +0300 To: Sergey Bronnikov Message-ID: <3nc2evmifho2awf6yzddx3uh7cskiwfafvtje46jnzhi47nwvv@x43uhh7wkbtx> References: <3bd73ab3f3a0e8b200c493ec09e65f5ecb711a6b.1711466825.git.sergeyb@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3bd73ab3f3a0e8b200c493ec09e65f5ecb711a6b.1711466825.git.sergeyb@tarantool.org> X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD905284D235AF2CB533336A7736FE07746DD6BA29442A641A4182A05F53808504022CEE1170FFE2875F378A8CA21F699D6F3C5CD8931696DB835F82DB637AA1EBA4E779315AB36DA6C X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE78524CA6C6209E9ADEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637543EE1E955FF04C98638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D81FE57BE2765ACFD107E08B3BF99D64E4E4BD9FB2748DEED0CC7F00164DA146DAFE8445B8C89999728AA50765F7900637F3E38EE449E3E2AE389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC81D471462564A2E19F6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA73AA81AA40904B5D9A18204E546F3947C2A336C65186350916E0066C2D8992A164AD6D5ED66289B523666184CF4C3C14F6136E347CC761E07725E5C173C3A84C32F36E1858E9CF555BA3038C0950A5D36B5C8C57E37DE458B330BD67F2E7D9AF16D1867E19FE14079C09775C1D3CA48CFE478A468B35FE7671DD303D21008E298D5E8D9A59859A8B6B372FE9A2E580EFC725E5C173C3A84C3483320834B361D1F089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 0D63561A33F958A5423A161DD26A9CCD5002B1117B3ED69693C1321A4A0F19FAD57BAD45EC4C5DE1823CB91A9FED034534781492E4B8EEADD0953842B444AAC3BDAD6C7F3747799A X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF2F33ECFBF48594D98A4D84CB357C621E593E85A140AE8C5DA4D875FD7B243DF091CDF2D949345910F212F37558E72466C53CC54B4A8C8F705626F9AF0ED014171619486506AA8591C226CC413062362A913E6812662D5F2A54F6898A6FDCBDC72A617DFBE5FEC2C6383653B6C8D9AE0FD16FCAA6493B703A X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojhu0AwaaKnHopX8TKnvIBrA== X-Mailru-Sender: 7940E2A4EB16C9976F80DCDCD59EC227E31217AC4C460687F378A8CA21F699D6F3C5CD8931696DB8E2527C969975515CFF9FCECFB8D89CB6C77752E0C033A69E235A20A81F3B0E39AB3C5F247CB2F7F93A5DB60FBEB33A8A0DA7A0AF5A3A8387 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit] Prevent loop in snap_usedef(). 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: Maxim Kokryashkin via Tarantool-patches Reply-To: Maxim Kokryashkin Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Hi, Sergey! Thanks for the patch! Please consider my comments below. On Tue, Mar 26, 2024 at 06:29:11PM +0300, Sergey Bronnikov wrote: > From: Sergey Bronnikov > > Reported by XmiliaH. > > (cherry picked from commit 0e66fc96377853d898390f1a02723c54ec3a42f7) > > It is possible to get an infinite loop in a function `snap_usedef` > when a `UCLO` makes a tight loop. The description should include explanation for the cause of the issue and should explain how it was resolved. > > Sergey Bronnikov: > * added the description and the test for the problem > > Part of tarantool/tarantool#9595 > --- > Branch: https://github.com/tarantool/luajit/tree/ligurio/lj-736-prevent-loop-in-snap_usedef > Issues: > - https://github.com/LuaJIT/LuaJIT/issues/736 > - https://github.com/tarantool/tarantool/issues/9595 > > src/lj_snap.c | 7 ++- > .../lj-736-BC_UCLO-triggers-infinite-loop.lua | 59 +++++++++++++++++++ > 2 files changed, 65 insertions(+), 1 deletion(-) > create mode 100644 test/tarantool-tests/lj-736-BC_UCLO-triggers-infinite-loop.lua > > diff --git a/src/lj_snap.c b/src/lj_snap.c > index 5a00b5cd..0710e1f0 100644 > --- a/src/lj_snap.c > +++ b/src/lj_snap.c > @@ -252,7 +252,12 @@ static BCReg snap_usedef(jit_State *J, uint8_t *udf, > BCReg minslot = bc_a(ins); > if (op >= BC_FORI && op <= BC_JFORL) minslot += FORL_EXT; > else if (op >= BC_ITERL && op <= BC_JITERL) minslot += bc_b(pc[-2])-1; > - else if (op == BC_UCLO) { pc += bc_j(ins); break; } > + else if (op == BC_UCLO) { > + ptrdiff_t delta = bc_j(ins); > + if (delta < 0) return maxslot; /* Prevent loop. */ > + pc += delta; > + break; > + } > for (s = minslot; s < maxslot; s++) DEF_SLOT(s); > return minslot < maxslot ? minslot : maxslot; > } > diff --git a/test/tarantool-tests/lj-736-BC_UCLO-triggers-infinite-loop.lua b/test/tarantool-tests/lj-736-BC_UCLO-triggers-infinite-loop.lua > new file mode 100644 > index 00000000..28a2b61b > --- /dev/null > +++ b/test/tarantool-tests/lj-736-BC_UCLO-triggers-infinite-loop.lua > @@ -0,0 +1,59 @@ > +local tap = require('tap') > +local test = tap.test('lj-736-BC_UCLO-triggers-infinite-loop'):skipcond({ > + ['Test requires JIT enabled'] = not jit.status(), > +}) > + > +test:plan(1) > + > +-- Test reproduces an issue when BC_UCLO triggers an infinite loop. > +-- See details in https://github.com/LuaJIT/LuaJIT/issues/736. > +-- > +-- Listing below demonstrates a problem - > +-- the bytecode UCLO on the line 13 makes a loop at 0013-0014: > +-- > +-- - BYTECODE -- bc_uclo.lua:0-20 > +-- 0001 KPRI 0 0 > +-- 0002 FNEW 1 0 ; bc_uclo.lua:5 > +-- 0003 KSHORT 2 1 > +-- 0004 KSHORT 3 4 > +-- 0005 KSHORT 4 1 > +-- 0006 FORI 2 => 0011 > +-- 0007 => ISNEN 5 0 ; 2 > +-- 0008 JMP 6 => 0010 > +-- 0009 UCLO 0 => 0012 > +-- 0010 => FORL 2 => 0007 > +-- 0011 => UCLO 0 => 0012 > +-- 0012 => KPRI 0 0 > +-- 0013 UCLO 0 => 0012 > +-- 0014 FNEW 1 1 ; bc_uclo.lua:18 > +-- 0015 UCLO 0 => 0016 > +-- 0016 => RET0 0 1 > + > +jit.opt.start('hotloop=1') > + > +do > + local uv = 0 > + local w = function() return uv end -- luacheck: no unused > + for i = 1, 2 do Add a comment that we have two iterations only because we only need to record the trace. > + -- Infinite loop is here. > + if i == 2 then > + if i == 2 then > + goto pass > + end > + goto unreachable > + end > + end > +end > + > +::unreachable:: > +-- Lua chunk below is required for reproducing a bug. > +do > + local uv = 0 -- luacheck: no unused > + goto unreachable > + local w = function() return uv end -- luacheck: ignore > +end > + > +::pass:: Please add a comment explaining why do we need a goto statement and an unreachable code segment. > + > +test:ok(true, 'BC_UCLO does not trigger an infinite loop') > +os.exit(test:check() and 0 or 1) The test executes without any failures for the x86 non-GC64 build before the patch. The exact command: $ cmake .. -DCMAKE_BUILD_TYPE=Debug -DLUA_USE_ASSERT=ON -DLUA_USE_APICHECK=ON && make -j && make test > -- > 2.34.1 >