-
Notifications
You must be signed in to change notification settings - Fork 707
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect
#pragma pack(...)
and make pack(n)
where n > 1
opaque
This is a bandaid for #537. It does *not* fix the underlying issue, which requires `#[repr(packed = "N")]` support in Rust. However, it does make sure that we don't generate type definitions with the wrong layout, or fail our generated layout tests.
- Loading branch information
Showing
7 changed files
with
227 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* automatically generated by rust-bindgen */ | ||
|
||
|
||
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] | ||
|
||
|
||
|
||
/// This should not be opaque; we can see the attributes and can pack the | ||
/// struct. | ||
#[repr(C, packed)] | ||
#[derive(Debug, Default, Copy, Clone)] | ||
pub struct AlignedToOne { | ||
pub i: ::std::os::raw::c_int, | ||
} | ||
#[test] | ||
fn bindgen_test_layout_AlignedToOne() { | ||
assert_eq!( | ||
::std::mem::size_of::<AlignedToOne>(), | ||
4usize, | ||
concat!("Size of: ", stringify!(AlignedToOne)) | ||
); | ||
assert_eq!( | ||
::std::mem::align_of::<AlignedToOne>(), | ||
1usize, | ||
concat!("Alignment of ", stringify!(AlignedToOne)) | ||
); | ||
assert_eq!( | ||
unsafe { &(*(0 as *const AlignedToOne)).i as *const _ as usize }, | ||
0usize, | ||
concat!( | ||
"Alignment of field: ", | ||
stringify!(AlignedToOne), | ||
"::", | ||
stringify!(i) | ||
) | ||
); | ||
} | ||
/// This should be opaque because although we can see the attributes, Rust | ||
/// doesn't have `#[repr(packed = "N")]` yet. | ||
#[repr(C)] | ||
#[derive(Debug, Default, Copy, Clone)] | ||
pub struct AlignedToTwo { | ||
pub _bindgen_opaque_blob: [u16; 2usize], | ||
} | ||
#[test] | ||
fn bindgen_test_layout_AlignedToTwo() { | ||
assert_eq!( | ||
::std::mem::size_of::<AlignedToTwo>(), | ||
4usize, | ||
concat!("Size of: ", stringify!(AlignedToTwo)) | ||
); | ||
assert_eq!( | ||
::std::mem::align_of::<AlignedToTwo>(), | ||
2usize, | ||
concat!("Alignment of ", stringify!(AlignedToTwo)) | ||
); | ||
} | ||
/// This should not be opaque because although `libclang` doesn't give us the | ||
/// `#pragma pack(1)`, we can detect that alignment is 1 and add | ||
/// `#[repr(packed)]` to the struct ourselves. | ||
#[repr(C, packed)] | ||
#[derive(Debug, Default, Copy, Clone)] | ||
pub struct PackedToOne { | ||
pub x: ::std::os::raw::c_int, | ||
pub y: ::std::os::raw::c_int, | ||
} | ||
#[test] | ||
fn bindgen_test_layout_PackedToOne() { | ||
assert_eq!( | ||
::std::mem::size_of::<PackedToOne>(), | ||
8usize, | ||
concat!("Size of: ", stringify!(PackedToOne)) | ||
); | ||
assert_eq!( | ||
::std::mem::align_of::<PackedToOne>(), | ||
1usize, | ||
concat!("Alignment of ", stringify!(PackedToOne)) | ||
); | ||
assert_eq!( | ||
unsafe { &(*(0 as *const PackedToOne)).x as *const _ as usize }, | ||
0usize, | ||
concat!( | ||
"Alignment of field: ", | ||
stringify!(PackedToOne), | ||
"::", | ||
stringify!(x) | ||
) | ||
); | ||
assert_eq!( | ||
unsafe { &(*(0 as *const PackedToOne)).y as *const _ as usize }, | ||
4usize, | ||
concat!( | ||
"Alignment of field: ", | ||
stringify!(PackedToOne), | ||
"::", | ||
stringify!(y) | ||
) | ||
); | ||
} | ||
/// In this case, even if we can detect the weird alignment triggered by | ||
/// `#pragma pack(2)`, we can't do anything about it because Rust doesn't have | ||
/// `#[repr(packed = "N")]`. Therefore, we must make it opaque. | ||
#[repr(C)] | ||
#[derive(Debug, Default, Copy, Clone)] | ||
pub struct PackedToTwo { | ||
pub _bindgen_opaque_blob: [u16; 4usize], | ||
} | ||
#[test] | ||
fn bindgen_test_layout_PackedToTwo() { | ||
assert_eq!( | ||
::std::mem::size_of::<PackedToTwo>(), | ||
8usize, | ||
concat!("Size of: ", stringify!(PackedToTwo)) | ||
); | ||
assert_eq!( | ||
::std::mem::align_of::<PackedToTwo>(), | ||
2usize, | ||
concat!("Alignment of ", stringify!(PackedToTwo)) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.