Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Bronnikov via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Sergey Kaplun <skaplun@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH luajit] MIPS64: Avoid unaligned load in lj_vm_exit_interp.
Date: Fri, 13 Mar 2026 14:05:28 +0300	[thread overview]
Message-ID: <ccd2df62-33f8-44d7-9855-1490d9ca063f@tarantool.org> (raw)
In-Reply-To: <20260312085017.17591-1-skaplun@tarantool.org>

[-- Attachment #1: Type: text/plain, Size: 4527 bytes --]

Hi, Sergey,

thanks for the patch! LGTM

Sergey

On 3/12/26 11:50, Sergey Kaplun wrote:
> From: Mike Pall <mike>
>
> 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
>     |  ldLFUNC:TMP2, -32(TMP1)
>     |  cleartpLFUNC: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 alsohttps://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)

[-- Attachment #2: Type: text/html, Size: 5506 bytes --]

      reply	other threads:[~2026-03-13 11:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-12  8:50 Sergey Kaplun via Tarantool-patches
2026-03-13 11:05 ` Sergey Bronnikov via Tarantool-patches [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ccd2df62-33f8-44d7-9855-1490d9ca063f@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=sergeyb@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit] MIPS64: Avoid unaligned load in lj_vm_exit_interp.' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox