Skip to content

Commit f067935

Browse files
committed
CFI: Fix encode_ty: unexpected Param(B/#1)
Fixes rust-lang#111510 and complements rust-lang#106547 by adding support for encoding type parameters and also by transforming trait objects' traits into their identities before emitting type checks.
1 parent 077fc26 commit f067935

4 files changed

+189
-64
lines changed

compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

+52-16
Original file line numberDiff line numberDiff line change
@@ -673,14 +673,21 @@ fn encode_ty<'tcx>(
673673
typeid.push_str(&s);
674674
}
675675

676+
// Type parameters
677+
ty::Param(..) => {
678+
// u5param as vendor extended type
679+
let mut s = String::from("u5param");
680+
compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
681+
typeid.push_str(&s);
682+
}
683+
676684
// Unexpected types
677685
ty::Bound(..)
678686
| ty::Error(..)
679687
| ty::GeneratorWitness(..)
680688
| ty::GeneratorWitnessMIR(..)
681689
| ty::Infer(..)
682690
| ty::Alias(..)
683-
| ty::Param(..)
684691
| ty::Placeholder(..) => {
685692
bug!("encode_ty: unexpected `{:?}`", ty.kind());
686693
}
@@ -689,6 +696,41 @@ fn encode_ty<'tcx>(
689696
typeid
690697
}
691698

699+
/// Transforms predicates for being encoded and used in the substitution dictionary.
700+
fn transform_predicates<'tcx>(
701+
tcx: TyCtxt<'tcx>,
702+
predicates: &List<ty::PolyExistentialPredicate<'tcx>>,
703+
_options: EncodeTyOptions,
704+
) -> &'tcx List<ty::PolyExistentialPredicate<'tcx>> {
705+
let predicates: Vec<ty::PolyExistentialPredicate<'tcx>> = predicates
706+
.iter()
707+
.map(|predicate| match predicate.skip_binder() {
708+
ty::ExistentialPredicate::Trait(trait_ref) => {
709+
let trait_ref = ty::TraitRef::identity(tcx, trait_ref.def_id);
710+
ty::Binder::dummy(ty::ExistentialPredicate::Trait(
711+
ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref),
712+
))
713+
}
714+
_ => predicate,
715+
})
716+
.collect();
717+
tcx.mk_poly_existential_predicates(&predicates)
718+
}
719+
720+
/// Transforms substs for being encoded and used in the substitution dictionary.
721+
fn transform_substs<'tcx>(
722+
tcx: TyCtxt<'tcx>,
723+
substs: SubstsRef<'tcx>,
724+
options: TransformTyOptions,
725+
) -> SubstsRef<'tcx> {
726+
let substs = substs.iter().map(|subst| match subst.unpack() {
727+
GenericArgKind::Type(ty) if ty.is_c_void(tcx) => tcx.mk_unit().into(),
728+
GenericArgKind::Type(ty) => transform_ty(tcx, ty, options).into(),
729+
_ => subst,
730+
});
731+
tcx.mk_substs_from_iter(substs)
732+
}
733+
692734
// Transforms a ty:Ty for being encoded and used in the substitution dictionary. It transforms all
693735
// c_void types into unit types unconditionally, generalizes pointers if
694736
// TransformTyOptions::GENERALIZE_POINTERS option is set, and normalizes integers if
@@ -697,7 +739,7 @@ fn transform_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, options: TransformTyOptio
697739
let mut ty = ty;
698740

699741
match ty.kind() {
700-
ty::Float(..) | ty::Char | ty::Str | ty::Never | ty::Foreign(..) | ty::Dynamic(..) => {}
742+
ty::Float(..) | ty::Char | ty::Str | ty::Never | ty::Foreign(..) => {}
701743

702744
ty::Bool => {
703745
if options.contains(EncodeTyOptions::NORMALIZE_INTEGERS) {
@@ -870,6 +912,14 @@ fn transform_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, options: TransformTyOptio
870912
}
871913
}
872914

915+
ty::Dynamic(predicates, _region, kind) => {
916+
ty = tcx.mk_dynamic(
917+
transform_predicates(tcx, predicates, options),
918+
tcx.lifetimes.re_erased,
919+
*kind,
920+
);
921+
}
922+
873923
ty::Bound(..)
874924
| ty::Error(..)
875925
| ty::GeneratorWitness(..)
@@ -885,20 +935,6 @@ fn transform_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, options: TransformTyOptio
885935
ty
886936
}
887937

