Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Kaplun via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Maxim Kokryashkin <m.kokryashkin@tarantool.org>,
	Sergey Bronnikov <sergeyb@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH luajit 3/6] FFI: Ensure returned string is alive in ffi.typeinfo().
Date: Mon, 23 Oct 2023 12:22:03 +0300	[thread overview]
Message-ID: <78ee203fe7832fde6d2f1dfb40591b8f191e59a5.1698049570.git.skaplun@tarantool.org> (raw)
In-Reply-To: <cover.1698049570.git.skaplun@tarantool.org>

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


  parent reply	other threads:[~2023-10-23  9:28 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-23  9:22 [Tarantool-patches] [PATCH luajit 0/6] FFI fixes Sergey Kaplun via Tarantool-patches
2023-10-23  9:22 ` [Tarantool-patches] [PATCH luajit 1/6] Abstract out on-demand loading of FFI library Sergey Kaplun via Tarantool-patches
2023-10-24 14:38   ` Maxim Kokryashkin via Tarantool-patches
2023-11-29 14:35   ` Sergey Bronnikov via Tarantool-patches
2023-10-23  9:22 ` [Tarantool-patches] [PATCH luajit 2/6] FFI: Fix missing cts->L initialization in argv2ctype() Sergey Kaplun via Tarantool-patches
2023-10-24 14:49   ` Maxim Kokryashkin via Tarantool-patches
2023-10-25 10:19     ` Sergey Kaplun via Tarantool-patches
2023-11-29 14:39   ` Sergey Bronnikov via Tarantool-patches
2023-10-23  9:22 ` Sergey Kaplun via Tarantool-patches [this message]
2023-10-24 21:33   ` [Tarantool-patches] [PATCH luajit 3/6] FFI: Ensure returned string is alive in ffi.typeinfo() Maxim Kokryashkin via Tarantool-patches
2023-11-29 14:48   ` Sergey Bronnikov via Tarantool-patches
2023-10-23  9:22 ` [Tarantool-patches] [PATCH luajit 4/6] FFI: Fix dangling reference to CType Sergey Kaplun via Tarantool-patches
2023-10-25  7:48   ` Maxim Kokryashkin via Tarantool-patches
2023-10-25 10:43     ` Sergey Kaplun via Tarantool-patches
2023-10-25 11:42       ` Maxim Kokryashkin via Tarantool-patches
2023-10-25  8:06   ` Maxim Kokryashkin via Tarantool-patches
2023-11-29 15:00   ` Sergey Bronnikov via Tarantool-patches
2023-10-23  9:22 ` [Tarantool-patches] [PATCH luajit 5/6] FFI: Fix dangling reference to CType. Improve checks Sergey Kaplun via Tarantool-patches
2023-10-25  8:05   ` Maxim Kokryashkin via Tarantool-patches
2023-10-25 10:46     ` Sergey Kaplun via Tarantool-patches
2023-10-25 11:42       ` Maxim Kokryashkin via Tarantool-patches
2023-11-29 15:41   ` Sergey Bronnikov via Tarantool-patches
2023-11-30  7:25     ` Sergey Kaplun via Tarantool-patches
2023-12-19 10:55       ` Sergey Bronnikov via Tarantool-patches
2023-10-23  9:22 ` [Tarantool-patches] [PATCH luajit 6/6] FFI: Fix dangling reference to CType in carith_checkarg() Sergey Kaplun via Tarantool-patches
2023-10-25  8:07   ` Maxim Kokryashkin via Tarantool-patches
2023-10-25 10:48     ` Sergey Kaplun via Tarantool-patches
2023-10-25 11:42       ` Maxim Kokryashkin via Tarantool-patches
2023-12-19 10:59   ` Sergey Bronnikov via Tarantool-patches
2023-12-19 11:01 ` [Tarantool-patches] [PATCH luajit 0/6] FFI fixes Sergey Bronnikov via Tarantool-patches
2024-01-10  8:53 ` Igor Munkin via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=78ee203fe7832fde6d2f1dfb40591b8f191e59a5.1698049570.git.skaplun@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=m.kokryashkin@tarantool.org \
    --cc=sergeyb@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit 3/6] FFI: Ensure returned string is alive in ffi.typeinfo().' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox