Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 1 | //=== VLASizeChecker.cpp - Undefined dereference checker --------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 9 | // This defines VLASizeChecker, a builtin check in ExprEngine that |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 10 | // performs checks for declaration of VLA of undefined or zero size. |
Jordy Rose | cf781e5 | 2010-07-06 23:33:54 +0000 | [diff] [blame] | 11 | // In addition, VLASizeChecker is responsible for defining the extent |
| 12 | // of the MemRegion that represents a VLA. |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Kristof Umann | 76a2150 | 2018-12-15 16:23:51 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/AST/CharUnits.h" |
| 18 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 68ed625 | 2011-02-28 01:27:54 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Benjamin Kramer | 3307c508 | 2012-02-04 12:31:12 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 27 | using namespace ento; |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | 795c611 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 29 | namespace { |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 30 | class VLASizeChecker : public Checker< check::PreStmt<DeclStmt> > { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 31 | mutable std::unique_ptr<BugType> BT; |
Jordan Rose | 1a9c0d1 | 2014-08-12 16:44:22 +0000 | [diff] [blame] | 32 | enum VLASize_Kind { VLA_Garbage, VLA_Zero, VLA_Tainted, VLA_Negative }; |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 33 | |
Henry Wong | e14e591 | 2018-05-02 12:11:22 +0000 | [diff] [blame] | 34 | void reportBug(VLASize_Kind Kind, const Expr *SizeE, ProgramStateRef State, |
| 35 | CheckerContext &C, |
| 36 | std::unique_ptr<BugReporterVisitor> Visitor = nullptr) const; |
| 37 | |
Ted Kremenek | 795c611 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 38 | public: |
Argyrios Kyrtzidis | 68ed625 | 2011-02-28 01:27:54 +0000 | [diff] [blame] | 39 | void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const; |
Ted Kremenek | 795c611 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 40 | }; |
| 41 | } // end anonymous namespace |
| 42 | |
Henry Wong | e14e591 | 2018-05-02 12:11:22 +0000 | [diff] [blame] | 43 | void VLASizeChecker::reportBug( |
| 44 | VLASize_Kind Kind, const Expr *SizeE, ProgramStateRef State, |
| 45 | CheckerContext &C, std::unique_ptr<BugReporterVisitor> Visitor) const { |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 46 | // Generate an error node. |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 47 | ExplodedNode *N = C.generateErrorNode(State); |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 48 | if (!N) |
| 49 | return; |
| 50 | |
| 51 | if (!BT) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 52 | BT.reset(new BuiltinBug( |
| 53 | this, "Dangerous variable-length array (VLA) declaration")); |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 54 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 55 | SmallString<256> buf; |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 56 | llvm::raw_svector_ostream os(buf); |
| 57 | os << "Declared variable-length array (VLA) "; |
| 58 | switch (Kind) { |
| 59 | case VLA_Garbage: |
| 60 | os << "uses a garbage value as its size"; |
| 61 | break; |
| 62 | case VLA_Zero: |
| 63 | os << "has zero size"; |
| 64 | break; |
| 65 | case VLA_Tainted: |
| 66 | os << "has tainted size"; |
| 67 | break; |
Jordan Rose | 1a9c0d1 | 2014-08-12 16:44:22 +0000 | [diff] [blame] | 68 | case VLA_Negative: |
| 69 | os << "has negative size"; |
| 70 | break; |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 73 | auto report = llvm::make_unique<BugReport>(*BT, os.str(), N); |
Henry Wong | e14e591 | 2018-05-02 12:11:22 +0000 | [diff] [blame] | 74 | report->addVisitor(std::move(Visitor)); |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 75 | report->addRange(SizeE->getSourceRange()); |
George Karpenkov | b2cf006 | 2018-10-23 18:24:53 +0000 | [diff] [blame] | 76 | bugreporter::trackExpressionValue(N, SizeE, *report); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 77 | C.emitReport(std::move(report)); |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Argyrios Kyrtzidis | 68ed625 | 2011-02-28 01:27:54 +0000 | [diff] [blame] | 80 | void VLASizeChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const { |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 81 | if (!DS->isSingleDecl()) |
| 82 | return; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 83 | |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 84 | const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl()); |
| 85 | if (!VD) |
| 86 | return; |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 87 | |
| 88 | ASTContext &Ctx = C.getASTContext(); |
| 89 | const VariableArrayType *VLA = Ctx.getAsVariableArrayType(VD->getType()); |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 90 | if (!VLA) |
| 91 | return; |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 93 | // FIXME: Handle multi-dimensional VLAs. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 94 | const Expr *SE = VLA->getSizeExpr(); |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 95 | ProgramStateRef state = C.getState(); |
George Karpenkov | d703ec9 | 2018-01-17 20:27:29 +0000 | [diff] [blame] | 96 | SVal sizeV = C.getSVal(SE); |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 98 | if (sizeV.isUndef()) { |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 99 | reportBug(VLA_Garbage, SE, state, C); |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 100 | return; |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 101 | } |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 102 | |
| 103 | // See if the size value is known. It can't be undefined because we would have |
| 104 | // warned about that already. |
| 105 | if (sizeV.isUnknown()) |
| 106 | return; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 107 | |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 108 | // Check if the size is tainted. |
| 109 | if (state->isTainted(sizeV)) { |
Henry Wong | e14e591 | 2018-05-02 12:11:22 +0000 | [diff] [blame] | 110 | reportBug(VLA_Tainted, SE, nullptr, C, |
| 111 | llvm::make_unique<TaintBugVisitor>(sizeV)); |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 115 | // Check if the size is zero. |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 116 | DefinedSVal sizeD = sizeV.castAs<DefinedSVal>(); |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 117 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 118 | ProgramStateRef stateNotZero, stateZero; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 119 | std::tie(stateNotZero, stateZero) = state->assume(sizeD); |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 120 | |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 121 | if (stateZero && !stateNotZero) { |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 122 | reportBug(VLA_Zero, SE, stateZero, C); |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 123 | return; |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 124 | } |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 126 | // From this point on, assume that the size is not zero. |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 127 | state = stateNotZero; |
| 128 | |
Jordy Rose | cf781e5 | 2010-07-06 23:33:54 +0000 | [diff] [blame] | 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. |
| 132 | |
Jordan Rose | 1a9c0d1 | 2014-08-12 16:44:22 +0000 | [diff] [blame] | 133 | // Check if the size is negative. |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 134 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordan Rose | 1a9c0d1 | 2014-08-12 16:44:22 +0000 | [diff] [blame] | 135 | |
| 136 | QualType Ty = SE->getType(); |
| 137 | DefinedOrUnknownSVal Zero = svalBuilder.makeZeroVal(Ty); |
| 138 | |
| 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; |
| 144 | |
| 145 | std::tie(StateNeg, StatePos) = CM.assumeDual(state, *LessThanZeroDVal); |
| 146 | if (StateNeg && !StatePos) { |
| 147 | reportBug(VLA_Negative, SE, state, C); |
| 148 | return; |
| 149 | } |
| 150 | state = StatePos; |
| 151 | } |
| 152 | |
| 153 | // Convert the array length to size_t. |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 154 | QualType SizeTy = Ctx.getSizeType(); |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 155 | NonLoc ArrayLength = |
| 156 | svalBuilder.evalCast(sizeD, SizeTy, SE->getType()).castAs<NonLoc>(); |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 157 | |
| 158 | // Get the element size. |
| 159 | CharUnits EleSize = Ctx.getTypeSizeInChars(VLA->getElementType()); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 160 | SVal EleSizeVal = svalBuilder.makeIntVal(EleSize.getQuantity(), SizeTy); |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 161 | |
| 162 | // Multiply the array length by the element size. |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 163 | SVal ArraySizeVal = svalBuilder.evalBinOpNN( |
| 164 | state, BO_Mul, ArrayLength, EleSizeVal.castAs<NonLoc>(), SizeTy); |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 165 | |
Ted Kremenek | c5bea1e | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 166 | // Finally, assume that the array's extent matches the given size. |
Anna Zaks | c9abbe2 | 2011-10-26 21:06:44 +0000 | [diff] [blame] | 167 | const LocationContext *LC = C.getLocationContext(); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 168 | DefinedOrUnknownSVal Extent = |
| 169 | state->getRegion(VD, LC)->getExtent(svalBuilder); |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 170 | DefinedOrUnknownSVal ArraySize = ArraySizeVal.castAs<DefinedOrUnknownSVal>(); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 171 | DefinedOrUnknownSVal sizeIsKnown = |
| 172 | svalBuilder.evalEQ(state, Extent, ArraySize); |
| 173 | state = state->assume(sizeIsKnown, true); |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 174 | |
Zhongxing Xu | 5b488b1 | 2010-07-06 07:08:47 +0000 | [diff] [blame] | 175 | // Assume should not fail at this point. |
| 176 | assert(state); |
| 177 | |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 178 | // Remember our assumptions! |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 179 | C.addTransition(state); |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 180 | } |
Argyrios Kyrtzidis | 68ed625 | 2011-02-28 01:27:54 +0000 | [diff] [blame] | 181 | |
| 182 | void ento::registerVLASizeChecker(CheckerManager &mgr) { |
| 183 | mgr.registerChecker<VLASizeChecker>(); |
| 184 | } |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 185 | |
| 186 | bool ento::shouldRegisterVLASizeChecker(const LangOptions &LO) { |
| 187 | return true; |
| 188 | } |