1 //=== VLASizeChecker.cpp - Undefined dereference checker --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This defines VLASizeChecker, a builtin check in ExprEngine that
11 // performs checks for declaration of VLA of undefined or zero size.
12 // In addition, VLASizeChecker is responsible for defining the extent
13 // of the MemRegion that represents a VLA.
15 //===----------------------------------------------------------------------===//
17 #include "ClangSACheckers.h"
18 #include "clang/AST/CharUnits.h"
19 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
20 #include "clang/StaticAnalyzer/Core/Checker.h"
21 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/Support/raw_ostream.h"
27 using namespace clang;
31 class VLASizeChecker : public Checker< check::PreStmt<DeclStmt> > {
32 mutable std::unique_ptr<BugType> BT;
33 enum VLASize_Kind { VLA_Garbage, VLA_Zero, VLA_Tainted, VLA_Negative };
35 void reportBug(VLASize_Kind Kind,
37 ProgramStateRef State,
38 CheckerContext &C) const;
40 void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
42 } // end anonymous namespace
44 void VLASizeChecker::reportBug(VLASize_Kind Kind,
46 ProgramStateRef State,
47 CheckerContext &C) const {
48 // Generate an error node.
49 ExplodedNode *N = C.generateErrorNode(State);
54 BT.reset(new BuiltinBug(
55 this, "Dangerous variable-length array (VLA) declaration"));
58 llvm::raw_svector_ostream os(buf);
59 os << "Declared variable-length array (VLA) ";
62 os << "uses a garbage value as its size";
65 os << "has zero size";
68 os << "has tainted size";
71 os << "has negative size";
75 auto report = llvm::make_unique<BugReport>(*BT, os.str(), N);
76 report->addRange(SizeE->getSourceRange());
77 bugreporter::trackNullOrUndefValue(N, SizeE, *report);
78 C.emitReport(std::move(report));
81 void VLASizeChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
82 if (!DS->isSingleDecl())
85 const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
89 ASTContext &Ctx = C.getASTContext();
90 const VariableArrayType *VLA = Ctx.getAsVariableArrayType(VD->getType());
94 // FIXME: Handle multi-dimensional VLAs.
95 const Expr *SE = VLA->getSizeExpr();
96 ProgramStateRef state = C.getState();
97 SVal sizeV = state->getSVal(SE, C.getLocationContext());
99 if (sizeV.isUndef()) {
100 reportBug(VLA_Garbage, SE, state, C);
104 // See if the size value is known. It can't be undefined because we would have
105 // warned about that already.
106 if (sizeV.isUnknown())
109 // Check if the size is tainted.
110 if (state->isTainted(sizeV)) {
111 reportBug(VLA_Tainted, SE, nullptr, C);
115 // Check if the size is zero.
116 DefinedSVal sizeD = sizeV.castAs<DefinedSVal>();
118 ProgramStateRef stateNotZero, stateZero;
119 std::tie(stateNotZero, stateZero) = state->assume(sizeD);
121 if (stateZero && !stateNotZero) {
122 reportBug(VLA_Zero, SE, stateZero, C);
126 // From this point on, assume that the size is not zero.
127 state = stateNotZero;
129 // VLASizeChecker is responsible for defining the extent of the array being
130 // declared. We do this by multiplying the array length by the element size,
131 // then matching that with the array region's extent symbol.
133 // Check if the size is negative.
134 SValBuilder &svalBuilder = C.getSValBuilder();
136 QualType Ty = SE->getType();
137 DefinedOrUnknownSVal Zero = svalBuilder.makeZeroVal(Ty);
139 SVal LessThanZeroVal = svalBuilder.evalBinOp(state, BO_LT, sizeD, Zero, Ty);
140 if (Optional<DefinedSVal> LessThanZeroDVal =
141 LessThanZeroVal.getAs<DefinedSVal>()) {
142 ConstraintManager &CM = C.getConstraintManager();
143 ProgramStateRef StatePos, StateNeg;
145 std::tie(StateNeg, StatePos) = CM.assumeDual(state, *LessThanZeroDVal);
146 if (StateNeg && !StatePos) {
147 reportBug(VLA_Negative, SE, state, C);
153 // Convert the array length to size_t.
154 QualType SizeTy = Ctx.getSizeType();
156 svalBuilder.evalCast(sizeD, SizeTy, SE->getType()).castAs<NonLoc>();
158 // Get the element size.
159 CharUnits EleSize = Ctx.getTypeSizeInChars(VLA->getElementType());
160 SVal EleSizeVal = svalBuilder.makeIntVal(EleSize.getQuantity(), SizeTy);
162 // Multiply the array length by the element size.
163 SVal ArraySizeVal = svalBuilder.evalBinOpNN(
164 state, BO_Mul, ArrayLength, EleSizeVal.castAs<NonLoc>(), SizeTy);
166 // Finally, assume that the array's extent matches the given size.
167 const LocationContext *LC = C.getLocationContext();
168 DefinedOrUnknownSVal Extent =
169 state->getRegion(VD, LC)->getExtent(svalBuilder);
170 DefinedOrUnknownSVal ArraySize = ArraySizeVal.castAs<DefinedOrUnknownSVal>();
171 DefinedOrUnknownSVal sizeIsKnown =
172 svalBuilder.evalEQ(state, Extent, ArraySize);
173 state = state->assume(sizeIsKnown, true);
175 // Assume should not fail at this point.
178 // Remember our assumptions!
179 C.addTransition(state);
182 void ento::registerVLASizeChecker(CheckerManager &mgr) {
183 mgr.registerChecker<VLASizeChecker>();