Skip to content

Commit ed3d222

Browse files
committed
[sanitizer][NFCI] Add 'SanitizerAnnotateDebugInfo'
This generalizes the debug info annotation code from llvm#139149 and moves it into a helper function, SanitizerAnnotateDebugInfo(). Future work can use 'ApplyDebugLocation ApplyTrapDI(*this, SanitizerAnnotateDebugInfo(Ordinal));' to add annotations to additional checks.
1 parent 3138f6c commit ed3d222

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,16 +1226,8 @@ void CodeGenFunction::EmitBoundsCheckImpl(const Expr *E, llvm::Value *Bound,
12261226

12271227
SanitizerScope SanScope(this);
12281228

1229-
llvm::DILocation *CheckDI = Builder.getCurrentDebugLocation();
12301229
auto CheckKind = SanitizerKind::SO_ArrayBounds;
1231-
// TODO: deprecate ClArrayBoundsPseudoFn
1232-
if ((ClArrayBoundsPseudoFn ||
1233-
CGM.getCodeGenOpts().SanitizeAnnotateDebugInfo.has(CheckKind)) &&
1234-
CheckDI) {
1235-
CheckDI = getDebugInfo()->CreateSyntheticInlineAt(
1236-
Builder.getCurrentDebugLocation(), "__ubsan_check_array_bounds");
1237-
}
1238-
ApplyDebugLocation ApplyTrapDI(*this, CheckDI);
1230+
ApplyDebugLocation ApplyTrapDI(*this, SanitizerAnnotateDebugInfo(CheckKind));
12391231

12401232
bool IndexSigned = IndexType->isSignedIntegerOrEnumerationType();
12411233
llvm::Value *IndexVal = Builder.CreateIntCast(Index, SizeTy, IndexSigned);
@@ -1252,6 +1244,35 @@ void CodeGenFunction::EmitBoundsCheckImpl(const Expr *E, llvm::Value *Bound,
12521244
StaticData, Index);
12531245
}
12541246

1247+
llvm::DILocation *CodeGenFunction::SanitizerAnnotateDebugInfo(
1248+
SanitizerKind::SanitizerOrdinal CheckKindOrdinal) {
1249+
std::string Label;
1250+
switch (CheckKindOrdinal) {
1251+
#define SANITIZER(NAME, ID) \
1252+
case SanitizerKind::SO_##ID: \
1253+
Label = "__ubsan_check_" NAME; \
1254+
break;
1255+
#include "clang/Basic/Sanitizers.def"
1256+
default:
1257+
llvm_unreachable("unexpected sanitizer kind");
1258+
}
1259+
1260+
// Sanitize label
1261+
for (unsigned int i = 0; i < Label.length(); i++)
1262+
if (!std::isalpha(Label[i]))
1263+
Label[i] = '_';
1264+
1265+
llvm::DILocation *CheckDI = Builder.getCurrentDebugLocation();
1266+
// TODO: deprecate ClArrayBoundsPseudoFn
1267+
if (((ClArrayBoundsPseudoFn &&
1268+
CheckKindOrdinal == SanitizerKind::SO_ArrayBounds) ||
1269+
CGM.getCodeGenOpts().SanitizeAnnotateDebugInfo.has(CheckKindOrdinal)) &&
1270+
CheckDI)
1271+
CheckDI = getDebugInfo()->CreateSyntheticInlineAt(CheckDI, Label);
1272+
1273+
return CheckDI;
1274+
}
1275+
12551276
CodeGenFunction::ComplexPairTy CodeGenFunction::
12561277
EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
12571278
bool isInc, bool isPre) {

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,6 +2816,11 @@ 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+
28192824
public:
28202825
// Captures all the allocas created during the scope of its RAII object.
28212826
struct AllocaTrackerRAII {

0 commit comments

Comments
 (0)