Skip to content

Commit 4f8709f

Browse files
authored
Check pointer size and emit opaque type if required (#2637)
* Check size of pointer types * Test that `__ptr32` is emitted as `u32` * Update CHANGELOG.md * Add expectation with raw pointer for clang 5 * Add expectation with raw pointer for clang 9 * Remove extra line
1 parent a960d97 commit 4f8709f

File tree

7 files changed

+155
-4
lines changed

7 files changed

+155
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@
184184
## Removed
185185
## Fixed
186186
- Allow compiling `bindgen-cli` with a static libclang.
187+
- Emit an opaque integer type for pointer types that don't have the same size
188+
as the target's pointer size.
187189
## Security
188190

189191
# 0.68.1

bindgen-tests/tests/expectations/tests/libclang-5/ptr32-has-different-size.rs

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/libclang-9/ptr32-has-different-size.rs

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/ptr32-has-different-size.rs

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// bindgen-flags: -- -fms-extensions
2+
typedef struct TEST_STRUCT {
3+
void* __ptr32 ptr_32bit;
4+
} TEST;

bindgen/codegen/error.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,36 @@ pub(crate) enum Error {
1414

1515
/// Function ABI is not supported.
1616
UnsupportedAbi(&'static str),
17+
18+
/// The pointer type size does not match the target's pointer size.
19+
InvalidPointerSize {
20+
ty_name: String,
21+
ty_size: usize,
22+
ptr_size: usize,
23+
},
1724
}
1825

1926
impl fmt::Display for Error {
2027
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21-
f.write_str(match *self {
28+
match self {
2229
Error::NoLayoutForOpaqueBlob => {
23-
"Tried to generate an opaque blob, but had no layout."
30+
"Tried to generate an opaque blob, but had no layout.".fmt(f)
2431
}
2532
Error::InstantiationOfOpaqueType => {
2633
"Instantiation of opaque template type or partial template specialization."
34+
.fmt(f)
2735
}
2836
Error::UnsupportedAbi(abi) => {
29-
return write!(
37+
write!(
3038
f,
3139
"{} ABI is not supported by the configured Rust target.",
3240
abi
3341
)
3442
}
35-
})
43+
Error::InvalidPointerSize { ty_name, ty_size, ptr_size } => {
44+
write!(f, "The {} pointer type has size {} but the current target's pointer size is {}.", ty_name, ty_size, ptr_size)
45+
}
46+
}
3647
}
3748
}
3849

bindgen/codegen/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use self::struct_layout::StructLayoutTracker;
2121
use super::BindgenOptions;
2222

2323
use crate::callbacks::{DeriveInfo, FieldInfo, TypeKind as DeriveTypeKind};
24+
use crate::codegen::error::Error;
2425
use crate::ir::analysis::{HasVtable, Sizedness};
2526
use crate::ir::annotations::{
2627
Annotations, FieldAccessorKind, FieldVisibilityKind,
@@ -3923,6 +3924,16 @@ impl TryToRustTy for Type {
39233924
}
39243925
TypeKind::Opaque => self.try_to_opaque(ctx, item),
39253926
TypeKind::Pointer(inner) | TypeKind::Reference(inner) => {
3927+
// Check that this type has the same size as the target's pointer type.
3928+
let size = self.get_layout(ctx, item).size;
3929+
if size != ctx.target_pointer_size() {
3930+
return Err(Error::InvalidPointerSize {
3931+
ty_name: self.name().unwrap_or("unknown").into(),
3932+
ty_size: size,
3933+
ptr_size: ctx.target_pointer_size(),
3934+
});
3935+
}
3936+
39263937
let is_const = ctx.resolve_type(inner).is_const();
39273938

39283939
let inner =

0 commit comments

Comments
 (0)