forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#123106 - maurer:cfi-closures, r=compiler-er…
…rors CFI: Abstract Closures and Coroutines This will abstract coroutines in a moment, it's just abstracting closures for now to show `@rcvalle` This uses the same principal as the methods on traits - figure out the `dyn` type representing the fn trait, instantiate it, and attach that alias set. We're essentially just computing how we would be called in a dynamic context, and attaching that.
- Loading branch information
Showing
5 changed files
with
187 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// 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 | ||
//@ run-pass | ||
|
||
#![feature(async_closure)] | ||
#![feature(async_fn_traits)] | ||
|
||
use std::ops::AsyncFn; | ||
|
||
#[inline(never)] | ||
fn identity<T>(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<dyn FnOnce() -> _> = Box::new(f) as _; | ||
let _ = g(); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// 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 | ||
//@ compile-flags: --test | ||
//@ run-pass | ||
|
||
#![feature(fn_traits)] | ||
|
||
fn foo<'a, T>() -> Box<dyn Fn(&'a T) -> &'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<F: 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<dyn FnOnce()> = Box::new(|| {}) as _; | ||
f(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Verifies that we can call dynamic coroutines | ||
|
||
//@ 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 | ||
//@ compile-flags: --test | ||
//@ run-pass | ||
|
||
#![feature(coroutines)] | ||
#![feature(coroutine_trait)] | ||
|
||
use std::ops::{Coroutine, CoroutineState}; | ||
use std::pin::{pin, Pin}; | ||
|
||
fn main() { | ||
let mut coro = |x: i32| { | ||
yield x; | ||
"done" | ||
}; | ||
let mut abstract_coro: Pin<&mut dyn Coroutine<i32,Yield=i32,Return=&'static str>> = pin!(coro); | ||
assert_eq!(abstract_coro.as_mut().resume(2), CoroutineState::Yielded(2)); | ||
assert_eq!(abstract_coro.as_mut().resume(0), CoroutineState::Complete("done")); | ||
} |