summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2023-12-17 09:34:25 +0100
committerRalf Jung <post@ralfj.de>2023-12-17 09:34:25 +0100
commitc7e3b3f84caf8b34773deba7db37ee2bc5fd23cf (patch)
treed35348579e9c65e67347ff4de3c8127ca5960187
parent2f19122f73027219a152e0c2ff35cebab940876c (diff)
downloadrust-c7e3b3f84caf8b34773deba7db37ee2bc5fd23cf.tar.gz
rust-c7e3b3f84caf8b34773deba7db37ee2bc5fd23cf.zip
do not allow ABI mismatches inside repr(C) types
-rw-r--r--library/core/src/primitive_docs.rs2
-rw-r--r--src/tools/miri/tests/fail/function_pointers/abi_mismatch_repr_C.rs16
-rw-r--r--src/tools/miri/tests/fail/function_pointers/abi_mismatch_repr_C.stderr17
3 files changed, 33 insertions, 2 deletions
diff --git a/library/core/src/primitive_docs.rs b/library/core/src/primitive_docs.rs
index 588f9b865b0..99208fba670 100644
--- a/library/core/src/primitive_docs.rs
+++ b/library/core/src/primitive_docs.rs
@@ -1575,8 +1575,6 @@ mod prim_ref {}
 /// Furthermore, ABI compatibility satisfies the following general properties:
 ///
 /// - Every type is ABI-compatible with itself.
-/// - If `T1` and `T2` are ABI-compatible, then two `repr(C)` types that only differ because one
-///   field type was changed from `T1` to `T2` are ABI-compatible.
 /// - If `T1` and `T2` are ABI-compatible and `T2` and `T3` are ABI-compatible, then so are `T1` and
 ///   `T3` (i.e., ABI-compatibility is transitive).
 /// - If `T1` and `T2` are ABI-compatible, then so are `T2` and `T1` (i.e., ABI-compatibility is
diff --git a/src/tools/miri/tests/fail/function_pointers/abi_mismatch_repr_C.rs b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_repr_C.rs
new file mode 100644
index 00000000000..2cf4e044777
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_repr_C.rs
@@ -0,0 +1,16 @@
+use std::num::*;
+
+#[repr(C)]
+struct S1(NonZeroI32);
+
+#[repr(C)]
+struct S2(i32);
+
+fn callee(_s: S2) {}
+
+fn main() {
+    let fnptr: fn(S2) = callee;
+    let fnptr: fn(S1) = unsafe { std::mem::transmute(fnptr) };
+    fnptr(S1(NonZeroI32::new(1).unwrap()));
+    //~^ ERROR: calling a function with argument of type S2 passing data of type S1
+}
diff --git a/src/tools/miri/tests/fail/function_pointers/abi_mismatch_repr_C.stderr b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_repr_C.stderr
new file mode 100644
index 00000000000..6d42bea9da9
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_repr_C.stderr
@@ -0,0 +1,17 @@
+error: Undefined Behavior: calling a function with argument of type S2 passing data of type S1
+  --> $DIR/abi_mismatch_repr_C.rs:LL:CC
+   |
+LL |     fnptr(S1(NonZeroI32::new(1).unwrap()));
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling a function with argument of type S2 passing data of type S1
+   |
+   = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
+   = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
+   = help: this means these two types are not *guaranteed* to be ABI-compatible across all targets
+   = help: if you think this code should be accepted anyway, please report an issue
+   = note: BACKTRACE:
+   = note: inside `main` at $DIR/abi_mismatch_repr_C.rs:LL:CC
+
+note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
+
+error: aborting due to 1 previous error
+