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 54BB46E236; Wed, 29 Jul 2026 11:44:28 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 54BB46E236 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1785314668; bh=vQcq7rTtgGQeBXC4K1L9JMSP7BkBj7vw+d5CT3kv5Pw=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=coh4GCr8yON2K7NuxxxEJiS02A+8g/ZXxqmUdNHrkXoPL4jFULU2ihuLgf9eQXflF rpgo/OCoAhM1wbIBPPtDs/5Ik8wY5YsR/i8ruw6zK4y6BHe3YIYKbWKphvyJVKvadi uQiGdyuo3BgU/1HmPg+O9tE3aDvxWw2GV2HZ7ngU= Received: from send58.i.mail.ru (send58.i.mail.ru [89.221.237.153]) (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 C64BA6E236 for ; Wed, 29 Jul 2026 11:44:26 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C64BA6E236 Received: by exim-smtp-6c76488b9f-bmlgn with esmtpa (envelope-from ) id 1woztp-00000000DR9-2Xab; Wed, 29 Jul 2026 11:44:26 +0300 Date: Wed, 29 Jul 2026 11:44:10 +0300 To: Evgeniy Temirgaleev Message-ID: References: <20260720125943.2531443-1-skaplun@tarantool.org> <1785254942.146802378@f510.i.mail.ru> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1785254942.146802378@f510.i.mail.ru> X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 78E4E2B564C1792B X-77F55803: 4F1203BC0FB41BD999EC7E29E32078B04FFDCCE722713752DFD7AF9B0DD2A2E5182A05F538085040AD7B48901CB4C2DF3DE06ABAFEAF6705BC36AE0187400C8FB7689EA1EBEAAE42294AE337B47F4242 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE714773D61402E8DE9EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637AC83A81C8FD4AD23D82A6BABE6F325AC2E85FA5F3EDFCBAA7353EFBB553375666E0AFCC7CBCEC7C79BB81394C79084458785BF0A96030BED7B64B6D25409F645389733CBF5DBD5E913377AFFFEAFD269176DF2183F8FC7C045A75973B56231AD8941B15DA834481FCF19DD082D7633A0EF3E4896CB9E6436389733CBF5DBD5E9D5E8D9A59859A8B61638054B7D09EC08CC7F00164DA146DA6F5DAA56C3B73B237318B6A418E8EAB8D32BA5DBAC0009BE9E8FC8737B5C2249378990534548B4FE76E601842F6C81A12EF20D2F80756B5FB606B96278B59C4276E601842F6C81A127C277FBC8AE2E8BC45053385E972D003AA81AA40904B5D99C9F4D5AE37F343AD1F44FA8B9022EA23BBE47FD9DD3FB595F5C1EE8F4F765FC72CEEB2601E22B093A03B725D353964B0B7D0EA88DDEDAC722CA9DD8327EE4930A3850AC1BE2E73525A4AB119743A3B3C4224003CC83647689D4C264860C145E X-C1DE0DAB: 0D63561A33F958A54B452B201DC9CDD45002B1117B3ED6961744CBE84A8D317A361FAC1196A180DE823CB91A9FED034534781492E4B8EEAD5DF1C2DF01CE7211BDAD6C7F3747799A X-C8649E89: 1C3962B70DF3F0AD73CAD6646DEDE191716CD42B3DD1D34CAB70F9BE574AE9C625B6776AC983F447FC0B9F89525902EE6F57B2FD27647F25E66C117BDB76D6594E54C4BD8EF2FC9DA201ED84D6A317EE5CA682B4D4EB06126AB2128EB9D1AAAD829FF18825DF600FB8341EE9D5BE9A0AA7BDD54E7903C5DF3A8192B273ACFA2D9C1921AB9BAE34E86536EB022892E5344C41F94D744909CECFA6C6B0C050A61A8CAF69B82BA93681CD72808BE417F3B9E0E7457915DAA85F X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXMZebaIdHP2ghjoIc/363UZI6Kf1ptIMVczkDsfooUjfwHBh1DNFSlQ= X-Mailru-Sender: 520A125C2F17F0B17094CDC02B85F11B06DFF2C96115AE6E3DE06ABAFEAF6705BC36AE0187400C8FB7CBEF92542CD7C88B0A2698F12F5C9EC77752E0C033A69E86920BD37369036789A8C6A0E60D2BB63A5DB60FBEB33A8A0DA7A0AF5A3A8387 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit] FFI: Shrink container of packed bitfield. 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" 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 > > 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 > > > > > 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