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 E67B16ECCD; Mon, 27 Jul 2026 10:39:44 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org E67B16ECCD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1785137985; bh=5h3M3ok1kx0ffKG+LiMuEKEEM9sLyhEFDwFgbJQvlCI=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=m5nfJZmSljSLx/VUf4xB3lWBFEOfAME7G6K7Er1gwJLfqBF4owjF8jdQoJmjydcFe UzRBS+favnCqR30USPnE9vmlsoFAmxU+OWXifX+jesdhpV4wqVN8VcT1LHtOG1o4Ii BybNWefDDIhKZM4bZnYYHvVCUKF9QV15+aeglsrM= Received: from send83.i.mail.ru (send83.i.mail.ru [89.221.237.178]) (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 52DFA6ECCD for ; Mon, 27 Jul 2026 10:39:43 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 52DFA6ECCD Received: by exim-smtp-6c76488b9f-55xb9 with esmtpa (envelope-from ) id 1woFw6-00000000Doc-10dt; Mon, 27 Jul 2026 10:39:42 +0300 Date: Mon, 27 Jul 2026 10:39:28 +0300 To: Sergey Bronnikov Message-ID: References: <7266d024dc58e71ec74929a2f2973c4454161c59.1784895433.git.sergeyb@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <7266d024dc58e71ec74929a2f2973c4454161c59.1784895433.git.sergeyb@tarantool.org> X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 78E4E2B564C1792B X-77F55803: 4F1203BC0FB41BD93ED3C6BF6FFA701F0B3D367FDA022B56BB42D82DE118CAD0182A05F53808504033C54FA29BB6275D3DE06ABAFEAF6705DF2A3904C2BE6B47B5D9755D177B73810134F15712FB01B7 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7B4D64F1449264784EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637F88016AB904663428638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B2390F2BFD0A38C8AA2E070BE324C7D3C483BE81142514C46CF6B57BC7E64490618DEB871D839B73339E8FC8737B5C22495FF0BFC5AEE34BE6CC7F00164DA146DAFE8445B8C89999729449624AB7ADAF37F6B57BC7E64490611E7FA7ABCAF51C92176DF2183F8FC7C05A64D9A1E9CA65708941B15DA834481F9449624AB7ADAF37BA3038C0950A5D3613377AFFFEAFD269176DF2183F8FC7C0A25C78F1EB8CDD6C7B076A6E789B0E97A8DF7F3B2552694AD5FFEEA1DED7F25D49FD398EE364050F140C956E756FBB7ACAA9C333723BB66AB3661434B16C20ACC84D3B47A649675FE827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8BF8ED170A0960FA5675ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: 0D63561A33F958A56B84213A5A25D4145002B1117B3ED6962677A4CEEF73FC5B19AC5B239BAD4335823CB91A9FED034534781492E4B8EEAD8D8BB953E4894305BDAD6C7F3747799A X-C8649E89: 1C3962B70DF3F0AD73CAD6646DEDE1918E10F71CB4DF9F96AB70F9BE574AE9C625B6776AC983F447FC0B9F89525902EE6F57B2FD27647F25E66C117BDB76D659A29E632CF28378EB00425AE876F01589F6A68C9DB1F554730B108D5EE7D32E01A363AB3B465013C7B8341EE9D5BE9A0A38D62A48767DD5BC0B1EFEF5B321B1E1C0F96AF9FA6BEBB66536EB022892E5344C41F94D744909CECFA6C6B0C050A61A8CAF69B82BA93681CD72808BE417F3B9E0E7457915DAA85F X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXMZebaIdHP2ghjoIc/363UZI6Kf1ptIMVczkDsfooUjf8M1VAC3IGYA= X-Mailru-Sender: 520A125C2F17F0B17094CDC02B85F11BFA721A8BE11675A83DE06ABAFEAF6705DF2A3904C2BE6B47B7CBEF92542CD7C88B0A2698F12F5C9EC77752E0C033A69E86920BD37369036789A8C6A0E60D2BB63A5DB60FBEB33A8A0DA7A0AF5A3A8387 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit] FFI: Prevent sanitizer warning in carith_ptr(). 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 Kaplun via Tarantool-patches Reply-To: Sergey Kaplun Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Hi, Sergey! Thanks for the patch! Please consider my comments below. On 24.07.26, Sergey Bronnikov wrote: > From: Mike Pall > > Reported by Sergey Bronnikov. > > (cherry picked from commit c3b379bf50c8819c61daa3afd9f21d9ec5708bd5) > Side note: We may mention that this is a follow-up for the commit 78f4de4d7d0c410eeaf75cec5bb178fdb7db1452 ("Avoid negation of signed integers in C that may hold INT*_MIN."). Feel free to ignore. > The Undefined Behaviour Sanitizer [1] produce a warning about Typo: s/produce/produces/ > signed integer overflow in the function carith_ptr(), when idx > equals INT_MIN in the expression idx = -idx. In practice, > negating -2147483648 wraps back to itself instead of producing the It's actually -9223372036854775808 or -0x8000000000000000 for 64-bit architecture. Let's use hexademic notation (or just the `INT_MIN`) instead of this number. > correct positive value, leading to incorrect pointer arithmetic in > carith_ptr() for MM_sub (pointer subtraction). The patch fixes > that by converting idx to unsigned (uintptr_t), computes two's > complement negation via bitwise negation + 1 (safe from overflow > in unsigned arithmetic), and casts back to ptrdiff_t. This avoids > signed overflow entirely and correctly negates even the INT_MIN > edge case. > > [1]: https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html > > Sergey Bronnikov: > * added the description and the test for the problem > > Part of tarantool/tarantool#12480 Typo: s/12480/12880/ > --- > > Branch: https://github.com/tarantool/luajit/tree/ligurio/lj-1459-ub-carith_ptr > Related issues: > - https://github.com/LuaJIT/LuaJIT/issues/1459 > - https://github.com/tarantool/tarantool/issues/12880 > > src/lj_carith.c | 2 +- > .../lj-1459-subtraction-carith.test.lua | 16 ++++++++++++++++ > 2 files changed, 17 insertions(+), 1 deletion(-) > create mode 100644 test/tarantool-tests/lj-1459-subtraction-carith.test.lua > > diff --git a/src/lj_carith.c b/src/lj_carith.c > index eb56d552..e971bfc2 100644 > --- a/src/lj_carith.c > +++ b/src/lj_carith.c > diff --git a/test/tarantool-tests/lj-1459-subtraction-carith.test.lua b/test/tarantool-tests/lj-1459-subtraction-carith.test.lua > new file mode 100644 > index 00000000..df401a56 > --- /dev/null > +++ b/test/tarantool-tests/lj-1459-subtraction-carith.test.lua > @@ -0,0 +1,16 @@ > +local tap = require('tap') > + > +-- The test file to demonstrate UBSan warning in carith_ptr(). Typo: s/UBSan/the UBSan/ Minor: s/carith_ptr()/`carith_ptr()`/ > +-- See also: https://github.com/LuaJIT/LuaJIT/issues/1459. > +local test = tap.test('lj-1459-subtraction-carith') > + > +test:plan(2) > + > +local func = load('_ = nil - 0LL%0') Minor: Let's use loadstring instead (I know that they are literally the same function in LuaJIT, but it's semantically better to use it for loading strings, while `load()` is used for generators). For Lua 5.1 they are not the same: | lua5.1 -e 'load"print(1)"()' | lua5.1: (command line):1: bad argument #1 to 'load' (function expected, got string) The `loadstring()` has been deprecated in Lua5.2. This `0LL%0` looks weird. Let's just use the resulting `-0x8000000000000000LL` instead. Also, please add the comment that `nil` as the first operand of subtraction is required, since it is required to trigger metamethod invocation. It successfully passes the argument check since it may be considered as NULL ptr for other metamethods. > +local res, err = pcall(func) > + > +test:is(res, false, 'correct result') > +local error_msg = "attempt to perform arithmetic on 'nil' and 'int64_t'" > +test:ok(err:match(error_msg), 'error on subtraction') > + > +test:done(true) > -- > 2.43.0 > -- Best regards, Sergey Kaplun