diff options
author | Emilio Cobos Álvarez <emilio@crisal.io> | 2020-01-12 17:36:29 +0100 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2020-01-13 02:33:01 +0100 |
commit | 9a2ce1ca6e4dc740fdffa756e962af906e09a2a1 (patch) | |
tree | 3607e6f199726267bd1636c9f729921c1d61905a | |
parent | ba409edf5d3a1acc6ac4dcb32d1e168cc3a2a41b (diff) |
Remove padding for over-aligned structs when we support repr(align).
Before repr(align), we could only safely guarantee up to 8-bytes of alignment (I
think the pointer-width check is a more conservative way of doing that, in
practice, because I _think_ u64 is 8-byte aligned even for smaller targets).
So when we may generate a potentially-under-aligned struct, we always used to
pad it so as to guarantee that at least the size (and thus reads from rust for
C-allocated structs) was fine.
But if we support repr(align), then the above is always unneeded.
-rw-r--r-- | src/codegen/struct_layout.rs | 15 | ||||
-rw-r--r-- | tests/expectations/tests/issue-1291.rs | 1 | ||||
-rw-r--r-- | tests/expectations/tests/issue-648-derive-debug-with-padding.rs | 2 | ||||
-rw-r--r-- | tests/expectations/tests/layout_array.rs | 1 | ||||
-rw-r--r-- | tests/expectations/tests/layout_array_too_long.rs | 1 | ||||
-rw-r--r-- | tests/expectations/tests/layout_kni_mbuf.rs | 1 | ||||
-rw-r--r-- | tests/expectations/tests/layout_large_align_field.rs | 2 | ||||
-rw-r--r-- | tests/expectations/tests/layout_mbuf.rs | 1 | ||||
-rw-r--r-- | tests/expectations/tests/libclang-5/call-conv-field.rs | 1 | ||||
-rw-r--r-- | tests/expectations/tests/unknown_attr.rs | 1 |
10 files changed, 9 insertions, 17 deletions
diff --git a/src/codegen/struct_layout.rs b/src/codegen/struct_layout.rs index 3c03ff11..45e779c9 100644 --- a/src/codegen/struct_layout.rs +++ b/src/codegen/struct_layout.rs @@ -258,6 +258,11 @@ impl<'a> StructLayoutTracker<'a> { } let padding_bytes = layout.size - self.latest_offset; + if padding_bytes == 0 { + return None; + } + + let repr_align = self.ctx.options().rust_features().repr_align; // We always pad to get to the correct size if the struct is one of // those we can't align properly. @@ -265,12 +270,10 @@ impl<'a> StructLayoutTracker<'a> { // Note that if the last field we saw was a bitfield, we may need to pad // regardless, because bitfields don't respect alignment as strictly as // other fields. - if padding_bytes > 0 && - (padding_bytes >= layout.align || - (self.last_field_was_bitfield && - padding_bytes >= - self.latest_field_layout.unwrap().align) || - layout.align > self.ctx.target_pointer_size()) + if padding_bytes >= layout.align || + (self.last_field_was_bitfield && + padding_bytes >= self.latest_field_layout.unwrap().align) || + (!repr_align && layout.align > self.ctx.target_pointer_size()) { let layout = if self.is_packed { Layout::new(padding_bytes, 1) diff --git a/tests/expectations/tests/issue-1291.rs b/tests/expectations/tests/issue-1291.rs index 64166917..81b29f7d 100644 --- a/tests/expectations/tests/issue-1291.rs +++ b/tests/expectations/tests/issue-1291.rs @@ -26,7 +26,6 @@ pub struct RTCRay { pub geomID: ::std::os::raw::c_uint, pub primID: ::std::os::raw::c_uint, pub instID: ::std::os::raw::c_uint, - pub __bindgen_padding_0: [u32; 3usize], } #[test] fn bindgen_test_layout_RTCRay() { diff --git a/tests/expectations/tests/issue-648-derive-debug-with-padding.rs b/tests/expectations/tests/issue-648-derive-debug-with-padding.rs index cb5ce74e..cb404650 100644 --- a/tests/expectations/tests/issue-648-derive-debug-with-padding.rs +++ b/tests/expectations/tests/issue-648-derive-debug-with-padding.rs @@ -16,7 +16,6 @@ #[derive(Copy, Clone)] pub struct NoDebug { pub c: ::std::os::raw::c_char, - pub __bindgen_padding_0: [u8; 63usize], } #[test] fn bindgen_test_layout_NoDebug() { @@ -61,7 +60,6 @@ impl ::std::cmp::PartialEq for NoDebug { pub struct ShouldDeriveDebugButDoesNot { pub c: [::std::os::raw::c_char; 32usize], pub d: ::std::os::raw::c_char, - pub __bindgen_padding_0: [u8; 31usize], } #[test] fn bindgen_test_layout_ShouldDeriveDebugButDoesNot() { diff --git a/tests/expectations/tests/layout_array.rs b/tests/expectations/tests/layout_array.rs index 597f03d5..3e0142d0 100644 --- a/tests/expectations/tests/layout_array.rs +++ b/tests/expectations/tests/layout_array.rs @@ -68,7 +68,6 @@ pub struct rte_mempool_ops { pub dequeue: rte_mempool_dequeue_t, ///< Get qty of available objs. pub get_count: rte_mempool_get_count, - pub __bindgen_padding_0: [u64; 7usize], } #[test] fn bindgen_test_layout_rte_mempool_ops() { diff --git a/tests/expectations/tests/layout_array_too_long.rs b/tests/expectations/tests/layout_array_too_long.rs index 2cd154af..5d84f053 100644 --- a/tests/expectations/tests/layout_array_too_long.rs +++ b/tests/expectations/tests/layout_array_too_long.rs @@ -163,7 +163,6 @@ pub struct ip_frag_pkt { pub last_idx: u32, ///< fragments pub frags: [ip_frag; 4usize], - pub __bindgen_padding_0: [u64; 6usize], } #[repr(C)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] diff --git a/tests/expectations/tests/layout_kni_mbuf.rs b/tests/expectations/tests/layout_kni_mbuf.rs index b554aed5..19e82571 100644 --- a/tests/expectations/tests/layout_kni_mbuf.rs +++ b/tests/expectations/tests/layout_kni_mbuf.rs @@ -33,7 +33,6 @@ pub struct rte_kni_mbuf { pub pad3: [::std::os::raw::c_char; 8usize], pub pool: *mut ::std::os::raw::c_void, pub next: *mut ::std::os::raw::c_void, - pub __bindgen_padding_1: [u64; 5usize], } #[test] fn bindgen_test_layout_rte_kni_mbuf() { diff --git a/tests/expectations/tests/layout_large_align_field.rs b/tests/expectations/tests/layout_large_align_field.rs index 3b3e82cd..55a05fe5 100644 --- a/tests/expectations/tests/layout_large_align_field.rs +++ b/tests/expectations/tests/layout_large_align_field.rs @@ -193,7 +193,6 @@ pub struct ip_frag_pkt { pub last_idx: u32, ///< fragments pub frags: [ip_frag; 4usize], - pub __bindgen_padding_0: [u64; 6usize], } #[repr(C)] #[derive(Debug, Copy, Clone)] @@ -417,7 +416,6 @@ pub struct ip_frag_tbl_stat { pub fail_total: u64, ///< # of 'no space' add failures. pub fail_nospace: u64, - pub __bindgen_padding_0: [u64; 2usize], } #[test] fn bindgen_test_layout_ip_frag_tbl_stat() { diff --git a/tests/expectations/tests/layout_mbuf.rs b/tests/expectations/tests/layout_mbuf.rs index 51e5526e..d0149b10 100644 --- a/tests/expectations/tests/layout_mbuf.rs +++ b/tests/expectations/tests/layout_mbuf.rs @@ -178,7 +178,6 @@ pub struct rte_mbuf { pub priv_size: u16, /// Timesync flags for use with IEEE1588. pub timesync: u16, - pub __bindgen_padding_0: [u32; 7usize], } /// 16-bit Reference counter. /// It should only be accessed using the following functions: diff --git a/tests/expectations/tests/libclang-5/call-conv-field.rs b/tests/expectations/tests/libclang-5/call-conv-field.rs index 3286ac46..9cb9606f 100644 --- a/tests/expectations/tests/libclang-5/call-conv-field.rs +++ b/tests/expectations/tests/libclang-5/call-conv-field.rs @@ -14,7 +14,6 @@ pub struct JNINativeInterface_ { pub GetVersion: ::std::option::Option< unsafe extern "stdcall" fn(env: *mut ::std::os::raw::c_void) -> ::std::os::raw::c_int, >, - pub __bindgen_padding_0: u32, pub __hack: ::std::os::raw::c_ulonglong, } #[test] diff --git a/tests/expectations/tests/unknown_attr.rs b/tests/expectations/tests/unknown_attr.rs index d7c8134c..0c0eeff1 100644 --- a/tests/expectations/tests/unknown_attr.rs +++ b/tests/expectations/tests/unknown_attr.rs @@ -14,7 +14,6 @@ pub struct max_align_t { pub __clang_max_align_nonce1: ::std::os::raw::c_longlong, pub __bindgen_padding_0: u64, pub __clang_max_align_nonce2: ::std::os::raw::c_longlong, - pub __bindgen_padding_1: u64, } #[test] fn bindgen_test_layout_max_align_t() { |