888-
/// Transforms substs for being encoded and used in the substitution dictionary.
889-
fn transform_substs<'tcx>(
890-
tcx: TyCtxt<'tcx>,
891-
substs: SubstsRef<'tcx>,
892-
options: TransformTyOptions,
893-
) -> SubstsRef<'tcx> {
894-
let substs = substs.iter().map(|subst| match subst.unpack() {
895-
GenericArgKind::Type(ty) if ty.is_c_void(tcx) => tcx.mk_unit().into(),
896-
GenericArgKind::Type(ty) => transform_ty(tcx, ty, options).into(),
897-
_ => subst,
898-
});
899-
tcx.mk_substs_from_iter(substs)
900-
}
901-
902938
/// Returns a type metadata identifier for the specified FnAbi using the Itanium C++ ABI with vendor
903939
/// extended type qualifiers and types for Rust types that are not used at the FFI boundary.
904940
#[instrument(level = "trace", skip(tcx))]

tests/codegen/sanitizer-cfi-emit-type-metadata-id-itanium-cxx-abi.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl<T> Trait1<T> for i32 {
4444
}
4545

4646
// Trait implementation
47-
impl<T> Trait1<T> for Struct1<T> {
47+
impl<T, U> Trait1<T> for Struct1<U> {
4848
fn foo(&self) { }
4949
}
5050

@@ -536,15 +536,15 @@ pub fn foo149(_: Type14<Bar>, _: Type14<Bar>, _: Type14<Bar>) { }
536536
// CHECK: ![[TYPE93]] = !{i64 0, !"_ZTSFvPFu3i32S_EE"}
537537
// CHECK: ![[TYPE94]] = !{i64 0, !"_ZTSFvPFu3i32S_ES0_E"}
538538
// CHECK: ![[TYPE95]] = !{i64 0, !"_ZTSFvPFu3i32S_ES0_S0_E"}
539-
// CHECK: ![[TYPE96]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function2FnIu5tupleIu3i32EEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIS0_ES_u6regionEEE"}
540-
// CHECK: ![[TYPE97]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function2FnIu5tupleIu3i32EEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIS0_ES_u6regionEES5_E"}
541-
// CHECK: ![[TYPE98]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function2FnIu5tupleIu3i32EEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIS0_ES_u6regionEES5_S5_E"}
542-
// CHECK: ![[TYPE99]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function5FnMutIu5tupleIu3i32EEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIS0_ES_u6regionEEE"}
543-
// CHECK: ![[TYPE100]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function5FnMutIu5tupleIu3i32EEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIS0_ES_u6regionEES5_E"}
544-
// CHECK: ![[TYPE101]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function5FnMutIu5tupleIu3i32EEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIS0_ES_u6regionEES5_S5_E"}
545-
// CHECK: ![[TYPE102]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnceIu5tupleIu3i32EEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIS0_ES_u6regionEEE"}
546-
// CHECK: ![[TYPE103]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnceIu5tupleIu3i32EEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIS0_ES_u6regionEES5_E"}
547-
// CHECK: ![[TYPE104]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnceIu5tupleIu3i32EEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIS0_ES_u6regionEES5_S5_E"}
539+
// CHECK: ![[TYPE96]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function2FnIu5paramEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIu5tupleIu3i32EES1_u6regionEEE"}
540+
// CHECK: ![[TYPE97]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function2FnIu5paramEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIu5tupleIu3i32EES1_u6regionEES6_E"}
541+
// CHECK: ![[TYPE98]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function2FnIu5paramEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIu5tupleIu3i32EES1_u6regionEES6_S6_E"}
542+
// CHECK: ![[TYPE99]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function5FnMutIu5paramEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIu5tupleIu3i32EES1_u6regionEEE"}
543+
// CHECK: ![[TYPE100]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function5FnMutIu5paramEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIu5tupleIu3i32EES1_u6regionEES6_E"}
544+
// CHECK: ![[TYPE101]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function5FnMutIu5paramEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIu5tupleIu3i32EES1_u6regionEES6_S6_E"}
545+
// CHECK: ![[TYPE102]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnceIu5paramEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIu5tupleIu3i32EES1_u6regionEEE"}
546+
// CHECK: ![[TYPE103]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnceIu5paramEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIu5tupleIu3i32EES1_u6regionEES6_E"}
547+
// CHECK: ![[TYPE104]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnceIu5paramEu{{[0-9]+}}NtNtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnce6OutputIu5tupleIu3i32EES1_u6regionEES6_S6_E"}
548548
// CHECK: ![[TYPE105]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtC{{[[:print:]]+}}_4core6marker4Sendu6regionEEE"}
549549
// CHECK: ![[TYPE106]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtC{{[[:print:]]+}}_4core6marker4Sendu6regionEES2_E"}
550550
// CHECK: ![[TYPE107]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtC{{[[:print:]]+}}_4core6marker4Sendu6regionEES2_S2_E"}
@@ -566,9 +566,9 @@ pub fn foo149(_: Type14<Bar>, _: Type14<Bar>, _: Type14<Bar>) { }
566566
// CHECK: ![[TYPE123]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NvNINvC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi3fn18{{[{}][{}]}}impl{{[}][}]}}3fooIu3i32EE"}
567567
// CHECK: ![[TYPE124]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NvNINvC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi3fn18{{[{}][{}]}}impl{{[}][}]}}3fooIu3i32ES0_E"}
568568
// CHECK: ![[TYPE125]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NvNINvC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi3fn18{{[{}][{}]}}impl{{[}][}]}}3fooIu3i32ES0_S0_E"}
569-
// CHECK: ![[TYPE126]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NvNtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi6Trait13fooIu3dynIu{{[0-9]+}}NtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi6Trait1Iu3i32Eu6regionES_EE"}
570-
// CHECK: ![[TYPE127]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NvNtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi6Trait13fooIu3dynIu{{[0-9]+}}NtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi6Trait1Iu3i32Eu6regionES_ES3_E"}
571-
// CHECK: ![[TYPE128]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NvNtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi6Trait13fooIu3dynIu{{[0-9]+}}NtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi6Trait1Iu3i32Eu6regionES_ES3_S3_E"}
569+
// CHECK: ![[TYPE126]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NvNtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi6Trait13fooIu3dynIu{{[0-9]+}}NtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi6Trait1Iu5paramEu6regionEu3i32EE"}
570+
// CHECK: ![[TYPE127]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NvNtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi6Trait13fooIu3dynIu{{[0-9]+}}NtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi6Trait1Iu5paramEu6regionEu3i32ES4_E"}
571+
// CHECK: ![[TYPE128]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NvNtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi6Trait13fooIu3dynIu{{[0-9]+}}NtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi6Trait1Iu5paramEu6regionEu3i32ES4_S4_E"}
572572
// CHECK: ![[TYPE129]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NvNtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi6Trait13fooIu3i32S_EE"}
573573
// CHECK: ![[TYPE130]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NvNtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi6Trait13fooIu3i32S_ES0_E"}
574574
// CHECK: ![[TYPE131]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NvNtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi6Trait13fooIu3i32S_ES0_S0_E"}
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,89 @@
11
// Verifies that type metadata identifiers for trait objects are emitted correctly.
22
//
33
// needs-sanitizer-cfi
4-
// compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi
4+
// compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Ctarget-feature=-crt-static -Zsanitizer=cfi
55

66
#![crate_type="lib"]
77

8-
trait Trait1 {
8+
pub trait Trait1 {
99
fn foo(&self);
1010
}
1111

12-
struct Type1;
12+
#[derive(Clone, Copy)]
13+
pub struct Type1;
1314

1415
impl Trait1 for Type1 {
1516
fn foo(&self) {
1617
}
1718
}
1819

19-
pub fn foo() {
20-
let a = Type1;
20+
pub trait Trait2<T> {
21+
fn bar(&self);
22+
}
23+
24+
pub struct Type2;
25+
26+
impl Trait2<i32> for Type2 {
27+
fn bar(&self) {
28+
}
29+
}
30+
31+
pub trait Trait3<T> {
32+
fn baz(&self, _: &T);
33+
}
34+
35+
pub struct Type3;
36+
37+
impl<T, U> Trait3<U> for T {
38+
fn baz(&self, _: &U) {
39+
}
40+
}
41+
42+
pub fn foo1(a: &dyn Trait1) {
2143
a.foo();
22-
// CHECK-LABEL: define{{.*}}foo{{.*}}!type !{{[0-9]+}}
23-
// CHECK: call <sanitizer_cfi_emit_type_metadata_trait_objects::Type1 as sanitizer_cfi_emit_type_metadata_trait_objects::Trait1>::foo
44+
// CHECK-LABEL: define{{.*}}4foo1{{.*}}!type !{{[0-9]+}}
45+
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%[0-9]}}, metadata !"[[TYPE1:[[:print:]]+]]")
2446
}
2547

