[Tarantool-patches] [PATCH luajit] Fix predict_next() in parser.
Sergey Kaplun
skaplun at tarantool.org
Tue Aug 15 17:25:41 MSK 2023
From: Mike Pall <mike>
Reported by Sergey Kaplun.
(cherry-picked from commit caf7cbc57c945f7b68871ad72abafb2b6e6fb7f5)
Assume, we have the following Lua code:
| local _
| for _ in (nil):foo() do end
The first part of the bytecode emitted for it is the following:
| 0001 KNIL 0 1
| 0002 MOV 2 1
| 0003 TGETS 1 1 0 ; "foo"
| 0004 CALL 1 4 2
The `0001 KNIL` is a result of merging two `KPRI` instructions: one for
the local variable, one for the slot with `nil` object. During parsing in
`predict_next()` the second `MOV` bytecode is examined to set `pairs` or
`next` local variable. But, as far as it moves `nil` value, that isn't
an actual variable, so it has no the name this leads to the crash.
This patch adds the check to be sure that `RD` in the `MOV` bytecode is
an actual variable.
Sergey Kaplun:
* added the description and the test for the problem
Part of tarantool/tarantool#8825
---
Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-1033-fix-parsing-predict-next
PR: https://github.com/tarantool/tarantool/pull/8987
Related issues:
* https://github.com/LuaJIT/LuaJIT/issues/1033
* https://github.com/tarantool/tarantool/issues/8825
src/lj_parse.c | 1 +
.../lj-1033-fix-parsing-predict-next.test.lua | 30 +++++++++++++++++++
2 files changed, 31 insertions(+)
create mode 100644 test/tarantool-tests/lj-1033-fix-parsing-predict-next.test.lua
diff --git a/src/lj_parse.c b/src/lj_parse.c
index 3f6caaec..420b95cb 100644
--- a/src/lj_parse.c
+++ b/src/lj_parse.c
@@ -2532,6 +2532,7 @@ static int predict_next(LexState *ls, FuncState *fs, BCPos pc)
cTValue *o;
switch (bc_op(ins)) {
case BC_MOV:
+ if (bc_d(ins) >= fs->nactvar) return 0;
name = gco2str(gcref(var_get(ls, fs, bc_d(ins)).name));
break;
case BC_UGET:
diff --git a/test/tarantool-tests/lj-1033-fix-parsing-predict-next.test.lua b/test/tarantool-tests/lj-1033-fix-parsing-predict-next.test.lua
new file mode 100644
index 00000000..624344eb
--- /dev/null
+++ b/test/tarantool-tests/lj-1033-fix-parsing-predict-next.test.lua
@@ -0,0 +1,30 @@
+local tap = require('tap')
+local test = tap.test('lj-1033-fix-parsing-predict-next')
+
+test:plan(3)
+
+local res_f = loadstring([[
+-- This local variable is necessary, because it emits `KPRI`
+-- bytecode, with which the next `KPRI` bytecode will be merged.
+--
+-- The resulting bytecode is the following:
+--
+-- 0001 KNIL 0 1
+-- 0002 MOV 2 1
+-- 0003 TGETS 1 1 0 ; "foo"
+-- 0004 CALL 1 4 2
+--
+-- This MOV don't use any variable value from the stack, so the
+-- attempt to get the name in `predict_next() leads to the crash.
+local _
+for _ in (nil):foo() do end
+]])
+
+test:ok(res_f, 'chunk loaded sucsessfully')
+
+local res, err = pcall(res_f)
+
+test:ok(not res, 'loaded function not executed')
+test:like(err, 'attempt to index a nil value', 'correct error message')
+
+test:done(true)
--
2.41.0
More information about the Tarantool-patches
mailing list