-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CFI: Fix encode_ty: unexpected Param(B/#1) #111697
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 63 additions & 18 deletions
81
tests/codegen/sanitizer-cfi-emit-type-metadata-trait-objects.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,89 @@ | ||
// Verifies that type metadata identifiers for trait objects are emitted correctly. | ||
// | ||
// needs-sanitizer-cfi | ||
// compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi | ||
// compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Ctarget-feature=-crt-static -Zsanitizer=cfi | ||
|
||
#![crate_type="lib"] | ||
|
||
trait Trait1 { | ||
pub trait Trait1 { | ||
fn foo(&self); | ||
} | ||
|
||
struct Type1; | ||
#[derive(Clone, Copy)] | ||
pub struct Type1; | ||
|
||
impl Trait1 for Type1 { | ||
fn foo(&self) { | ||
} | ||
} | ||
|
||
pub fn foo() { | ||
let a = Type1; | ||
pub trait Trait2<T> { | ||
fn bar(&self); | ||
} | ||
|
||
pub struct Type2; | ||
|
||
impl Trait2<i32> for Type2 { | ||
fn bar(&self) { | ||
} | ||
} | ||
|
||
pub trait Trait3<T> { | ||
fn baz(&self, _: &T); | ||
} | ||
|
||
pub struct Type3; | ||
|
||
impl<T, U> Trait3<U> for T { | ||
fn baz(&self, _: &U) { | ||
} | ||
} | ||
|
||
pub fn foo1(a: &dyn Trait1) { | ||
a.foo(); | ||
// CHECK-LABEL: define{{.*}}foo{{.*}}!type !{{[0-9]+}} | ||
// CHECK: call <sanitizer_cfi_emit_type_metadata_trait_objects::Type1 as sanitizer_cfi_emit_type_metadata_trait_objects::Trait1>::foo | ||
// CHECK-LABEL: define{{.*}}4foo1{{.*}}!type !{{[0-9]+}} | ||
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%[0-9]}}, metadata !"[[TYPE1:[[:print:]]+]]") | ||
} | ||
|
||
pub fn bar() { | ||
pub fn bar1() { | ||
let a = Type1; | ||
let b = &a as &dyn Trait1; | ||
b.foo(); | ||
// CHECK-LABEL: define{{.*}}bar{{.*}}!type !{{[0-9]+}} | ||
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%0|%1}}, metadata !"[[TYPE1:[[:print:]]+]]") | ||
// CHECK-LABEL: define{{.*}}4bar1{{.*}}!type !{{[0-9]+}} | ||
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%[0-9]}}, metadata !"[[TYPE2:[[:print:]]+]]") | ||
} | ||
|
||
pub fn baz() { | ||
let a = Type1; | ||
let b = &a as &dyn Trait1; | ||
a.foo(); | ||
b.foo(); | ||
// CHECK-LABEL: define{{.*}}baz{{.*}}!type !{{[0-9]+}} | ||
// CHECK: call <sanitizer_cfi_emit_type_metadata_trait_objects::Type1 as sanitizer_cfi_emit_type_metadata_trait_objects::Trait1>::foo | ||
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%0|%1}}, metadata !"[[TYPE1:[[:print:]]+]]") | ||
pub fn foo2<T>(a: &dyn Trait2<T>) { | ||
a.bar(); | ||
// CHECK-LABEL: define{{.*}}4foo2{{.*}}!type !{{[0-9]+}} | ||
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%[0-9]}}, metadata !"[[TYPE2:[[:print:]]+]]") | ||
} | ||
|
||
pub fn bar2() { | ||
let a = Type2; | ||
foo2(&a); | ||
let b = &a as &dyn Trait2<i32>; | ||
b.bar(); | ||
// CHECK-LABEL: define{{.*}}4bar2{{.*}}!type !{{[0-9]+}} | ||
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%[0-9]}}, metadata !"[[TYPE2:[[:print:]]+]]") | ||
} | ||
|
||
pub fn foo3(a: &dyn Trait3<Type3>) { | ||
let b = Type3; | ||
a.baz(&b); | ||
// CHECK-LABEL: define{{.*}}4foo3{{.*}}!type !{{[0-9]+}} | ||
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%[0-9]}}, metadata !"[[TYPE3:[[:print:]]+]]") | ||
} | ||
|
||
pub fn bar3() { | ||
let a = Type3; | ||
foo3(&a); | ||
let b = &a as &dyn Trait3<Type3>; | ||
b.baz(&a); | ||
// CHECK-LABEL: define{{.*}}4bar3{{.*}}!type !{{[0-9]+}} | ||
// CHECK: call i1 @llvm.type.test({{i8\*|ptr}} {{%f|%[0-9]}}, metadata !"[[TYPE3:[[:print:]]+]]") | ||
} | ||
|
||
// CHECK: !{{[0-9]+}} = !{i64 0, !"[[TYPE1]]"} | ||
// CHECK: !{{[0-9]+}} = !{i64 0, !"[[TYPE2]]"} | ||
// CHECK: !{{[0-9]+}} = !{i64 0, !"[[TYPE3]]"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without
-Zpolymorphize
this shouldn't happen unless a monomorphization is missing I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason for this is when transforming the concrete self into a reference to a trait object before emitting type metadata identifiers for trait methods (in predefine_fn), the trait resolved from the method is the identity trait (i.e., early bound), and this is also necessary for trait objects with generic type parameters (see Trait3 in tests).
Unless there is a way to bind the traits at that time, we'll need to work with identity traits (i.e., early bound) and add support for encoding early bound types both when emitting type metadata identifiers for trait methods and when emitting type checks.