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 B7F846A8173; Wed, 11 Oct 2023 18:09:41 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org B7F846A8173 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1697036981; bh=QkTVDJRDOP8GoJ6ZHJO/+S7WoLy4wSgpr6A2+/+asF4=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=FJuyV3z8FXcwNoa9zs25pZweT2BKQtPoxeUoXTWIibKH01l6GjPGjDNwNHTgHvIba 4l5+FFF/+sDFLYRvYrjmV6KcvOL+A3koAoOHg6M/eao5rGNByxHxqd2+U5Prlm7lE7 g2/PUwb+oVc6W7ghphVXpWq0K+Kq/ecnE2JkmIyE= Received: from smtp57.i.mail.ru (smtp57.i.mail.ru [95.163.41.95]) (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 8B7DD6A8174 for ; Wed, 11 Oct 2023 18:08:44 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 8B7DD6A8174 Received: by smtp57.i.mail.ru with esmtpa (envelope-from ) id 1qqapH-00DjtE-10; Wed, 11 Oct 2023 18:08:43 +0300 To: Maxim Kokryashkin , Sergey Bronnikov Date: Wed, 11 Oct 2023 18:04:10 +0300 Message-ID: <2756cdf8d8a4be9aa55dfdadbd3453da067d8969.1697034851.git.skaplun@tarantool.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD933FC8430F337940E2ACA3A34BCBF215684FFC9C14BDF5D04182A05F5380850404C228DA9ACA6FE270A1763E3DF4F43364F6BEADAF1A75C2D2B79D82AD3FFE9FD38762CAA0195358B X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE78EDA1DD9981EFADEEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006370218B86C916BF3528638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8E1F2B35C69A3AA24A95CE1E19D0E5228117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC587F3D2152687E5CA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352026055571C92BF10F28451B159A507268D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE26055571C92BF10FE0F2381F647739FAD8FC6C240DEA76429C9F4D5AE37F343AA9539A8B242431040A6AB1C7CE11FEE32D01283D1ACF37BA6136E347CC761E07C4224003CC836476E2F48590F00D11D6E2021AF6380DFAD1A18204E546F3947C6FF1B927F95F33162E808ACE2090B5E1725E5C173C3A84C3C5EA940A35A165FF2DBA43225CD8A89F890A246B268E114E5E1C53F199C2BB95B5C8C57E37DE458BEDA766A37F9254B7 X-C1DE0DAB: 0D63561A33F958A56D753EA231E1C16F4FD24AD5479A91D339571FA064253909F87CCE6106E1FC07E67D4AC08A07B9B04E7D9683544204AF9C5DF10A05D560A950611B66E3DA6D700B0A020F03D25A0997E3FB2386030E77 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF228F657A5083105A1D396E5D20086159984CFBF452F263C3A382C9889C7B73D7B469278E65BD125AED8146B44B593589DFFCBE6936D0A6319AC9E44775DA3D57A74DFFEFA5DC0E7F02C26D483E81D6BE5EF9655DD6DEA7D65774BB76CC95456EEC5B5AD62611EEC62B5AFB4261A09AF0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojPxfa+LcGtjjDBuR1+HaDaA== X-Mailru-Sender: 11C2EC085EDE56FAC07928AF2646A769F1DA709F152148E24F6BEADAF1A75C2D9FD6E2B231121C05DEDBA653FF35249392D99EB8CC7091A70E183A470755BFD208F19895AA18418972D6B4FCE48DF648AE208404248635DF X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit 2/2] Fix base register coalescing in side trace. 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 Sergey Kaplun, NiLuJe and Peter Cawley. (cherry-picked from commit aa2db7ebd1267836af5221336ccb4e9b4aa8372d) The previous patch fixed just part of the problem with the register coalesing. For example, the parent base register may be used inside the parent or child register sets when it shouldn't. This leads to incorrect register allocations, which may lead to crashes or undefined behaviour. This patch fixes it by excluding the parent base register from both register sets. The test case for this patch doesn't fail before the commit since it requires specific register allocation, which is hard to construct and very fragile. Due to the lack of ideal sync with the upstream repository, the test is passed before the patch. It should become correct in future patches. Resolves tarantool/tarantool#8767 Part of tarantool/tarantool#9145 Sergey Kaplun: * added the description and the test for the problem --- src/lj_asm.c | 7 +- src/lj_asm_arm.h | 7 +- src/lj_asm_arm64.h | 7 +- src/lj_asm_mips.h | 8 +- src/lj_asm_ppc.h | 8 +- src/lj_asm_x86.h | 8 +- .../lj-1031-asm-head-side-base-reg.test.lua | 139 ++++++++++++++++++ 7 files changed, 163 insertions(+), 21 deletions(-) create mode 100644 test/tarantool-tests/lj-1031-asm-head-side-base-reg.test.lua diff --git a/src/lj_asm.c b/src/lj_asm.c index ca06860a..5137d05c 100644 --- a/src/lj_asm.c +++ b/src/lj_asm.c @@ -1860,6 +1860,7 @@ static void asm_head_side(ASMState *as) RegSet allow = RSET_ALL; /* Inverse of all coalesced registers. */ RegSet live = RSET_EMPTY; /* Live parent registers. */ RegSet pallow = RSET_GPR; /* Registers needed by the parent stack check. */ + Reg pbase; IRIns *irp = &as->parent->ir[REF_BASE]; /* Parent base. */ int32_t spadj, spdelta; int pass2 = 0; @@ -1870,7 +1871,11 @@ static void asm_head_side(ASMState *as) /* Force snap #0 alloc to prevent register overwrite in stack check. */ asm_snap_alloc(as, 0); } - allow = asm_head_side_base(as, irp, allow); + pbase = asm_head_side_base(as, irp); + if (pbase != RID_NONE) { + rset_clear(allow, pbase); + rset_clear(pallow, pbase); + } /* Scan all parent SLOADs and collect register dependencies. */ for (i = as->stopins; i > REF_BASE; i--) { diff --git a/src/lj_asm_arm.h b/src/lj_asm_arm.h index 47564d2e..5a0f925f 100644 --- a/src/lj_asm_arm.h +++ b/src/lj_asm_arm.h @@ -2107,7 +2107,7 @@ static void asm_head_root_base(ASMState *as) } /* Coalesce BASE register for a side trace. */ -static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) +static Reg asm_head_side_base(ASMState *as, IRIns *irp) { IRIns *ir; asm_head_lreg(as); @@ -2115,16 +2115,15 @@ static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) if (ra_hasreg(ir->r) && (rset_test(as->modset, ir->r) || irt_ismarked(ir->t))) ra_spill(as, ir); if (ra_hasspill(irp->s)) { - rset_clear(allow, ra_dest(as, ir, allow)); + return ra_dest(as, ir, RSET_GPR); } else { Reg r = irp->r; lj_assertA(ra_hasreg(r), "base reg lost"); - rset_clear(allow, r); if (r != ir->r && !rset_test(as->freeset, r)) ra_restore(as, regcost_ref(as->cost[r])); ra_destreg(as, ir, r); + return r; } - return allow; } /* -- Tail of trace ------------------------------------------------------- */ diff --git a/src/lj_asm_arm64.h b/src/lj_asm_arm64.h index d1d4237b..88b47ceb 100644 --- a/src/lj_asm_arm64.h +++ b/src/lj_asm_arm64.h @@ -1873,7 +1873,7 @@ static void asm_head_root_base(ASMState *as) } /* Coalesce BASE register for a side trace. */ -static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) +static Reg asm_head_side_base(ASMState *as, IRIns *irp) { IRIns *ir; asm_head_lreg(as); @@ -1881,16 +1881,15 @@ static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) if (ra_hasreg(ir->r) && (rset_test(as->modset, ir->r) || irt_ismarked(ir->t))) ra_spill(as, ir); if (ra_hasspill(irp->s)) { - rset_clear(allow, ra_dest(as, ir, allow)); + return ra_dest(as, ir, RSET_GPR); } else { Reg r = irp->r; lj_assertA(ra_hasreg(r), "base reg lost"); - rset_clear(allow, r); if (r != ir->r && !rset_test(as->freeset, r)) ra_restore(as, regcost_ref(as->cost[r])); ra_destreg(as, ir, r); + return r; } - return allow; } /* -- Tail of trace ------------------------------------------------------- */ diff --git a/src/lj_asm_mips.h b/src/lj_asm_mips.h index ac9090f2..597c6d62 100644 --- a/src/lj_asm_mips.h +++ b/src/lj_asm_mips.h @@ -2595,7 +2595,7 @@ static void asm_head_root_base(ASMState *as) } /* Coalesce BASE register for a side trace. */ -static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) +static Reg asm_head_side_base(ASMState *as, IRIns *irp) { IRIns *ir = IR(REF_BASE); Reg r = ir->r; @@ -2605,15 +2605,15 @@ static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) if (rset_test(as->modset, r) || irt_ismarked(ir->t)) ir->r = RID_INIT; /* No inheritance for modified BASE register. */ if (irp->r == r) { - rset_clear(allow, r); /* Mark same BASE register as coalesced. */ + return r; /* Same BASE register already coalesced. */ } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) { - rset_clear(allow, irp->r); emit_move(as, r, irp->r); /* Move from coalesced parent reg. */ + return irp->r; } else { emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */ } } - return allow; + return RID_NONE; } /* -- Tail of trace ------------------------------------------------------- */ diff --git a/src/lj_asm_ppc.h b/src/lj_asm_ppc.h index 971dcc88..7bba71b3 100644 --- a/src/lj_asm_ppc.h +++ b/src/lj_asm_ppc.h @@ -2130,7 +2130,7 @@ static void asm_head_root_base(ASMState *as) } /* Coalesce BASE register for a side trace. */ -static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) +static Reg asm_head_side_base(ASMState *as, IRIns *irp) { IRIns *ir = IR(REF_BASE); Reg r = ir->r; @@ -2139,15 +2139,15 @@ static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) if (rset_test(as->modset, r) || irt_ismarked(ir->t)) ir->r = RID_INIT; /* No inheritance for modified BASE register. */ if (irp->r == r) { - rset_clear(allow, r); /* Mark same BASE register as coalesced. */ + return r; /* Same BASE register already coalesced. */ } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) { - rset_clear(allow, irp->r); emit_mr(as, r, irp->r); /* Move from coalesced parent reg. */ + return irp->r; } else { emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */ } } - return allow; + return RID_NONE; } /* -- Tail of trace ------------------------------------------------------- */ diff --git a/src/lj_asm_x86.h b/src/lj_asm_x86.h index 2b810c8d..86ce3937 100644 --- a/src/lj_asm_x86.h +++ b/src/lj_asm_x86.h @@ -2856,7 +2856,7 @@ static void asm_head_root_base(ASMState *as) } /* Coalesce or reload BASE register for a side trace. */ -static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) +static Reg asm_head_side_base(ASMState *as, IRIns *irp) { IRIns *ir = IR(REF_BASE); Reg r = ir->r; @@ -2865,16 +2865,16 @@ static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) if (rset_test(as->modset, r) || irt_ismarked(ir->t)) ir->r = RID_INIT; /* No inheritance for modified BASE register. */ if (irp->r == r) { - rset_clear(allow, r); /* Mark same BASE register as coalesced. */ + return r; /* Same BASE register already coalesced. */ } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) { /* Move from coalesced parent reg. */ - rset_clear(allow, irp->r); emit_rr(as, XO_MOV, r|REX_GC64, irp->r); + return irp->r; } else { emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */ } } - return allow; + return RID_NONE; } /* -- Tail of trace ------------------------------------------------------- */ diff --git a/test/tarantool-tests/lj-1031-asm-head-side-base-reg.test.lua b/test/tarantool-tests/lj-1031-asm-head-side-base-reg.test.lua new file mode 100644 index 00000000..fc5efaaa --- /dev/null +++ b/test/tarantool-tests/lj-1031-asm-head-side-base-reg.test.lua @@ -0,0 +1,139 @@ +local tap = require('tap') +-- Test file to demonstrate incorrect side trace head assembling +-- when use parent trace register holding base (`RID_BASE`). +-- See also, https://github.com/LuaJIT/LuaJIT/issues/1031. +-- +-- XXX: For now, the test doesn't fail even on arm64 due to the +-- different from upstream register allocation. Nevertheless, this +-- issue should be gone with future backporting, so just leave the +-- test case as is. +local test = tap.test('lj-1031-asm-head-side-base-reg'):skipcond({ + ['Test requires JIT enabled'] = not jit.status(), +}) + +local ffi = require 'ffi' +local int64_t = ffi.typeof('int64_t') + +test:plan(1) + +-- To reproduce the issue (reproduced only on arm64) we need a +-- little bit of a tricky structure for the traces: +-- +-> + -- start TRACE 1 +-- | | ... +-- | |---> + -- start TRACE 3 (side trace for the 1st) +-- | | | ... +-- | | v (link with TRACE 2) +-- | | +-> + -- start TRACE 2 (some loop that wasn't recorded +-- | | | | before due to the loop limit) +-- | | +---+ +-- +<--+ +-- Also, we need high register pressure to be sure that register +-- holding value of the base will be spilled. +-- So, during the assembly of "TRACE 3" (#6 in our test), +-- `RID_BASE` is spilled and restored via 32-bit fpr. This leads +-- to an incorrect value because it contains a 64-bit value. +-- See the original ticket for the details. + +-- XXX: Reduce amount of IR. +local tonumber = tonumber +local ffi_new = ffi.new + +-- Resulting slots for values calculated on trace. +-- luacheck: ignore +local r1 +local r2 +local r3 +local r4 +local r5 +local r6 +local r7 +local r8 +local r9 +local r10 +local r11 +local r12 +local r13 +local r14 +local r15 +local r16 +local r17 +local r18 +local r19 + +local ARR_SIZE = 1e2 +local lim_arr = {} + +-- XXX: Prevent irrelevant output in jit.dump(). +jit.off() + +local INNER_TRACE_LIMIT = 20 +local INNER_LIMIT1 = 1 +local INNER_LIMIT2 = 4 +for i = 1, INNER_TRACE_LIMIT do lim_arr[i] = INNER_LIMIT1 end +for i = INNER_TRACE_LIMIT + 1, ARR_SIZE + 1 do lim_arr[i] = INNER_LIMIT2 end + +-- Enable compilation back. +jit.on() + +jit.opt.start('hotloop=1', 'hotexit=2') + +-- XXX: Trace numbers are given with the respect of using +-- `jit.dump`. +-- Start TRACE 2 (1 is inside dump bc). +local k = 0 +while k < ARR_SIZE do + k = k + 1 + -- Forcify register pressure. + local l1 = ffi_new(int64_t, k + 1) + local l2 = ffi_new(int64_t, k + 2) + local l3 = ffi_new(int64_t, k + 3) + local l4 = ffi_new(int64_t, k + 4) + local l5 = ffi_new(int64_t, k + 5) + local l6 = ffi_new(int64_t, k + 6) + local l7 = ffi_new(int64_t, k + 7) + local l8 = ffi_new(int64_t, k + 8) + local l9 = ffi_new(int64_t, k + 9) + local l10 = ffi_new(int64_t, k + 10) + local l11 = ffi_new(int64_t, k + 11) + local l12 = ffi_new(int64_t, k + 12) + local l13 = ffi_new(int64_t, k + 13) + local l14 = ffi_new(int64_t, k + 14) + local l15 = ffi_new(int64_t, k + 15) + local l16 = ffi_new(int64_t, k + 16) + local l17 = ffi_new(int64_t, k + 17) + local l18 = ffi_new(int64_t, k + 18) + local l19 = ffi_new(int64_t, k + 19) + -- Side exit for TRACE 6 start 2/1. + -- luacheck: ignore + if k > 55 then else end + r1 = tonumber(l1) + r2 = tonumber(l2) + r3 = tonumber(l3) + r4 = tonumber(l4) + r5 = tonumber(l5) + r6 = tonumber(l6) + r7 = tonumber(l7) + r8 = tonumber(l8) + r9 = tonumber(l9) + r10 = tonumber(l10) + r11 = tonumber(l11) + r12 = tonumber(l12) + r13 = tonumber(l13) + r14 = tonumber(l14) + r15 = tonumber(l15) + r15 = tonumber(l15) + r16 = tonumber(l16) + r17 = tonumber(l17) + r18 = tonumber(l18) + r19 = tonumber(l19) + local lim_inner = lim_arr[k] + -- TRACE 3. Loop is not taken at the moment of recording TRACE 2 + -- (lim_inner == 1). + for _ = 1, lim_inner do + -- TRACE 6 stop -> 3. + end +end + +test:ok(true, 'correct side trace head assembling') + +test:done(true) -- 2.42.0