From c37bd09d88fb720eb5172a833fea2d74a3d14b9a Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 30 Aug 2023 12:26:17 +0200 Subject: miri function ABI check: specifically look for repr(transparent) --- .../function_pointers/abi_mismatch_array_vs_struct.rs | 16 ++++++++++++++++ .../abi_mismatch_array_vs_struct.stderr | 15 +++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/tools/miri/tests/fail/function_pointers/abi_mismatch_array_vs_struct.rs create mode 100644 src/tools/miri/tests/fail/function_pointers/abi_mismatch_array_vs_struct.stderr (limited to 'src/tools/miri/tests/fail/function_pointers') diff --git a/src/tools/miri/tests/fail/function_pointers/abi_mismatch_array_vs_struct.rs b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_array_vs_struct.rs new file mode 100644 index 00000000000..415e91b250f --- /dev/null +++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_array_vs_struct.rs @@ -0,0 +1,16 @@ +#![feature(portable_simd)] + +// Some targets treat arrays and structs very differently. We would probably catch that on those +// targets since we check the `PassMode`; here we ensure that we catch it on *all* targets +// (in particular, on x86-64 the pass mode is `Indirect` for both of these). +struct S(i32, i32, i32, i32); +type A = [i32; 4]; + +fn main() { + fn f(_: S) {} + + // These two types have the same size but are still not compatible. + let g = unsafe { std::mem::transmute::(f) }; + + g(Default::default()) //~ ERROR: calling a function with argument of type S passing data of type [i32; 4] +} diff --git a/src/tools/miri/tests/fail/function_pointers/abi_mismatch_array_vs_struct.stderr b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_array_vs_struct.stderr new file mode 100644 index 00000000000..50d4228c111 --- /dev/null +++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_array_vs_struct.stderr @@ -0,0 +1,15 @@ +error: Undefined Behavior: calling a function with argument of type S passing data of type [i32; 4] + --> $DIR/abi_mismatch_array_vs_struct.rs:LL:CC + | +LL | g(Default::default()) + | ^^^^^^^^^^^^^^^^^^^^^ calling a function with argument of type S passing data of type [i32; 4] + | + = 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 + = note: BACKTRACE: + = note: inside `main` at $DIR/abi_mismatch_array_vs_struct.rs:LL:CC + +note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace + +error: aborting due to previous error + -- cgit 1.4.1-3-g733a5