Skip to content

Commit 20b6e5a

Browse files
authored
Rollup merge of #111851 - rcvalle:rust-cfi-fix-111515, r=bjorn3
CFI: Fix encode_region: unexpected ReEarlyBound(0, 'a) Fixes #111515 and complements #106547 by adding support for encoding early bound regions and also excluding projections when transforming trait objects' traits into their identities before emitting type checks.
2 parents 416ae3c + 9bbdfea commit 20b6e5a

4 files changed

+78
-16
lines changed

compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,11 @@ fn encode_region<'tcx>(
272272
s.push('E');
273273
compress(dict, DictKey::Region(region), &mut s);
274274
}
275-
RegionKind::ReErased => {
275+
RegionKind::ReEarlyBound(..) | RegionKind::ReErased => {
276276
s.push_str("u6region");
277277
compress(dict, DictKey::Region(region), &mut s);
278278
}
279-
RegionKind::ReEarlyBound(..)
280-
| RegionKind::ReFree(..)
279+
RegionKind::ReFree(..)
281280
| RegionKind::ReStatic
282281
| RegionKind::ReError(_)
283282
| RegionKind::ReVar(..)
@@ -704,14 +703,15 @@ fn transform_predicates<'tcx>(
704703
) -> &'tcx List<ty::PolyExistentialPredicate<'tcx>> {
705704
let predicates: Vec<ty::PolyExistentialPredicate<'tcx>> = predicates
706705
.iter()
707-
.map(|predicate| match predicate.skip_binder() {
706+
.filter_map(|predicate| match predicate.skip_binder() {
708707
ty::ExistentialPredicate::Trait(trait_ref) => {
709708
let trait_ref = ty::TraitRef::identity(tcx, trait_ref.def_id);
710-
ty::Binder::dummy(ty::ExistentialPredicate::Trait(
709+
Some(ty::Binder::dummy(ty::ExistentialPredicate::Trait(
711710
ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref),
712-
))
711+
)))
713712
}
714-
_ => predicate,
713+
ty::ExistentialPredicate::Projection(..) => None,
714+
ty::ExistentialPredicate::AutoTrait(..) => Some(predicate),
715715
})
716716
.collect();
717717
tcx.mk_poly_existential_predicates(&predicates)

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -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:]]+}}_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"}
539+
// CHECK: ![[TYPE96]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function2FnIu5paramEu6regionEEE"}
540+
// CHECK: ![[TYPE97]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function2FnIu5paramEu6regionEES3_E"}
541+
// CHECK: ![[TYPE98]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function2FnIu5paramEu6regionEES3_S3_E"}
542+
// CHECK: ![[TYPE99]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function5FnMutIu5paramEu6regionEEE"}
543+
// CHECK: ![[TYPE100]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function5FnMutIu5paramEu6regionEES3_E"}
544+
// CHECK: ![[TYPE101]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function5FnMutIu5paramEu6regionEES3_S3_E"}
545+
// CHECK: ![[TYPE102]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnceIu5paramEu6regionEEE"}
546+
// CHECK: ![[TYPE103]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnceIu5paramEu6regionEES3_E"}
547+
// CHECK: ![[TYPE104]] = !{i64 0, !"_ZTSFvu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops8function6FnOnceIu5paramEu6regionEES3_S3_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"}

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

+31
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ impl<T, U> Trait3<U> for T {
3939
}
4040
}
4141

42+
pub trait Trait4<'a, T> {
43+
type Output: 'a;
44+
fn qux(&self, _: &T) -> Self::Output;
45+
}
46+
47+
pub struct Type4;
48+
49+
impl<'a, T, U> Trait4<'a, U> for T {
50+
type Output = &'a i32;
51+
fn qux(&self, _: &U) -> Self::Output {
52+
&0
53+
}
54+
}
55+
4256
pub fn foo1(a: &dyn Trait1) {
4357
a.foo();
4458
// CHECK-LABEL: define{{.*}}4foo1{{.*}}!type !{{[0-9]+}}
@@ -84,6 +98,23 @@ pub fn bar3() {
8498
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%[0-9]}}, metadata !"[[TYPE3:[[:print:]]+]]")
8599
}
86100

