Skip to content

Commit 7b62008

Browse files
committed
Auto merge of #118534 - RalfJung:extern-type-size-of-val, r=WaffleLapkin
codegen: panic when trying to compute size/align of extern type The alignment is also computed when accessing a field of extern type at non-zero offset, so we also panic in that case. Previously `size_of_val` worked because the code path there assumed that "thin pointer" means "sized". But that's not true any more with extern types. The returned size and align are just blatantly wrong, so it seems better to panic than returning wrong results. We use a non-unwinding panic since code probably does not expect size_of_val to panic.
2 parents 56ee1bb + 1de6ee6 commit 7b62008

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![feature(extern_types)]
2+
3+
extern "C" {
4+
type Opaque;
5+
}
6+
7+
struct Newtype(Opaque);
8+
9+
struct S {
10+
i: i32,
11+
j: i32,
12+
a: Newtype,
13+
}
14+
15+
fn main() {
16+
let buf = [0i32; 4];
17+
18+
let x: &Newtype = unsafe { &*(&buf as *const _ as *const Newtype) };
19+
// Projecting to the newtype works, because it is always at offset 0.
20+
let _field = &x.0;
21+
22+
let x: &S = unsafe { &*(&buf as *const _ as *const S) };
23+
// Accessing sized fields is perfectly fine, even at non-zero offsets.
24+
let _field = &x.i;
25+
let _field = &x.j;
26+
// This needs to compute the field offset, but we don't know the type's alignment,
27+
// so this panics.
28+
let _field = &x.a; //~ERROR: does not have a known offset
29+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: unsupported operation: `extern type` does not have a known offset
2+
--> $DIR/extern-type-field-offset.rs:LL:CC
3+
|
4+
LL | let _field = &x.a;
5+
| ^^^^ `extern type` does not have a known offset
6+
|
7+
= help: this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support
8+
= note: BACKTRACE:
9+
= note: inside `main` at $DIR/extern-type-field-offset.rs:LL:CC
10+
11+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
12+
13+
error: aborting due to 1 previous error
14+

0 commit comments

Comments
 (0)