[clang] 5defe49 - [sanitizer][NFCI] Add 'SanitizerAnnotateDebugInfo' (#139965)
via cfe-commits
cfe-commits at lists.llvm.org
Thu May 15 09:24:07 PDT 2025
Author: Thurston Dang
Date: 2025-05-15T09:24:04-07:00
New Revision: 5defe490c9b1356916245e1832859c361ac8d812
URL: https://github.com/llvm/llvm-project/commit/5defe490c9b1356916245e1832859c361ac8d812
DIFF: https://github.com/llvm/llvm-project/commit/5defe490c9b1356916245e1832859c361ac8d812.diff
LOG: [sanitizer][NFCI] Add 'SanitizerAnnotateDebugInfo' (#139965)
This generalizes the debug info annotation code from https://github.com/llvm/llvm-project/pull/139149 and moves it into a helper function, SanitizerAnnotateDebugInfo().
Future work can use 'ApplyDebugLocation ApplyTrapDI(*this, SanitizerAnnotateDebugInfo(Ordinal));' to add annotations to additional checks.
Added:
Modified:
clang/lib/CodeGen/CGExpr.cpp
clang/lib/CodeGen/CodeGenFunction.h
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index ec01c87c13b1d..37a5678aa61d5 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -1226,16 +1226,8 @@ void CodeGenFunction::EmitBoundsCheckImpl(const Expr *E, llvm::Value *Bound,
SanitizerScope SanScope(this);
- llvm::DILocation *CheckDI = Builder.getCurrentDebugLocation();
auto CheckKind = SanitizerKind::SO_ArrayBounds;
- // TODO: deprecate ClArrayBoundsPseudoFn
- if ((ClArrayBoundsPseudoFn ||
- CGM.getCodeGenOpts().SanitizeAnnotateDebugInfo.has(CheckKind)) &&
- CheckDI) {
- CheckDI = getDebugInfo()->CreateSyntheticInlineAt(
- Builder.getCurrentDebugLocation(), "__ubsan_check_array_bounds");
- }
- ApplyDebugLocation ApplyTrapDI(*this, CheckDI);
+ ApplyDebugLocation ApplyTrapDI(*this, SanitizerAnnotateDebugInfo(CheckKind));
bool IndexSigned = IndexType->isSignedIntegerOrEnumerationType();
llvm::Value *IndexVal = Builder.CreateIntCast(Index, SizeTy, IndexSigned);
@@ -1252,6 +1244,35 @@ void CodeGenFunction::EmitBoundsCheckImpl(const Expr *E, llvm::Value *Bound,
StaticData, Index);
}
+llvm::DILocation *CodeGenFunction::SanitizerAnnotateDebugInfo(
+ SanitizerKind::SanitizerOrdinal CheckKindOrdinal) {
+ std::string Label;
+ switch (CheckKindOrdinal) {
+#define SANITIZER(NAME, ID) \
+ case SanitizerKind::SO_##ID: \
+ Label = "__ubsan_check_" NAME; \
+ break;
+#include "clang/Basic/Sanitizers.def"
+ default:
+ llvm_unreachable("unexpected sanitizer kind");
+ }
+
+ // Sanitize label
+ for (unsigned int i = 0; i < Label.length(); i++)
+ if (!std::isalpha(Label[i]))
+ Label[i] = '_';
+
+ llvm::DILocation *CheckDI = Builder.getCurrentDebugLocation();
+ // TODO: deprecate ClArrayBoundsPseudoFn
+ if (((ClArrayBoundsPseudoFn &&
+ CheckKindOrdinal == SanitizerKind::SO_ArrayBounds) ||
+ CGM.getCodeGenOpts().SanitizeAnnotateDebugInfo.has(CheckKindOrdinal)) &&
+ CheckDI)
+ CheckDI = getDebugInfo()->CreateSyntheticInlineAt(CheckDI, Label);
+
+ return CheckDI;
+}
+
CodeGenFunction::ComplexPairTy CodeGenFunction::
EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
bool isInc, bool isPre) {
diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h
index fa4ceafc41893..7104303cba50e 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -2816,6 +2816,11 @@ 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 {
More information about the cfe-commits
mailing list