Skip to content

Commit

Permalink
Make bitfield unit allocation fallible
Browse files Browse the repository at this point in the history
Instead of panicking when we see a bitfield that does not have a layout, return
an error up the stack. If we get an error when allocating bitfields into units,
then make the whole struct opaque.

Fixes rust-lang#1140
  • Loading branch information
fitzgen committed Nov 2, 2017
1 parent 406b477 commit 37cf177
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
48 changes: 36 additions & 12 deletions src/ir/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ impl FieldMethods for RawField {
fn raw_fields_to_fields_and_bitfield_units<I>(
ctx: &BindgenContext,
raw_fields: I,
) -> Vec<Field>
) -> Result<Vec<Field>, ()>
where
I: IntoIterator<Item = RawField>,
{
Expand Down Expand Up @@ -503,15 +503,15 @@ where
&mut bitfield_unit_count,
&mut fields,
bitfields,
);
)?;
}

assert!(
raw_fields.next().is_none(),
"The above loop should consume all items in `raw_fields`"
);

fields
Ok(fields)
}

/// Given a set of contiguous raw bitfields, group and allocate them into
Expand All @@ -521,7 +521,8 @@ fn bitfields_to_allocation_units<E, I>(
bitfield_unit_count: &mut usize,
fields: &mut E,
raw_bitfields: I,
) where
) -> Result<(), ()>
where
E: Extend<Field>,
I: IntoIterator<Item = RawField>,
{
Expand Down Expand Up @@ -572,7 +573,7 @@ fn bitfields_to_allocation_units<E, I>(
let bitfield_width = bitfield.bitfield_width().unwrap() as usize;
let bitfield_layout = ctx.resolve_type(bitfield.ty())
.layout(ctx)
.expect("Bitfield without layout? Gah!");
.ok_or(())?;
let bitfield_size = bitfield_layout.size;
let bitfield_align = bitfield_layout.align;

Expand Down Expand Up @@ -649,6 +650,8 @@ fn bitfields_to_allocation_units<E, I>(
bitfields_in_unit,
);
}

Ok(())
}

/// A compound structure's fields are initially raw, and have bitfields that
Expand All @@ -662,6 +665,7 @@ fn bitfields_to_allocation_units<E, I>(
enum CompFields {
BeforeComputingBitfieldUnits(Vec<RawField>),
AfterComputingBitfieldUnits(Vec<Field>),
ErrorComputingBitfieldUnits,
}

impl Default for CompFields {
Expand All @@ -676,7 +680,7 @@ impl CompFields {
CompFields::BeforeComputingBitfieldUnits(ref mut raws) => {
raws.push(raw);
}
CompFields::AfterComputingBitfieldUnits(_) => {
_ => {
panic!(
"Must not append new fields after computing bitfield allocation units"
);
Expand All @@ -689,22 +693,36 @@ impl CompFields {
CompFields::BeforeComputingBitfieldUnits(ref mut raws) => {
mem::replace(raws, vec![])
}
CompFields::AfterComputingBitfieldUnits(_) => {
_ => {
panic!("Already computed bitfield units");
}
};

let fields_and_units =
let result =
raw_fields_to_fields_and_bitfield_units(ctx, raws);
mem::replace(
self,
CompFields::AfterComputingBitfieldUnits(fields_and_units),
);

match result {
Ok(fields_and_units) => {
mem::replace(
self,
CompFields::AfterComputingBitfieldUnits(fields_and_units));
}
Err(()) => {
mem::replace(
self,
CompFields::ErrorComputingBitfieldUnits
);
}
}
}

fn deanonymize_fields(&mut self, ctx: &BindgenContext, methods: &[Method]) {
let fields = match *self {
CompFields::AfterComputingBitfieldUnits(ref mut fields) => fields,
CompFields::ErrorComputingBitfieldUnits => {
// Nothing to do here.
return;
}
CompFields::BeforeComputingBitfieldUnits(_) => {
panic!("Not yet computed bitfield units.");
}
Expand Down Expand Up @@ -787,6 +805,7 @@ impl Trace for CompFields {
T: Tracer,
{
match *self {
CompFields::ErrorComputingBitfieldUnits => {}
CompFields::BeforeComputingBitfieldUnits(ref fields) => {
for f in fields {
tracer.visit_kind(f.ty().into(), EdgeKind::Field);
Expand Down Expand Up @@ -1046,6 +1065,7 @@ impl CompInfo {
/// Get this type's set of fields.
pub fn fields(&self) -> &[Field] {
match self.fields {
CompFields::ErrorComputingBitfieldUnits => &[][..],
CompFields::AfterComputingBitfieldUnits(ref fields) => fields,
CompFields::BeforeComputingBitfieldUnits(_) => {
panic!("Should always have computed bitfield units first");
Expand Down Expand Up @@ -1558,6 +1578,10 @@ impl IsOpaque for CompInfo {
return true
}

if let CompFields::ErrorComputingBitfieldUnits = self.fields {
return true;
}

// Bitfields with a width that is larger than their unit's width have
// some strange things going on, and the best we can do is make the
// whole struct opaque.
Expand Down
15 changes: 15 additions & 0 deletions tests/expectations/tests/templatized-bitfield.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* automatically generated by rust-bindgen */


#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]



/// We don't get a layout for this bitfield, since we don't know what `T` will
/// be, so we cannot allocate bitfield units. The best thing we can do is make
/// the struct opaque.
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct TemplatizedBitfield {
pub _address: u8,
}
7 changes: 7 additions & 0 deletions tests/headers/templatized-bitfield.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// We don't get a layout for this bitfield, since we don't know what `T` will
/// be, so we cannot allocate bitfield units. The best thing we can do is make
/// the struct opaque.
template <class T>
class TemplatizedBitfield {
T t : 6;
};

0 comments on commit 37cf177

Please sign in to comment.