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 C4295C16598; Mon, 3 Jun 2024 17:38:50 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C4295C16598 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1717425530; bh=23siboexRJfR3vnGDhPjqHZIRMTDs9HM0CGnYd1tH4o=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=xlJ/MN/8vEjILFoyGyBvFTGnrU0f4tPUVdG844fVocCRLCz+fIq3Qp085Niw8JWcC l4ibdGDvkPS7H1TwS+J+Vr6pRv/QjjMxkj5h71jhMe0lzUQZtpJYNXHsrQ9adqedRa 4zVrocJyStZQgPa0xuNSoUPpd0HemAEWmf9J2hV8= Received: from smtp44.i.mail.ru (smtp44.i.mail.ru [95.163.41.82]) (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 E6178554FC7 for ; Mon, 3 Jun 2024 17:38:20 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org E6178554FC7 Received: by smtp44.i.mail.ru with esmtpa (envelope-from ) id 1sE8pH-0000000F2Sb-1d64; Mon, 03 Jun 2024 17:38:19 +0300 To: Maxim Kokryashkin , Sergey Bronnikov Date: Mon, 3 Jun 2024 17:33:59 +0300 Message-ID: <75374f7bfe142c8133f42e96b6a6c55cfe0b7811.1717424008.git.skaplun@tarantool.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD9F634E6C4921FF12D984BF2620C21AB1A79CC4FD98C04B60A00894C459B0CD1B92514CE7D131B1B526C36CA141FC789AED1D8B223F0BED39459C7DE1929D1CFAC09DF98A5C5F6EC85 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE76ABD3380F320B62CEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006375AFA801FF88E3EFD8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D82710C581C14DFF3A262E5AE63C0E75AC75936898DA08B9B2CC7F00164DA146DAFE8445B8C89999728AA50765F7900637CAEE156C82D3D7D9389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC81D471462564A2E19F6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA73AA81AA40904B5D9A18204E546F3947CE6BDB36F057AC83C040F9FF01DFDA4A84AD6D5ED66289B523666184CF4C3C14F6136E347CC761E07725E5C173C3A84C3C36FA3AD04261E41BA3038C0950A5D36B5C8C57E37DE458B330BD67F2E7D9AF16D1867E19FE14079C09775C1D3CA48CF27ED053E960B195E1DD303D21008E298D5E8D9A59859A8B652D31B9D28593E5175ECD9A6C639B01B78DA827A17800CE70012B0FBE38D23AE731C566533BA786AA5CC5B56E945C8DA X-C1DE0DAB: 0D63561A33F958A5BF819BBE5EAD5D765002B1117B3ED6966F6AA463EC111DC9E41E333F9D1358D5823CB91A9FED034534781492E4B8EEAD9CFA8CFAC159CE19C79554A2A72441328621D336A7BC284946AD531847A6065A535571D14F44ED41 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF45264AE1992146AF8A8ED61A2002AF96DF6F876F7F4227A63DB73EBDFC0BE4C21EB2640544212C895A25C67AD25077CD09DD7D35D4129028CAAF78F86E7D5762D8F4B8C1334D9754C226CC413062362A913E6812662D5F2A5EAB5682573093F7837F15F2B5E4A70B33F2C28C22F508233FCF178C6DD14203 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojxO1G5pUKYoEkOc/qZvCp/A== X-Mailru-Sender: 520A125C2F17F0B1A9638AD358559B596B57CF94B96AA75F411046492FDDF80604DEB6E76BDE61CBB7CBEF92542CD7C88B0A2698F12F5C9EC77752E0C033A69E86920BD37369036789A8C6A0E60D2BB63A5DB60FBEB33A8A0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit 1/4] FFI: Fix ffi.alignof() for reference types. 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 Eric Gouyer. (cherry picked from commit 36b2962d400db3981a7d7322f85c469240eb6f3b) According to C++ Standard (5.3.6.3) [1], the `alignof()` for the reference should be the same as for the referenced type. This patch fixes the behaviour by following the reference to get a child id for `ffi.alignof()`. Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#9924 [1]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4594.pdf#subsection.5.3.6 --- src/lib_ffi.c | 2 +- src/lj_ctype.c | 8 ++++++ src/lj_ctype.h | 1 + .../lj-861-ctype-attributes.test.lua | 26 +++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/tarantool-tests/lj-861-ctype-attributes.test.lua diff --git a/src/lib_ffi.c b/src/lib_ffi.c index e60e7b19..2bfca25a 100644 --- a/src/lib_ffi.c +++ b/src/lib_ffi.c @@ -639,7 +639,7 @@ LJLIB_CF(ffi_alignof) LJLIB_REC(ffi_xof FF_ffi_alignof) CTState *cts = ctype_cts(L); CTypeID id = ffi_checkctype(L, cts, NULL); CTSize sz = 0; - CTInfo info = lj_ctype_info(cts, id, &sz); + CTInfo info = lj_ctype_info_raw(cts, id, &sz); setintV(L->top-1, 1 << ctype_align(info)); return 1; } diff --git a/src/lj_ctype.c b/src/lj_ctype.c index 0874fa61..83042118 100644 --- a/src/lj_ctype.c +++ b/src/lj_ctype.c @@ -345,6 +345,14 @@ CTInfo lj_ctype_info(CTState *cts, CTypeID id, CTSize *szp) return qual; } +/* Ditto, but follow a reference. */ +CTInfo lj_ctype_info_raw(CTState *cts, CTypeID id, CTSize *szp) +{ + CType *ct = ctype_get(cts, id); + if (ctype_isref(ct->info)) id = ctype_cid(ct->info); + return lj_ctype_info(cts, id, szp); +} + /* Get ctype metamethod. */ cTValue *lj_ctype_meta(CTState *cts, CTypeID id, MMS mm) { diff --git a/src/lj_ctype.h b/src/lj_ctype.h index fce29409..8edbd561 100644 --- a/src/lj_ctype.h +++ b/src/lj_ctype.h @@ -467,6 +467,7 @@ LJ_FUNC CType *lj_ctype_rawref(CTState *cts, CTypeID id); LJ_FUNC CTSize lj_ctype_size(CTState *cts, CTypeID id); LJ_FUNC CTSize lj_ctype_vlsize(CTState *cts, CType *ct, CTSize nelem); LJ_FUNC CTInfo lj_ctype_info(CTState *cts, CTypeID id, CTSize *szp); +LJ_FUNC CTInfo lj_ctype_info_raw(CTState *cts, CTypeID id, CTSize *szp); LJ_FUNC cTValue *lj_ctype_meta(CTState *cts, CTypeID id, MMS mm); LJ_FUNC GCstr *lj_ctype_repr(lua_State *L, CTypeID id, GCstr *name); LJ_FUNC GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned); diff --git a/test/tarantool-tests/lj-861-ctype-attributes.test.lua b/test/tarantool-tests/lj-861-ctype-attributes.test.lua new file mode 100644 index 00000000..d88045a5 --- /dev/null +++ b/test/tarantool-tests/lj-861-ctype-attributes.test.lua @@ -0,0 +1,26 @@ +local tap = require('tap') + +-- Test file to demonstrate LuaJIT incorrect behaviour during +-- parsing and working with ctypes with attributes. +-- See also: https://github.com/LuaJIT/LuaJIT/issues/861. + +local test = tap.test('lj-861-ctype-attributes') +local ffi = require('ffi') + +test:plan(2) + +local EXPECTED_ALIGN = 4 + +ffi.cdef([[ +struct __attribute__((aligned($))) s_aligned { + uint8_t a; +}; +]], EXPECTED_ALIGN) + +local ref_align = ffi.alignof(ffi.typeof('struct s_aligned &')) + +test:is(ref_align, EXPECTED_ALIGN, 'the reference alignment is correct') +test:is(ref_align, ffi.alignof(ffi.typeof('struct s_aligned')), + 'the alignment of a reference is the same as for the referenced type') + +test:done(true) -- 2.45.1