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 88C2D6ECD0; Mon, 29 Jun 2026 16:19:57 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 88C2D6ECD0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1782739197; bh=NK2UiE1GxFsFz6obguSoE/pT/akwRl4r078vtgnR00g=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=ODHoEcquSlAHT4GZYXnqDJ4yyv5nT1BTEKRVd3y3tnAxDXMVMO82uzf3FDUeyWg2m /yOL//5/V+OECxwyBcIRqaGWH8j0FgdkBZ2x5OY3vPAC9wP/lZesykGyaL8LloB+o3 afymqA4Yh1JuFaNU66N/2lJVhkWnK93VX7zdiwyw= Received: from send34.i.mail.ru (send34.i.mail.ru [89.221.237.129]) (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 82A0D6ECD0 for ; Mon, 29 Jun 2026 16:19:55 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 82A0D6ECD0 Received: by exim-smtp-78b8b8c574-nfqm9 with esmtpa (envelope-from ) id 1weBty-000000003zv-1Tmj; Mon, 29 Jun 2026 16:19:54 +0300 To: Sergey Bronnikov , Evgeniy Temirgaleev Date: Mon, 29 Jun 2026 16:19:14 +0300 Message-ID: <20260629131914.791223-1-skaplun@tarantool.org> X-Mailer: git-send-email 2.54.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD918D6BB028DF8CB6161B7F8FDF1C2D81233DA19CC55AF894A182A05F53808504057126AE5409ED0C13DE06ABAFEAF67055FD56BE98DC4886D0174D9367ABA64F8985936BB30EC2273 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7CDB54ABCDA26BE78EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637AC83A81C8FD4AD23D82A6BABE6F325AC2E85FA5F3EDFCBAA7353EFBB553375667C07923B88CEC0ED9A79944E1D5CD2893F0970F587427DC39EE6ADA970223452389733CBF5DBD5E913377AFFFEAFD269176DF2183F8FC7C06030C3405640F6718941B15DA834481FCF19DD082D7633A0EF3E4896CB9E6436389733CBF5DBD5E9D5E8D9A59859A8B601F8F2FECC0250C8CC7F00164DA146DA6F5DAA56C3B73B237318B6A418E8EAB8D32BA5DBAC0009BE9E8FC8737B5C2249AEF4DCA51A11EB8276E601842F6C81A12EF20D2F80756B5FB606B96278B59C4276E601842F6C81A127C277FBC8AE2E8B18E7440D383AA13B3AA81AA40904B5D99C9F4D5AE37F343AD1F44FA8B9022EA23BBE47FD9DD3FB595F5C1EE8F4F765FC72CEEB2601E22B093A03B725D353964B0B7D0EA88DDEDAC722CA9DD8327EE4930A3850AC1BE2E735D2D576BCF940C736C4224003CC83647689D4C264860C145E X-C1DE0DAB: 0D63561A33F958A5F62D309F2DE7612B5002B1117B3ED69651FDC4D430A16CC7108A05421C070DB8823CB91A9FED034534781492E4B8EEADB71243024C627CEAC79554A2A72441328621D336A7BC284946AD531847A6065A535571D14F44ED41 X-C8649E89: 1C3962B70DF3F0AD73CAD6646DEDE191716CD42B3DD1D34C77DD89D51EBB774225B6776AC983F447FC0B9F89525902EE6F57B2FD27647F25E66C117BDB76D65980F563F8D8FB5E67FB51D4CD1B30D224F7DC02A41FA094D1E86FEE2D729A6FB15AD74CEF77E5A2FAB8341EE9D5BE9A0A5A1D0BE24DE059211B59B2A1A7233E9B40A7261F47763024C7CEAA0681F5848F4C41F94D744909CECFA6C6B0C050A61A8CAF69B82BA93681CD72808BE417F3B9E0E7457915DAA85F X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXMZebaIdHP2ghjoIc/363UZI6Kf1ptIMVRSZSJkMhZtMbPEBo3ltbqI= X-Mailru-Sender: 520A125C2F17F0B17094CDC02B85F11BE0792A7B2959E02E3DE06ABAFEAF67055FD56BE98DC4886DB7CBEF92542CD7C88B0A2698F12F5C9EC77752E0C033A69E86920BD37369036789A8C6A0E60D2BB63A5DB60FBEB33A8A0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit] FFI/MacOS: Fix calling convention for on-stack varargs. 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 Sergey Kaplun. (cherry picked from commit a2bde60819d83e6f75130ac2c93ee4b3c7615800) This commit fixes the regression introduced by the commit 82ca6844234901366c043cd103f6eae345af753c ("FFI/MacOS: Fix calling convention for enums."). The `isva` flag is set to 0 even for vararg functions. Thus, arguments on the stack may be aligned incorrectly, leading to the crash. This patch fixes the behaviour by adjusting the flag value instead of resetting it. In addition to the original test, various tests have been added to cover the behaviour of the vararg FFI calls. Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#12480 --- Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-1455-macos-arm64-vararg-regression Related issues: * https://github.com/LuaJIT/LuaJIT/issues/1455 * https://github.com/neovim/neovim/pull/39994 * https://github.com/tarantool/tarantool/issues/12480 src/lj_ccall.c | 2 +- ...455-macos-arm64-vararg-regression.test.lua | 112 ++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 test/tarantool-tests/lj-1455-macos-arm64-vararg-regression.test.lua diff --git a/src/lj_ccall.c b/src/lj_ccall.c index 7c3ec1e5..ed692318 100644 --- a/src/lj_ccall.c +++ b/src/lj_ccall.c @@ -1082,7 +1082,7 @@ static int ccall_set_args(lua_State *L, CTState *cts, CType *ct, if (CCALL_ALIGN_STACKARG) { /* Align argument on stack. */ MSize align = (1u << ctype_align(ccall_struct_align(cts, d))) - 1; #if LJ_TARGET_ARM64 && LJ_TARGET_OSX - isva = ctype_isstruct(d->info); + isva |= ctype_isstruct(d->info); #endif if (rp || (CCALL_PACK_STACKARG && isva && align < CTSIZE_PTR-1)) align = CTSIZE_PTR-1; diff --git a/test/tarantool-tests/lj-1455-macos-arm64-vararg-regression.test.lua b/test/tarantool-tests/lj-1455-macos-arm64-vararg-regression.test.lua new file mode 100644 index 00000000..a6670cce --- /dev/null +++ b/test/tarantool-tests/lj-1455-macos-arm64-vararg-regression.test.lua @@ -0,0 +1,112 @@ +local ffi = require('ffi') +local tap = require('tap') + +-- The test file to test various FFI C vararg calls. +-- luacheck: push no max_comment_line_length +-- Originated from: https://github.com/neovim/neovim/blob/a5aa62e37b82214a1d4d1e0a54d193b155fb340c/test/unit/strings_spec.lua +-- luacheck: pop +-- See also: https://github.com/LuaJIT/LuaJIT/issues/1455. + +local test = tap.test('lj-1455-macos-arm64-vararg-regression') + +test:plan(45) + +ffi.cdef('int sprintf(char *str, const char *format, ...);') + +local buf = ffi.new('char[64]') + +local function t(expected, fmt, ...) + local args = {...} + local ctx = string.format('sprintf(buf, "%s"', fmt) + for _, x in ipairs(args) do + ctx = ctx .. ', ' .. tostring(x) + end + ctx = ctx .. string.format(') = %s', expected) + + test:test(ctx, function(subtest, ...) + subtest:plan(2) + subtest:is(ffi.C.sprintf(buf, fmt, ...), #expected, + ctx .. ' - return status') + subtest:is(ffi.string(buf), expected, ctx .. ' - result string') + end, ...) +end + +local function i(n) + return ffi.cast('int', n) +end + +local function l(n) + return ffi.cast('long', n) +end + +local function ll(n) + return ffi.cast('long long', n) +end + +local function z(n) + return ffi.cast('ptrdiff_t', n) +end + +local function u(n) + return ffi.cast('unsigned', n) +end + +local function ul(n) + return ffi.cast('unsigned long', n) +end + +local function ull(n) + return ffi.cast('unsigned long long', n) +end + +local function uz(n) + return ffi.cast('size_t', n) +end + +t('1234567', '%d', i(1234567)) +t('1234567', '%ld', l(1234567)) +t(' 1234567', '%9ld', l(1234567)) +t('1234567 ', '%-9ld', l(1234567)) +t('deadbeef', '%x', u(0xdeadbeef)) +t('one two', '%s %s', 'one', 'two') +t('1.234000', '%f', 1.234) +t('1.234000e+00', '%e', 1.234) +t('inf', '%f', 1.0 / 0.0) +t('-inf', '%f', -1.0 / 0.0) +t('-0.000000', '%f', tonumber('-0.0')) +t('%%%', '%%%%%%') +t('0x87654321', '%p', ffi.cast('char *', 0x87654321)) +t('0x0087654321', '%012p', ffi.cast('char *', 0x87654321)) +t('1234567 ', '%1$*2$ld', l(1234567), i(-9)) +t('1234567 ', '%1$*2$.*3$ld', l(1234567), i(-9), i(5)) +t('1234567 ', '%1$*3$.*2$ld', l(1234567), i(5), i(-9)) +t('1234567 ', '%3$*1$.*2$ld', i(-9), i(5), l(1234567)) +t('1234567', '%1$ld', l(1234567)) +t(' 1234567', '%1$*2$ld', l(1234567), i(9)) +t('9 12345 7654321', '%2$ld %1$d %3$lu', i(12345), l(9), ul(7654321)) +t('9 1234567 7654321', '%2$d %1$ld %3$lu', l(1234567), i(9), ul(7654321)) +t('9 1234567 7654321', '%2$d %1$lld %3$lu', ll(1234567), i(9), ul(7654321)) +t('9 12345 7654321', '%2$ld %1$u %3$lu', u(12345), l(9), ul(7654321)) +t('9 1234567 7654321', '%2$d %1$lu %3$lu', ul(1234567), i(9), ul(7654321)) +t('9 1234567 7654321', '%2$d %1$llu %3$lu', ull(1234567), i(9), ul(7654321)) +t('9 deadbeef 7654321', '%2$d %1$x %3$lu', u(0xdeadbeef), i(9), ul(7654321)) +t('9 c 7654321', '%2$ld %1$c %3$lu', i(('c'):byte()), l(9), ul(7654321)) +t('9 hi 7654321', '%2$ld %1$s %3$lu', 'hi', l(9), ul(7654321)) +t('9 0.000000e+00 7654321', '%2$ld %1$e %3$lu', 0.0, l(9), ul(7654321)) +t('two one two', '%2$s %1$s %2$s', 'one', 'two', 'three') +t('three one two', '%3$s %1$s %2$s', 'one', 'two', 'three') +t('1234567', '%1$d', i(1234567)) +t('deadbeef', '%1$x', u(0xdeadbeef)) +t('one two', '%1$s %2$s', 'one', 'two') +t('two one', '%2$s %1$s', 'one', 'two') +t('1.234000', '%1$f', 1.234) +t('1.234000e+00', '%1$e', 1.234) +t('inf', '%1$f', 1.0 / 0.0) +t('-inf', '%1$f', -1.0 / 0.0) +t('-0.000000', '%1$f', tonumber('-0.0')) +t('-1234567 -7654321', '%zd %zd', z(-1234567), z(-7654321)) +t('-7654321 -1234567', '%2$zd %1$zd', z(-1234567), z(-7654321)) +t('1234567 7654321', '%zu %zu', uz(1234567), uz(7654321)) +t('7654321 1234567', '%2$zu %1$zu', uz(1234567), uz(7654321)) + +test:done(true) -- 2.54.0