Tarantool development patches archive
 help / color / mirror / Atom feed
From: Maksim Kokryashkin via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: tarantool-patches@dev.tarantool.org, sergeyb@tarantool.org,
	skaplun@tarantool.org, m.kokryashkin@tarantool.org
Subject: [Tarantool-patches] [PATCH luajit v2] Fix recording of __concat metamethod.
Date: Fri,  2 Feb 2024 18:07:59 +0300	[thread overview]
Message-ID: <20240202150801.80361-1-max.kokryashkin@gmail.com> (raw)

From: Mike Pall <mike>

Reported by Elias Oelschner. Analyzed by XmiliaH.

(cherry-picked from commit 3ee3c9cfa988339f1bf3068530515e2a6fb179d2)

During the recording of `__concat` methametod, the `rec_mm_arith`
function overrides stack slots that are not restored for GC64
mode later after the call. This leads to a segmentation fault
later on. This patch fixes the issue by accounting for those
additional slots in the array used for restoring stack values.

Maxim Kokryashkin:
* added the description and the test for the problem

Part of tarantool/tarantool#9145
---
Changes in v2:
- Fixed comments as per review by Sergey Kaplun

Branch: https://github.com/tarantool/luajit/tree/fckxorg/lj-839-concat-recording
PR: https://github.com/tarantool/tarantool/pull/9597
Issues: https://github.com/tarantool/tarantool/issues/9145
https://github.com/luajit/luajit/issues/839

 src/lj_record.c                               |  2 +-
 .../lj-839-concat-recording.test.lua          | 27 +++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 test/tarantool-tests/lj-839-concat-recording.test.lua

diff --git a/src/lj_record.c b/src/lj_record.c
index a929b8aa..59549b03 100644
--- a/src/lj_record.c
+++ b/src/lj_record.c
@@ -1932,7 +1932,7 @@ static TRef rec_tnew(jit_State *J, uint32_t ah)
 static TRef rec_cat(jit_State *J, BCReg baseslot, BCReg topslot)
 {
   TRef *top = &J->base[topslot];
-  TValue savetv[5];
+  TValue savetv[5+LJ_FR2];
   BCReg s;
   RecordIndex ix;
   lj_assertJ(baseslot < topslot, "bad CAT arg");
diff --git a/test/tarantool-tests/lj-839-concat-recording.test.lua b/test/tarantool-tests/lj-839-concat-recording.test.lua
new file mode 100644
index 00000000..db82ffc0
--- /dev/null
+++ b/test/tarantool-tests/lj-839-concat-recording.test.lua
@@ -0,0 +1,27 @@
+local tap = require('tap')
+local test = tap.test('lj-839-concat-recording'):skipcond({
+  ['Test requires JIT enabled'] = not jit.status(),
+})
+test:plan(1)
+
+-- Test file to demonstrate LuaJIT overriding stack slots without
+-- restoration during the recording of the concat metamethod.
+-- See also: https://github.com/LuaJIT/LuaJIT/issues/839.
+
+-- Setup value with the `__concat` metamethod.
+local v1 = setmetatable({}, {
+  __concat = function(_, op2) return op2 end,
+});
+
+jit.opt.start('hotloop=1')
+local result
+for _ = 1, 4 do
+  -- `savetv` in `rec_cat` handles only up to 5 slots.
+  result = v1 .. '' .. '' .. '' .. '' .. 'canary'
+end
+
+-- Failure results in a LuaJIT assertion failure.
+-- The issue is GC64-specific, yet it is still being tested for
+-- other builds.
+test:is(result, 'canary', 'correct stack restoration')
+test:done(true)
--
2.39.3 (Apple Git-145)


             reply	other threads:[~2024-02-02 15:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-02 15:07 Maksim Kokryashkin via Tarantool-patches [this message]
2024-02-05 10:39 ` Sergey Kaplun via Tarantool-patches
2024-02-15 13:43 ` 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=20240202150801.80361-1-max.kokryashkin@gmail.com \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=m.kokryashkin@tarantool.org \
    --cc=max.kokryashkin@gmail.com \
    --cc=sergeyb@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit v2] Fix recording of __concat metamethod.' \
    /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