Skip to content

Commit 66b2708

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 cbcb16d commit 66b2708

File tree

5 files changed

+80
-20
lines changed

5 files changed

+80
-20
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

+26-20
Original file line numberDiff line numberDiff line change
@@ -1045,28 +1045,34 @@ pub fn transform_fnabi<'tcx>(
10451045
&& !fn_abi.args.is_empty()
10461046
&& fn_abi.args[0].layout.ty.is_ref()
10471047
{
1048-
// Replace the concrete self by a reference to a trait object (i.e., perform
1049-
// type erasure).
1050-
let self_ty = if fn_abi.args[0].layout.ty.is_mutable_ptr() {
1051-
Ty::new_mut_ref(
1052-
tcx,
1053-
tcx.lifetimes.re_erased,
1054-
new_dynamic_trait(
1055-
tcx,
1056-
trait_ref.skip_binder().def_id,
1057-
trait_ref.skip_binder().args,
1058-
),
1059-
)
1048+
let self_ty = if options.contains(EncodeTyOptions::NO_TYPE_ERASURE) {
1049+
// Do not perform type erasure for assigning a secondary type id to methods
1050+
// with their concrete self so they can be used as function pointers.
1051+
return fn_abi.clone();
10601052
} else {
1061-
Ty::new_imm_ref(
1062-
tcx,
1063-
tcx.lifetimes.re_erased,
1064-
new_dynamic_trait(
1053+
// Replace the concrete self by a reference to a trait object (i.e., perform
1054+
// type erasure).
1055+
if fn_abi.args[0].layout.ty.is_mutable_ptr() {
1056+
Ty::new_mut_ref(
10651057
tcx,
1066-
trait_ref.skip_binder().def_id,
1067-
trait_ref.skip_binder().args,
1068-
),
1069-
)
1058+
tcx.lifetimes.re_erased,
1059+
new_dynamic_trait(
1060+
tcx,
1061+
trait_ref.skip_binder().def_id,
1062+
trait_ref.skip_binder().args,
1063+
),
1064+
)
1065+
} else {
1066+
Ty::new_imm_ref(
1067+
tcx,
1068+
tcx.lifetimes.re_erased,
1069+
new_dynamic_trait(
1070+
tcx,
1071+
trait_ref.skip_binder().def_id,
1072+
trait_ref.skip_binder().args,
1073+
),
1074+
)
1075+
}
10701076
};
10711077
let mut fn_abi = fn_abi.clone();
10721078
// HACK(rcvalle): It is okay to not replace or update the entire ArgAbi here
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,21 @@
1+
// Verifies that casting a method to a function pointer works.
2+
//
3+
//@ needs-sanitizer-cfi
4+
//@ compile-flags: -Clto -Cprefer-dynamic=off -Ctarget-feature=-crt-static -Zsanitizer=cfi -Copt-level=0
5+
//@ run-pass
6+
7+
trait Trait1 {
8+
fn foo(&self);
9+
}
10+
11+
struct Type1;
12+
13+
impl Trait1 for Type1 {
14+
fn foo(&self) {}
15+
}
16+
17+
fn main() {
18+
let type1 = Type1 {};
19+
let f = <Type1 as Trait1>::foo;
20+
f(&type1);
21+
}

0 commit comments

Comments
 (0)