Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 1 | //== ArrayBoundCheckerV2.cpp ------------------------------------*- 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 | // |
| 10 | // This file defines ArrayBoundCheckerV2, which is a path-sensitive check |
| 11 | // which looks for an out-of-bound array element access. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Argyrios Kyrtzidis | 0a9ce3e | 2011-02-28 01:26:57 +0000 | [diff] [blame] | 15 | #include "ClangSACheckers.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/CharUnits.h" |
| 17 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 0a9ce3e | 2011-02-28 01:26:57 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Gabor Horvath | 855ad82 | 2016-08-22 10:07:32 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h" |
Argyrios Kyrtzidis | 0a9ce3e | 2011-02-28 01:26:57 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | f8cbac4 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +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" |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 27 | using namespace ento; |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 28 | |
| 29 | namespace { |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 30 | class ArrayBoundCheckerV2 : |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 31 | public Checker<check::Location> { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 32 | mutable std::unique_ptr<BuiltinBug> BT; |
| 33 | |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 34 | enum OOB_Kind { OOB_Precedes, OOB_Excedes, OOB_Tainted }; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 36 | void reportOOB(CheckerContext &C, ProgramStateRef errorState, |
Argyrios Kyrtzidis | 0a9ce3e | 2011-02-28 01:26:57 +0000 | [diff] [blame] | 37 | OOB_Kind kind) const; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 39 | public: |
Anna Zaks | 3e0f415 | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 40 | void checkLocation(SVal l, bool isLoad, const Stmt*S, |
| 41 | CheckerContext &C) const; |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | // FIXME: Eventually replace RegionRawOffset with this class. |
| 45 | class RegionRawOffsetV2 { |
| 46 | private: |
| 47 | const SubRegion *baseRegion; |
| 48 | SVal byteOffset; |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 49 | |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 50 | RegionRawOffsetV2() |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 51 | : baseRegion(nullptr), byteOffset(UnknownVal()) {} |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 52 | |
| 53 | public: |
| 54 | RegionRawOffsetV2(const SubRegion* base, SVal offset) |
| 55 | : baseRegion(base), byteOffset(offset) {} |
| 56 | |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 57 | NonLoc getByteOffset() const { return byteOffset.castAs<NonLoc>(); } |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 58 | const SubRegion *getRegion() const { return baseRegion; } |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 59 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 60 | static RegionRawOffsetV2 computeOffset(ProgramStateRef state, |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 61 | SValBuilder &svalBuilder, |
| 62 | SVal location); |
| 63 | |
| 64 | void dump() const; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 65 | void dumpToStream(raw_ostream &os) const; |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 66 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 67 | } |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 69 | static SVal computeExtentBegin(SValBuilder &svalBuilder, |
Ted Kremenek | 8a4c760 | 2011-04-12 17:21:33 +0000 | [diff] [blame] | 70 | const MemRegion *region) { |
Gabor Horvath | 7304027 | 2016-09-19 20:39:52 +0000 | [diff] [blame] | 71 | const MemSpaceRegion *SR = region->getMemorySpace(); |
| 72 | if (SR->getKind() == MemRegion::UnknownSpaceRegionKind) |
| 73 | return UnknownVal(); |
| 74 | else |
| 75 | return svalBuilder.makeZeroArrayIndex(); |
Ted Kremenek | 8a4c760 | 2011-04-12 17:21:33 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Gabor Horvath | 855ad82 | 2016-08-22 10:07:32 +0000 | [diff] [blame] | 78 | // TODO: once the constraint manager is smart enough to handle non simplified |
| 79 | // symbolic expressions remove this function. Note that this can not be used in |
| 80 | // the constraint manager as is, since this does not handle overflows. It is |
| 81 | // safe to assume, however, that memory offsets will not overflow. |
| 82 | static std::pair<NonLoc, nonloc::ConcreteInt> |
| 83 | getSimplifiedOffsets(NonLoc offset, nonloc::ConcreteInt extent, |
| 84 | SValBuilder &svalBuilder) { |
| 85 | Optional<nonloc::SymbolVal> SymVal = offset.getAs<nonloc::SymbolVal>(); |
| 86 | if (SymVal && SymVal->isExpression()) { |
| 87 | if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SymVal->getSymbol())) { |
| 88 | llvm::APSInt constant = |
| 89 | APSIntType(extent.getValue()).convert(SIE->getRHS()); |
| 90 | switch (SIE->getOpcode()) { |
| 91 | case BO_Mul: |
| 92 | // The constant should never be 0 here, since it the result of scaling |
| 93 | // based on the size of a type which is never 0. |
| 94 | if ((extent.getValue() % constant) != 0) |
| 95 | return std::pair<NonLoc, nonloc::ConcreteInt>(offset, extent); |
| 96 | else |
| 97 | return getSimplifiedOffsets( |
| 98 | nonloc::SymbolVal(SIE->getLHS()), |
| 99 | svalBuilder.makeIntVal(extent.getValue() / constant), |
| 100 | svalBuilder); |
| 101 | case BO_Add: |
| 102 | return getSimplifiedOffsets( |
| 103 | nonloc::SymbolVal(SIE->getLHS()), |
| 104 | svalBuilder.makeIntVal(extent.getValue() - constant), svalBuilder); |
| 105 | default: |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return std::pair<NonLoc, nonloc::ConcreteInt>(offset, extent); |
| 112 | } |
| 113 | |
Argyrios Kyrtzidis | 0a9ce3e | 2011-02-28 01:26:57 +0000 | [diff] [blame] | 114 | void ArrayBoundCheckerV2::checkLocation(SVal location, bool isLoad, |
Anna Zaks | 3e0f415 | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 115 | const Stmt* LoadS, |
Argyrios Kyrtzidis | 0a9ce3e | 2011-02-28 01:26:57 +0000 | [diff] [blame] | 116 | CheckerContext &checkerContext) const { |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 117 | |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 118 | // NOTE: Instead of using ProgramState::assumeInBound(), we are prototyping |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 119 | // some new logic here that reasons directly about memory region extents. |
| 120 | // Once that logic is more mature, we can bring it back to assumeInBound() |
| 121 | // for all clients to use. |
| 122 | // |
| 123 | // The algorithm we are using here for bounds checking is to see if the |
| 124 | // memory access is within the extent of the base region. Since we |
| 125 | // have some flexibility in defining the base region, we can achieve |
| 126 | // various levels of conservatism in our buffer overflow checking. |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 127 | ProgramStateRef state = checkerContext.getState(); |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 128 | ProgramStateRef originalState = state; |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 129 | |
| 130 | SValBuilder &svalBuilder = checkerContext.getSValBuilder(); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 131 | const RegionRawOffsetV2 &rawOffset = |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 132 | RegionRawOffsetV2::computeOffset(state, svalBuilder, location); |
| 133 | |
| 134 | if (!rawOffset.getRegion()) |
| 135 | return; |
| 136 | |
Gabor Horvath | 855ad82 | 2016-08-22 10:07:32 +0000 | [diff] [blame] | 137 | NonLoc rawOffsetVal = rawOffset.getByteOffset(); |
| 138 | |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 139 | // CHECK LOWER BOUND: Is byteOffset < extent begin? |
Ted Kremenek | 8a4c760 | 2011-04-12 17:21:33 +0000 | [diff] [blame] | 140 | // If so, we are doing a load/store |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 141 | // before the first valid offset in the memory region. |
| 142 | |
Ted Kremenek | 8a4c760 | 2011-04-12 17:21:33 +0000 | [diff] [blame] | 143 | SVal extentBegin = computeExtentBegin(svalBuilder, rawOffset.getRegion()); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 144 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 145 | if (Optional<NonLoc> NV = extentBegin.getAs<NonLoc>()) { |
Gabor Horvath | 855ad82 | 2016-08-22 10:07:32 +0000 | [diff] [blame] | 146 | if (NV->getAs<nonloc::ConcreteInt>()) { |
| 147 | std::pair<NonLoc, nonloc::ConcreteInt> simplifiedOffsets = |
| 148 | getSimplifiedOffsets(rawOffset.getByteOffset(), |
| 149 | NV->castAs<nonloc::ConcreteInt>(), |
| 150 | svalBuilder); |
| 151 | rawOffsetVal = simplifiedOffsets.first; |
| 152 | *NV = simplifiedOffsets.second; |
| 153 | } |
| 154 | |
| 155 | SVal lowerBound = svalBuilder.evalBinOpNN(state, BO_LT, rawOffsetVal, *NV, |
| 156 | svalBuilder.getConditionType()); |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 157 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 158 | Optional<NonLoc> lowerBoundToCheck = lowerBound.getAs<NonLoc>(); |
Ted Kremenek | 8a4c760 | 2011-04-12 17:21:33 +0000 | [diff] [blame] | 159 | if (!lowerBoundToCheck) |
| 160 | return; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 161 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 162 | ProgramStateRef state_precedesLowerBound, state_withinLowerBound; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 163 | std::tie(state_precedesLowerBound, state_withinLowerBound) = |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 164 | state->assume(*lowerBoundToCheck); |
| 165 | |
Ted Kremenek | 8a4c760 | 2011-04-12 17:21:33 +0000 | [diff] [blame] | 166 | // Are we constrained enough to definitely precede the lower bound? |
| 167 | if (state_precedesLowerBound && !state_withinLowerBound) { |
| 168 | reportOOB(checkerContext, state_precedesLowerBound, OOB_Precedes); |
| 169 | return; |
| 170 | } |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 171 | |
Ted Kremenek | 8a4c760 | 2011-04-12 17:21:33 +0000 | [diff] [blame] | 172 | // Otherwise, assume the constraint of the lower bound. |
| 173 | assert(state_withinLowerBound); |
| 174 | state = state_withinLowerBound; |
| 175 | } |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 176 | |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 177 | do { |
| 178 | // CHECK UPPER BOUND: Is byteOffset >= extent(baseRegion)? If so, |
| 179 | // we are doing a load/store after the last valid offset. |
| 180 | DefinedOrUnknownSVal extentVal = |
| 181 | rawOffset.getRegion()->getExtent(svalBuilder); |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 182 | if (!extentVal.getAs<NonLoc>()) |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 183 | break; |
| 184 | |
Gabor Horvath | 855ad82 | 2016-08-22 10:07:32 +0000 | [diff] [blame] | 185 | if (extentVal.getAs<nonloc::ConcreteInt>()) { |
| 186 | std::pair<NonLoc, nonloc::ConcreteInt> simplifiedOffsets = |
| 187 | getSimplifiedOffsets(rawOffset.getByteOffset(), |
| 188 | extentVal.castAs<nonloc::ConcreteInt>(), |
| 189 | svalBuilder); |
| 190 | rawOffsetVal = simplifiedOffsets.first; |
| 191 | extentVal = simplifiedOffsets.second; |
| 192 | } |
| 193 | |
| 194 | SVal upperbound = svalBuilder.evalBinOpNN(state, BO_GE, rawOffsetVal, |
| 195 | extentVal.castAs<NonLoc>(), |
| 196 | svalBuilder.getConditionType()); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 197 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 198 | Optional<NonLoc> upperboundToCheck = upperbound.getAs<NonLoc>(); |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 199 | if (!upperboundToCheck) |
| 200 | break; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 201 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 202 | ProgramStateRef state_exceedsUpperBound, state_withinUpperBound; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 203 | std::tie(state_exceedsUpperBound, state_withinUpperBound) = |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 204 | state->assume(*upperboundToCheck); |
Anna Zaks | 20829c9 | 2011-11-16 19:58:17 +0000 | [diff] [blame] | 205 | |
| 206 | // If we are under constrained and the index variables are tainted, report. |
| 207 | if (state_exceedsUpperBound && state_withinUpperBound) { |
Gabor Horvath | 855ad82 | 2016-08-22 10:07:32 +0000 | [diff] [blame] | 208 | if (state->isTainted(rawOffset.getByteOffset())) { |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 209 | reportOOB(checkerContext, state_exceedsUpperBound, OOB_Tainted); |
Anna Zaks | 20829c9 | 2011-11-16 19:58:17 +0000 | [diff] [blame] | 210 | return; |
Gabor Horvath | 855ad82 | 2016-08-22 10:07:32 +0000 | [diff] [blame] | 211 | } |
| 212 | } else if (state_exceedsUpperBound) { |
| 213 | // If we are constrained enough to definitely exceed the upper bound, |
| 214 | // report. |
Anna Zaks | 20829c9 | 2011-11-16 19:58:17 +0000 | [diff] [blame] | 215 | assert(!state_withinUpperBound); |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 216 | reportOOB(checkerContext, state_exceedsUpperBound, OOB_Excedes); |
| 217 | return; |
| 218 | } |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 219 | |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 220 | assert(state_withinUpperBound); |
| 221 | state = state_withinUpperBound; |
| 222 | } |
| 223 | while (false); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 224 | |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 225 | if (state != originalState) |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 226 | checkerContext.addTransition(state); |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void ArrayBoundCheckerV2::reportOOB(CheckerContext &checkerContext, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 230 | ProgramStateRef errorState, |
Argyrios Kyrtzidis | 0a9ce3e | 2011-02-28 01:26:57 +0000 | [diff] [blame] | 231 | OOB_Kind kind) const { |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 232 | |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 233 | ExplodedNode *errorNode = checkerContext.generateErrorNode(errorState); |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 234 | if (!errorNode) |
| 235 | return; |
| 236 | |
| 237 | if (!BT) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 238 | BT.reset(new BuiltinBug(this, "Out-of-bound access")); |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 239 | |
| 240 | // FIXME: This diagnostics are preliminary. We should get far better |
| 241 | // diagnostics for explaining buffer overruns. |
| 242 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 243 | SmallString<256> buf; |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 244 | llvm::raw_svector_ostream os(buf); |
Anna Zaks | b7eac9f | 2012-01-21 05:07:33 +0000 | [diff] [blame] | 245 | os << "Out of bound memory access "; |
| 246 | switch (kind) { |
| 247 | case OOB_Precedes: |
| 248 | os << "(accessed memory precedes memory block)"; |
| 249 | break; |
| 250 | case OOB_Excedes: |
| 251 | os << "(access exceeds upper limit of memory block)"; |
| 252 | break; |
| 253 | case OOB_Tainted: |
| 254 | os << "(index is tainted)"; |
| 255 | break; |
| 256 | } |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 257 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 258 | checkerContext.emitReport( |
| 259 | llvm::make_unique<BugReport>(*BT, os.str(), errorNode)); |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Eric Christopher | f18016c | 2017-11-16 03:18:09 +0000 | [diff] [blame] | 262 | #ifndef NDEBUG |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 263 | LLVM_DUMP_METHOD void RegionRawOffsetV2::dump() const { |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 264 | dumpToStream(llvm::errs()); |
| 265 | } |
| 266 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 267 | void RegionRawOffsetV2::dumpToStream(raw_ostream &os) const { |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 268 | os << "raw_offset_v2{" << getRegion() << ',' << getByteOffset() << '}'; |
| 269 | } |
Eric Christopher | f18016c | 2017-11-16 03:18:09 +0000 | [diff] [blame] | 270 | #endif |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 271 | |
| 272 | // Lazily computes a value to be used by 'computeOffset'. If 'val' |
| 273 | // is unknown or undefined, we lazily substitute '0'. Otherwise, |
| 274 | // return 'val'. |
| 275 | static inline SVal getValue(SVal val, SValBuilder &svalBuilder) { |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 276 | return val.getAs<UndefinedVal>() ? svalBuilder.makeArrayIndex(0) : val; |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | // Scale a base value by a scaling factor, and return the scaled |
| 280 | // value as an SVal. Used by 'computeOffset'. |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 281 | static inline SVal scaleValue(ProgramStateRef state, |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 282 | NonLoc baseVal, CharUnits scaling, |
| 283 | SValBuilder &sb) { |
| 284 | return sb.evalBinOpNN(state, BO_Mul, baseVal, |
| 285 | sb.makeArrayIndex(scaling.getQuantity()), |
| 286 | sb.getArrayIndexType()); |
| 287 | } |
| 288 | |
| 289 | // Add an SVal to another, treating unknown and undefined values as |
| 290 | // summing to UnknownVal. Used by 'computeOffset'. |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 291 | static SVal addValue(ProgramStateRef state, SVal x, SVal y, |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 292 | SValBuilder &svalBuilder) { |
| 293 | // We treat UnknownVals and UndefinedVals the same here because we |
| 294 | // only care about computing offsets. |
| 295 | if (x.isUnknownOrUndef() || y.isUnknownOrUndef()) |
| 296 | return UnknownVal(); |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 297 | |
| 298 | return svalBuilder.evalBinOpNN(state, BO_Add, x.castAs<NonLoc>(), |
| 299 | y.castAs<NonLoc>(), |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 300 | svalBuilder.getArrayIndexType()); |
| 301 | } |
| 302 | |
| 303 | /// Compute a raw byte offset from a base region. Used for array bounds |
| 304 | /// checking. |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 305 | RegionRawOffsetV2 RegionRawOffsetV2::computeOffset(ProgramStateRef state, |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 306 | SValBuilder &svalBuilder, |
| 307 | SVal location) |
| 308 | { |
| 309 | const MemRegion *region = location.getAsRegion(); |
| 310 | SVal offset = UndefinedVal(); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 311 | |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 312 | while (region) { |
| 313 | switch (region->getKind()) { |
| 314 | default: { |
Ted Kremenek | 8a4c760 | 2011-04-12 17:21:33 +0000 | [diff] [blame] | 315 | if (const SubRegion *subReg = dyn_cast<SubRegion>(region)) { |
| 316 | offset = getValue(offset, svalBuilder); |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 317 | if (!offset.isUnknownOrUndef()) |
| 318 | return RegionRawOffsetV2(subReg, offset); |
Ted Kremenek | 8a4c760 | 2011-04-12 17:21:33 +0000 | [diff] [blame] | 319 | } |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 320 | return RegionRawOffsetV2(); |
| 321 | } |
| 322 | case MemRegion::ElementRegionKind: { |
| 323 | const ElementRegion *elemReg = cast<ElementRegion>(region); |
| 324 | SVal index = elemReg->getIndex(); |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 325 | if (!index.getAs<NonLoc>()) |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 326 | return RegionRawOffsetV2(); |
| 327 | QualType elemType = elemReg->getElementType(); |
| 328 | // If the element is an incomplete type, go no further. |
| 329 | ASTContext &astContext = svalBuilder.getContext(); |
Anna Zaks | 0820e13 | 2014-10-03 21:49:03 +0000 | [diff] [blame] | 330 | if (elemType->isIncompleteType()) |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 331 | return RegionRawOffsetV2(); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 332 | |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 333 | // Update the offset. |
| 334 | offset = addValue(state, |
| 335 | getValue(offset, svalBuilder), |
| 336 | scaleValue(state, |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 337 | index.castAs<NonLoc>(), |
Anna Zaks | 20829c9 | 2011-11-16 19:58:17 +0000 | [diff] [blame] | 338 | astContext.getTypeSizeInChars(elemType), |
| 339 | svalBuilder), |
Ted Kremenek | e73571b | 2010-12-23 02:42:43 +0000 | [diff] [blame] | 340 | svalBuilder); |
| 341 | |
| 342 | if (offset.isUnknownOrUndef()) |
| 343 | return RegionRawOffsetV2(); |
| 344 | |
| 345 | region = elemReg->getSuperRegion(); |
| 346 | continue; |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | return RegionRawOffsetV2(); |
| 351 | } |
| 352 | |
Argyrios Kyrtzidis | 0a9ce3e | 2011-02-28 01:26:57 +0000 | [diff] [blame] | 353 | void ento::registerArrayBoundCheckerV2(CheckerManager &mgr) { |
| 354 | mgr.registerChecker<ArrayBoundCheckerV2>(); |
| 355 | } |