Skip to content
Merged
8 changes: 8 additions & 0 deletions clang/lib/CodeGen/CGClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2814,6 +2814,9 @@ void CodeGenFunction::EmitVTablePtrCheckForCall(const CXXRecordDecl *RD,
if (!SanOpts.has(SanitizerKind::CFICastStrict))
RD = LeastDerivedClassWithSameLayout(RD);

auto [Ordinal, _] = SanitizerInfoFromCFICheckKind(TCK);
ApplyDebugLocation ApplyTrapDI(*this, SanitizerAnnotateDebugInfo(Ordinal));

EmitVTablePtrCheck(RD, VTable, TCK, Loc);
}

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

auto [Ordinal, _] = SanitizerInfoFromCFICheckKind(TCK);
ApplyDebugLocation ApplyTrapDI(*this, SanitizerAnnotateDebugInfo(Ordinal));

llvm::BasicBlock *ContBlock = nullptr;

if (MayBeNull) {
Expand Down Expand Up @@ -2937,6 +2943,8 @@ llvm::Value *CodeGenFunction::EmitVTableTypeCheckedLoad(
SanitizerScope SanScope(this);

EmitSanitizerStatReport(llvm::SanStat_CFI_VCall);
ApplyDebugLocation ApplyTrapDI(
*this, SanitizerAnnotateDebugInfo(SanitizerKind::SO_CFIVCall));

llvm::Metadata *MD =
CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0));
Expand Down
7 changes: 7 additions & 0 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3971,6 +3971,8 @@ void CodeGenFunction::EmitCfiCheckFail() {
{Addr, AllVtables}),
IntPtrTy);

