Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH luajit] Fix external C call stack check when using LUAJIT_MODE_WRAPCFUNC.
@ 2026-11-23  7:09 Sergey Kaplun via Tarantool-patches
  2026-11-23 15:17 ` Sergey Bronnikov via Tarantool-patches
  0 siblings, 1 reply; 2+ messages in thread
From: Sergey Kaplun via Tarantool-patches @ 2026-11-23  7:09 UTC (permalink / raw)
  To: Evgeniy Temirgaleev, Sergey Bronnikov; +Cc: tarantool-patches

From: Mike Pall <mike>

Thanks to Peter Cawley.

(cherry picked from commit 14e2917e7ab3d6f043d6604298bfa66470c6f47d)

Enabling and disabling wrapped C function mode [1] unconditionally
changes the bytecode to `BC_FUCC[W] 0 0`. The first operand (RA) is
unused for x86/x64 VMs, but other VMs use it like the minimum available
stack slots required for the function call (`LUA_MINSTACK` by default on
VM startup). Hence, these bytecode rewriting lead to the different
timing of the STACKOV error being raised (before the call or during the
invocation of the C function).

This patch makes the behaviour consistent across VMs by modifying only
the opcode without changing its operands. Hence the `LUA_MINSTACK`
value of the first operand is preserved.

[1]: https://luajit.org/ext_c_api.html#mode_wrapcfunc

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

Part of tarantool/tarantool#13442
---

Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-1047-fix-funccw-arm64
Related issues:
* https://github.com/LuaJIT/LuaJIT/issues/1047
* https://github.com/tarantool/tarantool/issues/13442

Side note: CI on macOS x86_64 is red due to runner issues.

 src/lj_dispatch.c                             |   4 +-
 .../lj-1047-fix-funccw-arm64.test.c           | 105 ++++++++++++++++++
 2 files changed, 107 insertions(+), 2 deletions(-)
 create mode 100644 test/tarantool-c-tests/lj-1047-fix-funccw-arm64.test.c

diff --git a/src/lj_dispatch.c b/src/lj_dispatch.c
index e5f876b1..f16e00c6 100644
--- a/src/lj_dispatch.c
+++ b/src/lj_dispatch.c
@@ -307,9 +307,9 @@ int luaJIT_setmode(lua_State *L, int idx, int mode)
       } else {
 	return 0;  /* Failed. */
       }
-      g->bc_cfunc_ext = BCINS_AD(BC_FUNCCW, 0, 0);
+      setbc_op(&g->bc_cfunc_ext, BC_FUNCCW);
     } else {
-      g->bc_cfunc_ext = BCINS_AD(BC_FUNCC, 0, 0);
+      setbc_op(&g->bc_cfunc_ext, BC_FUNCC);
     }
     break;
   default:
diff --git a/test/tarantool-c-tests/lj-1047-fix-funccw-arm64.test.c b/test/tarantool-c-tests/lj-1047-fix-funccw-arm64.test.c
new file mode 100644
index 00000000..0c9b8794
--- /dev/null
+++ b/test/tarantool-c-tests/lj-1047-fix-funccw-arm64.test.c
@@ -0,0 +1,105 @@
+#include "lua.h"
+#include "lauxlib.h"
+
+#include "test.h"
+#include "utils.h"
+
+/*
+ * The test file to demonstrate inconsistent LuaJIT's behaviour
+ * for wrapped C functions, after `LUAJIT_MODE_WRAPCFUNC` changed.
+ * The error should be raised before call to the C function, since
+ * the minimum available space for the Lua stack should be checked
+ * by the VM.
+ * See also: https://github.com/LuaJIT/LuaJIT/issues/1047.
+ */
+
+/* Simple proxy. */
+static int ct_wrap(lua_State *L, lua_CFunction f)
+{
+	return f(L);
+}
+
+static int is_called = 0;
+
+static int push_minstack(lua_State *L)
+{
+	is_called = 1;
+	for (int i = 0; i < LUA_MINSTACK; i++)
+		lua_pushinteger(L, i);
+	return LUA_MINSTACK;
+}
+
+static int fill_stack(lua_State *L, int limit)
+{
+	while(lua_gettop(L) < limit)
+		lua_pushinteger(L, 42);
+	return 0;
+}
+
+static int test_wrapon(void *test_state)
+{
+	/* Setup. */
+	lua_State *mainL = test_state;
+	lua_State *L = lua_newthread(mainL);
+	is_called = 0;
+
+	lua_pushlightuserdata(L, (void *)ct_wrap);
+	luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_ON);
+	lua_pop(L, 1);
+
+	/* 2 per frame in GC64 mode including the dummy frame. */
+	fill_stack(L, LUAI_MAXSTACK - 2 - 2);
+
+	lua_pushcfunction(L, push_minstack);
+	int status = lua_pcall(L, 0, 0, 0);
+	assert_true(status == LUA_ERRRUN);
+	assert_str_equal(lua_tostring(L, -1), "stack overflow");
+	assert_true(is_called == 0);
+
+	/* Tear down. */
+	luaJIT_setmode(L, 0, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_OFF);
+	lua_settop(mainL, 0);
+	return TEST_EXIT_SUCCESS;
+}
+
+static int test_wrapoff(void *test_state)
+{
+	/* Setup. */
+	lua_State *mainL = test_state;
+	lua_State *L = lua_newthread(mainL);
+	is_called = 0;
+
+	/*
+	 * Turn on and turn off should result with the correct
+	 * default behaviour.
+	 */
+	lua_pushlightuserdata(L, (void *)ct_wrap);
+	luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_ON);
+	lua_pop(L, 1);
+	luaJIT_setmode(L, 0, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_OFF);
+
+	/* 2 per frame in GC64 mode including the dummy frame. */
+	fill_stack(L, LUAI_MAXSTACK - 2 - 2);
+
+	lua_pushcfunction(L, push_minstack);
+	int status = lua_pcall(L, 0, 0, 0);
+	assert_true(status == LUA_ERRRUN);
+	assert_str_equal(lua_tostring(L, -1), "stack overflow");
+	assert_true(is_called == 0);
+
+	/* Tear down. */
+	lua_settop(mainL, 0);
+	return TEST_EXIT_SUCCESS;
+}
+
+int main(void)
+{
+	lua_State *L = utils_lua_init();
+	const struct test_unit tgroup[] = {
+		test_unit_def(test_wrapon),
+		test_unit_def(test_wrapoff)
+	};
+	const int test_result = test_run_group(tgroup, L);
+	utils_lua_close(L);
+	return test_result;
+}
-- 
2.56.0


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

* Re: [Tarantool-patches] [PATCH luajit] Fix external C call stack check when using LUAJIT_MODE_WRAPCFUNC.
  2026-11-23  7:09 [Tarantool-patches] [PATCH luajit] Fix external C call stack check when using LUAJIT_MODE_WRAPCFUNC Sergey Kaplun via Tarantool-patches
@ 2026-11-23 15:17 ` Sergey Bronnikov via Tarantool-patches
  0 siblings, 0 replies; 2+ messages in thread
