Skip to content

Commit 9449923

Browse files
committed
CFI: Fix methods as function pointer cast
Fix casting between methods and function pointers by assigning a secondary type id to methods with their concrete self so they can be used as function pointers.
1 parent 8b9e47c commit 9449923

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

compiler/rustc_codegen_llvm/src/declare.rs

+10
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,30 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
141141

142142
if self.tcx.sess.is_sanitizer_cfi_enabled() {
143143
if let Some(instance) = instance {
144+
let mut typeids = Vec::new();
144145
let typeid = typeid_for_instance(self.tcx, instance, TypeIdOptions::empty());
146+
typeids.push(typeid.clone());
145147
self.set_type_metadata(llfn, typeid);
146148
let typeid =
147149
typeid_for_instance(self.tcx, instance, TypeIdOptions::GENERALIZE_POINTERS);
150+
typeids.push(typeid.clone());
148151
self.add_type_metadata(llfn, typeid);
149152
let typeid =
150153
typeid_for_instance(self.tcx, instance, TypeIdOptions::NORMALIZE_INTEGERS);
154+
typeids.push(typeid.clone());
151155
self.add_type_metadata(llfn, typeid);
152156
let typeid = typeid_for_instance(
153157
self.tcx,
154158
instance,
155159
TypeIdOptions::GENERALIZE_POINTERS | TypeIdOptions::NORMALIZE_INTEGERS,
156160
);
161+
typeids.push(typeid.clone());
157162
self.add_type_metadata(llfn, typeid);
163+
let typeid =
164+
typeid_for_instance(self.tcx, instance, TypeIdOptions::NO_TYPE_ERASURE);
165+
if !typeids.contains(&typeid) {
166+
self.add_type_metadata(llfn, typeid);
167+
}
158168
} else {
159169
let typeid = typeid_for_fnabi(self.tcx, fn_abi, TypeIdOptions::empty());
160170
self.set_type_metadata(llfn, typeid);

compiler/rustc_symbol_mangling/src/typeid.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ bitflags! {
1616
const GENERALIZE_POINTERS = 1;
1717
const GENERALIZE_REPR_C = 2;
1818
const NORMALIZE_INTEGERS = 4;
19+
const NO_TYPE_ERASURE = 8;
1920
}
2021
}
2122

compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,8 @@ pub fn typeid_for_instance<'tcx>(
11161116
instance.args = strip_receiver_auto(tcx, instance.args)
11171117
}
11181118

1119-
if let Some(impl_id) = tcx.impl_of_method(instance.def_id())
1119+
if !options.contains(EncodeTyOptions::NO_TYPE_ERASURE)
1120+
&& let Some(impl_id) = tcx.impl_of_method(instance.def_id())
11201121
&& let Some(trait_ref) = tcx.impl_trait_ref(impl_id)
11211122
{
11221123
let impl_method = tcx.associated_item(instance.def_id());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Verifies that a secondary type metadata identifier is assigned to methods with their concrete
2+
// self so they can be used as function pointers.
3+
//
4+
//@ needs-sanitizer-cfi
5+
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static
6+
7+
#![crate_type="lib"]
8+
9+
trait Trait1 {
10+
fn foo(&self);
11+
}
12+
13+
struct Type1;
14+
15+
impl Trait1 for Type1 {
16+
fn foo(&self) {}
17+
// CHECK: define{{.*}}3foo{{.*}}!type ![[TYPE1:[0-9]+]] !type !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}} !type ![[TYPE2:[0-9]+]]
18+
}
19+
20+
21+
// CHECK: ![[TYPE1]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtC{{[[:print:]]+}}_{{[[:print:]]+}}6Trait1u6regionEEE"}
22+
// CHECK: ![[TYPE2]] = !{i64 0, !"_ZTSFvu3refIu{{[0-9]+}}NtC{{[[:print:]]+}}_{{[[:print:]]+}}5Type1EE"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Verifies that casting a method to a function pointer works.
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+
trait Trait1 {
10+
fn foo(&self);
11+
}
12+
13+
struct Type1;
14+
15+
impl Trait1 for Type1 {
16+
fn foo(&self) {}
17+
}
18+
19+
fn main() {
20+
let type1 = Type1 {};
21+
let f = <Type1 as Trait1>::foo;
22+
f(&type1);
23+
}

0 commit comments

Comments
 (0)