xref: /llvm-project/clang/lib/StaticAnalyzer/Checkers/IteratorModeling.cpp (revision a59d4ae4313c0a961c50d14c0616b49220c5a469)
1 //===-- IteratorModeling.cpp --------------------------------------*- C++ -*--//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Defines a modeling-checker for modeling STL iterator-like iterators.
10 //
11 //===----------------------------------------------------------------------===//
12 //
13 // In the code, iterator can be represented as a:
14 // * type-I: typedef-ed pointer. Operations over such iterator, such as
15 //           comparisons or increments, are modeled straightforwardly by the
16 //           analyzer.
17 // * type-II: structure with its method bodies available.  Operations over such
18 //            iterator are inlined by the analyzer, and results of modeling
19 //            these operations are exposing implementation details of the
20 //            iterators, which is not necessarily helping.
21 // * type-III: completely opaque structure. Operations over such iterator are
22 //             modeled conservatively, producing conjured symbols everywhere.
23 //
24 // To handle all these types in a common way we introduce a structure called
25 // IteratorPosition which is an abstraction of the position the iterator
26 // represents using symbolic expressions. The checker handles all the
27 // operations on this structure.
28 //
29 // Additionally, depending on the circumstances, operators of types II and III
30 // can be represented as:
31 // * type-IIa, type-IIIa: conjured structure symbols - when returned by value
32 //                        from conservatively evaluated methods such as
33 //                        `.begin()`.
34 // * type-IIb, type-IIIb: memory regions of iterator-typed objects, such as
35 //                        variables or temporaries, when the iterator object is
36 //                        currently treated as an lvalue.
37 // * type-IIc, type-IIIc: compound values of iterator-typed objects, when the
38 //                        iterator object is treated as an rvalue taken of a
39 //                        particular lvalue, eg. a copy of "type-a" iterator
40 //                        object, or an iterator that existed before the
41 //                        analysis has started.
42 //
43 // To handle any of these three different representations stored in an SVal we
44 // use setter and getters functions which separate the three cases. To store
45 // them we use a pointer union of symbol and memory region.
46 //
47 // The checker works the following way: We record the begin and the
48 // past-end iterator for all containers whenever their `.begin()` and `.end()`
49 // are called. Since the Constraint Manager cannot handle such SVals we need
50 // to take over its role. We post-check equality and non-equality comparisons
51 // and record that the two sides are equal if we are in the 'equal' branch
52 // (true-branch for `==` and false-branch for `!=`).
53 //
54 // In case of type-I or type-II iterators we get a concrete integer as a result
55 // of the comparison (1 or 0) but in case of type-III we only get a Symbol. In
56 // this latter case we record the symbol and reload it in evalAssume() and do
57 // the propagation there. We also handle (maybe double) negated comparisons
58 // which are represented in the form of (x == 0 or x != 0) where x is the
59 // comparison itself.
60 //
61 // Since `SimpleConstraintManager` cannot handle complex symbolic expressions
62 // we only use expressions of the format S, S+n or S-n for iterator positions
63 // where S is a conjured symbol and n is an unsigned concrete integer. When
64 // making an assumption e.g. `S1 + n == S2 + m` we store `S1 - S2 == m - n` as
65 // a constraint which we later retrieve when doing an actual comparison.
66 
67 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
68 #include "clang/AST/DeclTemplate.h"
69 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
70 #include "clang/StaticAnalyzer/Core/Checker.h"
71 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
72 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
73 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicType.h"
74 
75 #include "Iterator.h"
76 
77 #include <utility>
78 
79 using namespace clang;
80 using namespace ento;
81 using namespace iterator;
82 
83 namespace {
84 
85 class IteratorModeling
86     : public Checker<check::PostCall, check::PostStmt<UnaryOperator>,
87                      check::PostStmt<BinaryOperator>,
88                      check::PostStmt<MaterializeTemporaryExpr>,
89                      check::Bind, check::LiveSymbols, check::DeadSymbols> {
90 
91   using AdvanceFn = void (IteratorModeling::*)(CheckerContext &, const Expr *,
92                                                SVal, SVal, SVal) const;
93 
94   void handleOverloadedOperator(CheckerContext &C, const CallEvent &Call,
95                                 OverloadedOperatorKind Op) const;
96   void handleAdvanceLikeFunction(CheckerContext &C, const CallEvent &Call,
97                                  const Expr *OrigExpr,
98                                  const AdvanceFn *Handler) const;
99 
100   void handleComparison(CheckerContext &C, const Expr *CE, SVal RetVal,
101                         const SVal &LVal, const SVal &RVal,
102                         OverloadedOperatorKind Op) const;
103   void processComparison(CheckerContext &C, ProgramStateRef State,
104                          SymbolRef Sym1, SymbolRef Sym2, const SVal &RetVal,
105                          OverloadedOperatorKind Op) const;
106   void handleIncrement(CheckerContext &C, const SVal &RetVal, const SVal &Iter,
107                        bool Postfix) const;
108   void handleDecrement(CheckerContext &C, const SVal &RetVal, const SVal &Iter,
109                        bool Postfix) const;
110   void handleRandomIncrOrDecr(CheckerContext &C, const Expr *CE,
111                               OverloadedOperatorKind Op, const SVal &RetVal,
112                               const SVal &LHS, const SVal &RHS) const;
113   void handlePtrIncrOrDecr(CheckerContext &C, const Expr *Iterator,
114                            OverloadedOperatorKind OK, SVal Offset) const;
115   void handleAdvance(CheckerContext &C, const Expr *CE, SVal RetVal, SVal Iter,
116                      SVal Amount) const;
117   void handlePrev(CheckerContext &C, const Expr *CE, SVal RetVal, SVal Iter,
118                   SVal Amount) const;
119   void handleNext(CheckerContext &C, const Expr *CE, SVal RetVal, SVal Iter,
120                   SVal Amount) const;
121   void assignToContainer(CheckerContext &C, const Expr *CE, const SVal &RetVal,
122                          const MemRegion *Cont) const;
123   bool noChangeInAdvance(CheckerContext &C, SVal Iter, const Expr *CE) const;
124   void printState(raw_ostream &Out, ProgramStateRef State, const char *NL,
125                   const char *Sep) const override;
126 
127   // std::advance, std::prev & std::next
128   CallDescriptionMap<AdvanceFn> AdvanceLikeFunctions = {
129       // template<class InputIt, class Distance>
130       // void advance(InputIt& it, Distance n);
131       {{{"std", "advance"}, 2}, &IteratorModeling::handleAdvance},
132 
133       // template<class BidirIt>
134       // BidirIt prev(
135       //   BidirIt it,
136       //   typename std::iterator_traits<BidirIt>::difference_type n = 1);
137       {{{"std", "prev"}, 2}, &IteratorModeling::handlePrev},
138 
139       // template<class ForwardIt>
140       // ForwardIt next(
141       //   ForwardIt it,
142       //   typename std::iterator_traits<ForwardIt>::difference_type n = 1);
143       {{{"std", "next"}, 2}, &IteratorModeling::handleNext},
144   };
145 
146 public:
147   IteratorModeling() = default;
148 
149   void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
150   void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &C) const;
151   void checkPostStmt(const UnaryOperator *UO, CheckerContext &C) const;
152   void checkPostStmt(const BinaryOperator *BO, CheckerContext &C) const;
153   void checkPostStmt(const CXXConstructExpr *CCE, CheckerContext &C) const;
154   void checkPostStmt(const DeclStmt *DS, CheckerContext &C) const;
155   void checkPostStmt(const MaterializeTemporaryExpr *MTE,
156                      CheckerContext &C) const;
157   void checkLiveSymbols(ProgramStateRef State, SymbolReaper &SR) const;
158   void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
159 };
160 
161 bool isSimpleComparisonOperator(OverloadedOperatorKind OK);
162 bool isSimpleComparisonOperator(BinaryOperatorKind OK);
163 ProgramStateRef removeIteratorPosition(ProgramStateRef State, const SVal &Val);
164 ProgramStateRef relateSymbols(ProgramStateRef State, SymbolRef Sym1,
165                               SymbolRef Sym2, bool Equal);
166 bool isBoundThroughLazyCompoundVal(const Environment &Env,
167                                    const MemRegion *Reg);
168 const ExplodedNode *findCallEnter(const ExplodedNode *Node, const Expr *Call);
169 
170 } // namespace
171 
172 void IteratorModeling::checkPostCall(const CallEvent &Call,
173                                      CheckerContext &C) const {
174   // Record new iterator positions and iterator position changes
175   const auto *Func = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
176   if (!Func)
177     return;
178 
179   if (Func->isOverloadedOperator()) {
180     const auto Op = Func->getOverloadedOperator();
181     handleOverloadedOperator(C, Call, Op);
182     return;
183   }
184 
185   const auto *OrigExpr = Call.getOriginExpr();
186   if (!OrigExpr)
187     return;
188 
189   const AdvanceFn *Handler = AdvanceLikeFunctions.lookup(Call);
190   if (Handler) {
191     handleAdvanceLikeFunction(C, Call, OrigExpr, Handler);
192     return;
193   }
194 
195   if (!isIteratorType(Call.getResultType()))
196     return;
197 
198   auto State = C.getState();
199 
200   // Already bound to container?
201   if (getIteratorPosition(State, Call.getReturnValue()))
202     return;
203 
204   // Copy-like and move constructors
205   if (isa<CXXConstructorCall>(&Call) && Call.getNumArgs() == 1) {
206     if (const auto *Pos = getIteratorPosition(State, Call.getArgSVal(0))) {
207       State = setIteratorPosition(State, Call.getReturnValue(), *Pos);
208       if (cast<CXXConstructorDecl>(Func)->isMoveConstructor()) {
209         State = removeIteratorPosition(State, Call.getArgSVal(0));
210       }
211       C.addTransition(State);
212       return;
213     }
214   }
215 
216   // Assumption: if return value is an iterator which is not yet bound to a
217   //             container, then look for the first iterator argument of the
218   //             same type as the return value and bind the return value to
219   //             the same container. This approach works for STL algorithms.
220   // FIXME: Add a more conservative mode
221   for (unsigned i = 0; i < Call.getNumArgs(); ++i) {
222     if (isIteratorType(Call.getArgExpr(i)->getType()) &&
223         Call.getArgExpr(i)->getType().getNonReferenceType().getDesugaredType(
224             C.getASTContext()).getTypePtr() ==
225         Call.getResultType().getDesugaredType(C.getASTContext()).getTypePtr()) {
226       if (const auto *Pos = getIteratorPosition(State, Call.getArgSVal(i))) {
227         assignToContainer(C, OrigExpr, Call.getReturnValue(),
228                           Pos->getContainer());
229         return;
230       }
231     }
232   }
233 }
234 
235 void IteratorModeling::checkBind(SVal Loc, SVal Val, const Stmt *S,
236                                  CheckerContext &C) const {
237   auto State = C.getState();
238   const auto *Pos = getIteratorPosition(State, Val);
239   if (Pos) {
240     State = setIteratorPosition(State, Loc, *Pos);
241     C.addTransition(State);
242   } else {
243     const auto *OldPos = getIteratorPosition(State, Loc);
244     if (OldPos) {
245       State = removeIteratorPosition(State, Loc);
246       C.addTransition(State);
247     }
248   }
249 }
250 
251 void IteratorModeling::checkPostStmt(const UnaryOperator *UO,
252                                      CheckerContext &C) const {
253   UnaryOperatorKind OK = UO->getOpcode();
254   if (!isIncrementOperator(OK) && !isDecrementOperator(OK))
255     return;
256 
257   auto &SVB = C.getSValBuilder();
258   handlePtrIncrOrDecr(C, UO->getSubExpr(),
259                       isIncrementOperator(OK) ? OO_Plus : OO_Minus,
260                       SVB.makeArrayIndex(1));
261 }
262 
263 void IteratorModeling::checkPostStmt(const BinaryOperator *BO,
264                                      CheckerContext &C) const {
265   ProgramStateRef State = C.getState();
266   BinaryOperatorKind OK = BO->getOpcode();
267   SVal RVal = State->getSVal(BO->getRHS(), C.getLocationContext());
268 
269   if (isSimpleComparisonOperator(BO->getOpcode())) {
270     SVal LVal = State->getSVal(BO->getLHS(), C.getLocationContext());
271     SVal Result = State->getSVal(BO, C.getLocationContext());
272     handleComparison(C, BO, Result, LVal, RVal,
273                      BinaryOperator::getOverloadedOperator(OK));
274   } else if (isRandomIncrOrDecrOperator(OK)) {
275     if (!BO->getRHS()->getType()->isIntegralOrEnumerationType())
276       return;
277     handlePtrIncrOrDecr(C, BO->getLHS(),
278                         BinaryOperator::getOverloadedOperator(OK), RVal);
279   }
280 }
281 
282 void IteratorModeling::checkPostStmt(const MaterializeTemporaryExpr *MTE,
283                                      CheckerContext &C) const {
284   /* Transfer iterator state to temporary objects */
285   auto State = C.getState();
286   const auto *Pos = getIteratorPosition(State, C.getSVal(MTE->getSubExpr()));
287   if (!Pos)
288     return;
289   State = setIteratorPosition(State, C.getSVal(MTE), *Pos);
290   C.addTransition(State);
291 }
292 
293 void IteratorModeling::checkLiveSymbols(ProgramStateRef State,
294                                         SymbolReaper &SR) const {
295   // Keep symbolic expressions of iterator positions alive
296   auto RegionMap = State->get<IteratorRegionMap>();
297   for (const auto &Reg : RegionMap) {
298     const auto Offset = Reg.second.getOffset();
299     for (auto i = Offset->symbol_begin(); i != Offset->symbol_end(); ++i)
300       if (isa<SymbolData>(*i))
301         SR.markLive(*i);
302   }
303 
304   auto SymbolMap = State->get<IteratorSymbolMap>();
305   for (const auto &Sym : SymbolMap) {
306     const auto Offset = Sym.second.getOffset();
307     for (auto i = Offset->symbol_begin(); i != Offset->symbol_end(); ++i)
308       if (isa<SymbolData>(*i))
309         SR.markLive(*i);
310   }
311 
312 }
313 
314 void IteratorModeling::checkDeadSymbols(SymbolReaper &SR,
315                                         CheckerContext &C) const {
316   // Cleanup
317   auto State = C.getState();
318 
319   auto RegionMap = State->get<IteratorRegionMap>();
320   for (const auto &Reg : RegionMap) {
321     if (!SR.isLiveRegion(Reg.first)) {
322       // The region behind the `LazyCompoundVal` is often cleaned up before
323       // the `LazyCompoundVal` itself. If there are iterator positions keyed
324       // by these regions their cleanup must be deferred.
325       if (!isBoundThroughLazyCompoundVal(State->getEnvironment(), Reg.first)) {
326         State = State->remove<IteratorRegionMap>(Reg.first);
327       }
328     }
329   }
330 
331   auto SymbolMap = State->get<IteratorSymbolMap>();
332   for (const auto &Sym : SymbolMap) {
333     if (!SR.isLive(Sym.first)) {
334       State = State->remove<IteratorSymbolMap>(Sym.first);
335     }
336   }
337 
338   C.addTransition(State);
339 }
340 
341 void
342 IteratorModeling::handleOverloadedOperator(CheckerContext &C,
343                                            const CallEvent &Call,
344                                            OverloadedOperatorKind Op) const {
345     if (isSimpleComparisonOperator(Op)) {
346       const auto *OrigExpr = Call.getOriginExpr();
347       if (!OrigExpr)
348         return;
349 
350       if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
351         handleComparison(C, OrigExpr, Call.getReturnValue(),
352                          InstCall->getCXXThisVal(), Call.getArgSVal(0), Op);
353         return;
354       }
355 
356       handleComparison(C, OrigExpr, Call.getReturnValue(), Call.getArgSVal(0),
357                          Call.getArgSVal(1), Op);
358       return;
359     } else if (isRandomIncrOrDecrOperator(Op)) {
360       const auto *OrigExpr = Call.getOriginExpr();
361       if (!OrigExpr)
362         return;
363 
364       if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
365         if (Call.getNumArgs() >= 1 &&
366               Call.getArgExpr(0)->getType()->isIntegralOrEnumerationType()) {
367           handleRandomIncrOrDecr(C, OrigExpr, Op, Call.getReturnValue(),
368                                  InstCall->getCXXThisVal(), Call.getArgSVal(0));
369           return;
370         }
371       } else {
372         if (Call.getNumArgs() >= 2 &&
373               Call.getArgExpr(1)->getType()->isIntegralOrEnumerationType()) {
374           handleRandomIncrOrDecr(C, OrigExpr, Op, Call.getReturnValue(),
375                                  Call.getArgSVal(0), Call.getArgSVal(1));
376           return;
377         }
378       }
379     } else if (isIncrementOperator(Op)) {
380       if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
381         handleIncrement(C, Call.getReturnValue(), InstCall->getCXXThisVal(),
382                         Call.getNumArgs());
383         return;
384       }
385 
386       handleIncrement(C, Call.getReturnValue(), Call.getArgSVal(0),
387                       Call.getNumArgs());
388       return;
389     } else if (isDecrementOperator(Op)) {
390       if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
391         handleDecrement(C, Call.getReturnValue(), InstCall->getCXXThisVal(),
392                         Call.getNumArgs());
393         return;
394       }
395 
396       handleDecrement(C, Call.getReturnValue(), Call.getArgSVal(0),
397                         Call.getNumArgs());
398       return;
399     }
400 }
401 
402 void
403 IteratorModeling::handleAdvanceLikeFunction(CheckerContext &C,
404                                             const CallEvent &Call,
405                                             const Expr *OrigExpr,
406                                             const AdvanceFn *Handler) const {
407   if (!C.wasInlined) {
408     (this->**Handler)(C, OrigExpr, Call.getReturnValue(),
409                       Call.getArgSVal(0), Call.getArgSVal(1));
410     return;
411   }
412 
413   // If std::advance() was inlined, but a non-standard function it calls inside
414   // was not, then we have to model it explicitly
415   const auto *IdInfo = cast<FunctionDecl>(Call.getDecl())->getIdentifier();
416   if (IdInfo) {
417     if (IdInfo->getName() == "advance") {
418       if (noChangeInAdvance(C, Call.getArgSVal(0), OrigExpr)) {
419         (this->**Handler)(C, OrigExpr, Call.getReturnValue(),
420                           Call.getArgSVal(0), Call.getArgSVal(1));
421       }
422     }
423   }
424 }
425 
426 void IteratorModeling::handleComparison(CheckerContext &C, const Expr *CE,
427                                        SVal RetVal, const SVal &LVal,
428                                        const SVal &RVal,
429                                        OverloadedOperatorKind Op) const {
430   // Record the operands and the operator of the comparison for the next
431   // evalAssume, if the result is a symbolic expression. If it is a concrete
432   // value (only one branch is possible), then transfer the state between
433   // the operands according to the operator and the result
434    auto State = C.getState();
435   const auto *LPos = getIteratorPosition(State, LVal);
436   const auto *RPos = getIteratorPosition(State, RVal);
437   const MemRegion *Cont = nullptr;
438   if (LPos) {
439     Cont = LPos->getContainer();
440   } else if (RPos) {
441     Cont = RPos->getContainer();
442   }
443   if (!Cont)
444     return;
445 
446   // At least one of the iterators has recorded positions. If one of them does
447   // not then create a new symbol for the offset.
448   SymbolRef Sym;
449   if (!LPos || !RPos) {
450     auto &SymMgr = C.getSymbolManager();
451     Sym = SymMgr.conjureSymbol(CE, C.getLocationContext(),
452                                C.getASTContext().LongTy, C.blockCount());
453     State = assumeNoOverflow(State, Sym, 4);
454   }
455 
456   if (!LPos) {
457     State = setIteratorPosition(State, LVal,
458                                 IteratorPosition::getPosition(Cont, Sym));
459     LPos = getIteratorPosition(State, LVal);
460   } else if (!RPos) {
461     State = setIteratorPosition(State, RVal,
462                                 IteratorPosition::getPosition(Cont, Sym));
463     RPos = getIteratorPosition(State, RVal);
464   }
465 
466   // If the value for which we just tried to set a new iterator position is
467   // an `SVal`for which no iterator position can be set then the setting was
468   // unsuccessful. We cannot handle the comparison in this case.
469   if (!LPos || !RPos)
470     return;
471 
472   // We cannot make assumptions on `UnknownVal`. Let us conjure a symbol
473   // instead.
474   if (RetVal.isUnknown()) {
475     auto &SymMgr = C.getSymbolManager();
476     auto *LCtx = C.getLocationContext();
477     RetVal = nonloc::SymbolVal(SymMgr.conjureSymbol(
478         CE, LCtx, C.getASTContext().BoolTy, C.blockCount()));
479     State = State->BindExpr(CE, LCtx, RetVal);
480   }
481 
482   processComparison(C, State, LPos->getOffset(), RPos->getOffset(), RetVal, Op);
483 }
484 
485 void IteratorModeling::processComparison(CheckerContext &C,
486                                          ProgramStateRef State, SymbolRef Sym1,
487                                          SymbolRef Sym2, const SVal &RetVal,
488                                          OverloadedOperatorKind Op) const {
489   if (const auto TruthVal = RetVal.getAs<nonloc::ConcreteInt>()) {
490     if ((State = relateSymbols(State, Sym1, Sym2,
491                               (Op == OO_EqualEqual) ==
492                                (TruthVal->getValue() != 0)))) {
493       C.addTransition(State);
494     } else {
495       C.generateSink(State, C.getPredecessor());
496     }
497     return;
498   }
499 
500   const auto ConditionVal = RetVal.getAs<DefinedSVal>();
501   if (!ConditionVal)
502     return;
503 
504   if (auto StateTrue = relateSymbols(State, Sym1, Sym2, Op == OO_EqualEqual)) {
505     StateTrue = StateTrue->assume(*ConditionVal, true);
506     C.addTransition(StateTrue);
507   }
508 
509   if (auto StateFalse = relateSymbols(State, Sym1, Sym2, Op != OO_EqualEqual)) {
510     StateFalse = StateFalse->assume(*ConditionVal, false);
511     C.addTransition(StateFalse);
512   }
513 }
514 
515 void IteratorModeling::handleIncrement(CheckerContext &C, const SVal &RetVal,
516                                        const SVal &Iter, bool Postfix) const {
517   // Increment the symbolic expressions which represents the position of the
518   // iterator
519   auto State = C.getState();
520   auto &BVF = C.getSymbolManager().getBasicVals();
521 
522   const auto *Pos = getIteratorPosition(State, Iter);
523   if (!Pos)
524     return;
525 
526   auto NewState =
527     advancePosition(State, Iter, OO_Plus,
528                     nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))));
529   assert(NewState &&
530          "Advancing position by concrete int should always be successful");
531 
532   const auto *NewPos = getIteratorPosition(NewState, Iter);
533   assert(NewPos &&
534          "Iterator should have position after successful advancement");
535 
536   State = setIteratorPosition(State, Iter, *NewPos);
537   State = setIteratorPosition(State, RetVal, Postfix ? *Pos : *NewPos);
538   C.addTransition(State);
539 }
540 
541 void IteratorModeling::handleDecrement(CheckerContext &C, const SVal &RetVal,
542                                        const SVal &Iter, bool Postfix) const {
543   // Decrement the symbolic expressions which represents the position of the
544   // iterator
545   auto State = C.getState();
546   auto &BVF = C.getSymbolManager().getBasicVals();
547 
548   const auto *Pos = getIteratorPosition(State, Iter);
549   if (!Pos)
550     return;
551 
552   auto NewState =
553     advancePosition(State, Iter, OO_Minus,
554                     nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))));
555   assert(NewState &&
556          "Advancing position by concrete int should always be successful");
557 
558   const auto *NewPos = getIteratorPosition(NewState, Iter);
559   assert(NewPos &&
560          "Iterator should have position after successful advancement");
561 
562   State = setIteratorPosition(State, Iter, *NewPos);
563   State = setIteratorPosition(State, RetVal, Postfix ? *Pos : *NewPos);
564   C.addTransition(State);
565 }
566 
567 void IteratorModeling::handleRandomIncrOrDecr(CheckerContext &C,
568                                               const Expr *CE,
569                                               OverloadedOperatorKind Op,
570                                               const SVal &RetVal,
571                                               const SVal &LHS,
572                                               const SVal &RHS) const {
573   // Increment or decrement the symbolic expressions which represents the
574   // position of the iterator
575   auto State = C.getState();
576 
577   const auto *Pos = getIteratorPosition(State, LHS);
578   if (!Pos)
579     return;
580 
581   const auto *value = &RHS;
582   SVal val;
583   if (auto loc = RHS.getAs<Loc>()) {
584     val = State->getRawSVal(*loc);
585     value = &val;
586   }
587 
588   auto &TgtVal = (Op == OO_PlusEqual || Op == OO_MinusEqual) ? LHS : RetVal;
589 
590   // `AdvancedState` is a state where the position of `LHS` is advanced. We
591   // only need this state to retrieve the new position, but we do not want
592   // to change the position of `LHS` (in every case).
593   auto AdvancedState = advancePosition(State, LHS, Op, *value);
594   if (AdvancedState) {
595     const auto *NewPos = getIteratorPosition(AdvancedState, LHS);
596     assert(NewPos &&
597            "Iterator should have position after successful advancement");
598 
599     State = setIteratorPosition(State, TgtVal, *NewPos);
600     C.addTransition(State);
601   } else {
602     assignToContainer(C, CE, TgtVal, Pos->getContainer());
603   }
604 }
605 
606 void IteratorModeling::handlePtrIncrOrDecr(CheckerContext &C,
607                                            const Expr *Iterator,
608                                            OverloadedOperatorKind OK,
609                                            SVal Offset) const {
610   if (!Offset.getAs<DefinedSVal>())
611     return;
612 
613   QualType PtrType = Iterator->getType();
614   if (!PtrType->isPointerType())
615     return;
616   QualType ElementType = PtrType->getPointeeType();
617 
618   ProgramStateRef State = C.getState();
619   SVal OldVal = State->getSVal(Iterator, C.getLocationContext());
620 
621   const IteratorPosition *OldPos = getIteratorPosition(State, OldVal);
622   if (!OldPos)
623     return;
624 
625   SVal NewVal;
626   if (OK == OO_Plus || OK == OO_PlusEqual) {
627     NewVal = State->getLValue(ElementType, Offset, OldVal);
628   } else {
629     auto &SVB = C.getSValBuilder();
630     SVal NegatedOffset = SVB.evalMinus(Offset.castAs<NonLoc>());
631     NewVal = State->getLValue(ElementType, NegatedOffset, OldVal);
632   }
633 
634   // `AdvancedState` is a state where the position of `Old` is advanced. We
635   // only need this state to retrieve the new position, but we do not want
636   // ever to change the position of `OldVal`.
637   auto AdvancedState = advancePosition(State, OldVal, OK, Offset);
638   if (AdvancedState) {
639     const IteratorPosition *NewPos = getIteratorPosition(AdvancedState, OldVal);
640     assert(NewPos &&
641            "Iterator should have position after successful advancement");
642 
643     ProgramStateRef NewState = setIteratorPosition(State, NewVal, *NewPos);
644     C.addTransition(NewState);
645   } else {
646     assignToContainer(C, Iterator, NewVal, OldPos->getContainer());
647   }
648 }
649 
650 void IteratorModeling::handleAdvance(CheckerContext &C, const Expr *CE,
651                                      SVal RetVal, SVal Iter,
652                                      SVal Amount) const {
653   handleRandomIncrOrDecr(C, CE, OO_PlusEqual, RetVal, Iter, Amount);
654 }
655 
656 void IteratorModeling::handlePrev(CheckerContext &C, const Expr *CE,
657                                   SVal RetVal, SVal Iter, SVal Amount) const {
658   handleRandomIncrOrDecr(C, CE, OO_Minus, RetVal, Iter, Amount);
659 }
660 
661 void IteratorModeling::handleNext(CheckerContext &C, const Expr *CE,
662                                   SVal RetVal, SVal Iter, SVal Amount) const {
663   handleRandomIncrOrDecr(C, CE, OO_Plus, RetVal, Iter, Amount);
664 }
665 
666 void IteratorModeling::assignToContainer(CheckerContext &C, const Expr *CE,
667                                          const SVal &RetVal,
668                                          const MemRegion *Cont) const {
669   Cont = Cont->getMostDerivedObjectRegion();
670 
671   auto State = C.getState();
672   const auto *LCtx = C.getLocationContext();
673   State = createIteratorPosition(State, RetVal, Cont, CE, LCtx, C.blockCount());
674 
675   C.addTransition(State);
676 }
677 
678 bool IteratorModeling::noChangeInAdvance(CheckerContext &C, SVal Iter,
679                                          const Expr *CE) const {
680   // Compare the iterator position before and after the call. (To be called
681   // from `checkPostCall()`.)
682   const auto StateAfter = C.getState();
683 
684   const auto *PosAfter = getIteratorPosition(StateAfter, Iter);
685   // If we have no position after the call of `std::advance`, then we are not
686   // interested. (Modeling of an inlined `std::advance()` should not remove the
687   // position in any case.)
688   if (!PosAfter)
689     return false;
690 
691   const ExplodedNode *N = findCallEnter(C.getPredecessor(), CE);
692   assert(N && "Any call should have a `CallEnter` node.");
693 
694   const auto StateBefore = N->getState();
695   const auto *PosBefore = getIteratorPosition(StateBefore, Iter);
696   // FIXME: `std::advance()` should not create a new iterator position but
697   //        change existing ones. However, in case of iterators implemented as
698   //        pointers the handling of parameters in `std::advance()`-like
699   //        functions is still incomplete which may result in cases where
700   //        the new position is assigned to the wrong pointer. This causes
701   //        crash if we use an assertion here.
702   if (!PosBefore)
703     return false;
704 
705   return PosBefore->getOffset() == PosAfter->getOffset();
706 }
707 
708 void IteratorModeling::printState(raw_ostream &Out, ProgramStateRef State,
709                                   const char *NL, const char *Sep) const {
710   auto SymbolMap = State->get<IteratorSymbolMap>();
711   auto RegionMap = State->get<IteratorRegionMap>();
712   // Use a counter to add newlines before every line except the first one.
713   unsigned Count = 0;
714 
715   if (!SymbolMap.isEmpty() || !RegionMap.isEmpty()) {
716     Out << Sep << "Iterator Positions :" << NL;
717     for (const auto &Sym : SymbolMap) {
718       if (Count++)
719         Out << NL;
720 
721       Sym.first->dumpToStream(Out);
722       Out << " : ";
723       const auto Pos = Sym.second;
724       Out << (Pos.isValid() ? "Valid" : "Invalid") << " ; Container == ";
725       Pos.getContainer()->dumpToStream(Out);
726       Out<<" ; Offset == ";
727       Pos.getOffset()->dumpToStream(Out);
728     }
729 
730     for (const auto &Reg : RegionMap) {
731       if (Count++)
732         Out << NL;
733 
734       Reg.first->dumpToStream(Out);
735       Out << " : ";
736       const auto Pos = Reg.second;
737       Out << (Pos.isValid() ? "Valid" : "Invalid") << " ; Container == ";
738       Pos.getContainer()->dumpToStream(Out);
739       Out<<" ; Offset == ";
740       Pos.getOffset()->dumpToStream(Out);
741     }
742   }
743 }
744 
745 namespace {
746 
747 bool isSimpleComparisonOperator(OverloadedOperatorKind OK) {
748   return OK == OO_EqualEqual || OK == OO_ExclaimEqual;
749 }
750 
751 bool isSimpleComparisonOperator(BinaryOperatorKind OK) {
752   return OK == BO_EQ || OK == BO_NE;
753 }
754 
755 ProgramStateRef removeIteratorPosition(ProgramStateRef State, const SVal &Val) {
756   if (auto Reg = Val.getAsRegion()) {
757     Reg = Reg->getMostDerivedObjectRegion();
758     return State->remove<IteratorRegionMap>(Reg);
759   } else if (const auto Sym = Val.getAsSymbol()) {
760     return State->remove<IteratorSymbolMap>(Sym);
761   } else if (const auto LCVal = Val.getAs<nonloc::LazyCompoundVal>()) {
762     return State->remove<IteratorRegionMap>(LCVal->getRegion());
763   }
764   return nullptr;
765 }
766 
767 ProgramStateRef relateSymbols(ProgramStateRef State, SymbolRef Sym1,
768                               SymbolRef Sym2, bool Equal) {
769   auto &SVB = State->getStateManager().getSValBuilder();
770 
771   // FIXME: This code should be reworked as follows:
772   // 1. Subtract the operands using evalBinOp().
773   // 2. Assume that the result doesn't overflow.
774   // 3. Compare the result to 0.
775   // 4. Assume the result of the comparison.
776   const auto comparison =
777     SVB.evalBinOp(State, BO_EQ, nonloc::SymbolVal(Sym1),
778                   nonloc::SymbolVal(Sym2), SVB.getConditionType());
779 
780   assert(comparison.getAs<DefinedSVal>() &&
781     "Symbol comparison must be a `DefinedSVal`");
782 
783   auto NewState = State->assume(comparison.castAs<DefinedSVal>(), Equal);
784   if (!NewState)
785     return nullptr;
786 
787   if (const auto CompSym = comparison.getAsSymbol()) {
788     assert(isa<SymIntExpr>(CompSym) &&
789            "Symbol comparison must be a `SymIntExpr`");
790     assert(BinaryOperator::isComparisonOp(
791                cast<SymIntExpr>(CompSym)->getOpcode()) &&
792            "Symbol comparison must be a comparison");
793     return assumeNoOverflow(NewState, cast<SymIntExpr>(CompSym)->getLHS(), 2);
794   }
795 
796   return NewState;
797 }
798 
799 bool isBoundThroughLazyCompoundVal(const Environment &Env,
800                                    const MemRegion *Reg) {
801   for (const auto &Binding : Env) {
802     if (const auto LCVal = Binding.second.getAs<nonloc::LazyCompoundVal>()) {
803       if (LCVal->getRegion() == Reg)
804         return true;
805     }
806   }
807 
808   return false;
809 }
810 
811 const ExplodedNode *findCallEnter(const ExplodedNode *Node, const Expr *Call) {
812   while (Node) {
813     ProgramPoint PP = Node->getLocation();
814     if (auto Enter = PP.getAs<CallEnter>()) {
815       if (Enter->getCallExpr() == Call)
816         break;
817     }
818 
819     Node = Node->getFirstPred();
820   }
821 
822   return Node;
823 }
824 
825 } // namespace
826 
827 void ento::registerIteratorModeling(CheckerManager &mgr) {
828   mgr.registerChecker<IteratorModeling>();
829 }
830 
831 bool ento::shouldRegisterIteratorModeling(const CheckerManager &mgr) {
832   return true;
833 }
834