Skip to content

Commit b24c33a

Browse files
authored
[cfi] Enable -fsanitize-annotate-debug-info functionality for CFI checks (llvm#139809)
This connects the -fsanitize-annotate-debug-info plumbing (llvm#138577) to CFI check codegen, using SanitizerAnnotateDebugInfo() (llvm#139965) and SanitizerInfoFromCFIKind (llvm#140117). Note: SanitizerAnnotateDebugInfo() is updated to a public function because it is needed in ItaniumCXXABI. Updates the tests from llvm#139149.
1 parent 12fb0d4 commit b24c33a

File tree

7 files changed

+130
-96
lines changed

7 files changed

+130
-96
lines changed

clang/lib/CodeGen/CGClass.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2814,6 +2814,9 @@ void CodeGenFunction::EmitVTablePtrCheckForCall(const CXXRecordDecl *RD,
28142814
if (!SanOpts.has(SanitizerKind::CFICastStrict))
28152815
RD = LeastDerivedClassWithSameLayout(RD);
28162816

2817+
auto [Ordinal, _] = SanitizerInfoFromCFICheckKind(TCK);
2818+
ApplyDebugLocation ApplyTrapDI(*this, SanitizerAnnotateDebugInfo(Ordinal));
2819+
28172820
EmitVTablePtrCheck(RD, VTable, TCK, Loc);
28182821
}
28192822

@@ -2836,6 +2839,9 @@ void CodeGenFunction::EmitVTablePtrCheckForCast(QualType T, Address Derived,
28362839
if (!SanOpts.has(SanitizerKind::CFICastStrict))
28372840
ClassDecl = LeastDerivedClassWithSameLayout(ClassDecl);
28382841

2842+
auto [Ordinal, _] = SanitizerInfoFromCFICheckKind(TCK);
2843+
ApplyDebugLocation ApplyTrapDI(*this, SanitizerAnnotateDebugInfo(Ordinal));
2844+
28392845
llvm::BasicBlock *ContBlock = nullptr;
28402846

28412847
if (MayBeNull) {
@@ -2937,6 +2943,8 @@ llvm::Value *CodeGenFunction::EmitVTableTypeCheckedLoad(
29372943
SanitizerScope SanScope(this);
29382944

29392945
EmitSanitizerStatReport(llvm::SanStat_CFI_VCall);
2946+
ApplyDebugLocation ApplyTrapDI(
2947+
*this, SanitizerAnnotateDebugInfo(SanitizerKind::SO_CFIVCall));
29402948

29412949
llvm::Metadata *MD =
29422950
CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0));

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3971,6 +3971,8 @@ void CodeGenFunction::EmitCfiCheckFail() {
39713971
{Addr, AllVtables}),
39723972
IntPtrTy);
39733973

3974+
// TODO: the instructions above are not annotated with debug info. It is
3975+
// inconvenient to do so because we have not determined SanitizerKind yet.
39743976
const std::pair<int, SanitizerKind::SanitizerOrdinal> CheckKinds[] = {
39753977
{CFITCK_VCall, SanitizerKind::SO_CFIVCall},
39763978
{CFITCK_NVCall, SanitizerKind::SO_CFINVCall},
@@ -3981,6 +3983,9 @@ void CodeGenFunction::EmitCfiCheckFail() {
39813983
for (auto CheckKindOrdinalPair : CheckKinds) {
39823984
int Kind = CheckKindOrdinalPair.first;
39833985
SanitizerKind::SanitizerOrdinal Ordinal = CheckKindOrdinalPair.second;
3986+
3987+
ApplyDebugLocation ApplyTrapDI(*this, SanitizerAnnotateDebugInfo(Ordinal));
3988+
39843989
llvm::Value *Cond =
39853990
Builder.CreateICmpNE(CheckKind, llvm::ConstantInt::get(Int8Ty, Kind));
39863991
if (CGM.getLangOpts().Sanitize.has(Ordinal))
@@ -6315,6 +6320,8 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType,
63156320
(!TargetDecl || !isa<FunctionDecl>(TargetDecl))) {
63166321
SanitizerScope SanScope(this);
63176322
EmitSanitizerStatReport(llvm::SanStat_CFI_ICall);
6323+
ApplyDebugLocation ApplyTrapDI(
6324+
*this, SanitizerAnnotateDebugInfo(SanitizerKind::SO_CFIICall));
63186325

63196326
llvm::Metadata *MD;
63206327
if (CGM.getCodeGenOpts().SanitizeCfiICallGeneralizePointers)

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,11 +2816,6 @@ class CodeGenFunction : public CodeGenTypeCache {
28162816
void emitStoresForInitAfterBZero(llvm::Constant *Init, Address Loc,
28172817
bool isVolatile, bool IsAutoInit);
28182818

2819-
/// Returns debug info, with additional annotation if enabled by
2820-
/// CGM.getCodeGenOpts().SanitizeAnnotateDebugInfo[CheckKindOrdinal].
2821-
llvm::DILocation *
2822-
SanitizerAnnotateDebugInfo(SanitizerKind::SanitizerOrdinal CheckKindOrdinal);
2823-
28242819
public:
28252820
// Captures all the allocas created during the scope of its RAII object.
28262821
struct AllocaTrackerRAII {
@@ -3367,6 +3362,11 @@ class CodeGenFunction : public CodeGenTypeCache {
33673362
llvm::Value *Index, QualType IndexType,
33683363
QualType IndexedType, bool Accessed);
33693364

3365+
/// Returns debug info, with additional annotation if enabled by
3366+
/// CGM.getCodeGenOpts().SanitizeAnnotateDebugInfo[CheckKindOrdinal].
3367+
llvm::DILocation *
3368+
SanitizerAnnotateDebugInfo(SanitizerKind::SanitizerOrdinal CheckKindOrdinal);
3369+
33703370
llvm::Value *GetCountedByFieldExprGEP(const Expr *Base, const FieldDecl *FD,
33713371
const FieldDecl *CountDecl);
33723372

clang/lib/CodeGen/ItaniumCXXABI.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,9 @@ CGCallee ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(
703703

704704
{
705705
CodeGenFunction::SanitizerScope SanScope(&CGF);
706+
ApplyDebugLocation ApplyTrapDI(
707+
CGF, CGF.SanitizerAnnotateDebugInfo(SanitizerKind::SO_CFIMFCall));
708+
706709
llvm::Value *TypeId = nullptr;
707710
llvm::Value *CheckResult = nullptr;
708711

@@ -800,6 +803,8 @@ CGCallee ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(
800803
CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
801804
if (RD->hasDefinition()) {
802805
CodeGenFunction::SanitizerScope SanScope(&CGF);
806+
ApplyDebugLocation ApplyTrapDI(
807+
CGF, CGF.SanitizerAnnotateDebugInfo(SanitizerKind::SO_CFIMFCall));
803808

804809
llvm::Constant *StaticData[] = {
805810
llvm::ConstantInt::get(CGF.Int8Ty, CodeGenFunction::CFITCK_NVMFCall),

clang/test/CodeGen/cfi-check-fail-debuginfo.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
// CHECK-SAME: ptr noundef [[F:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] !dbg [[DBG7:![0-9]+]] !type [[META16:![0-9]+]] !type [[META17:![0-9]+]] !type [[META18:![0-9]+]] {
1111
// CHECK-NEXT: [[ENTRY:.*:]]
1212
// CHECK-NEXT: #dbg_value(ptr [[F]], [[META15:![0-9]+]], !DIExpression(), [[META19:![0-9]+]])
13-
// CHECK-NEXT: [[TMP0:%.*]] = tail call i1 @llvm.type.test(ptr [[F]], metadata !"_ZTSFvvE"), !dbg [[DBG20:![0-9]+]], !nosanitize [[META21:![0-9]+]]
14-
// CHECK-NEXT: br i1 [[TMP0]], label %[[CFI_CONT:.*]], label %[[CFI_SLOWPATH:.*]], !dbg [[DBG20]], !prof [[PROF22:![0-9]+]], !nosanitize [[META21]]
13+
// CHECK-NEXT: [[TMP0:%.*]] = tail call i1 @llvm.type.test(ptr [[F]], metadata !"_ZTSFvvE"), !dbg [[DBG20:![0-9]+]], !nosanitize [[META24:![0-9]+]]
14+
// CHECK-NEXT: br i1 [[TMP0]], label %[[CFI_CONT:.*]], label %[[CFI_SLOWPATH:.*]], !dbg [[DBG20]], !prof [[PROF25:![0-9]+]], !nosanitize [[META24]]
1515
// CHECK: [[CFI_SLOWPATH]]:
16-
// CHECK-NEXT: tail call void @__cfi_slowpath(i64 9080559750644022485, ptr [[F]]) #[[ATTR6:[0-9]+]], !dbg [[DBG20]], !nosanitize [[META21]]
17-
// CHECK-NEXT: br label %[[CFI_CONT]], !dbg [[DBG20]], !nosanitize [[META21]]
16+
// CHECK-NEXT: tail call void @__cfi_slowpath(i64 9080559750644022485, ptr [[F]]) #[[ATTR6:[0-9]+]], !dbg [[DBG20]], !nosanitize [[META24]]
17+
// CHECK-NEXT: br label %[[CFI_CONT]], !dbg [[DBG20]], !nosanitize [[META24]]
1818
// CHECK: [[CFI_CONT]]:
19-
// CHECK-NEXT: tail call void [[F]]() #[[ATTR6]], !dbg [[DBG20]]
20-
// CHECK-NEXT: ret void, !dbg [[DBG23:![0-9]+]]
19+
// CHECK-NEXT: tail call void [[F]]() #[[ATTR6]], !dbg [[DBG23:![0-9]+]]
20+
// CHECK-NEXT: ret void, !dbg [[DBG26:![0-9]+]]
2121
//
2222
void caller(void (*f)(void)) {
2323
f();
@@ -38,8 +38,11 @@ void caller(void (*f)(void)) {
3838
// CHECK: [[META17]] = !{i64 0, !"_ZTSFvPvE.generalized"}
3939
// CHECK: [[META18]] = !{i64 0, i64 2451761621477796417}
4040
// CHECK: [[META19]] = !DILocation(line: 0, scope: [[DBG7]])
41-
// CHECK: [[DBG20]] = !DILocation(line: 23, column: 3, scope: [[DBG7]])
42-
// CHECK: [[META21]] = !{}
43-
// CHECK: [[PROF22]] = !{!"branch_weights", i32 1048575, i32 1}
44-
// CHECK: [[DBG23]] = !DILocation(line: 24, column: 1, scope: [[DBG7]])
41+
// CHECK: [[DBG20]] = !DILocation(line: 0, scope: [[META21:![0-9]+]], inlinedAt: [[DBG23]])
42+
// CHECK: [[META21]] = distinct !DISubprogram(name: "__ubsan_check_cfi_icall", scope: [[META8]], file: [[META8]], type: [[META22:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
43+
// CHECK: [[META22]] = !DISubroutineType(types: null)
44+
// CHECK: [[DBG23]] = !DILocation(line: 23, column: 3, scope: [[DBG7]])
45+
// CHECK: [[META24]] = !{}
46+
// CHECK: [[PROF25]] = !{!"branch_weights", i32 1048575, i32 1}
47+
// CHECK: [[DBG26]] = !DILocation(line: 24, column: 1, scope: [[DBG7]])
4548
//.

clang/test/CodeGen/cfi-icall-generalize-debuginfo.c

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,27 @@ int** f(const char *a, const char **b) {
2727
// UNGENERALIZED-SAME: ptr noundef [[FP:%.*]]) local_unnamed_addr #[[ATTR1:[0-9]+]] !dbg [[DBG25:![0-9]+]] !type [[META31:![0-9]+]] !type [[META32:![0-9]+]] {
2828
// UNGENERALIZED-NEXT: [[ENTRY:.*:]]
2929
// UNGENERALIZED-NEXT: #dbg_value(ptr [[FP]], [[META30:![0-9]+]], !DIExpression(), [[META33:![0-9]+]])
30-
// UNGENERALIZED-NEXT: [[TMP0:%.*]] = tail call i1 @llvm.type.test(ptr [[FP]], metadata !"_ZTSFPPiPKcPS2_E"), !dbg [[DBG34:![0-9]+]], !nosanitize [[META35:![0-9]+]]
31-
// UNGENERALIZED-NEXT: br i1 [[TMP0]], label %[[CONT:.*]], label %[[TRAP:.*]], !dbg [[DBG34]], !prof [[PROF36:![0-9]+]], !nosanitize [[META35]]
30+
// UNGENERALIZED-NEXT: [[TMP0:%.*]] = tail call i1 @llvm.type.test(ptr [[FP]], metadata !"_ZTSFPPiPKcPS2_E"), !dbg [[DBG34:![0-9]+]], !nosanitize [[META38:![0-9]+]]
31+
// UNGENERALIZED-NEXT: br i1 [[TMP0]], label %[[CONT:.*]], label %[[TRAP:.*]], !dbg [[DBG34]], !prof [[PROF39:![0-9]+]], !nosanitize [[META38]]
3232
// UNGENERALIZED: [[TRAP]]:
33-
// UNGENERALIZED-NEXT: tail call void @llvm.ubsantrap(i8 2) #[[ATTR4:[0-9]+]], !dbg [[DBG34]], !nosanitize [[META35]]
34-
// UNGENERALIZED-NEXT: unreachable, !dbg [[DBG34]], !nosanitize [[META35]]
33+
// UNGENERALIZED-NEXT: tail call void @llvm.ubsantrap(i8 2) #[[ATTR4:[0-9]+]], !dbg [[DBG34]], !nosanitize [[META38]]
34+
// UNGENERALIZED-NEXT: unreachable, !dbg [[DBG34]], !nosanitize [[META38]]
3535
// UNGENERALIZED: [[CONT]]:
36-
// UNGENERALIZED-NEXT: [[CALL:%.*]] = tail call ptr [[FP]](ptr noundef null, ptr noundef null) #[[ATTR5:[0-9]+]], !dbg [[DBG34]]
37-
// UNGENERALIZED-NEXT: ret void, !dbg [[DBG37:![0-9]+]]
36+
// UNGENERALIZED-NEXT: [[CALL:%.*]] = tail call ptr [[FP]](ptr noundef null, ptr noundef null) #[[ATTR5:[0-9]+]], !dbg [[DBG37:![0-9]+]]
37+
// UNGENERALIZED-NEXT: ret void, !dbg [[DBG40:![0-9]+]]
3838
//
3939
// GENERALIZED-LABEL: define dso_local void @g(
4040
// GENERALIZED-SAME: ptr noundef [[FP:%.*]]) local_unnamed_addr #[[ATTR1:[0-9]+]] !dbg [[DBG25:![0-9]+]] !type [[META31:![0-9]+]] !type [[META32:![0-9]+]] {
4141
// GENERALIZED-NEXT: [[ENTRY:.*:]]
4242
// GENERALIZED-NEXT: #dbg_value(ptr [[FP]], [[META30:![0-9]+]], !DIExpression(), [[META33:![0-9]+]])
43-
// GENERALIZED-NEXT: [[TMP0:%.*]] = tail call i1 @llvm.type.test(ptr [[FP]], metadata !"_ZTSFPvPKvS_E.generalized"), !dbg [[DBG34:![0-9]+]], !nosanitize [[META35:![0-9]+]]
44-
// GENERALIZED-NEXT: br i1 [[TMP0]], label %[[CONT:.*]], label %[[TRAP:.*]], !dbg [[DBG34]], !prof [[PROF36:![0-9]+]], !nosanitize [[META35]]
43+
// GENERALIZED-NEXT: [[TMP0:%.*]] = tail call i1 @llvm.type.test(ptr [[FP]], metadata !"_ZTSFPvPKvS_E.generalized"), !dbg [[DBG34:![0-9]+]], !nosanitize [[META38:![0-9]+]]
44+
// GENERALIZED-NEXT: br i1 [[TMP0]], label %[[CONT:.*]], label %[[TRAP:.*]], !dbg [[DBG34]], !prof [[PROF39:![0-9]+]], !nosanitize [[META38]]
4545
// GENERALIZED: [[TRAP]]:
46-
// GENERALIZED-NEXT: tail call void @llvm.ubsantrap(i8 2) #[[ATTR4:[0-9]+]], !dbg [[DBG34]], !nosanitize [[META35]]
47-
// GENERALIZED-NEXT: unreachable, !dbg [[DBG34]], !nosanitize [[META35]]
46+
// GENERALIZED-NEXT: tail call void @llvm.ubsantrap(i8 2) #[[ATTR4:[0-9]+]], !dbg [[DBG34]], !nosanitize [[META38]]
47+
// GENERALIZED-NEXT: unreachable, !dbg [[DBG34]], !nosanitize [[META38]]
4848
// GENERALIZED: [[CONT]]:
49-
// GENERALIZED-NEXT: [[CALL:%.*]] = tail call ptr [[FP]](ptr noundef null, ptr noundef null) #[[ATTR5:[0-9]+]], !dbg [[DBG34]]
50-
// GENERALIZED-NEXT: ret void, !dbg [[DBG37:![0-9]+]]
49+
// GENERALIZED-NEXT: [[CALL:%.*]] = tail call ptr [[FP]](ptr noundef null, ptr noundef null) #[[ATTR5:[0-9]+]], !dbg [[DBG37:![0-9]+]]
50+
// GENERALIZED-NEXT: ret void, !dbg [[DBG40:![0-9]+]]
5151
//
5252
void g(int** (*fp)(const char *, const char **)) {
5353
fp(0, 0);
@@ -84,10 +84,13 @@ void g(int** (*fp)(const char *, const char **)) {
8484
// UNGENERALIZED: [[META31]] = !{i64 0, !"_ZTSFvPFPPiPKcPS2_EE"}
8585
// UNGENERALIZED: [[META32]] = !{i64 0, !"_ZTSFvPvE.generalized"}
8686
// UNGENERALIZED: [[META33]] = !DILocation(line: 0, scope: [[DBG25]])
87-
// UNGENERALIZED: [[DBG34]] = !DILocation(line: 53, column: 3, scope: [[DBG25]])
88-
// UNGENERALIZED: [[META35]] = !{}
89-
// UNGENERALIZED: [[PROF36]] = !{!"branch_weights", i32 1048575, i32 1}
90-
// UNGENERALIZED: [[DBG37]] = !DILocation(line: 54, column: 1, scope: [[DBG25]])
87+
// UNGENERALIZED: [[DBG34]] = !DILocation(line: 0, scope: [[META35:![0-9]+]], inlinedAt: [[DBG37]])
88+
// UNGENERALIZED: [[META35]] = distinct !DISubprogram(name: "__ubsan_check_cfi_icall", scope: [[META11]], file: [[META11]], type: [[META36:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
89+
// UNGENERALIZED: [[META36]] = !DISubroutineType(types: null)
90+
// UNGENERALIZED: [[DBG37]] = !DILocation(line: 53, column: 3, scope: [[DBG25]])
91+
// UNGENERALIZED: [[META38]] = !{}
92+
// UNGENERALIZED: [[PROF39]] = !{!"branch_weights", i32 1048575, i32 1}
93+
// UNGENERALIZED: [[DBG40]] = !DILocation(line: 54, column: 1, scope: [[DBG25]])
9194
//.
9295
// GENERALIZED: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, retainedTypes: [[META2:![0-9]+]], splitDebugInlining: false, nameTableKind: None)
9396
// GENERALIZED: [[META1]] = !DIFile(filename: "{{.*}}<stdin>", directory: {{.*}})
@@ -119,8 +122,11 @@ void g(int** (*fp)(const char *, const char **)) {
119122
// GENERALIZED: [[META31]] = !{i64 0, !"_ZTSFvPFPPiPKcPS2_EE"}
120123
// GENERALIZED: [[META32]] = !{i64 0, !"_ZTSFvPvE.generalized"}
121124
// GENERALIZED: [[META33]] = !DILocation(line: 0, scope: [[DBG25]])
122-
// GENERALIZED: [[DBG34]] = !DILocation(line: 53, column: 3, scope: [[DBG25]])
123-
// GENERALIZED: [[META35]] = !{}
124-
// GENERALIZED: [[PROF36]] = !{!"branch_weights", i32 1048575, i32 1}
125-
// GENERALIZED: [[DBG37]] = !DILocation(line: 54, column: 1, scope: [[DBG25]])
125+
// GENERALIZED: [[DBG34]] = !DILocation(line: 0, scope: [[META35:![0-9]+]], inlinedAt: [[DBG37]])
126+
// GENERALIZED: [[META35]] = distinct !DISubprogram(name: "__ubsan_check_cfi_icall", scope: [[META11]], file: [[META11]], type: [[META36:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
127+
// GENERALIZED: [[META36]] = !DISubroutineType(types: null)
128+
// GENERALIZED: [[DBG37]] = !DILocation(line: 53, column: 3, scope: [[DBG25]])
129+
// GENERALIZED: [[META38]] = !{}
130+
// GENERALIZED: [[PROF39]] = !{!"branch_weights", i32 1048575, i32 1}
131+
// GENERALIZED: [[DBG40]] = !DILocation(line: 54, column: 1, scope: [[DBG25]])
126132
//.

0 commit comments

Comments
 (0)