Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH luajit v2] Fix recording of __concat metamethod.
@ 2024-02-02 15:07 Maksim Kokryashkin via Tarantool-patches
  2024-02-05 10:39 ` Sergey Kaplun via Tarantool-patches
  2024-02-15 13:43 ` Igor Munkin via Tarantool-patches
  0 siblings, 2 replies; 3+ messages in thread
From: Maksim Kokryashkin via Tarantool-patches @ 2024-02-02 15:07 UTC (permalink / raw)
  To: tarantool-patches, sergeyb, skaplun, m.kokryashkin

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)


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-02-15 13:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-02 15:07 [Tarantool-patches] [PATCH luajit v2] Fix recording of __concat metamethod Maksim Kokryashkin via Tarantool-patches
2024-02-05 10:39 ` Sergey Kaplun via Tarantool-patches
2024-02-15 13:43 ` Igor Munkin via Tarantool-patches

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