Hi, Sergey!
thanks for the patch! LGTM with minor comments.
Sergey
From: Mike Pall <mike>
Thanks to RaphaelIT7.
(cherry picked from commit acf8fcbd932a68d130fa3e3654e1b622e10fd6af)
The `math.floor()`, `math.ceil()` VM implementation uses the `.ffunc`
macro, which doesn't check the given arguments. If the top stack value
contains some garbage number value, it will be used as the function
result instead of raising an error.
This patch fixes the behaviour for non-GC64 mode by using the correct
`.ffunc_1` macro. The GC64 VM will be fixed in the next patch.
Sergey Kaplun:
* added the description and the test for the problem
Part of tarantool/tarantool#13442
---
src/vm_x86.dasc | 2 +-
.../lj-1532-math-floor-ceil-argcheck.test.lua | 36 +++++++++++++++++++
2 files changed, 37 insertions(+), 1 deletion(-)
create mode 100644 test/tarantool-tests/lj-1532-math-floor-ceil-argcheck.test.lua
diff --git a/src/vm_x86.dasc b/src/vm_x86.dasc
index 19653fee..e875d073 100644
--- a/src/vm_x86.dasc
+++ b/src/vm_x86.dasc
@@ -2212,7 +2212,7 @@ static void build_subroutines(BuildCtx *ctx)
|.endif
|
|.macro math_round, func
- | .ffunc math_ .. func
+ | .ffunc_1 math_ .. func
|.if DUALNUM
| cmp dword [BASE+4], LJ_TISNUM; jne >1
| mov RB, dword [BASE]; jmp ->fff_resi
diff --git a/test/tarantool-tests/lj-1532-math-floor-ceil-argcheck.test.lua b/test/tarantool-tests/lj-1532-math-floor-ceil-argcheck.test.lua
new file mode 100644
index 00000000..b9def637
--- /dev/null
+++ b/test/tarantool-tests/lj-1532-math-floor-ceil-argcheck.test.lua
@@ -0,0 +1,36 @@
+local tap = require('tap')
+
+-- Test file to demonstrate LuaJIT incorrect handling of no
+-- arguments for `math.ceil()`, `math.floor()` functions.
+-- See also: https://github.com/LuaJIT/LuaJIT/issues/1532.
+
+local test = tap.test('lj-1532-math-floor-ceil-argcheck')
+
+test:plan(2)
+
+-- A simple function to push a number value for `math.floor()`,
+-- `math.ceil()` to the Lua stack.
In fact, a function can push a value for any function, not only `math.floor()` or `math.ceil()`.
I would omit the comment at all.
nit: we usually put a comments before the line, isn't it?+local function stack() + return 1.2 +end + +local function call_wp(f) + stack() -- Push some value to the stack to mark the slot.
+ -- Use non-inlined variant for function name in the error
+ -- message.
+ local res = f()
+ return res
+end
+
+local function math_func_test(subtest, f)
+ subtest:plan(2)
+ local err, msg = pcall(call_wp, f)
+ subtest:is(err, false, 'correct status for empty ' .. subtest.name)
+ subtest:like(msg, "bad argument #1 to 'f' %(number expected, got no value%)",
+ 'correct error message for empty ' .. subtest.name)
+end
+
+test:test('math.ceil', math_func_test, math.ceil)
+test:test('math.floor', math_func_test, math.floor)
+
+test:done(true)