From 5c92043f2a1b42d52fc2468c2baf0c9cc597c1f8 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Fri, 18 Jan 2019 13:23:37 -0700 Subject: [PATCH] Fix layout with packed class that only has a vtable I noticed that a `#pragma packed` class that has only a vtable but no data members is not marked repr(packed). The bug is that is_packed examines data members but not the vtable when applying its heuristic. --- src/ir/comp.rs | 4 +++ tests/expectations/tests/packed-vtable.rs | 39 +++++++++++++++++++++++ tests/headers/packed-vtable.h | 10 ++++++ 3 files changed, 53 insertions(+) create mode 100644 tests/expectations/tests/packed-vtable.rs create mode 100644 tests/headers/packed-vtable.h diff --git a/src/ir/comp.rs b/src/ir/comp.rs index 12a8fa323f..85054e5aa6 100644 --- a/src/ir/comp.rs +++ b/src/ir/comp.rs @@ -1517,6 +1517,10 @@ impl CompInfo { }) { info!("Found a struct that was defined within `#pragma packed(...)`"); return true; + } else if self.has_own_virtual_method { + if parent_layout.align == 1 { + return true; + } } } diff --git a/tests/expectations/tests/packed-vtable.rs b/tests/expectations/tests/packed-vtable.rs new file mode 100644 index 0000000000..4bb206c68a --- /dev/null +++ b/tests/expectations/tests/packed-vtable.rs @@ -0,0 +1,39 @@ +/* automatically generated by rust-bindgen */ + +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] +#![cfg(feature = "nightly")] + +#[repr(C)] +pub struct PackedVtable__bindgen_vtable(::std::os::raw::c_void); +#[repr(C, packed)] +#[derive(Debug)] +pub struct PackedVtable { + pub vtable_: *const PackedVtable__bindgen_vtable, +} +#[test] +fn bindgen_test_layout_PackedVtable() { + assert_eq!( + ::std::mem::size_of::(), + 8usize, + concat!("Size of: ", stringify!(PackedVtable)) + ); + assert_eq!( + ::std::mem::align_of::(), + 1usize, + concat!("Alignment of ", stringify!(PackedVtable)) + ); +} +impl Default for PackedVtable { + fn default() -> Self { + unsafe { ::std::mem::zeroed() } + } +} +extern "C" { + #[link_name = "\u{1}_ZN12PackedVtableD1Ev"] + pub fn PackedVtable_PackedVtable_destructor(this: *mut PackedVtable); +} diff --git a/tests/headers/packed-vtable.h b/tests/headers/packed-vtable.h new file mode 100644 index 0000000000..d2413d4571 --- /dev/null +++ b/tests/headers/packed-vtable.h @@ -0,0 +1,10 @@ +// bindgen-flags: --raw-line '#![cfg(feature = "nightly")]' --rust-target 1.33 -- -x c++ -std=c++11 + +#pragma pack(1) + +// This should be packed. +struct PackedVtable { + virtual ~PackedVtable(); +}; + +#pragma pack()