Skip to content
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

Zero out padding in custom Default trait implementations #2051

Merged
merged 2 commits into from May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/codegen/mod.rs
Expand Up @@ -2196,9 +2196,32 @@ impl CodeGenerator for CompInfo {

if needs_default_impl {
let prefix = ctx.trait_prefix();
let body = if ctx.options().rust_features().maybe_uninit {
quote! {
let mut s = ::#prefix::mem::MaybeUninit::<Self>::uninit();
unsafe {
::#prefix::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
} else {
quote! {
unsafe {
let mut s: Self = ::#prefix::mem::uninitialized();
::#prefix::ptr::write_bytes(&mut s, 0, 1);
s
}
}
};
// Note we use `ptr::write_bytes()` instead of `mem::zeroed()` because the latter does
// not necessarily ensure padding bytes are zeroed. Some C libraries are sensitive to
// non-zero padding bytes, especially when forwards/backwards compatability is
// involved.
result.push(quote! {
impl #generics Default for #ty_for_impl {
fn default() -> Self { unsafe { ::#prefix::mem::zeroed() } }
fn default() -> Self {
danobi marked this conversation as resolved.
Show resolved Hide resolved
#body
}
}
});
}
Expand Down
30 changes: 25 additions & 5 deletions tests/expectations/tests/16-byte-alignment.rs
Expand Up @@ -99,7 +99,11 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1() {
}
impl Default for rte_ipv4_tuple__bindgen_ty_1 {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[test]
Expand Down Expand Up @@ -143,7 +147,11 @@ fn bindgen_test_layout_rte_ipv4_tuple() {
}
impl Default for rte_ipv4_tuple {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[repr(C)]
Expand Down Expand Up @@ -240,7 +248,11 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1() {
}
impl Default for rte_ipv6_tuple__bindgen_ty_1 {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[test]
Expand Down Expand Up @@ -284,7 +296,11 @@ fn bindgen_test_layout_rte_ipv6_tuple() {
}
impl Default for rte_ipv6_tuple {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[repr(C)]
Expand Down Expand Up @@ -333,6 +349,10 @@ fn bindgen_test_layout_rte_thash_tuple() {
}
impl Default for rte_thash_tuple {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
6 changes: 5 additions & 1 deletion tests/expectations/tests/16-byte-alignment_1_0.rs
Expand Up @@ -390,6 +390,10 @@ impl Clone for rte_thash_tuple {
}
impl Default for rte_thash_tuple {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
unsafe {
let mut s: Self = ::std::mem::uninitialized();
::std::ptr::write_bytes(&mut s, 0, 1);
s
}
}
}
12 changes: 10 additions & 2 deletions tests/expectations/tests/allowlist_basic.rs
Expand Up @@ -20,11 +20,19 @@ pub struct AllowlistMe_Inner<T> {
}
impl<T> Default for AllowlistMe_Inner<T> {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
impl<T> Default for AllowlistMe<T> {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
6 changes: 5 additions & 1 deletion tests/expectations/tests/anon-fields-prefix.rs
Expand Up @@ -150,6 +150,10 @@ fn bindgen_test_layout_color() {
}
impl Default for color {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
12 changes: 10 additions & 2 deletions tests/expectations/tests/anon_struct_in_union.rs
Expand Up @@ -74,7 +74,11 @@ fn bindgen_test_layout_s__bindgen_ty_1() {
}
impl Default for s__bindgen_ty_1 {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[test]
Expand All @@ -97,6 +101,10 @@ fn bindgen_test_layout_s() {
}
impl Default for s {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
18 changes: 15 additions & 3 deletions tests/expectations/tests/anon_union.rs
Expand Up @@ -38,12 +38,20 @@ pub union TErrorResult__bindgen_ty_1 {
}
impl Default for TErrorResult__bindgen_ty_1 {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
impl Default for TErrorResult {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[repr(C)]
Expand All @@ -65,7 +73,11 @@ fn bindgen_test_layout_ErrorResult() {
}
impl Default for ErrorResult {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[test]
Expand Down
12 changes: 10 additions & 2 deletions tests/expectations/tests/anon_union_1_0.rs
Expand Up @@ -83,7 +83,11 @@ pub struct TErrorResult__bindgen_ty_1 {
}
impl Default for TErrorResult {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
unsafe {
let mut s: Self = ::std::mem::uninitialized();
::std::ptr::write_bytes(&mut s, 0, 1);
s
}
}
}
#[repr(C)]
Expand Down Expand Up @@ -111,7 +115,11 @@ impl Clone for ErrorResult {
}
impl Default for ErrorResult {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
unsafe {
let mut s: Self = ::std::mem::uninitialized();
::std::ptr::write_bytes(&mut s, 0, 1);
s
}
}
}
#[test]
Expand Down
12 changes: 10 additions & 2 deletions tests/expectations/tests/anonymous-template-types.rs
Expand Up @@ -13,7 +13,11 @@ pub struct Foo<T> {
}
impl<T> Default for Foo<T> {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[repr(C)]
Expand All @@ -29,7 +33,11 @@ pub struct Quux<V> {
}
impl<V> Default for Quux<V> {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[repr(C)]
Expand Down
Expand Up @@ -12,7 +12,11 @@ pub struct std_char_traits {
}
impl Default for std_char_traits {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[repr(C)]
Expand Down
6 changes: 5 additions & 1 deletion tests/expectations/tests/bitfield_align_2.rs
Expand Up @@ -121,7 +121,11 @@ fn bindgen_test_layout_TaggedPtr() {
}
impl Default for TaggedPtr {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
impl TaggedPtr {
Expand Down
6 changes: 5 additions & 1 deletion tests/expectations/tests/blocklist-and-impl-debug.rs
Expand Up @@ -40,7 +40,11 @@ fn bindgen_test_layout_ShouldManuallyImplDebug() {
}
impl Default for ShouldManuallyImplDebug {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
impl ::std::fmt::Debug for ShouldManuallyImplDebug {
Expand Down
6 changes: 5 additions & 1 deletion tests/expectations/tests/blocks-signature.rs
Expand Up @@ -76,7 +76,11 @@ fn bindgen_test_layout_contains_block_pointers() {
}
impl Default for contains_block_pointers {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
pub type _bindgen_ty_id_33 = *const ::block::Block<(), ()>;
Expand Down
6 changes: 5 additions & 1 deletion tests/expectations/tests/blocks.rs
Expand Up @@ -75,6 +75,10 @@ fn bindgen_test_layout_contains_block_pointers() {
}
impl Default for contains_block_pointers {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
6 changes: 5 additions & 1 deletion tests/expectations/tests/c_naming.rs
Expand Up @@ -75,7 +75,11 @@ fn bindgen_test_layout_union_b() {
}
impl Default for union_b {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
pub type b = union_b;
Expand Down