-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[clang] callee_type metadata for indirect calls #163233
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dd76511
[clang] callee_type metadata for indirect calls
Prabhuk 4bedac1
Refactor call graph type metadata addition code out of CFI flow.
Prabhuk 369ac05
Clean up adding type metadata for potential indirect targets.
Prabhuk 2112b81
More cleanups.
Prabhuk 9ac4b00
Format fixes.
Prabhuk a9345a7
Add test to cover callback/internal linkage/addresstaken case.
Prabhuk ed7ced5
Use CHECK-LABEL by breaking up the check line into 2.
Prabhuk e348ff8
Use LABEL instead of DAG in tests.
Prabhuk ca97344
Remove expensive DAG based checks from tests.
Prabhuk 36cd62f
Use -SAME to define variables after -LABEL checks.
Prabhuk 2574080
Test minor fix.
Prabhuk 7a09d24
Merge branch 'main' into clang_callee_type
Prabhuk 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Tests that callback function whose address is taken is attached Type ID metadata | ||
// as it is a potential indirect call target. | ||
|
||
// RUN: %clang_cc1 -triple x86_64-unknown-linux -fexperimental-call-graph-section \ | ||
// RUN: -emit-llvm -o %t %s | ||
// RUN: FileCheck %s < %t | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
typedef void (*CallbackFn)(int); | ||
|
||
// Callback function with "internal" linkage. | ||
// CHECK-LABEL: define internal void @_ZL10myCallbacki( | ||
// CHECK-SAME: {{.*}} !type [[F_CALLBACK:![0-9]+]] | ||
static void myCallback(int value) | ||
{ | ||
volatile int sink = value; | ||
(void)sink; | ||
} | ||
|
||
int takeCallbackAddress() { | ||
// Take the address of the callback explicitly (address-taken function) | ||
CallbackFn cb = &myCallback; | ||
// Store the address in a volatile pointer to keep it observable | ||
volatile void* addr = (void*)cb; | ||
(void)addr; | ||
|
||
return 0; | ||
} | ||
|
||
// CHECK: [[F_CALLBACK]] = !{i64 0, !"_ZTSFviE.generalized"} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Tests that we assign appropriate identifiers to indirect calls and targets | ||
// specifically for C++ templates. | ||
|
||
// RUN: %clang_cc1 -triple x86_64-unknown-linux -fexperimental-call-graph-section \ | ||
// RUN: -emit-llvm -o %t %s | ||
// RUN: FileCheck --check-prefix=FT %s < %t | ||
// RUN: FileCheck --check-prefix=CST %s < %t | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// Class definitions and template classes (check for indirect target metadata) | ||
|
||
class Cls1 {}; | ||
|
||
// Cls2 is instantiated with T=Cls1 in foo(). Following checks are for this | ||
// instantiation. | ||
template <class T> | ||
class Cls2 { | ||
public: | ||
// FT-LABEL: define {{.*}} void @_ZN4Cls2I4Cls1E2f1Ev( | ||
// FT-SAME: {{.*}} !type [[F_TCLS2F1:![0-9]+]] | ||
void f1() {} | ||
|
||
// FT-LABEL: define {{.*}} void @_ZN4Cls2I4Cls1E2f2ES0_( | ||
// FT-SAME: {{.*}} !type [[F_TCLS2F2:![0-9]+]] | ||
void f2(T a) {} | ||
|
||
// FT-LABEL: define {{.*}} void @_ZN4Cls2I4Cls1E2f3EPS0_( | ||
// FT-SAME: {{.*}} !type [[F_TCLS2F3:![0-9]+]] | ||
void f3(T *a) {} | ||
|
||
// FT-LABEL: define {{.*}} void @_ZN4Cls2I4Cls1E2f4EPKS0_( | ||
// FT-SAME: {{.*}} !type [[F_TCLS2F4:![0-9]+]] | ||
void f4(const T *a) {} | ||
|
||
// FT-LABEL: define {{.*}} void @_ZN4Cls2I4Cls1E2f5ERS0_( | ||
// FT-SAME: {{.*}} !type [[F_TCLS2F5:![0-9]+]] | ||
void f5(T &a) {} | ||
|
||
// FT-LABEL: define {{.*}} void @_ZN4Cls2I4Cls1E2f6ERKS0_( | ||
// FT-SAME: {{.*}} !type [[F_TCLS2F6:![0-9]+]] | ||
void f6(const T &a) {} | ||
|
||
// Mixed type function pointer member | ||
T *(*fp)(T a, T *b, const T *c, T &d, const T &e); | ||
}; | ||
|
||
// FT: [[F_TCLS2F1]] = !{i64 0, !"_ZTSFvvE.generalized"} | ||
// FT: [[F_TCLS2F2]] = !{i64 0, !"_ZTSFv4Cls1E.generalized"} | ||
// FT: [[F_TCLS2F3]] = !{i64 0, !"_ZTSFvP4Cls1E.generalized"} | ||
// FT: [[F_TCLS2F4]] = !{i64 0, !"_ZTSFvPK4Cls1E.generalized"} | ||
// FT: [[F_TCLS2F5]] = !{i64 0, !"_ZTSFvR4Cls1E.generalized"} | ||
// FT: [[F_TCLS2F6]] = !{i64 0, !"_ZTSFvRK4Cls1E.generalized"} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// Callsites (check for indirect callsite operand bundles) | ||
|
||
template <class T> | ||
T *T_func(T a, T *b, const T *c, T &d, const T &e) { return b; } | ||
|
||
// CST-LABEL: define {{.*}} @_Z3foov | ||
// CST-SAME: {{.*}} !type [[F_TCLS2F1:![0-9]+]] | ||
void foo() { | ||
// Methods for Cls2<Cls1> is checked above within the template description. | ||
Cls2<Cls1> Obj; | ||
|
||
Obj.fp = T_func<Cls1>; | ||
Cls1 Cls1Obj; | ||
|
||
// CST: call noundef ptr %{{.*}}, !callee_type [[F_TFUNC_CLS1_CT:![0-9]+]] | ||
Obj.fp(Cls1Obj, &Cls1Obj, &Cls1Obj, Cls1Obj, Cls1Obj); | ||
|
||
// Make indirect calls to Cls2's member methods | ||
auto fp_f1 = &Cls2<Cls1>::f1; | ||
auto fp_f2 = &Cls2<Cls1>::f2; | ||
auto fp_f3 = &Cls2<Cls1>::f3; | ||
auto fp_f4 = &Cls2<Cls1>::f4; | ||
auto fp_f5 = &Cls2<Cls1>::f5; | ||
auto fp_f6 = &Cls2<Cls1>::f6; | ||
|
||
auto *Obj2Ptr = &Obj; | ||
|
||
// CST: call void %{{.*}}, !callee_type [[F_TCLS2F1_CT:![0-9]+]] | ||
(Obj2Ptr->*fp_f1)(); | ||
|
||
// CST: call void %{{.*}}, !callee_type [[F_TCLS2F2_CT:![0-9]+]] | ||
(Obj2Ptr->*fp_f2)(Cls1Obj); | ||
|
||
// CST: call void %{{.*}}, !callee_type [[F_TCLS2F3_CT:![0-9]+]] | ||
(Obj2Ptr->*fp_f3)(&Cls1Obj); | ||
|
||
// CST: call void %{{.*}}, !callee_type [[F_TCLS2F4_CT:![0-9]+]] | ||
(Obj2Ptr->*fp_f4)(&Cls1Obj); | ||
|
||
// CST: call void %{{.*}}, !callee_type [[F_TCLS2F5_CT:![0-9]+]] | ||
(Obj2Ptr->*fp_f5)(Cls1Obj); | ||
|
||
// CST: call void %{{.*}}, !callee_type [[F_TCLS2F6_CT:![0-9]+]] | ||
(Obj2Ptr->*fp_f6)(Cls1Obj); | ||
} | ||
|
||
// CST-LABEL: define {{.*}} @_Z6T_funcI4Cls1EPT_S1_S2_PKS1_RS1_RS3_( | ||
// CST-SAME: {{.*}} !type [[F_TFUNC_CLS1:![0-9]+]] | ||
|
||
// CST: [[F_TCLS2F1]] = !{i64 0, !"_ZTSFvvE.generalized"} | ||
// CST: [[F_TFUNC_CLS1_CT]] = !{[[F_TFUNC_CLS1:![0-9]+]]} | ||
// CST: [[F_TFUNC_CLS1]] = !{i64 0, !"_ZTSFP4Cls1S_S0_PKS_RS_RS1_E.generalized"} | ||
// CST: [[F_TCLS2F1_CT]] = !{[[F_TCLS2F1:![0-9]+]]} | ||
// CST: [[F_TCLS2F2_CT]] = !{[[F_TCLS2F2:![0-9]+]]} | ||
// CST: [[F_TCLS2F2]] = !{i64 0, !"_ZTSFv4Cls1E.generalized"} | ||
// CST: [[F_TCLS2F3_CT]] = !{[[F_TCLS2F3:![0-9]+]]} | ||
// CST: [[F_TCLS2F3]] = !{i64 0, !"_ZTSFvP4Cls1E.generalized"} | ||
// CST: [[F_TCLS2F4_CT]] = !{[[F_TCLS2F4:![0-9]+]]} | ||
// CST: [[F_TCLS2F4]] = !{i64 0, !"_ZTSFvPK4Cls1E.generalized"} | ||
// CST: [[F_TCLS2F5_CT]] = !{[[F_TCLS2F5:![0-9]+]]} | ||
// CST: [[F_TCLS2F5]] = !{i64 0, !"_ZTSFvR4Cls1E.generalized"} | ||
// CST: [[F_TCLS2F6_CT]] = !{[[F_TCLS2F6:![0-9]+]]} | ||
// CST: [[F_TCLS2F6]] = !{i64 0, !"_ZTSFvRK4Cls1E.generalized"} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Tests that we assign appropriate identifiers to indirect calls and targets | ||
// specifically for virtual methods. | ||
|
||
// RUN: %clang_cc1 -triple x86_64-unknown-linux -fexperimental-call-graph-section \ | ||
// RUN: -emit-llvm -o %t %s | ||
// RUN: FileCheck --check-prefix=FT %s < %t | ||
// RUN: FileCheck --check-prefix=CST %s < %t | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// Class definitions (check for indirect target metadata) | ||
|
||
class Base { | ||
public: | ||
// FT-LABEL: define {{.*}} @_ZN4Base2vfEPc( | ||
// FT-SAME: {{.*}} !type [[F_TVF:![0-9]+]] | ||
virtual int vf(char *a) { return 0; }; | ||
}; | ||
|
||
class Derived : public Base { | ||
public: | ||
// FT: define {{.*}} @_ZN7Derived2vfEPc({{.*}} !type [[F_TVF]] | ||
int vf(char *a) override { return 1; }; | ||
}; | ||
|
||
// FT: [[F_TVF]] = !{i64 0, !"_ZTSFiPcE.generalized"} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// Callsites (check for indirect callsite operand bundles) | ||
|
||
// CST-LABEL: define {{.*}} @_Z3foov | ||
void foo() { | ||
auto B = Base(); | ||
auto D = Derived(); | ||
|
||
Base *Bptr = &B; | ||
Base *BptrToD = &D; | ||
Derived *Dptr = &D; | ||
|
||
auto FpBaseVf = &Base::vf; | ||
auto FpDerivedVf = &Derived::vf; | ||
|
||
// CST: call noundef i32 %{{.*}}, !callee_type [[F_TVF_CT:![0-9]+]] | ||
(Bptr->*FpBaseVf)(0); | ||
|
||
// CST: call noundef i32 %{{.*}}, !callee_type [[F_TVF_CT:![0-9]+]] | ||
(BptrToD->*FpBaseVf)(0); | ||
|
||
// CST: call noundef i32 %{{.*}}, !callee_type [[F_TVF_CT:![0-9]+]] | ||
(Dptr->*FpBaseVf)(0); | ||
|
||
// CST: call noundef i32 %{{.*}}, !callee_type [[F_TVF_CT:![0-9]+]] | ||
(Dptr->*FpDerivedVf)(0); | ||
} | ||
|
||
// CST: [[F_TVF_CT]] = !{[[F_TVF:![0-9]+]]} | ||
// CST: [[F_TVF]] = !{i64 0, !"_ZTSFiPcE.generalized"} |
Oops, something went wrong.
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.
@arsenm @ilovepi --
The "IgnoreCallbackUses" here refers to the callback attribute:
https://clang.llvm.org/docs/AttributeReference.html#callback
As part of my LLVM change (https://github.com/llvm/llvm-project/blob/main/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp#L1702), I added a test that verifies hasAddressTaken call handles callback functions correctly https://github.com/llvm/llvm-project/blob/main/llvm/test/CodeGen/X86/call-graph-section-addrtaken.ll
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.
Added a new test case to cover the hasAddressTaken case. This test also covers callback function handling.