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 2/4] FFI: Fix sizeof expression in C parser for reference types.
Date: Mon,  3 Jun 2024 17:34:00 +0300	[thread overview]
Message-ID: <f5bbc531e23670edbbed987f7f754ade0ae3c23f.1717424008.git.skaplun@tarantool.org> (raw)
In-Reply-To: <cover.1717424008.git.skaplun@tarantool.org>

From: Mike Pall <mike>

(cherry picked from commit 899093a9e0fa5b16f27016381ef4b15529dadff2)

According to C++ Standard, the `alignof()` (5.3.6.3) [1] and `sizeof()`
(5.3.3.2) [2] 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 `alignof()` and `sizeof()` while parsing C definitions via
`ffi.cdef()`.

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
[2]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4594.pdf#subsection.5.3.3
---
 src/lj_cparse.c                                  |  2 +-
 .../lj-861-ctype-attributes.test.lua             | 16 +++++++++++++++-
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/src/lj_cparse.c b/src/lj_cparse.c
index 01deb3bf..8506d719 100644
--- a/src/lj_cparse.c
+++ b/src/lj_cparse.c
@@ -468,7 +468,7 @@ static void cp_expr_sizeof(CPState *cp, CPValue *k, int wantsz)
   } else {
     cp_expr_unary(cp, k);
   }
-  info = lj_ctype_info(cp->cts, k->id, &sz);
+  info = lj_ctype_info_raw(cp->cts, k->id, &sz);
   if (wantsz) {
     if (sz != CTSIZE_INVALID)
       k->u32 = sz;
diff --git a/test/tarantool-tests/lj-861-ctype-attributes.test.lua b/test/tarantool-tests/lj-861-ctype-attributes.test.lua
index d88045a5..e8b29d67 100644
--- a/test/tarantool-tests/lj-861-ctype-attributes.test.lua
+++ b/test/tarantool-tests/lj-861-ctype-attributes.test.lua
@@ -7,7 +7,7 @@ local tap = require('tap')
 local test = tap.test('lj-861-ctype-attributes')
 local ffi = require('ffi')
 
-test:plan(2)
+test:plan(4)
 
 local EXPECTED_ALIGN = 4
 
@@ -15,6 +15,15 @@ ffi.cdef([[
 struct __attribute__((aligned($))) s_aligned {
   uint8_t a;
 };
+
+struct test_parsing_sizeof {
+  char a[sizeof(struct s_aligned &)];
+};
+
+struct test_parsing_alignof {
+  char a[__alignof__(struct s_aligned &)];
+};
+
 ]], EXPECTED_ALIGN)
 
 local ref_align = ffi.alignof(ffi.typeof('struct s_aligned &'))
@@ -23,4 +32,9 @@ 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:is(ffi.sizeof('struct test_parsing_sizeof'), EXPECTED_ALIGN,
+        'correct sizeof during C parsing')
+test:is(ffi.sizeof('struct test_parsing_alignof'), EXPECTED_ALIGN,
+        'correct alignof during C parsing')
+
 test:done(true)
-- 
2.45.1


  parent reply	other threads:[~2024-06-03 14:39 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 ` [Tarantool-patches] [PATCH luajit 1/4] FFI: Fix ffi.alignof() for reference types Sergey Kaplun via Tarantool-patches
2024-06-11 15:40   ` Sergey Bronnikov via Tarantool-patches
2024-06-14 12:19   ` Maxim Kokryashkin via Tarantool-patches
2024-06-03 14:34 ` Sergey Kaplun via Tarantool-patches [this message]
2024-06-11 15:42   ` [Tarantool-patches] [PATCH luajit 2/4] FFI: Fix sizeof expression in C parser " 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=f5bbc531e23670edbbed987f7f754ade0ae3c23f.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 2/4] FFI: Fix sizeof expression in C parser 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