101+
pub fn foo4<'a>(a: &dyn Trait4<'a, Type4, Output = &'a i32>) {
102+
let b = Type4;
103+
a.qux(&b);
104+
// CHECK-LABEL: define{{.*}}4foo4{{.*}}!type !{{[0-9]+}}
105+
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%[0-9]}}, metadata !"[[TYPE4:[[:print:]]+]]")
106+
}
107+
108+
pub fn bar4<'a>() {
109+
let a = Type4;
110+
foo4(&a);
111+
let b = &a as &dyn Trait4<'a, Type4, Output = &'a i32>;
112+
b.qux(&a);
113+
// CHECK-LABEL: define{{.*}}4bar4{{.*}}!type !{{[0-9]+}}
114+
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%[0-9]}}, metadata !"[[TYPE4:[[:print:]]+]]")
115+
}
116+
87117
// CHECK: !{{[0-9]+}} = !{i64 0, !"[[TYPE1]]"}
88118
// CHECK: !{{[0-9]+}} = !{i64 0, !"[[TYPE2]]"}
89119
// CHECK: !{{[0-9]+}} = !{i64 0, !"[[TYPE3]]"}
120+
// CHECK: !{{[0-9]+}} = !{i64 0, !"[[TYPE4]]"}

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

+31
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ impl<T, U> Trait3<U> for T {
6363
}
6464
}
6565

66+
pub trait Trait4<'a, T> {
67+
type Output: 'a;
68+
fn qux(&self, _: &T) -> Self::Output;
69+
}
70+
71+
pub struct Type4;
72+
73+
impl<'a, T, U> Trait4<'a, U> for T {
74+
type Output = &'a i32;
75+
fn qux(&self, _: &U) -> Self::Output {
76+
&0
77+
}
78+
}
79+
6680
pub fn foo1(a: &dyn Trait1) {
6781
a.foo();
6882
// CHECK-LABEL: define{{.*}}4foo1{{.*}}!{{<unknown kind #36>|kcfi_type}} !{{[0-9]+}}
@@ -108,6 +122,23 @@ pub fn bar3() {
108122
// 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:]]+]]) ]
109123
}
110124

125+
pub fn foo4<'a>(a: &dyn Trait4<'a, Type4, Output = &'a i32>) {
126+
let b = Type4;
127+
a.qux(&b);
128+
// CHECK-LABEL: define{{.*}}4foo4{{.*}}!{{<unknown kind #36>|kcfi_type}} !{{[0-9]+}}
129+
// CHECK: call align 4 {{ptr|i32\*}} %{{[0-9]}}({{\{\}\*|ptr}} align 1 {{%[a-z]\.0|%_[0-9]}}, {{\{\}\*|ptr|%Type4\*}} align 1 {{%[a-z]\.0|%_[0-9]}}){{.*}}[ "kcfi"(i32 [[TYPE4:[[:print:]]+]]) ]
130+
}
131+
132+
pub fn bar4<'a>() {
133+
let a = Type4;
134+
foo4(&a);
135+
let b = &a as &dyn Trait4<'a, Type4, Output = &'a i32>;
136+
b.qux(&a);
137+
// CHECK-LABEL: define{{.*}}4bar4{{.*}}!{{<unknown kind #36>|kcfi_type}} !{{[0-9]+}}
138+
// CHECK: call align 4 {{ptr|i32\*}} %{{[0-9]}}({{\{\}\*|ptr}} align 1 {{%[a-z]\.0|%_[0-9]}}, {{\{\}\*|ptr|%Type4\*}} align 1 {{%[a-z]\.0|%_[0-9]}}){{.*}}[ "kcfi"(i32 [[TYPE4:[[:print:]]+]]) ]
139+
}
140+
111141
// CHECK: !{{[0-9]+}} = !{i32 [[TYPE1]]}
112142
// CHECK: !{{[0-9]+}} = !{i32 [[TYPE2]]}
113143
// CHECK: !{{[0-9]+}} = !{i32 [[TYPE3]]}
144+
// CHECK: !{{[0-9]+}} = !{i32 [[TYPE4]]}

0 commit comments

Comments
 (0)