003 File Manager
Current Path:
/usr/src/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers
usr
/
src
/
contrib
/
llvm-project
/
clang
/
lib
/
StaticAnalyzer
/
Checkers
/
📁
..
📄
AllocationState.h
(1.41 KB)
📄
AnalysisOrderChecker.cpp
(8.62 KB)
📄
AnalyzerStatsChecker.cpp
(5.04 KB)
📄
ArrayBoundChecker.cpp
(3.22 KB)
📄
ArrayBoundCheckerV2.cpp
(13.07 KB)
📄
BasicObjCFoundationChecks.cpp
(43.82 KB)
📄
BlockInCriticalSectionChecker.cpp
(6.69 KB)
📄
BoolAssignmentChecker.cpp
(3.45 KB)
📄
BuiltinFunctionChecker.cpp
(5 KB)
📄
CStringChecker.cpp
(93.24 KB)
📄
CStringSyntaxChecker.cpp
(9.8 KB)
📄
CXXSelfAssignmentChecker.cpp
(3.08 KB)
📄
CallAndMessageChecker.cpp
(26.57 KB)
📄
CastSizeChecker.cpp
(4.96 KB)
📄
CastToStructChecker.cpp
(4.43 KB)
📄
CastValueChecker.cpp
(19.65 KB)
📄
CheckObjCDealloc.cpp
(37.83 KB)
📄
CheckObjCInstMethSignature.cpp
(4.8 KB)
📄
CheckPlacementNew.cpp
(11.91 KB)
📄
CheckSecuritySyntaxOnly.cpp
(38.79 KB)
📄
CheckSizeofPointer.cpp
(3.16 KB)
📄
CheckerDocumentation.cpp
(14.64 KB)
📄
ChrootChecker.cpp
(4.88 KB)
📄
CloneChecker.cpp
(8.28 KB)
📄
ContainerModeling.cpp
(39.85 KB)
📄
ConversionChecker.cpp
(7.11 KB)
📄
DeadStoresChecker.cpp
(17.82 KB)
📄
DebugCheckers.cpp
(10.41 KB)
📄
DebugContainerModeling.cpp
(4.94 KB)
📄
DebugIteratorModeling.cpp
(5.15 KB)
📄
DeleteWithNonVirtualDtorChecker.cpp
(5.35 KB)
📄
DereferenceChecker.cpp
(10.54 KB)
📄
DirectIvarAssignment.cpp
(8.02 KB)
📄
DivZeroChecker.cpp
(3.42 KB)
📄
DynamicTypeChecker.cpp
(7.31 KB)
📄
DynamicTypePropagation.cpp
(42.11 KB)
📄
EnumCastOutOfRangeChecker.cpp
(5.75 KB)
📄
ExprInspectionChecker.cpp
(15.6 KB)
📄
FixedAddressChecker.cpp
(2.33 KB)
📄
FuchsiaHandleChecker.cpp
(23.1 KB)
📄
GCDAntipatternChecker.cpp
(7.85 KB)
📄
GTestChecker.cpp
(10.52 KB)
📄
GenericTaintChecker.cpp
(34.31 KB)
📄
IdenticalExprChecker.cpp
(18.88 KB)
📄
InnerPointerChecker.cpp
(11.54 KB)
📄
InterCheckerAPI.h
(1.09 KB)
📄
InvalidatedIteratorChecker.cpp
(4.93 KB)
📄
Iterator.cpp
(10.62 KB)
📄
Iterator.h
(6.32 KB)
📄
IteratorModeling.cpp
(31.33 KB)
📄
IteratorRangeChecker.cpp
(12.64 KB)
📄
IvarInvalidationChecker.cpp
(27.63 KB)
📄
LLVMConventionsChecker.cpp
(9.9 KB)
📄
LocalizationChecker.cpp
(52.38 KB)
📄
MIGChecker.cpp
(11.3 KB)
📁
MPI-Checker
📄
MacOSKeychainAPIChecker.cpp
(25.21 KB)
📄
MacOSXAPIChecker.cpp
(6.61 KB)
📄
MallocChecker.cpp
(127.38 KB)
📄
MallocOverflowSecurityChecker.cpp
(11.91 KB)
📄
MallocSizeofChecker.cpp
(8.03 KB)
📄
MismatchedIteratorChecker.cpp
(11.01 KB)
📄
MmapWriteExecChecker.cpp
(3.22 KB)
📄
Move.h
(1.07 KB)
📄
MoveChecker.cpp
(27 KB)
📄
NSAutoreleasePoolChecker.cpp
(2.93 KB)
📄
NSErrorChecker.cpp
(10.6 KB)
📄
NoReturnFunctionChecker.cpp
(5.48 KB)
📄
NonNullParamChecker.cpp
(11.22 KB)
📄
NonnullGlobalConstantsChecker.cpp
(5.09 KB)
📄
NullabilityChecker.cpp
(46.95 KB)
📄
NumberObjectConversionChecker.cpp
(13.69 KB)
📄
OSObjectCStyleCast.cpp
(3.14 KB)
📄
ObjCAtSyncChecker.cpp
(3.34 KB)
📄
ObjCAutoreleaseWriteChecker.cpp
(8.62 KB)
📄
ObjCContainersASTChecker.cpp
(5.5 KB)
📄
ObjCContainersChecker.cpp
(6.71 KB)
📄
ObjCMissingSuperCallChecker.cpp
(9.23 KB)
📄
ObjCPropertyChecker.cpp
(3.03 KB)
📄
ObjCSelfInitChecker.cpp
(16.21 KB)
📄
ObjCSuperDeallocChecker.cpp
(9.33 KB)
📄
ObjCUnusedIVarsChecker.cpp
(6.01 KB)
📄
PaddingChecker.cpp
(14.26 KB)
📄
PointerArithChecker.cpp
(12.18 KB)
📄
PointerIterationChecker.cpp
(3.79 KB)
📄
PointerSortingChecker.cpp
(4.5 KB)
📄
PointerSubChecker.cpp
(2.56 KB)
📄
PthreadLockChecker.cpp
(28.42 KB)
📁
RetainCountChecker
📄
ReturnPointerRangeChecker.cpp
(3.39 KB)
📄
ReturnUndefChecker.cpp
(4.1 KB)
📄
ReturnValueChecker.cpp
(5.79 KB)
📄
RunLoopAutoreleaseLeakChecker.cpp
(7.2 KB)
📄
STLAlgorithmModeling.cpp
(7 KB)
📄
SimpleStreamChecker.cpp
(9.43 KB)
📄
SmartPtr.h
(1.17 KB)
📄
SmartPtrChecker.cpp
(2.68 KB)
📄
SmartPtrModeling.cpp
(8.05 KB)
📄
StackAddrEscapeChecker.cpp
(15 KB)
📄
StdLibraryFunctionsChecker.cpp
(74.86 KB)
📄
StreamChecker.cpp
(37.36 KB)
📄
Taint.cpp
(9.04 KB)
📄
Taint.h
(4.19 KB)
📄
TaintTesterChecker.cpp
(2.17 KB)
📄
TestAfterDivZeroChecker.cpp
(8.19 KB)
📄
TraversalChecker.cpp
(4.38 KB)
📄
TrustNonnullChecker.cpp
(9.12 KB)
📄
UndefBranchChecker.cpp
(3.82 KB)
📄
UndefCapturedBlockVarChecker.cpp
(3.64 KB)
📄
UndefResultChecker.cpp
(7.19 KB)
📄
UndefinedArraySubscriptChecker.cpp
(2.36 KB)
📄
UndefinedAssignmentChecker.cpp
(3.74 KB)
📁
UninitializedObject
📄
UnixAPIChecker.cpp
(18.05 KB)
📄
UnreachableCodeChecker.cpp
(9.59 KB)
📄
VLASizeChecker.cpp
(10.95 KB)
📄
ValistChecker.cpp
(15.65 KB)
📄
VforkChecker.cpp
(7.67 KB)
📄
VirtualCallChecker.cpp
(8.11 KB)
📁
WebKit
📄
Yaml.h
(2.06 KB)
📁
cert
Editing: VLASizeChecker.cpp
//=== VLASizeChecker.cpp - Undefined dereference checker --------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This defines VLASizeChecker, a builtin check in ExprEngine that // performs checks for declaration of VLA of undefined or zero size. // In addition, VLASizeChecker is responsible for defining the extent // of the MemRegion that represents a VLA. // //===----------------------------------------------------------------------===// #include "Taint.h" #include "clang/AST/CharUnits.h" #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" #include "clang/StaticAnalyzer/Core/Checker.h" #include "clang/StaticAnalyzer/Core/CheckerManager.h" #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicSize.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallString.h" #include "llvm/Support/raw_ostream.h" using namespace clang; using namespace ento; using namespace taint; namespace { class VLASizeChecker : public Checker<check::PreStmt<DeclStmt>, check::PreStmt<UnaryExprOrTypeTraitExpr>> { mutable std::unique_ptr<BugType> BT; enum VLASize_Kind { VLA_Garbage, VLA_Zero, VLA_Tainted, VLA_Negative, VLA_Overflow }; /// Check a VLA for validity. /// Every dimension of the array and the total size is checked for validity. /// Returns null or a new state where the size is validated. /// 'ArraySize' will contain SVal that refers to the total size (in char) /// of the array. ProgramStateRef checkVLA(CheckerContext &C, ProgramStateRef State, const VariableArrayType *VLA, SVal &ArraySize) const; /// Check a single VLA index size expression for validity. ProgramStateRef checkVLAIndexSize(CheckerContext &C, ProgramStateRef State, const Expr *SizeE) const; void reportBug(VLASize_Kind Kind, const Expr *SizeE, ProgramStateRef State, CheckerContext &C, std::unique_ptr<BugReporterVisitor> Visitor = nullptr) const; public: void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const; void checkPreStmt(const UnaryExprOrTypeTraitExpr *UETTE, CheckerContext &C) const; }; } // end anonymous namespace ProgramStateRef VLASizeChecker::checkVLA(CheckerContext &C, ProgramStateRef State, const VariableArrayType *VLA, SVal &ArraySize) const { assert(VLA && "Function should be called with non-null VLA argument."); const VariableArrayType *VLALast = nullptr; llvm::SmallVector<const Expr *, 2> VLASizes; // Walk over the VLAs for every dimension until a non-VLA is found. // There is a VariableArrayType for every dimension (fixed or variable) until // the most inner array that is variably modified. // Dimension sizes are collected into 'VLASizes'. 'VLALast' is set to the // innermost VLA that was encountered. // In "int vla[x][2][y][3]" this will be the array for index "y" (with type // int[3]). 'VLASizes' contains 'x', '2', and 'y'. while (VLA) { const Expr *SizeE = VLA->getSizeExpr(); State = checkVLAIndexSize(C, State, SizeE); if (!State) return nullptr; VLASizes.push_back(SizeE); VLALast = VLA; VLA = C.getASTContext().getAsVariableArrayType(VLA->getElementType()); }; assert(VLALast && "Array should have at least one variably-modified dimension."); ASTContext &Ctx = C.getASTContext(); SValBuilder &SVB = C.getSValBuilder(); CanQualType SizeTy = Ctx.getSizeType(); uint64_t SizeMax = SVB.getBasicValueFactory().getMaxValue(SizeTy).getZExtValue(); // Get the element size. CharUnits EleSize = Ctx.getTypeSizeInChars(VLALast->getElementType()); NonLoc ArrSize = SVB.makeIntVal(EleSize.getQuantity(), SizeTy).castAs<NonLoc>(); // Try to calculate the known real size of the array in KnownSize. uint64_t KnownSize = 0; if (const llvm::APSInt *KV = SVB.getKnownValue(State, ArrSize)) KnownSize = KV->getZExtValue(); for (const Expr *SizeE : VLASizes) { auto SizeD = C.getSVal(SizeE).castAs<DefinedSVal>(); // Convert the array length to size_t. NonLoc IndexLength = SVB.evalCast(SizeD, SizeTy, SizeE->getType()).castAs<NonLoc>(); // Multiply the array length by the element size. SVal Mul = SVB.evalBinOpNN(State, BO_Mul, ArrSize, IndexLength, SizeTy); if (auto MulNonLoc = Mul.getAs<NonLoc>()) ArrSize = *MulNonLoc; else // Extent could not be determined. return State; if (const llvm::APSInt *IndexLVal = SVB.getKnownValue(State, IndexLength)) { // Check if the array size will overflow. // Size overflow check does not work with symbolic expressions because a // overflow situation can not be detected easily. uint64_t IndexL = IndexLVal->getZExtValue(); // FIXME: See https://reviews.llvm.org/D80903 for discussion of // some difference in assume and getKnownValue that leads to // unexpected behavior. Just bail on IndexL == 0 at this point. if (IndexL == 0) return nullptr; if (KnownSize <= SizeMax / IndexL) { KnownSize *= IndexL; } else { // Array size does not fit into size_t. reportBug(VLA_Overflow, SizeE, State, C); return nullptr; } } else { KnownSize = 0; } } ArraySize = ArrSize; return State; } ProgramStateRef VLASizeChecker::checkVLAIndexSize(CheckerContext &C, ProgramStateRef State, const Expr *SizeE) const { SVal SizeV = C.getSVal(SizeE); if (SizeV.isUndef()) { reportBug(VLA_Garbage, SizeE, State, C); return nullptr; } // See if the size value is known. It can't be undefined because we would have // warned about that already. if (SizeV.isUnknown()) return nullptr; // Check if the size is tainted. if (isTainted(State, SizeV)) { reportBug(VLA_Tainted, SizeE, nullptr, C, std::make_unique<TaintBugVisitor>(SizeV)); return nullptr; } // Check if the size is zero. DefinedSVal SizeD = SizeV.castAs<DefinedSVal>(); ProgramStateRef StateNotZero, StateZero; std::tie(StateNotZero, StateZero) = State->assume(SizeD); if (StateZero && !StateNotZero) { reportBug(VLA_Zero, SizeE, StateZero, C); return nullptr; } // From this point on, assume that the size is not zero. State = StateNotZero; // Check if the size is negative. SValBuilder &SVB = C.getSValBuilder(); QualType SizeTy = SizeE->getType(); DefinedOrUnknownSVal Zero = SVB.makeZeroVal(SizeTy); SVal LessThanZeroVal = SVB.evalBinOp(State, BO_LT, SizeD, Zero, SizeTy); if (Optional<DefinedSVal> LessThanZeroDVal = LessThanZeroVal.getAs<DefinedSVal>()) { ConstraintManager &CM = C.getConstraintManager(); ProgramStateRef StatePos, StateNeg; std::tie(StateNeg, StatePos) = CM.assumeDual(State, *LessThanZeroDVal); if (StateNeg && !StatePos) { reportBug(VLA_Negative, SizeE, State, C); return nullptr; } State = StatePos; } return State; } void VLASizeChecker::reportBug( VLASize_Kind Kind, const Expr *SizeE, ProgramStateRef State, CheckerContext &C, std::unique_ptr<BugReporterVisitor> Visitor) const { // Generate an error node. ExplodedNode *N = C.generateErrorNode(State); if (!N) return; if (!BT) BT.reset(new BuiltinBug( this, "Dangerous variable-length array (VLA) declaration")); SmallString<256> buf; llvm::raw_svector_ostream os(buf); os << "Declared variable-length array (VLA) "; switch (Kind) { case VLA_Garbage: os << "uses a garbage value as its size"; break; case VLA_Zero: os << "has zero size"; break; case VLA_Tainted: os << "has tainted size"; break; case VLA_Negative: os << "has negative size"; break; case VLA_Overflow: os << "has too large size"; break; } auto report = std::make_unique<PathSensitiveBugReport>(*BT, os.str(), N); report->addVisitor(std::move(Visitor)); report->addRange(SizeE->getSourceRange()); bugreporter::trackExpressionValue(N, SizeE, *report); C.emitReport(std::move(report)); } void VLASizeChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const { if (!DS->isSingleDecl()) return; ASTContext &Ctx = C.getASTContext(); SValBuilder &SVB = C.getSValBuilder(); ProgramStateRef State = C.getState(); QualType TypeToCheck; const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl()); if (VD) TypeToCheck = VD->getType().getCanonicalType(); else if (const auto *TND = dyn_cast<TypedefNameDecl>(DS->getSingleDecl())) TypeToCheck = TND->getUnderlyingType().getCanonicalType(); else return; const VariableArrayType *VLA = Ctx.getAsVariableArrayType(TypeToCheck); if (!VLA) return; // Check the VLA sizes for validity. SVal ArraySize; State = checkVLA(C, State, VLA, ArraySize); if (!State) return; auto ArraySizeNL = ArraySize.getAs<NonLoc>(); if (!ArraySizeNL) { // Array size could not be determined but state may contain new assumptions. C.addTransition(State); return; } // VLASizeChecker is responsible for defining the extent of the array being // declared. We do this by multiplying the array length by the element size, // then matching that with the array region's extent symbol. if (VD) { // Assume that the array's size matches the region size. const LocationContext *LC = C.getLocationContext(); DefinedOrUnknownSVal DynSize = getDynamicSize(State, State->getRegion(VD, LC), SVB); DefinedOrUnknownSVal SizeIsKnown = SVB.evalEQ(State, DynSize, *ArraySizeNL); State = State->assume(SizeIsKnown, true); // Assume should not fail at this point. assert(State); } // Remember our assumptions! C.addTransition(State); } void VLASizeChecker::checkPreStmt(const UnaryExprOrTypeTraitExpr *UETTE, CheckerContext &C) const { // Want to check for sizeof. if (UETTE->getKind() != UETT_SizeOf) return; // Ensure a type argument. if (!UETTE->isArgumentType()) return; const VariableArrayType *VLA = C.getASTContext().getAsVariableArrayType( UETTE->getTypeOfArgument().getCanonicalType()); // Ensure that the type is a VLA. if (!VLA) return; ProgramStateRef State = C.getState(); SVal ArraySize; State = checkVLA(C, State, VLA, ArraySize); if (!State) return; C.addTransition(State); } void ento::registerVLASizeChecker(CheckerManager &mgr) { mgr.registerChecker<VLASizeChecker>(); } bool ento::shouldRegisterVLASizeChecker(const CheckerManager &mgr) { return true; }
Upload File
Create Folder