26-
pub fn bar() {
48+
pub fn bar1() {
2749
let a = Type1;
2850
let b = &a as &dyn Trait1;
2951
b.foo();
30-
// CHECK-LABEL: define{{.*}}bar{{.*}}!type !{{[0-9]+}}
31-
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%0|%1}}, metadata !"[[TYPE1:[[:print:]]+]]")
52+
// CHECK-LABEL: define{{.*}}4bar1{{.*}}!type !{{[0-9]+}}
53+
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%[0-9]}}, metadata !"[[TYPE2:[[:print:]]+]]")
3254
}
3355

34-
pub fn baz() {
35-
let a = Type1;
36-
let b = &a as &dyn Trait1;
37-
a.foo();
38-
b.foo();
39-
// CHECK-LABEL: define{{.*}}baz{{.*}}!type !{{[0-9]+}}
40-
// CHECK: call <sanitizer_cfi_emit_type_metadata_trait_objects::Type1 as sanitizer_cfi_emit_type_metadata_trait_objects::Trait1>::foo
41-
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%0|%1}}, metadata !"[[TYPE1:[[:print:]]+]]")
56+
pub fn foo2<T>(a: &dyn Trait2<T>) {
57+
a.bar();
58+
// CHECK-LABEL: define{{.*}}4foo2{{.*}}!type !{{[0-9]+}}
59+
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%[0-9]}}, metadata !"[[TYPE2:[[:print:]]+]]")
60+
}
61+
62+
pub fn bar2() {
63+
let a = Type2;
64+
foo2(&a);
65+
let b = &a as &dyn Trait2<i32>;
66+
b.bar();
67+
// CHECK-LABEL: define{{.*}}4bar2{{.*}}!type !{{[0-9]+}}
68+
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%[0-9]}}, metadata !"[[TYPE2:[[:print:]]+]]")
69+
}
70+
71+
pub fn foo3(a: &dyn Trait3<Type3>) {
72+
let b = Type3;
73+
a.baz(&b);
74+
// CHECK-LABEL: define{{.*}}4foo3{{.*}}!type !{{[0-9]+}}
75+
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%[0-9]}}, metadata !"[[TYPE3:[[:print:]]+]]")
76+
}
77+
78+
pub fn bar3() {
79+
let a = Type3;
80+
foo3(&a);
81+
let b = &a as &dyn Trait3<Type3>;
82+
b.baz(&a);
83+
// CHECK-LABEL: define{{.*}}4bar3{{.*}}!type !{{[0-9]+}}
84+
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%[0-9]}}, metadata !"[[TYPE3:[[:print:]]+]]")
4285
}
4386

