[Tarantool-patches] [PATCH luajit 3/6] FFI: Ensure returned string is alive in ffi.typeinfo().

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


From: Mike Pall <mike>

Reported by zhuizhuhaomeng.

(cherry-picked from commit 94a40bb238092e73f3dc0c3626911a7efa997c22)

`ffi.typeinfo()` doesn't check that the string containing the name type
is alive when it sets this string to the returned table. This leads to
the corresponding assertion failure in `checklivetv()`. This patch flips
the white colour of the stored string to resurrect it.

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

Part of tarantool/tarantool#9145
---
 src/lib_ffi.c                                 |  1 +
 .../lj-745-ffi-typeinfo-dead-names.test.lua   | 28 +++++++++++++++++++
 2 files changed, 29 insertions(+)
 create mode 100644 test/tarantool-tests/lj-745-ffi-typeinfo-dead-names.test.lua

diff --git a/src/lib_ffi.c b/src/lib_ffi.c
index 62af54c1..e60e7b19 100644
--- a/src/lib_ffi.c
+++ b/src/lib_ffi.c
@@ -573,6 +573,7 @@ LJLIB_CF(ffi_typeinfo)
       setintV(lj_tab_setstr(L, t, lj_str_newlit(L, "sib")), (int32_t)ct->sib);
     if (gcref(ct->name)) {
       GCstr *s = gco2str(gcref(ct->name));
+      if (isdead(G(L), obj2gco(s))) flipwhite(obj2gco(s));
       setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "name")), s);
     }
     lj_gc_check(L);
diff --git a/test/tarantool-tests/lj-745-ffi-typeinfo-dead-names.test.lua b/test/tarantool-tests/lj-745-ffi-typeinfo-dead-names.test.lua
new file mode 100644
index 00000000..02e13759
--- /dev/null
+++ b/test/tarantool-tests/lj-745-ffi-typeinfo-dead-names.test.lua
@@ -0,0 +1,28 @@
+local tap = require('tap')
+local ffi = require('ffi')
+local test = tap.test('lj-745-ffi-typeinfo-dead-names')
+
+test:plan(1)
+
+-- Start from the beginning of the GC cycle.
+collectgarbage()
+
+local function ctypes_iteration()
+  local i = 1
+  -- Test `checklivetv()` assertion in `setstrV()` inside
+  -- `ffi.typeinfo()`.
+  while ffi.typeinfo(i) do i = i + 1 end
+end
+
+-- Call `ffi.typeinfo()` much enough to be sure that strings with
+-- names of types become dead. The number of iterations is big
+-- enough (more than x2 of the required value) to see assertion
+-- failure guaranteed under Tarantool too (it has much more alive
+-- objects on start).
+for _ = 1, 100 do
+  ctypes_iteration()
+end
+
+test:ok(true, 'no assertion failure inside ffi.typeinfo()')
+
+test:done(true)
-- 
2.42.0



More information about the Tarantool-patches mailing list