Skip to content
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
27 changes: 27 additions & 0 deletions bindgen-tests/tests/expectations/tests/issue-3406.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions bindgen-tests/tests/expectations/tests/layout_array.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions bindgen-tests/tests/expectations/tests/unknown_attr.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions bindgen-tests/tests/headers/issue-3406.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
struct __attribute__((aligned(16))) Inner {
char byte;
};

struct Outer {
int before;
struct Inner inner;
};
49 changes: 8 additions & 41 deletions bindgen/codegen/struct_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::helpers;
use crate::ir::comp::CompInfo;
use crate::ir::context::BindgenContext;
use crate::ir::layout::Layout;
use crate::ir::ty::{Type, TypeKind};
use crate::ir::ty::Type;
use crate::FieldVisibilityKind;
use proc_macro2::{Ident, Span};
use std::cmp;
Expand Down Expand Up @@ -142,27 +142,7 @@ impl<'a> StructLayoutTracker<'a> {
field_ty: &Type,
field_offset: Option<usize>,
) -> Option<proc_macro2::TokenStream> {
let mut field_layout = field_ty.layout(self.ctx)?;

if let TypeKind::Array(inner, len) =
*field_ty.canonical_type(self.ctx).kind()
{
// FIXME(emilio): As an _ultra_ hack, we correct the layout returned
// by arrays of structs that have a bigger alignment than what we
// can support.
//
// This means that the structs in the array are super-unsafe to
// access, since they won't be properly aligned, but there's not too
// much we can do about it.
if let Some(layout) = self.ctx.resolve_type(inner).layout(self.ctx)
{
if layout.align > MAX_GUARANTEED_ALIGN {
field_layout.size =
align_to(layout.size, layout.align) * len;
field_layout.align = MAX_GUARANTEED_ALIGN;
}
}
}
let field_layout = field_ty.layout(self.ctx)?;
self.saw_field_with_layout(field_name, field_layout, field_offset)
}

Expand Down Expand Up @@ -206,9 +186,8 @@ impl<'a> StructLayoutTracker<'a> {
let force_padding = self.ctx.options().force_explicit_padding;

// Otherwise the padding is useless.
let need_padding = force_padding ||
padding_bytes >= field_layout.align ||
field_layout.align > MAX_GUARANTEED_ALIGN;
let need_padding =
force_padding || padding_bytes >= field_layout.align;

debug!(
"Offset: <padding>: {} -> {}",
Expand All @@ -222,12 +201,7 @@ impl<'a> StructLayoutTracker<'a> {
field_offset.unwrap_or(0) / 8,
);

let padding_align = if force_padding {
1
} else {
cmp::min(field_layout.align, MAX_GUARANTEED_ALIGN)
};

let padding_align = 1;
if need_padding && padding_bytes != 0 {
Some(Layout::new(padding_bytes, padding_align))
} else {
Expand Down Expand Up @@ -310,8 +284,6 @@ impl<'a> StructLayoutTracker<'a> {
return None;
}

let repr_align = true;

// We always pad to get to the correct size if the struct is one of
// those we can't align properly.
//
Expand All @@ -320,8 +292,7 @@ impl<'a> StructLayoutTracker<'a> {
// other fields.
if padding_bytes >= layout.align ||
(self.last_field_was_bitfield &&
padding_bytes >= self.latest_field_layout.unwrap().align) ||
(!repr_align && layout.align > MAX_GUARANTEED_ALIGN)
padding_bytes >= self.latest_field_layout.unwrap().align)
{
let layout = if self.is_packed {
Layout::new(padding_bytes, 1)
Expand All @@ -343,23 +314,19 @@ impl<'a> StructLayoutTracker<'a> {
}

pub(crate) fn requires_explicit_align(&self, layout: Layout) -> bool {
let repr_align = true;

// Always force explicit repr(align) for stuff more than 16-byte aligned
// to work-around https://github.com/rust-lang/rust/issues/54341.
//
// Worst-case this just generates redundant alignment attributes.
if repr_align && self.max_field_align >= 16 {
if self.max_field_align >= 16 {
return true;
}

if self.max_field_align >= layout.align {
return false;
}

// We can only generate up-to a 8-bytes of alignment unless we support
// repr(align).
repr_align || layout.align <= MAX_GUARANTEED_ALIGN
true
}

fn padding_bytes(&self, layout: Layout) -> usize {
Expand Down
Loading