Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Kaplun via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Sergey Bronnikov <sergeyb@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH luajit 2/2] Fix VM event error handling for finalizers.
Date: Sat, 28 Mar 2026 18:31:17 +0300	[thread overview]
Message-ID: <97ef9d8e0cbd59df6ef18a57dd44554f44c12129.1774711616.git.skaplun@tarantool.org> (raw)
In-Reply-To: <cover.1774711616.git.skaplun@tarantool.org>

From: Mike Pall <mike>

Reported by Sergey Kaplun.

(cherry picked from commit fbb36bb6bfa88716a47c58bcf9ce9f2ef752abac)

After the previous commit, the VM handler for the 'errfin' VM event
works incorrectly. The error object is taken not from the stack on which
the VM handler is invoked. Hence, it breaks the non-VM stack and returns
an incorrect error message.

This patch fixes the issue by copying the object from the corresponding
Lua stack.

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

Part of tarantool/tarantool#12134
---
 src/lj_gc.c                                   |  6 +++--
 .../lj-1445-errfin-errmsg.test.lua            | 27 +++++++++++++++++++
 2 files changed, 31 insertions(+), 2 deletions(-)
 create mode 100644 test/tarantool-tests/lj-1445-errfin-errmsg.test.lua

diff --git a/src/lj_gc.c b/src/lj_gc.c
index 83ad9ee9..7e4f0a47 100644
--- a/src/lj_gc.c
+++ b/src/lj_gc.c
@@ -519,10 +519,12 @@ static void gc_call_finalizer(global_State *g, lua_State *L,
   if (LJ_HASPROFILE && (oldh & HOOK_PROFILE)) lj_dispatch_update(g);
   g->gc.threshold = oldt;  /* Restore GC threshold. */
   if (errcode) {
+    TValue tmp;
+    copyTV(VL, &tmp, VL->top-1);
+    VL->top--;
     lj_vmevent_send(g, ERRFIN,
-      copyTV(V, V->top++, L->top-1);
+      copyTV(V, V->top++, &tmp);
     );
-    L->top--;
   }
 }
 
diff --git a/test/tarantool-tests/lj-1445-errfin-errmsg.test.lua b/test/tarantool-tests/lj-1445-errfin-errmsg.test.lua
new file mode 100644
index 00000000..def8b79c
--- /dev/null
+++ b/test/tarantool-tests/lj-1445-errfin-errmsg.test.lua
@@ -0,0 +1,27 @@
+local tap = require('tap')
+local test = tap.test('lj-1445-errfin-errmsg')
+
+test:plan(2)
+
+local EXPECTED_ERR = 'expected err'
+local function bad_fin()
+  error(EXPECTED_ERR)
+end
+local EXPECTED_LOCATION = debug.getinfo(bad_fin).linedefined + 1
+
+local match_err = false
+local match_line = false
+local function errfin_handler(errmsg)
+  match_err = errmsg:match(EXPECTED_ERR)
+  match_line = errmsg:match(EXPECTED_LOCATION)
+end
+
+jit.attach(errfin_handler, 'errfin')
+
+debug.getmetatable(newproxy(true)).__gc = bad_fin
+collectgarbage()
+
+test:ok(match_err, 'correct error message in errfin handler')
+test:ok(match_line, 'correct source line in errfin handler')
+
+test:done(true)
-- 
2.53.0


      parent reply	other threads:[~2026-03-28 15:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-28 15:31 [Tarantool-patches] [PATCH luajit 0/2] VM events and finalizers in separate state Sergey Kaplun via Tarantool-patches
2026-03-28 15:31 ` [Tarantool-patches] [PATCH luajit 1/2] Run " Sergey Kaplun via Tarantool-patches
2026-03-28 15:31 ` Sergey Kaplun via Tarantool-patches [this message]

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=97ef9d8e0cbd59df6ef18a57dd44554f44c12129.1774711616.git.skaplun@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=sergeyb@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit 2/2] Fix VM event error handling for finalizers.' \
    /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