Skip to content

Commit d98f561

Browse files
committed
Fix alignment bug
`StructureBuilder::insert` doesn't affect the alignment of the type, so we need to always explicitly specify alignment.
1 parent 6ee8cda commit d98f561

File tree

1 file changed

+8
-12
lines changed
  • rust/binaryninja-derive/src

1 file changed

+8
-12
lines changed

rust/binaryninja-derive/src/lib.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,10 @@ fn impl_abstract_structure_type(
384384
.map(|field| AbstractField::from_field(field, &name, pointer_width))
385385
.collect::<Result<Vec<_>>>()?;
386386

387+
if abstract_fields.is_empty() {
388+
return Err(name.span().error("expected at least one named field"));
389+
}
390+
387391
// Generate the arguments to `StructureBuilder::insert`. Luckily `mem::offset_of!` was stabilized in
388392
// Rust 1.77 or otherwise this would be a lot more complicated.
389393
let layout_name = format_ident!("__{name}_layout");
@@ -434,15 +438,7 @@ fn impl_abstract_structure_type(
434438
Some(n) => quote! { #[repr(packed(#n))] },
435439
None => quote! { #[repr(packed)] },
436440
});
437-
let (align, set_alignment) = repr
438-
.align
439-
.map(|n| {
440-
(
441-
quote! { #[repr(align(#n))] },
442-
quote! { .set_alignment(Self::LAYOUT.align()) },
443-
)
444-
})
445-
.unzip();
441+
let align = repr.align.map(|n| quote! { #[repr(align(#n))] });
446442

447443
// Distinguish between structs and unions
448444
let (kind, set_union) = match kind {
@@ -478,11 +474,11 @@ fn impl_abstract_structure_type(
478474
fn resolve_type() -> ::binaryninja::rc::Ref<::binaryninja::types::Type> {
479475
::binaryninja::types::Type::structure(
480476
&::binaryninja::types::Structure::builder()
481-
#(.insert(#args))*
482-
.set_width(Self::LAYOUT.size() as u64)
483477
.set_packed(#is_packed)
484-
#set_alignment
478+
.set_width(Self::LAYOUT.size() as u64)
479+
.set_alignment(Self::LAYOUT.align())
485480
#set_union
481+
#(.insert(#args))*
486482
.finalize()
487483
)
488484
}

0 commit comments

Comments
 (0)