-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
4 changed files
with
100 additions
and
18 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 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,45 @@ | ||
// Check various forms of dynamic closure calls | ||
// | ||
// 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 | ||
//@ 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); | ||
} |
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,23 @@ | ||
// Verifies that we can call dynamic coroutines | ||
// | ||
// 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 | ||
|
||
#![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")); | ||
} |