Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Kaplun via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Evgeniy Temirgaleev <e.temirgaleev@tarantool.org>,
	Sergey Bronnikov <sergeyb@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH luajit] FFI: Adjust J->maxslot after ffi.offsetof.
Date: Tue, 24 Nov 2026 18:51:25 +0300	[thread overview]
Message-ID: <20261124155125.1713061-1-skaplun@tarantool.org> (raw)

From: Mike Pall <mike>

Reported by Elias Hogstvedt.

(cherry picked from commit 3000f7cdb1123f2a4d8b690d4292c541359c670c)

The `recff_ffi_xof()` for `ffi.offsetof()` records 3 slots returned to
the Lua stack unconditionally, while the VM returns this much only for
bitfields. This leads to the maximum slot assertion failure in the
`rec_check_slots()`.

This patch fixes this by adjusting the `maxslot` before the recording of
the next instruction in the `LJ_POST_FIXCONST` fixup step by the correct
slots delta.

Sergey Kaplun:
* added the description and the test for the problem

Part of tarantool/tarantool#13442
---

Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-1514-ffi-offsetof-maxslot
Related issues:
* https://github.com/LuaJIT/LuaJIT/issues/1514
* https://github.com/tarantool/tarantool/issues/13442

 src/lj_record.c                               |  2 ++
 .../lj-1514-ffi-offsetof-maxslot.test.lua     | 34 +++++++++++++++++++
 2 files changed, 36 insertions(+)
 create mode 100644 test/tarantool-tests/lj-1514-ffi-offsetof-maxslot.test.lua

diff --git a/src/lj_record.c b/src/lj_record.c
index dbfc7f47..ffc07b52 100644
--- a/src/lj_record.c
+++ b/src/lj_record.c
@@ -2118,6 +2118,8 @@ void lj_record_ins(jit_State *J)
       {
 	BCReg s;
 	TValue *tv = J->L->base;
+	ptrdiff_t delta = J->L->top - tv;
+	if (J->maxslot > (BCReg)delta) J->maxslot = (BCReg)delta;
 	for (s = 0; s < J->maxslot; s++)  /* Constify stack slots (if any). */
 	  if (J->base[s] == TREF_NIL && !tvisnil(&tv[s]))
 	    J->base[s] = lj_record_constify(J, &tv[s]);
diff --git a/test/tarantool-tests/lj-1514-ffi-offsetof-maxslot.test.lua b/test/tarantool-tests/lj-1514-ffi-offsetof-maxslot.test.lua
new file mode 100644
index 00000000..dd6cc50f
--- /dev/null
+++ b/test/tarantool-tests/lj-1514-ffi-offsetof-maxslot.test.lua
@@ -0,0 +1,34 @@
+local tap = require('tap')
+
+-- The test file to demonstrate an incorrect JIT slots recording
+-- for the `ffi.offsetof()` fast function.
+-- See also: https://github.com/LuaJIT/LuaJIT/issues/1514.
+local test = tap.test('lj-1514-ffi-offsetof-maxslot'):skipcond({
+  ['Test requires JIT enabled'] = not jit.status(),
+})
+
+test:plan(1)
+
+local ffi = require('ffi')
+
+ffi.cdef[[
+  struct test {int a;}
+]]
+
+jit.opt.start('hotloop=1')
+
+local ct = ffi.typeof('struct test')
+
+local res
+for _ = 1, 4 do
+  -- Need to use CALLM for the `ffi.offsetof()` call to adjust
+  -- JIT slots to occupy extra slots above the top of the Lua
+  -- stack. Use `tonumber()` as an external function as the
+  -- simplest proxy. `ffi.offsetof()` returns 1 value for regular
+  -- fields.
+  res = tonumber(ffi.offsetof(ct, 'a'))
+end
+
+test:is(res, 0, 'correct offset value')
+
+test:done(true)
-- 
2.56.0


             reply	other threads:[~2026-11-24 15:49 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 15:51 Sergey Kaplun via Tarantool-patches [this message]
2026-11-24 16:39 ` Evgeniy Temirgaleev via Tarantool-patches
2026-11-24 17:31   ` Sergey Kaplun via Tarantool-patches

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=20261124155125.1713061-1-skaplun@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=e.temirgaleev@tarantool.org \
    --cc=sergeyb@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit] FFI: Adjust J->maxslot after ffi.offsetof.' \
    /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