@@ -748,7 +748,9 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
748
748
if (Ty.isVolatileQualified ())
749
749
return ;
750
750
751
- SanitizerScope SanScope (this );
751
+ SanitizerScope SanScope (
752
+ this , {SanitizerKind::SO_Null, SanitizerKind::SO_ObjectSize,
753
+ SanitizerKind::SO_Alignment, SanitizerKind::SO_Vptr});
752
754
753
755
SmallVector<std::pair<llvm::Value *, SanitizerKind::SanitizerOrdinal>, 3 >
754
756
Checks;
@@ -989,7 +991,7 @@ static llvm::Value *getArrayIndexingBound(CodeGenFunction &CGF,
989
991
if (CE->getCastKind () == CK_ArrayToPointerDecay &&
990
992
!CE->getSubExpr ()->isFlexibleArrayMemberLike (CGF.getContext (),
991
993
StrictFlexArraysLevel)) {
992
- CodeGenFunction::SanitizerScope SanScope (&CGF);
994
+ CodeGenFunction::SanitizerScope SanScope (&CGF, {} );
993
995
994
996
IndexedType = CE->getSubExpr ()->getType ();
995
997
const ArrayType *AT = IndexedType->castAsArrayTypeUnsafe ();
@@ -1002,7 +1004,7 @@ static llvm::Value *getArrayIndexingBound(CodeGenFunction &CGF,
1002
1004
}
1003
1005
}
1004
1006
1005
- CodeGenFunction::SanitizerScope SanScope (&CGF);
1007
+ CodeGenFunction::SanitizerScope SanScope (&CGF, {} );
1006
1008
1007
1009
QualType EltTy{Base->getType ()->getPointeeOrArrayElementType (), 0 };
1008
1010
if (llvm::Value *POS = CGF.LoadPassedObjectSize (Base, EltTy)) {
@@ -1224,10 +1226,8 @@ void CodeGenFunction::EmitBoundsCheckImpl(const Expr *E, llvm::Value *Bound,
1224
1226
if (!Bound)
1225
1227
return ;
1226
1228
1227
- SanitizerScope SanScope (this );
1228
-
1229
1229
auto CheckKind = SanitizerKind::SO_ArrayBounds;
1230
- ApplyDebugLocation ApplyTrapDI (* this , SanitizerAnnotateDebugInfo ( CheckKind) );
1230
+ SanitizerScope SanScope ( this , { CheckKind} );
1231
1231
1232
1232
bool IndexSigned = IndexType->isSignedIntegerOrEnumerationType ();
1233
1233
llvm::Value *IndexVal = Builder.CreateIntCast (Index, SizeTy, IndexSigned);
@@ -1245,30 +1245,21 @@ void CodeGenFunction::EmitBoundsCheckImpl(const Expr *E, llvm::Value *Bound,
1245
1245
}
1246
1246
1247
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
-
1248
+ ArrayRef<SanitizerKind::SanitizerOrdinal> Ordinals) {
1265
1249
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
+ }
1272
1263
1273
1264
return CheckDI;
1274
1265
}
@@ -1994,8 +1985,11 @@ bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty,
1994
1985
if (!getRangeForType (*this , Ty, Min, End, /* StrictEnums=*/ true , IsBool))
1995
1986
return true ;
1996
1987
1988
+ SanitizerKind::SanitizerOrdinal Kind =
1989
+ NeedsEnumCheck ? SanitizerKind::SO_Enum : SanitizerKind::SO_Bool;
1990
+
1997
1991
auto &Ctx = getLLVMContext ();
1998
- SanitizerScope SanScope (this );
1992
+ SanitizerScope SanScope (this , {Kind} );
1999
1993
llvm::Value *Check;
2000
1994
--End;
2001
1995
if (!Min) {
@@ -2009,8 +2003,6 @@ bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty,
2009
2003
}
2010
2004
llvm::Constant *StaticArgs[] = {EmitCheckSourceLocation (Loc),
2011
2005
EmitCheckTypeDescriptor (Ty)};
2012
- SanitizerKind::SanitizerOrdinal Kind =
2013
- NeedsEnumCheck ? SanitizerKind::SO_Enum : SanitizerKind::SO_Bool;
2014
2006
EmitCheck (std::make_pair (Check, Kind), SanitizerHandler::LoadInvalidValue,
2015
2007
StaticArgs, Value);
2016
2008
return true ;
@@ -3931,7 +3923,14 @@ void CodeGenFunction::EmitCfiCheckStub() {
3931
3923
// can be nullptr if the calling module has -fsanitize-trap behavior for this
3932
3924
// check kind; in this case __cfi_check_fail traps as well.
3933
3925
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});
3935
3934
FunctionArgList Args;
3936
3935
ImplicitParamDecl ArgData (getContext (), getContext ().VoidPtrTy ,
3937
3936
ImplicitParamKind::Other);
@@ -4030,7 +4029,7 @@ void CodeGenFunction::EmitCfiCheckFail() {
4030
4029
4031
4030
void CodeGenFunction::EmitUnreachable (SourceLocation Loc) {
4032
4031
if (SanOpts.has (SanitizerKind::Unreachable)) {
4033
- SanitizerScope SanScope (this );
4032
+ SanitizerScope SanScope (this , {SanitizerKind::SO_Unreachable} );
4034
4033
EmitCheck (std::make_pair (static_cast <llvm::Value *>(Builder.getFalse ()),
4035
4034
SanitizerKind::SO_Unreachable),
4036
4035
SanitizerHandler::BuiltinUnreachable,
@@ -6271,7 +6270,7 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType,
6271
6270
!isa<FunctionNoProtoType>(PointeeType)) {
6272
6271
if (llvm::Constant *PrefixSig =
6273
6272
CGM.getTargetCodeGenInfo ().getUBSanFunctionSignature (CGM)) {
6274
- SanitizerScope SanScope (this );
6273
+ SanitizerScope SanScope (this , {SanitizerKind::SO_Function} );
6275
6274
auto *TypeHash = getUBSanFunctionTypeHash (PointeeType);
6276
6275
6277
6276
llvm::Type *PrefixSigType = PrefixSig->getType ();
@@ -6350,7 +6349,7 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType,
6350
6349
// function pointer is a member of the bit set for the function type.
6351
6350
if (SanOpts.has (SanitizerKind::CFIICall) &&
6352
6351
(!TargetDecl || !isa<FunctionDecl>(TargetDecl))) {
6353
- SanitizerScope SanScope (this );
6352
+ SanitizerScope SanScope (this , {SanitizerKind::SO_CFIICall} );
6354
6353
EmitSanitizerStatReport (llvm::SanStat_CFI_ICall);
6355
6354
ApplyDebugLocation ApplyTrapDI (
6356
6355
*this , SanitizerAnnotateDebugInfo (SanitizerKind::SO_CFIICall));
0 commit comments