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 76E10162784B; Mon, 16 Mar 2026 13:47:58 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 76E10162784B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1773658078; bh=fBgWnAp+6is5jn7k3wlYnqn8mLVFTYpdWqj0srndO0E=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=IHeCDEZmSxMSrOorhOBD9zt5H8et9cUbaapsRhRonaL/qZMNS47//0XH+c4g2kdo6 6N8f2+WfA+FPNqGfdOrgkSS54ZFzW+5XlgVkONaehKWEJ+QHr9PuTSsJCiwh5jrCPT bkogIx7nfrTuQhvo9e/e/T+jp6uYNjzqmHvqYHU0= Received: from send59.i.mail.ru (send59.i.mail.ru [89.221.237.154]) (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 C1A63162784A for ; Mon, 16 Mar 2026 13:47:56 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C1A63162784A Received: by exim-smtp-64cdfc6c8d-nbmhf with esmtpa (envelope-from ) id 1w25UJ-00000000VSw-3JZk; Mon, 16 Mar 2026 13:47:56 +0300 To: Sergey Bronnikov Date: Mon, 16 Mar 2026 13:48:53 +0300 Message-ID: <20260316104853.23901-1-skaplun@tarantool.org> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD91ABAE9865AC7DC88B3EFAEBFEBF496FD2C117F49B0EEF5A4182A05F53808504089CCFDF46433DC893DE06ABAFEAF67054609296ED9068BB2DDA5116E60259625810A56970F496ED6 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7FBCED7D376B82B5EEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637AC83A81C8FD4AD23D82A6BABE6F325AC2E85FA5F3EDFCBAA7353EFBB55337566BCD8AD057D84402C2ACBAD46143DB6C428FC964101A696E9E6668419ECFDB2FD389733CBF5DBD5E913377AFFFEAFD269176DF2183F8FC7C0ECC8AC47CD0EDEFF8941B15DA834481FCF19DD082D7633A0EF3E4896CB9E6436389733CBF5DBD5E9D5E8D9A59859A8B6D52CD31C43BF465FCC7F00164DA146DA6F5DAA56C3B73B237318B6A418E8EAB8D32BA5DBAC0009BE9E8FC8737B5C22493247F7D7586455C976E601842F6C81A12EF20D2F80756B5FB606B96278B59C4276E601842F6C81A127C277FBC8AE2E8B28A0C3D23A91D7003AA81AA40904B5D99C9F4D5AE37F343AD1F44FA8B9022EA23BBE47FD9DD3FB595F5C1EE8F4F765FC72CEEB2601E22B093A03B725D353964B0B7D0EA88DDEDAC722CA9DD8327EE4930A3850AC1BE2E7358CCB3ED2A1DE2304C4224003CC83647689D4C264860C145E X-C1DE0DAB: 0D63561A33F958A5ED350C93EBAC295B5002B1117B3ED696BE062D2BFF7F45DD92B673A2F5DDD7E7823CB91A9FED034534781492E4B8EEADB71243024C627CEAC79554A2A72441328621D336A7BC284946AD531847A6065A17B107DEF921CE79BDAD6C7F3747799A X-C8649E89: 1C3962B70DF3F0AD73CAD6646DEDE191716CD42B3DD1D34C77DD89D51EBB774225B6776AC983F447FC0B9F89525902EE6F57B2FD27647F25E66C117BDB76D659D75EB5E01CE25630002B6B389F20F0A0F53ACF4512852B9FD5912BE002FEE06A490DCA5DD65EBE50B8341EE9D5BE9A0AE38F940D171AF5E2F6FA5E386DC19D46352B72D9CBB430026536EB022892E5344C41F94D744909CECFA6C6B0C050A61A8CAF69B82BA93681CD72808BE417F3B9E0E7457915DAA85F X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXMZebaIdHP2ghjoIc/363UZI6Kf1ptIMVdbVVJCphTR/SlbLky7a1Jo= X-Mailru-Sender: 520A125C2F17F0B17094CDC02B85F11B21E8CEE1202C6F243DE06ABAFEAF67054609296ED9068BB2B7CBEF92542CD7C88B0A2698F12F5C9EC77752E0C033A69E86920BD37369036789A8C6A0E60D2BB63A5DB60FBEB33A8A0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit] FFI: Fix constructor index resolution in JIT compiler. 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 Reported by Vladimir Davydov and Sergey Kaplun. (cherry picked from commit 02e29995581cb568a784a9b996cd5e9139cd0737) This patch is a follow-up for the commit 6cee1333ef2b340e1b23e3051b9f433fc803d2ff ("FFI: Resolve metamethods for constructors, too."). It allows the use of metamethods of the objects to be called on their constructors. Unfortunately, the JIT part isn't updated and may cause inconsistencies since it is indexing the field of the structure referenced by the given CType. This patch makes the semantics of JIT the same as for the VM. Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#12134 --- Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-1441-record-constructor-metamethod Related issues: * https://github.com/LuaJIT/LuaJIT/issues/1441 * https://github.com/tarantool/tarantool/issues/12134 src/lj_crecord.c | 2 + ...441-record-constructor-metamethod.test.lua | 79 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 test/tarantool-tests/lj-1441-record-constructor-metamethod.test.lua diff --git a/src/lj_crecord.c b/src/lj_crecord.c index b016eaec..d486ee85 100644 --- a/src/lj_crecord.c +++ b/src/lj_crecord.c @@ -881,6 +881,8 @@ again: } J->base[0] = lj_ir_kint(J, (int32_t)fct->size); return; /* Interpreter will throw for newindex. */ + } else if (cd && cd->ctypeid == CTID_CTYPEID) { + /* Only resolve constants and metamethods for constructors. */ } else if (ctype_isbitfield(fct->info)) { if (ofs) ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs)); diff --git a/test/tarantool-tests/lj-1441-record-constructor-metamethod.test.lua b/test/tarantool-tests/lj-1441-record-constructor-metamethod.test.lua new file mode 100644 index 00000000..370e49e0 --- /dev/null +++ b/test/tarantool-tests/lj-1441-record-constructor-metamethod.test.lua @@ -0,0 +1,79 @@ +local tap = require('tap') + +-- The test file to demonstrate LuaJIT's incorrect recording of +-- the __index metamethod invocation on the cdata's constructor. +-- See also: https://github.com/LuaJIT/LuaJIT/issues/1441. + +local test = tap.test('lj-1441-record-constructor-metamethod'):skipcond({ + ['Test requires JIT enabled'] = not jit.status(), +}) + +test:plan(3) + +local ffi = require('ffi') + +ffi.cdef[[ + struct test_recursive {int a;}; + struct test_finite {int a;}; +]] + +local recursive_t = ffi.typeof('struct test_recursive') +local finite_t = ffi.typeof('struct test_finite') + +local MAGIC = 42 + +local function new_recursive() + return ffi.new(recursive_t, 0) +end + +local function new_finite() + return ffi.new(finite_t, 0) +end + +local function index_func_recursive(v) + -- Should raise an error (stack overflow). + return ffi.typeof(v).a +end + +-- Special object to invoke metamethod on the cdata. +local one_more_step = new_finite() + +local function index_func_finite(v) + if v == one_more_step then + -- XXX: Avoid tail-calls. + local x = ffi.typeof(v).a + return x + else + return MAGIC + end +end + +ffi.metatype(recursive_t, { + __index = index_func_recursive, +}) + +ffi.metatype(finite_t, { + __index = index_func_finite, +}) + +jit.opt.start('hotloop=1') + +-- Test the recursive call. Expect the stack overflow error. +local o_rec = new_recursive() +local result, errmsg +for _ = 1, 4 do + result, errmsg = pcall(index_func_recursive, o_rec) +end + +test:ok(not result, 'correct status for recursive call') +test:like(errmsg, 'stack overflow', 'correct error message for recursive call') + +-- Test the finite call. Expect the specific value. +local got +for _ = 1, 4 do + got = index_func_finite(one_more_step) +end + +test:is(got, MAGIC, 'correct result value on trace for finite call') + +test:done(true) -- 2.53.0