Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Kaplun via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Maxim Kokryashkin <m.kokryashkin@tarantool.org>,
	Sergey Bronnikov <sergeyb@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH luajit 1/2] Fix bit op coercion in DUALNUM builds.
Date: Wed,  2 Oct 2024 11:09:05 +0300	[thread overview]
Message-ID: <7efd063772135322abfcc8de1214d20ebea69bad.1727855711.git.skaplun@tarantool.org> (raw)
In-Reply-To: <cover.1727855711.git.skaplun@tarantool.org>

From: Mike Pall <mike>

Thanks to Sergey Kaplun.

(cherry picked from commit f5fd22203eadf57ccbaa4a298010d23974b22fc0)

The `lj_carith_check64()` function coerces the given number value to the
32-bit wide value. In this case, the 64-bit-wide operands will lose
upper bits.

This patch removes the excess coercion for the DUALNUM mode.

Sergey Kaplun:
* added the description and the test for the problem

Part of tarantool/tarantool#10199
---
 src/lj_carith.c                               |  4 +---
 .../lj-1273-dualnum-bit-coercion.test.lua     | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 3 deletions(-)
 create mode 100644 test/tarantool-tests/lj-1273-dualnum-bit-coercion.test.lua

diff --git a/src/lj_carith.c b/src/lj_carith.c
index 90b3220f..eb56d552 100644
--- a/src/lj_carith.c
+++ b/src/lj_carith.c
@@ -349,9 +349,7 @@ uint64_t lj_carith_check64(lua_State *L, int narg, CTypeID *id)
   if (LJ_LIKELY(tvisint(o))) {
     return (uint32_t)intV(o);
   } else {
-    int32_t i = lj_num2bit(numV(o));
-    if (LJ_DUALNUM) setintV(o, i);
-    return (uint32_t)i;
+    return (uint32_t)lj_num2bit(numV(o));
   }
 }
 
diff --git a/test/tarantool-tests/lj-1273-dualnum-bit-coercion.test.lua b/test/tarantool-tests/lj-1273-dualnum-bit-coercion.test.lua
new file mode 100644
index 00000000..e83dbbcd
--- /dev/null
+++ b/test/tarantool-tests/lj-1273-dualnum-bit-coercion.test.lua
@@ -0,0 +1,19 @@
+local tap = require('tap')
+
+-- Test file to demonstrate LuaJIT misbehaviour on operating
+-- for 64-bit operands in built-in `bit` library in DUALNUM mode.
+-- See also, https://github.com/LuaJIT/LuaJIT/issues/1273.
+
+local test = tap.test('lj-1273-dualnum-bit-coercion')
+test:plan(1)
+
+local bit = require('bit')
+
+-- The cdata value for 2 ^ 33.
+local EXPECTED = 2 ^ 33 + 0LL
+-- Same value is used as mask for `bit.band()`.
+local MASK = EXPECTED
+
+test:is(bit.band(2 ^ 33, MASK), EXPECTED, 'correct bit.band result')
+
+test:done(true)
-- 
2.46.2


  reply	other threads:[~2024-10-02  8:10 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-02  8:09 [Tarantool-patches] [PATCH luajit 0/2] Fixes for 64 bit operands of the bit library Sergey Kaplun via Tarantool-patches
2024-10-02  8:09 ` Sergey Kaplun via Tarantool-patches [this message]
2024-10-08 10:12   ` [Tarantool-patches] [PATCH luajit 1/2] Fix bit op coercion in DUALNUM builds Sergey Bronnikov via Tarantool-patches
2024-10-11 19:08   ` Maxim Kokryashkin via Tarantool-patches
2024-10-02  8:09 ` [Tarantool-patches] [PATCH luajit 2/2] FFI: Fix 64 bit shift fold rules Sergey Kaplun via Tarantool-patches
2024-10-08 12:07   ` Sergey Bronnikov via Tarantool-patches
2024-10-08 14:24     ` Sergey Kaplun via Tarantool-patches
2024-10-09 14:29       ` Sergey Bronnikov via Tarantool-patches
2024-10-11 19:12       ` Maxim Kokryashkin via Tarantool-patches
2024-10-18 15:17 ` [Tarantool-patches] [PATCH luajit 0/2] Fixes for 64 bit operands of the bit library Sergey Kaplun via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7efd063772135322abfcc8de1214d20ebea69bad.1727855711.git.skaplun@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=m.kokryashkin@tarantool.org \
    --cc=sergeyb@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit 1/2] Fix bit op coercion in DUALNUM builds.' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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