Skip to content

Commit 0520200

Browse files
authored
Rollup merge of #123635 - maurer:kcfi-no-assoc, r=compiler-errors
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`
2 parents c0ac5d8 + 284da5d commit 0520200

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

compiler/rustc_middle/src/ty/instance.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,10 @@ impl<'tcx> Instance<'tcx> {
520520
// Reify `Trait::method` implementations if KCFI is enabled
521521
// FIXME(maurer) only reify it if it is a vtable-safe function
522522
_ if tcx.sess.is_sanitizer_kcfi_enabled()
523-
&& tcx.associated_item(def_id).trait_item_def_id.is_some() =>
523+
&& tcx
524+
.opt_associated_item(def_id)
525+
.and_then(|assoc| assoc.trait_item_def_id)
526+
.is_some() =>
524527
{
525528
// If this function could also go in a vtable, we need to `ReifyShim` it with
526529
// KCFI because it can only attach one type per function.

tests/ui/sanitizer/cfi-method-fn-ptr-cast.rs renamed to tests/ui/sanitizer/cfi-fn-ptr.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Verifies that casting a method to a function pointer works.
1+
// Verifies that casting to a function pointer works.
22

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

49+
fn foo<T>(_: &T) {}
50+
4951
fn main() {
5052
let type1 = Type1 {};
5153
let f = <Type1 as Trait1>::foo;
5254
f(&type1);
5355
// Check again with different optimization barriers
5456
S2 { f: <S as Foo>::foo }.foo(&S);
5557
// Check mismatched #[track_caller]
56-
S2 { f: <S as Foo>::bar }.foo(&S)
58+
S2 { f: <S as Foo>::bar }.foo(&S);
59+
// Check non-method functions
60+
S2 { f: foo }.foo(&S)
5761
}

0 commit comments

Comments
 (0)