4487
// CHECK: !{{[0-9]+}} = !{i64 0, !"[[TYPE1]]"}
88+
// CHECK: !{{[0-9]+}} = !{i64 0, !"[[TYPE2]]"}
89+
// CHECK: !{{[0-9]+}} = !{i64 0, !"[[TYPE3]]"}

tests/codegen/sanitizer-kcfi-emit-type-metadata-trait-objects.rs

+61-17
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,84 @@ trait Freeze { }
3030
#[lang="drop_in_place"]
3131
fn drop_in_place_fn<T>() { }
3232

33-
trait Trait1 {
33+
pub trait Trait1 {
3434
fn foo(&self);
3535
}
3636

37-
struct Type1;
37+
pub struct Type1;
3838

3939
impl Trait1 for Type1 {
4040
fn foo(&self) {
4141
}
4242
}
4343

44-
pub fn foo() {
45-
let a = Type1;
44+
pub trait Trait2<T> {
45+
fn bar(&self);
46+
}
47+
48+
pub struct Type2;
49+
50+
impl Trait2<i32> for Type2 {
51+
fn bar(&self) {
52+
}
53+
}
54+
55+
pub trait Trait3<T> {
56+
fn baz(&self, _: &T);
57+
}
58+
59+
pub struct Type3;
60+
61+
impl<T, U> Trait3<U> for T {
62+
fn baz(&self, _: &U) {
63+
}
64+
}
65+
66+
pub fn foo1(a: &dyn Trait1) {
4667
a.foo();
47-
// CHECK-LABEL: define{{.*}}foo{{.*}}!{{<unknown kind #36>|kcfi_type}} !{{[0-9]+}}
48-
// CHECK: call <sanitizer_kcfi_emit_type_metadata_trait_objects::Type1 as sanitizer_kcfi_emit_type_metadata_trait_objects::Trait1>::foo
68+
// CHECK-LABEL: define{{.*}}4foo1{{.*}}!{{<unknown kind #36>|kcfi_type}} !{{[0-9]+}}
69+
// CHECK: call void %{{[0-9]}}({{\{\}\*|ptr}} align 1 {{%[a-z]\.0|%_[0-9]}}){{.*}}[ "kcfi"(i32 [[TYPE1:[[:print:]]+]]) ]
4970
}
5071

51-
pub fn bar() {
72+
pub fn bar1() {
5273
let a = Type1;
5374
let b = &a as &dyn Trait1;
5475
b.foo();
55-
// CHECK-LABEL: define{{.*}}bar{{.*}}!{{<unknown kind #36>|kcfi_type}} !{{[0-9]+}}
56-
// CHECK: call void %0({{\{\}\*|ptr}} align 1 {{%b\.0|%_1}}){{.*}}[ "kcfi"(i32 [[TYPE1:[[:print:]]+]]) ]
76+
// CHECK-LABEL: define{{.*}}4bar1{{.*}}!{{<unknown kind #36>|kcfi_type}} !{{[0-9]+}}
77+
// CHECK: call void %{{[0-9]}}({{\{\}\*|ptr}} align 1 {{%[a-z]\.0|%_[0-9]}}){{.*}}[ "kcfi"(i32 [[TYPE1:[[:print:]]+]]) ]
5778
}
5879

59-
pub fn baz() {
60-
let a = Type1;
61-
let b = &a as &dyn Trait1;
62-
a.foo();
63-
b.foo();
64-
// CHECK-LABEL: define{{.*}}baz{{.*}}!{{<unknown kind #36>|kcfi_type}} !{{[0-9]+}}
65-
// CHECK: call <sanitizer_kcfi_emit_type_metadata_trait_objects::Type1 as sanitizer_kcfi_emit_type_metadata_trait_objects::Trait1>::foo
66-
// CHECK: call void %0({{\{\}\*|ptr}} align 1 {{%b\.0|%_1}}){{.*}}[ "kcfi"(i32 [[TYPE1:[[:print:]]+]]) ]
80+
pub fn foo2<T>(a: &dyn Trait2<T>) {
81+
a.bar();
82+
// CHECK-LABEL: define{{.*}}4foo2{{.*}}!{{<unknown kind #36>|kcfi_type}} !{{[0-9]+}}
83+
// CHECK: call void %{{[0-9]}}({{\{\}\*|ptr}} align 1 {{%[a-z]\.0|%_[0-9]}}){{.*}}[ "kcfi"(i32 [[TYPE2:[[:print:]]+]]) ]
84+
}
85+
86+
pub fn bar2() {
87+
let a = Type2;
88+
foo2(&a);
89+
let b = &a as &dyn Trait2<i32>;
90+
b.bar();
91+
// CHECK-LABEL: define{{.*}}4bar2{{.*}}!{{<unknown kind #36>|kcfi_type}} !{{[0-9]+}}
92+
// CHECK: call void %{{[0-9]}}({{\{\}\*|ptr}} align 1 {{%[a-z]\.0|%_[0-9]}}){{.*}}[ "kcfi"(i32 [[TYPE2:[[:print:]]+]]) ]
93+
}
94+
95+
pub fn foo3(a: &dyn Trait3<Type3>) {
96+
let b = Type3;
97+
a.baz(&b);
98+
// CHECK-LABEL: define{{.*}}4foo3{{.*}}!{{<unknown kind #36>|kcfi_type}} !{{[0-9]+}}
99+
// CHECK: call void %{{[0-9]}}({{\{\}\*|ptr}} align 1 {{%[a-z]\.0|%_[0-9]}}, {{\{\}\*|ptr|%Type3\*}} align 1 {{%[a-z]\.0|%_[0-9]}}){{.*}}[ "kcfi"(i32 [[TYPE3:[[:print:]]+]]) ]
100+
}
101+
102+
pub fn bar3() {
103+
let a = Type3;
104+
foo3(&a);
105+
let b = &a as &dyn Trait3<Type3>;
106+
b.baz(&a);
107+
// CHECK-LABEL: define{{.*}}4bar3{{.*}}!{{<unknown kind #36>|kcfi_type}} !{{[0-9]+}}
108+
// CHECK: call void %{{[0-9]}}({{\{\}\*|ptr}} align 1 {{%[a-z]\.0|%_[0-9]}}, {{\{\}\*|ptr|%Type3\*}} align 1 {{%[a-z]\.0|%_[0-9]}}){{.*}}[ "kcfi"(i32 [[TYPE3:[[:print:]]+]]) ]
67109
}
68110

69111
// CHECK: !{{[0-9]+}} = !{i32 [[TYPE1]]}
112+
// CHECK: !{{[0-9]+}} = !{i32 [[TYPE2]]}
113+
// CHECK: !{{[0-9]+}} = !{i32 [[TYPE3]]}

0 commit comments

Comments
 (0)