Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6bc5c7c

Browse files
committed
[ubsan] Add more -fsanitize-annotate-debug-info checks
This extends llvm#138577 to more UBSan checks. Note that the annotations are less detailed: they will always be __ubsan_check_singularity, rather than using the SanitizerKind (previous behavior, which is not always possible for all UBSan checks) or SanitizerHandler. This is a (minor) regression compared to llvm#128977 and llvm#139809. Updates the tests from llvm#128976, llvm#139149 and llvm#141814.
1 parent 9553514 commit 6bc5c7c

16 files changed

+269
-220
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,7 +2006,7 @@ Value *CodeGenFunction::EmitCheckedArgForBuiltin(const Expr *E,
20062006
if (!SanOpts.has(SanitizerKind::Builtin))
20072007
return ArgValue;
20082008

2009-
SanitizerScope SanScope(this);
2009+
SanitizerScope SanScope(this, {SanitizerKind::SO_Builtin});
20102010
Value *Cond = Builder.CreateICmpNE(
20112011
ArgValue, llvm::Constant::getNullValue(ArgValue->getType()));
20122012
EmitCheck(std::make_pair(Cond, SanitizerKind::SO_Builtin),
@@ -2022,7 +2022,7 @@ Value *CodeGenFunction::EmitCheckedArgForAssume(const Expr *E) {
20222022
if (!SanOpts.has(SanitizerKind::Builtin))
20232023
return ArgValue;
20242024

2025-
SanitizerScope SanScope(this);
2025+
SanitizerScope SanScope(this, {SanitizerKind::SO_Builtin});
20262026
EmitCheck(
20272027
std::make_pair(ArgValue, SanitizerKind::SO_Builtin),
20282028
SanitizerHandler::InvalidBuiltin,
@@ -2048,7 +2048,10 @@ static Value *EmitOverflowCheckedAbs(CodeGenFunction &CGF, const CallExpr *E,
20482048
return EmitAbs(CGF, ArgValue, true);
20492049
}
20502050

2051-
CodeGenFunction::SanitizerScope SanScope(&CGF);
2051+
SmallVector<SanitizerKind::SanitizerOrdinal, 3> Kinds;
2052+
if (SanitizeOverflow)
2053+
Kinds.push_back(SanitizerKind::SO_SignedIntegerOverflow);
2054+
CodeGenFunction::SanitizerScope SanScope(&CGF, Kinds);
20522055

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

clang/lib/CodeGen/CGCall.cpp

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

4159-
SanitizerScope SanScope(this);
4159+
SanitizerScope SanScope(this, {CheckKind});
41604160

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

4544-
SanitizerScope SanScope(this);
4544+
SanitizerScope SanScope(this, {CheckKind});
45454545
llvm::Value *Cond = EmitNonNullRValueCheck(RV, ArgType);
45464546
llvm::Constant *StaticData[] = {
45474547
EmitCheckSourceLocation(ArgLoc),
@@ -5976,7 +5976,7 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
59765976
// attribute to insert handler calls.
59775977
if (SanOpts.hasOneOf(SanitizerKind::Address |
59785978
SanitizerKind::KernelAddress)) {
5979-
SanitizerScope SanScope(this);
5979+
SanitizerScope SanScope(this, {});
59805980
llvm::IRBuilder<>::InsertPointGuard IPGuard(Builder);
59815981
Builder.SetInsertPoint(CI);
59825982
auto *FnType = llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);

clang/lib/CodeGen/CGClass.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,7 @@ namespace {
16781678
static void EmitSanitizerDtorCallback(
16791679
CodeGenFunction &CGF, StringRef Name, llvm::Value *Ptr,
16801680
std::optional<CharUnits::QuantityType> PoisonSize = {}) {
1681-
CodeGenFunction::SanitizerScope SanScope(&CGF);
1681+
CodeGenFunction::SanitizerScope SanScope(&CGF, {});
16821682
// Pass in void pointer and size of region as arguments to runtime
16831683
// function
16841684
SmallVector<llvm::Value *, 2> Args = {Ptr};
@@ -2885,7 +2885,7 @@ void CodeGenFunction::EmitVTablePtrCheck(const CXXRecordDecl *RD,
28852885
SanitizerMask::bitPosToMask(M), TypeName))
28862886
return;
28872887

2888-
SanitizerScope SanScope(this);
2888+
SanitizerScope SanScope(this, {M});
28892889
EmitSanitizerStatReport(SSK);
28902890

28912891
llvm::Metadata *MD =
@@ -2942,11 +2942,9 @@ bool CodeGenFunction::ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD) {
29422942
llvm::Value *CodeGenFunction::EmitVTableTypeCheckedLoad(
29432943
const CXXRecordDecl *RD, llvm::Value *VTable, llvm::Type *VTableTy,
29442944
uint64_t VTableByteOffset) {
2945-
SanitizerScope SanScope(this);
2945+
SanitizerScope SanScope(this, {SanitizerKind::SO_CFIVCall});
29462946

29472947
EmitSanitizerStatReport(llvm::SanStat_CFI_VCall);
2948-
ApplyDebugLocation ApplyTrapDI(
2949-
*this, SanitizerAnnotateDebugInfo(SanitizerKind::SO_CFIVCall));
29502948

29512949
llvm::Metadata *MD =
29522950
CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0));

clang/lib/CodeGen/CGDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ void CodeGenFunction::EmitNullabilityCheck(LValue LHS, llvm::Value *RHS,
765765

766766
// Check if the right hand side of the assignment is nonnull, if the left
767767
// hand side must be nonnull.
768-
SanitizerScope SanScope(this);
768+
SanitizerScope SanScope(this, {SanitizerKind::SO_NullabilityAssign});
769769
llvm::Value *IsNotNull = Builder.CreateIsNotNull(RHS);
770770
llvm::Constant *StaticData[] = {
771771
EmitCheckSourceLocation(Loc), EmitCheckTypeDescriptor(LHS.getType()),
@@ -2852,7 +2852,7 @@ void CodeGenFunction::EmitParmDecl(const VarDecl &D, ParamValue Arg,
28522852
if (requiresReturnValueNullabilityCheck()) {
28532853
auto Nullability = Ty->getNullability();
28542854
if (Nullability && *Nullability == NullabilityKind::NonNull) {
2855-
SanitizerScope SanScope(this);
2855+
SanitizerScope SanScope(this, {});
28562856
RetValNullabilityPrecondition =
28572857
Builder.CreateAnd(RetValNullabilityPrecondition,
28582858
Builder.CreateIsNotNull(Arg.getAnyValue()));

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,9 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
748748
if (Ty.isVolatileQualified())
749749
return;
750750

751-
SanitizerScope SanScope(this);
751+
SanitizerScope SanScope(
752+
this, {SanitizerKind::SO_Null, SanitizerKind::SO_ObjectSize,
753+
SanitizerKind::SO_Alignment, SanitizerKind::SO_Vptr});
752754

753755
SmallVector<std::pair<llvm::Value *, SanitizerKind::SanitizerOrdinal>, 3>
754756
Checks;
@@ -989,7 +991,7 @@ static llvm::Value *getArrayIndexingBound(CodeGenFunction &CGF,
989991
if (CE->getCastKind() == CK_ArrayToPointerDecay &&
990992
!CE->getSubExpr()->isFlexibleArrayMemberLike(CGF.getContext(),
991993
StrictFlexArraysLevel)) {
992-
CodeGenFunction::SanitizerScope SanScope(&CGF);
994+
CodeGenFunction::SanitizerScope SanScope(&CGF, {});
993995

994996
IndexedType = CE->getSubExpr()->getType();
995997
const ArrayType *AT = IndexedType->castAsArrayTypeUnsafe();
@@ -1002,7 +1004,7 @@ static llvm::Value *getArrayIndexingBound(CodeGenFunction &CGF,
10021004
}
10031005
}
10041006

1005-
CodeGenFunction::SanitizerScope SanScope(&CGF);
1007+
CodeGenFunction::SanitizerScope SanScope(&CGF, {});
10061008

10071009
QualType EltTy{Base->getType()->getPointeeOrArrayElementType(), 0};
10081010
if (llvm::Value *POS = CGF.LoadPassedObjectSize(Base, EltTy)) {
@@ -1224,10 +1226,8 @@ void CodeGenFunction::EmitBoundsCheckImpl(const Expr *E, llvm::Value *Bound,
12241226
if (!Bound)
12251227
return;
12261228

1227-
SanitizerScope SanScope(this);
1228-
12291229
auto CheckKind = SanitizerKind::SO_ArrayBounds;
1230-
ApplyDebugLocation ApplyTrapDI(*this, SanitizerAnnotateDebugInfo(CheckKind));
1230+
SanitizerScope SanScope(this, {CheckKind});
12311231

12321232
bool IndexSigned = IndexType->isSignedIntegerOrEnumerationType();
12331233
llvm::Value *IndexVal = Builder.CreateIntCast(Index, SizeTy, IndexSigned);
@@ -1245,30 +1245,21 @@ void CodeGenFunction::EmitBoundsCheckImpl(const Expr *E, llvm::Value *Bound,
12451245
}
12461246

12471247
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-
1248+
ArrayRef<SanitizerKind::SanitizerOrdinal> Ordinals) {
12651249
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);
1250+
1251+
// TODO: the annotation could be more precise:
1252+
// 1) use the ordinal name if there is only one ordinal
1253+
// 2) use the overarching SanitizerHandler if there are multiple ordinals
1254+
for (auto Ord : Ordinals) {
1255+
if (((ClArrayBoundsPseudoFn && Ord == SanitizerKind::SO_ArrayBounds) ||
1256+
CGM.getCodeGenOpts().SanitizeAnnotateDebugInfo.has(Ord)) &&
1257+
CheckDI) {
1258+
CheckDI = getDebugInfo()->CreateSyntheticInlineAt(
1259+
CheckDI, "__ubsan_check_singularity");
1260+
break;
1261+
}
1262+
}
12721263

12731264
return CheckDI;
12741265
}
@@ -1994,8 +1985,11 @@ bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty,
19941985
if (!getRangeForType(*this, Ty, Min, End, /*StrictEnums=*/true, IsBool))
19951986
return true;
19961987

1988+
SanitizerKind::SanitizerOrdinal Kind =
1989+
NeedsEnumCheck ? SanitizerKind::SO_Enum : SanitizerKind::SO_Bool;
1990+
19971991
auto &Ctx = getLLVMContext();
1998-
SanitizerScope SanScope(this);
1992+
SanitizerScope SanScope(this, {Kind});
19991993
llvm::Value *Check;
20001994
--End;
20011995
if (!Min) {
@@ -2009,8 +2003,6 @@ bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty,
20092003
}
20102004
llvm::Constant *StaticArgs[] = {EmitCheckSourceLocation(Loc),
20112005
EmitCheckTypeDescriptor(Ty)};
2012-
SanitizerKind::SanitizerOrdinal Kind =
2013-
NeedsEnumCheck ? SanitizerKind::SO_Enum : SanitizerKind::SO_Bool;
20142006
EmitCheck(std::make_pair(Check, Kind), SanitizerHandler::LoadInvalidValue,
20152007
StaticArgs, Value);
20162008
return true;
@@ -3931,7 +3923,14 @@ void CodeGenFunction::EmitCfiCheckStub() {
39313923
// can be nullptr if the calling module has -fsanitize-trap behavior for this
39323924
// check kind; in this case __cfi_check_fail traps as well.
39333925
void CodeGenFunction::EmitCfiCheckFail() {
3934-
SanitizerScope SanScope(this);
3926+
// TODO: the SanitizerKind is not yet determined for this check (and might
3927+
// not even be available, if Data == nullptr). However, we still want to
3928+
// annotate the instrumentation. We approximate this by using all the CFI
3929+
// kinds.
3930+
SanitizerScope SanScope(
3931+
this, {SanitizerKind::SO_CFIVCall, SanitizerKind::SO_CFINVCall,
3932+
SanitizerKind::SO_CFIDerivedCast,
3933+
SanitizerKind::SO_CFIUnrelatedCast, SanitizerKind::SO_CFIICall});
39353934
FunctionArgList Args;
39363935
ImplicitParamDecl ArgData(getContext(), getContext().VoidPtrTy,
39373936
ImplicitParamKind::Other);
@@ -4030,7 +4029,7 @@ void CodeGenFunction::EmitCfiCheckFail() {
40304029

40314030
void CodeGenFunction::EmitUnreachable(SourceLocation Loc) {
40324031
if (SanOpts.has(SanitizerKind::Unreachable)) {
4033-
SanitizerScope SanScope(this);
4032+
SanitizerScope SanScope(this, {SanitizerKind::SO_Unreachable});
40344033
EmitCheck(std::make_pair(static_cast<llvm::Value *>(Builder.getFalse()),
40354034
SanitizerKind::SO_Unreachable),
40364035
SanitizerHandler::BuiltinUnreachable,
@@ -6271,7 +6270,7 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType,
62716270
!isa<FunctionNoProtoType>(PointeeType)) {
62726271
if (llvm::Constant *PrefixSig =
62736272
CGM.getTargetCodeGenInfo().getUBSanFunctionSignature(CGM)) {
6274-
SanitizerScope SanScope(this);
6273+
SanitizerScope SanScope(this, {SanitizerKind::SO_Function});
62756274
auto *TypeHash = getUBSanFunctionTypeHash(PointeeType);
62766275

62776276
llvm::Type *PrefixSigType = PrefixSig->getType();
@@ -6350,7 +6349,7 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType,
63506349
// function pointer is a member of the bit set for the function type.
63516350
if (SanOpts.has(SanitizerKind::CFIICall) &&
63526351
(!TargetDecl || !isa<FunctionDecl>(TargetDecl))) {
6353-
SanitizerScope SanScope(this);
6352+
SanitizerScope SanScope(this, {SanitizerKind::SO_CFIICall});
63546353
EmitSanitizerStatReport(llvm::SanStat_CFI_ICall);
63556354
ApplyDebugLocation ApplyTrapDI(
63566355
*this, SanitizerAnnotateDebugInfo(SanitizerKind::SO_CFIICall));

0 commit comments

Comments
 (0)