Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 1 | //=== VLASizeChecker.cpp - Undefined dereference checker --------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Argyrios Kyrtzidis | 1696f50 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 10 | // This defines VLASizeChecker, a builtin check in ExprEngine that |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 11 | // performs checks for declaration of VLA of undefined or zero size. |
Jordy Rose | cf781e5 | 2010-07-06 23:33:54 +0000 | [diff] [blame] | 12 | // In addition, VLASizeChecker is responsible for defining the extent |
| 13 | // of the MemRegion that represents a VLA. |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Argyrios Kyrtzidis | 68ed625 | 2011-02-28 01:27:54 +0000 | [diff] [blame] | 17 | #include "ClangSACheckers.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/AST/CharUnits.h" |
| 19 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 68ed625 | 2011-02-28 01:27:54 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Benjamin Kramer | 3307c508 | 2012-02-04 12:31:12 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 28 | using namespace ento; |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 29 | |
Ted Kremenek | 795c611 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 30 | namespace { |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 31 | class VLASizeChecker : public Checker< check::PreStmt<DeclStmt> > { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 32 | mutable std::unique_ptr<BugType> BT; |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 33 | enum VLASize_Kind { VLA_Garbage, VLA_Zero, VLA_Tainted }; |
| 34 | |
| 35 | void reportBug(VLASize_Kind Kind, |
| 36 | const Expr *SizeE, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 37 | ProgramStateRef State, |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 38 | CheckerContext &C) const; |
Ted Kremenek | 795c611 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 39 | public: |
Argyrios Kyrtzidis | 68ed625 | 2011-02-28 01:27:54 +0000 | [diff] [blame] | 40 | void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const; |
Ted Kremenek | 795c611 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 41 | }; |
| 42 | } // end anonymous namespace |
| 43 | |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 44 | void VLASizeChecker::reportBug(VLASize_Kind Kind, |
| 45 | const Expr *SizeE, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 46 | ProgramStateRef State, |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 47 | CheckerContext &C) const { |
| 48 | // Generate an error node. |
| 49 | ExplodedNode *N = C.generateSink(State); |
| 50 | if (!N) |
| 51 | return; |
| 52 | |
| 53 | if (!BT) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 54 | BT.reset(new BuiltinBug( |
| 55 | this, "Dangerous variable-length array (VLA) declaration")); |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 56 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 57 | SmallString<256> buf; |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 58 | llvm::raw_svector_ostream os(buf); |
| 59 | os << "Declared variable-length array (VLA) "; |
| 60 | switch (Kind) { |
| 61 | case VLA_Garbage: |
| 62 | os << "uses a garbage value as its size"; |
| 63 | break; |
| 64 | case VLA_Zero: |
| 65 | os << "has zero size"; |
| 66 | break; |
| 67 | case VLA_Tainted: |
| 68 | os << "has tainted size"; |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | BugReport *report = new BugReport(*BT, os.str(), N); |
| 73 | report->addRange(SizeE->getSourceRange()); |
Jordan Rose | a0f7d35 | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 74 | bugreporter::trackNullOrUndefValue(N, SizeE, *report); |
Jordan Rose | e10d5a7 | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 75 | C.emitReport(report); |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | |
Argyrios Kyrtzidis | 68ed625 | 2011-02-28 01:27:54 +0000 | [diff] [blame] | 79 | void VLASizeChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const { |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 80 | if (!DS->isSingleDecl()) |
| 81 | return; |
| 82 | |
| 83 | const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl()); |
| 84 | if (!VD) |
| 85 | return; |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 86 | |
| 87 | ASTContext &Ctx = C.getASTContext(); |
| 88 | const VariableArrayType *VLA = Ctx.getAsVariableArrayType(VD->getType()); |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 89 | if (!VLA) |
| 90 | return; |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 92 | // FIXME: Handle multi-dimensional VLAs. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 93 | const Expr *SE = VLA->getSizeExpr(); |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 94 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 95 | SVal sizeV = state->getSVal(SE, C.getLocationContext()); |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 96 | |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 97 | if (sizeV.isUndef()) { |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 98 | reportBug(VLA_Garbage, SE, state, C); |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 99 | return; |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 100 | } |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 101 | |
| 102 | // See if the size value is known. It can't be undefined because we would have |
| 103 | // warned about that already. |
| 104 | if (sizeV.isUnknown()) |
| 105 | return; |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 106 | |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 107 | // Check if the size is tainted. |
| 108 | if (state->isTainted(sizeV)) { |
| 109 | reportBug(VLA_Tainted, SE, 0, C); |
| 110 | return; |
| 111 | } |
| 112 | |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 113 | // Check if the size is zero. |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 114 | DefinedSVal sizeD = sizeV.castAs<DefinedSVal>(); |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 115 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 116 | ProgramStateRef stateNotZero, stateZero; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 117 | std::tie(stateNotZero, stateZero) = state->assume(sizeD); |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 119 | if (stateZero && !stateNotZero) { |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 120 | reportBug(VLA_Zero, SE, stateZero, C); |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 121 | return; |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 122 | } |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 123 | |
| 124 | // From this point on, assume that the size is not zero. |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 125 | state = stateNotZero; |
| 126 | |
Jordy Rose | cf781e5 | 2010-07-06 23:33:54 +0000 | [diff] [blame] | 127 | // VLASizeChecker is responsible for defining the extent of the array being |
| 128 | // declared. We do this by multiplying the array length by the element size, |
| 129 | // then matching that with the array region's extent symbol. |
| 130 | |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 131 | // Convert the array length to size_t. |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 132 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 133 | QualType SizeTy = Ctx.getSizeType(); |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 134 | NonLoc ArrayLength = |
| 135 | svalBuilder.evalCast(sizeD, SizeTy, SE->getType()).castAs<NonLoc>(); |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 136 | |
| 137 | // Get the element size. |
| 138 | CharUnits EleSize = Ctx.getTypeSizeInChars(VLA->getElementType()); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 139 | SVal EleSizeVal = svalBuilder.makeIntVal(EleSize.getQuantity(), SizeTy); |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 140 | |
| 141 | // Multiply the array length by the element size. |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 142 | SVal ArraySizeVal = svalBuilder.evalBinOpNN( |
| 143 | state, BO_Mul, ArrayLength, EleSizeVal.castAs<NonLoc>(), SizeTy); |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | c5bea1e | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 145 | // Finally, assume that the array's extent matches the given size. |
Anna Zaks | c9abbe2 | 2011-10-26 21:06:44 +0000 | [diff] [blame] | 146 | const LocationContext *LC = C.getLocationContext(); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 147 | DefinedOrUnknownSVal Extent = |
| 148 | state->getRegion(VD, LC)->getExtent(svalBuilder); |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 149 | DefinedOrUnknownSVal ArraySize = ArraySizeVal.castAs<DefinedOrUnknownSVal>(); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 150 | DefinedOrUnknownSVal sizeIsKnown = |
| 151 | svalBuilder.evalEQ(state, Extent, ArraySize); |
| 152 | state = state->assume(sizeIsKnown, true); |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 153 | |
Zhongxing Xu | 5b488b1 | 2010-07-06 07:08:47 +0000 | [diff] [blame] | 154 | // Assume should not fail at this point. |
| 155 | assert(state); |
| 156 | |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 157 | // Remember our assumptions! |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 158 | C.addTransition(state); |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 159 | } |
Argyrios Kyrtzidis | 68ed625 | 2011-02-28 01:27:54 +0000 | [diff] [blame] | 160 | |
| 161 | void ento::registerVLASizeChecker(CheckerManager &mgr) { |
| 162 | mgr.registerChecker<VLASizeChecker>(); |
| 163 | } |