<HTML><BODY><div class="cl-rz0ucu2z76"><div>Hi, Sergey! Thanks for the patch!</div><div> </div><div>LGTM<br> </div><div data-signature-widget="container"><div data-signature-widget="content"><div>--<br>Best regards,</div><div>Evgeniy Temirgaleev</div></div></div><br><div class="mail-quote-collapse"><blockquote style="border-left:1px solid #0857A6;margin:10px;padding:0 0 0 10px"><span>From: Sergey Kaplun <<a href="mailto:skaplun@tarantool.org">skaplun@tarantool.org</a>><br>To: Sergey Bronnikov <<a href="mailto:sergeyb@tarantool.org">sergeyb@tarantool.org</a>>, Evgeniy Temirgaleev <<a href="mailto:e.temirgaleev@tarantool.org">e.temirgaleev@tarantool.org</a>><br>Cc: tarantool-patches@dev.tarantool.org, Sergey Kaplun <<a href="mailto:skaplun@tarantool.org">skaplun@tarantool.org</a>><br>Date: Monday, July 20, 2026 11:53 AM +03:00</span><br> <div><div id=""><div class="cl-kjh7vha46u"><div class="js-helper_mr_css_attr js-readmsg-msg_mr_css_attr"><div id="style_17845375910308841062_mr_css_attr"><div id="style_17845375910308841062_BODY_mr_css_attr">From: Mike Pall <mike><br><br>Thanks to Sergey Kaplun.<br><br>(cherry picked from commit d2c1327f57dc96f6ed8b11474f3bc5e09a9d227a)<br><br>If the error handler can't fit to the top of the Lua stack, the stack is<br>resized up to `LJ_STACK_MAXEX + 1 + 2 * LUA_MINSTACK` hoping that<br>`lj_state_relimitstack()` shrinks it back. In case when another error is<br>(gently) handled in the error handler (`pcall(error)` or failed<br>`load()`) the stack is shrunk to `LJ_STACK_MAX` leaving the rest of the<br>handler to be executed on the clipped stack, which leads to the<br>heap-overflow or a crash.<br><br>This patch fixes the issue by hardening the relimiting border to match<br>the extra space allocated before the stack overflow. Also, it fixes some<br>typos in the comments.<br><br>Sergey Kaplun:<br>* added the description and the test for the problem<br><br>Part of tarantool/tarantool#12880<br>---<br><br>The test is leading to the core dump on GC64 build.<br><br>Branch: <a href="https://github.com/tarantool/luajit/tree/skaplun/lj-1471-fix-stackov-relimit">https://github.com/tarantool/luajit/tree/skaplun/lj-1471-fix-stackov-relimit</a><br>Related issues:<br>* <a href="https://github.com/LuaJIT/LuaJIT/issues/1471">https://github.com/LuaJIT/LuaJIT/issues/1471</a><br>* <a href="https://github.com/tarantool/tarantool/issues/12880">https://github.com/tarantool/tarantool/issues/12880</a><br><br>src/lj_state.c | 17 ++++---<br>.../lj-1471-fix-stackov-relimit.test.lua | 48 +++++++++++++++++++<br>2 files changed, 59 insertions(+), 6 deletions(-)<br>create mode 100644 test/tarantool-tests/lj-1471-fix-stackov-relimit.test.lua<br><br>diff --git a/src/lj_state.c b/src/lj_state.c<br>index 2fa6a2f6..ea0f24f8 100644<br>--- a/src/lj_state.c<br>+++ b/src/lj_state.c<br>@@ -44,8 +44,9 @@<br>#define LJ_STACK_MAX LUAI_MAXSTACK /* Max. stack size. */<br>#define LJ_STACK_START (2*LJ_STACK_MIN) /* Starting stack size. */<br>#define LJ_STACK_MAXEX (LJ_STACK_MAX + 1 + LJ_STACK_EXTRA)<br>+#define LJ_STACK_ERREX (1 + 2*LJ_STACK_MIN) /* Extra for error handling. */<br><br>-/* Explanation of LJ_STACK_EXTRA:<br>+/* Explanation for LJ_STACK_EXTRA:<br>**<br>** Calls to metamethods store their arguments beyond the current top<br>** without checking for the stack limit. This avoids stack resizes which<br>@@ -58,6 +59,11 @@<br>** slots above top, but then mobj is always a function. So we can get by<br>** with 5 extra slots.<br>** LJ_FR2: We need 2 more slots for the frame PC and the continuation PC.<br>+**<br>+** Explanation for LJ_STACK_ERREX:<br>+**<br>+** The 1 is space for the error message, and 2 * LJ_STACK_MIN is for<br>+** the lj_state_checkstack() call in lj_err_run().<br>*/<br><br>/* Resize stack slots and adjust pointers in state. */<br>@@ -101,7 +107,8 @@ static void resizestack(lua_State *L, MSize n)<br>/* Relimit stack after error, in case the limit was overdrawn. */<br>void lj_state_relimitstack(lua_State *L)<br>{<br>- if (L->stacksize > LJ_STACK_MAXEX && L->top-tvref(L->stack) < LJ_STACK_MAX-1)<br>+ if (L->stacksize > LJ_STACK_MAXEX &&<br>+ L->top-tvref(L->stack) < LJ_STACK_MAX - 1 - LJ_STACK_ERREX)<br>resizestack(L, LJ_STACK_MAX);<br>}<br><br>@@ -147,11 +154,9 @@ void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need)<br>/* An error handler might want to inspect the stack overflow error, but<br>** will need some stack space to run in. We give it a stack size beyond<br>** the normal limit in order to do so, then rely on lj_state_relimitstack<br>- ** calls during unwinding to bring us back to a convential stack size.<br>- ** The + 1 is space for the error message, and 2 * LUA_MINSTACK is for<br>- ** the lj_state_checkstack() call in lj_err_run().<br>+ ** calls during unwinding to bring us back to a conventional stack size.<br>*/<br>- resizestack(L, LJ_STACK_MAX + 1 + 2 * LUA_MINSTACK);<br>+ resizestack(L, LJ_STACK_MAX + LJ_STACK_ERREX);<br>lj_err_stkov(L); /* May invoke an error handler. */<br>} else {<br>/* If we're here, then the stack overflow error handler is requesting<br>diff --git a/test/tarantool-tests/lj-1471-fix-stackov-relimit.test.lua b/test/tarantool-tests/lj-1471-fix-stackov-relimit.test.lua<br>new file mode 100644<br>index 00000000..6a10cc20<br>--- /dev/null<br>+++ b/test/tarantool-tests/lj-1471-fix-stackov-relimit.test.lua<br>@@ -0,0 +1,48 @@<br>+local tap = require('tap')<br>+<br>+-- The test file demonstrates a heap-overflow due to Lua stack<br>+-- out-of-bounds access after an incorrect stack shrinking after<br>+-- unwinding.<br>+-- See also <a href="https://github.com/LuaJIT/LuaJIT/issues/1471">https://github.com/LuaJIT/LuaJIT/issues/1471</a>.<br>+<br>+local test = tap.test('lj-1471-fix-stackov-relimit')<br>+<br>+test:plan(1)<br>+<br>+local function recursive_add(v1, v2)<br>+ -- Slot to eat the stack.<br>+ -- luacheck: no unused<br>+ local _<br>+ recursive_add(v1, v2)<br>+end<br>+<br>+local table_mt = {<br>+ __add = recursive_add,<br>+}<br>+<br>+local t1 = setmetatable({}, table_mt)<br>+local t2 = setmetatable({}, table_mt)<br>+<br>+coroutine.wrap(function()<br>+ xpcall(error, function()<br>+ pcall(error) -- Shrink stack back after unwinding.<br>+ -- XXX: Empirical amount of stack slots to observe the issue.<br>+ -- After the stack overflow, the `xpcall()` handler is invoked<br>+ -- again (see <a href="https://github.com/LuaJIT/LuaJIT/issues/1382">https://github.com/LuaJIT/LuaJIT/issues/1382</a>).<br>+ -- The stack is overallocated beyond its normal limit to<br>+ -- handle the error. After the `pcall(error)`, stack is<br>+ -- shrinking back to its normal size, leaving no space for<br>+ -- metamethod invocation. It is leaving to heap-overflow and a<br>+ -- crash.<br>+ -- luacheck: no unused<br>+ local _, _, _, _, _, _, _, _, _, _<br>+ local _, _, _, _, _, _, _, _, _, _<br>+ local _, _, _, _, _, _, _, _, _, _<br>+ local _<br>+ local _ = t1 + t2<br>+ end)<br>+end)()<br>+<br>+test:ok(true, 'no heap overflow after stack relimiting')<br>+<br>+test:done(true)<br>--<br>2.55.0</div></div></div></div></div></div></blockquote></div></div></BODY></HTML>