Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Kaplun via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Igor Munkin <imun@tarantool.org>,
	Sergey Ostanevich <sergos@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH luajit] Fix frame traversal for __gc handler frames.
Date: Tue,  5 Oct 2021 13:28:29 +0300	[thread overview]
Message-ID: <20211005102829.30267-1-skaplun@tarantool.org> (raw)

From: Mike Pall <mike>

Reported by Changochen.

(cherry picked from 53f82e6e2e858a0a62fd1a2ff47e9866693382e6)

Additional stack traversal is needed to find an error function set for
handling runtime errors. cframe unwinding is missed for a C protected
frame during this stack traversal. It leads to undefined behaviour or
crash, when raising a runtime error on stack with the CP frame before an
error function handler (for example, an error in __gc handler).

This patch adds missing unwinding for CP frame.

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

Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-601-fix-gc-finderrfunc
Tarantool branch: https://github.com/tarantool/tarantool/tree/skaplun/gh-noticket-fix-gc-finderrfunc
LuaJIT issue: https://github.com/LuaJIT/LuaJIT/issues/601

 src/lj_err.c                                  |  1 +
 .../lj-601-fix-gc-finderrfunc.test.lua        | 25 +++++++++++++++++++
 2 files changed, 26 insertions(+)
 create mode 100644 test/tarantool-tests/lj-601-fix-gc-finderrfunc.test.lua

diff --git a/src/lj_err.c b/src/lj_err.c
index b6be357e..b520b3d3 100644
--- a/src/lj_err.c
+++ b/src/lj_err.c
@@ -585,6 +585,7 @@ static ptrdiff_t finderrfunc(lua_State *L)
       if (cframe_canyield(cf)) return 0;
       if (cframe_errfunc(cf) >= 0)
 	return cframe_errfunc(cf);
+      cf = cframe_prev(cf);
       frame = frame_prevd(frame);
     case FRAME_PCALL:
diff --git a/test/tarantool-tests/lj-601-fix-gc-finderrfunc.test.lua b/test/tarantool-tests/lj-601-fix-gc-finderrfunc.test.lua
new file mode 100644
index 00000000..d8d79100
--- /dev/null
+++ b/test/tarantool-tests/lj-601-fix-gc-finderrfunc.test.lua
@@ -0,0 +1,25 @@
+local tap = require('tap')
+local test = tap.test('lj-601-fix-gc-finderrfunc')
+-- Test file to demonstrate LuaJIT incorrect behaviour, when
+-- throwing error in __gc finalizer.
+-- See also, https://github.com/LuaJIT/LuaJIT/issues/601.
+local a = newproxy(true)
+getmetatable(a).__gc = function()
+  -- Function to raise error via `lj_err_run()` inside __gc.
+  local _ = load(function() collectgarbage()() end)
+-- XXX: Generate a small bunch of proxies. Need several to call
+-- `collectgarbage()` on another proxy inside __gc. N cycles is
+-- empirical number.
+for _ = 1, 4 do newproxy(a) end
+test:ok(true, 'successfully collectgarbage with error')
+os.exit(test:check() and 0 or 1)

             reply	other threads:[~2021-10-05 10:30 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-05 10:28 Sergey Kaplun via Tarantool-patches [this message]
2021-10-07 16:31 ` sergos via Tarantool-patches
2021-10-08  8:39   ` Sergey Kaplun via Tarantool-patches
2021-10-14  8:58     ` sergos via Tarantool-patches
2021-11-02 16:08     ` Igor Munkin via Tarantool-patches
2021-11-08 10:42       ` Sergey Kaplun via Tarantool-patches
2021-11-23 12:57         ` 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:

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

  git send-email \
    --in-reply-to=20211005102829.30267-1-skaplun@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imun@tarantool.org \
    --cc=sergos@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit] Fix frame traversal for __gc handler frames.' \


* 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