Skip to content

Commit 2eee786

Browse files
committed
SanitizerDebugLocation
1 parent 8176e88 commit 2eee786

File tree

13 files changed

+163
-145
lines changed

13 files changed

+163
-145
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,7 +2010,7 @@ Value *CodeGenFunction::EmitCheckedArgForBuiltin(const Expr *E,
20102010

20112011
auto CheckOrdinal = SanitizerKind::SO_Builtin;
20122012
auto CheckHandler = SanitizerHandler::InvalidBuiltin;
2013-
SanitizerScope SanScope(this, {CheckOrdinal}, CheckHandler);
2013+
SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
20142014
Value *Cond = Builder.CreateICmpNE(
20152015
ArgValue, llvm::Constant::getNullValue(ArgValue->getType()));
20162016
EmitCheck(std::make_pair(Cond, CheckOrdinal), CheckHandler,
@@ -2027,7 +2027,7 @@ Value *CodeGenFunction::EmitCheckedArgForAssume(const Expr *E) {
20272027

20282028
auto CheckOrdinal = SanitizerKind::SO_Builtin;
20292029
auto CheckHandler = SanitizerHandler::InvalidBuiltin;
2030-
SanitizerScope SanScope(this, {CheckOrdinal}, CheckHandler);
2030+
SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
20312031
EmitCheck(
20322032
std::make_pair(ArgValue, CheckOrdinal), CheckHandler,
20332033
{EmitCheckSourceLocation(E->getExprLoc()),
@@ -2060,7 +2060,7 @@ static Value *EmitOverflowCheckedAbs(CodeGenFunction &CGF, const CallExpr *E,
20602060
} else
20612061
CheckHandler = SanitizerHandler::SubOverflow;
20622062

2063-
CodeGenFunction::SanitizerScope SanScope(&CGF, Ordinals, CheckHandler);
2063+
SanitizerDebugLocation SanScope(&CGF, Ordinals, CheckHandler);
20642064

20652065
Constant *Zero = Constant::getNullValue(ArgValue->getType());
20662066
Value *ResultAndOverflow = CGF.Builder.CreateBinaryIntrinsic(

clang/lib/CodeGen/CGCall.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4157,7 +4157,7 @@ void CodeGenFunction::EmitReturnValueCheck(llvm::Value *RV) {
41574157
Handler = SanitizerHandler::NullabilityReturn;
41584158
}
41594159

4160-
SanitizerScope SanScope(this, {CheckKind}, Handler);
4160+
SanitizerDebugLocation SanScope(this, {CheckKind}, Handler);
41614161

41624162
// Make sure the "return" source location is valid. If we're checking a
41634163
// nullability annotation, make sure the preconditions for the check are met.
@@ -4542,7 +4542,7 @@ void CodeGenFunction::EmitNonNullArgCheck(RValue RV, QualType ArgType,
45424542
Handler = SanitizerHandler::NullabilityArg;
45434543
}
45444544

4545-
SanitizerScope SanScope(this, {CheckKind}, Handler);
4545+
SanitizerDebugLocation SanScope(this, {CheckKind}, Handler);
45464546
llvm::Value *Cond = EmitNonNullRValueCheck(RV, ArgType);
45474547
llvm::Constant *StaticData[] = {
45484548
EmitCheckSourceLocation(ArgLoc),

clang/lib/CodeGen/CGClass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2890,7 +2890,7 @@ void CodeGenFunction::EmitVTablePtrCheck(const CXXRecordDecl *RD,
28902890
return;
28912891

28922892
auto CheckHandler = SanitizerHandler::CFICheckFail;
2893-
SanitizerScope SanScope(this, {M}, CheckHandler);
2893+
SanitizerDebugLocation SanScope(this, {M}, CheckHandler);
28942894
EmitSanitizerStatReport(SSK);
28952895

28962896
llvm::Metadata *MD =
@@ -2949,7 +2949,7 @@ llvm::Value *CodeGenFunction::EmitVTableTypeCheckedLoad(
29492949
uint64_t VTableByteOffset) {
29502950
auto CheckOrdinal = SanitizerKind::SO_CFIVCall;
29512951
auto CheckHandler = SanitizerHandler::CFICheckFail;
2952-
SanitizerScope SanScope(this, {CheckOrdinal}, CheckHandler);
2952+
SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
29532953

29542954
EmitSanitizerStatReport(llvm::SanStat_CFI_VCall);
29552955

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@
5858
using namespace clang;
5959
using namespace clang::CodeGen;
6060

61+
// TODO: consider deprecating ClArrayBoundsPseudoFn; functionality is subsumed
62+
// by -fsanitize-annotate-debug-info
63+
static llvm::cl::opt<bool> ClArrayBoundsPseudoFn(
64+
"array-bounds-pseudofn", llvm::cl::Hidden, llvm::cl::Optional,
65+
llvm::cl::desc("Emit debug info that places array-bounds instrumentation "
66+
"in an inline function called __ubsan_check_array_bounds."));
67+
6168
static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) {
6269
auto TI = Ctx.getTypeInfo(Ty);
6370
if (TI.isAlignRequired())
@@ -6413,3 +6420,47 @@ CodeGenFunction::LexicalScope::~LexicalScope() {
64136420
ForceCleanup();
64146421
}
64156422
}
6423+
6424+
llvm::DILocation *CodeGenFunction::SanitizerAnnotateDebugInfo(
6425+
ArrayRef<SanitizerKind::SanitizerOrdinal> Ordinals,
6426+
SanitizerHandler Handler) {
6427+
std::string Label;
6428+
switch (Handler) {
6429+
#define SANITIZER_CHECK(Enum, Name, Version) \
6430+
case Enum: \
6431+
Label = "__ubsan_check_" #Name; \
6432+
break;
6433+
6434+
LIST_SANITIZER_CHECKS
6435+
#undef SANITIZER_CHECK
6436+
};
6437+
6438+
llvm::DILocation *CheckDI = Builder.getCurrentDebugLocation();
6439+
6440+
// TODO: the annotation could be more precise e.g.,
6441+
// use the ordinal name if there is only one ordinal
6442+
for (auto Ord : Ordinals) {
6443+
// TODO: deprecate ClArrayBoundsPseudoFn
6444+
if (((ClArrayBoundsPseudoFn && Ord == SanitizerKind::SO_ArrayBounds) ||
6445+
CGM.getCodeGenOpts().SanitizeAnnotateDebugInfo.has(Ord)) &&
6446+
CheckDI) {
6447+
return getDebugInfo()->CreateSyntheticInlineAt(CheckDI, Label);
6448+
}
6449+
}
6450+
6451+
return CheckDI;
6452+
}
6453+
6454+
SanitizerDebugLocation::SanitizerDebugLocation(
6455+
CodeGenFunction *CGF, ArrayRef<SanitizerKind::SanitizerOrdinal> Ordinals,
6456+
SanitizerHandler Handler)
6457+
: CGF(CGF),
6458+
Apply(*CGF, CGF->SanitizerAnnotateDebugInfo(Ordinals, Handler)) {
6459+
assert(!CGF->IsSanitizerScope);
6460+
CGF->IsSanitizerScope = true;
6461+
}
6462+
6463+
SanitizerDebugLocation::~SanitizerDebugLocation() {
6464+
assert(CGF->IsSanitizerScope);
6465+
CGF->IsSanitizerScope = false;
6466+
}

clang/lib/CodeGen/CGDebugInfo.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H
1515

1616
#include "CGBuilder.h"
17+
#include "SanitizerHandler.h"
1718
#include "clang/AST/DeclCXX.h"
1819
#include "clang/AST/Expr.h"
1920
#include "clang/AST/ExternalASTSource.h"
@@ -974,6 +975,17 @@ class ApplyInlineDebugLocation {
974975
~ApplyInlineDebugLocation();
975976
};
976977

978+
class SanitizerDebugLocation {
979+
CodeGenFunction *CGF;
980+
ApplyDebugLocation Apply;
981+
982+
public:
983+
SanitizerDebugLocation(CodeGenFunction *CGF,
984+
ArrayRef<SanitizerKind::SanitizerOrdinal> Ordinals,
985+
SanitizerHandler Handler);
986+
~SanitizerDebugLocation();
987+
};
988+
977989
} // namespace CodeGen
978990
} // namespace clang
979991

clang/lib/CodeGen/CGDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ void CodeGenFunction::EmitNullabilityCheck(LValue LHS, llvm::Value *RHS,
768768
// hand side must be nonnull.
769769
auto CheckOrdinal = SanitizerKind::SO_NullabilityAssign;
770770
auto CheckHandler = SanitizerHandler::TypeMismatch;
771-
SanitizerScope SanScope(this, {CheckOrdinal}, CheckHandler);
771+
SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
772772
llvm::Value *IsNotNull = Builder.CreateIsNotNull(RHS);
773773
llvm::Constant *StaticData[] = {
774774
EmitCheckSourceLocation(Loc), EmitCheckTypeDescriptor(LHS.getType()),

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,6 @@ llvm::cl::opt<bool> ClSanitizeGuardChecks(
6565

6666
} // namespace clang
6767

68-
// TODO: consider deprecating ClArrayBoundsPseudoFn; functionality is subsumed
69-
// by -fsanitize-annotate-debug-info
70-
static llvm::cl::opt<bool> ClArrayBoundsPseudoFn(
71-
"array-bounds-pseudofn", llvm::cl::Hidden, llvm::cl::Optional,
72-
llvm::cl::desc("Emit debug info that places array-bounds instrumentation "
73-
"in an inline function called __ubsan_check_array_bounds."));
74-
7568
//===--------------------------------------------------------------------===//
7669
// Defines for metadata
7770
//===--------------------------------------------------------------------===//
@@ -755,10 +748,11 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
755748
// SO_Vptr's corresponding handler is DynamicTypeCacheMiss, not TypeMismatch.
756749
// However, it relies upon IsGuaranteedNonNull, hence the instructions cannot
757750
// be fully separated from the TypeMismatch.
758-
SanitizerScope SanScope(this,
759-
{SanitizerKind::SO_Null, SanitizerKind::SO_ObjectSize,
760-
SanitizerKind::SO_Alignment, SanitizerKind::SO_Vptr},
761-
CheckHandler);
751+
SanitizerDebugLocation SanScope(
752+
this,
753+
{SanitizerKind::SO_Null, SanitizerKind::SO_ObjectSize,
754+
SanitizerKind::SO_Alignment, SanitizerKind::SO_Vptr},
755+
CheckHandler);
762756

763757
SmallVector<std::pair<llvm::Value *, SanitizerKind::SanitizerOrdinal>, 3>
764758
Checks;
@@ -1235,7 +1229,7 @@ void CodeGenFunction::EmitBoundsCheckImpl(const Expr *E, llvm::Value *Bound,
12351229

12361230
auto CheckKind = SanitizerKind::SO_ArrayBounds;
12371231
auto CheckHandler = SanitizerHandler::OutOfBounds;
1238-
SanitizerScope SanScope(this, {CheckKind}, CheckHandler);
1232+
SanitizerDebugLocation SanScope(this, {CheckKind}, CheckHandler);
12391233

12401234
bool IndexSigned = IndexType->isSignedIntegerOrEnumerationType();
12411235
llvm::Value *IndexVal = Builder.CreateIntCast(Index, SizeTy, IndexSigned);
@@ -1251,37 +1245,6 @@ void CodeGenFunction::EmitBoundsCheckImpl(const Expr *E, llvm::Value *Bound,
12511245
EmitCheck(std::make_pair(Check, CheckKind), CheckHandler, StaticData, Index);
12521246
}
12531247

1254-
llvm::DILocation *CodeGenFunction::SanitizerAnnotateDebugInfo(
1255-
ArrayRef<SanitizerKind::SanitizerOrdinal> Ordinals,
1256-
SanitizerHandler Handler) {
1257-
std::string Label;
1258-
switch (Handler) {
1259-
#define SANITIZER_CHECK(Enum, Name, Version) \
1260-
case Enum: \
1261-
Label = "__ubsan_check_" #Name; \
1262-
break;
1263-
1264-
LIST_SANITIZER_CHECKS
1265-
#undef SANITIZER_CHECK
1266-
};
1267-
1268-
llvm::DILocation *CheckDI = Builder.getCurrentDebugLocation();
1269-
1270-
// TODO: the annotation could be more precise e.g.,
1271-
// use the ordinal name if there is only one ordinal
1272-
for (auto Ord : Ordinals) {
1273-
// TODO: deprecate ClArrayBoundsPseudoFn
1274-
if (((ClArrayBoundsPseudoFn && Ord == SanitizerKind::SO_ArrayBounds) ||
1275-
CGM.getCodeGenOpts().SanitizeAnnotateDebugInfo.has(Ord)) &&
1276-
CheckDI) {
1277-
CheckDI = getDebugInfo()->CreateSyntheticInlineAt(CheckDI, Label);
1278-
break;
1279-
}
1280-
}
1281-
1282-
return CheckDI;
1283-
}
1284-
12851248
CodeGenFunction::ComplexPairTy CodeGenFunction::
12861249
EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
12871250
bool isInc, bool isPre) {
@@ -2008,7 +1971,7 @@ bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty,
20081971

20091972
auto &Ctx = getLLVMContext();
20101973
auto CheckHandler = SanitizerHandler::LoadInvalidValue;
2011-
SanitizerScope SanScope(this, {Kind}, CheckHandler);
1974+
SanitizerDebugLocation SanScope(this, {Kind}, CheckHandler);
20121975
llvm::Value *Check;
20131976
--End;
20141977
if (!Min) {
@@ -3946,7 +3909,7 @@ void CodeGenFunction::EmitCfiCheckFail() {
39463909
// not even be available, if Data == nullptr). However, we still want to
39473910
// annotate the instrumentation. We approximate this by using all the CFI
39483911
// kinds.
3949-
SanitizerScope SanScope(
3912+
SanitizerDebugLocation SanScope(
39503913
this,
39513914
{SanitizerKind::SO_CFIVCall, SanitizerKind::SO_CFINVCall,
39523915
SanitizerKind::SO_CFIDerivedCast, SanitizerKind::SO_CFIUnrelatedCast,
@@ -4051,7 +4014,7 @@ void CodeGenFunction::EmitUnreachable(SourceLocation Loc) {
40514014
if (SanOpts.has(SanitizerKind::Unreachable)) {
40524015
auto CheckOrdinal = SanitizerKind::SO_Unreachable;
40534016
auto CheckHandler = SanitizerHandler::BuiltinUnreachable;
4054-
SanitizerScope SanScope(this, {CheckOrdinal}, CheckHandler);
4017+
SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
40554018
EmitCheck(std::make_pair(static_cast<llvm::Value *>(Builder.getFalse()),
40564019
CheckOrdinal),
40574020
CheckHandler, EmitCheckSourceLocation(Loc), {});
@@ -6293,7 +6256,7 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType,
62936256
CGM.getTargetCodeGenInfo().getUBSanFunctionSignature(CGM)) {
62946257
auto CheckOrdinal = SanitizerKind::SO_Function;
62956258
auto CheckHandler = SanitizerHandler::FunctionTypeMismatch;
6296-
SanitizerScope SanScope(this, {CheckOrdinal}, CheckHandler);
6259+
SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
62976260
auto *TypeHash = getUBSanFunctionTypeHash(PointeeType);
62986261

62996262
llvm::Type *PrefixSigType = PrefixSig->getType();
@@ -6373,7 +6336,7 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType,
63736336
(!TargetDecl || !isa<FunctionDecl>(TargetDecl))) {
63746337
auto CheckOrdinal = SanitizerKind::SO_CFIICall;
63756338
auto CheckHandler = SanitizerHandler::CFICheckFail;
6376-
SanitizerScope SanScope(this, {CheckOrdinal}, CheckHandler);
6339+
SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
63776340
EmitSanitizerStatReport(llvm::SanStat_CFI_ICall);
63786341

63796342
llvm::Metadata *MD;

0 commit comments

Comments
 (0)