[Tarantool-patches] [PATCH luajit] FFI: Fix pointer difference operation on 64 bit platforms.

Sergey Kaplun skaplun at tarantool.org
Wed Jul 29 10:39:22 MSK 2026


Hi, Evgeniy!
Thanks for the review!
Please consider my answer below.

On 28.07.26, Evgeniy Temirgaleev wrote:
> Hi, Sergey! Thanks for the patch!
> 
> LGTM with a note on a test, please see.
> --
> Best regards,
> Evgeniy Temirgaleev
> 
> > 
> > From: Sergey Kaplun <skaplun at tarantool.org>
> > To: Sergey Bronnikov <sergeyb at tarantool.org>, Evgeniy Temirgaleev <e.temirgaleev at tarantool.org
> > >
> > Cc: tarantool-patches at dev.tarantool.org, Sergey Kaplun <skaplun at tarantool.org
> > >
> > Date: Monday, July 20, 2026 5:24 PM +03:00
> > From: Mike Pall <mike>
> > 
> > Thanks to cuiweixie.
> > 
> > (cherry picked from commit b58b07189521e82c7a8e8bc43fc3c271f89832fd)
> > 
> > The metamethod for FFI pointer subtraction casts the result value to a
> > 32-bit value, which leads to invalid results.
> > 
> > This patch removes the excess cast.
> > 
> > Sergey Kaplun:
> > * added the description and the test for the problem
> > 
> > Part of tarantool/tarantool#12880
> > ---
> > 
> > Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-1449-fix-ptr-diff-64-bit
> > 
> > Related issues:
> > * https://github.com/LuaJIT/LuaJIT/issues/1449
> > * https://github.com/tarantool/tarantool/issues/12880
> > 
> > src/lj_carith.c | 2 +-
> > .../lj-1449-fix-ptr-diff-64-bit.test.lua | 39 +++++++++++++++++++
> > 2 files changed, 40 insertions(+), 1 deletion(-)
> > create mode 100644
> > test/tarantool-tests/lj-1449-fix-ptr-diff-64-bit.test.lua
> > 
> > diff --git a/src/lj_carith.c b/src/lj_carith.c
> > index eb56d552..3384c2cd 100644
> > --- a/src/lj_carith.c
> > +++ b/src/lj_carith.c

<snipped>

> > diff --git a/test/tarantool-tests/lj-1449-fix-ptr-diff-64-bit.test.lua
> > b/test/tarantool-tests/lj-1449-fix-ptr-diff-64-bit.test.lua
> > new file mode 100644
> > index 00000000..032b9b4d
> > --- /dev/null
> > +++ b/test/tarantool-tests/lj-1449-fix-ptr-diff-64-bit.test.lua

<snipped>

> > +jit.opt.start('hotloop=1')
> > +
> > +for i = 1, 4 do
> > + -- Use constants on trace.
> > + local delta = 0x80000001ULL
> > + local b = 0x700000000000ULL
> > + local pt0 = ffi.cast('char *', b)
> > + local pt1 = ffi.cast('char *', b + delta)
> > + results[i] = pt1 - pt0
> > +end
> > +
> > 
> 
> It seems, we need to add a correct sample to the results, e.g. table.insert(results, diff)

It is not necessary. The correctness of the VM is checked in the
previous test case. Here we check only consistency between the VM and
JIT. So, if JIT behaviour is different the test will fail (also, it
means that the JIT result is incorrect, since it doesn't match the
correct VM result tested before).

Also, if one runs this test without the patch applied, the assertion in
`record_check_slots()` is raised, since the resulting value after
folding is not the same as given by the VM.

> > +test:samevalues(results, 'consistent JIT and VM behaviour for ptr
> > subtraction')
> > +
> > +test:done(true)
> > --
> > 2.55.0
> >

-- 
Best regards,
Sergey Kaplun


More information about the Tarantool-patches mailing list