[Tarantool-patches] [PATCH luajit 1/2] Fix bit op coercion in DUALNUM builds.

Sergey Kaplun skaplun at tarantool.org
Wed Oct 2 11:09:05 MSK 2024


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



More information about the Tarantool-patches mailing list