| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 1 | //===-- IteratorChecker.cpp ---------------------------------------*- 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 |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Defines a checker for using iterators outside their range (past end). Usage |
| 10 | // means here dereferencing, incrementing etc. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | // |
| 14 | // In the code, iterator can be represented as a: |
| 15 | // * type-I: typedef-ed pointer. Operations over such iterator, such as |
| 16 | // comparisons or increments, are modeled straightforwardly by the |
| 17 | // analyzer. |
| 18 | // * type-II: structure with its method bodies available. Operations over such |
| 19 | // iterator are inlined by the analyzer, and results of modeling |
| 20 | // these operations are exposing implementation details of the |
| 21 | // iterators, which is not necessarily helping. |
| 22 | // * type-III: completely opaque structure. Operations over such iterator are |
| 23 | // modeled conservatively, producing conjured symbols everywhere. |
| 24 | // |
| 25 | // To handle all these types in a common way we introduce a structure called |
| 26 | // IteratorPosition which is an abstraction of the position the iterator |
| 27 | // represents using symbolic expressions. The checker handles all the |
| 28 | // operations on this structure. |
| 29 | // |
| 30 | // Additionally, depending on the circumstances, operators of types II and III |
| 31 | // can be represented as: |
| 32 | // * type-IIa, type-IIIa: conjured structure symbols - when returned by value |
| 33 | // from conservatively evaluated methods such as |
| 34 | // `.begin()`. |
| 35 | // * type-IIb, type-IIIb: memory regions of iterator-typed objects, such as |
| 36 | // variables or temporaries, when the iterator object is |
| 37 | // currently treated as an lvalue. |
| 38 | // * type-IIc, type-IIIc: compound values of iterator-typed objects, when the |
| 39 | // iterator object is treated as an rvalue taken of a |
| 40 | // particular lvalue, eg. a copy of "type-a" iterator |
| 41 | // object, or an iterator that existed before the |
| 42 | // analysis has started. |
| 43 | // |
| 44 | // To handle any of these three different representations stored in an SVal we |
| 45 | // use setter and getters functions which separate the three cases. To store |
| 46 | // them we use a pointer union of symbol and memory region. |
| 47 | // |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 48 | // The checker works the following way: We record the begin and the |
| 49 | // past-end iterator for all containers whenever their `.begin()` and `.end()` |
| 50 | // are called. Since the Constraint Manager cannot handle such SVals we need |
| 51 | // to take over its role. We post-check equality and non-equality comparisons |
| 52 | // and record that the two sides are equal if we are in the 'equal' branch |
| 53 | // (true-branch for `==` and false-branch for `!=`). |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 54 | // |
| 55 | // In case of type-I or type-II iterators we get a concrete integer as a result |
| 56 | // of the comparison (1 or 0) but in case of type-III we only get a Symbol. In |
| 57 | // this latter case we record the symbol and reload it in evalAssume() and do |
| 58 | // the propagation there. We also handle (maybe double) negated comparisons |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 59 | // which are represented in the form of (x == 0 or x != 0) where x is the |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 60 | // comparison itself. |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 61 | // |
| 62 | // Since `SimpleConstraintManager` cannot handle complex symbolic expressions |
| 63 | // we only use expressions of the format S, S+n or S-n for iterator positions |
| 64 | // where S is a conjured symbol and n is an unsigned concrete integer. When |
| 65 | // making an assumption e.g. `S1 + n == S2 + m` we store `S1 - S2 == m - n` as |
| 66 | // a constraint which we later retrieve when doing an actual comparison. |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 67 | |
| Kristof Umann | 76a2150 | 2018-12-15 16:23:51 +0000 | [diff] [blame] | 68 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 69 | #include "clang/AST/DeclTemplate.h" |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 70 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 71 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 72 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
| 73 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| Csaba Dabis | e4bf456 | 2019-08-22 00:36:42 +0000 | [diff] [blame] | 74 | #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicType.h" |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 75 | |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 76 | #include <utility> |
| 77 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 78 | using namespace clang; |
| 79 | using namespace ento; |
| 80 | |
| 81 | namespace { |
| 82 | |
| 83 | // Abstract position of an iterator. This helps to handle all three kinds |
| 84 | // of operators in a common way by using a symbolic position. |
| 85 | struct IteratorPosition { |
| 86 | private: |
| 87 | |
| 88 | // Container the iterator belongs to |
| 89 | const MemRegion *Cont; |
| 90 | |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 91 | // Whether iterator is valid |
| 92 | const bool Valid; |
| 93 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 94 | // Abstract offset |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 95 | const SymbolRef Offset; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 96 | |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 97 | IteratorPosition(const MemRegion *C, bool V, SymbolRef Of) |
| 98 | : Cont(C), Valid(V), Offset(Of) {} |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 99 | |
| 100 | public: |
| 101 | const MemRegion *getContainer() const { return Cont; } |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 102 | bool isValid() const { return Valid; } |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 103 | SymbolRef getOffset() const { return Offset; } |
| 104 | |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 105 | IteratorPosition invalidate() const { |
| 106 | return IteratorPosition(Cont, false, Offset); |
| 107 | } |
| 108 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 109 | static IteratorPosition getPosition(const MemRegion *C, SymbolRef Of) { |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 110 | return IteratorPosition(C, true, Of); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | IteratorPosition setTo(SymbolRef NewOf) const { |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 114 | return IteratorPosition(Cont, Valid, NewOf); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| Adam Balogh | 6b23b1a | 2018-09-10 09:04:27 +0000 | [diff] [blame] | 117 | IteratorPosition reAssign(const MemRegion *NewCont) const { |
| 118 | return IteratorPosition(NewCont, Valid, Offset); |
| 119 | } |
| 120 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 121 | bool operator==(const IteratorPosition &X) const { |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 122 | return Cont == X.Cont && Valid == X.Valid && Offset == X.Offset; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | bool operator!=(const IteratorPosition &X) const { |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 126 | return Cont != X.Cont || Valid != X.Valid || Offset != X.Offset; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 130 | ID.AddPointer(Cont); |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 131 | ID.AddInteger(Valid); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 132 | ID.Add(Offset); |
| 133 | } |
| 134 | }; |
| 135 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 136 | // Structure to record the symbolic begin and end position of a container |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 137 | struct ContainerData { |
| 138 | private: |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 139 | const SymbolRef Begin, End; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 140 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 141 | ContainerData(SymbolRef B, SymbolRef E) : Begin(B), End(E) {} |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 142 | |
| 143 | public: |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 144 | static ContainerData fromBegin(SymbolRef B) { |
| 145 | return ContainerData(B, nullptr); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 148 | static ContainerData fromEnd(SymbolRef E) { |
| 149 | return ContainerData(nullptr, E); |
| 150 | } |
| 151 | |
| 152 | SymbolRef getBegin() const { return Begin; } |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 153 | SymbolRef getEnd() const { return End; } |
| 154 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 155 | ContainerData newBegin(SymbolRef B) const { return ContainerData(B, End); } |
| 156 | |
| 157 | ContainerData newEnd(SymbolRef E) const { return ContainerData(Begin, E); } |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 158 | |
| 159 | bool operator==(const ContainerData &X) const { |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 160 | return Begin == X.Begin && End == X.End; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | bool operator!=(const ContainerData &X) const { |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 164 | return Begin != X.Begin || End != X.End; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 168 | ID.Add(Begin); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 169 | ID.Add(End); |
| 170 | } |
| 171 | }; |
| 172 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 173 | class IteratorChecker |
| 174 | : public Checker<check::PreCall, check::PostCall, |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 175 | check::PostStmt<MaterializeTemporaryExpr>, check::Bind, |
| Adam Balogh | 0f88cae | 2019-11-08 08:56:31 +0100 | [diff] [blame] | 176 | check::LiveSymbols, check::DeadSymbols, eval::Call> { |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 177 | |
| 178 | std::unique_ptr<BugType> OutOfRangeBugType; |
| Adam Balogh | 21583b7 | 2018-09-10 09:03:22 +0000 | [diff] [blame] | 179 | std::unique_ptr<BugType> MismatchedBugType; |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 180 | std::unique_ptr<BugType> InvalidatedBugType; |
| Adam Balogh | 0f88cae | 2019-11-08 08:56:31 +0100 | [diff] [blame] | 181 | std::unique_ptr<BugType> DebugMsgBugType; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 182 | |
| Adam Balogh | 54976e7 | 2019-04-23 07:15:55 +0000 | [diff] [blame] | 183 | void handleComparison(CheckerContext &C, const Expr *CE, const SVal &RetVal, |
| 184 | const SVal &LVal, const SVal &RVal, |
| 185 | OverloadedOperatorKind Op) const; |
| 186 | void processComparison(CheckerContext &C, ProgramStateRef State, |
| 187 | SymbolRef Sym1, SymbolRef Sym2, const SVal &RetVal, |
| 188 | OverloadedOperatorKind Op) const; |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 189 | void verifyAccess(CheckerContext &C, const SVal &Val) const; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 190 | void verifyDereference(CheckerContext &C, const SVal &Val) const; |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 191 | void handleIncrement(CheckerContext &C, const SVal &RetVal, const SVal &Iter, |
| 192 | bool Postfix) const; |
| 193 | void handleDecrement(CheckerContext &C, const SVal &RetVal, const SVal &Iter, |
| 194 | bool Postfix) const; |
| 195 | void handleRandomIncrOrDecr(CheckerContext &C, OverloadedOperatorKind Op, |
| 196 | const SVal &RetVal, const SVal &LHS, |
| 197 | const SVal &RHS) const; |
| 198 | void handleBegin(CheckerContext &C, const Expr *CE, const SVal &RetVal, |
| 199 | const SVal &Cont) const; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 200 | void handleEnd(CheckerContext &C, const Expr *CE, const SVal &RetVal, |
| 201 | const SVal &Cont) const; |
| 202 | void assignToContainer(CheckerContext &C, const Expr *CE, const SVal &RetVal, |
| 203 | const MemRegion *Cont) const; |
| Adam Balogh | 6b23b1a | 2018-09-10 09:04:27 +0000 | [diff] [blame] | 204 | void handleAssign(CheckerContext &C, const SVal &Cont, |
| 205 | const Expr *CE = nullptr, |
| 206 | const SVal &OldCont = UndefinedVal()) const; |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 207 | void handleClear(CheckerContext &C, const SVal &Cont) const; |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 208 | void handlePushBack(CheckerContext &C, const SVal &Cont) const; |
| 209 | void handlePopBack(CheckerContext &C, const SVal &Cont) const; |
| 210 | void handlePushFront(CheckerContext &C, const SVal &Cont) const; |
| 211 | void handlePopFront(CheckerContext &C, const SVal &Cont) const; |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 212 | void handleInsert(CheckerContext &C, const SVal &Iter) const; |
| 213 | void handleErase(CheckerContext &C, const SVal &Iter) const; |
| 214 | void handleErase(CheckerContext &C, const SVal &Iter1, |
| 215 | const SVal &Iter2) const; |
| 216 | void handleEraseAfter(CheckerContext &C, const SVal &Iter) const; |
| 217 | void handleEraseAfter(CheckerContext &C, const SVal &Iter1, |
| 218 | const SVal &Iter2) const; |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 219 | void verifyIncrement(CheckerContext &C, const SVal &Iter) const; |
| 220 | void verifyDecrement(CheckerContext &C, const SVal &Iter) const; |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 221 | void verifyRandomIncrOrDecr(CheckerContext &C, OverloadedOperatorKind Op, |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 222 | const SVal &LHS, const SVal &RHS) const; |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 223 | void verifyMatch(CheckerContext &C, const SVal &Iter, |
| 224 | const MemRegion *Cont) const; |
| Adam Balogh | 21583b7 | 2018-09-10 09:03:22 +0000 | [diff] [blame] | 225 | void verifyMatch(CheckerContext &C, const SVal &Iter1, |
| 226 | const SVal &Iter2) const; |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 227 | IteratorPosition advancePosition(CheckerContext &C, OverloadedOperatorKind Op, |
| 228 | const IteratorPosition &Pos, |
| 229 | const SVal &Distance) const; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 230 | void reportOutOfRangeBug(const StringRef &Message, const SVal &Val, |
| 231 | CheckerContext &C, ExplodedNode *ErrNode) const; |
| Adam Balogh | 21583b7 | 2018-09-10 09:03:22 +0000 | [diff] [blame] | 232 | void reportMismatchedBug(const StringRef &Message, const SVal &Val1, |
| 233 | const SVal &Val2, CheckerContext &C, |
| 234 | ExplodedNode *ErrNode) const; |
| Adam Balogh | 3659f7a | 2018-09-10 09:05:31 +0000 | [diff] [blame] | 235 | void reportMismatchedBug(const StringRef &Message, const SVal &Val, |
| 236 | const MemRegion *Reg, CheckerContext &C, |
| 237 | ExplodedNode *ErrNode) const; |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 238 | void reportInvalidatedBug(const StringRef &Message, const SVal &Val, |
| 239 | CheckerContext &C, ExplodedNode *ErrNode) const; |
| Adam Balogh | 0f88cae | 2019-11-08 08:56:31 +0100 | [diff] [blame] | 240 | template <typename Getter> |
| 241 | void analyzerContainerDataField(const CallExpr *CE, CheckerContext &C, |
| 242 | Getter get) const; |
| 243 | void analyzerContainerBegin(const CallExpr *CE, CheckerContext &C) const; |
| 244 | void analyzerContainerEnd(const CallExpr *CE, CheckerContext &C) const; |
| 245 | template <typename Getter> |
| 246 | void analyzerIteratorDataField(const CallExpr *CE, CheckerContext &C, |
| 247 | Getter get, SVal Default) const; |
| 248 | void analyzerIteratorPosition(const CallExpr *CE, CheckerContext &C) const; |
| 249 | void analyzerIteratorContainer(const CallExpr *CE, CheckerContext &C) const; |
| 250 | void analyzerIteratorValidity(const CallExpr *CE, CheckerContext &C) const; |
| 251 | ExplodedNode *reportDebugMsg(llvm::StringRef Msg, CheckerContext &C) const; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 252 | |
| Adam Balogh | 0f88cae | 2019-11-08 08:56:31 +0100 | [diff] [blame] | 253 | typedef void (IteratorChecker::*FnCheck)(const CallExpr *, |
| 254 | CheckerContext &) const; |
| 255 | |
| 256 | CallDescriptionMap<FnCheck> Callbacks = { |
| 257 | {{0, "clang_analyzer_container_begin", 1}, |
| 258 | &IteratorChecker::analyzerContainerBegin}, |
| 259 | {{0, "clang_analyzer_container_end", 1}, |
| 260 | &IteratorChecker::analyzerContainerEnd}, |
| 261 | {{0, "clang_analyzer_iterator_position", 1}, |
| 262 | &IteratorChecker::analyzerIteratorPosition}, |
| 263 | {{0, "clang_analyzer_iterator_container", 1}, |
| 264 | &IteratorChecker::analyzerIteratorContainer}, |
| 265 | {{0, "clang_analyzer_iterator_validity", 1}, |
| 266 | &IteratorChecker::analyzerIteratorValidity}, |
| 267 | }; |
| 268 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 269 | public: |
| 270 | IteratorChecker(); |
| 271 | |
| 272 | enum CheckKind { |
| 273 | CK_IteratorRangeChecker, |
| Adam Balogh | 21583b7 | 2018-09-10 09:03:22 +0000 | [diff] [blame] | 274 | CK_MismatchedIteratorChecker, |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 275 | CK_InvalidatedIteratorChecker, |
| Adam Balogh | 0f88cae | 2019-11-08 08:56:31 +0100 | [diff] [blame] | 276 | CK_DebugIteratorModeling, |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 277 | CK_NumCheckKinds |
| 278 | }; |
| 279 | |
| 280 | DefaultBool ChecksEnabled[CK_NumCheckKinds]; |
| Kristof Umann | 7264942 | 2019-09-12 19:09:24 +0000 | [diff] [blame] | 281 | CheckerNameRef CheckNames[CK_NumCheckKinds]; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 282 | |
| 283 | void checkPreCall(const CallEvent &Call, CheckerContext &C) const; |
| 284 | void checkPostCall(const CallEvent &Call, CheckerContext &C) const; |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 285 | void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &C) const; |
| 286 | void checkPostStmt(const CXXConstructExpr *CCE, CheckerContext &C) const; |
| 287 | void checkPostStmt(const DeclStmt *DS, CheckerContext &C) const; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 288 | void checkPostStmt(const MaterializeTemporaryExpr *MTE, |
| 289 | CheckerContext &C) const; |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 290 | void checkLiveSymbols(ProgramStateRef State, SymbolReaper &SR) const; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 291 | void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const; |
| Adam Balogh | 0f88cae | 2019-11-08 08:56:31 +0100 | [diff] [blame] | 292 | bool evalCall(const CallEvent &Call, CheckerContext &C) const; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 293 | }; |
| 294 | } // namespace |
| 295 | |
| 296 | REGISTER_MAP_WITH_PROGRAMSTATE(IteratorSymbolMap, SymbolRef, IteratorPosition) |
| 297 | REGISTER_MAP_WITH_PROGRAMSTATE(IteratorRegionMap, const MemRegion *, |
| 298 | IteratorPosition) |
| 299 | |
| 300 | REGISTER_MAP_WITH_PROGRAMSTATE(ContainerMap, const MemRegion *, ContainerData) |
| 301 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 302 | namespace { |
| 303 | |
| 304 | bool isIteratorType(const QualType &Type); |
| 305 | bool isIterator(const CXXRecordDecl *CRD); |
| Adam Balogh | 3659f7a | 2018-09-10 09:05:31 +0000 | [diff] [blame] | 306 | bool isComparisonOperator(OverloadedOperatorKind OK); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 307 | bool isBeginCall(const FunctionDecl *Func); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 308 | bool isEndCall(const FunctionDecl *Func); |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 309 | bool isAssignCall(const FunctionDecl *Func); |
| 310 | bool isClearCall(const FunctionDecl *Func); |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 311 | bool isPushBackCall(const FunctionDecl *Func); |
| 312 | bool isEmplaceBackCall(const FunctionDecl *Func); |
| 313 | bool isPopBackCall(const FunctionDecl *Func); |
| 314 | bool isPushFrontCall(const FunctionDecl *Func); |
| 315 | bool isEmplaceFrontCall(const FunctionDecl *Func); |
| 316 | bool isPopFrontCall(const FunctionDecl *Func); |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 317 | bool isInsertCall(const FunctionDecl *Func); |
| 318 | bool isEraseCall(const FunctionDecl *Func); |
| 319 | bool isEraseAfterCall(const FunctionDecl *Func); |
| 320 | bool isEmplaceCall(const FunctionDecl *Func); |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 321 | bool isAssignmentOperator(OverloadedOperatorKind OK); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 322 | bool isSimpleComparisonOperator(OverloadedOperatorKind OK); |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 323 | bool isAccessOperator(OverloadedOperatorKind OK); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 324 | bool isDereferenceOperator(OverloadedOperatorKind OK); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 325 | bool isIncrementOperator(OverloadedOperatorKind OK); |
| 326 | bool isDecrementOperator(OverloadedOperatorKind OK); |
| 327 | bool isRandomIncrOrDecrOperator(OverloadedOperatorKind OK); |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 328 | bool hasSubscriptOperator(ProgramStateRef State, const MemRegion *Reg); |
| 329 | bool frontModifiable(ProgramStateRef State, const MemRegion *Reg); |
| 330 | bool backModifiable(ProgramStateRef State, const MemRegion *Reg); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 331 | SymbolRef getContainerBegin(ProgramStateRef State, const MemRegion *Cont); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 332 | SymbolRef getContainerEnd(ProgramStateRef State, const MemRegion *Cont); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 333 | ProgramStateRef createContainerBegin(ProgramStateRef State, |
| Adam Balogh | 33160c4 | 2019-05-20 11:04:27 +0000 | [diff] [blame] | 334 | const MemRegion *Cont, const Expr *E, |
| 335 | QualType T, const LocationContext *LCtx, |
| 336 | unsigned BlockCount); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 337 | ProgramStateRef createContainerEnd(ProgramStateRef State, const MemRegion *Cont, |
| Adam Balogh | 33160c4 | 2019-05-20 11:04:27 +0000 | [diff] [blame] | 338 | const Expr *E, QualType T, |
| 339 | const LocationContext *LCtx, |
| 340 | unsigned BlockCount); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 341 | const IteratorPosition *getIteratorPosition(ProgramStateRef State, |
| 342 | const SVal &Val); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 343 | ProgramStateRef setIteratorPosition(ProgramStateRef State, const SVal &Val, |
| 344 | const IteratorPosition &Pos); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 345 | ProgramStateRef removeIteratorPosition(ProgramStateRef State, const SVal &Val); |
| Adam Balogh | 54976e7 | 2019-04-23 07:15:55 +0000 | [diff] [blame] | 346 | ProgramStateRef assumeNoOverflow(ProgramStateRef State, SymbolRef Sym, |
| 347 | long Scale); |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 348 | ProgramStateRef invalidateAllIteratorPositions(ProgramStateRef State, |
| 349 | const MemRegion *Cont); |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 350 | ProgramStateRef |
| 351 | invalidateAllIteratorPositionsExcept(ProgramStateRef State, |
| 352 | const MemRegion *Cont, SymbolRef Offset, |
| 353 | BinaryOperator::Opcode Opc); |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 354 | ProgramStateRef invalidateIteratorPositions(ProgramStateRef State, |
| 355 | SymbolRef Offset, |
| 356 | BinaryOperator::Opcode Opc); |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 357 | ProgramStateRef invalidateIteratorPositions(ProgramStateRef State, |
| 358 | SymbolRef Offset1, |
| 359 | BinaryOperator::Opcode Opc1, |
| 360 | SymbolRef Offset2, |
| 361 | BinaryOperator::Opcode Opc2); |
| Adam Balogh | 6b23b1a | 2018-09-10 09:04:27 +0000 | [diff] [blame] | 362 | ProgramStateRef reassignAllIteratorPositions(ProgramStateRef State, |
| 363 | const MemRegion *Cont, |
| 364 | const MemRegion *NewCont); |
| 365 | ProgramStateRef reassignAllIteratorPositionsUnless(ProgramStateRef State, |
| 366 | const MemRegion *Cont, |
| 367 | const MemRegion *NewCont, |
| 368 | SymbolRef Offset, |
| 369 | BinaryOperator::Opcode Opc); |
| 370 | ProgramStateRef rebaseSymbolInIteratorPositionsIf( |
| 371 | ProgramStateRef State, SValBuilder &SVB, SymbolRef OldSym, |
| 372 | SymbolRef NewSym, SymbolRef CondSym, BinaryOperator::Opcode Opc); |
| Adam Balogh | 54976e7 | 2019-04-23 07:15:55 +0000 | [diff] [blame] | 373 | ProgramStateRef relateSymbols(ProgramStateRef State, SymbolRef Sym1, |
| 374 | SymbolRef Sym2, bool Equal); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 375 | const ContainerData *getContainerData(ProgramStateRef State, |
| 376 | const MemRegion *Cont); |
| 377 | ProgramStateRef setContainerData(ProgramStateRef State, const MemRegion *Cont, |
| 378 | const ContainerData &CData); |
| Adam Balogh | a692120 | 2018-07-30 08:52:21 +0000 | [diff] [blame] | 379 | bool hasLiveIterators(ProgramStateRef State, const MemRegion *Cont); |
| Adam Balogh | 21583b7 | 2018-09-10 09:03:22 +0000 | [diff] [blame] | 380 | bool isBoundThroughLazyCompoundVal(const Environment &Env, |
| 381 | const MemRegion *Reg); |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 382 | bool isPastTheEnd(ProgramStateRef State, const IteratorPosition &Pos); |
| 383 | bool isAheadOfRange(ProgramStateRef State, const IteratorPosition &Pos); |
| 384 | bool isBehindPastTheEnd(ProgramStateRef State, const IteratorPosition &Pos); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 385 | bool isZero(ProgramStateRef State, const NonLoc &Val); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 386 | } // namespace |
| 387 | |
| 388 | IteratorChecker::IteratorChecker() { |
| 389 | OutOfRangeBugType.reset( |
| Adam Balogh | 12f5c7f | 2019-08-29 09:35:47 +0000 | [diff] [blame] | 390 | new BugType(this, "Iterator out of range", "Misuse of STL APIs")); |
| Adam Balogh | 21583b7 | 2018-09-10 09:03:22 +0000 | [diff] [blame] | 391 | MismatchedBugType.reset( |
| George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 392 | new BugType(this, "Iterator(s) mismatched", "Misuse of STL APIs", |
| 393 | /*SuppressOnSink=*/true)); |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 394 | InvalidatedBugType.reset( |
| Adam Balogh | 12f5c7f | 2019-08-29 09:35:47 +0000 | [diff] [blame] | 395 | new BugType(this, "Iterator invalidated", "Misuse of STL APIs")); |
| Adam Balogh | 0f88cae | 2019-11-08 08:56:31 +0100 | [diff] [blame] | 396 | DebugMsgBugType.reset( |
| 397 | new BugType(this, "Checking analyzer assumptions", "debug", |
| 398 | /*SuppressOnSink=*/true)); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | void IteratorChecker::checkPreCall(const CallEvent &Call, |
| 402 | CheckerContext &C) const { |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 403 | // Check for out of range access or access of invalidated position and |
| 404 | // iterator mismatches |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 405 | const auto *Func = dyn_cast_or_null<FunctionDecl>(Call.getDecl()); |
| 406 | if (!Func) |
| 407 | return; |
| 408 | |
| 409 | if (Func->isOverloadedOperator()) { |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 410 | if (ChecksEnabled[CK_InvalidatedIteratorChecker] && |
| 411 | isAccessOperator(Func->getOverloadedOperator())) { |
| 412 | // Check for any kind of access of invalidated iterator positions |
| 413 | if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) { |
| 414 | verifyAccess(C, InstCall->getCXXThisVal()); |
| 415 | } else { |
| 416 | verifyAccess(C, Call.getArgSVal(0)); |
| 417 | } |
| 418 | } |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 419 | if (ChecksEnabled[CK_IteratorRangeChecker]) { |
| 420 | if (isIncrementOperator(Func->getOverloadedOperator())) { |
| 421 | // Check for out-of-range incrementions |
| 422 | if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) { |
| 423 | verifyIncrement(C, InstCall->getCXXThisVal()); |
| 424 | } else { |
| 425 | if (Call.getNumArgs() >= 1) { |
| 426 | verifyIncrement(C, Call.getArgSVal(0)); |
| 427 | } |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 428 | } |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 429 | } else if (isDecrementOperator(Func->getOverloadedOperator())) { |
| 430 | // Check for out-of-range decrementions |
| 431 | if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) { |
| 432 | verifyDecrement(C, InstCall->getCXXThisVal()); |
| 433 | } else { |
| 434 | if (Call.getNumArgs() >= 1) { |
| 435 | verifyDecrement(C, Call.getArgSVal(0)); |
| 436 | } |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 437 | } |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 438 | } else if (isRandomIncrOrDecrOperator(Func->getOverloadedOperator())) { |
| 439 | if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) { |
| 440 | // Check for out-of-range incrementions and decrementions |
| Adam Balogh | 8557f17 | 2019-08-05 06:45:41 +0000 | [diff] [blame] | 441 | if (Call.getNumArgs() >= 1 && |
| 442 | Call.getArgExpr(0)->getType()->isIntegralOrEnumerationType()) { |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 443 | verifyRandomIncrOrDecr(C, Func->getOverloadedOperator(), |
| 444 | InstCall->getCXXThisVal(), |
| 445 | Call.getArgSVal(0)); |
| 446 | } |
| 447 | } else { |
| Adam Balogh | 8557f17 | 2019-08-05 06:45:41 +0000 | [diff] [blame] | 448 | if (Call.getNumArgs() >= 2 && |
| 449 | Call.getArgExpr(1)->getType()->isIntegralOrEnumerationType()) { |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 450 | verifyRandomIncrOrDecr(C, Func->getOverloadedOperator(), |
| 451 | Call.getArgSVal(0), Call.getArgSVal(1)); |
| 452 | } |
| 453 | } |
| 454 | } else if (isDereferenceOperator(Func->getOverloadedOperator())) { |
| 455 | // Check for dereference of out-of-range iterators |
| 456 | if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) { |
| 457 | verifyDereference(C, InstCall->getCXXThisVal()); |
| 458 | } else { |
| 459 | verifyDereference(C, Call.getArgSVal(0)); |
| 460 | } |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 461 | } |
| Adam Balogh | 3659f7a | 2018-09-10 09:05:31 +0000 | [diff] [blame] | 462 | } else if (ChecksEnabled[CK_MismatchedIteratorChecker] && |
| 463 | isComparisonOperator(Func->getOverloadedOperator())) { |
| 464 | // Check for comparisons of iterators of different containers |
| 465 | if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) { |
| 466 | if (Call.getNumArgs() < 1) |
| 467 | return; |
| 468 | |
| 469 | if (!isIteratorType(InstCall->getCXXThisExpr()->getType()) || |
| 470 | !isIteratorType(Call.getArgExpr(0)->getType())) |
| 471 | return; |
| 472 | |
| 473 | verifyMatch(C, InstCall->getCXXThisVal(), Call.getArgSVal(0)); |
| 474 | } else { |
| 475 | if (Call.getNumArgs() < 2) |
| 476 | return; |
| 477 | |
| 478 | if (!isIteratorType(Call.getArgExpr(0)->getType()) || |
| 479 | !isIteratorType(Call.getArgExpr(1)->getType())) |
| 480 | return; |
| 481 | |
| 482 | verifyMatch(C, Call.getArgSVal(0), Call.getArgSVal(1)); |
| 483 | } |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 484 | } |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 485 | } else if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) { |
| 486 | if (!ChecksEnabled[CK_MismatchedIteratorChecker]) |
| 487 | return; |
| 488 | |
| 489 | const auto *ContReg = InstCall->getCXXThisVal().getAsRegion(); |
| 490 | if (!ContReg) |
| 491 | return; |
| 492 | // Check for erase, insert and emplace using iterator of another container |
| 493 | if (isEraseCall(Func) || isEraseAfterCall(Func)) { |
| 494 | verifyMatch(C, Call.getArgSVal(0), |
| 495 | InstCall->getCXXThisVal().getAsRegion()); |
| 496 | if (Call.getNumArgs() == 2) { |
| 497 | verifyMatch(C, Call.getArgSVal(1), |
| 498 | InstCall->getCXXThisVal().getAsRegion()); |
| 499 | } |
| 500 | } else if (isInsertCall(Func)) { |
| 501 | verifyMatch(C, Call.getArgSVal(0), |
| 502 | InstCall->getCXXThisVal().getAsRegion()); |
| 503 | if (Call.getNumArgs() == 3 && |
| 504 | isIteratorType(Call.getArgExpr(1)->getType()) && |
| 505 | isIteratorType(Call.getArgExpr(2)->getType())) { |
| 506 | verifyMatch(C, Call.getArgSVal(1), Call.getArgSVal(2)); |
| 507 | } |
| 508 | } else if (isEmplaceCall(Func)) { |
| 509 | verifyMatch(C, Call.getArgSVal(0), |
| 510 | InstCall->getCXXThisVal().getAsRegion()); |
| 511 | } |
| Adam Balogh | 3659f7a | 2018-09-10 09:05:31 +0000 | [diff] [blame] | 512 | } else if (isa<CXXConstructorCall>(&Call)) { |
| 513 | // Check match of first-last iterator pair in a constructor of a container |
| 514 | if (Call.getNumArgs() < 2) |
| 515 | return; |
| 516 | |
| 517 | const auto *Ctr = cast<CXXConstructorDecl>(Call.getDecl()); |
| 518 | if (Ctr->getNumParams() < 2) |
| 519 | return; |
| 520 | |
| 521 | if (Ctr->getParamDecl(0)->getName() != "first" || |
| 522 | Ctr->getParamDecl(1)->getName() != "last") |
| 523 | return; |
| 524 | |
| 525 | if (!isIteratorType(Call.getArgExpr(0)->getType()) || |
| 526 | !isIteratorType(Call.getArgExpr(1)->getType())) |
| 527 | return; |
| 528 | |
| 529 | verifyMatch(C, Call.getArgSVal(0), Call.getArgSVal(1)); |
| 530 | } else { |
| Adam Balogh | 21583b7 | 2018-09-10 09:03:22 +0000 | [diff] [blame] | 531 | // The main purpose of iterators is to abstract away from different |
| 532 | // containers and provide a (maybe limited) uniform access to them. |
| 533 | // This implies that any correctly written template function that |
| 534 | // works on multiple containers using iterators takes different |
| 535 | // template parameters for different containers. So we can safely |
| 536 | // assume that passing iterators of different containers as arguments |
| 537 | // whose type replaces the same template parameter is a bug. |
| 538 | // |
| 539 | // Example: |
| 540 | // template<typename I1, typename I2> |
| 541 | // void f(I1 first1, I1 last1, I2 first2, I2 last2); |
| 542 | // |
| 543 | // In this case the first two arguments to f() must be iterators must belong |
| 544 | // to the same container and the last to also to the same container but |
| Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 545 | // not necessarily to the same as the first two. |
| Adam Balogh | 21583b7 | 2018-09-10 09:03:22 +0000 | [diff] [blame] | 546 | |
| 547 | if (!ChecksEnabled[CK_MismatchedIteratorChecker]) |
| 548 | return; |
| 549 | |
| 550 | const auto *Templ = Func->getPrimaryTemplate(); |
| 551 | if (!Templ) |
| 552 | return; |
| 553 | |
| 554 | const auto *TParams = Templ->getTemplateParameters(); |
| 555 | const auto *TArgs = Func->getTemplateSpecializationArgs(); |
| 556 | |
| 557 | // Iterate over all the template parameters |
| 558 | for (size_t I = 0; I < TParams->size(); ++I) { |
| 559 | const auto *TPDecl = dyn_cast<TemplateTypeParmDecl>(TParams->getParam(I)); |
| 560 | if (!TPDecl) |
| 561 | continue; |
| 562 | |
| 563 | if (TPDecl->isParameterPack()) |
| 564 | continue; |
| 565 | |
| 566 | const auto TAType = TArgs->get(I).getAsType(); |
| 567 | if (!isIteratorType(TAType)) |
| 568 | continue; |
| 569 | |
| 570 | SVal LHS = UndefinedVal(); |
| 571 | |
| 572 | // For every template parameter which is an iterator type in the |
| 573 | // instantiation look for all functions' parameters' type by it and |
| 574 | // check whether they belong to the same container |
| 575 | for (auto J = 0U; J < Func->getNumParams(); ++J) { |
| 576 | const auto *Param = Func->getParamDecl(J); |
| 577 | const auto *ParamType = |
| 578 | Param->getType()->getAs<SubstTemplateTypeParmType>(); |
| 579 | if (!ParamType || |
| 580 | ParamType->getReplacedParameter()->getDecl() != TPDecl) |
| 581 | continue; |
| 582 | if (LHS.isUndef()) { |
| 583 | LHS = Call.getArgSVal(J); |
| 584 | } else { |
| 585 | verifyMatch(C, LHS, Call.getArgSVal(J)); |
| 586 | } |
| 587 | } |
| 588 | } |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
| 592 | void IteratorChecker::checkPostCall(const CallEvent &Call, |
| 593 | CheckerContext &C) const { |
| 594 | // Record new iterator positions and iterator position changes |
| 595 | const auto *Func = dyn_cast_or_null<FunctionDecl>(Call.getDecl()); |
| 596 | if (!Func) |
| 597 | return; |
| 598 | |
| 599 | if (Func->isOverloadedOperator()) { |
| 600 | const auto Op = Func->getOverloadedOperator(); |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 601 | if (isAssignmentOperator(Op)) { |
| Artem Dergachev | 630f7da | 2019-08-28 18:44:38 +0000 | [diff] [blame] | 602 | // Overloaded 'operator=' must be a non-static member function. |
| 603 | const auto *InstCall = cast<CXXInstanceCall>(&Call); |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 604 | if (cast<CXXMethodDecl>(Func)->isMoveAssignmentOperator()) { |
| Adam Balogh | 6b23b1a | 2018-09-10 09:04:27 +0000 | [diff] [blame] | 605 | handleAssign(C, InstCall->getCXXThisVal(), Call.getOriginExpr(), |
| 606 | Call.getArgSVal(0)); |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 607 | return; |
| Adam Balogh | 6b23b1a | 2018-09-10 09:04:27 +0000 | [diff] [blame] | 608 | } |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 609 | |
| 610 | handleAssign(C, InstCall->getCXXThisVal()); |
| 611 | return; |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 612 | } else if (isSimpleComparisonOperator(Op)) { |
| Adam Balogh | 54976e7 | 2019-04-23 07:15:55 +0000 | [diff] [blame] | 613 | const auto *OrigExpr = Call.getOriginExpr(); |
| 614 | if (!OrigExpr) |
| 615 | return; |
| 616 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 617 | if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) { |
| Adam Balogh | 54976e7 | 2019-04-23 07:15:55 +0000 | [diff] [blame] | 618 | handleComparison(C, OrigExpr, Call.getReturnValue(), |
| 619 | InstCall->getCXXThisVal(), Call.getArgSVal(0), Op); |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 620 | return; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 621 | } |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 622 | |
| 623 | handleComparison(C, OrigExpr, Call.getReturnValue(), Call.getArgSVal(0), |
| 624 | Call.getArgSVal(1), Op); |
| 625 | return; |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 626 | } else if (isRandomIncrOrDecrOperator(Func->getOverloadedOperator())) { |
| 627 | if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) { |
| Adam Balogh | 8557f17 | 2019-08-05 06:45:41 +0000 | [diff] [blame] | 628 | if (Call.getNumArgs() >= 1 && |
| 629 | Call.getArgExpr(0)->getType()->isIntegralOrEnumerationType()) { |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 630 | handleRandomIncrOrDecr(C, Func->getOverloadedOperator(), |
| 631 | Call.getReturnValue(), |
| 632 | InstCall->getCXXThisVal(), Call.getArgSVal(0)); |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 633 | return; |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 634 | } |
| 635 | } else { |
| Adam Balogh | 8557f17 | 2019-08-05 06:45:41 +0000 | [diff] [blame] | 636 | if (Call.getNumArgs() >= 2 && |
| 637 | Call.getArgExpr(1)->getType()->isIntegralOrEnumerationType()) { |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 638 | handleRandomIncrOrDecr(C, Func->getOverloadedOperator(), |
| 639 | Call.getReturnValue(), Call.getArgSVal(0), |
| 640 | Call.getArgSVal(1)); |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 641 | return; |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | } else if (isIncrementOperator(Func->getOverloadedOperator())) { |
| 645 | if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) { |
| 646 | handleIncrement(C, Call.getReturnValue(), InstCall->getCXXThisVal(), |
| 647 | Call.getNumArgs()); |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 648 | return; |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 649 | } |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 650 | |
| 651 | handleIncrement(C, Call.getReturnValue(), Call.getArgSVal(0), |
| 652 | Call.getNumArgs()); |
| 653 | return; |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 654 | } else if (isDecrementOperator(Func->getOverloadedOperator())) { |
| 655 | if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) { |
| 656 | handleDecrement(C, Call.getReturnValue(), InstCall->getCXXThisVal(), |
| 657 | Call.getNumArgs()); |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 658 | return; |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 659 | } |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 660 | |
| 661 | handleDecrement(C, Call.getReturnValue(), Call.getArgSVal(0), |
| 662 | Call.getNumArgs()); |
| 663 | return; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 664 | } |
| 665 | } else { |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 666 | if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) { |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 667 | if (isAssignCall(Func)) { |
| 668 | handleAssign(C, InstCall->getCXXThisVal()); |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 669 | return; |
| 670 | } |
| 671 | |
| 672 | if (isClearCall(Func)) { |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 673 | handleClear(C, InstCall->getCXXThisVal()); |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 674 | return; |
| 675 | } |
| 676 | |
| 677 | if (isPushBackCall(Func) || isEmplaceBackCall(Func)) { |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 678 | handlePushBack(C, InstCall->getCXXThisVal()); |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 679 | return; |
| 680 | } |
| 681 | |
| 682 | if (isPopBackCall(Func)) { |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 683 | handlePopBack(C, InstCall->getCXXThisVal()); |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 684 | return; |
| 685 | } |
| 686 | |
| 687 | if (isPushFrontCall(Func) || isEmplaceFrontCall(Func)) { |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 688 | handlePushFront(C, InstCall->getCXXThisVal()); |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 689 | return; |
| 690 | } |
| 691 | |
| 692 | if (isPopFrontCall(Func)) { |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 693 | handlePopFront(C, InstCall->getCXXThisVal()); |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 694 | return; |
| 695 | } |
| 696 | |
| 697 | if (isInsertCall(Func) || isEmplaceCall(Func)) { |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 698 | handleInsert(C, Call.getArgSVal(0)); |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 699 | return; |
| 700 | } |
| 701 | |
| 702 | if (isEraseCall(Func)) { |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 703 | if (Call.getNumArgs() == 1) { |
| 704 | handleErase(C, Call.getArgSVal(0)); |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 705 | return; |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 706 | } |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 707 | |
| 708 | if (Call.getNumArgs() == 2) { |
| 709 | handleErase(C, Call.getArgSVal(0), Call.getArgSVal(1)); |
| 710 | return; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | if (isEraseAfterCall(Func)) { |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 715 | if (Call.getNumArgs() == 1) { |
| 716 | handleEraseAfter(C, Call.getArgSVal(0)); |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 717 | return; |
| 718 | } |
| 719 | |
| 720 | if (Call.getNumArgs() == 2) { |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 721 | handleEraseAfter(C, Call.getArgSVal(0), Call.getArgSVal(1)); |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 722 | return; |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 723 | } |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 727 | const auto *OrigExpr = Call.getOriginExpr(); |
| 728 | if (!OrigExpr) |
| 729 | return; |
| 730 | |
| 731 | if (!isIteratorType(Call.getResultType())) |
| 732 | return; |
| 733 | |
| 734 | auto State = C.getState(); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 735 | |
| 736 | if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) { |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 737 | if (isBeginCall(Func)) { |
| 738 | handleBegin(C, OrigExpr, Call.getReturnValue(), |
| 739 | InstCall->getCXXThisVal()); |
| 740 | return; |
| 741 | } |
| Adam Balogh | d538b70 | 2019-04-26 07:30:07 +0000 | [diff] [blame] | 742 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 743 | if (isEndCall(Func)) { |
| 744 | handleEnd(C, OrigExpr, Call.getReturnValue(), |
| 745 | InstCall->getCXXThisVal()); |
| 746 | return; |
| 747 | } |
| 748 | } |
| 749 | |
| Adam Balogh | 6b23b1a | 2018-09-10 09:04:27 +0000 | [diff] [blame] | 750 | // Already bound to container? |
| 751 | if (getIteratorPosition(State, Call.getReturnValue())) |
| 752 | return; |
| 753 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 754 | // Copy-like and move constructors |
| 755 | if (isa<CXXConstructorCall>(&Call) && Call.getNumArgs() == 1) { |
| 756 | if (const auto *Pos = getIteratorPosition(State, Call.getArgSVal(0))) { |
| 757 | State = setIteratorPosition(State, Call.getReturnValue(), *Pos); |
| 758 | if (cast<CXXConstructorDecl>(Func)->isMoveConstructor()) { |
| 759 | State = removeIteratorPosition(State, Call.getArgSVal(0)); |
| 760 | } |
| 761 | C.addTransition(State); |
| 762 | return; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | // Assumption: if return value is an iterator which is not yet bound to a |
| 767 | // container, then look for the first iterator argument, and |
| 768 | // bind the return value to the same container. This approach |
| 769 | // works for STL algorithms. |
| 770 | // FIXME: Add a more conservative mode |
| 771 | for (unsigned i = 0; i < Call.getNumArgs(); ++i) { |
| 772 | if (isIteratorType(Call.getArgExpr(i)->getType())) { |
| 773 | if (const auto *Pos = getIteratorPosition(State, Call.getArgSVal(i))) { |
| 774 | assignToContainer(C, OrigExpr, Call.getReturnValue(), |
| 775 | Pos->getContainer()); |
| 776 | return; |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 783 | void IteratorChecker::checkBind(SVal Loc, SVal Val, const Stmt *S, |
| 784 | CheckerContext &C) const { |
| 785 | auto State = C.getState(); |
| 786 | const auto *Pos = getIteratorPosition(State, Val); |
| 787 | if (Pos) { |
| 788 | State = setIteratorPosition(State, Loc, *Pos); |
| 789 | C.addTransition(State); |
| 790 | } else { |
| 791 | const auto *OldPos = getIteratorPosition(State, Loc); |
| 792 | if (OldPos) { |
| 793 | State = removeIteratorPosition(State, Loc); |
| 794 | C.addTransition(State); |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 799 | void IteratorChecker::checkPostStmt(const MaterializeTemporaryExpr *MTE, |
| 800 | CheckerContext &C) const { |
| 801 | /* Transfer iterator state to temporary objects */ |
| 802 | auto State = C.getState(); |
| Tyker | 08ea1ee | 2019-11-16 17:04:34 +0100 | [diff] [blame] | 803 | const auto *Pos = getIteratorPosition(State, C.getSVal(MTE->getSubExpr())); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 804 | if (!Pos) |
| 805 | return; |
| George Karpenkov | d703ec9 | 2018-01-17 20:27:29 +0000 | [diff] [blame] | 806 | State = setIteratorPosition(State, C.getSVal(MTE), *Pos); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 807 | C.addTransition(State); |
| 808 | } |
| 809 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 810 | void IteratorChecker::checkLiveSymbols(ProgramStateRef State, |
| 811 | SymbolReaper &SR) const { |
| 812 | // Keep symbolic expressions of iterator positions, container begins and ends |
| 813 | // alive |
| 814 | auto RegionMap = State->get<IteratorRegionMap>(); |
| 815 | for (const auto Reg : RegionMap) { |
| Adam Balogh | 0a7592b | 2018-07-16 09:27:27 +0000 | [diff] [blame] | 816 | const auto Offset = Reg.second.getOffset(); |
| 817 | for (auto i = Offset->symbol_begin(); i != Offset->symbol_end(); ++i) |
| 818 | if (isa<SymbolData>(*i)) |
| 819 | SR.markLive(*i); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | auto SymbolMap = State->get<IteratorSymbolMap>(); |
| 823 | for (const auto Sym : SymbolMap) { |
| Adam Balogh | 0a7592b | 2018-07-16 09:27:27 +0000 | [diff] [blame] | 824 | const auto Offset = Sym.second.getOffset(); |
| 825 | for (auto i = Offset->symbol_begin(); i != Offset->symbol_end(); ++i) |
| 826 | if (isa<SymbolData>(*i)) |
| 827 | SR.markLive(*i); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | auto ContMap = State->get<ContainerMap>(); |
| 831 | for (const auto Cont : ContMap) { |
| 832 | const auto CData = Cont.second; |
| 833 | if (CData.getBegin()) { |
| 834 | SR.markLive(CData.getBegin()); |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 835 | if(const auto *SIE = dyn_cast<SymIntExpr>(CData.getBegin())) |
| 836 | SR.markLive(SIE->getLHS()); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 837 | } |
| 838 | if (CData.getEnd()) { |
| 839 | SR.markLive(CData.getEnd()); |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 840 | if(const auto *SIE = dyn_cast<SymIntExpr>(CData.getEnd())) |
| 841 | SR.markLive(SIE->getLHS()); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | } |
| 845 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 846 | void IteratorChecker::checkDeadSymbols(SymbolReaper &SR, |
| 847 | CheckerContext &C) const { |
| 848 | // Cleanup |
| 849 | auto State = C.getState(); |
| 850 | |
| 851 | auto RegionMap = State->get<IteratorRegionMap>(); |
| 852 | for (const auto Reg : RegionMap) { |
| 853 | if (!SR.isLiveRegion(Reg.first)) { |
| Adam Balogh | 21583b7 | 2018-09-10 09:03:22 +0000 | [diff] [blame] | 854 | // The region behind the `LazyCompoundVal` is often cleaned up before |
| 855 | // the `LazyCompoundVal` itself. If there are iterator positions keyed |
| 856 | // by these regions their cleanup must be deferred. |
| 857 | if (!isBoundThroughLazyCompoundVal(State->getEnvironment(), Reg.first)) { |
| 858 | State = State->remove<IteratorRegionMap>(Reg.first); |
| 859 | } |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | |
| 863 | auto SymbolMap = State->get<IteratorSymbolMap>(); |
| 864 | for (const auto Sym : SymbolMap) { |
| 865 | if (!SR.isLive(Sym.first)) { |
| 866 | State = State->remove<IteratorSymbolMap>(Sym.first); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | auto ContMap = State->get<ContainerMap>(); |
| 871 | for (const auto Cont : ContMap) { |
| 872 | if (!SR.isLiveRegion(Cont.first)) { |
| Adam Balogh | a692120 | 2018-07-30 08:52:21 +0000 | [diff] [blame] | 873 | // We must keep the container data while it has live iterators to be able |
| 874 | // to compare them to the begin and the end of the container. |
| 875 | if (!hasLiveIterators(State, Cont.first)) { |
| 876 | State = State->remove<ContainerMap>(Cont.first); |
| 877 | } |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
| Reka Kovacs | e48ea89 | 2018-07-30 16:14:59 +0000 | [diff] [blame] | 881 | C.addTransition(State); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| Adam Balogh | 54976e7 | 2019-04-23 07:15:55 +0000 | [diff] [blame] | 884 | void IteratorChecker::handleComparison(CheckerContext &C, const Expr *CE, |
| 885 | const SVal &RetVal, const SVal &LVal, |
| 886 | const SVal &RVal, |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 887 | OverloadedOperatorKind Op) const { |
| 888 | // Record the operands and the operator of the comparison for the next |
| 889 | // evalAssume, if the result is a symbolic expression. If it is a concrete |
| 890 | // value (only one branch is possible), then transfer the state between |
| 891 | // the operands according to the operator and the result |
| Adam Balogh | 54976e7 | 2019-04-23 07:15:55 +0000 | [diff] [blame] | 892 | auto State = C.getState(); |
| 893 | const auto *LPos = getIteratorPosition(State, LVal); |
| 894 | const auto *RPos = getIteratorPosition(State, RVal); |
| 895 | const MemRegion *Cont = nullptr; |
| 896 | if (LPos) { |
| 897 | Cont = LPos->getContainer(); |
| 898 | } else if (RPos) { |
| 899 | Cont = RPos->getContainer(); |
| 900 | } |
| 901 | if (!Cont) |
| 902 | return; |
| 903 | |
| 904 | // At least one of the iterators have recorded positions. If one of them has |
| 905 | // not then create a new symbol for the offset. |
| 906 | SymbolRef Sym; |
| 907 | if (!LPos || !RPos) { |
| 908 | auto &SymMgr = C.getSymbolManager(); |
| Adam Balogh | d2e2e20 | 2019-04-23 11:18:50 +0000 | [diff] [blame] | 909 | Sym = SymMgr.conjureSymbol(CE, C.getLocationContext(), |
| Adam Balogh | 54976e7 | 2019-04-23 07:15:55 +0000 | [diff] [blame] | 910 | C.getASTContext().LongTy, C.blockCount()); |
| 911 | State = assumeNoOverflow(State, Sym, 4); |
| 912 | } |
| 913 | |
| 914 | if (!LPos) { |
| 915 | State = setIteratorPosition(State, LVal, |
| 916 | IteratorPosition::getPosition(Cont, Sym)); |
| 917 | LPos = getIteratorPosition(State, LVal); |
| 918 | } else if (!RPos) { |
| 919 | State = setIteratorPosition(State, RVal, |
| 920 | IteratorPosition::getPosition(Cont, Sym)); |
| 921 | RPos = getIteratorPosition(State, RVal); |
| 922 | } |
| 923 | |
| 924 | processComparison(C, State, LPos->getOffset(), RPos->getOffset(), RetVal, Op); |
| 925 | } |
| 926 | |
| 927 | void IteratorChecker::processComparison(CheckerContext &C, |
| 928 | ProgramStateRef State, SymbolRef Sym1, |
| 929 | SymbolRef Sym2, const SVal &RetVal, |
| 930 | OverloadedOperatorKind Op) const { |
| 931 | if (const auto TruthVal = RetVal.getAs<nonloc::ConcreteInt>()) { |
| Adam Balogh | 8f88270 | 2019-04-23 07:45:10 +0000 | [diff] [blame] | 932 | if ((State = relateSymbols(State, Sym1, Sym2, |
| Adam Balogh | 54976e7 | 2019-04-23 07:15:55 +0000 | [diff] [blame] | 933 | (Op == OO_EqualEqual) == |
| Adam Balogh | 8f88270 | 2019-04-23 07:45:10 +0000 | [diff] [blame] | 934 | (TruthVal->getValue() != 0)))) { |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 935 | C.addTransition(State); |
| 936 | } else { |
| 937 | C.generateSink(State, C.getPredecessor()); |
| 938 | } |
| Adam Balogh | 54976e7 | 2019-04-23 07:15:55 +0000 | [diff] [blame] | 939 | return; |
| 940 | } |
| 941 | |
| 942 | const auto ConditionVal = RetVal.getAs<DefinedSVal>(); |
| 943 | if (!ConditionVal) |
| 944 | return; |
| 945 | |
| 946 | if (auto StateTrue = relateSymbols(State, Sym1, Sym2, Op == OO_EqualEqual)) { |
| 947 | StateTrue = StateTrue->assume(*ConditionVal, true); |
| 948 | C.addTransition(StateTrue); |
| 949 | } |
| 950 | |
| 951 | if (auto StateFalse = relateSymbols(State, Sym1, Sym2, Op != OO_EqualEqual)) { |
| 952 | StateFalse = StateFalse->assume(*ConditionVal, false); |
| 953 | C.addTransition(StateFalse); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
| 957 | void IteratorChecker::verifyDereference(CheckerContext &C, |
| 958 | const SVal &Val) const { |
| 959 | auto State = C.getState(); |
| 960 | const auto *Pos = getIteratorPosition(State, Val); |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 961 | if (Pos && isPastTheEnd(State, *Pos)) { |
| Adam Balogh | 12f5c7f | 2019-08-29 09:35:47 +0000 | [diff] [blame] | 962 | auto *N = C.generateErrorNode(State); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 963 | if (!N) |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 964 | return; |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 965 | reportOutOfRangeBug("Past-the-end iterator dereferenced.", Val, C, N); |
| 966 | return; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 970 | void IteratorChecker::verifyAccess(CheckerContext &C, const SVal &Val) const { |
| 971 | auto State = C.getState(); |
| 972 | const auto *Pos = getIteratorPosition(State, Val); |
| 973 | if (Pos && !Pos->isValid()) { |
| Adam Balogh | 12f5c7f | 2019-08-29 09:35:47 +0000 | [diff] [blame] | 974 | auto *N = C.generateErrorNode(State); |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 975 | if (!N) { |
| 976 | return; |
| 977 | } |
| 978 | reportInvalidatedBug("Invalidated iterator accessed.", Val, C, N); |
| 979 | } |
| 980 | } |
| 981 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 982 | void IteratorChecker::handleIncrement(CheckerContext &C, const SVal &RetVal, |
| 983 | const SVal &Iter, bool Postfix) const { |
| 984 | // Increment the symbolic expressions which represents the position of the |
| 985 | // iterator |
| 986 | auto State = C.getState(); |
| 987 | const auto *Pos = getIteratorPosition(State, Iter); |
| 988 | if (Pos) { |
| 989 | auto &SymMgr = C.getSymbolManager(); |
| 990 | auto &BVF = SymMgr.getBasicVals(); |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 991 | const auto NewPos = |
| 992 | advancePosition(C, OO_Plus, *Pos, |
| 993 | nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1)))); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 994 | State = setIteratorPosition(State, Iter, NewPos); |
| 995 | State = setIteratorPosition(State, RetVal, Postfix ? *Pos : NewPos); |
| 996 | C.addTransition(State); |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | void IteratorChecker::handleDecrement(CheckerContext &C, const SVal &RetVal, |
| 1001 | const SVal &Iter, bool Postfix) const { |
| 1002 | // Decrement the symbolic expressions which represents the position of the |
| 1003 | // iterator |
| 1004 | auto State = C.getState(); |
| 1005 | const auto *Pos = getIteratorPosition(State, Iter); |
| 1006 | if (Pos) { |
| 1007 | auto &SymMgr = C.getSymbolManager(); |
| 1008 | auto &BVF = SymMgr.getBasicVals(); |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 1009 | const auto NewPos = |
| 1010 | advancePosition(C, OO_Minus, *Pos, |
| 1011 | nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1)))); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1012 | State = setIteratorPosition(State, Iter, NewPos); |
| 1013 | State = setIteratorPosition(State, RetVal, Postfix ? *Pos : NewPos); |
| 1014 | C.addTransition(State); |
| 1015 | } |
| 1016 | } |
| 1017 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1018 | void IteratorChecker::handleRandomIncrOrDecr(CheckerContext &C, |
| 1019 | OverloadedOperatorKind Op, |
| 1020 | const SVal &RetVal, |
| 1021 | const SVal &LHS, |
| 1022 | const SVal &RHS) const { |
| 1023 | // Increment or decrement the symbolic expressions which represents the |
| 1024 | // position of the iterator |
| 1025 | auto State = C.getState(); |
| 1026 | const auto *Pos = getIteratorPosition(State, LHS); |
| 1027 | if (!Pos) |
| 1028 | return; |
| 1029 | |
| 1030 | const auto *value = &RHS; |
| 1031 | if (auto loc = RHS.getAs<Loc>()) { |
| 1032 | const auto val = State->getRawSVal(*loc); |
| 1033 | value = &val; |
| 1034 | } |
| 1035 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1036 | auto &TgtVal = (Op == OO_PlusEqual || Op == OO_MinusEqual) ? LHS : RetVal; |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 1037 | State = |
| 1038 | setIteratorPosition(State, TgtVal, advancePosition(C, Op, *Pos, *value)); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1039 | C.addTransition(State); |
| 1040 | } |
| 1041 | |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 1042 | void IteratorChecker::verifyIncrement(CheckerContext &C, |
| 1043 | const SVal &Iter) const { |
| 1044 | auto &BVF = C.getSValBuilder().getBasicValueFactory(); |
| 1045 | verifyRandomIncrOrDecr(C, OO_Plus, Iter, |
| 1046 | nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1)))); |
| 1047 | } |
| 1048 | |
| 1049 | void IteratorChecker::verifyDecrement(CheckerContext &C, |
| 1050 | const SVal &Iter) const { |
| 1051 | auto &BVF = C.getSValBuilder().getBasicValueFactory(); |
| 1052 | verifyRandomIncrOrDecr(C, OO_Minus, Iter, |
| 1053 | nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1)))); |
| 1054 | } |
| 1055 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1056 | void IteratorChecker::verifyRandomIncrOrDecr(CheckerContext &C, |
| 1057 | OverloadedOperatorKind Op, |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1058 | const SVal &LHS, |
| 1059 | const SVal &RHS) const { |
| 1060 | auto State = C.getState(); |
| 1061 | |
| 1062 | // If the iterator is initially inside its range, then the operation is valid |
| 1063 | const auto *Pos = getIteratorPosition(State, LHS); |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 1064 | if (!Pos) |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1065 | return; |
| 1066 | |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 1067 | auto Value = RHS; |
| 1068 | if (auto ValAsLoc = RHS.getAs<Loc>()) { |
| 1069 | Value = State->getRawSVal(*ValAsLoc); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 1072 | if (Value.isUnknown()) |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1073 | return; |
| 1074 | |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 1075 | // Incremention or decremention by 0 is never a bug. |
| 1076 | if (isZero(State, Value.castAs<NonLoc>())) |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1077 | return; |
| 1078 | |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 1079 | // The result may be the past-end iterator of the container, but any other |
| 1080 | // out of range position is undefined behaviour |
| 1081 | if (isAheadOfRange(State, advancePosition(C, Op, *Pos, Value))) { |
| Adam Balogh | 12f5c7f | 2019-08-29 09:35:47 +0000 | [diff] [blame] | 1082 | auto *N = C.generateErrorNode(State); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1083 | if (!N) |
| 1084 | return; |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 1085 | reportOutOfRangeBug("Iterator decremented ahead of its valid range.", LHS, |
| 1086 | C, N); |
| 1087 | } |
| 1088 | if (isBehindPastTheEnd(State, advancePosition(C, Op, *Pos, Value))) { |
| Adam Balogh | 12f5c7f | 2019-08-29 09:35:47 +0000 | [diff] [blame] | 1089 | auto *N = C.generateErrorNode(State); |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 1090 | if (!N) |
| 1091 | return; |
| 1092 | reportOutOfRangeBug("Iterator incremented behind the past-the-end " |
| 1093 | "iterator.", LHS, C, N); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 1097 | void IteratorChecker::verifyMatch(CheckerContext &C, const SVal &Iter, |
| 1098 | const MemRegion *Cont) const { |
| 1099 | // Verify match between a container and the container of an iterator |
| Adam Balogh | 42d241f | 2018-12-04 10:22:28 +0000 | [diff] [blame] | 1100 | Cont = Cont->getMostDerivedObjectRegion(); |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 1101 | |
| Adam Balogh | d703305 | 2019-03-13 13:55:11 +0000 | [diff] [blame] | 1102 | if (const auto *ContSym = Cont->getSymbolicBase()) { |
| 1103 | if (isa<SymbolConjured>(ContSym->getSymbol())) |
| 1104 | return; |
| 1105 | } |
| 1106 | |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 1107 | auto State = C.getState(); |
| 1108 | const auto *Pos = getIteratorPosition(State, Iter); |
| Adam Balogh | d703305 | 2019-03-13 13:55:11 +0000 | [diff] [blame] | 1109 | if (!Pos) |
| 1110 | return; |
| 1111 | |
| 1112 | const auto *IterCont = Pos->getContainer(); |
| 1113 | |
| 1114 | // Skip symbolic regions based on conjured symbols. Two conjured symbols |
| 1115 | // may or may not be the same. For example, the same function can return |
| 1116 | // the same or a different container but we get different conjured symbols |
| 1117 | // for each call. This may cause false positives so omit them from the check. |
| 1118 | if (const auto *ContSym = IterCont->getSymbolicBase()) { |
| 1119 | if (isa<SymbolConjured>(ContSym->getSymbol())) |
| 1120 | return; |
| 1121 | } |
| 1122 | |
| 1123 | if (IterCont != Cont) { |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 1124 | auto *N = C.generateNonFatalErrorNode(State); |
| 1125 | if (!N) { |
| 1126 | return; |
| 1127 | } |
| Adam Balogh | d703305 | 2019-03-13 13:55:11 +0000 | [diff] [blame] | 1128 | reportMismatchedBug("Container accessed using foreign iterator argument.", |
| 1129 | Iter, Cont, C, N); |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | |
| Adam Balogh | 21583b7 | 2018-09-10 09:03:22 +0000 | [diff] [blame] | 1133 | void IteratorChecker::verifyMatch(CheckerContext &C, const SVal &Iter1, |
| 1134 | const SVal &Iter2) const { |
| 1135 | // Verify match between the containers of two iterators |
| 1136 | auto State = C.getState(); |
| 1137 | const auto *Pos1 = getIteratorPosition(State, Iter1); |
| Adam Balogh | d703305 | 2019-03-13 13:55:11 +0000 | [diff] [blame] | 1138 | if (!Pos1) |
| 1139 | return; |
| 1140 | |
| 1141 | const auto *IterCont1 = Pos1->getContainer(); |
| 1142 | |
| 1143 | // Skip symbolic regions based on conjured symbols. Two conjured symbols |
| 1144 | // may or may not be the same. For example, the same function can return |
| 1145 | // the same or a different container but we get different conjured symbols |
| 1146 | // for each call. This may cause false positives so omit them from the check. |
| 1147 | if (const auto *ContSym = IterCont1->getSymbolicBase()) { |
| 1148 | if (isa<SymbolConjured>(ContSym->getSymbol())) |
| 1149 | return; |
| 1150 | } |
| 1151 | |
| Adam Balogh | 21583b7 | 2018-09-10 09:03:22 +0000 | [diff] [blame] | 1152 | const auto *Pos2 = getIteratorPosition(State, Iter2); |
| Adam Balogh | d703305 | 2019-03-13 13:55:11 +0000 | [diff] [blame] | 1153 | if (!Pos2) |
| 1154 | return; |
| 1155 | |
| 1156 | const auto *IterCont2 = Pos2->getContainer(); |
| 1157 | if (const auto *ContSym = IterCont2->getSymbolicBase()) { |
| 1158 | if (isa<SymbolConjured>(ContSym->getSymbol())) |
| 1159 | return; |
| 1160 | } |
| 1161 | |
| 1162 | if (IterCont1 != IterCont2) { |
| Adam Balogh | 21583b7 | 2018-09-10 09:03:22 +0000 | [diff] [blame] | 1163 | auto *N = C.generateNonFatalErrorNode(State); |
| 1164 | if (!N) |
| 1165 | return; |
| 1166 | reportMismatchedBug("Iterators of different containers used where the " |
| 1167 | "same container is expected.", Iter1, Iter2, C, N); |
| 1168 | } |
| 1169 | } |
| 1170 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1171 | void IteratorChecker::handleBegin(CheckerContext &C, const Expr *CE, |
| 1172 | const SVal &RetVal, const SVal &Cont) const { |
| 1173 | const auto *ContReg = Cont.getAsRegion(); |
| 1174 | if (!ContReg) |
| 1175 | return; |
| 1176 | |
| Adam Balogh | 42d241f | 2018-12-04 10:22:28 +0000 | [diff] [blame] | 1177 | ContReg = ContReg->getMostDerivedObjectRegion(); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1178 | |
| 1179 | // If the container already has a begin symbol then use it. Otherwise first |
| 1180 | // create a new one. |
| 1181 | auto State = C.getState(); |
| 1182 | auto BeginSym = getContainerBegin(State, ContReg); |
| 1183 | if (!BeginSym) { |
| Adam Balogh | 33160c4 | 2019-05-20 11:04:27 +0000 | [diff] [blame] | 1184 | State = createContainerBegin(State, ContReg, CE, C.getASTContext().LongTy, |
| 1185 | C.getLocationContext(), C.blockCount()); |
| 1186 | BeginSym = getContainerBegin(State, ContReg); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1187 | } |
| 1188 | State = setIteratorPosition(State, RetVal, |
| 1189 | IteratorPosition::getPosition(ContReg, BeginSym)); |
| 1190 | C.addTransition(State); |
| 1191 | } |
| 1192 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 1193 | void IteratorChecker::handleEnd(CheckerContext &C, const Expr *CE, |
| 1194 | const SVal &RetVal, const SVal &Cont) const { |
| 1195 | const auto *ContReg = Cont.getAsRegion(); |
| 1196 | if (!ContReg) |
| 1197 | return; |
| 1198 | |
| Adam Balogh | 42d241f | 2018-12-04 10:22:28 +0000 | [diff] [blame] | 1199 | ContReg = ContReg->getMostDerivedObjectRegion(); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 1200 | |
| 1201 | // If the container already has an end symbol then use it. Otherwise first |
| 1202 | // create a new one. |
| 1203 | auto State = C.getState(); |
| 1204 | auto EndSym = getContainerEnd(State, ContReg); |
| 1205 | if (!EndSym) { |
| Adam Balogh | 33160c4 | 2019-05-20 11:04:27 +0000 | [diff] [blame] | 1206 | State = createContainerEnd(State, ContReg, CE, C.getASTContext().LongTy, |
| 1207 | C.getLocationContext(), C.blockCount()); |
| 1208 | EndSym = getContainerEnd(State, ContReg); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 1209 | } |
| 1210 | State = setIteratorPosition(State, RetVal, |
| 1211 | IteratorPosition::getPosition(ContReg, EndSym)); |
| 1212 | C.addTransition(State); |
| 1213 | } |
| 1214 | |
| 1215 | void IteratorChecker::assignToContainer(CheckerContext &C, const Expr *CE, |
| 1216 | const SVal &RetVal, |
| 1217 | const MemRegion *Cont) const { |
| Adam Balogh | 42d241f | 2018-12-04 10:22:28 +0000 | [diff] [blame] | 1218 | Cont = Cont->getMostDerivedObjectRegion(); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 1219 | |
| 1220 | auto State = C.getState(); |
| 1221 | auto &SymMgr = C.getSymbolManager(); |
| 1222 | auto Sym = SymMgr.conjureSymbol(CE, C.getLocationContext(), |
| 1223 | C.getASTContext().LongTy, C.blockCount()); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1224 | State = assumeNoOverflow(State, Sym, 4); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 1225 | State = setIteratorPosition(State, RetVal, |
| 1226 | IteratorPosition::getPosition(Cont, Sym)); |
| 1227 | C.addTransition(State); |
| 1228 | } |
| 1229 | |
| Adam Balogh | 6b23b1a | 2018-09-10 09:04:27 +0000 | [diff] [blame] | 1230 | void IteratorChecker::handleAssign(CheckerContext &C, const SVal &Cont, |
| 1231 | const Expr *CE, const SVal &OldCont) const { |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 1232 | const auto *ContReg = Cont.getAsRegion(); |
| 1233 | if (!ContReg) |
| 1234 | return; |
| 1235 | |
| Adam Balogh | 42d241f | 2018-12-04 10:22:28 +0000 | [diff] [blame] | 1236 | ContReg = ContReg->getMostDerivedObjectRegion(); |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 1237 | |
| 1238 | // Assignment of a new value to a container always invalidates all its |
| 1239 | // iterators |
| 1240 | auto State = C.getState(); |
| 1241 | const auto CData = getContainerData(State, ContReg); |
| 1242 | if (CData) { |
| 1243 | State = invalidateAllIteratorPositions(State, ContReg); |
| 1244 | } |
| 1245 | |
| Adam Balogh | 6b23b1a | 2018-09-10 09:04:27 +0000 | [diff] [blame] | 1246 | // In case of move, iterators of the old container (except the past-end |
| 1247 | // iterators) remain valid but refer to the new container |
| 1248 | if (!OldCont.isUndef()) { |
| 1249 | const auto *OldContReg = OldCont.getAsRegion(); |
| 1250 | if (OldContReg) { |
| Adam Balogh | 42d241f | 2018-12-04 10:22:28 +0000 | [diff] [blame] | 1251 | OldContReg = OldContReg->getMostDerivedObjectRegion(); |
| Adam Balogh | 6b23b1a | 2018-09-10 09:04:27 +0000 | [diff] [blame] | 1252 | const auto OldCData = getContainerData(State, OldContReg); |
| 1253 | if (OldCData) { |
| 1254 | if (const auto OldEndSym = OldCData->getEnd()) { |
| Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 1255 | // If we already assigned an "end" symbol to the old container, then |
| Adam Balogh | 6b23b1a | 2018-09-10 09:04:27 +0000 | [diff] [blame] | 1256 | // first reassign all iterator positions to the new container which |
| 1257 | // are not past the container (thus not greater or equal to the |
| 1258 | // current "end" symbol). |
| 1259 | State = reassignAllIteratorPositionsUnless(State, OldContReg, ContReg, |
| 1260 | OldEndSym, BO_GE); |
| 1261 | auto &SymMgr = C.getSymbolManager(); |
| 1262 | auto &SVB = C.getSValBuilder(); |
| 1263 | // Then generate and assign a new "end" symbol for the new container. |
| 1264 | auto NewEndSym = |
| 1265 | SymMgr.conjureSymbol(CE, C.getLocationContext(), |
| 1266 | C.getASTContext().LongTy, C.blockCount()); |
| 1267 | State = assumeNoOverflow(State, NewEndSym, 4); |
| 1268 | if (CData) { |
| 1269 | State = setContainerData(State, ContReg, CData->newEnd(NewEndSym)); |
| 1270 | } else { |
| 1271 | State = setContainerData(State, ContReg, |
| 1272 | ContainerData::fromEnd(NewEndSym)); |
| 1273 | } |
| 1274 | // Finally, replace the old "end" symbol in the already reassigned |
| 1275 | // iterator positions with the new "end" symbol. |
| 1276 | State = rebaseSymbolInIteratorPositionsIf( |
| 1277 | State, SVB, OldEndSym, NewEndSym, OldEndSym, BO_LT); |
| 1278 | } else { |
| 1279 | // There was no "end" symbol assigned yet to the old container, |
| 1280 | // so reassign all iterator positions to the new container. |
| 1281 | State = reassignAllIteratorPositions(State, OldContReg, ContReg); |
| 1282 | } |
| 1283 | if (const auto OldBeginSym = OldCData->getBegin()) { |
| 1284 | // If we already assigned a "begin" symbol to the old container, then |
| 1285 | // assign it to the new container and remove it from the old one. |
| 1286 | if (CData) { |
| 1287 | State = |
| 1288 | setContainerData(State, ContReg, CData->newBegin(OldBeginSym)); |
| 1289 | } else { |
| 1290 | State = setContainerData(State, ContReg, |
| 1291 | ContainerData::fromBegin(OldBeginSym)); |
| 1292 | } |
| 1293 | State = |
| 1294 | setContainerData(State, OldContReg, OldCData->newEnd(nullptr)); |
| 1295 | } |
| 1296 | } else { |
| 1297 | // There was neither "begin" nor "end" symbol assigned yet to the old |
| 1298 | // container, so reassign all iterator positions to the new container. |
| 1299 | State = reassignAllIteratorPositions(State, OldContReg, ContReg); |
| 1300 | } |
| 1301 | } |
| 1302 | } |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 1303 | C.addTransition(State); |
| 1304 | } |
| 1305 | |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 1306 | void IteratorChecker::handleClear(CheckerContext &C, const SVal &Cont) const { |
| 1307 | const auto *ContReg = Cont.getAsRegion(); |
| 1308 | if (!ContReg) |
| 1309 | return; |
| 1310 | |
| Adam Balogh | 42d241f | 2018-12-04 10:22:28 +0000 | [diff] [blame] | 1311 | ContReg = ContReg->getMostDerivedObjectRegion(); |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 1312 | |
| 1313 | // The clear() operation invalidates all the iterators, except the past-end |
| 1314 | // iterators of list-like containers |
| 1315 | auto State = C.getState(); |
| 1316 | if (!hasSubscriptOperator(State, ContReg) || |
| 1317 | !backModifiable(State, ContReg)) { |
| 1318 | const auto CData = getContainerData(State, ContReg); |
| 1319 | if (CData) { |
| 1320 | if (const auto EndSym = CData->getEnd()) { |
| 1321 | State = |
| 1322 | invalidateAllIteratorPositionsExcept(State, ContReg, EndSym, BO_GE); |
| 1323 | C.addTransition(State); |
| 1324 | return; |
| 1325 | } |
| 1326 | } |
| 1327 | } |
| 1328 | State = invalidateAllIteratorPositions(State, ContReg); |
| 1329 | C.addTransition(State); |
| 1330 | } |
| 1331 | |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 1332 | void IteratorChecker::handlePushBack(CheckerContext &C, |
| 1333 | const SVal &Cont) const { |
| 1334 | const auto *ContReg = Cont.getAsRegion(); |
| 1335 | if (!ContReg) |
| 1336 | return; |
| 1337 | |
| Adam Balogh | 42d241f | 2018-12-04 10:22:28 +0000 | [diff] [blame] | 1338 | ContReg = ContReg->getMostDerivedObjectRegion(); |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 1339 | |
| 1340 | // For deque-like containers invalidate all iterator positions |
| 1341 | auto State = C.getState(); |
| 1342 | if (hasSubscriptOperator(State, ContReg) && frontModifiable(State, ContReg)) { |
| 1343 | State = invalidateAllIteratorPositions(State, ContReg); |
| 1344 | C.addTransition(State); |
| 1345 | return; |
| 1346 | } |
| 1347 | |
| 1348 | const auto CData = getContainerData(State, ContReg); |
| 1349 | if (!CData) |
| 1350 | return; |
| 1351 | |
| 1352 | // For vector-like containers invalidate the past-end iterator positions |
| 1353 | if (const auto EndSym = CData->getEnd()) { |
| 1354 | if (hasSubscriptOperator(State, ContReg)) { |
| 1355 | State = invalidateIteratorPositions(State, EndSym, BO_GE); |
| 1356 | } |
| 1357 | auto &SymMgr = C.getSymbolManager(); |
| 1358 | auto &BVF = SymMgr.getBasicVals(); |
| 1359 | auto &SVB = C.getSValBuilder(); |
| 1360 | const auto newEndSym = |
| 1361 | SVB.evalBinOp(State, BO_Add, |
| 1362 | nonloc::SymbolVal(EndSym), |
| 1363 | nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))), |
| 1364 | SymMgr.getType(EndSym)).getAsSymbol(); |
| 1365 | State = setContainerData(State, ContReg, CData->newEnd(newEndSym)); |
| 1366 | } |
| 1367 | C.addTransition(State); |
| 1368 | } |
| 1369 | |
| 1370 | void IteratorChecker::handlePopBack(CheckerContext &C, const SVal &Cont) const { |
| 1371 | const auto *ContReg = Cont.getAsRegion(); |
| 1372 | if (!ContReg) |
| 1373 | return; |
| 1374 | |
| Adam Balogh | 42d241f | 2018-12-04 10:22:28 +0000 | [diff] [blame] | 1375 | ContReg = ContReg->getMostDerivedObjectRegion(); |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 1376 | |
| 1377 | auto State = C.getState(); |
| 1378 | const auto CData = getContainerData(State, ContReg); |
| 1379 | if (!CData) |
| 1380 | return; |
| 1381 | |
| 1382 | if (const auto EndSym = CData->getEnd()) { |
| 1383 | auto &SymMgr = C.getSymbolManager(); |
| 1384 | auto &BVF = SymMgr.getBasicVals(); |
| 1385 | auto &SVB = C.getSValBuilder(); |
| 1386 | const auto BackSym = |
| 1387 | SVB.evalBinOp(State, BO_Sub, |
| 1388 | nonloc::SymbolVal(EndSym), |
| 1389 | nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))), |
| 1390 | SymMgr.getType(EndSym)).getAsSymbol(); |
| 1391 | // For vector-like and deque-like containers invalidate the last and the |
| 1392 | // past-end iterator positions. For list-like containers only invalidate |
| 1393 | // the last position |
| 1394 | if (hasSubscriptOperator(State, ContReg) && |
| 1395 | backModifiable(State, ContReg)) { |
| 1396 | State = invalidateIteratorPositions(State, BackSym, BO_GE); |
| 1397 | State = setContainerData(State, ContReg, CData->newEnd(nullptr)); |
| 1398 | } else { |
| 1399 | State = invalidateIteratorPositions(State, BackSym, BO_EQ); |
| 1400 | } |
| 1401 | auto newEndSym = BackSym; |
| 1402 | State = setContainerData(State, ContReg, CData->newEnd(newEndSym)); |
| 1403 | C.addTransition(State); |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | void IteratorChecker::handlePushFront(CheckerContext &C, |
| 1408 | const SVal &Cont) const { |
| 1409 | const auto *ContReg = Cont.getAsRegion(); |
| 1410 | if (!ContReg) |
| 1411 | return; |
| 1412 | |
| Adam Balogh | 42d241f | 2018-12-04 10:22:28 +0000 | [diff] [blame] | 1413 | ContReg = ContReg->getMostDerivedObjectRegion(); |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 1414 | |
| 1415 | // For deque-like containers invalidate all iterator positions |
| 1416 | auto State = C.getState(); |
| 1417 | if (hasSubscriptOperator(State, ContReg)) { |
| 1418 | State = invalidateAllIteratorPositions(State, ContReg); |
| 1419 | C.addTransition(State); |
| 1420 | } else { |
| 1421 | const auto CData = getContainerData(State, ContReg); |
| 1422 | if (!CData) |
| 1423 | return; |
| 1424 | |
| 1425 | if (const auto BeginSym = CData->getBegin()) { |
| 1426 | auto &SymMgr = C.getSymbolManager(); |
| 1427 | auto &BVF = SymMgr.getBasicVals(); |
| 1428 | auto &SVB = C.getSValBuilder(); |
| 1429 | const auto newBeginSym = |
| 1430 | SVB.evalBinOp(State, BO_Sub, |
| 1431 | nonloc::SymbolVal(BeginSym), |
| 1432 | nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))), |
| 1433 | SymMgr.getType(BeginSym)).getAsSymbol(); |
| 1434 | State = setContainerData(State, ContReg, CData->newBegin(newBeginSym)); |
| 1435 | C.addTransition(State); |
| 1436 | } |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | void IteratorChecker::handlePopFront(CheckerContext &C, |
| 1441 | const SVal &Cont) const { |
| 1442 | const auto *ContReg = Cont.getAsRegion(); |
| 1443 | if (!ContReg) |
| 1444 | return; |
| 1445 | |
| Adam Balogh | 42d241f | 2018-12-04 10:22:28 +0000 | [diff] [blame] | 1446 | ContReg = ContReg->getMostDerivedObjectRegion(); |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 1447 | |
| 1448 | auto State = C.getState(); |
| 1449 | const auto CData = getContainerData(State, ContReg); |
| 1450 | if (!CData) |
| 1451 | return; |
| 1452 | |
| 1453 | // For deque-like containers invalidate all iterator positions. For list-like |
| 1454 | // iterators only invalidate the first position |
| 1455 | if (const auto BeginSym = CData->getBegin()) { |
| 1456 | if (hasSubscriptOperator(State, ContReg)) { |
| 1457 | State = invalidateIteratorPositions(State, BeginSym, BO_LE); |
| 1458 | } else { |
| 1459 | State = invalidateIteratorPositions(State, BeginSym, BO_EQ); |
| 1460 | } |
| 1461 | auto &SymMgr = C.getSymbolManager(); |
| 1462 | auto &BVF = SymMgr.getBasicVals(); |
| 1463 | auto &SVB = C.getSValBuilder(); |
| 1464 | const auto newBeginSym = |
| 1465 | SVB.evalBinOp(State, BO_Add, |
| 1466 | nonloc::SymbolVal(BeginSym), |
| 1467 | nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))), |
| 1468 | SymMgr.getType(BeginSym)).getAsSymbol(); |
| 1469 | State = setContainerData(State, ContReg, CData->newBegin(newBeginSym)); |
| 1470 | C.addTransition(State); |
| 1471 | } |
| 1472 | } |
| 1473 | |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 1474 | void IteratorChecker::handleInsert(CheckerContext &C, const SVal &Iter) const { |
| 1475 | auto State = C.getState(); |
| 1476 | const auto *Pos = getIteratorPosition(State, Iter); |
| 1477 | if (!Pos) |
| 1478 | return; |
| 1479 | |
| 1480 | // For deque-like containers invalidate all iterator positions. For |
| 1481 | // vector-like containers invalidate iterator positions after the insertion. |
| 1482 | const auto *Cont = Pos->getContainer(); |
| 1483 | if (hasSubscriptOperator(State, Cont) && backModifiable(State, Cont)) { |
| 1484 | if (frontModifiable(State, Cont)) { |
| 1485 | State = invalidateAllIteratorPositions(State, Cont); |
| 1486 | } else { |
| 1487 | State = invalidateIteratorPositions(State, Pos->getOffset(), BO_GE); |
| 1488 | } |
| 1489 | if (const auto *CData = getContainerData(State, Cont)) { |
| 1490 | if (const auto EndSym = CData->getEnd()) { |
| 1491 | State = invalidateIteratorPositions(State, EndSym, BO_GE); |
| 1492 | State = setContainerData(State, Cont, CData->newEnd(nullptr)); |
| 1493 | } |
| 1494 | } |
| 1495 | C.addTransition(State); |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | void IteratorChecker::handleErase(CheckerContext &C, const SVal &Iter) const { |
| 1500 | auto State = C.getState(); |
| 1501 | const auto *Pos = getIteratorPosition(State, Iter); |
| 1502 | if (!Pos) |
| 1503 | return; |
| 1504 | |
| 1505 | // For deque-like containers invalidate all iterator positions. For |
| 1506 | // vector-like containers invalidate iterator positions at and after the |
| 1507 | // deletion. For list-like containers only invalidate the deleted position. |
| 1508 | const auto *Cont = Pos->getContainer(); |
| 1509 | if (hasSubscriptOperator(State, Cont) && backModifiable(State, Cont)) { |
| 1510 | if (frontModifiable(State, Cont)) { |
| 1511 | State = invalidateAllIteratorPositions(State, Cont); |
| 1512 | } else { |
| 1513 | State = invalidateIteratorPositions(State, Pos->getOffset(), BO_GE); |
| 1514 | } |
| 1515 | if (const auto *CData = getContainerData(State, Cont)) { |
| 1516 | if (const auto EndSym = CData->getEnd()) { |
| 1517 | State = invalidateIteratorPositions(State, EndSym, BO_GE); |
| 1518 | State = setContainerData(State, Cont, CData->newEnd(nullptr)); |
| 1519 | } |
| 1520 | } |
| 1521 | } else { |
| 1522 | State = invalidateIteratorPositions(State, Pos->getOffset(), BO_EQ); |
| 1523 | } |
| 1524 | C.addTransition(State); |
| 1525 | } |
| 1526 | |
| 1527 | void IteratorChecker::handleErase(CheckerContext &C, const SVal &Iter1, |
| 1528 | const SVal &Iter2) const { |
| 1529 | auto State = C.getState(); |
| 1530 | const auto *Pos1 = getIteratorPosition(State, Iter1); |
| 1531 | const auto *Pos2 = getIteratorPosition(State, Iter2); |
| 1532 | if (!Pos1 || !Pos2) |
| 1533 | return; |
| 1534 | |
| 1535 | // For deque-like containers invalidate all iterator positions. For |
| 1536 | // vector-like containers invalidate iterator positions at and after the |
| 1537 | // deletion range. For list-like containers only invalidate the deleted |
| 1538 | // position range [first..last]. |
| 1539 | const auto *Cont = Pos1->getContainer(); |
| 1540 | if (hasSubscriptOperator(State, Cont) && backModifiable(State, Cont)) { |
| 1541 | if (frontModifiable(State, Cont)) { |
| 1542 | State = invalidateAllIteratorPositions(State, Cont); |
| 1543 | } else { |
| 1544 | State = invalidateIteratorPositions(State, Pos1->getOffset(), BO_GE); |
| 1545 | } |
| 1546 | if (const auto *CData = getContainerData(State, Cont)) { |
| 1547 | if (const auto EndSym = CData->getEnd()) { |
| 1548 | State = invalidateIteratorPositions(State, EndSym, BO_GE); |
| 1549 | State = setContainerData(State, Cont, CData->newEnd(nullptr)); |
| 1550 | } |
| 1551 | } |
| 1552 | } else { |
| 1553 | State = invalidateIteratorPositions(State, Pos1->getOffset(), BO_GE, |
| 1554 | Pos2->getOffset(), BO_LT); |
| 1555 | } |
| 1556 | C.addTransition(State); |
| 1557 | } |
| 1558 | |
| 1559 | void IteratorChecker::handleEraseAfter(CheckerContext &C, |
| 1560 | const SVal &Iter) const { |
| 1561 | auto State = C.getState(); |
| 1562 | const auto *Pos = getIteratorPosition(State, Iter); |
| 1563 | if (!Pos) |
| 1564 | return; |
| 1565 | |
| 1566 | // Invalidate the deleted iterator position, which is the position of the |
| 1567 | // parameter plus one. |
| 1568 | auto &SymMgr = C.getSymbolManager(); |
| 1569 | auto &BVF = SymMgr.getBasicVals(); |
| 1570 | auto &SVB = C.getSValBuilder(); |
| 1571 | const auto NextSym = |
| 1572 | SVB.evalBinOp(State, BO_Add, |
| 1573 | nonloc::SymbolVal(Pos->getOffset()), |
| 1574 | nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))), |
| 1575 | SymMgr.getType(Pos->getOffset())).getAsSymbol(); |
| 1576 | State = invalidateIteratorPositions(State, NextSym, BO_EQ); |
| 1577 | C.addTransition(State); |
| 1578 | } |
| 1579 | |
| 1580 | void IteratorChecker::handleEraseAfter(CheckerContext &C, const SVal &Iter1, |
| 1581 | const SVal &Iter2) const { |
| 1582 | auto State = C.getState(); |
| 1583 | const auto *Pos1 = getIteratorPosition(State, Iter1); |
| 1584 | const auto *Pos2 = getIteratorPosition(State, Iter2); |
| 1585 | if (!Pos1 || !Pos2) |
| 1586 | return; |
| 1587 | |
| 1588 | // Invalidate the deleted iterator position range (first..last) |
| 1589 | State = invalidateIteratorPositions(State, Pos1->getOffset(), BO_GT, |
| 1590 | Pos2->getOffset(), BO_LT); |
| 1591 | C.addTransition(State); |
| 1592 | } |
| 1593 | |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 1594 | IteratorPosition IteratorChecker::advancePosition(CheckerContext &C, |
| 1595 | OverloadedOperatorKind Op, |
| 1596 | const IteratorPosition &Pos, |
| 1597 | const SVal &Distance) const { |
| 1598 | auto State = C.getState(); |
| 1599 | auto &SymMgr = C.getSymbolManager(); |
| 1600 | auto &SVB = C.getSValBuilder(); |
| 1601 | |
| 1602 | assert ((Op == OO_Plus || Op == OO_PlusEqual || |
| 1603 | Op == OO_Minus || Op == OO_MinusEqual) && |
| 1604 | "Advance operator must be one of +, -, += and -=."); |
| 1605 | auto BinOp = (Op == OO_Plus || Op == OO_PlusEqual) ? BO_Add : BO_Sub; |
| 1606 | if (const auto IntDist = Distance.getAs<nonloc::ConcreteInt>()) { |
| 1607 | // For concrete integers we can calculate the new position |
| 1608 | return Pos.setTo(SVB.evalBinOp(State, BinOp, |
| 1609 | nonloc::SymbolVal(Pos.getOffset()), *IntDist, |
| 1610 | SymMgr.getType(Pos.getOffset())) |
| 1611 | .getAsSymbol()); |
| 1612 | } else { |
| 1613 | // For other symbols create a new symbol to keep expressions simple |
| 1614 | const auto &LCtx = C.getLocationContext(); |
| 1615 | const auto NewPosSym = SymMgr.conjureSymbol(nullptr, LCtx, |
| 1616 | SymMgr.getType(Pos.getOffset()), |
| 1617 | C.blockCount()); |
| 1618 | State = assumeNoOverflow(State, NewPosSym, 4); |
| 1619 | return Pos.setTo(NewPosSym); |
| 1620 | } |
| 1621 | } |
| 1622 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 1623 | void IteratorChecker::reportOutOfRangeBug(const StringRef &Message, |
| 1624 | const SVal &Val, CheckerContext &C, |
| 1625 | ExplodedNode *ErrNode) const { |
| Artem Dergachev | 2f169e7 | 2019-09-09 20:34:40 +0000 | [diff] [blame] | 1626 | auto R = std::make_unique<PathSensitiveBugReport>(*OutOfRangeBugType, Message, |
| 1627 | ErrNode); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 1628 | R->markInteresting(Val); |
| 1629 | C.emitReport(std::move(R)); |
| 1630 | } |
| 1631 | |
| Adam Balogh | 21583b7 | 2018-09-10 09:03:22 +0000 | [diff] [blame] | 1632 | void IteratorChecker::reportMismatchedBug(const StringRef &Message, |
| 1633 | const SVal &Val1, const SVal &Val2, |
| 1634 | CheckerContext &C, |
| 1635 | ExplodedNode *ErrNode) const { |
| Artem Dergachev | 2f169e7 | 2019-09-09 20:34:40 +0000 | [diff] [blame] | 1636 | auto R = std::make_unique<PathSensitiveBugReport>(*MismatchedBugType, Message, |
| 1637 | ErrNode); |
| Adam Balogh | 21583b7 | 2018-09-10 09:03:22 +0000 | [diff] [blame] | 1638 | R->markInteresting(Val1); |
| 1639 | R->markInteresting(Val2); |
| 1640 | C.emitReport(std::move(R)); |
| 1641 | } |
| 1642 | |
| Adam Balogh | 3659f7a | 2018-09-10 09:05:31 +0000 | [diff] [blame] | 1643 | void IteratorChecker::reportMismatchedBug(const StringRef &Message, |
| 1644 | const SVal &Val, const MemRegion *Reg, |
| 1645 | CheckerContext &C, |
| 1646 | ExplodedNode *ErrNode) const { |
| Artem Dergachev | 2f169e7 | 2019-09-09 20:34:40 +0000 | [diff] [blame] | 1647 | auto R = std::make_unique<PathSensitiveBugReport>(*MismatchedBugType, Message, |
| 1648 | ErrNode); |
| Adam Balogh | 3659f7a | 2018-09-10 09:05:31 +0000 | [diff] [blame] | 1649 | R->markInteresting(Val); |
| 1650 | R->markInteresting(Reg); |
| 1651 | C.emitReport(std::move(R)); |
| 1652 | } |
| 1653 | |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 1654 | void IteratorChecker::reportInvalidatedBug(const StringRef &Message, |
| 1655 | const SVal &Val, CheckerContext &C, |
| 1656 | ExplodedNode *ErrNode) const { |
| Artem Dergachev | 2f169e7 | 2019-09-09 20:34:40 +0000 | [diff] [blame] | 1657 | auto R = std::make_unique<PathSensitiveBugReport>(*InvalidatedBugType, |
| 1658 | Message, ErrNode); |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 1659 | R->markInteresting(Val); |
| 1660 | C.emitReport(std::move(R)); |
| 1661 | } |
| 1662 | |
| Adam Balogh | 0f88cae | 2019-11-08 08:56:31 +0100 | [diff] [blame] | 1663 | bool IteratorChecker::evalCall(const CallEvent &Call, |
| 1664 | CheckerContext &C) const { |
| 1665 | if (!ChecksEnabled[CK_DebugIteratorModeling]) |
| 1666 | return false; |
| 1667 | |
| 1668 | const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr()); |
| 1669 | if (!CE) |
| 1670 | return false; |
| 1671 | |
| 1672 | const FnCheck *Handler = Callbacks.lookup(Call); |
| 1673 | if (!Handler) |
| 1674 | return false; |
| 1675 | |
| 1676 | (this->**Handler)(CE, C); |
| 1677 | return true; |
| 1678 | } |
| 1679 | |
| 1680 | template <typename Getter> |
| 1681 | void IteratorChecker::analyzerContainerDataField(const CallExpr *CE, |
| 1682 | CheckerContext &C, |
| 1683 | Getter get) const { |
| 1684 | if (CE->getNumArgs() == 0) { |
| 1685 | reportDebugMsg("Missing container argument", C); |
| 1686 | return; |
| 1687 | } |
| 1688 | |
| 1689 | auto State = C.getState(); |
| 1690 | const MemRegion *Cont = C.getSVal(CE->getArg(0)).getAsRegion(); |
| 1691 | if (Cont) { |
| 1692 | const auto *Data = getContainerData(State, Cont); |
| 1693 | if (Data) { |
| 1694 | SymbolRef Field = get(Data); |
| 1695 | if (Field) { |
| 1696 | State = State->BindExpr(CE, C.getLocationContext(), |
| 1697 | nonloc::SymbolVal(Field)); |
| 1698 | C.addTransition(State); |
| 1699 | return; |
| 1700 | } |
| 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | auto &BVF = C.getSValBuilder().getBasicValueFactory(); |
| 1705 | State = State->BindExpr(CE, C.getLocationContext(), |
| 1706 | nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(0)))); |
| 1707 | } |
| 1708 | |
| 1709 | void IteratorChecker::analyzerContainerBegin(const CallExpr *CE, |
| 1710 | CheckerContext &C) const { |
| 1711 | analyzerContainerDataField(CE, C, [](const ContainerData *D) { |
| 1712 | return D->getBegin(); |
| 1713 | }); |
| 1714 | } |
| 1715 | |
| 1716 | void IteratorChecker::analyzerContainerEnd(const CallExpr *CE, |
| 1717 | CheckerContext &C) const { |
| 1718 | analyzerContainerDataField(CE, C, [](const ContainerData *D) { |
| 1719 | return D->getEnd(); |
| 1720 | }); |
| 1721 | } |
| 1722 | |
| 1723 | template <typename Getter> |
| 1724 | void IteratorChecker::analyzerIteratorDataField(const CallExpr *CE, |
| 1725 | CheckerContext &C, |
| 1726 | Getter get, |
| 1727 | SVal Default) const { |
| 1728 | if (CE->getNumArgs() == 0) { |
| 1729 | reportDebugMsg("Missing iterator argument", C); |
| 1730 | return; |
| 1731 | } |
| 1732 | |
| 1733 | auto State = C.getState(); |
| 1734 | SVal V = C.getSVal(CE->getArg(0)); |
| 1735 | const auto *Pos = getIteratorPosition(State, V); |
| 1736 | if (Pos) { |
| 1737 | State = State->BindExpr(CE, C.getLocationContext(), get(Pos)); |
| 1738 | } else { |
| 1739 | State = State->BindExpr(CE, C.getLocationContext(), Default); |
| 1740 | } |
| 1741 | C.addTransition(State); |
| 1742 | } |
| 1743 | |
| 1744 | void IteratorChecker::analyzerIteratorPosition(const CallExpr *CE, |
| 1745 | CheckerContext &C) const { |
| 1746 | auto &BVF = C.getSValBuilder().getBasicValueFactory(); |
| 1747 | analyzerIteratorDataField(CE, C, [](const IteratorPosition *P) { |
| 1748 | return nonloc::SymbolVal(P->getOffset()); |
| 1749 | }, nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(0)))); |
| 1750 | } |
| 1751 | |
| 1752 | void IteratorChecker::analyzerIteratorContainer(const CallExpr *CE, |
| 1753 | CheckerContext &C) const { |
| 1754 | auto &BVF = C.getSValBuilder().getBasicValueFactory(); |
| 1755 | analyzerIteratorDataField(CE, C, [](const IteratorPosition *P) { |
| 1756 | return loc::MemRegionVal(P->getContainer()); |
| 1757 | }, loc::ConcreteInt(BVF.getValue(llvm::APSInt::get(0)))); |
| 1758 | } |
| 1759 | |
| 1760 | void IteratorChecker::analyzerIteratorValidity(const CallExpr *CE, |
| 1761 | CheckerContext &C) const { |
| 1762 | auto &BVF = C.getSValBuilder().getBasicValueFactory(); |
| 1763 | analyzerIteratorDataField(CE, C, [&BVF](const IteratorPosition *P) { |
| 1764 | return |
| 1765 | nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get((P->isValid())))); |
| 1766 | }, nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(0)))); |
| 1767 | } |
| 1768 | |
| 1769 | ExplodedNode *IteratorChecker::reportDebugMsg(llvm::StringRef Msg, |
| 1770 | CheckerContext &C) const { |
| 1771 | ExplodedNode *N = C.generateNonFatalErrorNode(); |
| 1772 | if (!N) |
| 1773 | return nullptr; |
| 1774 | |
| 1775 | auto &BR = C.getBugReporter(); |
| 1776 | BR.emitReport(std::make_unique<PathSensitiveBugReport>(*DebugMsgBugType, |
| 1777 | Msg, N)); |
| 1778 | return N; |
| 1779 | } |
| 1780 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 1781 | namespace { |
| 1782 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1783 | bool isLess(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2); |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 1784 | bool isGreater(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2); |
| 1785 | bool isEqual(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 1786 | bool compare(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2, |
| 1787 | BinaryOperator::Opcode Opc); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1788 | bool compare(ProgramStateRef State, NonLoc NL1, NonLoc NL2, |
| 1789 | BinaryOperator::Opcode Opc); |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 1790 | const CXXRecordDecl *getCXXRecordDecl(ProgramStateRef State, |
| 1791 | const MemRegion *Reg); |
| Adam Balogh | 6b23b1a | 2018-09-10 09:04:27 +0000 | [diff] [blame] | 1792 | SymbolRef rebaseSymbol(ProgramStateRef State, SValBuilder &SVB, SymbolRef Expr, |
| 1793 | SymbolRef OldSym, SymbolRef NewSym); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 1794 | |
| 1795 | bool isIteratorType(const QualType &Type) { |
| 1796 | if (Type->isPointerType()) |
| 1797 | return true; |
| 1798 | |
| 1799 | const auto *CRD = Type->getUnqualifiedDesugaredType()->getAsCXXRecordDecl(); |
| 1800 | return isIterator(CRD); |
| 1801 | } |
| 1802 | |
| 1803 | bool isIterator(const CXXRecordDecl *CRD) { |
| 1804 | if (!CRD) |
| 1805 | return false; |
| 1806 | |
| 1807 | const auto Name = CRD->getName(); |
| 1808 | if (!(Name.endswith_lower("iterator") || Name.endswith_lower("iter") || |
| 1809 | Name.endswith_lower("it"))) |
| 1810 | return false; |
| 1811 | |
| 1812 | bool HasCopyCtor = false, HasCopyAssign = true, HasDtor = false, |
| 1813 | HasPreIncrOp = false, HasPostIncrOp = false, HasDerefOp = false; |
| 1814 | for (const auto *Method : CRD->methods()) { |
| 1815 | if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Method)) { |
| 1816 | if (Ctor->isCopyConstructor()) { |
| 1817 | HasCopyCtor = !Ctor->isDeleted() && Ctor->getAccess() == AS_public; |
| 1818 | } |
| 1819 | continue; |
| 1820 | } |
| 1821 | if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(Method)) { |
| 1822 | HasDtor = !Dtor->isDeleted() && Dtor->getAccess() == AS_public; |
| 1823 | continue; |
| 1824 | } |
| 1825 | if (Method->isCopyAssignmentOperator()) { |
| 1826 | HasCopyAssign = !Method->isDeleted() && Method->getAccess() == AS_public; |
| 1827 | continue; |
| 1828 | } |
| 1829 | if (!Method->isOverloadedOperator()) |
| 1830 | continue; |
| 1831 | const auto OPK = Method->getOverloadedOperator(); |
| 1832 | if (OPK == OO_PlusPlus) { |
| 1833 | HasPreIncrOp = HasPreIncrOp || (Method->getNumParams() == 0); |
| 1834 | HasPostIncrOp = HasPostIncrOp || (Method->getNumParams() == 1); |
| 1835 | continue; |
| 1836 | } |
| 1837 | if (OPK == OO_Star) { |
| 1838 | HasDerefOp = (Method->getNumParams() == 0); |
| 1839 | continue; |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | return HasCopyCtor && HasCopyAssign && HasDtor && HasPreIncrOp && |
| 1844 | HasPostIncrOp && HasDerefOp; |
| 1845 | } |
| 1846 | |
| Adam Balogh | 3659f7a | 2018-09-10 09:05:31 +0000 | [diff] [blame] | 1847 | bool isComparisonOperator(OverloadedOperatorKind OK) { |
| 1848 | return OK == OO_EqualEqual || OK == OO_ExclaimEqual || OK == OO_Less || |
| 1849 | OK == OO_LessEqual || OK == OO_Greater || OK == OO_GreaterEqual; |
| 1850 | } |
| 1851 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 1852 | bool isBeginCall(const FunctionDecl *Func) { |
| 1853 | const auto *IdInfo = Func->getIdentifier(); |
| 1854 | if (!IdInfo) |
| 1855 | return false; |
| 1856 | return IdInfo->getName().endswith_lower("begin"); |
| 1857 | } |
| 1858 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 1859 | bool isEndCall(const FunctionDecl *Func) { |
| 1860 | const auto *IdInfo = Func->getIdentifier(); |
| 1861 | if (!IdInfo) |
| 1862 | return false; |
| 1863 | return IdInfo->getName().endswith_lower("end"); |
| 1864 | } |
| 1865 | |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 1866 | bool isAssignCall(const FunctionDecl *Func) { |
| 1867 | const auto *IdInfo = Func->getIdentifier(); |
| 1868 | if (!IdInfo) |
| 1869 | return false; |
| 1870 | if (Func->getNumParams() > 2) |
| 1871 | return false; |
| 1872 | return IdInfo->getName() == "assign"; |
| 1873 | } |
| 1874 | |
| 1875 | bool isClearCall(const FunctionDecl *Func) { |
| 1876 | const auto *IdInfo = Func->getIdentifier(); |
| 1877 | if (!IdInfo) |
| 1878 | return false; |
| 1879 | if (Func->getNumParams() > 0) |
| 1880 | return false; |
| 1881 | return IdInfo->getName() == "clear"; |
| 1882 | } |
| 1883 | |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 1884 | bool isPushBackCall(const FunctionDecl *Func) { |
| 1885 | const auto *IdInfo = Func->getIdentifier(); |
| 1886 | if (!IdInfo) |
| 1887 | return false; |
| 1888 | if (Func->getNumParams() != 1) |
| 1889 | return false; |
| 1890 | return IdInfo->getName() == "push_back"; |
| 1891 | } |
| 1892 | |
| 1893 | bool isEmplaceBackCall(const FunctionDecl *Func) { |
| 1894 | const auto *IdInfo = Func->getIdentifier(); |
| 1895 | if (!IdInfo) |
| 1896 | return false; |
| 1897 | if (Func->getNumParams() < 1) |
| 1898 | return false; |
| 1899 | return IdInfo->getName() == "emplace_back"; |
| 1900 | } |
| 1901 | |
| 1902 | bool isPopBackCall(const FunctionDecl *Func) { |
| 1903 | const auto *IdInfo = Func->getIdentifier(); |
| 1904 | if (!IdInfo) |
| 1905 | return false; |
| 1906 | if (Func->getNumParams() > 0) |
| 1907 | return false; |
| 1908 | return IdInfo->getName() == "pop_back"; |
| 1909 | } |
| 1910 | |
| 1911 | bool isPushFrontCall(const FunctionDecl *Func) { |
| 1912 | const auto *IdInfo = Func->getIdentifier(); |
| 1913 | if (!IdInfo) |
| 1914 | return false; |
| 1915 | if (Func->getNumParams() != 1) |
| 1916 | return false; |
| 1917 | return IdInfo->getName() == "push_front"; |
| 1918 | } |
| 1919 | |
| 1920 | bool isEmplaceFrontCall(const FunctionDecl *Func) { |
| 1921 | const auto *IdInfo = Func->getIdentifier(); |
| 1922 | if (!IdInfo) |
| 1923 | return false; |
| 1924 | if (Func->getNumParams() < 1) |
| 1925 | return false; |
| 1926 | return IdInfo->getName() == "emplace_front"; |
| 1927 | } |
| 1928 | |
| 1929 | bool isPopFrontCall(const FunctionDecl *Func) { |
| 1930 | const auto *IdInfo = Func->getIdentifier(); |
| 1931 | if (!IdInfo) |
| 1932 | return false; |
| 1933 | if (Func->getNumParams() > 0) |
| 1934 | return false; |
| 1935 | return IdInfo->getName() == "pop_front"; |
| 1936 | } |
| 1937 | |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 1938 | bool isInsertCall(const FunctionDecl *Func) { |
| 1939 | const auto *IdInfo = Func->getIdentifier(); |
| 1940 | if (!IdInfo) |
| 1941 | return false; |
| 1942 | if (Func->getNumParams() < 2 || Func->getNumParams() > 3) |
| 1943 | return false; |
| 1944 | if (!isIteratorType(Func->getParamDecl(0)->getType())) |
| 1945 | return false; |
| 1946 | return IdInfo->getName() == "insert"; |
| 1947 | } |
| 1948 | |
| 1949 | bool isEmplaceCall(const FunctionDecl *Func) { |
| 1950 | const auto *IdInfo = Func->getIdentifier(); |
| 1951 | if (!IdInfo) |
| 1952 | return false; |
| 1953 | if (Func->getNumParams() < 2) |
| 1954 | return false; |
| 1955 | if (!isIteratorType(Func->getParamDecl(0)->getType())) |
| 1956 | return false; |
| 1957 | return IdInfo->getName() == "emplace"; |
| 1958 | } |
| 1959 | |
| 1960 | bool isEraseCall(const FunctionDecl *Func) { |
| 1961 | const auto *IdInfo = Func->getIdentifier(); |
| 1962 | if (!IdInfo) |
| 1963 | return false; |
| 1964 | if (Func->getNumParams() < 1 || Func->getNumParams() > 2) |
| 1965 | return false; |
| 1966 | if (!isIteratorType(Func->getParamDecl(0)->getType())) |
| 1967 | return false; |
| 1968 | if (Func->getNumParams() == 2 && |
| 1969 | !isIteratorType(Func->getParamDecl(1)->getType())) |
| 1970 | return false; |
| 1971 | return IdInfo->getName() == "erase"; |
| 1972 | } |
| 1973 | |
| 1974 | bool isEraseAfterCall(const FunctionDecl *Func) { |
| 1975 | const auto *IdInfo = Func->getIdentifier(); |
| 1976 | if (!IdInfo) |
| 1977 | return false; |
| 1978 | if (Func->getNumParams() < 1 || Func->getNumParams() > 2) |
| 1979 | return false; |
| 1980 | if (!isIteratorType(Func->getParamDecl(0)->getType())) |
| 1981 | return false; |
| 1982 | if (Func->getNumParams() == 2 && |
| 1983 | !isIteratorType(Func->getParamDecl(1)->getType())) |
| 1984 | return false; |
| 1985 | return IdInfo->getName() == "erase_after"; |
| 1986 | } |
| 1987 | |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 1988 | bool isAssignmentOperator(OverloadedOperatorKind OK) { return OK == OO_Equal; } |
| 1989 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 1990 | bool isSimpleComparisonOperator(OverloadedOperatorKind OK) { |
| 1991 | return OK == OO_EqualEqual || OK == OO_ExclaimEqual; |
| 1992 | } |
| 1993 | |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 1994 | bool isAccessOperator(OverloadedOperatorKind OK) { |
| 1995 | return isDereferenceOperator(OK) || isIncrementOperator(OK) || |
| 1996 | isDecrementOperator(OK) || isRandomIncrOrDecrOperator(OK); |
| 1997 | } |
| 1998 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 1999 | bool isDereferenceOperator(OverloadedOperatorKind OK) { |
| 2000 | return OK == OO_Star || OK == OO_Arrow || OK == OO_ArrowStar || |
| 2001 | OK == OO_Subscript; |
| 2002 | } |
| 2003 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 2004 | bool isIncrementOperator(OverloadedOperatorKind OK) { |
| 2005 | return OK == OO_PlusPlus; |
| 2006 | } |
| 2007 | |
| 2008 | bool isDecrementOperator(OverloadedOperatorKind OK) { |
| 2009 | return OK == OO_MinusMinus; |
| 2010 | } |
| 2011 | |
| 2012 | bool isRandomIncrOrDecrOperator(OverloadedOperatorKind OK) { |
| 2013 | return OK == OO_Plus || OK == OO_PlusEqual || OK == OO_Minus || |
| 2014 | OK == OO_MinusEqual; |
| 2015 | } |
| 2016 | |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 2017 | bool hasSubscriptOperator(ProgramStateRef State, const MemRegion *Reg) { |
| 2018 | const auto *CRD = getCXXRecordDecl(State, Reg); |
| 2019 | if (!CRD) |
| 2020 | return false; |
| 2021 | |
| 2022 | for (const auto *Method : CRD->methods()) { |
| 2023 | if (!Method->isOverloadedOperator()) |
| 2024 | continue; |
| 2025 | const auto OPK = Method->getOverloadedOperator(); |
| 2026 | if (OPK == OO_Subscript) { |
| 2027 | return true; |
| 2028 | } |
| 2029 | } |
| 2030 | return false; |
| 2031 | } |
| 2032 | |
| 2033 | bool frontModifiable(ProgramStateRef State, const MemRegion *Reg) { |
| 2034 | const auto *CRD = getCXXRecordDecl(State, Reg); |
| 2035 | if (!CRD) |
| 2036 | return false; |
| 2037 | |
| 2038 | for (const auto *Method : CRD->methods()) { |
| 2039 | if (!Method->getDeclName().isIdentifier()) |
| 2040 | continue; |
| 2041 | if (Method->getName() == "push_front" || Method->getName() == "pop_front") { |
| 2042 | return true; |
| 2043 | } |
| 2044 | } |
| 2045 | return false; |
| 2046 | } |
| 2047 | |
| 2048 | bool backModifiable(ProgramStateRef State, const MemRegion *Reg) { |
| 2049 | const auto *CRD = getCXXRecordDecl(State, Reg); |
| 2050 | if (!CRD) |
| 2051 | return false; |
| 2052 | |
| 2053 | for (const auto *Method : CRD->methods()) { |
| 2054 | if (!Method->getDeclName().isIdentifier()) |
| 2055 | continue; |
| 2056 | if (Method->getName() == "push_back" || Method->getName() == "pop_back") { |
| 2057 | return true; |
| 2058 | } |
| 2059 | } |
| 2060 | return false; |
| 2061 | } |
| 2062 | |
| 2063 | const CXXRecordDecl *getCXXRecordDecl(ProgramStateRef State, |
| 2064 | const MemRegion *Reg) { |
| 2065 | auto TI = getDynamicTypeInfo(State, Reg); |
| 2066 | if (!TI.isValid()) |
| 2067 | return nullptr; |
| 2068 | |
| 2069 | auto Type = TI.getType(); |
| 2070 | if (const auto *RefT = Type->getAs<ReferenceType>()) { |
| 2071 | Type = RefT->getPointeeType(); |
| 2072 | } |
| 2073 | |
| 2074 | return Type->getUnqualifiedDesugaredType()->getAsCXXRecordDecl(); |
| 2075 | } |
| 2076 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 2077 | SymbolRef getContainerBegin(ProgramStateRef State, const MemRegion *Cont) { |
| 2078 | const auto *CDataPtr = getContainerData(State, Cont); |
| 2079 | if (!CDataPtr) |
| 2080 | return nullptr; |
| 2081 | |
| 2082 | return CDataPtr->getBegin(); |
| 2083 | } |
| 2084 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2085 | SymbolRef getContainerEnd(ProgramStateRef State, const MemRegion *Cont) { |
| 2086 | const auto *CDataPtr = getContainerData(State, Cont); |
| 2087 | if (!CDataPtr) |
| 2088 | return nullptr; |
| 2089 | |
| 2090 | return CDataPtr->getEnd(); |
| 2091 | } |
| 2092 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 2093 | ProgramStateRef createContainerBegin(ProgramStateRef State, |
| Adam Balogh | 33160c4 | 2019-05-20 11:04:27 +0000 | [diff] [blame] | 2094 | const MemRegion *Cont, const Expr *E, |
| 2095 | QualType T, const LocationContext *LCtx, |
| 2096 | unsigned BlockCount) { |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 2097 | // Only create if it does not exist |
| 2098 | const auto *CDataPtr = getContainerData(State, Cont); |
| Adam Balogh | 33160c4 | 2019-05-20 11:04:27 +0000 | [diff] [blame] | 2099 | if (CDataPtr && CDataPtr->getBegin()) |
| 2100 | return State; |
| 2101 | |
| 2102 | auto &SymMgr = State->getSymbolManager(); |
| 2103 | const SymbolConjured *Sym = SymMgr.conjureSymbol(E, LCtx, T, BlockCount, |
| 2104 | "begin"); |
| 2105 | State = assumeNoOverflow(State, Sym, 4); |
| 2106 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 2107 | if (CDataPtr) { |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 2108 | const auto CData = CDataPtr->newBegin(Sym); |
| 2109 | return setContainerData(State, Cont, CData); |
| 2110 | } |
| Adam Balogh | 33160c4 | 2019-05-20 11:04:27 +0000 | [diff] [blame] | 2111 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 2112 | const auto CData = ContainerData::fromBegin(Sym); |
| 2113 | return setContainerData(State, Cont, CData); |
| 2114 | } |
| 2115 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2116 | ProgramStateRef createContainerEnd(ProgramStateRef State, const MemRegion *Cont, |
| Adam Balogh | 33160c4 | 2019-05-20 11:04:27 +0000 | [diff] [blame] | 2117 | const Expr *E, QualType T, |
| 2118 | const LocationContext *LCtx, |
| 2119 | unsigned BlockCount) { |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2120 | // Only create if it does not exist |
| 2121 | const auto *CDataPtr = getContainerData(State, Cont); |
| Adam Balogh | 33160c4 | 2019-05-20 11:04:27 +0000 | [diff] [blame] | 2122 | if (CDataPtr && CDataPtr->getEnd()) |
| 2123 | return State; |
| 2124 | |
| 2125 | auto &SymMgr = State->getSymbolManager(); |
| 2126 | const SymbolConjured *Sym = SymMgr.conjureSymbol(E, LCtx, T, BlockCount, |
| 2127 | "end"); |
| 2128 | State = assumeNoOverflow(State, Sym, 4); |
| 2129 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2130 | if (CDataPtr) { |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 2131 | const auto CData = CDataPtr->newEnd(Sym); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2132 | return setContainerData(State, Cont, CData); |
| 2133 | } |
| Adam Balogh | 33160c4 | 2019-05-20 11:04:27 +0000 | [diff] [blame] | 2134 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 2135 | const auto CData = ContainerData::fromEnd(Sym); |
| 2136 | return setContainerData(State, Cont, CData); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | const ContainerData *getContainerData(ProgramStateRef State, |
| 2140 | const MemRegion *Cont) { |
| 2141 | return State->get<ContainerMap>(Cont); |
| 2142 | } |
| 2143 | |
| 2144 | ProgramStateRef setContainerData(ProgramStateRef State, const MemRegion *Cont, |
| 2145 | const ContainerData &CData) { |
| 2146 | return State->set<ContainerMap>(Cont, CData); |
| 2147 | } |
| 2148 | |
| 2149 | const IteratorPosition *getIteratorPosition(ProgramStateRef State, |
| 2150 | const SVal &Val) { |
| Adam Balogh | 42d241f | 2018-12-04 10:22:28 +0000 | [diff] [blame] | 2151 | if (auto Reg = Val.getAsRegion()) { |
| 2152 | Reg = Reg->getMostDerivedObjectRegion(); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2153 | return State->get<IteratorRegionMap>(Reg); |
| 2154 | } else if (const auto Sym = Val.getAsSymbol()) { |
| 2155 | return State->get<IteratorSymbolMap>(Sym); |
| 2156 | } else if (const auto LCVal = Val.getAs<nonloc::LazyCompoundVal>()) { |
| 2157 | return State->get<IteratorRegionMap>(LCVal->getRegion()); |
| 2158 | } |
| 2159 | return nullptr; |
| 2160 | } |
| 2161 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2162 | ProgramStateRef setIteratorPosition(ProgramStateRef State, const SVal &Val, |
| 2163 | const IteratorPosition &Pos) { |
| Adam Balogh | 42d241f | 2018-12-04 10:22:28 +0000 | [diff] [blame] | 2164 | if (auto Reg = Val.getAsRegion()) { |
| 2165 | Reg = Reg->getMostDerivedObjectRegion(); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2166 | return State->set<IteratorRegionMap>(Reg, Pos); |
| 2167 | } else if (const auto Sym = Val.getAsSymbol()) { |
| 2168 | return State->set<IteratorSymbolMap>(Sym, Pos); |
| 2169 | } else if (const auto LCVal = Val.getAs<nonloc::LazyCompoundVal>()) { |
| 2170 | return State->set<IteratorRegionMap>(LCVal->getRegion(), Pos); |
| 2171 | } |
| 2172 | return nullptr; |
| 2173 | } |
| 2174 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2175 | ProgramStateRef removeIteratorPosition(ProgramStateRef State, const SVal &Val) { |
| Adam Balogh | 42d241f | 2018-12-04 10:22:28 +0000 | [diff] [blame] | 2176 | if (auto Reg = Val.getAsRegion()) { |
| 2177 | Reg = Reg->getMostDerivedObjectRegion(); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2178 | return State->remove<IteratorRegionMap>(Reg); |
| 2179 | } else if (const auto Sym = Val.getAsSymbol()) { |
| 2180 | return State->remove<IteratorSymbolMap>(Sym); |
| 2181 | } else if (const auto LCVal = Val.getAs<nonloc::LazyCompoundVal>()) { |
| 2182 | return State->remove<IteratorRegionMap>(LCVal->getRegion()); |
| 2183 | } |
| 2184 | return nullptr; |
| 2185 | } |
| 2186 | |
| Adam Balogh | 54976e7 | 2019-04-23 07:15:55 +0000 | [diff] [blame] | 2187 | ProgramStateRef relateSymbols(ProgramStateRef State, SymbolRef Sym1, |
| 2188 | SymbolRef Sym2, bool Equal) { |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2189 | auto &SVB = State->getStateManager().getSValBuilder(); |
| Adam Balogh | 0a7592b | 2018-07-16 09:27:27 +0000 | [diff] [blame] | 2190 | |
| 2191 | // FIXME: This code should be reworked as follows: |
| 2192 | // 1. Subtract the operands using evalBinOp(). |
| 2193 | // 2. Assume that the result doesn't overflow. |
| 2194 | // 3. Compare the result to 0. |
| 2195 | // 4. Assume the result of the comparison. |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2196 | const auto comparison = |
| Adam Balogh | 54976e7 | 2019-04-23 07:15:55 +0000 | [diff] [blame] | 2197 | SVB.evalBinOp(State, BO_EQ, nonloc::SymbolVal(Sym1), |
| 2198 | nonloc::SymbolVal(Sym2), SVB.getConditionType()); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 2199 | |
| Adam Balogh | 0a7592b | 2018-07-16 09:27:27 +0000 | [diff] [blame] | 2200 | assert(comparison.getAs<DefinedSVal>() && |
| 2201 | "Symbol comparison must be a `DefinedSVal`"); |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 2202 | |
| Adam Balogh | 0a7592b | 2018-07-16 09:27:27 +0000 | [diff] [blame] | 2203 | auto NewState = State->assume(comparison.castAs<DefinedSVal>(), Equal); |
| Adam Balogh | 54976e7 | 2019-04-23 07:15:55 +0000 | [diff] [blame] | 2204 | if (!NewState) |
| 2205 | return nullptr; |
| 2206 | |
| Adam Balogh | 0a7592b | 2018-07-16 09:27:27 +0000 | [diff] [blame] | 2207 | if (const auto CompSym = comparison.getAsSymbol()) { |
| 2208 | assert(isa<SymIntExpr>(CompSym) && |
| 2209 | "Symbol comparison must be a `SymIntExpr`"); |
| 2210 | assert(BinaryOperator::isComparisonOp( |
| 2211 | cast<SymIntExpr>(CompSym)->getOpcode()) && |
| 2212 | "Symbol comparison must be a comparison"); |
| 2213 | return assumeNoOverflow(NewState, cast<SymIntExpr>(CompSym)->getLHS(), 2); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
| Adam Balogh | 0a7592b | 2018-07-16 09:27:27 +0000 | [diff] [blame] | 2216 | return NewState; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
| Adam Balogh | a692120 | 2018-07-30 08:52:21 +0000 | [diff] [blame] | 2219 | bool hasLiveIterators(ProgramStateRef State, const MemRegion *Cont) { |
| 2220 | auto RegionMap = State->get<IteratorRegionMap>(); |
| 2221 | for (const auto Reg : RegionMap) { |
| 2222 | if (Reg.second.getContainer() == Cont) |
| 2223 | return true; |
| 2224 | } |
| 2225 | |
| 2226 | auto SymbolMap = State->get<IteratorSymbolMap>(); |
| 2227 | for (const auto Sym : SymbolMap) { |
| 2228 | if (Sym.second.getContainer() == Cont) |
| 2229 | return true; |
| 2230 | } |
| 2231 | |
| 2232 | return false; |
| 2233 | } |
| 2234 | |
| Adam Balogh | 21583b7 | 2018-09-10 09:03:22 +0000 | [diff] [blame] | 2235 | bool isBoundThroughLazyCompoundVal(const Environment &Env, |
| 2236 | const MemRegion *Reg) { |
| 2237 | for (const auto Binding: Env) { |
| 2238 | if (const auto LCVal = Binding.second.getAs<nonloc::LazyCompoundVal>()) { |
| 2239 | if (LCVal->getRegion() == Reg) |
| 2240 | return true; |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | return false; |
| 2245 | } |
| 2246 | |
| Adam Balogh | 54976e7 | 2019-04-23 07:15:55 +0000 | [diff] [blame] | 2247 | // This function tells the analyzer's engine that symbols produced by our |
| 2248 | // checker, most notably iterator positions, are relatively small. |
| 2249 | // A distance between items in the container should not be very large. |
| 2250 | // By assuming that it is within around 1/8 of the address space, |
| 2251 | // we can help the analyzer perform operations on these symbols |
| 2252 | // without being afraid of integer overflows. |
| 2253 | // FIXME: Should we provide it as an API, so that all checkers could use it? |
| 2254 | ProgramStateRef assumeNoOverflow(ProgramStateRef State, SymbolRef Sym, |
| 2255 | long Scale) { |
| 2256 | SValBuilder &SVB = State->getStateManager().getSValBuilder(); |
| 2257 | BasicValueFactory &BV = SVB.getBasicValueFactory(); |
| 2258 | |
| 2259 | QualType T = Sym->getType(); |
| 2260 | assert(T->isSignedIntegerOrEnumerationType()); |
| 2261 | APSIntType AT = BV.getAPSIntType(T); |
| 2262 | |
| 2263 | ProgramStateRef NewState = State; |
| 2264 | |
| 2265 | llvm::APSInt Max = AT.getMaxValue() / AT.getValue(Scale); |
| 2266 | SVal IsCappedFromAbove = |
| 2267 | SVB.evalBinOpNN(State, BO_LE, nonloc::SymbolVal(Sym), |
| 2268 | nonloc::ConcreteInt(Max), SVB.getConditionType()); |
| 2269 | if (auto DV = IsCappedFromAbove.getAs<DefinedSVal>()) { |
| 2270 | NewState = NewState->assume(*DV, true); |
| 2271 | if (!NewState) |
| 2272 | return State; |
| 2273 | } |
| 2274 | |
| 2275 | llvm::APSInt Min = -Max; |
| 2276 | SVal IsCappedFromBelow = |
| 2277 | SVB.evalBinOpNN(State, BO_GE, nonloc::SymbolVal(Sym), |
| 2278 | nonloc::ConcreteInt(Min), SVB.getConditionType()); |
| 2279 | if (auto DV = IsCappedFromBelow.getAs<DefinedSVal>()) { |
| 2280 | NewState = NewState->assume(*DV, true); |
| 2281 | if (!NewState) |
| 2282 | return State; |
| 2283 | } |
| 2284 | |
| 2285 | return NewState; |
| 2286 | } |
| 2287 | |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 2288 | template <typename Condition, typename Process> |
| 2289 | ProgramStateRef processIteratorPositions(ProgramStateRef State, Condition Cond, |
| 2290 | Process Proc) { |
| 2291 | auto &RegionMapFactory = State->get_context<IteratorRegionMap>(); |
| 2292 | auto RegionMap = State->get<IteratorRegionMap>(); |
| 2293 | bool Changed = false; |
| 2294 | for (const auto Reg : RegionMap) { |
| 2295 | if (Cond(Reg.second)) { |
| 2296 | RegionMap = RegionMapFactory.add(RegionMap, Reg.first, Proc(Reg.second)); |
| 2297 | Changed = true; |
| 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | if (Changed) |
| 2302 | State = State->set<IteratorRegionMap>(RegionMap); |
| 2303 | |
| 2304 | auto &SymbolMapFactory = State->get_context<IteratorSymbolMap>(); |
| 2305 | auto SymbolMap = State->get<IteratorSymbolMap>(); |
| 2306 | Changed = false; |
| 2307 | for (const auto Sym : SymbolMap) { |
| 2308 | if (Cond(Sym.second)) { |
| 2309 | SymbolMap = SymbolMapFactory.add(SymbolMap, Sym.first, Proc(Sym.second)); |
| 2310 | Changed = true; |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | if (Changed) |
| 2315 | State = State->set<IteratorSymbolMap>(SymbolMap); |
| 2316 | |
| 2317 | return State; |
| 2318 | } |
| 2319 | |
| 2320 | ProgramStateRef invalidateAllIteratorPositions(ProgramStateRef State, |
| 2321 | const MemRegion *Cont) { |
| 2322 | auto MatchCont = [&](const IteratorPosition &Pos) { |
| 2323 | return Pos.getContainer() == Cont; |
| 2324 | }; |
| 2325 | auto Invalidate = [&](const IteratorPosition &Pos) { |
| 2326 | return Pos.invalidate(); |
| 2327 | }; |
| 2328 | return processIteratorPositions(State, MatchCont, Invalidate); |
| 2329 | } |
| 2330 | |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 2331 | ProgramStateRef |
| 2332 | invalidateAllIteratorPositionsExcept(ProgramStateRef State, |
| 2333 | const MemRegion *Cont, SymbolRef Offset, |
| 2334 | BinaryOperator::Opcode Opc) { |
| 2335 | auto MatchContAndCompare = [&](const IteratorPosition &Pos) { |
| 2336 | return Pos.getContainer() == Cont && |
| 2337 | !compare(State, Pos.getOffset(), Offset, Opc); |
| 2338 | }; |
| 2339 | auto Invalidate = [&](const IteratorPosition &Pos) { |
| 2340 | return Pos.invalidate(); |
| 2341 | }; |
| 2342 | return processIteratorPositions(State, MatchContAndCompare, Invalidate); |
| 2343 | } |
| 2344 | |
| Adam Balogh | 9a48ba6 | 2018-09-10 09:06:31 +0000 | [diff] [blame] | 2345 | ProgramStateRef invalidateIteratorPositions(ProgramStateRef State, |
| 2346 | SymbolRef Offset, |
| 2347 | BinaryOperator::Opcode Opc) { |
| 2348 | auto Compare = [&](const IteratorPosition &Pos) { |
| 2349 | return compare(State, Pos.getOffset(), Offset, Opc); |
| 2350 | }; |
| 2351 | auto Invalidate = [&](const IteratorPosition &Pos) { |
| 2352 | return Pos.invalidate(); |
| 2353 | }; |
| 2354 | return processIteratorPositions(State, Compare, Invalidate); |
| 2355 | } |
| 2356 | |
| Adam Balogh | 2e7cb34 | 2018-09-10 09:07:47 +0000 | [diff] [blame] | 2357 | ProgramStateRef invalidateIteratorPositions(ProgramStateRef State, |
| 2358 | SymbolRef Offset1, |
| 2359 | BinaryOperator::Opcode Opc1, |
| 2360 | SymbolRef Offset2, |
| 2361 | BinaryOperator::Opcode Opc2) { |
| 2362 | auto Compare = [&](const IteratorPosition &Pos) { |
| 2363 | return compare(State, Pos.getOffset(), Offset1, Opc1) && |
| 2364 | compare(State, Pos.getOffset(), Offset2, Opc2); |
| 2365 | }; |
| 2366 | auto Invalidate = [&](const IteratorPosition &Pos) { |
| 2367 | return Pos.invalidate(); |
| 2368 | }; |
| 2369 | return processIteratorPositions(State, Compare, Invalidate); |
| 2370 | } |
| 2371 | |
| Adam Balogh | 6b23b1a | 2018-09-10 09:04:27 +0000 | [diff] [blame] | 2372 | ProgramStateRef reassignAllIteratorPositions(ProgramStateRef State, |
| 2373 | const MemRegion *Cont, |
| 2374 | const MemRegion *NewCont) { |
| 2375 | auto MatchCont = [&](const IteratorPosition &Pos) { |
| 2376 | return Pos.getContainer() == Cont; |
| 2377 | }; |
| 2378 | auto ReAssign = [&](const IteratorPosition &Pos) { |
| 2379 | return Pos.reAssign(NewCont); |
| 2380 | }; |
| 2381 | return processIteratorPositions(State, MatchCont, ReAssign); |
| 2382 | } |
| 2383 | |
| 2384 | ProgramStateRef reassignAllIteratorPositionsUnless(ProgramStateRef State, |
| 2385 | const MemRegion *Cont, |
| 2386 | const MemRegion *NewCont, |
| 2387 | SymbolRef Offset, |
| 2388 | BinaryOperator::Opcode Opc) { |
| 2389 | auto MatchContAndCompare = [&](const IteratorPosition &Pos) { |
| 2390 | return Pos.getContainer() == Cont && |
| 2391 | !compare(State, Pos.getOffset(), Offset, Opc); |
| 2392 | }; |
| 2393 | auto ReAssign = [&](const IteratorPosition &Pos) { |
| 2394 | return Pos.reAssign(NewCont); |
| 2395 | }; |
| 2396 | return processIteratorPositions(State, MatchContAndCompare, ReAssign); |
| 2397 | } |
| 2398 | |
| 2399 | // This function rebases symbolic expression `OldSym + Int` to `NewSym + Int`, |
| 2400 | // `OldSym - Int` to `NewSym - Int` and `OldSym` to `NewSym` in any iterator |
| 2401 | // position offsets where `CondSym` is true. |
| 2402 | ProgramStateRef rebaseSymbolInIteratorPositionsIf( |
| 2403 | ProgramStateRef State, SValBuilder &SVB, SymbolRef OldSym, |
| 2404 | SymbolRef NewSym, SymbolRef CondSym, BinaryOperator::Opcode Opc) { |
| 2405 | auto LessThanEnd = [&](const IteratorPosition &Pos) { |
| 2406 | return compare(State, Pos.getOffset(), CondSym, Opc); |
| 2407 | }; |
| 2408 | auto RebaseSymbol = [&](const IteratorPosition &Pos) { |
| 2409 | return Pos.setTo(rebaseSymbol(State, SVB, Pos.getOffset(), OldSym, |
| 2410 | NewSym)); |
| 2411 | }; |
| 2412 | return processIteratorPositions(State, LessThanEnd, RebaseSymbol); |
| 2413 | } |
| 2414 | |
| 2415 | // This function rebases symbolic expression `OldExpr + Int` to `NewExpr + Int`, |
| 2416 | // `OldExpr - Int` to `NewExpr - Int` and `OldExpr` to `NewExpr` in expression |
| 2417 | // `OrigExpr`. |
| 2418 | SymbolRef rebaseSymbol(ProgramStateRef State, SValBuilder &SVB, |
| 2419 | SymbolRef OrigExpr, SymbolRef OldExpr, |
| 2420 | SymbolRef NewSym) { |
| 2421 | auto &SymMgr = SVB.getSymbolManager(); |
| 2422 | auto Diff = SVB.evalBinOpNN(State, BO_Sub, nonloc::SymbolVal(OrigExpr), |
| 2423 | nonloc::SymbolVal(OldExpr), |
| 2424 | SymMgr.getType(OrigExpr)); |
| 2425 | |
| 2426 | const auto DiffInt = Diff.getAs<nonloc::ConcreteInt>(); |
| 2427 | if (!DiffInt) |
| 2428 | return OrigExpr; |
| 2429 | |
| 2430 | return SVB.evalBinOpNN(State, BO_Add, *DiffInt, nonloc::SymbolVal(NewSym), |
| 2431 | SymMgr.getType(OrigExpr)).getAsSymbol(); |
| 2432 | } |
| 2433 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 2434 | bool isZero(ProgramStateRef State, const NonLoc &Val) { |
| 2435 | auto &BVF = State->getBasicVals(); |
| 2436 | return compare(State, Val, |
| 2437 | nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(0))), |
| 2438 | BO_EQ); |
| 2439 | } |
| 2440 | |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 2441 | bool isPastTheEnd(ProgramStateRef State, const IteratorPosition &Pos) { |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2442 | const auto *Cont = Pos.getContainer(); |
| 2443 | const auto *CData = getContainerData(State, Cont); |
| 2444 | if (!CData) |
| 2445 | return false; |
| 2446 | |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 2447 | const auto End = CData->getEnd(); |
| 2448 | if (End) { |
| 2449 | if (isEqual(State, Pos.getOffset(), End)) { |
| 2450 | return true; |
| 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | return false; |
| 2455 | } |
| 2456 | |
| 2457 | bool isAheadOfRange(ProgramStateRef State, const IteratorPosition &Pos) { |
| 2458 | const auto *Cont = Pos.getContainer(); |
| 2459 | const auto *CData = getContainerData(State, Cont); |
| 2460 | if (!CData) |
| 2461 | return false; |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2462 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 2463 | const auto Beg = CData->getBegin(); |
| 2464 | if (Beg) { |
| 2465 | if (isLess(State, Pos.getOffset(), Beg)) { |
| 2466 | return true; |
| 2467 | } |
| 2468 | } |
| 2469 | |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 2470 | return false; |
| 2471 | } |
| 2472 | |
| 2473 | bool isBehindPastTheEnd(ProgramStateRef State, const IteratorPosition &Pos) { |
| 2474 | const auto *Cont = Pos.getContainer(); |
| 2475 | const auto *CData = getContainerData(State, Cont); |
| 2476 | if (!CData) |
| 2477 | return false; |
| 2478 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2479 | const auto End = CData->getEnd(); |
| 2480 | if (End) { |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 2481 | if (isGreater(State, Pos.getOffset(), End)) { |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2482 | return true; |
| 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | return false; |
| 2487 | } |
| 2488 | |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 2489 | bool isLess(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2) { |
| 2490 | return compare(State, Sym1, Sym2, BO_LT); |
| 2491 | } |
| 2492 | |
| Adam Balogh | d5bd3f6 | 2018-12-04 10:27:27 +0000 | [diff] [blame] | 2493 | bool isGreater(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2) { |
| 2494 | return compare(State, Sym1, Sym2, BO_GT); |
| 2495 | } |
| 2496 | |
| 2497 | bool isEqual(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2) { |
| 2498 | return compare(State, Sym1, Sym2, BO_EQ); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | bool compare(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2, |
| 2502 | BinaryOperator::Opcode Opc) { |
| Adam Balogh | b03ed5e | 2018-06-28 10:58:53 +0000 | [diff] [blame] | 2503 | return compare(State, nonloc::SymbolVal(Sym1), nonloc::SymbolVal(Sym2), Opc); |
| 2504 | } |
| 2505 | |
| 2506 | bool compare(ProgramStateRef State, NonLoc NL1, NonLoc NL2, |
| 2507 | BinaryOperator::Opcode Opc) { |
| 2508 | auto &SVB = State->getStateManager().getSValBuilder(); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2509 | |
| 2510 | const auto comparison = |
| Adam Balogh | 0a7592b | 2018-07-16 09:27:27 +0000 | [diff] [blame] | 2511 | SVB.evalBinOp(State, Opc, NL1, NL2, SVB.getConditionType()); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2512 | |
| Adam Balogh | 0a7592b | 2018-07-16 09:27:27 +0000 | [diff] [blame] | 2513 | assert(comparison.getAs<DefinedSVal>() && |
| 2514 | "Symbol comparison must be a `DefinedSVal`"); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2515 | |
| Adam Balogh | 0a7592b | 2018-07-16 09:27:27 +0000 | [diff] [blame] | 2516 | return !State->assume(comparison.castAs<DefinedSVal>(), false); |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | } // namespace |
| 2520 | |
| Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 2521 | void ento::registerIteratorModeling(CheckerManager &mgr) { |
| 2522 | mgr.registerChecker<IteratorChecker>(); |
| 2523 | } |
| 2524 | |
| 2525 | bool ento::shouldRegisterIteratorModeling(const LangOptions &LO) { |
| 2526 | return true; |
| 2527 | } |
| 2528 | |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2529 | #define REGISTER_CHECKER(name) \ |
| 2530 | void ento::register##name(CheckerManager &Mgr) { \ |
| Kristof Umann | 204bf2b | 2019-01-26 21:41:50 +0000 | [diff] [blame] | 2531 | auto *checker = Mgr.getChecker<IteratorChecker>(); \ |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2532 | checker->ChecksEnabled[IteratorChecker::CK_##name] = true; \ |
| 2533 | checker->CheckNames[IteratorChecker::CK_##name] = \ |
| Kristof Umann | 7264942 | 2019-09-12 19:09:24 +0000 | [diff] [blame] | 2534 | Mgr.getCurrentCheckerName(); \ |
| Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 2535 | } \ |
| 2536 | \ |
| Kristof Umann | 7264942 | 2019-09-12 19:09:24 +0000 | [diff] [blame] | 2537 | bool ento::shouldRegister##name(const LangOptions &LO) { return true; } |
| Artem Dergachev | 8fa639e | 2017-05-29 15:03:20 +0000 | [diff] [blame] | 2538 | |
| 2539 | REGISTER_CHECKER(IteratorRangeChecker) |
| Adam Balogh | 21583b7 | 2018-09-10 09:03:22 +0000 | [diff] [blame] | 2540 | REGISTER_CHECKER(MismatchedIteratorChecker) |
| Adam Balogh | 2cfbe93 | 2018-08-28 08:41:15 +0000 | [diff] [blame] | 2541 | REGISTER_CHECKER(InvalidatedIteratorChecker) |
| Adam Balogh | 0f88cae | 2019-11-08 08:56:31 +0100 | [diff] [blame] | 2542 | REGISTER_CHECKER(DebugIteratorModeling) |