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

Ensure all derive analyses check array limit on bitfields #1001

Merged
merged 2 commits into from
Sep 21, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/ir/analysis/derive_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -266,6 +267,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())
})
Expand Down
8 changes: 8 additions & 0 deletions src/ir/analysis/derive_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand Down
8 changes: 8 additions & 0 deletions src/ir/analysis/derive_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
8 changes: 8 additions & 0 deletions src/ir/analysis/derive_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
8 changes: 8 additions & 0 deletions src/ir/analysis/derive_partial_eq_or_partial_ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
21 changes: 21 additions & 0 deletions tests/expectations/tests/bitfield_large_overflow.rs
Original file line number Diff line number Diff line change
@@ -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;
}

5 changes: 5 additions & 0 deletions tests/headers/bitfield_large_overflow.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// bindgen-flags: --no-layout-tests

struct {
unsigned : 632;
} a;