summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEmilio Cobos Álvarez <emilio@crisal.io>2020-11-28 01:33:32 +0100
committerEmilio Cobos Álvarez <emilio@crisal.io>2020-11-28 03:14:51 +0100
commit19142ac6b3d8ee4fc5686d6f30b77e660026e528 (patch)
tree7be336cb2bfd34e6a8c844b868efb4332aba9c4f /tests
parent6a5726eac514b49ec8a9f8360ed5d0d73da9feb7 (diff)
struct_layout: Fix field offset computation for packed(n) structs.
This can cause unnecessary padding to be computed otherwise at the end of the struct. With repr(packed(n)), a field can have padding to adjacent fields as long as its alignment is less than n. So reuse the code we have to align to a field layout, aligning to the struct layout instead. Fixes #1934
Diffstat (limited to 'tests')
-rw-r--r--tests/expectations/tests/divide-by-zero-in-struct-layout.rs1
-rw-r--r--tests/expectations/tests/packed-n-with-padding.rs48
-rw-r--r--tests/headers/packed-n-with-padding.h8
3 files changed, 56 insertions, 1 deletions
diff --git a/tests/expectations/tests/divide-by-zero-in-struct-layout.rs b/tests/expectations/tests/divide-by-zero-in-struct-layout.rs
index 1366548e..1484719b 100644
--- a/tests/expectations/tests/divide-by-zero-in-struct-layout.rs
+++ b/tests/expectations/tests/divide-by-zero-in-struct-layout.rs
@@ -130,7 +130,6 @@ impl WithBitfieldAndAttrPacked {
pub struct WithBitfieldAndPacked {
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 0usize], u8>,
pub a: ::std::os::raw::c_uint,
- pub __bindgen_padding_0: u8,
}
impl WithBitfieldAndPacked {
#[inline]
diff --git a/tests/expectations/tests/packed-n-with-padding.rs b/tests/expectations/tests/packed-n-with-padding.rs
new file mode 100644
index 00000000..13cb0306
--- /dev/null
+++ b/tests/expectations/tests/packed-n-with-padding.rs
@@ -0,0 +1,48 @@
+#![allow(
+ dead_code,
+ non_snake_case,
+ non_camel_case_types,
+ non_upper_case_globals
+)]
+
+#[repr(C, packed(2))]
+#[derive(Debug, Default, Copy, Clone)]
+pub struct Packed {
+ pub a: ::std::os::raw::c_char,
+ pub b: ::std::os::raw::c_short,
+ pub c: ::std::os::raw::c_char,
+ pub d: ::std::os::raw::c_int,
+}
+#[test]
+fn bindgen_test_layout_Packed() {
+ assert_eq!(
+ ::std::mem::size_of::<Packed>(),
+ 10usize,
+ concat!("Size of: ", stringify!(Packed))
+ );
+ assert_eq!(
+ ::std::mem::align_of::<Packed>(),
+ 2usize,
+ concat!("Alignment of ", stringify!(Packed))
+ );
+ assert_eq!(
+ unsafe { &(*(::std::ptr::null::<Packed>())).a as *const _ as usize },
+ 0usize,
+ concat!("Offset of field: ", stringify!(Packed), "::", stringify!(a))
+ );
+ assert_eq!(
+ unsafe { &(*(::std::ptr::null::<Packed>())).b as *const _ as usize },
+ 2usize,
+ concat!("Offset of field: ", stringify!(Packed), "::", stringify!(b))
+ );
+ assert_eq!(
+ unsafe { &(*(::std::ptr::null::<Packed>())).c as *const _ as usize },
+ 4usize,
+ concat!("Offset of field: ", stringify!(Packed), "::", stringify!(c))
+ );
+ assert_eq!(
+ unsafe { &(*(::std::ptr::null::<Packed>())).d as *const _ as usize },
+ 6usize,
+ concat!("Offset of field: ", stringify!(Packed), "::", stringify!(d))
+ );
+}
diff --git a/tests/headers/packed-n-with-padding.h b/tests/headers/packed-n-with-padding.h
new file mode 100644
index 00000000..8a6233b5
--- /dev/null
+++ b/tests/headers/packed-n-with-padding.h
@@ -0,0 +1,8 @@
+#pragma pack(push, 2)
+struct Packed {
+ char a;
+ short b;
+ char c;
+ int d;
+};
+#pragma pack(pop)