From: Sergey Bronnikov via Tarantool-patches @ 2026-11-23 15:17 UTC (permalink / raw)
  To: Sergey Kaplun, Evgeniy Temirgaleev; +Cc: tarantool-patches

[-- Attachment #1: Type: text/plain, Size: 1732 bytes --]

Hi, Sergey!

Thanks for the patch! Please see the comment below.

Sergey

On 11/23/26 10:09, Sergey Kaplun wrote:
> From: Mike Pall <mike>
>
> Thanks to Peter Cawley.
>
> (cherry picked from commit 14e2917e7ab3d6f043d6604298bfa66470c6f47d)
>
> Enabling and disabling wrapped C function mode [1] unconditionally
> changes the bytecode to `BC_FUCC[W] 0 0`. The first operand (RA) is
> unused for x86/x64 VMs, but other VMs use it like the minimum available
> stack slots required for the function call (`LUA_MINSTACK` by default on
> VM startup). Hence, these bytecode rewriting lead to the different
> timing of the STACKOV error being raised (before the call or during the
> invocation of the C function).
>
> This patch makes the behaviour consistent across VMs by modifying only
> the opcode without changing its operands. Hence the `LUA_MINSTACK`
> value of the first operand is preserved.
>
> [1]:https://luajit.org/ext_c_api.html#mode_wrapcfunc
>
> Sergey Kaplun:
> * added the description and the test for the problem
>
> Part of tarantool/tarantool#13442
> ---
>
> Branch:https://github.com/tarantool/luajit/tree/skaplun/lj-1047-fix-funccw-arm64
> Related issues:
> *https://github.com/LuaJIT/LuaJIT/issues/1047
> *https://github.com/tarantool/tarantool/issues/13442
>
> Side note: CI on macOS x86_64 is red due to runner issues.
>
>   src/lj_dispatch.c                             |   4 +-
>   .../lj-1047-fix-funccw-arm64.test.c           | 105 ++++++++++++++++++
>   2 files changed, 107 insertions(+), 2 deletions(-)
>   create mode 100644 test/tarantool-c-tests/lj-1047-fix-funccw-arm64.test.c

It is not clear why the test (or patch) is arm64-specific.

Either explain in the commit message or remove suffix.

<snipped>

[-- Attachment #2: Type: text/html, Size: 2770 bytes --]

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

end of thread, other threads:[~2026-11-23 15:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23  7:09 [Tarantool-patches] [PATCH luajit] Fix external C call stack check when using LUAJIT_MODE_WRAPCFUNC Sergey Kaplun via Tarantool-patches
2026-11-23 15:17 ` Sergey Bronnikov 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