From: Sergey Kaplun via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Evgeniy Temirgaleev <e.temirgaleev@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH luajit] FFI: Shrink container of packed bitfield.
Date: Wed, 29 Jul 2026 11:44:10 +0300 [thread overview]
Message-ID: <amm9Wr-HOe-lQRlH@root> (raw)
In-Reply-To: <1785254942.146802378@f510.i.mail.ru>
Hi, Evgeniy!
Thanks for the review!
Please, see my answers below.
On 28.07.26, Evgeniy Temirgaleev wrote:
> Hi, Sergey! Thanks for the patch!
>
> LGTM after updating the test. I tried the test and it passed without the fix.
You need to build LuaJIT with ASAN support, see the comment below.
>
> To check container bit size calculation we can add a field at bit 16. It’s byte offset with invalid calculation will be 0 and it will be 2 with the fixed calculation.
Nice idea, I like it.
>
> The output of the test with check printing:
>
> Without the fix:
>
> TAP version 13
> 1..2
> XX-R: 4
> ok - correct 0-initialization
> XX: 4
> XX-R: 4
> ok - bitfield set correctly
> # align=2 sz=4
> # bitfield ofs=0 bitofs=0 bitsize=1
> # bitfield15 ofs=0 bitofs=1 bitsize=15
> # bitfield16 ofs=0 bitofs=16 bitsize=1
>
> With the fix:
>
> TAP version 13
> 1..2
> XX-R: 2
> ok - correct 0-initialization
> XX: 2
> XX-R: 2
> ok - bitfield set correctly
> # align=2 sz=4
> # bitfield ofs=0 bitofs=0 bitsize=1
> # bitfield15 ofs=0 bitofs=1 bitsize=15
> # bitfield16 ofs=2 bitofs=0 bitsize=1
>
<snipped>
> diff --git a/test/tarantool-tests/lj-1451-ffi-packed-bitfield.test.lua b/test/tarantool-tests/lj-1451-ffi-packed-bitfield.test.lua
> index 07e53f76..bdd5c69e 100644
> --- a/test/tarantool-tests/lj-1451-ffi-packed-bitfield.test.lua
> +++ b/test/tarantool-tests/lj-1451-ffi-packed-bitfield.test.lua
> @@ -12,6 +12,8 @@ ffi.cdef[[
> #pragma pack(push, 2)
> typedef struct {
> unsigned int bitfield:1;
> + unsigned int bitfield15:15;
> + unsigned int bitfield16:1;
> } packed_struct;
> #pragma pack(pop)
> ]]
> @@ -25,4 +27,16 @@ test:is(packed.bitfield, 0, 'correct 0-initialization')
> packed.bitfield = 1
> test:is(packed.bitfield, 1, 'bitfield set correctly')
>
> +local ps = ffi.typeof('packed_struct')
> +test:diag('align=%d sz=%d', ffi.alignof(ps), ffi.sizeof(ps))
> +local o, bo, bs
> +o, bo, bs = ffi.offsetof(ps, 'bitfield')
> +test:diag('bitfield ofs=%d bitofs=%d bitsize=%d', o, bo, bs)
> +o, bo, bs = ffi.offsetof(ps, 'bitfield15')
> +test:diag('bitfield15 ofs=%d bitofs=%d bitsize=%d', o, bo, bs)
> +o, bo, bs = ffi.offsetof(ps, 'bitfield16')
> +test:diag('bitfield16 ofs=%d bitofs=%d bitsize=%d', o, bo, bs)
> +
> +test:fail()
> +
> test:done(true)
The test fails if run under ASAN. Build LuaJIT with its support like the
following:
| cmake -DLUAJIT_ENABLE_GC64=ON -DLUAJIT_USE_SYSMALLOC=ON -DLUAJIT_USE_ASAN=ON -DLUAJIT_USE_UBSAN=ON -DCMAKE_BUILD_TYPE=Debug -DLUA_USE_ASSERT=ON -DLUA_USE_APICHECK=ON . && make -j
But I like the idea to check memory layout of the given structure. See
the iterative patch below:
===================================================================
diff --git a/test/tarantool-tests/lj-1451-ffi-packed-bitfield.test.lua b/test/tarantool-tests/lj-1451-ffi-packed-bitfield.test.lua
index 07e53f76..76483b0d 100644
--- a/test/tarantool-tests/lj-1451-ffi-packed-bitfield.test.lua
+++ b/test/tarantool-tests/lj-1451-ffi-packed-bitfield.test.lua
@@ -10,13 +10,21 @@ local ffi = require('ffi')
ffi.cdef[[
#pragma pack(push, 2)
+
typedef struct {
unsigned int bitfield:1;
} packed_struct;
+
+typedef struct {
+ unsigned int bitfield0:1;
+ unsigned int bitfield15:15;
+ unsigned int bitfield16:1;
+} packed_struct2;
+
#pragma pack(pop)
]]
-test:plan(2)
+test:plan(5)
local packed = ffi.new('packed_struct')
@@ -25,4 +33,10 @@ test:is(packed.bitfield, 0, 'correct 0-initialization')
packed.bitfield = 1
test:is(packed.bitfield, 1, 'bitfield set correctly')
+-- Check correct structure layout.
+local byteoffset, bitpos, bitsize = ffi.offsetof('packed_struct2', 'bitfield16')
+test:is(byteoffset, 2, 'byteoffset is correct')
+test:is(bitpos, 0, 'bitpos is correct')
+test:is(bitsize, 1, 'bitsize is correct')
+
test:done(true)
===================================================================
Branch is force-pushed.
>
> --
> Best regards,
> Evgeniy Temirgaleev
>
> >
<snipped>
> > b/test/tarantool-tests/lj-1451-ffi-packed-bitfield.test.lua
> > new file mode 100644
> > index 00000000..07e53f76
> > --- /dev/null
> > +++ b/test/tarantool-tests/lj-1451-ffi-packed-bitfield.test.lua
> > @@ -0,0 +1,28 @@
> > +local tap = require('tap')
> > +
> > +-- Test file to demonstrate LuaJIT's incorrect behaviour of the
> > +-- `#pragma` pack directive for bitfields in structures.
> > +-- See also: https://github.com/LuaJIT/LuaJIT/issues/1451.
> > +
> > +local test = tap.test('lj-1451-ffi-packed-bitfield')
> > +
> > +local ffi = require('ffi')
> > +
> > +ffi.cdef[[
> > +#pragma pack(push, 2)
> > +typedef struct {
> > + unsigned int bitfield:1;
> > +} packed_struct;
> > +#pragma pack(pop)
> > +]]
> > +
> > +test:plan(2)
> > +
> > +local packed = ffi.new('packed_struct')
> > +
> > +test:is(packed.bitfield, 0, 'correct 0-initialization')
> > +
> > +packed.bitfield = 1
> > +test:is(packed.bitfield, 1, 'bitfield set correctly')
> > +
> > +test:done(true)
> > --
> > 2.55.0
> >
--
Best regards,
Sergey Kaplun
next prev parent reply other threads:[~2026-07-29 8:44 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-20 12:59 Sergey Kaplun via Tarantool-patches
2026-07-24 9:39 ` Sergey Bronnikov via Tarantool-patches
2026-07-28 16:09 ` Evgeniy Temirgaleev via Tarantool-patches
2026-07-29 8:44 ` Sergey Kaplun via Tarantool-patches [this message]
2026-07-29 15:50 ` Evgeniy Temirgaleev via Tarantool-patches
2026-07-29 17:03 ` Sergey Kaplun via Tarantool-patches
2026-07-30 7:18 ` Evgeniy Temirgaleev 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=amm9Wr-HOe-lQRlH@root \
--to=tarantool-patches@dev.tarantool.org \
--cc=e.temirgaleev@tarantool.org \
--cc=skaplun@tarantool.org \
--subject='Re: [Tarantool-patches] [PATCH luajit] FFI: Shrink container of packed bitfield.' \
/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