From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 4B95A6ECE2; Mon, 23 Nov 2026 10:07:01 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4B95A6ECE2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1795417621; bh=W/xN1Bo6UuVrtSL6MrRarCFvc+xlDu7pGkybpeMeqvY=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=w5fMZ3mXUKDrP2730f1vu1f74EgNTgDZRjiwDpQoXOhuPiLAcu64gajf0eb8oWQXX hhLrQ6kVynpmsXPsEsXxpYGWH5uwlrhhGR+EESqnUIzHE15Ffb8ZOFO8zuaY53PUhX dRcwWJqBE3lD3KgpIVZzXpXa2FfnX9Lyh77Y40aM= Received: from send151.i.mail.ru (send151.i.mail.ru [89.221.237.246]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 10ACD6ECE2 for ; Mon, 23 Nov 2026 10:06:59 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 10ACD6ECE2 Received: by exim-smtp-canary-30-7cf57c478c-h96hx with esmtpa (envelope-from ) id 1xVO8f-00000000Rnh-3Gql; Mon, 23 Nov 2026 10:06:58 +0300 To: Evgeniy Temirgaleev , Sergey Bronnikov Date: Mon, 23 Nov 2026 10:09:10 +0300 Message-ID: <20261123070910.1354298-1-skaplun@tarantool.org> X-Mailer: git-send-email 2.56.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD9749DDC91F62168F90E401E2CCDC86F6A9286E8ED2F958BEA182A05F5380850408946039C51EC45603DE06ABAFEAF6705F8ADD30D30CE17E71EE5845F2FE4BF59657EB3FB719050AD X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7BAE5222749FC9020C2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6759CC434672EE6371C2A783ECEC0211ADC4224003CC836476D5A39DEEDB180909611E41BBFE2FEB2B61257C7AFB3F37071E65F5E030FFB41796BD1CCE66509CC3E0EB486575A5ED569FA2833FD35BB23D27C277FBC8AE2E8B55D5BE2F85BDEC5FA471835C12D1D977C4224003CC836476EB9C41850244470149FD398EE364050F0AC5B80A05675ACD3D5BA627BF9F2FCFB3661434B16C20ACC84D3B47A649675FE827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8B8E48D8FAA4D20A9F75ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: 0D63561A33F958A554978B314F5B29E85002B1117B3ED6963789584867B268FCD57BAD45EC4C5DE1823CB91A9FED034534781492E4B8EEAD049D0ED29243A754C79554A2A72441328621D336A7BC284946AD531847A6065A535571D14F44ED41 X-C8649E89: 1C3962B70DF3F0AD73CAD6646DEDE191716CD42B3DD1D34C77DD89D51EBB774225B6776AC983F447FC0B9F89525902EE6F57B2FD27647F25E66C117BDB76D659622203155BEF370253ABD89D21A09A6600B7DC3F27562927AB4A5D332E062FC3B7F79F309A4BF481B8341EE9D5BE9A0AB70CBDF8B33C656E4A1060A1218B8216730A2BCC2D80D9BEC7CEAA0681F5848F4C41F94D744909CECFA6C6B0C050A61A8CAF69B82BA93681CD72808BE417F3B9E0E7457915DAA85F X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXOcIETfglQORZ0zpDET4Zrk3igikrdHlWAIc83UjhR8UAP+qXqN/L60= X-Mailru-Sender: 520A125C2F17F0B17094CDC02B85F11B3BA69FAC870849FF3DE06ABAFEAF6705F8ADD30D30CE17E7B7CBEF92542CD7C88B0A2698F12F5C9EC77752E0C033A69E86920BD37369036789A8C6A0E60D2BB63A5DB60FBEB33A8A0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit] Fix external C call stack check when using LUAJIT_MODE_WRAPCFUNC. X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Kaplun via Tarantool-patches Reply-To: Sergey Kaplun Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Mike Pall 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