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 E00FB57649C; Wed, 9 Aug 2023 18:42:28 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org E00FB57649C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1691595749; bh=zDpd5swiCX4zWnV4othItwfmVQ0rPHCuVBEVv6OtYj8=; 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=pCG0sPy7qGgN08DAZBfXCh+2ZZSjpiG0OQZvgPsYeXQIHvmnXe4b1Kya+tmR7DJ2u 2mx4jc0yHLzbMnyIhLa+DMAd0nCvfe/MmF8tGOGc8UcJpoyU+0hG3zfZbQlg7DNtVa PMSgt7pjCvM/y1VTIC5Ygfy+o5zGcGA7O0Md+rC8= Received: from smtp32.i.mail.ru (smtp32.i.mail.ru [95.163.41.73]) (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 3A345576067 for ; Wed, 9 Aug 2023 18:40:58 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3A345576067 Received: by smtp32.i.mail.ru with esmtpa (envelope-from ) id 1qTlIv-003Nbf-1l; Wed, 09 Aug 2023 18:40:57 +0300 To: Igor Munkin , Sergey Bronnikov Date: Wed, 9 Aug 2023 18:35:52 +0300 Message-ID: <7179245cf38c56a88bb8f3aa1bbeaf15402fcd1a.1691592488.git.skaplun@tarantool.org> X-Mailer: git-send-email 2.41.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: 4F1203BC0FB41BD94DC436DAD9FF86068A8B70255D5151F61D5B4853E07EEE2F182A05F5380850404C228DA9ACA6FE27AA385494ED7194A639BC923D1AD8FECE69CEF8E69FCCD00D64135331CBC963C2 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7ACA11F7F2395C8CCEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006375D6F9E2FC7F7A8E98638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8A292EAC48E268B869E962CE1B041CE08117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC00E8CE3DD197987DA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18E5D25F19253116ADD2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE4B6963042765DA4BBA3038C0950A5D36C8A9BA7A39EFB766D91E3A1F190DE8FDBA3038C0950A5D36D5E8D9A59859A8B69F5E362153655AB1D81D268191BDAD3D3666184CF4C3C14F3FC91FA280E0CE3D1A620F70A64A45A98AA50765F79006372E808ACE2090B5E1725E5C173C3A84C3C5EA940A35A165FF2DBA43225CD8A89F83C798A30B85E16BC6EABA9B74D0DA47B5C8C57E37DE458BEDA766A37F9254B7 X-C1DE0DAB: 0D63561A33F958A5D1699559EE285DFD2F6BEBBAB7CE8134DDD4B58190BA6399F87CCE6106E1FC07E67D4AC08A07B9B0F254576263B31EA99C5DF10A05D560A950611B66E3DA6D700B0A020F03D25A0997E3FB2386030E77 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CFEB7AFCC72B1058A09FBE37F0BF5CE2D132ECA9D16340354BE01CE3E37BBAEE449F3638D7DAB8774B1529AA6C8ABD04CAA2B229C3A60708D426BAC56308CE760DA74DFFEFA5DC0E7F02C26D483E81D6BE5EF9655DD6DEA7D65774BB76CC95456EEC5B5AD62611EEC62B5AFB4261A09AF0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojUzxoxvtYX2pW6toWKxtxlg== X-Mailru-Sender: 11C2EC085EDE56FAC07928AF2646A769D01AACAE622D669539BC923D1AD8FECEFE5F7FEA15315F16DEDBA653FF35249392D99EB8CC7091A70E183A470755BFD208F19895AA18418972D6B4FCE48DF648AE208404248635DF X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit 03/19] MIPS: Fix handling of spare long-range jump slots. 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 Contributed by Djordje Kovacevic and Stefan Pejic. (cherry-picked from commit c7c3c4da432ddb543d4b0a9abbb245f11b26afd0) `asm_setup_jump()` in presumes that `sizeof(MCLink)` is 8 bytes, but for MIPS64 its size is 16 bytes. This leads to incorrect check in `asm_sparejump_setup()`, so mcode bottom is not updated. This patch fixes check of the MCLink offset from the mcbot. Nevertheless, the emitting of spare jump slots is still incorrect, so the introduced test still fails due to incorrect iteration through the sparce table (the last slot is out of mcode range). This should be fixed via backporting of the commit dbb78630169a8106b355a5be8af627e98c362f1e ("MIPS: Fix handling of long-range spare jumps."). But it triggers the new unconditional assert, that is added in this patch, mentioning that sizemcode is too bit. So some workaround should be found, when this test will be enabled for MIPS. Since test also validates the behaviour of long-range jumps to side traces for arm64 and x64, and we have no testing for MIPS64 (yet), we can leave it as is without a skipcond. Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#8825 --- src/lj_asm_mips.h | 9 +-- src/lj_jit.h | 6 ++ src/lj_mcode.c | 6 -- ...x-mips64-spare-side-exit-patching.test.lua | 65 +++++++++++++++++++ 4 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 test/tarantool-tests/fix-mips64-spare-side-exit-patching.test.lua diff --git a/src/lj_asm_mips.h b/src/lj_asm_mips.h index 03215821..0e60fc07 100644 --- a/src/lj_asm_mips.h +++ b/src/lj_asm_mips.h @@ -65,10 +65,9 @@ static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow) static void asm_sparejump_setup(ASMState *as) { MCode *mxp = as->mcbot; - /* Assumes sizeof(MCLink) == 8. */ - if (((uintptr_t)mxp & (LJ_PAGESIZE-1)) == 8) { + if (((uintptr_t)mxp & (LJ_PAGESIZE-1)) == sizeof(MCLink)) { lua_assert(MIPSI_NOP == 0); - memset(mxp+2, 0, MIPS_SPAREJUMP*8); + memset(mxp, 0, MIPS_SPAREJUMP*2*sizeof(MCode)); mxp += MIPS_SPAREJUMP*2; lua_assert(mxp < as->mctop); lj_mcode_sync(as->mcbot, mxp); @@ -2486,7 +2485,9 @@ void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target) if (!cstart) cstart = p-1; } else { /* Branch out of range. Use spare jump slot in mcarea. */ int i; - for (i = 2; i < 2+MIPS_SPAREJUMP*2; i += 2) { + for (i = (int)(sizeof(MCLink)/sizeof(MCode)); + i < (int)(sizeof(MCLink)/sizeof(MCode)+MIPS_SPAREJUMP*2); + i += 2) { if (mcarea[i] == tjump) { delta = mcarea+i - p; goto patchbranch; diff --git a/src/lj_jit.h b/src/lj_jit.h index f2ad3c6e..cc8efd20 100644 --- a/src/lj_jit.h +++ b/src/lj_jit.h @@ -158,6 +158,12 @@ typedef uint8_t MCode; typedef uint32_t MCode; #endif +/* Linked list of MCode areas. */ +typedef struct MCLink { + MCode *next; /* Next area. */ + size_t size; /* Size of current area. */ +} MCLink; + /* Stack snapshot header. */ typedef struct SnapShot { uint32_t mapofs; /* Offset into snapshot map. */ diff --git a/src/lj_mcode.c b/src/lj_mcode.c index 7184d3b4..c6361018 100644 --- a/src/lj_mcode.c +++ b/src/lj_mcode.c @@ -272,12 +272,6 @@ static void *mcode_alloc(jit_State *J, size_t sz) /* -- MCode area management ----------------------------------------------- */ -/* Linked list of MCode areas. */ -typedef struct MCLink { - MCode *next; /* Next area. */ - size_t size; /* Size of current area. */ -} MCLink; - /* Allocate a new MCode area. */ static void mcode_allocarea(jit_State *J) { diff --git a/test/tarantool-tests/fix-mips64-spare-side-exit-patching.test.lua b/test/tarantool-tests/fix-mips64-spare-side-exit-patching.test.lua new file mode 100644 index 00000000..fdc826cb --- /dev/null +++ b/test/tarantool-tests/fix-mips64-spare-side-exit-patching.test.lua @@ -0,0 +1,65 @@ +local tap = require('tap') +local test = tap.test('fix-mips64-spare-side-exit-patching'):skipcond({ + ['Test requires JIT enabled'] = not jit.status(), + ['Disabled on *BSD due to #4819'] = jit.os == 'BSD', + -- Need to fix the MIPS behaviour first. + ['Disabled for MIPS architectures'] = jit.arch:match('mips'), +}) + +local generators = require('utils').jit.generators +local frontend = require('utils').frontend + +test:plan(1) + +-- Make compiler work hard. +jit.opt.start( + -- No optimizations at all to produce more mcode. + 0, + -- Try to compile all compiled paths as early as JIT can. + 'hotloop=1', + 'hotexit=1', + -- Allow to use 2000 traces to avoid flushes. + 'maxtrace=2000', + -- Allow to compile 8Mb of mcode to be sure the issue occurs. + 'maxmcode=8192', + -- Use big mcode area for traces to avoid using different + -- spare slots. + 'sizemcode=256' +) + +local MAX_SPARE_SLOT = 4 +local function parent(marker) + -- Use several side exit to fill spare exit space (default is + -- 4 slots, each slot has 2 instructions -- jump and nop). + -- luacheck: ignore + if marker > MAX_SPARE_SLOT then end + if marker > 3 then end + if marker > 2 then end + if marker > 1 then end + if marker > 0 then end + -- XXX: use `fmod()` to avoid leaving the function and use + -- stitching here. + return math.fmod(1, 1) +end + +-- Compile parent trace first. +parent(0) +parent(0) + +local parent_traceno = frontend.gettraceno(parent) +local last_traceno = parent_traceno + +-- Now generate some mcode to forcify long jump with a spare slot. +-- Each iteration provide different addresses and uses a different +-- spare slot. After it compile and execute new side trace. +for i = 1, MAX_SPARE_SLOT + 1 do + generators.fillmcode(last_traceno, 1024 * 1024) + parent(i) + parent(i) + parent(i) + last_traceno = misc.getmetrics().jit_trace_num +end + +test:ok(true, 'all traces executed correctly') + +test:done(true) -- 2.41.0