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 A99D4132E485; Wed, 12 Mar 2025 18:36:20 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A99D4132E485 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1741793780; bh=4vTLtBB3uFCsKoTiwv1zWNdxe68I2+udqhi6WI07YaI=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=kYklEP1IPG/86Ju4IiOEmqswpSbKrWQfKq2uyvZMKEiUYww4Z7q1JBzP309NKwNFA iU4WPNhkj50IkxXedquV0AOJKJrh40qA5ZAewlTkNaX65+rf7URxP5wshjcCgmbdJn /e/hRQ1OVUEjipFPLM7OAG4+hXqb4Nc/RoJsUlmQ= Received: from send151.i.mail.ru (send151.i.mail.ru [89.221.237.246]) (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 9B8F6132E485 for ; Wed, 12 Mar 2025 18:36:19 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 9B8F6132E485 Received: by exim-smtp-69cc44787d-vgjk7 with esmtpa (envelope-from ) id 1tsO82-000000007bK-2mTF; Wed, 12 Mar 2025 18:36:19 +0300 To: Sergey Bronnikov Date: Wed, 12 Mar 2025 18:36:16 +0300 Message-ID: <20250312153616.13143-1-skaplun@tarantool.org> X-Mailer: git-send-email 2.48.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9CB00722163A93FBBA0C550EA33BC578BF701CD1B232FD50FCD62213F67905E7A18ED74ED8CA12DEFEDA12F13EF547394DDAE3046E843986BBDAF6484B7CCA83024DAF05A372A3159 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7590E57235B5C00BDEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637AC83A81C8FD4AD23D82A6BABE6F325AC2E85FA5F3EDFCBAA7353EFBB55337566A1031DB47C569B603907BBC9D5DCE9EBD8C50578D769129D30C1212902E32247389733CBF5DBD5E913377AFFFEAFD269176DF2183F8FC7C091DAD9F922AA71188941B15DA834481FCF19DD082D7633A0EF3E4896CB9E6436389733CBF5DBD5E9D5E8D9A59859A8B6D52CD31C43BF465FCC7F00164DA146DA6F5DAA56C3B73B237318B6A418E8EAB8D32BA5DBAC0009BE9E8FC8737B5C2249AEF4DCA51A11EB8276E601842F6C81A12EF20D2F80756B5FB606B96278B59C4276E601842F6C81A127C277FBC8AE2E8B18E7440D383AA13B3AA81AA40904B5D99C9F4D5AE37F343AD1F44FA8B9022EA23BBE47FD9DD3FB595F5C1EE8F4F765FC72CEEB2601E22B093A03B725D353964B0B7D0EA88DDEDAC722CA9DD8327EE4930A3850AC1BE2E735F43AACC0BCEB2632C4224003CC83647689D4C264860C145E X-C1DE0DAB: 0D63561A33F958A5E31350325A6AC9CB5002B1117B3ED696BE0D877ABC96E2B0ED71F038FC046993823CB91A9FED034534781492E4B8EEAD6804EF05EF4ADF2AF36E2E0160E5C55395B8A2A0B6518DF68C46860778A80D548E8926FB43031F38 X-C8649E89: 1C3962B70DF3F0ADBF74143AD284FC7177DD89D51EBB7742DC8270968E61249B1004E42C50DC4CA955A7F0CF078B5EC49A30900B95165D34D4140D813EC137C66115095037196A2D3C00A32CFFAAC8EF318046E221B15B3B3C59BF6BBF9A39E71D7E09C32AA3244C860D88FB2A1628D177DD89D51EBB774237776F6140740339EA455F16B58544A2557BDE0DD54B3590A5AE236DF995FB59829709634694AABAED6A17656DB59BCAD427812AF56FC65B X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXMZebaIdHP2ghjoIc/363UZI6Kf1ptIMVQiWK+2I7Y2srR0WqjRHSz0= X-Mailru-Sender: 520A125C2F17F0B1A9638AD358559B597DD0CD0EC6C27CA93DE06ABAFEAF670593014B99D61AEF48B7CBEF92542CD7C88B0A2698F12F5C9EC77752E0C033A69E86920BD37369036789A8C6A0E60D2BB63A5DB60FBEB33A8A0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit] Fix bit op coercion for shifts in DUALNUM builds. 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 Junlong Li. (cherry picked from commit 69bbf3c1b01de8239444b0c430a89fa868978fea) This is a follow-up to the commit 8cd79d198df4b0e14882a663a1673e1308f09899 ("Fix bit op coercion in DUALNUM builds."). After removing the coercion from `lj_carith_check64()`, the bit shift operation may end in an infinite loop in the case of infinite retrying to coerce the second operand from number to integer TValue type. This patch fixes that by unconditionally coercing the second argument in the `LJLIB_ASM(bit_lshift)` fast function handler. Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#11055 --- Branch: https://github.com/tarantool/luajit/tree/skaplun/fix-bit-shift-dualnum Note: CI is red due to problems with the integration testing. See also: https://github.com/tarantool/tarantool/pull/11220 Related issue: https://github.com/tarantool/tarantool/issues/11055 ML: https://www.freelists.org/post/luajit/dead-loop-in-bitrshift. How to build locally for reproducing: | cmake -DLUAJIT_NUMMODE=2 -DLUA_USE_APICHECK=ON -DLUA_USE_ASSERT=ON -DCMAKE_BUILD_TYPE=Debug . && make -j And run the test like the following: | ctest --timeout 1 -R fix-bit-shift-dualnum src/lib_bit.c | 2 +- .../fix-bit-shift-dualnum.test.lua | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test/tarantool-tests/fix-bit-shift-dualnum.test.lua diff --git a/src/lib_bit.c b/src/lib_bit.c index 6dbaf351..9ac5e645 100644 --- a/src/lib_bit.c +++ b/src/lib_bit.c @@ -98,7 +98,7 @@ LJLIB_ASM(bit_lshift) LJLIB_REC(bit_shift IR_BSHL) x = lj_carith_shift64(x, sh, curr_func(L)->c.ffid - (int)FF_bit_lshift); return bit_result64(L, id, x); } - if (id2) setintV(L->base+1, sh); + setintV(L->base+1, sh); return FFH_RETRY; #else lj_lib_checknumber(L, 1); diff --git a/test/tarantool-tests/fix-bit-shift-dualnum.test.lua b/test/tarantool-tests/fix-bit-shift-dualnum.test.lua new file mode 100644 index 00000000..474a365f --- /dev/null +++ b/test/tarantool-tests/fix-bit-shift-dualnum.test.lua @@ -0,0 +1,27 @@ +local tap = require('tap') + +-- Test file to demonstrate LuaJIT misbehaviour for bitshift +-- operations in DUALNUM mode. +-- See also: +-- https://www.freelists.org/post/luajit/dead-loop-in-bitrshift. + +local test = tap.test('fix-bit-shift-dualnum') +test:plan(5) + +-- This produces the number (not integer) `TValue` type for the +-- DUALNUM build. If the second parameter of any of the shift +-- functions is not an integer in the DUALNUM build, LuaJIT tries +-- to convert it to an integer. In the case of a number, it does +-- nothing and endlessly retries the call to the fallback +-- function. +local SHIFT_V = 1 - '0' + +-- Any of the shift calls below causes the infinite FFH retrying +-- loop before the patch. +test:ok(bit.arshift(0, SHIFT_V), 0, 'no infifnite loop in bit.arshift') +test:ok(bit.lshift(0, SHIFT_V), 0, 'no infifnite loop in bit.lshift') +test:ok(bit.rshift(0, SHIFT_V), 0, 'no infifnite loop in bit.rshift') +test:ok(bit.rol(0, SHIFT_V), 0, 'no infifnite loop in bit.rol') +test:ok(bit.ror(0, SHIFT_V), 0, 'no infifnite loop in bit.ror') + +test:done(true) -- 2.48.1