// TODO: the instructions above are not annotated with debug info. It is
// inconvenient to do so because we have not determined SanitizerKind yet.
const std::pair<int, SanitizerKind::SanitizerOrdinal> CheckKinds[] = {
{CFITCK_VCall, SanitizerKind::SO_CFIVCall},
{CFITCK_NVCall, SanitizerKind::SO_CFINVCall},
Expand All @@ -3981,6 +3983,9 @@ void CodeGenFunction::EmitCfiCheckFail() {
for (auto CheckKindOrdinalPair : CheckKinds) {
int Kind = CheckKindOrdinalPair.first;
SanitizerKind::SanitizerOrdinal Ordinal = CheckKindOrdinalPair.second;

ApplyDebugLocation ApplyTrapDI(*this, SanitizerAnnotateDebugInfo(Ordinal));

llvm::Value *Cond =
Builder.CreateICmpNE(CheckKind, llvm::ConstantInt::get(Int8Ty, Kind));
if (CGM.getLangOpts().Sanitize.has(Ordinal))
Expand Down Expand Up @@ -6315,6 +6320,8 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType,
(!TargetDecl || !isa<FunctionDecl>(TargetDecl))) {
SanitizerScope SanScope(this);
EmitSanitizerStatReport(llvm::SanStat_CFI_ICall);
ApplyDebugLocation ApplyTrapDI(
*this, SanitizerAnnotateDebugInfo(SanitizerKind::SO_CFIICall));

llvm::Metadata *MD;
if (CGM.getCodeGenOpts().SanitizeCfiICallGeneralizePointers)
Expand Down
10 changes: 5 additions & 5 deletions clang/lib/CodeGen/CodeGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -2816,11 +2816,6 @@ class CodeGenFunction : public CodeGenTypeCache {
void emitStoresForInitAfterBZero(llvm::Constant *Init, Address Loc,
bool isVolatile, bool IsAutoInit);

/// Returns debug info, with additional annotation if enabled by
/// CGM.getCodeGenOpts().SanitizeAnnotateDebugInfo[CheckKindOrdinal].
llvm::DILocation *
SanitizerAnnotateDebugInfo(SanitizerKind::SanitizerOrdinal CheckKindOrdinal);

public:
// Captures all the allocas created during the scope of its RAII object.
struct AllocaTrackerRAII {
Expand Down Expand Up @@ -3367,6 +3362,11 @@ class CodeGenFunction : public CodeGenTypeCache {
llvm::Value *Index, QualType IndexType,
QualType IndexedType, bool Accessed);

/// Returns debug info, with additional annotation if enabled by
/// CGM.getCodeGenOpts().SanitizeAnnotateDebugInfo[CheckKindOrdinal].
llvm::DILocation *
SanitizerAnnotateDebugInfo(SanitizerKind::SanitizerOrdinal CheckKindOrdinal);

llvm::Value *GetCountedByFieldExprGEP(const Expr *Base, const FieldDecl *FD,
const FieldDecl *CountDecl);

Expand Down
5 changes: 5 additions & 0 deletions clang/lib/CodeGen/ItaniumCXXABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,9 @@ CGCallee ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(

{
CodeGenFunction::SanitizerScope SanScope(&CGF);
ApplyDebugLocation ApplyTrapDI(
CGF, CGF.SanitizerAnnotateDebugInfo(SanitizerKind::SO_CFIMFCall));

llvm::Value *TypeId = nullptr;
llvm::Value *CheckResult = nullptr;

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

llvm::Constant *StaticData[] = {
llvm::ConstantInt::get(CGF.Int8Ty, CodeGenFunction::CFITCK_NVMFCall),
Expand Down
23 changes: 13 additions & 10 deletions clang/test/CodeGen/cfi-check-fail-debuginfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
// 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]+]] {
// CHECK-NEXT: [[ENTRY:.*:]]
// CHECK-NEXT: #dbg_value(ptr [[F]], [[META15:![0-9]+]], !DIExpression(), [[META19:![0-9]+]])
// CHECK-NEXT: [[TMP0:%.*]] = tail call i1 @llvm.type.test(ptr [[F]], metadata !"_ZTSFvvE"), !dbg [[DBG20:![0-9]+]], !nosanitize [[META21:![0-9]+]]
// CHECK-NEXT: br i1 [[TMP0]], label %[[CFI_CONT:.*]], label %[[CFI_SLOWPATH:.*]], !dbg [[DBG20]], !prof [[PROF22:![0-9]+]], !nosanitize [[META21]]
// CHECK-NEXT: [[TMP0:%.*]] = tail call i1 @llvm.type.test(ptr [[F]], metadata !"_ZTSFvvE"), !dbg [[DBG20:![0-9]+]], !nosanitize [[META24:![0-9]+]]
// CHECK-NEXT: br i1 [[TMP0]], label %[[CFI_CONT:.*]], label %[[CFI_SLOWPATH:.*]], !dbg [[DBG20]], !prof [[PROF25:![0-9]+]], !nosanitize [[META24]]
// CHECK: [[CFI_SLOWPATH]]:
// CHECK-NEXT: tail call void @__cfi_slowpath(i64 9080559750644022485, ptr [[F]]) #[[ATTR6:[0-9]+]], !dbg [[DBG20]], !nosanitize [[META21]]
// CHECK-NEXT: br label %[[CFI_CONT]], !dbg [[DBG20]], !nosanitize [[META21]]
// CHECK-NEXT: tail call void @__cfi_slowpath(i64 9080559750644022485, ptr [[F]]) #[[ATTR6:[0-9]+]], !dbg [[DBG20]], !nosanitize [[META24]]
// CHECK-NEXT: br label %[[CFI_CONT]], !dbg [[DBG20]], !nosanitize [[META24]]
// CHECK: [[CFI_CONT]]:
// CHECK-NEXT: tail call void [[F]]() #[[ATTR6]], !dbg [[DBG20]]
// CHECK-NEXT: ret void, !dbg [[DBG23:![0-9]+]]
// CHECK-NEXT: tail call void [[F]]() #[[ATTR6]], !dbg [[DBG23:![0-9]+]]
// CHECK-NEXT: ret void, !dbg [[DBG26:![0-9]+]]
//
void caller(void (*f)(void)) {
f();
Expand All @@ -38,8 +38,11 @@ void caller(void (*f)(void)) {
// CHECK: [[META17]] = !{i64 0, !"_ZTSFvPvE.generalized"}
// CHECK: [[META18]] = !{i64 0, i64 2451761621477796417}
// CHECK: [[META19]] = !DILocation(line: 0, scope: [[DBG7]])
// CHECK: [[DBG20]] = !DILocation(line: 23, column: 3, scope: [[DBG7]])
// CHECK: [[META21]] = !{}
// CHECK: [[PROF22]] = !{!"branch_weights", i32 1048575, i32 1}
// CHECK: [[DBG23]] = !DILocation(line: 24, column: 1, scope: [[DBG7]])
// CHECK: [[DBG20]] = !DILocation(line: 0, scope: [[META21:![0-9]+]], inlinedAt: [[DBG23]])
// CHECK: [[META21]] = distinct !DISubprogram(name: "__ubsan_check_cfi_icall", scope: [[META8]], file: [[META8]], type: [[META22:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
// CHECK: [[META22]] = !DISubroutineType(types: null)
// CHECK: [[DBG23]] = !DILocation(line: 23, column: 3, scope: [[DBG7]])
// CHECK: [[META24]] = !{}
// CHECK: [[PROF25]] = !{!"branch_weights", i32 1048575, i32 1}
// CHECK: [[DBG26]] = !DILocation(line: 24, column: 1, scope: [[DBG7]])
//.
46 changes: 26 additions & 20 deletions clang/test/CodeGen/cfi-icall-generalize-debuginfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@ int** f(const char *a, const char **b) {
// UNGENERALIZED-SAME: ptr noundef [[FP:%.*]]) local_unnamed_addr #[[ATTR1:[0-9]+]] !dbg [[DBG25:![0-9]+]] !type [[META31:![0-9]+]] !type [[META32:![0-9]+]] {
// UNGENERALIZED-NEXT: [[ENTRY:.*:]]
// UNGENERALIZED-NEXT: #dbg_value(ptr [[FP]], [[META30:![0-9]+]], !DIExpression(), [[META33:![0-9]+]])
// UNGENERALIZED-NEXT: [[TMP0:%.*]] = tail call i1 @llvm.type.test(ptr [[FP]], metadata !"_ZTSFPPiPKcPS2_E"), !dbg [[DBG34:![0-9]+]], !nosanitize [[META35:![0-9]+]]
// UNGENERALIZED-NEXT: br i1 [[TMP0]], label %[[CONT:.*]], label %[[TRAP:.*]], !dbg [[DBG34]], !prof [[PROF36:![0-9]+]], !nosanitize [[META35]]
// UNGENERALIZED-NEXT: [[TMP0:%.*]] = tail call i1 @llvm.type.test(ptr [[FP]], metadata !"_ZTSFPPiPKcPS2_E"), !dbg [[DBG34:![0-9]+]], !nosanitize [[META38:![0-9]+]]
// UNGENERALIZED-NEXT: br i1 [[TMP0]], label %[[CONT:.*]], label %[[TRAP:.*]], !dbg [[DBG34]], !prof [[PROF39:![0-9]+]], !nosanitize [[META38]]
// UNGENERALIZED: [[TRAP]]:
// UNGENERALIZED-NEXT: tail call void @llvm.ubsantrap(i8 2) #[[ATTR4:[0-9]+]], !dbg [[DBG34]], !nosanitize [[META35]]
// UNGENERALIZED-NEXT: unreachable, !dbg [[DBG34]], !nosanitize [[META35]]
// UNGENERALIZED-NEXT: tail call void @llvm.ubsantrap(i8 2) #[[ATTR4:[0-9]+]], !dbg [[DBG34]], !nosanitize [[META38]]
// UNGENERALIZED-NEXT: unreachable, !dbg [[DBG34]], !nosanitize [[META38]]
// UNGENERALIZED: [[CONT]]:
// UNGENERALIZED-NEXT: [[CALL:%.*]] = tail call ptr [[FP]](ptr noundef null, ptr noundef null) #[[ATTR5:[0-9]+]], !dbg [[DBG34]]
// UNGENERALIZED-NEXT: ret void, !dbg [[DBG37:![0-9]+]]
// UNGENERALIZED-NEXT: [[CALL:%.*]] = tail call ptr [[FP]](ptr noundef null, ptr noundef null) #[[ATTR5:[0-9]+]], !dbg [[DBG37:![0-9]+]]
// UNGENERALIZED-NEXT: ret void, !dbg [[DBG40:![0-9]+]]
//
// GENERALIZED-LABEL: define dso_local void @g(
// GENERALIZED-SAME: ptr noundef [[FP:%.*]]) local_unnamed_addr #[[ATTR1:[0-9]+]] !dbg [[DBG25:![0-9]+]] !type [[META31:![0-9]+]] !type [[META32:![0-9]+]] {
// GENERALIZED-NEXT: [[ENTRY:.*:]]
// GENERALIZED-NEXT: #dbg_value(ptr [[FP]], [[META30:![0-9]+]], !DIExpression(), [[META33:![0-9]+]])
// GENERALIZED-NEXT: [[TMP0:%.*]] = tail call i1 @llvm.type.test(ptr [[FP]], metadata !"_ZTSFPvPKvS_E.generalized"), !dbg [[DBG34:![0-9]+]], !nosanitize [[META35:![0-9]+]]
// GENERALIZED-NEXT: br i1 [[TMP0]], label %[[CONT:.*]], label %[[TRAP:.*]], !dbg [[DBG34]], !prof [[PROF36:![0-9]+]], !nosanitize [[META35]]
// GENERALIZED-NEXT: [[TMP0:%.*]] = tail call i1 @llvm.type.test(ptr [[FP]], metadata !"_ZTSFPvPKvS_E.generalized"), !dbg [[DBG34:![0-9]+]], !nosanitize [[META38:![0-9]+]]
// GENERALIZED-NEXT: br i1 [[TMP0]], label %[[CONT:.*]], label %[[TRAP:.*]], !dbg [[DBG34]], !prof [[PROF39:![0-9]+]], !nosanitize [[META38]]
// GENERALIZED: [[TRAP]]:
// GENERALIZED-NEXT: tail call void @llvm.ubsantrap(i8 2) #[[ATTR4:[0-9]+]], !dbg [[DBG34]], !nosanitize [[META35]]
// GENERALIZED-NEXT: unreachable, !dbg [[DBG34]], !nosanitize [[META35]]
// GENERALIZED-NEXT: tail call void @llvm.ubsantrap(i8 2) #[[ATTR4:[0-9]+]], !dbg [[DBG34]], !nosanitize [[META38]]
// GENERALIZED-NEXT: unreachable, !dbg [[DBG34]], !nosanitize [[META38]]
// GENERALIZED: [[CONT]]:
// GENERALIZED-NEXT: [[CALL:%.*]] = tail call ptr [[FP]](ptr noundef null, ptr noundef null) #[[ATTR5:[0-9]+]], !dbg [[DBG34]]
// GENERALIZED-NEXT: ret void, !dbg [[DBG37:![0-9]+]]
// GENERALIZED-NEXT: [[CALL:%.*]] = tail call ptr [[FP]](ptr noundef null, ptr noundef null) #[[ATTR5:[0-9]+]], !dbg [[DBG37:![0-9]+]]
// GENERALIZED-NEXT: ret void, !dbg [[DBG40:![0-9]+]]
//
void g(int** (*fp)(const char *, const char **)) {
fp(0, 0);
Expand Down Expand Up @@ -84,10 +84,13 @@ void g(int** (*fp)(const char *, const char **)) {
// UNGENERALIZED: [[META31]] = !{i64 0, !"_ZTSFvPFPPiPKcPS2_EE"}
// UNGENERALIZED: [[META32]] = !{i64 0, !"_ZTSFvPvE.generalized"}
// UNGENERALIZED: [[META33]] = !DILocation(line: 0, scope: [[DBG25]])
// UNGENERALIZED: [[DBG34]] = !DILocation(line: 53, column: 3, scope: [[DBG25]])
// UNGENERALIZED: [[META35]] = !{}
// UNGENERALIZED: [[PROF36]] = !{!"branch_weights", i32 1048575, i32 1}
// UNGENERALIZED: [[DBG37]] = !DILocation(line: 54, column: 1, scope: [[DBG25]])
// UNGENERALIZED: [[DBG34]] = !DILocation(line: 0, scope: [[META35:![0-9]+]], inlinedAt: [[DBG37]])
// UNGENERALIZED: [[META35]] = distinct !DISubprogram(name: "__ubsan_check_cfi_icall", scope: [[META11]], file: [[META11]], type: [[META36:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
// UNGENERALIZED: [[META36]] = !DISubroutineType(types: null)
// UNGENERALIZED: [[DBG37]] = !DILocation(line: 53, column: 3, scope: [[DBG25]])
// UNGENERALIZED: [[META38]] = !{}
// UNGENERALIZED: [[PROF39]] = !{!"branch_weights", i32 1048575, i32 1}
// UNGENERALIZED: [[DBG40]] = !DILocation(line: 54, column: 1, scope: [[DBG25]])
//.
// 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)
// GENERALIZED: [[META1]] = !DIFile(filename: "{{.*}}<stdin>", directory: {{.*}})
Expand Down Expand Up @@ -119,8 +122,11 @@ void g(int** (*fp)(const char *, const char **)) {
// GENERALIZED: [[META31]] = !{i64 0, !"_ZTSFvPFPPiPKcPS2_EE"}
// GENERALIZED: [[META32]] = !{i64 0, !"_ZTSFvPvE.generalized"}
// GENERALIZED: [[META33]] = !DILocation(line: 0, scope: [[DBG25]])
// GENERALIZED: [[DBG34]] = !DILocation(line: 53, column: 3, scope: [[DBG25]])
// GENERALIZED: [[META35]] = !{}
// GENERALIZED: [[PROF36]] = !{!"branch_weights", i32 1048575, i32 1}
// GENERALIZED: [[DBG37]] = !DILocation(line: 54, column: 1, scope: [[DBG25]])
// GENERALIZED: [[DBG34]] = !DILocation(line: 0, scope: [[META35:![0-9]+]], inlinedAt: [[DBG37]])
// GENERALIZED: [[META35]] = distinct !DISubprogram(name: "__ubsan_check_cfi_icall", scope: [[META11]], file: [[META11]], type: [[META36:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
// GENERALIZED: [[META36]] = !DISubroutineType(types: null)
// GENERALIZED: [[DBG37]] = !DILocation(line: 53, column: 3, scope: [[DBG25]])
// GENERALIZED: [[META38]] = !{}
// GENERALIZED: [[PROF39]] = !{!"branch_weights", i32 1048575, i32 1}
// GENERALIZED: [[DBG40]] = !DILocation(line: 54, column: 1, scope: [[DBG25]])
//.
Loading