Skip to content

Commit 7a5ae20

Browse files
authored
Merge pull request #1495 from tromey/packed-only-vtable
Fix layout with packed class that only has a vtable
2 parents 42bfc23 + 5c92043 commit 7a5ae20

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/ir/comp.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,10 @@ impl CompInfo {
15171517
}) {
15181518
info!("Found a struct that was defined within `#pragma packed(...)`");
15191519
return true;
1520+
} else if self.has_own_virtual_method {
1521+
if parent_layout.align == 1 {
1522+
return true;
1523+
}
15201524
}
15211525
}
15221526

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
9+
#![cfg(feature = "nightly")]
10+
11+
#[repr(C)]
12+
pub struct PackedVtable__bindgen_vtable(::std::os::raw::c_void);
13+
#[repr(C, packed)]
14+
#[derive(Debug)]
15+
pub struct PackedVtable {
16+
pub vtable_: *const PackedVtable__bindgen_vtable,
17+
}
18+
#[test]
19+
fn bindgen_test_layout_PackedVtable() {
20+
assert_eq!(
21+
::std::mem::size_of::<PackedVtable>(),
22+
8usize,
23+
concat!("Size of: ", stringify!(PackedVtable))
24+
);
25+
assert_eq!(
26+
::std::mem::align_of::<PackedVtable>(),
27+
1usize,
28+
concat!("Alignment of ", stringify!(PackedVtable))
29+
);
30+
}
31+
impl Default for PackedVtable {
32+
fn default() -> Self {
33+
unsafe { ::std::mem::zeroed() }
34+
}
35+
}
36+
extern "C" {
37+
#[link_name = "\u{1}_ZN12PackedVtableD1Ev"]
38+
pub fn PackedVtable_PackedVtable_destructor(this: *mut PackedVtable);
39+
}

tests/headers/packed-vtable.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// bindgen-flags: --raw-line '#![cfg(feature = "nightly")]' --rust-target 1.33 -- -x c++ -std=c++11
2+
3+
#pragma pack(1)
4+
5+
// This should be packed.
6+
struct PackedVtable {
7+
virtual ~PackedVtable();
8+
};
9+
10+
#pragma pack()

0 commit comments

Comments
 (0)