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 DC26C16DE354; Thu, 12 Mar 2026 11:49:25 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org DC26C16DE354 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1773305366; bh=wK+rkUVetuImauvGNtSZdKFAfHF4j+kPaka7QfJdKEM=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=CNBvOEgJOdL066GouIt6o6UOcXb4WQefKui2ERVPEcUufqBbQx5KVs/GTc9l8rOeh sNZvPHt5FejyeQErMn6hEYz9UiZr0l12EOnb3TSpRgegB9dmiH/rn+3y6c2F7/rcZd 37DJUGNbmTbwW44LkOaAzFDa2qHaaio4W0YmQEUo= Received: from send220.i.mail.ru (send220.i.mail.ru [95.163.59.59]) (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 E4EB416DE354 for ; Thu, 12 Mar 2026 11:49:24 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org E4EB416DE354 Received: by exim-smtp-64cdfc6c8d-rgqkg with esmtpa (envelope-from ) id 1w0bjP-000000002iJ-3sR1; Thu, 12 Mar 2026 11:49:24 +0300 To: Sergey Bronnikov Date: Thu, 12 Mar 2026 11:50:17 +0300 Message-ID: <20260312085017.17591-1-skaplun@tarantool.org> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD91ABAE9865AC7DC88B865A22E496AAB539DFA8EEAAC77DF87182A05F538085040886DD1CCE55AF27F3DE06ABAFEAF67055EABEB393E5BC58516506B2E049A5E590F40345E41D45524 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7FBB2043146276655EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637AC83A81C8FD4AD23D82A6BABE6F325AC2E85FA5F3EDFCBAA7353EFBB5533756627194F95AE6E7C2D0A9AC5F01230CF55169B49877CD62B8FC7E99A397DF7037F389733CBF5DBD5E913377AFFFEAFD269176DF2183F8FC7C08794E14F7ADDB10D8941B15DA834481FCF19DD082D7633A0EF3E4896CB9E6436389733CBF5DBD5E9D5E8D9A59859A8B6A50BD5087FBFCDAACC7F00164DA146DA6F5DAA56C3B73B237318B6A418E8EAB8D32BA5DBAC0009BE9E8FC8737B5C22494705F91287EA2BDB76E601842F6C81A12EF20D2F80756B5FB606B96278B59C4276E601842F6C81A127C277FBC8AE2E8BA1EEF9386886340E3AA81AA40904B5D99C9F4D5AE37F343AD1F44FA8B9022EA23BBE47FD9DD3FB595F5C1EE8F4F765FC72CEEB2601E22B093A03B725D353964B0B7D0EA88DDEDAC722CA9DD8327EE4930A3850AC1BE2E7356C9A9530EBF72002C4224003CC83647689D4C264860C145E X-C1DE0DAB: 0D63561A33F958A5A14ED7730E91535E5002B1117B3ED6969135EE987F9DC6A7F09842853758E9E5823CB91A9FED034534781492E4B8EEAD6A17C1D737525568C79554A2A72441328621D336A7BC284946AD531847A6065A535571D14F44ED41 X-C8649E89: 1C3962B70DF3F0AD73CAD6646DEDE191716CD42B3DD1D34C77DD89D51EBB774225B6776AC983F447FC0B9F89525902EE6F57B2FD27647F25E66C117BDB76D659F550994B59A6E9CE9A463146E5897F7F82A7D4D133A65B4D647801980B8887AC3DC4AD8F75ACD80DB8341EE9D5BE9A0A34381CC8537947370A91A4229BCECD6667C2D556F4872DAC6536EB022892E5344C41F94D744909CECFA6C6B0C050A61A8CAF69B82BA93681CD72808BE417F3B9E0E7457915DAA85F X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXMZebaIdHP2ghjoIc/363UZI6Kf1ptIMVdbVVJCphTR/GMECiRd0kqo= X-Mailru-Sender: 583F1D7ACE8F49BDD951BA70C165859EA4EDD2CE6B18204CE9E07FF329DA7D471A40C4EE18EA6A1E5E2818E960BBF402F2400F607609286E924004A7DEC283833C7120B22964430C52B393F8C72A41A84198E0F3ECE9B5443453F38A29522196 X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit] MIPS64: Avoid unaligned load in lj_vm_exit_interp. 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. (cherry picked from commit 2aec641e01ab80e86ea75d944c0919fa6c03c37c) MIPS processors originally required all memory accesses to be naturally aligned. If we use ld instruction to load a double-word from the address which is word-aligned, MIPS raises the exception SIGBUS. When exiting the interpreter, if the current function is a fast function, the code in the `lj_vm_exit_interp()` throws SIGBUS. The pc field for the fast function points to the word-aligned bytecodes for ASM fast functions, and PC2PROTO offset is double-word-aligned. The resulting address is somewhere in the dispatch table. Hence, some (odd-indexed) fast function access leads to the BUS error. For other architectures the load from unaligned access is not a problem so there are no exceptions. This patch prevents unaligned memory access by address loading only after fast-function checks. Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#12134 --- Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-1428-mips64-bus-error-stitch Related issues: * https://github.com/LuaJIT/LuaJIT/issues/1428 * https://github.com/tarantool/tarantool/issues/12134 How to reproduce locally: | make -j HOST_CC="gcc " CROSS=mips64el-unknown-linux-gnu- -f Makefile.original CCDEBUG=" -g -ggdb3" CFLAGS=" -O0" XCFLAGS=" -DLUA_USE_APICHECK -DLUA_USE_ASSERT " | LUA_PATH="src/?.lua;test/tarantool-tests/?.lua;;" LD_LIBRARY_PATH="/usr/lib/gcc/mips64el-unknown-linux-gnu/15/" qemu-mips64el -L /usr/mips64el-unknown-linux-gnu/ src/luajit test/tarantool-tests/lj-1428-mips64-bus-error-stitch.test.lua src/vm_mips64.dasc | 8 ++--- .../lj-1428-mips64-bus-error-stitch.test.lua | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 test/tarantool-tests/lj-1428-mips64-bus-error-stitch.test.lua diff --git a/src/vm_mips64.dasc b/src/vm_mips64.dasc index 34da6473..36250ab3 100644 --- a/src/vm_mips64.dasc +++ b/src/vm_mips64.dasc @@ -2575,9 +2575,8 @@ static void build_subroutines(BuildCtx *ctx) | li TISNIL, LJ_TNIL | li TISNUM, LJ_TISNUM // Setup type comparison constants. | .FPU mtc1 TMP3, TOBIT - | ld TMP1, LFUNC:RB->pc + | ld TMP3, LFUNC:RB->pc | sd r0, DISPATCH_GL(jit_base)(DISPATCH) - | ld KBASE, PC2PROTO(k)(TMP1) | .FPU cvt.d.s TOBIT, TOBIT | // Modified copy of ins_next which handles function header dispatch, too. | lw INS, 0(PC) @@ -2593,6 +2592,7 @@ static void build_subroutines(BuildCtx *ctx) | decode_RA8a RA, INS | beqz TMP2, >2 |. decode_RA8b RA + | ld KBASE, PC2PROTO(k)(TMP3) | jr AT |. decode_RD8b RD |2: @@ -2610,8 +2610,8 @@ static void build_subroutines(BuildCtx *ctx) | dsubu TMP1, BASE, TMP0 | ld LFUNC:TMP2, -32(TMP1) | cleartp LFUNC:TMP2 - | ld TMP1, LFUNC:TMP2->pc - | ld KBASE, PC2PROTO(k)(TMP1) + | ld TMP3, LFUNC:TMP2->pc + | ld KBASE, PC2PROTO(k)(TMP3) |3: | daddiu RC, MULTRES, -8 | jr AT diff --git a/test/tarantool-tests/lj-1428-mips64-bus-error-stitch.test.lua b/test/tarantool-tests/lj-1428-mips64-bus-error-stitch.test.lua new file mode 100644 index 00000000..a81051c6 --- /dev/null +++ b/test/tarantool-tests/lj-1428-mips64-bus-error-stitch.test.lua @@ -0,0 +1,33 @@ +local tap = require('tap') + +-- The test file to demonstrate the incorrect exit to the +-- interpreter into fast functions on mips64. +-- See also https://github.com/LuaJIT/LuaJIT/issues/1428. + +local test = tap.test('lj-1428-mips64-bus-error-stitch'):skipcond({ + ['Test requires JIT enabled'] = not jit.status(), +}) + +test:plan(1) + +local function always_number(val) + return tonumber(val) or 1 +end + +jit.opt.start('hotloop=1') + +-- `tonumber()` with a string argument produces stitching and +-- exits to the interpreter after that. +-- On mips64 the `PC2PROTO` offset leads to an unaligned address +-- for this fast function. + +always_number('') +always_number('') + +-- Start the stitched trace and exit to the interpreter. +-- Leads to the Bus error on mips64 before the patch. +always_number('') + +test:ok(true, 'no bus error') + +test:done(true) -- 2.53.0