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/StaticAnalyzer/Core/Checker.h"
19 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
21 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
22 #include "clang/AST/CharUnits.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/STLExtras.h"
26 using namespace clang;
30 class VLASizeChecker : public Checker< check::PreStmt<DeclStmt> > {
31 mutable OwningPtr<BugType> BT;
32 enum VLASize_Kind { VLA_Garbage, VLA_Zero, VLA_Tainted };
34 void reportBug(VLASize_Kind Kind,
36 ProgramStateRef State,
37 CheckerContext &C) const;
39 void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
41 } // end anonymous namespace
43 void VLASizeChecker::reportBug(VLASize_Kind Kind,
45 ProgramStateRef State,
46 CheckerContext &C) const {
47 // Generate an error node.
48 ExplodedNode *N = C.generateSink(State);
53 BT.reset(new BuiltinBug("Dangerous variable-length array (VLA) declaration"));
56 llvm::raw_svector_ostream os(buf);
57 os << "Declared variable-length array (VLA) ";
60 os << "uses a garbage value as its size";
63 os << "has zero size";
66 os << "has tainted size";
70 BugReport *report = new BugReport(*BT, os.str(), N);
71 report->addRange(SizeE->getSourceRange());
72 bugreporter::trackNullOrUndefValue(N, SizeE, *report);
77 void VLASizeChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
78 if (!DS->isSingleDecl())
81 const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
85 ASTContext &Ctx = C.getASTContext();
86 const VariableArrayType *VLA = Ctx.getAsVariableArrayType(VD->getType());
90 // FIXME: Handle multi-dimensional VLAs.
91 const Expr *SE = VLA->getSizeExpr();
92 ProgramStateRef state = C.getState();
93 SVal sizeV = state->getSVal(SE, C.getLocationContext());
95 if (sizeV.isUndef()) {
96 reportBug(VLA_Garbage, SE, state, C);
100 // See if the size value is known. It can't be undefined because we would have
101 // warned about that already.
102 if (sizeV.isUnknown())
105 // Check if the size is tainted.
106 if (state->isTainted(sizeV)) {
107 reportBug(VLA_Tainted, SE, 0, C);
111 // Check if the size is zero.
112 DefinedSVal sizeD = cast<DefinedSVal>(sizeV);
114 ProgramStateRef stateNotZero, stateZero;
115 llvm::tie(stateNotZero, stateZero) = state->assume(sizeD);
117 if (stateZero && !stateNotZero) {
118 reportBug(VLA_Zero, SE, stateZero, C);
122 // From this point on, assume that the size is not zero.
123 state = stateNotZero;
125 // VLASizeChecker is responsible for defining the extent of the array being
126 // declared. We do this by multiplying the array length by the element size,
127 // then matching that with the array region's extent symbol.
129 // Convert the array length to size_t.
130 SValBuilder &svalBuilder = C.getSValBuilder();
131 QualType SizeTy = Ctx.getSizeType();
132 NonLoc ArrayLength = cast<NonLoc>(svalBuilder.evalCast(sizeD, SizeTy,
135 // Get the element size.
136 CharUnits EleSize = Ctx.getTypeSizeInChars(VLA->getElementType());
137 SVal EleSizeVal = svalBuilder.makeIntVal(EleSize.getQuantity(), SizeTy);
139 // Multiply the array length by the element size.
140 SVal ArraySizeVal = svalBuilder.evalBinOpNN(state, BO_Mul, ArrayLength,
141 cast<NonLoc>(EleSizeVal), SizeTy);
143 // Finally, assume that the array's extent matches the given size.
144 const LocationContext *LC = C.getLocationContext();
145 DefinedOrUnknownSVal Extent =
146 state->getRegion(VD, LC)->getExtent(svalBuilder);
147 DefinedOrUnknownSVal ArraySize = cast<DefinedOrUnknownSVal>(ArraySizeVal);
148 DefinedOrUnknownSVal sizeIsKnown =
149 svalBuilder.evalEQ(state, Extent, ArraySize);
150 state = state->assume(sizeIsKnown, true);
152 // Assume should not fail at this point.
155 // Remember our assumptions!
156 C.addTransition(state);
159 void ento::registerVLASizeChecker(CheckerManager &mgr) {
160 mgr.registerChecker<VLASizeChecker>();