-
Notifications
You must be signed in to change notification settings - Fork 755
fix some bitfield size calculate bug #3215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Now, we can perceive non-starting bitfields and non-ending bitfields
r? @emilio |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Generally it looks good but I suspect this is specific to 64-bit architectures? Or am I missing something? In any case some more comments would be good. Thanks
For 32bit structures compiled across platforms, align_of is not accurate whether it is set to 8 or 4.
add bitfield pack offset bug
fix and add #3104 case this change add depended clang report offset |
@emilio Can you take some time to review this PR ? |
Fixes #3238 |
struct appHand_supportedAppProtocolRes {
appHand_responseCodeType ResponseCode; // enum with few variants, offset = 0x0
uint8_t SchemaID; // offset = 0x1 (why?, isn't enum i32?)
unsigned int SchemaID_isUsed:1; // offset = 0x2 ?, size of struct = 4
}; Generated with bindgen 0.72: #[repr(C)]
pub struct appHand_supportedAppProtocolRes {
pub ResponseCode: appHand_responseCodeType::Type,
pub SchemaID: u8,
pub _bitfield_align_1: [u8; 0],
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 1usize]>, // offset = 0x5 ?
pub __bindgen_padding_0: u16, // offset = 0x6, size of struct = 8
} Generated with this PR: #[repr(C)]
pub struct appHand_supportedAppProtocolRes {
pub ResponseCode: appHand_responseCodeType::Type,
pub SchemaID: u8,
pub _bitfield_align_1: [u8; 0],
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 3usize]>, // offset = 0x5, still
} Bitfield storage alignment is invalid EDIT: Wrong original offsets |
Yes In the C compiler, SchemaID_isUsed and SchemaID will be packaged into an int and accessed in 4 byte alignment. However, in bindgen, it is difficult to ensure this requirement under the current structure, which will cause some performance losses, but it is better than the wrong offset (the purpose of this PR is to fix the wrong offset/size, but at present, it does not destroy the alignment of the entire structure) |
This actually was a thing with different compilers used from bindgen and C sides... GCC fitted enum to 1 byte, but clang did not. |
Now, we can perceive non-starting bitfields and non-ending bitfields
Also, I modified the calculation method of align: only calculate alignment for bitfield from structure using
bitfield_width
The reason is that the code now no longer relies on
#[repr(C)]
to generate alignments, but always contains all bytesThe problem that is currently known is that the test case
bitfield-linux-32.hpp
was dropped#[repr(packed(4))]
. I don't know the reason, hope someone can help me