[Tarantool-patches] [PATCH luajit 2/6] FFI: Fix missing cts->L initialization in argv2ctype().

Sergey Kaplun skaplun at tarantool.org
Mon Oct 23 12:22:02 MSK 2023


From: Mike Pall <mike>

(cherry-picked from commit 50d6883e6027c4c2f9a5e495fee6b7fff1bd73c9)

When start trace recording without an initialized `L` in CType State
(possible if the recording is started before any `ffi` library usage),
the corresponding assertion fails in the `lj_ctype_intern()`. This patch
adds missing initialization during recording.

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

Part of tarantool/tarantool#9145
---
 src/lj_crecord.c                                  |  2 +-
 .../fix-argv2ctype-cts-L-init.test.lua            | 15 +++++++++++++++
 .../fix-argv2ctype-cts-L-init/script.lua          | 14 ++++++++++++++
 3 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100644 test/tarantool-tests/fix-argv2ctype-cts-L-init.test.lua
 create mode 100644 test/tarantool-tests/fix-argv2ctype-cts-L-init/script.lua

diff --git a/src/lj_crecord.c b/src/lj_crecord.c
index e1d1110f..10d1dc70 100644
--- a/src/lj_crecord.c
+++ b/src/lj_crecord.c
@@ -78,7 +78,7 @@ static CTypeID argv2ctype(jit_State *J, TRef tr, cTValue *o)
     /* Specialize to the string containing the C type declaration. */
     emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, s));
     cp.L = J->L;
-    cp.cts = ctype_ctsG(J2G(J));
+    cp.cts = ctype_cts(J->L);
     oldtop = cp.cts->top;
     cp.srcname = strdata(s);
     cp.p = strdata(s);
diff --git a/test/tarantool-tests/fix-argv2ctype-cts-L-init.test.lua b/test/tarantool-tests/fix-argv2ctype-cts-L-init.test.lua
new file mode 100644
index 00000000..ee45e424
--- /dev/null
+++ b/test/tarantool-tests/fix-argv2ctype-cts-L-init.test.lua
@@ -0,0 +1,15 @@
+local tap = require('tap')
+local test = tap.test('fix-argv2ctype-cts-L-init'):skipcond({
+  ['Test requires JIT enabled'] = not jit.status(),
+})
+
+-- Loading of 'tap' module initialize `cts->L` during parsing.
+-- Run standalone script for testing.
+local script = require('utils').exec.makecmd(arg)
+
+test:plan(1)
+
+local output = script()
+test:is(output, 'OK', 'correct recording with uninitialized cts->L')
+
+test:done(true)
diff --git a/test/tarantool-tests/fix-argv2ctype-cts-L-init/script.lua b/test/tarantool-tests/fix-argv2ctype-cts-L-init/script.lua
new file mode 100644
index 00000000..2131385a
--- /dev/null
+++ b/test/tarantool-tests/fix-argv2ctype-cts-L-init/script.lua
@@ -0,0 +1,14 @@
+local ffi = require('ffi')
+
+jit.opt.start('hotloop=1')
+
+local i = 1
+-- Use `while` to start compilation of the trace at the first
+-- iteration, before `ffi.new()` is called, so `cts->L` is
+-- uninitialized.
+while i < 3 do
+  ffi.new('uint64_t', i)
+  i = i + 1
+end
+
+print('OK')
-- 
2.42.0



More information about the Tarantool-patches mailing list