<HTML><BODY><div class="cl-0gci5k0xlq">Hi, Sergey! Thanks for the patch!<br><br>LGTM with a note on a test, please see.<br> <div data-signature-widget="container"><div data-signature-widget="content"><div>--<br>Best regards,</div><div>Evgeniy Temirgaleev</div></div></div><br><div class="mail-quote-collapse"><blockquote style="border-left:1px solid #0857A6;margin:10px;padding:0 0 0 10px"><span>From: Sergey Kaplun <<a href="mailto:skaplun@tarantool.org">skaplun@tarantool.org</a>><br>To: Sergey Bronnikov <<a href="mailto:sergeyb@tarantool.org">sergeyb@tarantool.org</a>>, Evgeniy Temirgaleev <<a href="mailto:e.temirgaleev@tarantool.org">e.temirgaleev@tarantool.org</a>><br>Cc: tarantool-patches@dev.tarantool.org, Sergey Kaplun <<a href="mailto:skaplun@tarantool.org">skaplun@tarantool.org</a>><br>Date: Monday, July 20, 2026 5:24 PM +03:00</span><br> <div><div id=""><div class="cl-jmbqwszmq7"><div class="js-helper_mr_css_attr js-readmsg-msg_mr_css_attr"><div id="style_17845574860753108787_mr_css_attr"><div id="style_17845574860753108787_BODY_mr_css_attr">From: Mike Pall <mike><br><br>Thanks to cuiweixie.<br><br>(cherry picked from commit b58b07189521e82c7a8e8bc43fc3c271f89832fd)<br><br>The metamethod for FFI pointer subtraction casts the result value to a<br>32-bit value, which leads to invalid results.<br><br>This patch removes the excess cast.<br><br>Sergey Kaplun:<br>* added the description and the test for the problem<br><br>Part of tarantool/tarantool#12880<br>---<br><br>Branch: <a href="https://github.com/tarantool/luajit/tree/skaplun/lj-1449-fix-ptr-diff-64-bit">https://github.com/tarantool/luajit/tree/skaplun/lj-1449-fix-ptr-diff-64-bit</a><br>Related issues:<br>* <a href="https://github.com/LuaJIT/LuaJIT/issues/1449">https://github.com/LuaJIT/LuaJIT/issues/1449</a><br>* <a href="https://github.com/tarantool/tarantool/issues/12880">https://github.com/tarantool/tarantool/issues/12880</a><br><br>src/lj_carith.c | 2 +-<br>.../lj-1449-fix-ptr-diff-64-bit.test.lua | 39 +++++++++++++++++++<br>2 files changed, 40 insertions(+), 1 deletion(-)<br>create mode 100644 test/tarantool-tests/lj-1449-fix-ptr-diff-64-bit.test.lua<br><br>diff --git a/src/lj_carith.c b/src/lj_carith.c<br>index eb56d552..3384c2cd 100644<br>--- a/src/lj_carith.c<br>+++ b/src/lj_carith.c<br>@@ -120,7 +120,7 @@ static int carith_ptr(lua_State *L, CTState *cts, CDArith *ca, MMS mm)<br>/* All valid pointer differences on x64 are in (-2^47, +2^47),<br>** which fits into a double without loss of precision.<br>*/<br>- setintptrV(L->top-1, (int32_t)diff);<br>+ setintptrV(L->top-1, diff);<br>return 1;<br>} else if (mm == MM_lt) { /* Pointer comparison (unsigned). */<br>setboolV(L->top-1, ((uintptr_t)pp < (uintptr_t)pp2));<br>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<br>new file mode 100644<br>index 00000000..032b9b4d<br>--- /dev/null<br>+++ b/test/tarantool-tests/lj-1449-fix-ptr-diff-64-bit.test.lua<br>@@ -0,0 +1,39 @@<br>+local tap = require('tap')<br>+<br>+-- Test file to demonstrate LuaJIT's incorrect 64-bit pointer<br>+-- subtraction.<br>+-- See also: <a href="https://github.com/LuaJIT/LuaJIT/issues/1449">https://github.com/LuaJIT/LuaJIT/issues/1449</a>.<br>+<br>+local test = tap.test('lj-1449-fix-ptr-diff-64-bit')<br>+<br>+local ffi = require('ffi')<br>+<br>+test:plan(2)<br>+<br>+local diff = 0x80000001ULL<br>+local base = 0x700000000000ULL<br>+local p0 = ffi.cast('char *', base)<br>+local p1 = ffi.cast('char *', base + diff)<br>+<br>+test:is(p1 - p0, diff, 'correct pointer difference between 64-bit pointers')<br>+<br>+test:skipcond({<br>+ ['Test requires JIT enabled'] = not jit.status(),<br>+})<br>+<br>+local results = {}</div></div></div></div></div></div></blockquote></div></div><div class="cl-0gci5k0xlq"><div class="mail-quote-collapse"><blockquote style="border-left:1px solid #0857A6;margin:10px;padding:0 0 0 10px"><div><div><div class="cl-jmbqwszmq7"><div class="js-helper_mr_css_attr js-readmsg-msg_mr_css_attr"><div><div>+<br>+jit.opt.start('hotloop=1')<br>+<br>+for i = 1, 4 do<br>+ -- Use constants on trace.<br>+ local delta = 0x80000001ULL<br>+ local b = 0x700000000000ULL<br>+ local pt0 = ffi.cast('char *', b)<br>+ local pt1 = ffi.cast('char *', b + delta)<br>+ results[i] = pt1 - pt0<br>+end<br>+</div></div></div></div></div></div></blockquote>It seems, we need to add a correct sample to the results, e.g. table.insert(results, diff)<blockquote style="border-left:1px solid #0857A6;margin:10px;padding:0 0 0 10px"><div><div><div class="cl-jmbqwszmq7"><div class="js-helper_mr_css_attr js-readmsg-msg_mr_css_attr"><div><div>+test:samevalues(results, 'consistent JIT and VM behaviour for ptr subtraction')<br>+<br>+test:done(true)<br>--<br>2.55.0</div></div></div></div></div></div></blockquote></div></div></BODY></HTML>