Skip to content

Commit

Permalink
Rollup merge of #123635 - maurer:kcfi-no-assoc, r=compiler-errors
Browse files Browse the repository at this point in the history
CFI: Fix ICE in KCFI non-associated function pointers

We oddly weren't testing the more usual case of casting non-methods to function pointers. The KCFI shim insertion logic would ICE on these due to asking for an irrefutable associated item if we cast a function to a function pointer without needing a traditional shim.

r? `@compiler-errors`
  • Loading branch information
matthiaskrgr authored Apr 8, 2024
2 parents c0ac5d8 + 284da5d commit 0520200
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion compiler/rustc_middle/src/ty/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,10 @@ impl<'tcx> Instance<'tcx> {
// Reify `Trait::method` implementations if KCFI is enabled
// FIXME(maurer) only reify it if it is a vtable-safe function
_ if tcx.sess.is_sanitizer_kcfi_enabled()
&& tcx.associated_item(def_id).trait_item_def_id.is_some() =>
&& tcx
.opt_associated_item(def_id)
.and_then(|assoc| assoc.trait_item_def_id)
.is_some() =>
{
// If this function could also go in a vtable, we need to `ReifyShim` it with
// KCFI because it can only attach one type per function.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Verifies that casting a method to a function pointer works.
// Verifies that casting to a function pointer works.

//@ revisions: cfi kcfi
// FIXME(#122848) Remove only-linux once OSX CFI binaries work
Expand Down Expand Up @@ -46,12 +46,16 @@ impl Trait1 for Type1 {
fn foo(&self) {}
}

fn foo<T>(_: &T) {}

fn main() {
let type1 = Type1 {};
let f = <Type1 as Trait1>::foo;
f(&type1);
// Check again with different optimization barriers
S2 { f: <S as Foo>::foo }.foo(&S);
// Check mismatched #[track_caller]
S2 { f: <S as Foo>::bar }.foo(&S)
S2 { f: <S as Foo>::bar }.foo(&S);
// Check non-method functions
S2 { f: foo }.foo(&S)
}

0 comments on commit 0520200

Please sign in to comment.