Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9fe0fe1

Browse files
committedMar 26, 2024·
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.
1 parent eec30e9 commit 9fe0fe1

File tree

4 files changed

+100
-18
lines changed

4 files changed

+100
-18
lines changed
 

‎compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

+32
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use rustc_data_structures::base_n;
1111
use rustc_data_structures::fx::FxHashMap;
1212
use rustc_hir as hir;
13+
use rustc_hir::lang_items::LangItem;
1314
use rustc_middle::ty::layout::IntegerExt;
1415
use rustc_middle::ty::TypeVisitableExt;
1516
use rustc_middle::ty::{
@@ -1150,6 +1151,37 @@ pub fn typeid_for_instance<'tcx>(
11501151
tcx.mk_args_trait(invoke_ty, trait_ref.args.into_iter().skip(1));
11511152
instance.args = instance.args.rebase_onto(tcx, impl_id, abstract_trait_args);
11521153
}
1154+
} else if tcx.is_closure_like(instance.def_id()) {
1155+
// We're either a closure or a coroutine. Our goal is to find the trait we're defined on,
1156+
// instantiate it, and take the type of its only method as our own.
1157+
let closure_ty = instance.ty(tcx, ty::ParamEnv::reveal_all());
1158+
let (trait_id, tuple_args) = match closure_ty.kind() {
1159+
ty::Closure(_def_id, args) => {
1160+
let closure_args = ty::ClosureArgs { args };
1161+
let trait_id = tcx.fn_trait_kind_to_def_id(closure_args.kind()).unwrap();
1162+
let tuple_args = closure_args.sig().inputs().no_bound_vars().unwrap()[0];
1163+
(trait_id, tuple_args)
1164+
}
1165+
ty::Coroutine(_def_id, args) => (
1166+
tcx.require_lang_item(LangItem::Coroutine, None),
1167+
ty::CoroutineArgs { args }.resume_ty(),
1168+
),
1169+
x => bug!("Unexpected type kind for closure-like: {x:?}"),
1170+
};
1171+
let trait_ref = ty::TraitRef::new(tcx, trait_id, [closure_ty, tuple_args]);
1172+
let invoke_ty = trait_object_ty(tcx, ty::Binder::dummy(trait_ref));
1173+
let abstract_args = tcx.mk_args_trait(invoke_ty, trait_ref.args.into_iter().skip(1));
1174+
// There should be exactly one method on this trait, and it should be the one we're
1175+
// defining.
1176+
let call = tcx
1177+
.associated_items(trait_id)
1178+
.in_definition_order()
1179+
.find(|it| it.kind == ty::AssocKind::Fn)
1180+
.expect("No call-family function on closure-like Fn trait?")
1181+
.def_id;
1182+
1183+
instance.def = ty::InstanceDef::Virtual(call, 0);
1184+
instance.args = abstract_args;
11531185
}
11541186

11551187
let fn_abi = tcx

‎tests/ui/sanitizer/cfi-closure-fn-ptr-cast.rs

-18
This file was deleted.

‎tests/ui/sanitizer/cfi-closures.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Check various forms of dynamic closure calls
2+
//
3+
// FIXME(#122848): Remove only-linux when fixed.
4+
//@ only-linux
5+
//@ needs-sanitizer-cfi
6+
//@ compile-flags: -Clto -Copt-level=0 -Cprefer-dynamic=off -Ctarget-feature=-crt-static -Zsanitizer=cfi
7+
//@ compile-flags: --test
8+
//@ run-pass
9+
10+
#![feature(fn_traits)]
11+
12+
fn foo<'a, T>() -> Box<dyn Fn(&'a T) -> &'a T> {
13+
Box::new(|x| x)
14+
}
15+
16+
#[test]
17+
fn dyn_fn_with_params() {
18+
let x = 3;
19+
let f = foo();
20+
f(&x);
21+
// FIXME remove once drops are working.
22+
std::mem::forget(f);
23+
}
24+
25+
#[test]
26+
fn call_fn_trait() {
27+
let f: &(dyn Fn()) = &(|| {}) as _;
28+
f.call(());
29+
}
30+
31+
#[test]
32+
fn fn_ptr_cast() {
33+
let f: &fn() = &((|| ()) as _);
34+
f();
35+
}
36+
37+
fn use_fnmut<F: FnMut()>(mut f: F) {
38+
f()
39+
}
40+
41+
#[test]
42+
fn fn_to_fnmut() {
43+
let f: &(dyn Fn()) = &(|| {}) as _;
44+
use_fnmut(f);
45+
}

‎tests/ui/sanitizer/cfi-coroutine.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Verifies that we can call dynamic coroutines
2+
//
3+
// FIXME(#122848): Remove only-linux when fixed.
4+
//@ only-linux
5+
//@ needs-sanitizer-cfi
6+
//@ compile-flags: -Clto -Copt-level=0 -Cprefer-dynamic=off -Ctarget-feature=-crt-static -Zsanitizer=cfi
7+
//@ run-pass
8+
9+
#![feature(coroutines)]
10+
#![feature(coroutine_trait)]
11+
12+
use std::ops::{Coroutine, CoroutineState};
13+
use std::pin::{pin, Pin};
14+
15+
fn main() {
16+
let mut coro = |x: i32| {
17+
yield x;
18+
"done"
19+
};
20+
let mut abstract_coro: Pin<&mut dyn Coroutine<i32,Yield=i32,Return=&'static str>> = pin!(coro);
21+
assert_eq!(abstract_coro.as_mut().resume(2), CoroutineState::Yielded(2));
22+
assert_eq!(abstract_coro.as_mut().resume(0), CoroutineState::Complete("done"));
23+
}

0 commit comments

Comments
 (0)
Please sign in to comment.