Skip to content

Commit 8c017d6

Browse files
committed
Merge from 'main' to 'sycl-web' (1 commits)
CONFLICT (content): Merge conflict in clang/lib/CodeGen/CGDebugInfo.h
2 parents e7c474d + 428afa6 commit 8c017d6

File tree

14 files changed

+410
-256
lines changed

14 files changed

+410
-256
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,11 +2062,12 @@ Value *CodeGenFunction::EmitCheckedArgForBuiltin(const Expr *E,
20622062
if (!SanOpts.has(SanitizerKind::Builtin))
20632063
return ArgValue;
20642064

2065-
SanitizerScope SanScope(this);
2065+
auto CheckOrdinal = SanitizerKind::SO_Builtin;
2066+
auto CheckHandler = SanitizerHandler::InvalidBuiltin;
2067+
SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
20662068
Value *Cond = Builder.CreateICmpNE(
20672069
ArgValue, llvm::Constant::getNullValue(ArgValue->getType()));
2068-
EmitCheck(std::make_pair(Cond, SanitizerKind::SO_Builtin),
2069-
SanitizerHandler::InvalidBuiltin,
2070+
EmitCheck(std::make_pair(Cond, CheckOrdinal), CheckHandler,
20702071
{EmitCheckSourceLocation(E->getExprLoc()),
20712072
llvm::ConstantInt::get(Builder.getInt8Ty(), Kind)},
20722073
{});
@@ -2078,10 +2079,11 @@ Value *CodeGenFunction::EmitCheckedArgForAssume(const Expr *E) {
20782079
if (!SanOpts.has(SanitizerKind::Builtin))
20792080
return ArgValue;
20802081

2081-
SanitizerScope SanScope(this);
2082+
auto CheckOrdinal = SanitizerKind::SO_Builtin;
2083+
auto CheckHandler = SanitizerHandler::InvalidBuiltin;
2084+
SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
20822085
EmitCheck(
2083-
std::make_pair(ArgValue, SanitizerKind::SO_Builtin),
2084-
SanitizerHandler::InvalidBuiltin,
2086+
std::make_pair(ArgValue, CheckOrdinal), CheckHandler,
20852087
{EmitCheckSourceLocation(E->getExprLoc()),
20862088
llvm::ConstantInt::get(Builder.getInt8Ty(), BCK_AssumePassedFalse)},
20872089
std::nullopt);
@@ -2104,7 +2106,15 @@ static Value *EmitOverflowCheckedAbs(CodeGenFunction &CGF, const CallExpr *E,
21042106
return EmitAbs(CGF, ArgValue, true);
21052107
}
21062108

2107-
CodeGenFunction::SanitizerScope SanScope(&CGF);
2109+
SmallVector<SanitizerKind::SanitizerOrdinal, 1> Ordinals;
2110+
SanitizerHandler CheckHandler;
2111+
if (SanitizeOverflow) {
2112+
Ordinals.push_back(SanitizerKind::SO_SignedIntegerOverflow);
2113+
CheckHandler = SanitizerHandler::NegateOverflow;
2114+
} else
2115+
CheckHandler = SanitizerHandler::SubOverflow;
2116+
2117+
SanitizerDebugLocation SanScope(&CGF, Ordinals, CheckHandler);
21082118

21092119
Constant *Zero = Constant::getNullValue(ArgValue->getType());
21102120
Value *ResultAndOverflow = CGF.Builder.CreateBinaryIntrinsic(
@@ -2116,12 +2126,12 @@ static Value *EmitOverflowCheckedAbs(CodeGenFunction &CGF, const CallExpr *E,
21162126
// TODO: support -ftrapv-handler.
21172127
if (SanitizeOverflow) {
21182128
CGF.EmitCheck({{NotOverflow, SanitizerKind::SO_SignedIntegerOverflow}},
2119-
SanitizerHandler::NegateOverflow,
2129+
CheckHandler,
21202130
{CGF.EmitCheckSourceLocation(E->getArg(0)->getExprLoc()),
21212131
CGF.EmitCheckTypeDescriptor(E->getType())},
21222132
{ArgValue});
21232133
} else
2124-
CGF.EmitTrapCheck(NotOverflow, SanitizerHandler::SubOverflow);
2134+
CGF.EmitTrapCheck(NotOverflow, CheckHandler);
21252135

21262136
Value *CmpResult = CGF.Builder.CreateICmpSLT(ArgValue, Zero, "abscond");
21272137
return CGF.Builder.CreateSelect(CmpResult, Result, ArgValue, "abs");

clang/lib/CodeGen/CGCall.cpp

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

4307-
SanitizerScope SanScope(this);
4307+
SanitizerDebugLocation SanScope(this, {CheckKind}, Handler);
43084308

43094309
// Make sure the "return" source location is valid. If we're checking a
43104310
// nullability annotation, make sure the preconditions for the check are met.
@@ -4689,7 +4689,7 @@ void CodeGenFunction::EmitNonNullArgCheck(RValue RV, QualType ArgType,
46894689
Handler = SanitizerHandler::NullabilityArg;
46904690
}
46914691

4692-
SanitizerScope SanScope(this);
4692+
SanitizerDebugLocation SanScope(this, {CheckKind}, Handler);
46934693
llvm::Value *Cond = EmitNonNullRValueCheck(RV, ArgType);
46944694
llvm::Constant *StaticData[] = {
46954695
EmitCheckSourceLocation(ArgLoc),

clang/lib/CodeGen/CGClass.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,7 +2823,8 @@ void CodeGenFunction::EmitVTablePtrCheckForCall(const CXXRecordDecl *RD,
28232823
RD = LeastDerivedClassWithSameLayout(RD);
28242824

28252825
auto [Ordinal, _] = SanitizerInfoFromCFICheckKind(TCK);
2826-
ApplyDebugLocation ApplyTrapDI(*this, SanitizerAnnotateDebugInfo(Ordinal));
2826+
SanitizerDebugLocation SanScope(this, {Ordinal},
2827+
SanitizerHandler::CFICheckFail);
28272828

28282829
EmitVTablePtrCheck(RD, VTable, TCK, Loc);
28292830
}
@@ -2848,7 +2849,8 @@ void CodeGenFunction::EmitVTablePtrCheckForCast(QualType T, Address Derived,
28482849
ClassDecl = LeastDerivedClassWithSameLayout(ClassDecl);
28492850

28502851
auto [Ordinal, _] = SanitizerInfoFromCFICheckKind(TCK);
2851-
ApplyDebugLocation ApplyTrapDI(*this, SanitizerAnnotateDebugInfo(Ordinal));
2852+
SanitizerDebugLocation SanScope(this, {Ordinal},
2853+
SanitizerHandler::CFICheckFail);
28522854

28532855
llvm::BasicBlock *ContBlock = nullptr;
28542856

@@ -2880,6 +2882,8 @@ void CodeGenFunction::EmitVTablePtrCheck(const CXXRecordDecl *RD,
28802882
llvm::Value *VTable,
28812883
CFITypeCheckKind TCK,
28822884
SourceLocation Loc) {
2885+
assert(IsSanitizerScope);
2886+
28832887
if (!CGM.getCodeGenOpts().SanitizeCfiCrossDso &&
28842888
!CGM.HasHiddenLTOVisibility(RD))
28852889
return;
@@ -2891,7 +2895,6 @@ void CodeGenFunction::EmitVTablePtrCheck(const CXXRecordDecl *RD,
28912895
SanitizerMask::bitPosToMask(M), TypeName))
28922896
return;
28932897

2894-
SanitizerScope SanScope(this);
28952898
EmitSanitizerStatReport(SSK);
28962899

28972900
llvm::Metadata *MD =
@@ -2948,11 +2951,11 @@ bool CodeGenFunction::ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD) {
29482951
llvm::Value *CodeGenFunction::EmitVTableTypeCheckedLoad(
29492952
const CXXRecordDecl *RD, llvm::Value *VTable, llvm::Type *VTableTy,
29502953
uint64_t VTableByteOffset) {
2951-
SanitizerScope SanScope(this);
2954+
auto CheckOrdinal = SanitizerKind::SO_CFIVCall;
2955+
auto CheckHandler = SanitizerHandler::CFICheckFail;
2956+
SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
29522957

29532958
EmitSanitizerStatReport(llvm::SanStat_CFI_VCall);
2954-
ApplyDebugLocation ApplyTrapDI(
2955-
*this, SanitizerAnnotateDebugInfo(SanitizerKind::SO_CFIVCall));
29562959

29572960
llvm::Metadata *MD =
29582961
CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0));
@@ -2971,8 +2974,7 @@ llvm::Value *CodeGenFunction::EmitVTableTypeCheckedLoad(
29712974
if (SanOpts.has(SanitizerKind::CFIVCall) &&
29722975
!getContext().getNoSanitizeList().containsType(SanitizerKind::CFIVCall,
29732976
TypeName)) {
2974-
EmitCheck(std::make_pair(CheckResult, SanitizerKind::SO_CFIVCall),
2975-
SanitizerHandler::CFICheckFail, {}, {});
2977+
EmitCheck(std::make_pair(CheckResult, CheckOrdinal), CheckHandler, {}, {});
29762978
}
29772979

29782980
return Builder.CreateBitCast(Builder.CreateExtractValue(CheckedLoad, 0),

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 82 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())
@@ -6455,3 +6462,78 @@ CodeGenFunction::LexicalScope::~LexicalScope() {
64556462
ForceCleanup();
64566463
}
64576464
}
6465+
6466+
static std::string SanitizerHandlerToCheckLabel(SanitizerHandler Handler) {
6467+
std::string Label;
6468+
switch (Handler) {
6469+
#define SANITIZER_CHECK(Enum, Name, Version) \
6470+
case Enum: \
6471+
Label = "__ubsan_check_" #Name; \
6472+
break;
6473+
6474+
LIST_SANITIZER_CHECKS
6475+
#undef SANITIZER_CHECK
6476+
};
6477+
6478+
// Label doesn't require sanitization
6479+
return Label;
6480+
}
6481+
6482+
static std::string
6483+
SanitizerOrdinalToCheckLabel(SanitizerKind::SanitizerOrdinal Ordinal) {
6484+
std::string Label;
6485+
switch (Ordinal) {
6486+
#define SANITIZER(NAME, ID) \
6487+
case SanitizerKind::SO_##ID: \
6488+
Label = "__ubsan_check_" NAME; \
6489+
break;
6490+
#include "clang/Basic/Sanitizers.def"
6491+
default:
6492+
llvm_unreachable("unexpected sanitizer kind");
6493+
}
6494+
6495+
// Sanitize label (convert hyphens to underscores; also futureproof against
6496+
// non-alpha)
6497+
for (unsigned int i = 0; i < Label.length(); i++)
6498+
if (!std::isalpha(Label[i]))
6499+
Label[i] = '_';
6500+
6501+
return Label;
6502+
}
6503+
6504+
llvm::DILocation *CodeGenFunction::SanitizerAnnotateDebugInfo(
6505+
ArrayRef<SanitizerKind::SanitizerOrdinal> Ordinals,
6506+
SanitizerHandler Handler) {
6507+
std::string Label;
6508+
if (Ordinals.size() == 1)
6509+
Label = SanitizerOrdinalToCheckLabel(Ordinals[0]);
6510+
else
6511+
Label = SanitizerHandlerToCheckLabel(Handler);
6512+
6513+
llvm::DILocation *CheckDI = Builder.getCurrentDebugLocation();
6514+
6515+
for (auto Ord : Ordinals) {
6516+
// TODO: deprecate ClArrayBoundsPseudoFn
6517+
if (((ClArrayBoundsPseudoFn && Ord == SanitizerKind::SO_ArrayBounds) ||
6518+
CGM.getCodeGenOpts().SanitizeAnnotateDebugInfo.has(Ord)) &&
6519+
CheckDI) {
6520+
return getDebugInfo()->CreateSyntheticInlineAt(CheckDI, Label);
6521+
}
6522+
}
6523+
6524+
return CheckDI;
6525+
}
6526+
6527+
SanitizerDebugLocation::SanitizerDebugLocation(
6528+
CodeGenFunction *CGF, ArrayRef<SanitizerKind::SanitizerOrdinal> Ordinals,
6529+
SanitizerHandler Handler)
6530+
: CGF(CGF),
6531+
Apply(*CGF, CGF->SanitizerAnnotateDebugInfo(Ordinals, Handler)) {
6532+
assert(!CGF->IsSanitizerScope);
6533+
CGF->IsSanitizerScope = true;
6534+
}
6535+
6536+
SanitizerDebugLocation::~SanitizerDebugLocation() {
6537+
assert(CGF->IsSanitizerScope);
6538+
CGF->IsSanitizerScope = false;
6539+
}

clang/lib/CodeGen/CGDebugInfo.h

Lines changed: 11 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"
@@ -980,6 +981,16 @@ class ApplyInlineDebugLocation {
980981
};
981982

982983
bool noSystemDebugInfo(const Decl *D, const CodeGenModule &CGM);
984+
class SanitizerDebugLocation {
985+
CodeGenFunction *CGF;
986+
ApplyDebugLocation Apply;
987+
988+
public:
989+
SanitizerDebugLocation(CodeGenFunction *CGF,
990+
ArrayRef<SanitizerKind::SanitizerOrdinal> Ordinals,
991+
SanitizerHandler Handler);
992+
~SanitizerDebugLocation();
993+
};
983994

984995
} // namespace CodeGen
985996
} // namespace clang

clang/lib/CodeGen/CGDecl.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -802,14 +802,15 @@ void CodeGenFunction::EmitNullabilityCheck(LValue LHS, llvm::Value *RHS,
802802

803803
// Check if the right hand side of the assignment is nonnull, if the left
804804
// hand side must be nonnull.
805-
SanitizerScope SanScope(this);
805+
auto CheckOrdinal = SanitizerKind::SO_NullabilityAssign;
806+
auto CheckHandler = SanitizerHandler::TypeMismatch;
807+
SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
806808
llvm::Value *IsNotNull = Builder.CreateIsNotNull(RHS);
807809
llvm::Constant *StaticData[] = {
808810
EmitCheckSourceLocation(Loc), EmitCheckTypeDescriptor(LHS.getType()),
809811
llvm::ConstantInt::get(Int8Ty, 0), // The LogAlignment info is unused.
810812
llvm::ConstantInt::get(Int8Ty, TCK_NonnullAssign)};
811-
EmitCheck({{IsNotNull, SanitizerKind::SO_NullabilityAssign}},
812-
SanitizerHandler::TypeMismatch, StaticData, RHS);
813+
EmitCheck({{IsNotNull, CheckOrdinal}}, CheckHandler, StaticData, RHS);
813814
}
814815

815816
void CodeGenFunction::EmitScalarInit(const Expr *init, const ValueDecl *D,

0 commit comments

Comments
 (0)