diff options
| author | bors <bors@rust-lang.org> | 2023-08-31 07:43:23 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-08-31 07:43:23 +0000 |
| commit | dca2d1ff00bf96d244b1bb9a2117a92ec50ac71d (patch) | |
| tree | 6f017098d5c4b2d7cbfff5737c46823a48c0c47c /src | |
| parent | 8cbd2c847b682a3ce460a79fa7576ee2eb461a00 (diff) | |
| parent | c37bd09d88fb720eb5172a833fea2d74a3d14b9a (diff) | |
| download | rust-dca2d1ff00bf96d244b1bb9a2117a92ec50ac71d.tar.gz rust-dca2d1ff00bf96d244b1bb9a2117a92ec50ac71d.zip | |
Auto merge of #115374 - RalfJung:miri-fn-abi, r=oli-obk
miri function ABI check: accept repr(transparent) wrappers as compatible
Diffstat (limited to 'src')
17 files changed, 116 insertions, 11 deletions
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::<fn(S), fn(A)>(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 + diff --git a/src/tools/miri/tests/fail/function_pointers/abi_mismatch_int_vs_float.rs b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_int_vs_float.rs new file mode 100644 index 00000000000..a1fda329e8d --- /dev/null +++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_int_vs_float.rs @@ -0,0 +1,7 @@ +fn main() { + fn f(_: f32) {} + + let g = unsafe { std::mem::transmute::<fn(f32), fn(i32)>(f) }; + + g(42) //~ ERROR: calling a function with argument of type f32 passing data of type i32 +} diff --git a/src/tools/miri/tests/fail/function_pointers/abi_mismatch_int_vs_float.stderr b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_int_vs_float.stderr new file mode 100644 index 00000000000..a53126c733e --- /dev/null +++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_int_vs_float.stderr @@ -0,0 +1,15 @@ +error: Undefined Behavior: calling a function with argument of type f32 passing data of type i32 + --> $DIR/abi_mismatch_int_vs_float.rs:LL:CC + | +LL | g(42) + | ^^^^^ calling a function with argument of type f32 passing data of type i32 + | + = 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_int_vs_float.rs:LL:CC + +note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace + +error: aborting due to previous error + diff --git a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr4.rs b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_raw_pointer.rs index f0ea5ccfe0f..f0ea5ccfe0f 100644 --- a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr4.rs +++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_raw_pointer.rs diff --git a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr4.stderr b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_raw_pointer.stderr index 610425658fe..6eacfeece14 100644 --- a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr4.stderr +++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_raw_pointer.stderr @@ -1,5 +1,5 @@ error: Undefined Behavior: calling a function with argument of type *const [i32] passing data of type *const i32 - --> $DIR/cast_fn_ptr4.rs:LL:CC + --> $DIR/abi_mismatch_raw_pointer.rs:LL:CC | LL | g(&42 as *const i32) | ^^^^^^^^^^^^^^^^^^^^ calling a function with argument of type *const [i32] passing data of type *const i32 @@ -7,7 +7,7 @@ LL | g(&42 as *const i32) = 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/cast_fn_ptr4.rs:LL:CC + = note: inside `main` at $DIR/abi_mismatch_raw_pointer.rs:LL:CC note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace diff --git a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr5.rs b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_return_type.rs index 0fdab49b94b..0fdab49b94b 100644 --- a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr5.rs +++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_return_type.rs diff --git a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr5.stderr b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_return_type.stderr index c4e08b58430..eedc1235773 100644 --- a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr5.stderr +++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_return_type.stderr @@ -1,5 +1,5 @@ error: Undefined Behavior: calling a function with return type u32 passing return place of type () - --> $DIR/cast_fn_ptr5.rs:LL:CC + --> $DIR/abi_mismatch_return_type.rs:LL:CC | LL | g() | ^^^ calling a function with return type u32 passing return place of type () @@ -7,7 +7,7 @@ LL | g() = 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/cast_fn_ptr5.rs:LL:CC + = note: inside `main` at $DIR/abi_mismatch_return_type.rs:LL:CC note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace diff --git a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr2.rs b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_simple.rs index 20384f0965b..20384f0965b 100644 --- a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr2.rs +++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_simple.rs diff --git a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr2.stderr b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_simple.stderr index 086712e0d13..bc500a90b77 100644 --- a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr2.stderr +++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_simple.stderr @@ -1,5 +1,5 @@ error: Undefined Behavior: calling a function with argument of type (i32, i32) passing data of type i32 - --> $DIR/cast_fn_ptr2.rs:LL:CC + --> $DIR/abi_mismatch_simple.rs:LL:CC | LL | g(42) | ^^^^^ calling a function with argument of type (i32, i32) passing data of type i32 @@ -7,7 +7,7 @@ LL | g(42) = 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/cast_fn_ptr2.rs:LL:CC + = note: inside `main` at $DIR/abi_mismatch_simple.rs:LL:CC note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace diff --git a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr3.rs b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_too_few_args.rs index 920fb51abb6..920fb51abb6 100644 --- a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr3.rs +++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_too_few_args.rs diff --git a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr3.stderr b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_too_few_args.stderr index 55fd7d60720..558d83bcfd2 100644 --- a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr3.stderr +++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_too_few_args.stderr @@ -1,5 +1,5 @@ error: Undefined Behavior: calling a function with fewer arguments than it requires - --> $DIR/cast_fn_ptr3.rs:LL:CC + --> $DIR/abi_mismatch_too_few_args.rs:LL:CC | LL | g() | ^^^ calling a function with fewer arguments than it requires @@ -7,7 +7,7 @@ LL | g() = 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/cast_fn_ptr3.rs:LL:CC + = note: inside `main` at $DIR/abi_mismatch_too_few_args.rs:LL:CC note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace diff --git a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr1.rs b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_too_many_args.rs index c0e96a43cc5..c0e96a43cc5 100644 --- a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr1.rs +++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_too_many_args.rs diff --git a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr1.stderr b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_too_many_args.stderr index bb2a2637959..dc12073952f 100644 --- a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr1.stderr +++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_too_many_args.stderr @@ -1,5 +1,5 @@ error: Undefined Behavior: calling a function with more arguments than it expected - --> $DIR/cast_fn_ptr1.rs:LL:CC + --> $DIR/abi_mismatch_too_many_args.rs:LL:CC | LL | g(42) | ^^^^^ calling a function with more arguments than it expected @@ -7,7 +7,7 @@ LL | g(42) = 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/cast_fn_ptr1.rs:LL:CC + = note: inside `main` at $DIR/abi_mismatch_too_many_args.rs:LL:CC note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace diff --git a/src/tools/miri/tests/fail/function_pointers/abi_mismatch_vector.rs b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_vector.rs new file mode 100644 index 00000000000..80f357b61ba --- /dev/null +++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_vector.rs @@ -0,0 +1,11 @@ +#![feature(portable_simd)] +use std::simd; + +fn main() { + fn f(_: simd::u32x8) {} + + // These two vector types have the same size but are still not compatible. + let g = unsafe { std::mem::transmute::<fn(simd::u32x8), fn(simd::u64x4)>(f) }; + + g(Default::default()) //~ ERROR: calling a function with argument of type std::simd::Simd<u32, 8> passing data of type std::simd::Simd<u64, 4> +} diff --git a/src/tools/miri/tests/fail/function_pointers/abi_mismatch_vector.stderr b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_vector.stderr new file mode 100644 index 00000000000..7dcca1e85b8 --- /dev/null +++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_vector.stderr @@ -0,0 +1,15 @@ +error: Undefined Behavior: calling a function with argument of type std::simd::Simd<u32, 8> passing data of type std::simd::Simd<u64, 4> + --> $DIR/abi_mismatch_vector.rs:LL:CC + | +LL | g(Default::default()) + | ^^^^^^^^^^^^^^^^^^^^^ calling a function with argument of type std::simd::Simd<u32, 8> passing data of type std::simd::Simd<u64, 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_vector.rs:LL:CC + +note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace + +error: aborting due to previous error + diff --git a/src/tools/miri/tests/pass/function_calls/abi_compat.rs b/src/tools/miri/tests/pass/function_calls/abi_compat.rs index 67b87b46bd9..0786450f751 100644 --- a/src/tools/miri/tests/pass/function_calls/abi_compat.rs +++ b/src/tools/miri/tests/pass/function_calls/abi_compat.rs @@ -1,5 +1,7 @@ +#![feature(portable_simd)] use std::num; use std::mem; +use std::simd; fn test_abi_compat<T, U>(t: T, u: U) { fn id<T>(x: T) -> T { x } @@ -15,6 +17,23 @@ fn test_abi_compat<T, U>(t: T, u: U) { drop(f(t)); } +/// Ensure that `T` is compatible with various repr(transparent) wrappers around `T`. +fn test_abi_newtype<T: Copy>(t: T) { + #[repr(transparent)] + struct Wrapper1<T>(T); + #[repr(transparent)] + struct Wrapper2<T>(T, ()); + #[repr(transparent)] + struct Wrapper2a<T>((), T); + #[repr(transparent)] + struct Wrapper3<T>(T, [u8; 0]); + + test_abi_compat(t, Wrapper1(t)); + test_abi_compat(t, Wrapper2(t, ())); + test_abi_compat(t, Wrapper2a((), t)); + test_abi_compat(t, Wrapper3(t, [])); +} + fn main() { test_abi_compat(0u32, 'x'); test_abi_compat(&0u32, &([true; 4], [0u32; 0])); @@ -22,6 +41,13 @@ fn main() { test_abi_compat(42u32, num::NonZeroU32::new(1).unwrap()); test_abi_compat(0u32, Some(num::NonZeroU32::new(1).unwrap())); test_abi_compat(0u32, 0i32); - // Note that `bool` and `u8` are *not* compatible! + test_abi_compat(simd::u32x8::splat(1), simd::i32x8::splat(1)); + // Note that `bool` and `u8` are *not* compatible, at least on x86-64! // One of them has `arg_ext: Zext`, the other does not. + + test_abi_newtype(0u32); + test_abi_newtype(0f32); + test_abi_newtype((0u32, 1u32, 2u32)); + test_abi_newtype([0u32, 1u32, 2u32]); + test_abi_newtype([0i32; 0]); } |
