Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH luajit] FFI: Adjust J->maxslot after ffi.offsetof.
@ 2026-11-24 15:51 Sergey Kaplun via Tarantool-patches
  2026-11-24 16:39 ` Evgeniy Temirgaleev via Tarantool-patches
  0 siblings, 1 reply; 3+ messages in thread
From: Sergey Kaplun via Tarantool-patches @ 2026-11-24 15:51 UTC (permalink / raw)
  To: Evgeniy Temirgaleev, Sergey Bronnikov; +Cc: tarantool-patches

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-24 17:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 15:51 [Tarantool-patches] [PATCH luajit] FFI: Adjust J->maxslot after ffi.offsetof Sergey Kaplun via Tarantool-patches
2026-11-24 16:39 ` Evgeniy Temirgaleev via Tarantool-patches
2026-11-24 17:31   ` Sergey Kaplun via Tarantool-patches

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