Skip to content

Commit 8387c7f

Browse files
author
bors-servo
authored
Auto merge of #1060 - aeleos:master, r=fitzgen
Make bitfields larger than type opaque @fitzgen r? Fixes #1007 by ensuring that bitfields larger than type will be opaque, ensuring the layout is correct.
2 parents 2930a85 + aa27283 commit 8387c7f

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

src/ir/comp.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -1473,8 +1473,25 @@ impl DotAttributes for CompInfo {
14731473
impl IsOpaque for CompInfo {
14741474
type Extra = ();
14751475

1476-
fn is_opaque(&self, _: &BindgenContext, _: &()) -> bool {
1477-
self.has_non_type_template_params
1476+
fn is_opaque(&self, ctx: &BindgenContext, _: &()) -> bool {
1477+
// Early return to avoid extra computation
1478+
if self.has_non_type_template_params {
1479+
return true
1480+
}
1481+
1482+
self.fields().iter().any(|f| match *f {
1483+
Field::DataMember(_) => {
1484+
false
1485+
},
1486+
Field::Bitfields(ref unit) => {
1487+
unit.bitfields().iter().any(|bf| {
1488+
let bitfield_layout = ctx.resolve_type(bf.ty())
1489+
.layout(ctx)
1490+
.expect("Bitfield without layout? Gah!");
1491+
bf.width() / 8 > bitfield_layout.size as u32
1492+
})
1493+
}
1494+
})
14781495
}
14791496
}
14801497

src/ir/context.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1137,8 +1137,6 @@ impl BindgenContext {
11371137
{
11381138
self.in_codegen = true;
11391139

1140-
self.assert_no_dangling_references();
1141-
11421140
if !self.collected_typerefs() {
11431141
self.resolve_typerefs();
11441142
self.compute_bitfield_units();
@@ -1147,8 +1145,6 @@ impl BindgenContext {
11471145

11481146
self.deanonymize_fields();
11491147

1150-
// And assert once again, because resolving type refs and processing
1151-
// replacements both mutate the IR graph.
11521148
self.assert_no_dangling_references();
11531149

11541150
// Compute the whitelisted set after processing replacements and

tests/expectations/tests/bitfield_large_overflow.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,26 @@
55

66

77
#[repr(C)]
8+
#[derive(Debug, Default, Copy)]
89
pub struct _bindgen_ty_1 {
9-
pub _bitfield_1: [u8; 128usize],
10-
pub __bindgen_align: [u64; 0usize],
10+
pub _bindgen_opaque_blob: [u64; 10usize],
1111
}
12-
impl Default for _bindgen_ty_1 {
13-
fn default() -> Self {
14-
unsafe { ::std::mem::zeroed() }
12+
#[test]
13+
fn bindgen_test_layout__bindgen_ty_1() {
14+
assert_eq!(
15+
::std::mem::size_of::<_bindgen_ty_1>(),
16+
80usize,
17+
concat!("Size of: ", stringify!(_bindgen_ty_1))
18+
);
19+
assert_eq!(
20+
::std::mem::align_of::<_bindgen_ty_1>(),
21+
8usize,
22+
concat!("Alignment of ", stringify!(_bindgen_ty_1))
23+
);
24+
}
25+
impl Clone for _bindgen_ty_1 {
26+
fn clone(&self) -> Self {
27+
*self
1528
}
1629
}
1730
extern "C" {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// bindgen-flags: --no-layout-tests
2-
31
struct {
42
unsigned : 632;
53
} a;

0 commit comments

Comments
 (0)