Skip to content

Commit f354836

Browse files
authored
Merge pull request #1592 from emilio/incomplete-array-layout
ir: Properly find the layout of incomplete arrays.
2 parents 3dc22fd + e0fc5ca commit f354836

File tree

5 files changed

+184
-2
lines changed

5 files changed

+184
-2
lines changed

src/ir/ty.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ impl Type {
249249
self.layout.or_else(|| {
250250
match self.kind {
251251
TypeKind::Comp(ref ci) => ci.layout(ctx),
252+
TypeKind::Array(inner, length) if length == 0 => {
253+
Some(Layout::new(
254+
0,
255+
ctx.resolve_type(inner).layout(ctx)?.align,
256+
))
257+
}
252258
// FIXME(emilio): This is a hack for anonymous union templates.
253259
// Use the actual pointer size!
254260
TypeKind::Pointer(..) => {
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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+
10+
#[repr(C)]
11+
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
12+
pub struct __BindgenBitfieldUnit<Storage, Align> {
13+
storage: Storage,
14+
align: [Align; 0],
15+
}
16+
impl<Storage, Align> __BindgenBitfieldUnit<Storage, Align> {
17+
#[inline]
18+
pub const fn new(storage: Storage) -> Self {
19+
Self { storage, align: [] }
20+
}
21+
}
22+
impl<Storage, Align> __BindgenBitfieldUnit<Storage, Align>
23+
where
24+
Storage: AsRef<[u8]> + AsMut<[u8]>,
25+
{
26+
#[inline]
27+
pub fn get_bit(&self, index: usize) -> bool {
28+
debug_assert!(index / 8 < self.storage.as_ref().len());
29+
let byte_index = index / 8;
30+
let byte = self.storage.as_ref()[byte_index];
31+
let bit_index = if cfg!(target_endian = "big") {
32+
7 - (index % 8)
33+
} else {
34+
index % 8
35+
};
36+
let mask = 1 << bit_index;
37+
byte & mask == mask
38+
}
39+
#[inline]
40+
pub fn set_bit(&mut self, index: usize, val: bool) {
41+
debug_assert!(index / 8 < self.storage.as_ref().len());
42+
let byte_index = index / 8;
43+
let byte = &mut self.storage.as_mut()[byte_index];
44+
let bit_index = if cfg!(target_endian = "big") {
45+
7 - (index % 8)
46+
} else {
47+
index % 8
48+
};
49+
let mask = 1 << bit_index;
50+
if val {
51+
*byte |= mask;
52+
} else {
53+
*byte &= !mask;
54+
}
55+
}
56+
#[inline]
57+
pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 {
58+
debug_assert!(bit_width <= 64);
59+
debug_assert!(bit_offset / 8 < self.storage.as_ref().len());
60+
debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len());
61+
let mut val = 0;
62+
for i in 0..(bit_width as usize) {
63+
if self.get_bit(i + bit_offset) {
64+
let index = if cfg!(target_endian = "big") {
65+
bit_width as usize - 1 - i
66+
} else {
67+
i
68+
};
69+
val |= 1 << index;
70+
}
71+
}
72+
val
73+
}
74+
#[inline]
75+
pub fn set(&mut self, bit_offset: usize, bit_width: u8, val: u64) {
76+
debug_assert!(bit_width <= 64);
77+
debug_assert!(bit_offset / 8 < self.storage.as_ref().len());
78+
debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len());
79+
for i in 0..(bit_width as usize) {
80+
let mask = 1 << i;
81+
let val_bit_is_set = val & mask == mask;
82+
let index = if cfg!(target_endian = "big") {
83+
bit_width as usize - 1 - i
84+
} else {
85+
i
86+
};
87+
self.set_bit(index + bit_offset, val_bit_is_set);
88+
}
89+
}
90+
}
91+
#[repr(C)]
92+
#[derive(Default)]
93+
pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]);
94+
impl<T> __IncompleteArrayField<T> {
95+
#[inline]
96+
pub const fn new() -> Self {
97+
__IncompleteArrayField(::std::marker::PhantomData, [])
98+
}
99+
#[inline]
100+
pub unsafe fn as_ptr(&self) -> *const T {
101+
::std::mem::transmute(self)
102+
}
103+
#[inline]
104+
pub unsafe fn as_mut_ptr(&mut self) -> *mut T {
105+
::std::mem::transmute(self)
106+
}
107+
#[inline]
108+
pub unsafe fn as_slice(&self, len: usize) -> &[T] {
109+
::std::slice::from_raw_parts(self.as_ptr(), len)
110+
}
111+
#[inline]
112+
pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] {
113+
::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
114+
}
115+
}
116+
impl<T> ::std::fmt::Debug for __IncompleteArrayField<T> {
117+
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
118+
fmt.write_str("__IncompleteArrayField")
119+
}
120+
}
121+
impl<T> ::std::clone::Clone for __IncompleteArrayField<T> {
122+
#[inline]
123+
fn clone(&self) -> Self {
124+
Self::new()
125+
}
126+
}
127+
#[repr(C)]
128+
#[derive(Debug)]
129+
pub struct foo {
130+
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 1usize], u8>,
131+
pub b: __IncompleteArrayField<*mut ::std::os::raw::c_void>,
132+
}
133+
#[test]
134+
fn bindgen_test_layout_foo() {
135+
assert_eq!(
136+
::std::mem::size_of::<foo>(),
137+
8usize,
138+
concat!("Size of: ", stringify!(foo))
139+
);
140+
assert_eq!(
141+
::std::mem::align_of::<foo>(),
142+
8usize,
143+
concat!("Alignment of ", stringify!(foo))
144+
);
145+
}
146+
impl Default for foo {
147+
fn default() -> Self {
148+
unsafe { ::std::mem::zeroed() }
149+
}
150+
}
151+
impl foo {
152+
#[inline]
153+
pub fn a(&self) -> ::std::os::raw::c_char {
154+
unsafe { ::std::mem::transmute(self._bitfield_1.get(0usize, 1u8) as u8) }
155+
}
156+
#[inline]
157+
pub fn set_a(&mut self, val: ::std::os::raw::c_char) {
158+
unsafe {
159+
let val: u8 = ::std::mem::transmute(val);
160+
self._bitfield_1.set(0usize, 1u8, val as u64)
161+
}
162+
}
163+
#[inline]
164+
pub fn new_bitfield_1(a: ::std::os::raw::c_char) -> __BindgenBitfieldUnit<[u8; 1usize], u8> {
165+
let mut __bindgen_bitfield_unit: __BindgenBitfieldUnit<[u8; 1usize], u8> =
166+
Default::default();
167+
__bindgen_bitfield_unit.set(0usize, 1u8, {
168+
let a: u8 = unsafe { ::std::mem::transmute(a) };
169+
a as u64
170+
});
171+
__bindgen_bitfield_unit
172+
}
173+
}

tests/expectations/tests/layout_align.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ impl<T> ::std::clone::Clone for __IncompleteArrayField<T> {
125125
}
126126
}
127127
#[repr(C)]
128-
#[repr(align(8))]
129128
#[derive(Debug)]
130129
pub struct rte_kni_fifo {
131130
///< Next position to be written

tests/expectations/tests/zero-sized-array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fn bindgen_test_layout_InheritsZeroSizedArray() {
122122
);
123123
}
124124
/// And this should not get an `_address` field either.
125-
#[repr(C, packed)]
125+
#[repr(C)]
126126
#[derive(Debug, Default)]
127127
pub struct DynamicallySizedArray {
128128
pub arr: __IncompleteArrayField<::std::os::raw::c_char>,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
struct foo {
2+
char a : 1;
3+
void *b[];
4+
};

0 commit comments

Comments
 (0)