Skip to content

Commit

Permalink
CFI: Encode Coroutine Witnesses
Browse files Browse the repository at this point in the history
  • Loading branch information
maurer committed Mar 27, 2024
1 parent 10a7aa1 commit a031ba9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,17 @@ fn encode_ty<'tcx>(
typeid.push_str(&s);
}

ty::CoroutineWitness(def_id, args, ..) => {
// u<length><name>[I<element-type1..element-typeN>E], where <element-type> is <subst>,
// as vendor extended type.
let mut s = String::new();
let name = encode_ty_name(tcx, *def_id);
let _ = write!(s, "u{}{}", name.len(), &name);
s.push_str(&encode_args(tcx, args, dict, options));
compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
typeid.push_str(&s);
}

// Pointer types
ty::Ref(region, ty0, ..) => {
// [U3mut]u3refI<element-type>E as vendor extended type qualifier and type
Expand Down Expand Up @@ -732,12 +743,7 @@ fn encode_ty<'tcx>(
}

// Unexpected types
ty::Alias(..)
| ty::Bound(..)
| ty::Error(..)
| ty::CoroutineWitness(..)
| ty::Infer(..)
| ty::Placeholder(..) => {
ty::Alias(..) | ty::Bound(..) | ty::Error(..) | ty::Infer(..) | ty::Placeholder(..) => {
bug!("encode_ty: unexpected `{:?}`", ty.kind());
}
};
Expand Down Expand Up @@ -790,7 +796,7 @@ fn transform_ty<'tcx>(
options: TransformTyOptions,
) -> Ty<'tcx> {
match ty.kind() {
ty::Float(..) | ty::Str | ty::Never | ty::Foreign(..) | ty::CoroutineWitness(..) => {}
ty::Float(..) | ty::Str | ty::Never | ty::Foreign(..) => {}

ty::Bool => {
if options.contains(EncodeTyOptions::NORMALIZE_INTEGERS) {
Expand Down Expand Up @@ -934,6 +940,14 @@ fn transform_ty<'tcx>(
ty = Ty::new_coroutine(tcx, *def_id, transform_args(tcx, args, parents, options));
}

ty::CoroutineWitness(def_id, args) => {
ty = Ty::new_coroutine_witness(
tcx,
*def_id,
transform_args(tcx, args, parents, options),
);
}

ty::Ref(region, ty0, ..) => {
if options.contains(TransformTyOptions::GENERALIZE_POINTERS) {
if ty.is_mutable_ptr() {
Expand Down
30 changes: 30 additions & 0 deletions tests/ui/sanitizer/cfi-async-closures.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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();
}

0 comments on commit a031ba9

Please sign in to comment.