From 95879ddfbf2abe335118fcbb9308753bb092a21c Mon Sep 17 00:00:00 2001 From: Oliver Geller Date: Mon, 18 Sep 2017 18:42:12 -0400 Subject: [PATCH 1/2] Ensure all derive analyses check array limit on bitfields --- src/ir/analysis/derive_copy.rs | 8 ++++++++ src/ir/analysis/derive_debug.rs | 8 ++++++++ src/ir/analysis/derive_default.rs | 8 ++++++++ src/ir/analysis/derive_hash.rs | 8 ++++++++ src/ir/analysis/derive_partial_eq_or_partial_ord.rs | 8 ++++++++ 5 files changed, 40 insertions(+) diff --git a/src/ir/analysis/derive_copy.rs b/src/ir/analysis/derive_copy.rs index ef01c65aa8..fa9cd6e091 100644 --- a/src/ir/analysis/derive_copy.rs +++ b/src/ir/analysis/derive_copy.rs @@ -266,6 +266,14 @@ impl<'ctx> MonotoneFramework for CannotDeriveCopy<'ctx> { self.is_not_copy(data.ty()) } Field::Bitfields(ref bfu) => { + if bfu.layout().align > RUST_DERIVE_IN_ARRAY_LIMIT { + trace!( + " we cannot derive Copy for a bitfield larger then \ + the limit" + ); + return true; + } + bfu.bitfields().iter().any(|b| { self.is_not_copy(b.ty()) }) diff --git a/src/ir/analysis/derive_debug.rs b/src/ir/analysis/derive_debug.rs index 2bfaff7183..7df745c9de 100644 --- a/src/ir/analysis/derive_debug.rs +++ b/src/ir/analysis/derive_debug.rs @@ -268,6 +268,14 @@ impl<'ctx> MonotoneFramework for CannotDeriveDebug<'ctx> { self.is_not_debug(data.ty()) } Field::Bitfields(ref bfu) => { + if bfu.layout().align > RUST_DERIVE_IN_ARRAY_LIMIT { + trace!( + " we cannot derive Debug for a bitfield larger then \ + the limit" + ); + return true; + } + bfu.bitfields().iter().any(|b| { self.is_not_debug(b.ty()) }) diff --git a/src/ir/analysis/derive_default.rs b/src/ir/analysis/derive_default.rs index 96805863dd..7acbe04a65 100644 --- a/src/ir/analysis/derive_default.rs +++ b/src/ir/analysis/derive_default.rs @@ -308,6 +308,14 @@ impl<'ctx> MonotoneFramework for CannotDeriveDefault<'ctx> { self.is_not_default(data.ty()) } Field::Bitfields(ref bfu) => { + if bfu.layout().align > RUST_DERIVE_IN_ARRAY_LIMIT { + trace!( + " we cannot derive Default for a bitfield larger then \ + the limit" + ); + return true; + } + bfu.bitfields().iter().any(|b| { !self.ctx.whitelisted_items().contains( &b.ty(), diff --git a/src/ir/analysis/derive_hash.rs b/src/ir/analysis/derive_hash.rs index 80ea0abf8a..5313cae3c2 100644 --- a/src/ir/analysis/derive_hash.rs +++ b/src/ir/analysis/derive_hash.rs @@ -283,6 +283,14 @@ impl<'ctx> MonotoneFramework for CannotDeriveHash<'ctx> { self.cannot_derive_hash.contains(&data.ty()) } Field::Bitfields(ref bfu) => { + if bfu.layout().align > RUST_DERIVE_IN_ARRAY_LIMIT { + trace!( + " we cannot derive Hash for a bitfield larger then \ + the limit" + ); + return true; + } + bfu.bitfields().iter().any(|b| { !self.ctx.whitelisted_items().contains( &b.ty(), diff --git a/src/ir/analysis/derive_partial_eq_or_partial_ord.rs b/src/ir/analysis/derive_partial_eq_or_partial_ord.rs index 1d4a5939ee..1c3ab05980 100644 --- a/src/ir/analysis/derive_partial_eq_or_partial_ord.rs +++ b/src/ir/analysis/derive_partial_eq_or_partial_ord.rs @@ -292,6 +292,14 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> { ) } Field::Bitfields(ref bfu) => { + if bfu.layout().align > RUST_DERIVE_IN_ARRAY_LIMIT { + trace!( + " we cannot derive PartialEq for a bitfield larger then \ + the limit" + ); + return true; + } + bfu.bitfields().iter().any(|b| { !self.ctx.whitelisted_items().contains( &b.ty(), From 4111a46da52ebb4ada98a6c2277e6ac00eae06cc Mon Sep 17 00:00:00 2001 From: Oliver Geller Date: Mon, 18 Sep 2017 23:55:27 -0400 Subject: [PATCH 2/2] Add large bitfield derive test and fix missing limit --- src/ir/analysis/derive_copy.rs | 1 + .../tests/bitfield_large_overflow.rs | 21 +++++++++++++++++++ tests/headers/bitfield_large_overflow.hpp | 5 +++++ 3 files changed, 27 insertions(+) create mode 100644 tests/expectations/tests/bitfield_large_overflow.rs create mode 100644 tests/headers/bitfield_large_overflow.hpp diff --git a/src/ir/analysis/derive_copy.rs b/src/ir/analysis/derive_copy.rs index fa9cd6e091..264d227a06 100644 --- a/src/ir/analysis/derive_copy.rs +++ b/src/ir/analysis/derive_copy.rs @@ -9,6 +9,7 @@ use ir::derive::CanTriviallyDeriveCopy; use ir::item::IsOpaque; use ir::template::TemplateParameters; use ir::traversal::EdgeKind; +use ir::ty::RUST_DERIVE_IN_ARRAY_LIMIT; use ir::ty::TypeKind; use std::collections::HashMap; use std::collections::HashSet; diff --git a/tests/expectations/tests/bitfield_large_overflow.rs b/tests/expectations/tests/bitfield_large_overflow.rs new file mode 100644 index 0000000000..523570e4a3 --- /dev/null +++ b/tests/expectations/tests/bitfield_large_overflow.rs @@ -0,0 +1,21 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] + + +#[repr(C)] +pub struct _bindgen_ty_1 { + pub _bitfield_1: [u8; 128usize], + pub __bindgen_align: [u64; 0usize], +} +impl Default for _bindgen_ty_1 { + fn default() -> Self { + unsafe { ::std::mem::zeroed() } + } +} +extern "C" { + #[link_name = "a"] + pub static mut a: _bindgen_ty_1; +} + diff --git a/tests/headers/bitfield_large_overflow.hpp b/tests/headers/bitfield_large_overflow.hpp new file mode 100644 index 0000000000..227829b8fd --- /dev/null +++ b/tests/headers/bitfield_large_overflow.hpp @@ -0,0 +1,5 @@ +// bindgen-flags: --no-layout-tests + +struct { + unsigned : 632; +} a;