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.
CFI: Rewrite closure and coroutine instances to their trait method
Similar to methods on a trait object, the most common way to indirectly call a closure or coroutine is through the vtable on the appropriate trait. This uses the same approach as we use for trait methods, after backing out the trait arguments from the type.
- Loading branch information
Showing
5 changed files
with
144 additions
and
22 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
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")); | ||
} |