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 BF4C0BC5501; Tue, 11 Jun 2024 18:40:26 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org BF4C0BC5501 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1718120426; bh=BJ37q5Efcc06CDiv/PpdSHTffq4tnqXsNyfL+bo5bUc=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=e6B0kqvak+HCc1wFy57QiPMnOnR847nepMO5uTOdazCNHRf4YSRRCRFTI53uDQNxM m19zUlH4oRKN4ugvksISYRKnq4rzJxlrneOD8oSenN/77NLrvK1fXkzmYxNDKK5yln LPVjK5yEj+dpI/o4iN7O1KIePTRILmFW5QJ0IVsU= Received: from smtp49.i.mail.ru (smtp49.i.mail.ru [95.163.41.91]) (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 3D721BC5501 for ; Tue, 11 Jun 2024 18:40:25 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3D721BC5501 Received: by smtp49.i.mail.ru with esmtpa (envelope-from ) id 1sH3bk-0000000GiEk-1InW; Tue, 11 Jun 2024 18:40:24 +0300 Content-Type: multipart/alternative; boundary="------------mZpfT9SsyKxz01O09WbA9QsR" Message-ID: <427b72db-64db-4555-bf72-5da4c6dc410a@tarantool.org> Date: Tue, 11 Jun 2024 18:40:23 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: Sergey Kaplun , Maxim Kokryashkin Cc: tarantool-patches@dev.tarantool.org References: <75374f7bfe142c8133f42e96b6a6c55cfe0b7811.1717424008.git.skaplun@tarantool.org> In-Reply-To: <75374f7bfe142c8133f42e96b6a6c55cfe0b7811.1717424008.git.skaplun@tarantool.org> X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD9AC8CA0B4439200FAAB3485384346218550C915DCECBB078D00894C459B0CD1B961656AC0DFB7A5841AB432888F7DF02E89256BE7011D8F82577F1FB29389E95DF8D9167951636B18 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7EED5D2FAB4CEB1EDEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063781C5E355D70FD85B8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8439DF96D9D7F68F60769E61AE15FE47350D600CC65CE30B5CC7F00164DA146DAFE8445B8C89999728AA50765F790063783E00425F71A4181389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC8062BEEFFB5F8EA3EF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA73AA81AA40904B5D9A18204E546F3947C64E7220B7C5505929735652A29929C6C4AD6D5ED66289B523666184CF4C3C14F6136E347CC761E07725E5C173C3A84C3BB339968D8EBD1C6BA3038C0950A5D36B5C8C57E37DE458B330BD67F2E7D9AF16D1867E19FE14079C09775C1D3CA48CFED8438A78DFE0A9E1DD303D21008E298D5E8D9A59859A8B64854413538E1713F75ECD9A6C639B01B78DA827A17800CE7AA592860F4B06D71731C566533BA786AA5CC5B56E945C8DA X-C1DE0DAB: 0D63561A33F958A5E1ADE849F9AAC30C5002B1117B3ED696DC9E0F795ABC800A1A1B8FE1FED62FE8823CB91A9FED034534781492E4B8EEAD9CFA8CFAC159CE19BDAD6C7F3747799A X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF5BA554F40B32AC632DB1DC09C77CC8A188F159BA5E256146C190EA78B57D1006A54F2CFF063EE8C004A26A3878DC1330DC9D6868B6B8E8EE7623AA5790BBFC0E5573BE00906E239E5F4332CA8FE04980913E6812662D5F2AB9AF64DB4688768036DF5FE9C0001AF333F2C28C22F508233FCF178C6DD14203 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj7p5wIdCuWKMYCNUSrqMXOw== X-Mailru-Sender: C4F68CFF4024C8867DFDF7C7F25884584F1BDC4CEB870080CC811723C208B41748C164772AFDD0C2C45F183D5CA42588645D15D82EE4B272BD6E4642A116CA93524AA66B5ACBE6721EF430B9A63E2A504198E0F3ECE9B5443453F38A29522196 X-Mras: Ok Subject: Re: [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 Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" This is a multi-part message in MIME format. --------------mZpfT9SsyKxz01O09WbA9QsR Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Hi, Sergey thanks for the patch! LGTM On 03.06.2024 17:33, Sergey Kaplun wrote: > 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) --------------mZpfT9SsyKxz01O09WbA9QsR Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit

Hi, Sergey


thanks for the patch! LGTM


On 03.06.2024 17:33, Sergey Kaplun wrote:
From: Mike Pall <mike>

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)
--------------mZpfT9SsyKxz01O09WbA9QsR--