Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Kaplun via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Maxim Kokryashkin <m.kokryashkin@tarantool.org>,
	Sergey Bronnikov <sergeyb@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH luajit 1/4] FFI: Fix ffi.alignof() for reference types.
Date: Mon,  3 Jun 2024 17:33:59 +0300	[thread overview]
Message-ID: <75374f7bfe142c8133f42e96b6a6c55cfe0b7811.1717424008.git.skaplun@tarantool.org> (raw)
In-Reply-To: <cover.1717424008.git.skaplun@tarantool.org>

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


  reply	other threads:[~2024-06-03 14:38 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-03 14:33 [Tarantool-patches] [PATCH luajit 0/4] Fixes for ctypes with attributes Sergey Kaplun via Tarantool-patches
2024-06-03 14:33 ` Sergey Kaplun via Tarantool-patches [this message]
2024-06-11 15:40   ` [Tarantool-patches] [PATCH luajit 1/4] FFI: Fix ffi.alignof() for reference types Sergey Bronnikov via Tarantool-patches
2024-06-14 12:19   ` Maxim Kokryashkin via Tarantool-patches
2024-06-03 14:34 ` [Tarantool-patches] [PATCH luajit 2/4] FFI: Fix sizeof expression in C parser " Sergey Kaplun via Tarantool-patches
2024-06-11 15:42   ` Sergey Bronnikov via Tarantool-patches
2024-06-14 12:21   ` Maxim Kokryashkin via Tarantool-patches
2024-06-03 14:34 ` [Tarantool-patches] [PATCH luajit 3/4] FFI: Allow ffi.metatype() for typedefs with attributes Sergey Kaplun via Tarantool-patches
2024-06-11 15:48   ` Sergey Bronnikov via Tarantool-patches
2024-06-13  7:34     ` Sergey Kaplun via Tarantool-patches
2024-06-13 15:14       ` Sergey Bronnikov via Tarantool-patches
2024-06-14 12:39   ` Maxim Kokryashkin via Tarantool-patches
2024-06-03 14:34 ` [Tarantool-patches] [PATCH luajit 4/4] FFI: Fix ffi.metatype() for non-raw types Sergey Kaplun via Tarantool-patches
2024-06-11 15:50   ` Sergey Bronnikov via Tarantool-patches
2024-06-14 12:40   ` Maxim Kokryashkin via Tarantool-patches
2024-07-09  8:03 ` [Tarantool-patches] [PATCH luajit 0/4] Fixes for ctypes with attributes Sergey Kaplun via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=75374f7bfe142c8133f42e96b6a6c55cfe0b7811.1717424008.git.skaplun@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=m.kokryashkin@tarantool.org \
    --cc=sergeyb@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit 1/4] FFI: Fix ffi.alignof() for reference types.' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox