Skip to content

Commit c7e3b3f

Browse files
committed
do not allow ABI mismatches inside repr(C) types
1 parent 2f19122 commit c7e3b3f

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

library/core/src/primitive_docs.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1575,8 +1575,6 @@ mod prim_ref {}
15751575
/// Furthermore, ABI compatibility satisfies the following general properties:
15761576
///
15771577
/// - Every type is ABI-compatible with itself.
1578-
/// - If `T1` and `T2` are ABI-compatible, then two `repr(C)` types that only differ because one
1579-
/// field type was changed from `T1` to `T2` are ABI-compatible.
15801578
/// - If `T1` and `T2` are ABI-compatible and `T2` and `T3` are ABI-compatible, then so are `T1` and
15811579
/// `T3` (i.e., ABI-compatibility is transitive).
15821580
/// - If `T1` and `T2` are ABI-compatible, then so are `T2` and `T1` (i.e., ABI-compatibility is
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use std::num::*;
2+
3+
#[repr(C)]
4+
struct S1(NonZeroI32);
5+
6+
#[repr(C)]
7+
struct S2(i32);
8+
9+
fn callee(_s: S2) {}
10+
11+
fn main() {
12+
let fnptr: fn(S2) = callee;
13+
let fnptr: fn(S1) = unsafe { std::mem::transmute(fnptr) };
14+
fnptr(S1(NonZeroI32::new(1).unwrap()));
15+
//~^ ERROR: calling a function with argument of type S2 passing data of type S1
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: Undefined Behavior: calling a function with argument of type S2 passing data of type S1
2+
--> $DIR/abi_mismatch_repr_C.rs:LL:CC
3+
|
4+
LL | fnptr(S1(NonZeroI32::new(1).unwrap()));
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling a function with argument of type S2 passing data of type S1
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= help: this means these two types are not *guaranteed* to be ABI-compatible across all targets
10+
= help: if you think this code should be accepted anyway, please report an issue
11+
= note: BACKTRACE:
12+
= note: inside `main` at $DIR/abi_mismatch_repr_C.rs:LL:CC
13+
14+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
15+
16+
error: aborting due to 1 previous error
17+

0 commit comments

Comments
 (0)