From 66c93ac8bad2e17a11debffef6776049cade50b5 Mon Sep 17 00:00:00 2001 From: Ramon de C Valle Date: Mon, 12 Aug 2024 14:59:50 -0700 Subject: CFI: Move CFI ui tests to cfi directory Moves the CFI ui tests to the cfi directory and removes the cfi prefix from tests file names similarly to how the cfi codegen tests are organized. --- .../cfi-assoc-ty-lifetime-issue-123053.rs | 38 --------- tests/ui/sanitizer/cfi-async-closures.rs | 33 -------- tests/ui/sanitizer/cfi-can-reveal-opaques.rs | 44 ----------- .../cfi-canonical-jump-tables-requires-cfi.rs | 8 -- .../cfi-canonical-jump-tables-requires-cfi.stderr | 4 - tests/ui/sanitizer/cfi-closures.rs | 90 ---------------------- tests/ui/sanitizer/cfi-complex-receiver.rs | 47 ----------- tests/ui/sanitizer/cfi-coroutine.rs | 67 ---------------- tests/ui/sanitizer/cfi-drop-in-place.rs | 20 ----- tests/ui/sanitizer/cfi-drop-no-principal.rs | 21 ----- tests/ui/sanitizer/cfi-fn-ptr.rs | 61 --------------- .../sanitizer/cfi-generalize-pointers-attr-cfg.rs | 11 --- .../cfi-generalize-pointers-requires-cfi.rs | 9 --- .../cfi-generalize-pointers-requires-cfi.stderr | 4 - .../ui/sanitizer/cfi-invalid-attr-cfi-encoding.rs | 11 --- .../sanitizer/cfi-invalid-attr-cfi-encoding.stderr | 8 -- .../cfi-is-incompatible-with-kcfi.aarch64.stderr | 6 -- .../ui/sanitizer/cfi-is-incompatible-with-kcfi.rs | 12 --- .../cfi-is-incompatible-with-kcfi.x86_64.stderr | 6 -- .../sanitizer/cfi-normalize-integers-attr-cfg.rs | 11 --- .../cfi-normalize-integers-requires-cfi.rs | 9 --- .../cfi-normalize-integers-requires-cfi.stderr | 4 - tests/ui/sanitizer/cfi-requires-lto.rs | 8 -- tests/ui/sanitizer/cfi-requires-lto.stderr | 4 - tests/ui/sanitizer/cfi-self-ref.rs | 38 --------- tests/ui/sanitizer/cfi-sized-associated-ty.rs | 38 --------- tests/ui/sanitizer/cfi-supertraits.rs | 79 ------------------- tests/ui/sanitizer/cfi-virtual-auto.rs | 27 ------- ...-with-rustc-lto-requires-single-codegen-unit.rs | 8 -- ...h-rustc-lto-requires-single-codegen-unit.stderr | 4 - .../cfi/assoc-ty-lifetime-issue-123053.rs | 38 +++++++++ tests/ui/sanitizer/cfi/async-closures.rs | 33 ++++++++ tests/ui/sanitizer/cfi/can-reveal-opaques.rs | 44 +++++++++++ .../cfi/canonical-jump-tables-requires-cfi.rs | 8 ++ .../cfi/canonical-jump-tables-requires-cfi.stderr | 4 + tests/ui/sanitizer/cfi/closures.rs | 90 ++++++++++++++++++++++ tests/ui/sanitizer/cfi/complex-receiver.rs | 47 +++++++++++ tests/ui/sanitizer/cfi/coroutine.rs | 67 ++++++++++++++++ tests/ui/sanitizer/cfi/drop-in-place.rs | 20 +++++ tests/ui/sanitizer/cfi/drop-no-principal.rs | 21 +++++ tests/ui/sanitizer/cfi/fn-ptr.rs | 61 +++++++++++++++ .../sanitizer/cfi/generalize-pointers-attr-cfg.rs | 11 +++ .../cfi/generalize-pointers-requires-cfi.rs | 9 +++ .../cfi/generalize-pointers-requires-cfi.stderr | 4 + tests/ui/sanitizer/cfi/invalid-attr-encoding.rs | 11 +++ .../ui/sanitizer/cfi/invalid-attr-encoding.stderr | 8 ++ .../cfi/is-incompatible-with-kcfi.aarch64.stderr | 6 ++ .../ui/sanitizer/cfi/is-incompatible-with-kcfi.rs | 12 +++ .../cfi/is-incompatible-with-kcfi.x86_64.stderr | 6 ++ .../sanitizer/cfi/normalize-integers-attr-cfg.rs | 11 +++ .../cfi/normalize-integers-requires-cfi.rs | 9 +++ .../cfi/normalize-integers-requires-cfi.stderr | 4 + tests/ui/sanitizer/cfi/requires-lto.rs | 8 ++ tests/ui/sanitizer/cfi/requires-lto.stderr | 4 + tests/ui/sanitizer/cfi/self-ref.rs | 38 +++++++++ tests/ui/sanitizer/cfi/sized-associated-ty.rs | 38 +++++++++ tests/ui/sanitizer/cfi/supertraits.rs | 79 +++++++++++++++++++ tests/ui/sanitizer/cfi/virtual-auto.rs | 27 +++++++ .../with-rustc-lto-requires-single-codegen-unit.rs | 8 ++ ...h-rustc-lto-requires-single-codegen-unit.stderr | 4 + 60 files changed, 730 insertions(+), 730 deletions(-) delete mode 100644 tests/ui/sanitizer/cfi-assoc-ty-lifetime-issue-123053.rs delete mode 100644 tests/ui/sanitizer/cfi-async-closures.rs delete mode 100644 tests/ui/sanitizer/cfi-can-reveal-opaques.rs delete mode 100644 tests/ui/sanitizer/cfi-canonical-jump-tables-requires-cfi.rs delete mode 100644 tests/ui/sanitizer/cfi-canonical-jump-tables-requires-cfi.stderr delete mode 100644 tests/ui/sanitizer/cfi-closures.rs delete mode 100644 tests/ui/sanitizer/cfi-complex-receiver.rs delete mode 100644 tests/ui/sanitizer/cfi-coroutine.rs delete mode 100644 tests/ui/sanitizer/cfi-drop-in-place.rs delete mode 100644 tests/ui/sanitizer/cfi-drop-no-principal.rs delete mode 100644 tests/ui/sanitizer/cfi-fn-ptr.rs delete mode 100644 tests/ui/sanitizer/cfi-generalize-pointers-attr-cfg.rs delete mode 100644 tests/ui/sanitizer/cfi-generalize-pointers-requires-cfi.rs delete mode 100644 tests/ui/sanitizer/cfi-generalize-pointers-requires-cfi.stderr delete mode 100644 tests/ui/sanitizer/cfi-invalid-attr-cfi-encoding.rs delete mode 100644 tests/ui/sanitizer/cfi-invalid-attr-cfi-encoding.stderr delete mode 100644 tests/ui/sanitizer/cfi-is-incompatible-with-kcfi.aarch64.stderr delete mode 100644 tests/ui/sanitizer/cfi-is-incompatible-with-kcfi.rs delete mode 100644 tests/ui/sanitizer/cfi-is-incompatible-with-kcfi.x86_64.stderr delete mode 100644 tests/ui/sanitizer/cfi-normalize-integers-attr-cfg.rs delete mode 100644 tests/ui/sanitizer/cfi-normalize-integers-requires-cfi.rs delete mode 100644 tests/ui/sanitizer/cfi-normalize-integers-requires-cfi.stderr delete mode 100644 tests/ui/sanitizer/cfi-requires-lto.rs delete mode 100644 tests/ui/sanitizer/cfi-requires-lto.stderr delete mode 100644 tests/ui/sanitizer/cfi-self-ref.rs delete mode 100644 tests/ui/sanitizer/cfi-sized-associated-ty.rs delete mode 100644 tests/ui/sanitizer/cfi-supertraits.rs delete mode 100644 tests/ui/sanitizer/cfi-virtual-auto.rs delete mode 100644 tests/ui/sanitizer/cfi-with-rustc-lto-requires-single-codegen-unit.rs delete mode 100644 tests/ui/sanitizer/cfi-with-rustc-lto-requires-single-codegen-unit.stderr create mode 100644 tests/ui/sanitizer/cfi/assoc-ty-lifetime-issue-123053.rs create mode 100644 tests/ui/sanitizer/cfi/async-closures.rs create mode 100644 tests/ui/sanitizer/cfi/can-reveal-opaques.rs create mode 100644 tests/ui/sanitizer/cfi/canonical-jump-tables-requires-cfi.rs create mode 100644 tests/ui/sanitizer/cfi/canonical-jump-tables-requires-cfi.stderr create mode 100644 tests/ui/sanitizer/cfi/closures.rs create mode 100644 tests/ui/sanitizer/cfi/complex-receiver.rs create mode 100644 tests/ui/sanitizer/cfi/coroutine.rs create mode 100644 tests/ui/sanitizer/cfi/drop-in-place.rs create mode 100644 tests/ui/sanitizer/cfi/drop-no-principal.rs create mode 100644 tests/ui/sanitizer/cfi/fn-ptr.rs create mode 100644 tests/ui/sanitizer/cfi/generalize-pointers-attr-cfg.rs create mode 100644 tests/ui/sanitizer/cfi/generalize-pointers-requires-cfi.rs create mode 100644 tests/ui/sanitizer/cfi/generalize-pointers-requires-cfi.stderr create mode 100644 tests/ui/sanitizer/cfi/invalid-attr-encoding.rs create mode 100644 tests/ui/sanitizer/cfi/invalid-attr-encoding.stderr create mode 100644 tests/ui/sanitizer/cfi/is-incompatible-with-kcfi.aarch64.stderr create mode 100644 tests/ui/sanitizer/cfi/is-incompatible-with-kcfi.rs create mode 100644 tests/ui/sanitizer/cfi/is-incompatible-with-kcfi.x86_64.stderr create mode 100644 tests/ui/sanitizer/cfi/normalize-integers-attr-cfg.rs create mode 100644 tests/ui/sanitizer/cfi/normalize-integers-requires-cfi.rs create mode 100644 tests/ui/sanitizer/cfi/normalize-integers-requires-cfi.stderr create mode 100644 tests/ui/sanitizer/cfi/requires-lto.rs create mode 100644 tests/ui/sanitizer/cfi/requires-lto.stderr create mode 100644 tests/ui/sanitizer/cfi/self-ref.rs create mode 100644 tests/ui/sanitizer/cfi/sized-associated-ty.rs create mode 100644 tests/ui/sanitizer/cfi/supertraits.rs create mode 100644 tests/ui/sanitizer/cfi/virtual-auto.rs create mode 100644 tests/ui/sanitizer/cfi/with-rustc-lto-requires-single-codegen-unit.rs create mode 100644 tests/ui/sanitizer/cfi/with-rustc-lto-requires-single-codegen-unit.stderr diff --git a/tests/ui/sanitizer/cfi-assoc-ty-lifetime-issue-123053.rs b/tests/ui/sanitizer/cfi-assoc-ty-lifetime-issue-123053.rs deleted file mode 100644 index dd604b6bf7d..00000000000 --- a/tests/ui/sanitizer/cfi-assoc-ty-lifetime-issue-123053.rs +++ /dev/null @@ -1,38 +0,0 @@ -// Regression test for issue 123053, where associated types with lifetimes caused generation of the -// trait object type to fail, causing an ICE. -// -//@ needs-sanitizer-cfi -//@ compile-flags: -Ccodegen-units=1 -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi --edition=2021 -//@ no-prefer-dynamic -//@ only-x86_64-unknown-linux-gnu -//@ build-pass - -trait Iterable { - type Item<'a> - where - Self: 'a; - type Iter<'a>: Iterator> - where - Self: 'a; - - fn iter<'a>(&'a self) -> Self::Iter<'a>; -} - -impl Iterable for [T] { - type Item<'a> = as Iterator>::Item where T: 'a; - type Iter<'a> = std::slice::Iter<'a, T> where T: 'a; - - fn iter<'a>(&'a self) -> Self::Iter<'a> { - self.iter() - } -} - -fn get_first<'a, I: Iterable + ?Sized>(it: &'a I) -> Option> { - it.iter().next() -} - -fn main() { - let v = vec![1, 2, 3]; - - assert_eq!(Some(&1), get_first(&*v)); -} diff --git a/tests/ui/sanitizer/cfi-async-closures.rs b/tests/ui/sanitizer/cfi-async-closures.rs deleted file mode 100644 index d94f2992d84..00000000000 --- a/tests/ui/sanitizer/cfi-async-closures.rs +++ /dev/null @@ -1,33 +0,0 @@ -// Check various forms of dynamic closure calls - -//@ edition: 2021 -//@ revisions: cfi kcfi -// FIXME(#122848) Remove only-linux once OSX CFI binaries work -//@ only-linux -//@ [cfi] needs-sanitizer-cfi -//@ [kcfi] needs-sanitizer-kcfi -//@ compile-flags: -C target-feature=-crt-static -//@ [cfi] compile-flags: -C codegen-units=1 -C lto -C prefer-dynamic=off -C opt-level=0 -//@ [cfi] compile-flags: -Z sanitizer=cfi -//@ [kcfi] compile-flags: -Z sanitizer=kcfi -//@ [kcfi] compile-flags: -C panic=abort -Z panic-abort-tests -C prefer-dynamic=off -//@ run-pass - -#![feature(async_closure)] -#![feature(async_fn_traits)] - -use std::ops::AsyncFn; - -#[inline(never)] -fn identity(x: T) -> T { x } - -// We can't actually create a `dyn AsyncFn()`, because it's not object-safe, but we should check -// that we don't bug out when we encounter one. - -fn main() { - let f = identity(async || ()); - let _ = f.async_call(()); - let _ = f(); - let g: Box _> = Box::new(f) as _; - let _ = g(); -} diff --git a/tests/ui/sanitizer/cfi-can-reveal-opaques.rs b/tests/ui/sanitizer/cfi-can-reveal-opaques.rs deleted file mode 100644 index 55988a62a8c..00000000000 --- a/tests/ui/sanitizer/cfi-can-reveal-opaques.rs +++ /dev/null @@ -1,44 +0,0 @@ -//@ needs-sanitizer-cfi -//@ compile-flags: -Ccodegen-units=1 -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi -//@ no-prefer-dynamic -//@ only-x86_64-unknown-linux-gnu -//@ build-pass - -// See comment below for why this test exists. - -trait Tr { - type Projection; -} - -impl Tr for F -where - F: Fn() -> U -{ - type Projection = U; -} - -fn test, U>(b: B) -> B::Projection -{ - todo!() -} - -fn main() { - fn rpit_fn() -> impl Sized {} - - // When CFI runs, it tries to compute the signature of the call. This - // ends up giving us a signature of: - // `fn test::() -> >::Projection`, - // where `rpit_fn` is the ZST FnDef for the function. However, we were - // previously using a Reveal::UserFacing param-env. This means that the - // `>::Projection` return type is impossible to normalize, - // since it would require proving `rpit_fn: Fn() -> ()`, but we cannot - // prove that the `impl Sized` opaque is `()` with a user-facing param-env. - // This leads to a normalization error, and then an ICE. - // - // Side-note: - // So why is the second generic of `test` "`()`", and not the - // `impl Sized` since we inferred it from the return type of `rpit_fn` - // during typeck? Well, that's because we're using the generics from the - // terminator of the MIR, which has had the RevealAll pass performed on it. - let _ = test(rpit_fn); -} diff --git a/tests/ui/sanitizer/cfi-canonical-jump-tables-requires-cfi.rs b/tests/ui/sanitizer/cfi-canonical-jump-tables-requires-cfi.rs deleted file mode 100644 index 10c5bf6ea5e..00000000000 --- a/tests/ui/sanitizer/cfi-canonical-jump-tables-requires-cfi.rs +++ /dev/null @@ -1,8 +0,0 @@ -// Verifies that `-Zsanitizer-cfi-canonical-jump-tables` requires `-Zsanitizer=cfi`. -// -//@ needs-sanitizer-cfi -//@ compile-flags: -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer-cfi-canonical-jump-tables=false - -#![feature(no_core)] -#![no_core] -#![no_main] diff --git a/tests/ui/sanitizer/cfi-canonical-jump-tables-requires-cfi.stderr b/tests/ui/sanitizer/cfi-canonical-jump-tables-requires-cfi.stderr deleted file mode 100644 index de67d6a6b7f..00000000000 --- a/tests/ui/sanitizer/cfi-canonical-jump-tables-requires-cfi.stderr +++ /dev/null @@ -1,4 +0,0 @@ -error: `-Zsanitizer-cfi-canonical-jump-tables` requires `-Zsanitizer=cfi` - -error: aborting due to 1 previous error - diff --git a/tests/ui/sanitizer/cfi-closures.rs b/tests/ui/sanitizer/cfi-closures.rs deleted file mode 100644 index 9f9002da674..00000000000 --- a/tests/ui/sanitizer/cfi-closures.rs +++ /dev/null @@ -1,90 +0,0 @@ -// Check various forms of dynamic closure calls - -//@ revisions: cfi kcfi -// FIXME(#122848) Remove only-linux once OSX CFI binaries work -//@ only-linux -//@ [cfi] needs-sanitizer-cfi -//@ [kcfi] needs-sanitizer-kcfi -//@ compile-flags: -C target-feature=-crt-static -//@ [cfi] compile-flags: -C codegen-units=1 -C lto -C prefer-dynamic=off -C opt-level=0 -//@ [cfi] compile-flags: -Z sanitizer=cfi -//@ [kcfi] compile-flags: -Z sanitizer=kcfi -//@ [kcfi] compile-flags: -C panic=abort -Z panic-abort-tests -C prefer-dynamic=off -//@ compile-flags: --test -//@ run-pass - -#![feature(fn_traits)] -#![feature(unboxed_closures)] - -fn foo<'a, T>() -> Box &'a T> { - Box::new(|x| x) -} - -#[test] -fn dyn_fn_with_params() { - let x = 3; - let f = foo(); - f(&x); - // FIXME remove once drops are working. - std::mem::forget(f); -} - -#[test] -fn call_fn_trait() { - let f: &(dyn Fn()) = &(|| {}) as _; - f.call(()); -} - -#[test] -fn fn_ptr_cast() { - let f: &fn() = &((|| ()) as _); - f(); -} - -fn use_fnmut(mut f: F) { - f() -} - -#[test] -fn fn_to_fnmut() { - let f: &(dyn Fn()) = &(|| {}) as _; - use_fnmut(f); -} - -fn hrtb_helper(f: &dyn for<'a> Fn(&'a usize)) { - f(&10) -} - -#[test] -fn hrtb_fn() { - hrtb_helper((&|x: &usize| println!("{}", *x)) as _) -} - -#[test] -fn fnonce() { - let f: Box = Box::new(|| {}) as _; - f(); -} - -fn use_closure(call: extern "rust-call" fn(&C, ()) -> i32, f: &C) -> i32 { - call(f, ()) -} - -#[test] -fn closure_addr_taken() { - let x = 3i32; - let f = || x; - let call = Fn::<()>::call; - use_closure(call, &f); -} - -fn use_closure_once(call: extern "rust-call" fn(C, ()) -> i32, f: C) -> i32 { - call(f, ()) -} - -#[test] -fn closure_once_addr_taken() { - let g = || 3; - let call2 = FnOnce::<()>::call_once; - use_closure_once(call2, g); -} diff --git a/tests/ui/sanitizer/cfi-complex-receiver.rs b/tests/ui/sanitizer/cfi-complex-receiver.rs deleted file mode 100644 index c7b45a775ca..00000000000 --- a/tests/ui/sanitizer/cfi-complex-receiver.rs +++ /dev/null @@ -1,47 +0,0 @@ -// Check that more complex receivers work: -// * Arc as for custom receivers -// * &dyn Bar for type constraints - -//@ revisions: cfi kcfi -// FIXME(#122848) Remove only-linux once OSX CFI binaries work -//@ only-linux -//@ [cfi] needs-sanitizer-cfi -//@ [kcfi] needs-sanitizer-kcfi -//@ compile-flags: -C target-feature=-crt-static -//@ [cfi] compile-flags: -C codegen-units=1 -C lto -C prefer-dynamic=off -C opt-level=0 -//@ [cfi] compile-flags: -Z sanitizer=cfi -//@ [kcfi] compile-flags: -Z sanitizer=kcfi -//@ [kcfi] compile-flags: -C panic=abort -C prefer-dynamic=off -//@ run-pass - -use std::sync::Arc; - -trait Foo { - fn foo(self: Arc); -} - -struct FooImpl; - -impl Foo for FooImpl { - fn foo(self: Arc) {} -} - -trait Bar { - type T; - fn bar(&self) -> Self::T; -} - -struct BarImpl; - -impl Bar for BarImpl { - type T = i32; - fn bar(&self) -> Self::T { 7 } -} - -fn main() { - let foo: Arc = Arc::new(FooImpl); - foo.foo(); - - let bar: &dyn Bar = &BarImpl; - assert_eq!(bar.bar(), 7); -} diff --git a/tests/ui/sanitizer/cfi-coroutine.rs b/tests/ui/sanitizer/cfi-coroutine.rs deleted file mode 100644 index ad994fcf737..00000000000 --- a/tests/ui/sanitizer/cfi-coroutine.rs +++ /dev/null @@ -1,67 +0,0 @@ -// Verifies that we can call dynamic coroutines - -//@ revisions: cfi kcfi -// FIXME(#122848) Remove only-linux once OSX CFI binaries work -//@ only-linux -//@ edition: 2024 -//@ [cfi] needs-sanitizer-cfi -//@ [kcfi] needs-sanitizer-kcfi -//@ compile-flags: -C target-feature=-crt-static -//@ [cfi] compile-flags: -C codegen-units=1 -C lto -C prefer-dynamic=off -C opt-level=0 -//@ [cfi] compile-flags: -Z sanitizer=cfi -//@ [kcfi] compile-flags: -Z sanitizer=kcfi -//@ [kcfi] compile-flags: -C panic=abort -Z panic-abort-tests -C prefer-dynamic=off -//@ compile-flags: --test -Z unstable-options -//@ run-pass - -#![feature(coroutines, stmt_expr_attributes)] -#![feature(coroutine_trait)] -#![feature(noop_waker)] -#![feature(gen_blocks)] -#![feature(async_iterator)] - -use std::ops::{Coroutine, CoroutineState}; -use std::pin::{pin, Pin}; -use std::task::{Context, Poll, Waker}; -use std::async_iter::AsyncIterator; - -#[test] -fn general_coroutine() { - let mut coro = #[coroutine] |x: i32| { - yield x; - "done" - }; - let mut abstract_coro: Pin<&mut dyn Coroutine> = pin!(coro); - assert_eq!(abstract_coro.as_mut().resume(2), CoroutineState::Yielded(2)); - assert_eq!(abstract_coro.as_mut().resume(0), CoroutineState::Complete("done")); -} - -async fn async_fn() {} - -#[test] -fn async_coroutine() { - let f: fn() -> Pin>> = || Box::pin(async_fn()); - let _ = async { f().await; }; - assert_eq!(f().as_mut().poll(&mut Context::from_waker(Waker::noop())), Poll::Ready(())); -} - -async gen fn async_gen_fn() -> u8 { - yield 5; -} - -#[test] -fn async_gen_coroutine() { - let f: fn() -> Pin>> = || Box::pin(async_gen_fn()); - assert_eq!(f().as_mut().poll_next(&mut Context::from_waker(Waker::noop())), - Poll::Ready(Some(5))); -} - -gen fn gen_fn() -> u8 { - yield 6; -} - -#[test] -fn gen_coroutine() { - let f: fn() -> Box> = || Box::new(gen_fn()); - assert_eq!(f().next(), Some(6)); -} diff --git a/tests/ui/sanitizer/cfi-drop-in-place.rs b/tests/ui/sanitizer/cfi-drop-in-place.rs deleted file mode 100644 index 8ce2c432602..00000000000 --- a/tests/ui/sanitizer/cfi-drop-in-place.rs +++ /dev/null @@ -1,20 +0,0 @@ -// Verifies that drops can be called on arbitrary trait objects. -// -// FIXME(#122848): Remove only-linux when fixed. -//@ only-linux -//@ needs-sanitizer-cfi -//@ compile-flags: -Clto -Copt-level=0 -Cprefer-dynamic=off -Ctarget-feature=-crt-static -Zsanitizer=cfi -//@ run-pass - -struct EmptyDrop; - -struct NonEmptyDrop; - -impl Drop for NonEmptyDrop { - fn drop(&mut self) {} -} - -fn main() { - let _ = Box::new(EmptyDrop) as Box; - let _ = Box::new(NonEmptyDrop) as Box; -} diff --git a/tests/ui/sanitizer/cfi-drop-no-principal.rs b/tests/ui/sanitizer/cfi-drop-no-principal.rs deleted file mode 100644 index c1c88c8c71c..00000000000 --- a/tests/ui/sanitizer/cfi-drop-no-principal.rs +++ /dev/null @@ -1,21 +0,0 @@ -// Check that dropping a trait object without a principal trait succeeds - -//@ needs-sanitizer-cfi -// FIXME(#122848) Remove only-linux once OSX CFI binaries works -//@ only-linux -//@ compile-flags: --crate-type=bin -Cprefer-dynamic=off -Clto -Zsanitizer=cfi -//@ compile-flags: -C target-feature=-crt-static -C codegen-units=1 -C opt-level=0 -// FIXME(#118761) Should be run-pass once the labels on drop are compatible. -// This test is being landed ahead of that to test that the compiler doesn't ICE while labeling the -// callsite for a drop, but the vtable doesn't have the correct label yet. -//@ build-pass - -struct CustomDrop; - -impl Drop for CustomDrop { - fn drop(&mut self) {} -} - -fn main() { - let _ = Box::new(CustomDrop) as Box; -} diff --git a/tests/ui/sanitizer/cfi-fn-ptr.rs b/tests/ui/sanitizer/cfi-fn-ptr.rs deleted file mode 100644 index 505b4b8e7f0..00000000000 --- a/tests/ui/sanitizer/cfi-fn-ptr.rs +++ /dev/null @@ -1,61 +0,0 @@ -// Verifies that casting to a function pointer works. - -//@ revisions: cfi kcfi -// FIXME(#122848) Remove only-linux once OSX CFI binaries work -//@ only-linux -//@ [cfi] needs-sanitizer-cfi -//@ [kcfi] needs-sanitizer-kcfi -//@ compile-flags: -C target-feature=-crt-static -//@ [cfi] compile-flags: -C opt-level=0 -C codegen-units=1 -C lto -//@ [cfi] compile-flags: -C prefer-dynamic=off -//@ [cfi] compile-flags: -Z sanitizer=cfi -//@ [kcfi] compile-flags: -Z sanitizer=kcfi -//@ [kcfi] compile-flags: -C panic=abort -C prefer-dynamic=off -//@ run-pass - -trait Foo { - fn foo(&self); - fn bar(&self); -} - -struct S; - -impl Foo for S { - fn foo(&self) {} - #[track_caller] - fn bar(&self) {} -} - -struct S2 { - f: fn(&S) -} - -impl S2 { - fn foo(&self, s: &S) { - (self.f)(s) - } -} - -trait Trait1 { - fn foo(&self); -} - -struct Type1; - -impl Trait1 for Type1 { - fn foo(&self) {} -} - -fn foo(_: &T) {} - -fn main() { - let type1 = Type1 {}; - let f = ::foo; - f(&type1); - // Check again with different optimization barriers - S2 { f: ::foo }.foo(&S); - // Check mismatched #[track_caller] - S2 { f: ::bar }.foo(&S); - // Check non-method functions - S2 { f: foo }.foo(&S) -} diff --git a/tests/ui/sanitizer/cfi-generalize-pointers-attr-cfg.rs b/tests/ui/sanitizer/cfi-generalize-pointers-attr-cfg.rs deleted file mode 100644 index d46002c69fd..00000000000 --- a/tests/ui/sanitizer/cfi-generalize-pointers-attr-cfg.rs +++ /dev/null @@ -1,11 +0,0 @@ -// Verifies that when compiling with `-Zsanitizer-cfi-generalize-pointers` the -// `#[cfg(sanitizer_cfi_generalize_pointers)]` attribute is configured. -// -//@ needs-sanitizer-cfi -//@ check-pass -//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Zsanitizer-cfi-generalize-pointers - -#![feature(cfg_sanitizer_cfi)] - -#[cfg(sanitizer_cfi_generalize_pointers)] -fn main() {} diff --git a/tests/ui/sanitizer/cfi-generalize-pointers-requires-cfi.rs b/tests/ui/sanitizer/cfi-generalize-pointers-requires-cfi.rs deleted file mode 100644 index 8ba13bd3639..00000000000 --- a/tests/ui/sanitizer/cfi-generalize-pointers-requires-cfi.rs +++ /dev/null @@ -1,9 +0,0 @@ -// Verifies that `-Zsanitizer-cfi-generalize-pointers` requires `-Zsanitizer=cfi` or -// `-Zsanitizer=kcfi`. -// -//@ needs-sanitizer-cfi -//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer-cfi-generalize-pointers - -#![feature(no_core)] -#![no_core] -#![no_main] diff --git a/tests/ui/sanitizer/cfi-generalize-pointers-requires-cfi.stderr b/tests/ui/sanitizer/cfi-generalize-pointers-requires-cfi.stderr deleted file mode 100644 index 621708de241..00000000000 --- a/tests/ui/sanitizer/cfi-generalize-pointers-requires-cfi.stderr +++ /dev/null @@ -1,4 +0,0 @@ -error: `-Zsanitizer-cfi-generalize-pointers` requires `-Zsanitizer=cfi` or `-Zsanitizer=kcfi` - -error: aborting due to 1 previous error - diff --git a/tests/ui/sanitizer/cfi-invalid-attr-cfi-encoding.rs b/tests/ui/sanitizer/cfi-invalid-attr-cfi-encoding.rs deleted file mode 100644 index 7ef6bd2f0ac..00000000000 --- a/tests/ui/sanitizer/cfi-invalid-attr-cfi-encoding.rs +++ /dev/null @@ -1,11 +0,0 @@ -// Verifies that invalid user-defined CFI encodings can't be used. -// -//@ needs-sanitizer-cfi -//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi - -#![feature(cfi_encoding, no_core)] -#![no_core] -#![no_main] - -#[cfi_encoding] //~ERROR 10:1: 10:16: malformed `cfi_encoding` attribute input -pub struct Type1(i32); diff --git a/tests/ui/sanitizer/cfi-invalid-attr-cfi-encoding.stderr b/tests/ui/sanitizer/cfi-invalid-attr-cfi-encoding.stderr deleted file mode 100644 index 93ec134241e..00000000000 --- a/tests/ui/sanitizer/cfi-invalid-attr-cfi-encoding.stderr +++ /dev/null @@ -1,8 +0,0 @@ -error: malformed `cfi_encoding` attribute input - --> $DIR/cfi-invalid-attr-cfi-encoding.rs:10:1 - | -LL | #[cfi_encoding] - | ^^^^^^^^^^^^^^^ help: must be of the form: `#[cfi_encoding = "encoding"]` - -error: aborting due to 1 previous error - diff --git a/tests/ui/sanitizer/cfi-is-incompatible-with-kcfi.aarch64.stderr b/tests/ui/sanitizer/cfi-is-incompatible-with-kcfi.aarch64.stderr deleted file mode 100644 index 7f596a19104..00000000000 --- a/tests/ui/sanitizer/cfi-is-incompatible-with-kcfi.aarch64.stderr +++ /dev/null @@ -1,6 +0,0 @@ -error: cfi sanitizer is not supported for this target - -error: `-Zsanitizer=cfi` is incompatible with `-Zsanitizer=kcfi` - -error: aborting due to 2 previous errors - diff --git a/tests/ui/sanitizer/cfi-is-incompatible-with-kcfi.rs b/tests/ui/sanitizer/cfi-is-incompatible-with-kcfi.rs deleted file mode 100644 index c628709d7a1..00000000000 --- a/tests/ui/sanitizer/cfi-is-incompatible-with-kcfi.rs +++ /dev/null @@ -1,12 +0,0 @@ -// Verifies that `-Zsanitizer=cfi` is incompatible with `-Zsanitizer=kcfi`. -// -//@ revisions: aarch64 x86_64 -//@ [aarch64] compile-flags: --target aarch64-unknown-none -//@ [aarch64] needs-llvm-components: aarch64 -//@ [x86_64] compile-flags: --target x86_64-unknown-none -//@ [x86_64] needs-llvm-components: x86 -//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Zsanitizer=kcfi - -#![feature(no_core)] -#![no_core] -#![no_main] diff --git a/tests/ui/sanitizer/cfi-is-incompatible-with-kcfi.x86_64.stderr b/tests/ui/sanitizer/cfi-is-incompatible-with-kcfi.x86_64.stderr deleted file mode 100644 index 7f596a19104..00000000000 --- a/tests/ui/sanitizer/cfi-is-incompatible-with-kcfi.x86_64.stderr +++ /dev/null @@ -1,6 +0,0 @@ -error: cfi sanitizer is not supported for this target - -error: `-Zsanitizer=cfi` is incompatible with `-Zsanitizer=kcfi` - -error: aborting due to 2 previous errors - diff --git a/tests/ui/sanitizer/cfi-normalize-integers-attr-cfg.rs b/tests/ui/sanitizer/cfi-normalize-integers-attr-cfg.rs deleted file mode 100644 index 24c2c2c13da..00000000000 --- a/tests/ui/sanitizer/cfi-normalize-integers-attr-cfg.rs +++ /dev/null @@ -1,11 +0,0 @@ -// Verifies that when compiling with `-Zsanitizer-cfi-normalize-integers` the -// `#[cfg(sanitizer_cfi_normalize_integers)]` attribute is configured. -// -//@ needs-sanitizer-cfi -//@ check-pass -//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Zsanitizer-cfi-normalize-integers - -#![feature(cfg_sanitizer_cfi)] - -#[cfg(sanitizer_cfi_normalize_integers)] -fn main() {} diff --git a/tests/ui/sanitizer/cfi-normalize-integers-requires-cfi.rs b/tests/ui/sanitizer/cfi-normalize-integers-requires-cfi.rs deleted file mode 100644 index a7ecefbf7ef..00000000000 --- a/tests/ui/sanitizer/cfi-normalize-integers-requires-cfi.rs +++ /dev/null @@ -1,9 +0,0 @@ -// Verifies that `-Zsanitizer-cfi-normalize-integers` requires `-Zsanitizer=cfi` or -// `-Zsanitizer=kcfi` -// -//@ needs-sanitizer-cfi -//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer-cfi-normalize-integers - -#![feature(no_core)] -#![no_core] -#![no_main] diff --git a/tests/ui/sanitizer/cfi-normalize-integers-requires-cfi.stderr b/tests/ui/sanitizer/cfi-normalize-integers-requires-cfi.stderr deleted file mode 100644 index 748fb60dad9..00000000000 --- a/tests/ui/sanitizer/cfi-normalize-integers-requires-cfi.stderr +++ /dev/null @@ -1,4 +0,0 @@ -error: `-Zsanitizer-cfi-normalize-integers` requires `-Zsanitizer=cfi` or `-Zsanitizer=kcfi` - -error: aborting due to 1 previous error - diff --git a/tests/ui/sanitizer/cfi-requires-lto.rs b/tests/ui/sanitizer/cfi-requires-lto.rs deleted file mode 100644 index 5a34f696e05..00000000000 --- a/tests/ui/sanitizer/cfi-requires-lto.rs +++ /dev/null @@ -1,8 +0,0 @@ -// Verifies that `-Zsanitizer=cfi` requires `-Clto` or `-Clinker-plugin-lto`. -// -//@ needs-sanitizer-cfi -//@ compile-flags: -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi - -#![feature(no_core)] -#![no_core] -#![no_main] diff --git a/tests/ui/sanitizer/cfi-requires-lto.stderr b/tests/ui/sanitizer/cfi-requires-lto.stderr deleted file mode 100644 index efc0c43138e..00000000000 --- a/tests/ui/sanitizer/cfi-requires-lto.stderr +++ /dev/null @@ -1,4 +0,0 @@ -error: `-Zsanitizer=cfi` requires `-Clto` or `-Clinker-plugin-lto` - -error: aborting due to 1 previous error - diff --git a/tests/ui/sanitizer/cfi-self-ref.rs b/tests/ui/sanitizer/cfi-self-ref.rs deleted file mode 100644 index 3b524ac661c..00000000000 --- a/tests/ui/sanitizer/cfi-self-ref.rs +++ /dev/null @@ -1,38 +0,0 @@ -// Check that encoding self-referential types works with #[repr(transparent)] - -//@ revisions: cfi kcfi -// FIXME(#122848) Remove only-linux once OSX CFI binaries work -//@ only-linux -//@ [cfi] needs-sanitizer-cfi -//@ [kcfi] needs-sanitizer-kcfi -//@ compile-flags: -C target-feature=-crt-static -//@ [cfi] compile-flags: -C codegen-units=1 -C lto -C prefer-dynamic=off -C opt-level=0 -//@ [cfi] compile-flags: -Z sanitizer=cfi -//@ [kcfi] compile-flags: -Z sanitizer=kcfi -//@ [kcfi] compile-flags: -C panic=abort -C prefer-dynamic=off -//@ run-pass - -use std::marker::PhantomData; - -struct X { - _x: u8, - p: PhantomData, -} - -#[repr(transparent)] -struct Y(X); - -trait Fooable { - fn foo(&self, y: Y); -} - -struct Bar; - -impl Fooable for Bar { - fn foo(&self, _: Y) {} -} - -fn main() { - let x = &Bar as &dyn Fooable; - x.foo(Y(X {_x: 0, p: PhantomData})); -} diff --git a/tests/ui/sanitizer/cfi-sized-associated-ty.rs b/tests/ui/sanitizer/cfi-sized-associated-ty.rs deleted file mode 100644 index f5b4e22e9d9..00000000000 --- a/tests/ui/sanitizer/cfi-sized-associated-ty.rs +++ /dev/null @@ -1,38 +0,0 @@ -// Check that we only elaborate non-`Self: Sized` associated types when -// erasing the receiver from trait ref. - -//@ revisions: cfi kcfi -// FIXME(#122848) Remove only-linux once OSX CFI binaries work -//@ only-linux -//@ [cfi] needs-sanitizer-cfi -//@ [kcfi] needs-sanitizer-kcfi -//@ compile-flags: -C target-feature=-crt-static -//@ [cfi] compile-flags: -C codegen-units=1 -C lto -C prefer-dynamic=off -C opt-level=0 -//@ [cfi] compile-flags: -Z sanitizer=cfi -//@ [kcfi] compile-flags: -Z sanitizer=kcfi -//@ [kcfi] compile-flags: -C panic=abort -C prefer-dynamic=off -//@ run-pass - -trait Foo { - type Bar<'a> - where - Self: Sized; - - fn test(&self); -} - -impl Foo for () { - type Bar<'a> = () - where - Self: Sized; - - fn test(&self) {} -} - -fn test(x: &dyn Foo) { - x.test(); -} - -fn main() { - test(&()); -} diff --git a/tests/ui/sanitizer/cfi-supertraits.rs b/tests/ui/sanitizer/cfi-supertraits.rs deleted file mode 100644 index 4bb6177577f..00000000000 --- a/tests/ui/sanitizer/cfi-supertraits.rs +++ /dev/null @@ -1,79 +0,0 @@ -#![feature(trait_upcasting)] -// Check that super-traits are callable. - -//@ revisions: cfi kcfi -// FIXME(#122848) Remove only-linux once OSX CFI binaries work -//@ only-linux -//@ [cfi] needs-sanitizer-cfi -//@ [kcfi] needs-sanitizer-kcfi -//@ compile-flags: -C target-feature=-crt-static -//@ [cfi] compile-flags: -C codegen-units=1 -C lto -C prefer-dynamic=off -C opt-level=0 -//@ [cfi] compile-flags: -Z sanitizer=cfi -//@ [kcfi] compile-flags: -Z sanitizer=kcfi -//@ [kcfi] compile-flags: -C panic=abort -C prefer-dynamic=off -//@ run-pass - -trait Parent1 { - type P1; - fn p1(&self) -> Self::P1; - fn d(&self) -> i32 { - 42 - } -} - -trait Parent2 { - type P2; - fn p2(&self) -> Self::P2; -} - -trait Child : Parent1 + Parent2 { - type C; - fn c(&self) -> Self::C; -} - -struct Foo; - -impl Parent1 for Foo { - type P1 = u16; - fn p1(&self) -> Self::P1 { - println!("p1"); - 1 - } -} - -impl Parent2 for Foo { - type P2 = u32; - fn p2(&self) -> Self::P2 { - println!("p2"); - 2 - } -} - -impl Child for Foo { - type C = u8; - fn c(&self) -> Self::C { - println!("c"); - 0 - } -} - -fn main() { - // Child can access its own methods and super methods. - let x = &Foo as &dyn Child; - x.c(); - x.p1(); - x.p2(); - x.d(); - // Parents can be created and access their methods. - let y = &Foo as &dyn Parent1; - y.p1(); - y.d(); - let z = &Foo as &dyn Parent2; - z.p2(); - // Trait upcasting works - let x1 = x as &dyn Parent1; - x1.p1(); - x1.d(); - let x2 = x as &dyn Parent2; - x2.p2(); -} diff --git a/tests/ui/sanitizer/cfi-virtual-auto.rs b/tests/ui/sanitizer/cfi-virtual-auto.rs deleted file mode 100644 index 6971d516a20..00000000000 --- a/tests/ui/sanitizer/cfi-virtual-auto.rs +++ /dev/null @@ -1,27 +0,0 @@ -// Tests that calling a trait object method on a trait object with additional auto traits works. - -//@ revisions: cfi kcfi -// FIXME(#122848) Remove only-linux once OSX CFI binaries work -//@ only-linux -//@ [cfi] needs-sanitizer-cfi -//@ [kcfi] needs-sanitizer-kcfi -//@ compile-flags: -C target-feature=-crt-static -//@ [cfi] compile-flags: -C codegen-units=1 -C lto -C prefer-dynamic=off -C opt-level=0 -//@ [cfi] compile-flags: -Z sanitizer=cfi -//@ [kcfi] compile-flags: -Z sanitizer=kcfi -//@ [kcfi] compile-flags: -C panic=abort -C prefer-dynamic=off -//@ run-pass - -trait Foo { - fn foo(&self); -} - -struct Bar; -impl Foo for Bar { - fn foo(&self) {} -} - -pub fn main() { - let x: &(dyn Foo + Send) = &Bar; - x.foo(); -} diff --git a/tests/ui/sanitizer/cfi-with-rustc-lto-requires-single-codegen-unit.rs b/tests/ui/sanitizer/cfi-with-rustc-lto-requires-single-codegen-unit.rs deleted file mode 100644 index 954e4ec3b85..00000000000 --- a/tests/ui/sanitizer/cfi-with-rustc-lto-requires-single-codegen-unit.rs +++ /dev/null @@ -1,8 +0,0 @@ -// Verifies that `-Zsanitizer=cfi` with `-Clto` or `-Clto=thin` requires `-Ccodegen-units=1`. -// -//@ needs-sanitizer-cfi -//@ compile-flags: -Ccodegen-units=2 -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi - -#![feature(no_core)] -#![no_core] -#![no_main] diff --git a/tests/ui/sanitizer/cfi-with-rustc-lto-requires-single-codegen-unit.stderr b/tests/ui/sanitizer/cfi-with-rustc-lto-requires-single-codegen-unit.stderr deleted file mode 100644 index 8d6dc1d8f1e..00000000000 --- a/tests/ui/sanitizer/cfi-with-rustc-lto-requires-single-codegen-unit.stderr +++ /dev/null @@ -1,4 +0,0 @@ -error: `-Zsanitizer=cfi` with `-Clto` requires `-Ccodegen-units=1` - -error: aborting due to 1 previous error - diff --git a/tests/ui/sanitizer/cfi/assoc-ty-lifetime-issue-123053.rs b/tests/ui/sanitizer/cfi/assoc-ty-lifetime-issue-123053.rs new file mode 100644 index 00000000000..dd604b6bf7d --- /dev/null +++ b/tests/ui/sanitizer/cfi/assoc-ty-lifetime-issue-123053.rs @@ -0,0 +1,38 @@ +// Regression test for issue 123053, where associated types with lifetimes caused generation of the +// trait object type to fail, causing an ICE. +// +//@ needs-sanitizer-cfi +//@ compile-flags: -Ccodegen-units=1 -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi --edition=2021 +//@ no-prefer-dynamic +//@ only-x86_64-unknown-linux-gnu +//@ build-pass + +trait Iterable { + type Item<'a> + where + Self: 'a; + type Iter<'a>: Iterator> + where + Self: 'a; + + fn iter<'a>(&'a self) -> Self::Iter<'a>; +} + +impl Iterable for [T] { + type Item<'a> = as Iterator>::Item where T: 'a; + type Iter<'a> = std::slice::Iter<'a, T> where T: 'a; + + fn iter<'a>(&'a self) -> Self::Iter<'a> { + self.iter() + } +} + +fn get_first<'a, I: Iterable + ?Sized>(it: &'a I) -> Option> { + it.iter().next() +} + +fn main() { + let v = vec![1, 2, 3]; + + assert_eq!(Some(&1), get_first(&*v)); +} diff --git a/tests/ui/sanitizer/cfi/async-closures.rs b/tests/ui/sanitizer/cfi/async-closures.rs new file mode 100644 index 00000000000..d94f2992d84 --- /dev/null +++ b/tests/ui/sanitizer/cfi/async-closures.rs @@ -0,0 +1,33 @@ +// Check various forms of dynamic closure calls + +//@ edition: 2021 +//@ revisions: cfi kcfi +// FIXME(#122848) Remove only-linux once OSX CFI binaries work +//@ only-linux +//@ [cfi] needs-sanitizer-cfi +//@ [kcfi] needs-sanitizer-kcfi +//@ compile-flags: -C target-feature=-crt-static +//@ [cfi] compile-flags: -C codegen-units=1 -C lto -C prefer-dynamic=off -C opt-level=0 +//@ [cfi] compile-flags: -Z sanitizer=cfi +//@ [kcfi] compile-flags: -Z sanitizer=kcfi +//@ [kcfi] compile-flags: -C panic=abort -Z panic-abort-tests -C prefer-dynamic=off +//@ run-pass + +#![feature(async_closure)] +#![feature(async_fn_traits)] + +use std::ops::AsyncFn; + +#[inline(never)] +fn identity(x: T) -> T { x } + +// We can't actually create a `dyn AsyncFn()`, because it's not object-safe, but we should check +// that we don't bug out when we encounter one. + +fn main() { + let f = identity(async || ()); + let _ = f.async_call(()); + let _ = f(); + let g: Box _> = Box::new(f) as _; + let _ = g(); +} diff --git a/tests/ui/sanitizer/cfi/can-reveal-opaques.rs b/tests/ui/sanitizer/cfi/can-reveal-opaques.rs new file mode 100644 index 00000000000..55988a62a8c --- /dev/null +++ b/tests/ui/sanitizer/cfi/can-reveal-opaques.rs @@ -0,0 +1,44 @@ +//@ needs-sanitizer-cfi +//@ compile-flags: -Ccodegen-units=1 -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi +//@ no-prefer-dynamic +//@ only-x86_64-unknown-linux-gnu +//@ build-pass + +// See comment below for why this test exists. + +trait Tr { + type Projection; +} + +impl Tr for F +where + F: Fn() -> U +{ + type Projection = U; +} + +fn test, U>(b: B) -> B::Projection +{ + todo!() +} + +fn main() { + fn rpit_fn() -> impl Sized {} + + // When CFI runs, it tries to compute the signature of the call. This + // ends up giving us a signature of: + // `fn test::() -> >::Projection`, + // where `rpit_fn` is the ZST FnDef for the function. However, we were + // previously using a Reveal::UserFacing param-env. This means that the + // `>::Projection` return type is impossible to normalize, + // since it would require proving `rpit_fn: Fn() -> ()`, but we cannot + // prove that the `impl Sized` opaque is `()` with a user-facing param-env. + // This leads to a normalization error, and then an ICE. + // + // Side-note: + // So why is the second generic of `test` "`()`", and not the + // `impl Sized` since we inferred it from the return type of `rpit_fn` + // during typeck? Well, that's because we're using the generics from the + // terminator of the MIR, which has had the RevealAll pass performed on it. + let _ = test(rpit_fn); +} diff --git a/tests/ui/sanitizer/cfi/canonical-jump-tables-requires-cfi.rs b/tests/ui/sanitizer/cfi/canonical-jump-tables-requires-cfi.rs new file mode 100644 index 00000000000..10c5bf6ea5e --- /dev/null +++ b/tests/ui/sanitizer/cfi/canonical-jump-tables-requires-cfi.rs @@ -0,0 +1,8 @@ +// Verifies that `-Zsanitizer-cfi-canonical-jump-tables` requires `-Zsanitizer=cfi`. +// +//@ needs-sanitizer-cfi +//@ compile-flags: -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer-cfi-canonical-jump-tables=false + +#![feature(no_core)] +#![no_core] +#![no_main] diff --git a/tests/ui/sanitizer/cfi/canonical-jump-tables-requires-cfi.stderr b/tests/ui/sanitizer/cfi/canonical-jump-tables-requires-cfi.stderr new file mode 100644 index 00000000000..de67d6a6b7f --- /dev/null +++ b/tests/ui/sanitizer/cfi/canonical-jump-tables-requires-cfi.stderr @@ -0,0 +1,4 @@ +error: `-Zsanitizer-cfi-canonical-jump-tables` requires `-Zsanitizer=cfi` + +error: aborting due to 1 previous error + diff --git a/tests/ui/sanitizer/cfi/closures.rs b/tests/ui/sanitizer/cfi/closures.rs new file mode 100644 index 00000000000..9f9002da674 --- /dev/null +++ b/tests/ui/sanitizer/cfi/closures.rs @@ -0,0 +1,90 @@ +// Check various forms of dynamic closure calls + +//@ revisions: cfi kcfi +// FIXME(#122848) Remove only-linux once OSX CFI binaries work +//@ only-linux +//@ [cfi] needs-sanitizer-cfi +//@ [kcfi] needs-sanitizer-kcfi +//@ compile-flags: -C target-feature=-crt-static +//@ [cfi] compile-flags: -C codegen-units=1 -C lto -C prefer-dynamic=off -C opt-level=0 +//@ [cfi] compile-flags: -Z sanitizer=cfi +//@ [kcfi] compile-flags: -Z sanitizer=kcfi +//@ [kcfi] compile-flags: -C panic=abort -Z panic-abort-tests -C prefer-dynamic=off +//@ compile-flags: --test +//@ run-pass + +#![feature(fn_traits)] +#![feature(unboxed_closures)] + +fn foo<'a, T>() -> Box &'a T> { + Box::new(|x| x) +} + +#[test] +fn dyn_fn_with_params() { + let x = 3; + let f = foo(); + f(&x); + // FIXME remove once drops are working. + std::mem::forget(f); +} + +#[test] +fn call_fn_trait() { + let f: &(dyn Fn()) = &(|| {}) as _; + f.call(()); +} + +#[test] +fn fn_ptr_cast() { + let f: &fn() = &((|| ()) as _); + f(); +} + +fn use_fnmut(mut f: F) { + f() +} + +#[test] +fn fn_to_fnmut() { + let f: &(dyn Fn()) = &(|| {}) as _; + use_fnmut(f); +} + +fn hrtb_helper(f: &dyn for<'a> Fn(&'a usize)) { + f(&10) +} + +#[test] +fn hrtb_fn() { + hrtb_helper((&|x: &usize| println!("{}", *x)) as _) +} + +#[test] +fn fnonce() { + let f: Box = Box::new(|| {}) as _; + f(); +} + +fn use_closure(call: extern "rust-call" fn(&C, ()) -> i32, f: &C) -> i32 { + call(f, ()) +} + +#[test] +fn closure_addr_taken() { + let x = 3i32; + let f = || x; + let call = Fn::<()>::call; + use_closure(call, &f); +} + +fn use_closure_once(call: extern "rust-call" fn(C, ()) -> i32, f: C) -> i32 { + call(f, ()) +} + +#[test] +fn closure_once_addr_taken() { + let g = || 3; + let call2 = FnOnce::<()>::call_once; + use_closure_once(call2, g); +} diff --git a/tests/ui/sanitizer/cfi/complex-receiver.rs b/tests/ui/sanitizer/cfi/complex-receiver.rs new file mode 100644 index 00000000000..c7b45a775ca --- /dev/null +++ b/tests/ui/sanitizer/cfi/complex-receiver.rs @@ -0,0 +1,47 @@ +// Check that more complex receivers work: +// * Arc as for custom receivers +// * &dyn Bar for type constraints + +//@ revisions: cfi kcfi +// FIXME(#122848) Remove only-linux once OSX CFI binaries work +//@ only-linux +//@ [cfi] needs-sanitizer-cfi +//@ [kcfi] needs-sanitizer-kcfi +//@ compile-flags: -C target-feature=-crt-static +//@ [cfi] compile-flags: -C codegen-units=1 -C lto -C prefer-dynamic=off -C opt-level=0 +//@ [cfi] compile-flags: -Z sanitizer=cfi +//@ [kcfi] compile-flags: -Z sanitizer=kcfi +//@ [kcfi] compile-flags: -C panic=abort -C prefer-dynamic=off +//@ run-pass + +use std::sync::Arc; + +trait Foo { + fn foo(self: Arc); +} + +struct FooImpl; + +impl Foo for FooImpl { + fn foo(self: Arc) {} +} + +trait Bar { + type T; + fn bar(&self) -> Self::T; +} + +struct BarImpl; + +impl Bar for BarImpl { + type T = i32; + fn bar(&self) -> Self::T { 7 } +} + +fn main() { + let foo: Arc = Arc::new(FooImpl); + foo.foo(); + + let bar: &dyn Bar = &BarImpl; + assert_eq!(bar.bar(), 7); +} diff --git a/tests/ui/sanitizer/cfi/coroutine.rs b/tests/ui/sanitizer/cfi/coroutine.rs new file mode 100644 index 00000000000..ad994fcf737 --- /dev/null +++ b/tests/ui/sanitizer/cfi/coroutine.rs @@ -0,0 +1,67 @@ +// Verifies that we can call dynamic coroutines + +//@ revisions: cfi kcfi +// FIXME(#122848) Remove only-linux once OSX CFI binaries work +//@ only-linux +//@ edition: 2024 +//@ [cfi] needs-sanitizer-cfi +//@ [kcfi] needs-sanitizer-kcfi +//@ compile-flags: -C target-feature=-crt-static +//@ [cfi] compile-flags: -C codegen-units=1 -C lto -C prefer-dynamic=off -C opt-level=0 +//@ [cfi] compile-flags: -Z sanitizer=cfi +//@ [kcfi] compile-flags: -Z sanitizer=kcfi +//@ [kcfi] compile-flags: -C panic=abort -Z panic-abort-tests -C prefer-dynamic=off +//@ compile-flags: --test -Z unstable-options +//@ run-pass + +#![feature(coroutines, stmt_expr_attributes)] +#![feature(coroutine_trait)] +#![feature(noop_waker)] +#![feature(gen_blocks)] +#![feature(async_iterator)] + +use std::ops::{Coroutine, CoroutineState}; +use std::pin::{pin, Pin}; +use std::task::{Context, Poll, Waker}; +use std::async_iter::AsyncIterator; + +#[test] +fn general_coroutine() { + let mut coro = #[coroutine] |x: i32| { + yield x; + "done" + }; + let mut abstract_coro: Pin<&mut dyn Coroutine> = pin!(coro); + assert_eq!(abstract_coro.as_mut().resume(2), CoroutineState::Yielded(2)); + assert_eq!(abstract_coro.as_mut().resume(0), CoroutineState::Complete("done")); +} + +async fn async_fn() {} + +#[test] +fn async_coroutine() { + let f: fn() -> Pin>> = || Box::pin(async_fn()); + let _ = async { f().await; }; + assert_eq!(f().as_mut().poll(&mut Context::from_waker(Waker::noop())), Poll::Ready(())); +} + +async gen fn async_gen_fn() -> u8 { + yield 5; +} + +#[test] +fn async_gen_coroutine() { + let f: fn() -> Pin>> = || Box::pin(async_gen_fn()); + assert_eq!(f().as_mut().poll_next(&mut Context::from_waker(Waker::noop())), + Poll::Ready(Some(5))); +} + +gen fn gen_fn() -> u8 { + yield 6; +} + +#[test] +fn gen_coroutine() { + let f: fn() -> Box> = || Box::new(gen_fn()); + assert_eq!(f().next(), Some(6)); +} diff --git a/tests/ui/sanitizer/cfi/drop-in-place.rs b/tests/ui/sanitizer/cfi/drop-in-place.rs new file mode 100644 index 00000000000..8ce2c432602 --- /dev/null +++ b/tests/ui/sanitizer/cfi/drop-in-place.rs @@ -0,0 +1,20 @@ +// Verifies that drops can be called on arbitrary trait objects. +// +// FIXME(#122848): Remove only-linux when fixed. +//@ only-linux +//@ needs-sanitizer-cfi +//@ compile-flags: -Clto -Copt-level=0 -Cprefer-dynamic=off -Ctarget-feature=-crt-static -Zsanitizer=cfi +//@ run-pass + +struct EmptyDrop; + +struct NonEmptyDrop; + +impl Drop for NonEmptyDrop { + fn drop(&mut self) {} +} + +fn main() { + let _ = Box::new(EmptyDrop) as Box; + let _ = Box::new(NonEmptyDrop) as Box; +} diff --git a/tests/ui/sanitizer/cfi/drop-no-principal.rs b/tests/ui/sanitizer/cfi/drop-no-principal.rs new file mode 100644 index 00000000000..c1c88c8c71c --- /dev/null +++ b/tests/ui/sanitizer/cfi/drop-no-principal.rs @@ -0,0 +1,21 @@ +// Check that dropping a trait object without a principal trait succeeds + +//@ needs-sanitizer-cfi +// FIXME(#122848) Remove only-linux once OSX CFI binaries works +//@ only-linux +//@ compile-flags: --crate-type=bin -Cprefer-dynamic=off -Clto -Zsanitizer=cfi +//@ compile-flags: -C target-feature=-crt-static -C codegen-units=1 -C opt-level=0 +// FIXME(#118761) Should be run-pass once the labels on drop are compatible. +// This test is being landed ahead of that to test that the compiler doesn't ICE while labeling the +// callsite for a drop, but the vtable doesn't have the correct label yet. +//@ build-pass + +struct CustomDrop; + +impl Drop for CustomDrop { + fn drop(&mut self) {} +} + +fn main() { + let _ = Box::new(CustomDrop) as Box; +} diff --git a/tests/ui/sanitizer/cfi/fn-ptr.rs b/tests/ui/sanitizer/cfi/fn-ptr.rs new file mode 100644 index 00000000000..505b4b8e7f0 --- /dev/null +++ b/tests/ui/sanitizer/cfi/fn-ptr.rs @@ -0,0 +1,61 @@ +// Verifies that casting to a function pointer works. + +//@ revisions: cfi kcfi +// FIXME(#122848) Remove only-linux once OSX CFI binaries work +//@ only-linux +//@ [cfi] needs-sanitizer-cfi +//@ [kcfi] needs-sanitizer-kcfi +//@ compile-flags: -C target-feature=-crt-static +//@ [cfi] compile-flags: -C opt-level=0 -C codegen-units=1 -C lto +//@ [cfi] compile-flags: -C prefer-dynamic=off +//@ [cfi] compile-flags: -Z sanitizer=cfi +//@ [kcfi] compile-flags: -Z sanitizer=kcfi +//@ [kcfi] compile-flags: -C panic=abort -C prefer-dynamic=off +//@ run-pass + +trait Foo { + fn foo(&self); + fn bar(&self); +} + +struct S; + +impl Foo for S { + fn foo(&self) {} + #[track_caller] + fn bar(&self) {} +} + +struct S2 { + f: fn(&S) +} + +impl S2 { + fn foo(&self, s: &S) { + (self.f)(s) + } +} + +trait Trait1 { + fn foo(&self); +} + +struct Type1; + +impl Trait1 for Type1 { + fn foo(&self) {} +} + +fn foo(_: &T) {} + +fn main() { + let type1 = Type1 {}; + let f = ::foo; + f(&type1); + // Check again with different optimization barriers + S2 { f: ::foo }.foo(&S); + // Check mismatched #[track_caller] + S2 { f: ::bar }.foo(&S); + // Check non-method functions + S2 { f: foo }.foo(&S) +} diff --git a/tests/ui/sanitizer/cfi/generalize-pointers-attr-cfg.rs b/tests/ui/sanitizer/cfi/generalize-pointers-attr-cfg.rs new file mode 100644 index 00000000000..d46002c69fd --- /dev/null +++ b/tests/ui/sanitizer/cfi/generalize-pointers-attr-cfg.rs @@ -0,0 +1,11 @@ +// Verifies that when compiling with `-Zsanitizer-cfi-generalize-pointers` the +// `#[cfg(sanitizer_cfi_generalize_pointers)]` attribute is configured. +// +//@ needs-sanitizer-cfi +//@ check-pass +//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Zsanitizer-cfi-generalize-pointers + +#![feature(cfg_sanitizer_cfi)] + +#[cfg(sanitizer_cfi_generalize_pointers)] +fn main() {} diff --git a/tests/ui/sanitizer/cfi/generalize-pointers-requires-cfi.rs b/tests/ui/sanitizer/cfi/generalize-pointers-requires-cfi.rs new file mode 100644 index 00000000000..8ba13bd3639 --- /dev/null +++ b/tests/ui/sanitizer/cfi/generalize-pointers-requires-cfi.rs @@ -0,0 +1,9 @@ +// Verifies that `-Zsanitizer-cfi-generalize-pointers` requires `-Zsanitizer=cfi` or +// `-Zsanitizer=kcfi`. +// +//@ needs-sanitizer-cfi +//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer-cfi-generalize-pointers + +#![feature(no_core)] +#![no_core] +#![no_main] diff --git a/tests/ui/sanitizer/cfi/generalize-pointers-requires-cfi.stderr b/tests/ui/sanitizer/cfi/generalize-pointers-requires-cfi.stderr new file mode 100644 index 00000000000..621708de241 --- /dev/null +++ b/tests/ui/sanitizer/cfi/generalize-pointers-requires-cfi.stderr @@ -0,0 +1,4 @@ +error: `-Zsanitizer-cfi-generalize-pointers` requires `-Zsanitizer=cfi` or `-Zsanitizer=kcfi` + +error: aborting due to 1 previous error + diff --git a/tests/ui/sanitizer/cfi/invalid-attr-encoding.rs b/tests/ui/sanitizer/cfi/invalid-attr-encoding.rs new file mode 100644 index 00000000000..7ef6bd2f0ac --- /dev/null +++ b/tests/ui/sanitizer/cfi/invalid-attr-encoding.rs @@ -0,0 +1,11 @@ +// Verifies that invalid user-defined CFI encodings can't be used. +// +//@ needs-sanitizer-cfi +//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi + +#![feature(cfi_encoding, no_core)] +#![no_core] +#![no_main] + +#[cfi_encoding] //~ERROR 10:1: 10:16: malformed `cfi_encoding` attribute input +pub struct Type1(i32); diff --git a/tests/ui/sanitizer/cfi/invalid-attr-encoding.stderr b/tests/ui/sanitizer/cfi/invalid-attr-encoding.stderr new file mode 100644 index 00000000000..1aa6bef17b1 --- /dev/null +++ b/tests/ui/sanitizer/cfi/invalid-attr-encoding.stderr @@ -0,0 +1,8 @@ +error: malformed `cfi_encoding` attribute input + --> $DIR/invalid-attr-encoding.rs:10:1 + | +LL | #[cfi_encoding] + | ^^^^^^^^^^^^^^^ help: must be of the form: `#[cfi_encoding = "encoding"]` + +error: aborting due to 1 previous error + diff --git a/tests/ui/sanitizer/cfi/is-incompatible-with-kcfi.aarch64.stderr b/tests/ui/sanitizer/cfi/is-incompatible-with-kcfi.aarch64.stderr new file mode 100644 index 00000000000..7f596a19104 --- /dev/null +++ b/tests/ui/sanitizer/cfi/is-incompatible-with-kcfi.aarch64.stderr @@ -0,0 +1,6 @@ +error: cfi sanitizer is not supported for this target + +error: `-Zsanitizer=cfi` is incompatible with `-Zsanitizer=kcfi` + +error: aborting due to 2 previous errors + diff --git a/tests/ui/sanitizer/cfi/is-incompatible-with-kcfi.rs b/tests/ui/sanitizer/cfi/is-incompatible-with-kcfi.rs new file mode 100644 index 00000000000..c628709d7a1 --- /dev/null +++ b/tests/ui/sanitizer/cfi/is-incompatible-with-kcfi.rs @@ -0,0 +1,12 @@ +// Verifies that `-Zsanitizer=cfi` is incompatible with `-Zsanitizer=kcfi`. +// +//@ revisions: aarch64 x86_64 +//@ [aarch64] compile-flags: --target aarch64-unknown-none +//@ [aarch64] needs-llvm-components: aarch64 +//@ [x86_64] compile-flags: --target x86_64-unknown-none +//@ [x86_64] needs-llvm-components: x86 +//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Zsanitizer=kcfi + +#![feature(no_core)] +#![no_core] +#![no_main] diff --git a/tests/ui/sanitizer/cfi/is-incompatible-with-kcfi.x86_64.stderr b/tests/ui/sanitizer/cfi/is-incompatible-with-kcfi.x86_64.stderr new file mode 100644 index 00000000000..7f596a19104 --- /dev/null +++ b/tests/ui/sanitizer/cfi/is-incompatible-with-kcfi.x86_64.stderr @@ -0,0 +1,6 @@ +error: cfi sanitizer is not supported for this target + +error: `-Zsanitizer=cfi` is incompatible with `-Zsanitizer=kcfi` + +error: aborting due to 2 previous errors + diff --git a/tests/ui/sanitizer/cfi/normalize-integers-attr-cfg.rs b/tests/ui/sanitizer/cfi/normalize-integers-attr-cfg.rs new file mode 100644 index 00000000000..24c2c2c13da --- /dev/null +++ b/tests/ui/sanitizer/cfi/normalize-integers-attr-cfg.rs @@ -0,0 +1,11 @@ +// Verifies that when compiling with `-Zsanitizer-cfi-normalize-integers` the +// `#[cfg(sanitizer_cfi_normalize_integers)]` attribute is configured. +// +//@ needs-sanitizer-cfi +//@ check-pass +//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Zsanitizer-cfi-normalize-integers + +#![feature(cfg_sanitizer_cfi)] + +#[cfg(sanitizer_cfi_normalize_integers)] +fn main() {} diff --git a/tests/ui/sanitizer/cfi/normalize-integers-requires-cfi.rs b/tests/ui/sanitizer/cfi/normalize-integers-requires-cfi.rs new file mode 100644 index 00000000000..a7ecefbf7ef --- /dev/null +++ b/tests/ui/sanitizer/cfi/normalize-integers-requires-cfi.rs @@ -0,0 +1,9 @@ +// Verifies that `-Zsanitizer-cfi-normalize-integers` requires `-Zsanitizer=cfi` or +// `-Zsanitizer=kcfi` +// +//@ needs-sanitizer-cfi +//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer-cfi-normalize-integers + +#![feature(no_core)] +#![no_core] +#![no_main] diff --git a/tests/ui/sanitizer/cfi/normalize-integers-requires-cfi.stderr b/tests/ui/sanitizer/cfi/normalize-integers-requires-cfi.stderr new file mode 100644 index 00000000000..748fb60dad9 --- /dev/null +++ b/tests/ui/sanitizer/cfi/normalize-integers-requires-cfi.stderr @@ -0,0 +1,4 @@ +error: `-Zsanitizer-cfi-normalize-integers` requires `-Zsanitizer=cfi` or `-Zsanitizer=kcfi` + +error: aborting due to 1 previous error + diff --git a/tests/ui/sanitizer/cfi/requires-lto.rs b/tests/ui/sanitizer/cfi/requires-lto.rs new file mode 100644 index 00000000000..5a34f696e05 --- /dev/null +++ b/tests/ui/sanitizer/cfi/requires-lto.rs @@ -0,0 +1,8 @@ +// Verifies that `-Zsanitizer=cfi` requires `-Clto` or `-Clinker-plugin-lto`. +// +//@ needs-sanitizer-cfi +//@ compile-flags: -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi + +#![feature(no_core)] +#![no_core] +#![no_main] diff --git a/tests/ui/sanitizer/cfi/requires-lto.stderr b/tests/ui/sanitizer/cfi/requires-lto.stderr new file mode 100644 index 00000000000..efc0c43138e --- /dev/null +++ b/tests/ui/sanitizer/cfi/requires-lto.stderr @@ -0,0 +1,4 @@ +error: `-Zsanitizer=cfi` requires `-Clto` or `-Clinker-plugin-lto` + +error: aborting due to 1 previous error + diff --git a/tests/ui/sanitizer/cfi/self-ref.rs b/tests/ui/sanitizer/cfi/self-ref.rs new file mode 100644 index 00000000000..3b524ac661c --- /dev/null +++ b/tests/ui/sanitizer/cfi/self-ref.rs @@ -0,0 +1,38 @@ +// Check that encoding self-referential types works with #[repr(transparent)] + +//@ revisions: cfi kcfi +// FIXME(#122848) Remove only-linux once OSX CFI binaries work +//@ only-linux +//@ [cfi] needs-sanitizer-cfi +//@ [kcfi] needs-sanitizer-kcfi +//@ compile-flags: -C target-feature=-crt-static +//@ [cfi] compile-flags: -C codegen-units=1 -C lto -C prefer-dynamic=off -C opt-level=0 +//@ [cfi] compile-flags: -Z sanitizer=cfi +//@ [kcfi] compile-flags: -Z sanitizer=kcfi +//@ [kcfi] compile-flags: -C panic=abort -C prefer-dynamic=off +//@ run-pass + +use std::marker::PhantomData; + +struct X { + _x: u8, + p: PhantomData, +} + +#[repr(transparent)] +struct Y(X); + +trait Fooable { + fn foo(&self, y: Y); +} + +struct Bar; + +impl Fooable for Bar { + fn foo(&self, _: Y) {} +} + +fn main() { + let x = &Bar as &dyn Fooable; + x.foo(Y(X {_x: 0, p: PhantomData})); +} diff --git a/tests/ui/sanitizer/cfi/sized-associated-ty.rs b/tests/ui/sanitizer/cfi/sized-associated-ty.rs new file mode 100644 index 00000000000..f5b4e22e9d9 --- /dev/null +++ b/tests/ui/sanitizer/cfi/sized-associated-ty.rs @@ -0,0 +1,38 @@ +// Check that we only elaborate non-`Self: Sized` associated types when +// erasing the receiver from trait ref. + +//@ revisions: cfi kcfi +// FIXME(#122848) Remove only-linux once OSX CFI binaries work +//@ only-linux +//@ [cfi] needs-sanitizer-cfi +//@ [kcfi] needs-sanitizer-kcfi +//@ compile-flags: -C target-feature=-crt-static +//@ [cfi] compile-flags: -C codegen-units=1 -C lto -C prefer-dynamic=off -C opt-level=0 +//@ [cfi] compile-flags: -Z sanitizer=cfi +//@ [kcfi] compile-flags: -Z sanitizer=kcfi +//@ [kcfi] compile-flags: -C panic=abort -C prefer-dynamic=off +//@ run-pass + +trait Foo { + type Bar<'a> + where + Self: Sized; + + fn test(&self); +} + +impl Foo for () { + type Bar<'a> = () + where + Self: Sized; + + fn test(&self) {} +} + +fn test(x: &dyn Foo) { + x.test(); +} + +fn main() { + test(&()); +} diff --git a/tests/ui/sanitizer/cfi/supertraits.rs b/tests/ui/sanitizer/cfi/supertraits.rs new file mode 100644 index 00000000000..4bb6177577f --- /dev/null +++ b/tests/ui/sanitizer/cfi/supertraits.rs @@ -0,0 +1,79 @@ +#![feature(trait_upcasting)] +// Check that super-traits are callable. + +//@ revisions: cfi kcfi +// FIXME(#122848) Remove only-linux once OSX CFI binaries work +//@ only-linux +//@ [cfi] needs-sanitizer-cfi +//@ [kcfi] needs-sanitizer-kcfi +//@ compile-flags: -C target-feature=-crt-static +//@ [cfi] compile-flags: -C codegen-units=1 -C lto -C prefer-dynamic=off -C opt-level=0 +//@ [cfi] compile-flags: -Z sanitizer=cfi +//@ [kcfi] compile-flags: -Z sanitizer=kcfi +//@ [kcfi] compile-flags: -C panic=abort -C prefer-dynamic=off +//@ run-pass + +trait Parent1 { + type P1; + fn p1(&self) -> Self::P1; + fn d(&self) -> i32 { + 42 + } +} + +trait Parent2 { + type P2; + fn p2(&self) -> Self::P2; +} + +trait Child : Parent1 + Parent2 { + type C; + fn c(&self) -> Self::C; +} + +struct Foo; + +impl Parent1 for Foo { + type P1 = u16; + fn p1(&self) -> Self::P1 { + println!("p1"); + 1 + } +} + +impl Parent2 for Foo { + type P2 = u32; + fn p2(&self) -> Self::P2 { + println!("p2"); + 2 + } +} + +impl Child for Foo { + type C = u8; + fn c(&self) -> Self::C { + println!("c"); + 0 + } +} + +fn main() { + // Child can access its own methods and super methods. + let x = &Foo as &dyn Child; + x.c(); + x.p1(); + x.p2(); + x.d(); + // Parents can be created and access their methods. + let y = &Foo as &dyn Parent1; + y.p1(); + y.d(); + let z = &Foo as &dyn Parent2; + z.p2(); + // Trait upcasting works + let x1 = x as &dyn Parent1; + x1.p1(); + x1.d(); + let x2 = x as &dyn Parent2; + x2.p2(); +} diff --git a/tests/ui/sanitizer/cfi/virtual-auto.rs b/tests/ui/sanitizer/cfi/virtual-auto.rs new file mode 100644 index 00000000000..6971d516a20 --- /dev/null +++ b/tests/ui/sanitizer/cfi/virtual-auto.rs @@ -0,0 +1,27 @@ +// Tests that calling a trait object method on a trait object with additional auto traits works. + +//@ revisions: cfi kcfi +// FIXME(#122848) Remove only-linux once OSX CFI binaries work +//@ only-linux +//@ [cfi] needs-sanitizer-cfi +//@ [kcfi] needs-sanitizer-kcfi +//@ compile-flags: -C target-feature=-crt-static +//@ [cfi] compile-flags: -C codegen-units=1 -C lto -C prefer-dynamic=off -C opt-level=0 +//@ [cfi] compile-flags: -Z sanitizer=cfi +//@ [kcfi] compile-flags: -Z sanitizer=kcfi +//@ [kcfi] compile-flags: -C panic=abort -C prefer-dynamic=off +//@ run-pass + +trait Foo { + fn foo(&self); +} + +struct Bar; +impl Foo for Bar { + fn foo(&self) {} +} + +pub fn main() { + let x: &(dyn Foo + Send) = &Bar; + x.foo(); +} diff --git a/tests/ui/sanitizer/cfi/with-rustc-lto-requires-single-codegen-unit.rs b/tests/ui/sanitizer/cfi/with-rustc-lto-requires-single-codegen-unit.rs new file mode 100644 index 00000000000..954e4ec3b85 --- /dev/null +++ b/tests/ui/sanitizer/cfi/with-rustc-lto-requires-single-codegen-unit.rs @@ -0,0 +1,8 @@ +// Verifies that `-Zsanitizer=cfi` with `-Clto` or `-Clto=thin` requires `-Ccodegen-units=1`. +// +//@ needs-sanitizer-cfi +//@ compile-flags: -Ccodegen-units=2 -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi + +#![feature(no_core)] +#![no_core] +#![no_main] diff --git a/tests/ui/sanitizer/cfi/with-rustc-lto-requires-single-codegen-unit.stderr b/tests/ui/sanitizer/cfi/with-rustc-lto-requires-single-codegen-unit.stderr new file mode 100644 index 00000000000..8d6dc1d8f1e --- /dev/null +++ b/tests/ui/sanitizer/cfi/with-rustc-lto-requires-single-codegen-unit.stderr @@ -0,0 +1,4 @@ +error: `-Zsanitizer=cfi` with `-Clto` requires `-Ccodegen-units=1` + +error: aborting due to 1 previous error + -- cgit 1.4.1-3-g733a5