From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id ACBB66F3EA; Fri, 24 Jul 2026 12:45:59 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org ACBB66F3EA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1784886359; bh=HelCNRmG3C7kwdYRlDceQM2UQ0+IqgbhYjYdoR+MALc=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Q9pu7aOgwmA88pxmb3WZbznu9CcizYA6LCkurs8jgkDXNk5iZBPzkvq1X5RUu9G1u JRej6kxaWDaBjQQv7OVEeFrxcWz4cuAZW649q6r1y5eyMKXdBH1ektNjsf+DxeiMzA k6kGev9lBV9Co8HI3j5IA4N0KZD2tXrJRXnonOLM= Received: from send173.i.mail.ru (send173.i.mail.ru [95.163.59.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 8099B6F3EA for ; Fri, 24 Jul 2026 12:45:58 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 8099B6F3EA Received: by exim-smtp-66d48668b6-psfvx with esmtpa (envelope-from ) id 1wnCTd-00000000Iil-1fbm; Fri, 24 Jul 2026 12:45:57 +0300 Content-Type: multipart/alternative; boundary="------------VYTXGhqq8f4BfNiXhV2xfl1K" Message-ID: <1a426cbc-b899-4d74-8b47-2521df6c3110@tarantool.org> Date: Fri, 24 Jul 2026 12:45:55 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: Sergey Kaplun , Evgeniy Temirgaleev Cc: tarantool-patches@dev.tarantool.org References: <20260720142430.2571440-1-skaplun@tarantool.org> In-Reply-To: <20260720142430.2571440-1-skaplun@tarantool.org> X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD9D0E21E736CB183FE271D0AAB18E394BAE57937E206CDEBD3182A05F5380850404C228DA9ACA6FE27089474A121B212773DE06ABAFEAF6705770B240214951108202F34C0695831664CE688CB148548FA X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE770FEBAE29342FA8FEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637AC83A81C8FD4AD23D82A6BABE6F325AC2E85FA5F3EDFCBAA7353EFBB55337566DDF253B6BCADDB7947810ABEAD67854FCAA86EB74D35B47EDCD3DA2BEDBC4308389733CBF5DBD5E913377AFFFEAFD269176DF2183F8FC7C0A29E2F051442AF778941B15DA834481FCF19DD082D7633A0EF3E4896CB9E6436389733CBF5DBD5E9D5E8D9A59859A8B6D52CD31C43BF465FCC7F00164DA146DA6F5DAA56C3B73B237318B6A418E8EAB86D1867E19FE14079C09775C1D3CA48CF3D321E7403792E342EB15956EA79C166A417C69337E82CC275ECD9A6C639B01B78DA827A17800CE7EE061C4D93700B7A731C566533BA786AA5CC5B56E945C8DA X-C1DE0DAB: 0D63561A33F958A577FA91DE6C40693E5002B1117B3ED69679F719DE7616A16B33EE06AFCD9648883610D81D389A125CDE35189EBF2DEA28FEA14CD2CD220BB99C5DF10A05D560A9880EC71AF561E0AAD9143641EC25BB399D3BD01E92EE41A1 X-C8649E89: 1C3962B70DF3F0AD73CAD6646DEDE191716CD42B3DD1D34CAB70F9BE574AE9C625B6776AC983F447FC0B9F89525902EE6F57B2FD27647F25E66C117BDB76D659F91C18EAE64DF2D00AD8548F7DB93F14CFE3E3974A32FE71A509065389A6C29D8C9443C89502051CB8341EE9D5BE9A0A520B2F3A3FACCD88591360899F534B38BDE330FCCE7AF2096536EB022892E5344C41F94D744909CE2512F26BEC029E55448553D2254B8D95CD72808BE417F3B9E0E7457915DAA85F X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXMZebaIdHP2ghjoIc/363UZI6Kf1ptIMVWmxowtcrDwUF83gQ6Z+59I= X-DA7885C5: 41544C9FB97CDAC4F255D290C0D534F99EC5A9CD45D36DF140B1303A58D210B886ED7EA972AD16C35B1A4C17EAA7BC4BEF2421ABFA55128DAF83EF9164C44C7E X-Mailru-Sender: 689FA8AB762F7393520AF17B8A65FDE2264F512C0F3ED4B1C7758804C6809AC8DCF1EDC3296431FFEF86D5F70DA33880E41E8EF7A07863ECB274557F927329BE2DDF8182D28ACDB545BD1C3CC395C826B4A721A3011E896F X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit] FFI: Fix pointer difference operation on 64 bit platforms. X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" This is a multi-part message in MIME format. --------------VYTXGhqq8f4BfNiXhV2xfl1K Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Hi, Sergey, thanks for the patch! LGTM Sergey On 7/20/26 17:24, Sergey Kaplun wrote: > From: Mike Pall > > 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 > @@ -120,7 +120,7 @@ static int carith_ptr(lua_State *L, CTState *cts, CDArith *ca, MMS mm) > /* All valid pointer differences on x64 are in (-2^47, +2^47), > ** which fits into a double without loss of precision. > */ > - setintptrV(L->top-1, (int32_t)diff); > + setintptrV(L->top-1, diff); > return 1; > } else if (mm == MM_lt) { /* Pointer comparison (unsigned). */ > setboolV(L->top-1, ((uintptr_t)pp < (uintptr_t)pp2)); > 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 > @@ -0,0 +1,39 @@ > +local tap = require('tap') > + > +-- Test file to demonstrate LuaJIT's incorrect 64-bit pointer > +-- subtraction. > +-- See also:https://github.com/LuaJIT/LuaJIT/issues/1449. > + > +local test = tap.test('lj-1449-fix-ptr-diff-64-bit') > + > +local ffi = require('ffi') > + > +test:plan(2) > + > +local diff = 0x80000001ULL > +local base = 0x700000000000ULL > +local p0 = ffi.cast('char *', base) > +local p1 = ffi.cast('char *', base + diff) > + > +test:is(p1 - p0, diff, 'correct pointer difference between 64-bit pointers') > + > +test:skipcond({ > + ['Test requires JIT enabled'] = not jit.status(), > +}) > + > +local results = {} > + > +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 > + > +test:samevalues(results, 'consistent JIT and VM behaviour for ptr subtraction') > + > +test:done(true) --------------VYTXGhqq8f4BfNiXhV2xfl1K Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit

Hi, Sergey,

thanks for the patch! LGTM

Sergey

On 7/20/26 17:24, Sergey Kaplun wrote:
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
@@ -120,7 +120,7 @@ static int carith_ptr(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
 	/* All valid pointer differences on x64 are in (-2^47, +2^47),
 	** which fits into a double without loss of precision.
 	*/
-	setintptrV(L->top-1, (int32_t)diff);
+	setintptrV(L->top-1, diff);
 	return 1;
       } else if (mm == MM_lt) {  /* Pointer comparison (unsigned). */
 	setboolV(L->top-1, ((uintptr_t)pp < (uintptr_t)pp2));
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
@@ -0,0 +1,39 @@
+local tap = require('tap')
+
+-- Test file to demonstrate LuaJIT's incorrect 64-bit pointer
+-- subtraction.
+-- See also: https://github.com/LuaJIT/LuaJIT/issues/1449.
+
+local test = tap.test('lj-1449-fix-ptr-diff-64-bit')
+
+local ffi = require('ffi')
+
+test:plan(2)
+
+local diff = 0x80000001ULL
+local base = 0x700000000000ULL
+local p0 = ffi.cast('char *', base)
+local p1 = ffi.cast('char *', base + diff)
+
+test:is(p1 - p0, diff, 'correct pointer difference between 64-bit pointers')
+
+test:skipcond({
+  ['Test requires JIT enabled'] = not jit.status(),
+})
+
+local results = {}
+
+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
+
+test:samevalues(results, 'consistent JIT and VM behaviour for ptr subtraction')
+
+test:done(true)
--------------VYTXGhqq8f4BfNiXhV2xfl1K--