Skip to content

CFI: Fix types that implement Fn, FnMut, or FnOnce #144936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use rustc_hir as hir;
use rustc_hir::LangItem;
use rustc_middle::bug;
use rustc_middle::ty::{
self, ExistentialPredicateStableCmpExt as _, Instance, InstanceKind, IntTy, List, TraitRef, Ty,
TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, UintTy,
self, ExistentialPredicateStableCmpExt as _, Instance, IntTy, List, TraitRef, Ty, TyCtxt,
TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, UintTy,
};
use rustc_span::def_id::DefId;
use rustc_span::{DUMMY_SP, sym};
Expand Down Expand Up @@ -458,6 +458,15 @@ pub(crate) fn transform_instance<'tcx>(
instance
}

fn default_or_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> Option<DefId> {
match instance.def {
ty::InstanceKind::Item(def_id) | ty::InstanceKind::FnPtrShim(def_id, _) => {
tcx.opt_associated_item(def_id).map(|item| item.def_id)
}
_ => None,
}
}

fn implemented_method<'tcx>(
tcx: TyCtxt<'tcx>,
instance: Instance<'tcx>,
Expand All @@ -474,11 +483,9 @@ fn implemented_method<'tcx>(
trait_method = tcx.associated_item(method_id);
trait_id = trait_ref.skip_binder().def_id;
impl_id
} else if let InstanceKind::Item(def_id) = instance.def
&& let Some(trait_method_bound) = tcx.opt_associated_item(def_id)
{
// Provided method in a `trait` block
trait_method = trait_method_bound;
} else if let Some(trait_method_def_id) = default_or_shim(tcx, instance) {
// Provided method in a `trait` block or a synthetic `shim`
trait_method = tcx.associated_item(trait_method_def_id);
method_id = instance.def_id();
trait_id = tcx.trait_of_assoc(method_id)?;
trait_ref = ty::EarlyBinder::bind(TraitRef::from_method(tcx, trait_id, instance.args));
Expand Down
30 changes: 30 additions & 0 deletions tests/ui/sanitizer/cfi/fn-trait-objects.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Verifies that types that implement the Fn, FnMut, or FnOnce traits can be
// called through their trait methods.
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Ctarget-feature=-crt-static -Ccodegen-units=1 -Clto -Cprefer-dynamic=off -Copt-level=0 -Zsanitizer=cfi --test
//@ run-pass

#![feature(fn_traits)]
#![feature(unboxed_closures)]

fn foo(_a: u32) {}

#[test]
fn test_fn_trait() {
let f: Box<dyn Fn(u32)> = Box::new(foo);
Fn::call(&f, (0,));
}

#[test]
fn test_fnmut_trait() {
let mut a = 0;
let mut f: Box<dyn FnMut(u32)> = Box::new(|x| a += x);
FnMut::call_mut(&mut f, (1,));
}

#[test]
fn test_fnonce_trait() {
let f: Box<dyn FnOnce(u32)> = Box::new(foo);
FnOnce::call_once(f, (2,));
}
30 changes: 30 additions & 0 deletions tests/ui/sanitizer/kcfi/fn-trait-objects.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Verifies that types that implement the Fn, FnMut, or FnOnce traits can be
// called through their trait methods.
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Ctarget-feature=-crt-static -Zpanic_abort_tests -Cpanic=abort -Cprefer-dynamic=off -Copt-level=0 -Zsanitizer=kcfi --test
//@ run-pass

#![feature(fn_traits)]
#![feature(unboxed_closures)]

fn foo(_a: u32) {}

#[test]
fn test_fn_trait() {
let f: Box<dyn Fn(u32)> = Box::new(foo);
Fn::call(&f, (0,));
}

#[test]
fn test_fnmut_trait() {
let mut a = 0;
let mut f: Box<dyn FnMut(u32)> = Box::new(|x| a += x);
FnMut::call_mut(&mut f, (1,));
}

#[test]
fn test_fnonce_trait() {
let f: Box<dyn FnOnce(u32)> = Box::new(foo);
FnOnce::call_once(f, (2,));
}
Loading