IteratorChecker.cpp 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388
  1. //===-- IteratorChecker.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 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. //
  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 `!=`).
  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
  59. // which are represented in the form of (x == 0 or x != 0) where x is the
  60. // comparison itself.
  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.
  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. #include <utility>
  75. using namespace clang;
  76. using namespace ento;
  77. namespace {
  78. // Abstract position of an iterator. This helps to handle all three kinds
  79. // of operators in a common way by using a symbolic position.
  80. struct IteratorPosition {
  81. private:
  82. // Container the iterator belongs to
  83. const MemRegion *Cont;
  84. // Whether iterator is valid
  85. const bool Valid;
  86. // Abstract offset
  87. const SymbolRef Offset;
  88. IteratorPosition(const MemRegion *C, bool V, SymbolRef Of)
  89. : Cont(C), Valid(V), Offset(Of) {}
  90. public:
  91. const MemRegion *getContainer() const { return Cont; }
  92. bool isValid() const { return Valid; }
  93. SymbolRef getOffset() const { return Offset; }
  94. IteratorPosition invalidate() const {
  95. return IteratorPosition(Cont, false, Offset);
  96. }
  97. static IteratorPosition getPosition(const MemRegion *C, SymbolRef Of) {
  98. return IteratorPosition(C, true, Of);
  99. }
  100. IteratorPosition setTo(SymbolRef NewOf) const {
  101. return IteratorPosition(Cont, Valid, NewOf);
  102. }
  103. IteratorPosition reAssign(const MemRegion *NewCont) const {
  104. return IteratorPosition(NewCont, Valid, Offset);
  105. }
  106. bool operator==(const IteratorPosition &X) const {
  107. return Cont == X.Cont && Valid == X.Valid && Offset == X.Offset;
  108. }
  109. bool operator!=(const IteratorPosition &X) const {
  110. return Cont != X.Cont || Valid != X.Valid || Offset != X.Offset;
  111. }
  112. void Profile(llvm::FoldingSetNodeID &ID) const {
  113. ID.AddPointer(Cont);
  114. ID.AddInteger(Valid);
  115. ID.Add(Offset);
  116. }
  117. };
  118. // Structure to record the symbolic begin and end position of a container
  119. struct ContainerData {
  120. private:
  121. const SymbolRef Begin, End;
  122. ContainerData(SymbolRef B, SymbolRef E) : Begin(B), End(E) {}
  123. public:
  124. static ContainerData fromBegin(SymbolRef B) {
  125. return ContainerData(B, nullptr);
  126. }
  127. static ContainerData fromEnd(SymbolRef E) {
  128. return ContainerData(nullptr, E);
  129. }
  130. SymbolRef getBegin() const { return Begin; }
  131. SymbolRef getEnd() const { return End; }
  132. ContainerData newBegin(SymbolRef B) const { return ContainerData(B, End); }
  133. ContainerData newEnd(SymbolRef E) const { return ContainerData(Begin, E); }
  134. bool operator==(const ContainerData &X) const {
  135. return Begin == X.Begin && End == X.End;
  136. }
  137. bool operator!=(const ContainerData &X) const {
  138. return Begin != X.Begin || End != X.End;
  139. }
  140. void Profile(llvm::FoldingSetNodeID &ID) const {
  141. ID.Add(Begin);
  142. ID.Add(End);
  143. }
  144. };
  145. class IteratorChecker
  146. : public Checker<check::PreCall, check::PostCall,
  147. check::PostStmt<MaterializeTemporaryExpr>, check::Bind,
  148. check::LiveSymbols, check::DeadSymbols> {
  149. std::unique_ptr<BugType> OutOfRangeBugType;
  150. std::unique_ptr<BugType> MismatchedBugType;
  151. std::unique_ptr<BugType> InvalidatedBugType;
  152. void handleComparison(CheckerContext &C, const Expr *CE, const SVal &RetVal,
  153. const SVal &LVal, const SVal &RVal,
  154. OverloadedOperatorKind Op) const;
  155. void processComparison(CheckerContext &C, ProgramStateRef State,
  156. SymbolRef Sym1, SymbolRef Sym2, const SVal &RetVal,
  157. OverloadedOperatorKind Op) const;
  158. void verifyAccess(CheckerContext &C, const SVal &Val) const;
  159. void verifyDereference(CheckerContext &C, const SVal &Val) const;
  160. void handleIncrement(CheckerContext &C, const SVal &RetVal, const SVal &Iter,
  161. bool Postfix) const;
  162. void handleDecrement(CheckerContext &C, const SVal &RetVal, const SVal &Iter,
  163. bool Postfix) const;
  164. void handleRandomIncrOrDecr(CheckerContext &C, OverloadedOperatorKind Op,
  165. const SVal &RetVal, const SVal &LHS,
  166. const SVal &RHS) const;
  167. void handleBegin(CheckerContext &C, const Expr *CE, const SVal &RetVal,
  168. const SVal &Cont) const;
  169. void handleEnd(CheckerContext &C, const Expr *CE, const SVal &RetVal,
  170. const SVal &Cont) const;
  171. void assignToContainer(CheckerContext &C, const Expr *CE, const SVal &RetVal,
  172. const MemRegion *Cont) const;
  173. void handleAssign(CheckerContext &C, const SVal &Cont,
  174. const Expr *CE = nullptr,
  175. const SVal &OldCont = UndefinedVal()) const;
  176. void handleClear(CheckerContext &C, const SVal &Cont) const;
  177. void handlePushBack(CheckerContext &C, const SVal &Cont) const;
  178. void handlePopBack(CheckerContext &C, const SVal &Cont) const;
  179. void handlePushFront(CheckerContext &C, const SVal &Cont) const;
  180. void handlePopFront(CheckerContext &C, const SVal &Cont) const;
  181. void handleInsert(CheckerContext &C, const SVal &Iter) const;
  182. void handleErase(CheckerContext &C, const SVal &Iter) const;
  183. void handleErase(CheckerContext &C, const SVal &Iter1,
  184. const SVal &Iter2) const;
  185. void handleEraseAfter(CheckerContext &C, const SVal &Iter) const;
  186. void handleEraseAfter(CheckerContext &C, const SVal &Iter1,
  187. const SVal &Iter2) const;
  188. void verifyIncrement(CheckerContext &C, const SVal &Iter) const;
  189. void verifyDecrement(CheckerContext &C, const SVal &Iter) const;
  190. void verifyRandomIncrOrDecr(CheckerContext &C, OverloadedOperatorKind Op,
  191. const SVal &LHS, const SVal &RHS) const;
  192. void verifyMatch(CheckerContext &C, const SVal &Iter,
  193. const MemRegion *Cont) const;
  194. void verifyMatch(CheckerContext &C, const SVal &Iter1,
  195. const SVal &Iter2) const;
  196. IteratorPosition advancePosition(CheckerContext &C, OverloadedOperatorKind Op,
  197. const IteratorPosition &Pos,
  198. const SVal &Distance) const;
  199. void reportOutOfRangeBug(const StringRef &Message, const SVal &Val,
  200. CheckerContext &C, ExplodedNode *ErrNode) const;
  201. void reportMismatchedBug(const StringRef &Message, const SVal &Val1,
  202. const SVal &Val2, CheckerContext &C,
  203. ExplodedNode *ErrNode) const;
  204. void reportMismatchedBug(const StringRef &Message, const SVal &Val,
  205. const MemRegion *Reg, CheckerContext &C,
  206. ExplodedNode *ErrNode) const;
  207. void reportInvalidatedBug(const StringRef &Message, const SVal &Val,
  208. CheckerContext &C, ExplodedNode *ErrNode) const;
  209. public:
  210. IteratorChecker();
  211. enum CheckKind {
  212. CK_IteratorRangeChecker,
  213. CK_MismatchedIteratorChecker,
  214. CK_InvalidatedIteratorChecker,
  215. CK_NumCheckKinds
  216. };
  217. DefaultBool ChecksEnabled[CK_NumCheckKinds];
  218. CheckName CheckNames[CK_NumCheckKinds];
  219. void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
  220. void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
  221. void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &C) const;
  222. void checkPostStmt(const CXXConstructExpr *CCE, CheckerContext &C) const;
  223. void checkPostStmt(const DeclStmt *DS, CheckerContext &C) const;
  224. void checkPostStmt(const MaterializeTemporaryExpr *MTE,
  225. CheckerContext &C) const;
  226. void checkLiveSymbols(ProgramStateRef State, SymbolReaper &SR) const;
  227. void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
  228. };
  229. } // namespace
  230. REGISTER_MAP_WITH_PROGRAMSTATE(IteratorSymbolMap, SymbolRef, IteratorPosition)
  231. REGISTER_MAP_WITH_PROGRAMSTATE(IteratorRegionMap, const MemRegion *,
  232. IteratorPosition)
  233. REGISTER_MAP_WITH_PROGRAMSTATE(ContainerMap, const MemRegion *, ContainerData)
  234. namespace {
  235. bool isIteratorType(const QualType &Type);
  236. bool isIterator(const CXXRecordDecl *CRD);
  237. bool isComparisonOperator(OverloadedOperatorKind OK);
  238. bool isBeginCall(const FunctionDecl *Func);
  239. bool isEndCall(const FunctionDecl *Func);
  240. bool isAssignCall(const FunctionDecl *Func);
  241. bool isClearCall(const FunctionDecl *Func);
  242. bool isPushBackCall(const FunctionDecl *Func);
  243. bool isEmplaceBackCall(const FunctionDecl *Func);
  244. bool isPopBackCall(const FunctionDecl *Func);
  245. bool isPushFrontCall(const FunctionDecl *Func);
  246. bool isEmplaceFrontCall(const FunctionDecl *Func);
  247. bool isPopFrontCall(const FunctionDecl *Func);
  248. bool isInsertCall(const FunctionDecl *Func);
  249. bool isEraseCall(const FunctionDecl *Func);
  250. bool isEraseAfterCall(const FunctionDecl *Func);
  251. bool isEmplaceCall(const FunctionDecl *Func);
  252. bool isAssignmentOperator(OverloadedOperatorKind OK);
  253. bool isSimpleComparisonOperator(OverloadedOperatorKind OK);
  254. bool isAccessOperator(OverloadedOperatorKind OK);
  255. bool isDereferenceOperator(OverloadedOperatorKind OK);
  256. bool isIncrementOperator(OverloadedOperatorKind OK);
  257. bool isDecrementOperator(OverloadedOperatorKind OK);
  258. bool isRandomIncrOrDecrOperator(OverloadedOperatorKind OK);
  259. bool hasSubscriptOperator(ProgramStateRef State, const MemRegion *Reg);
  260. bool frontModifiable(ProgramStateRef State, const MemRegion *Reg);
  261. bool backModifiable(ProgramStateRef State, const MemRegion *Reg);
  262. SymbolRef getContainerBegin(ProgramStateRef State, const MemRegion *Cont);
  263. SymbolRef getContainerEnd(ProgramStateRef State, const MemRegion *Cont);
  264. ProgramStateRef createContainerBegin(ProgramStateRef State,
  265. const MemRegion *Cont, const Expr *E,
  266. QualType T, const LocationContext *LCtx,
  267. unsigned BlockCount);
  268. ProgramStateRef createContainerEnd(ProgramStateRef State, const MemRegion *Cont,
  269. const Expr *E, QualType T,
  270. const LocationContext *LCtx,
  271. unsigned BlockCount);
  272. const IteratorPosition *getIteratorPosition(ProgramStateRef State,
  273. const SVal &Val);
  274. ProgramStateRef setIteratorPosition(ProgramStateRef State, const SVal &Val,
  275. const IteratorPosition &Pos);
  276. ProgramStateRef removeIteratorPosition(ProgramStateRef State, const SVal &Val);
  277. ProgramStateRef assumeNoOverflow(ProgramStateRef State, SymbolRef Sym,
  278. long Scale);
  279. ProgramStateRef invalidateAllIteratorPositions(ProgramStateRef State,
  280. const MemRegion *Cont);
  281. ProgramStateRef
  282. invalidateAllIteratorPositionsExcept(ProgramStateRef State,
  283. const MemRegion *Cont, SymbolRef Offset,
  284. BinaryOperator::Opcode Opc);
  285. ProgramStateRef invalidateIteratorPositions(ProgramStateRef State,
  286. SymbolRef Offset,
  287. BinaryOperator::Opcode Opc);
  288. ProgramStateRef invalidateIteratorPositions(ProgramStateRef State,
  289. SymbolRef Offset1,
  290. BinaryOperator::Opcode Opc1,
  291. SymbolRef Offset2,
  292. BinaryOperator::Opcode Opc2);
  293. ProgramStateRef reassignAllIteratorPositions(ProgramStateRef State,
  294. const MemRegion *Cont,
  295. const MemRegion *NewCont);
  296. ProgramStateRef reassignAllIteratorPositionsUnless(ProgramStateRef State,
  297. const MemRegion *Cont,
  298. const MemRegion *NewCont,
  299. SymbolRef Offset,
  300. BinaryOperator::Opcode Opc);
  301. ProgramStateRef rebaseSymbolInIteratorPositionsIf(
  302. ProgramStateRef State, SValBuilder &SVB, SymbolRef OldSym,
  303. SymbolRef NewSym, SymbolRef CondSym, BinaryOperator::Opcode Opc);
  304. ProgramStateRef relateSymbols(ProgramStateRef State, SymbolRef Sym1,
  305. SymbolRef Sym2, bool Equal);
  306. const ContainerData *getContainerData(ProgramStateRef State,
  307. const MemRegion *Cont);
  308. ProgramStateRef setContainerData(ProgramStateRef State, const MemRegion *Cont,
  309. const ContainerData &CData);
  310. bool hasLiveIterators(ProgramStateRef State, const MemRegion *Cont);
  311. bool isBoundThroughLazyCompoundVal(const Environment &Env,
  312. const MemRegion *Reg);
  313. bool isPastTheEnd(ProgramStateRef State, const IteratorPosition &Pos);
  314. bool isAheadOfRange(ProgramStateRef State, const IteratorPosition &Pos);
  315. bool isBehindPastTheEnd(ProgramStateRef State, const IteratorPosition &Pos);
  316. bool isZero(ProgramStateRef State, const NonLoc &Val);
  317. } // namespace
  318. IteratorChecker::IteratorChecker() {
  319. OutOfRangeBugType.reset(
  320. new BugType(this, "Iterator out of range", "Misuse of STL APIs"));
  321. MismatchedBugType.reset(
  322. new BugType(this, "Iterator(s) mismatched", "Misuse of STL APIs",
  323. /*SuppressOnSink=*/true));
  324. InvalidatedBugType.reset(
  325. new BugType(this, "Iterator invalidated", "Misuse of STL APIs"));
  326. }
  327. void IteratorChecker::checkPreCall(const CallEvent &Call,
  328. CheckerContext &C) const {
  329. // Check for out of range access or access of invalidated position and
  330. // iterator mismatches
  331. const auto *Func = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
  332. if (!Func)
  333. return;
  334. if (Func->isOverloadedOperator()) {
  335. if (ChecksEnabled[CK_InvalidatedIteratorChecker] &&
  336. isAccessOperator(Func->getOverloadedOperator())) {
  337. // Check for any kind of access of invalidated iterator positions
  338. if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
  339. verifyAccess(C, InstCall->getCXXThisVal());
  340. } else {
  341. verifyAccess(C, Call.getArgSVal(0));
  342. }
  343. }
  344. if (ChecksEnabled[CK_IteratorRangeChecker]) {
  345. if (isIncrementOperator(Func->getOverloadedOperator())) {
  346. // Check for out-of-range incrementions
  347. if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
  348. verifyIncrement(C, InstCall->getCXXThisVal());
  349. } else {
  350. if (Call.getNumArgs() >= 1) {
  351. verifyIncrement(C, Call.getArgSVal(0));
  352. }
  353. }
  354. } else if (isDecrementOperator(Func->getOverloadedOperator())) {
  355. // Check for out-of-range decrementions
  356. if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
  357. verifyDecrement(C, InstCall->getCXXThisVal());
  358. } else {
  359. if (Call.getNumArgs() >= 1) {
  360. verifyDecrement(C, Call.getArgSVal(0));
  361. }
  362. }
  363. } else if (isRandomIncrOrDecrOperator(Func->getOverloadedOperator())) {
  364. if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
  365. // Check for out-of-range incrementions and decrementions
  366. if (Call.getNumArgs() >= 1 &&
  367. Call.getArgExpr(0)->getType()->isIntegralOrEnumerationType()) {
  368. verifyRandomIncrOrDecr(C, Func->getOverloadedOperator(),
  369. InstCall->getCXXThisVal(),
  370. Call.getArgSVal(0));
  371. }
  372. } else {
  373. if (Call.getNumArgs() >= 2 &&
  374. Call.getArgExpr(1)->getType()->isIntegralOrEnumerationType()) {
  375. verifyRandomIncrOrDecr(C, Func->getOverloadedOperator(),
  376. Call.getArgSVal(0), Call.getArgSVal(1));
  377. }
  378. }
  379. } else if (isDereferenceOperator(Func->getOverloadedOperator())) {
  380. // Check for dereference of out-of-range iterators
  381. if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
  382. verifyDereference(C, InstCall->getCXXThisVal());
  383. } else {
  384. verifyDereference(C, Call.getArgSVal(0));
  385. }
  386. }
  387. } else if (ChecksEnabled[CK_MismatchedIteratorChecker] &&
  388. isComparisonOperator(Func->getOverloadedOperator())) {
  389. // Check for comparisons of iterators of different containers
  390. if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
  391. if (Call.getNumArgs() < 1)
  392. return;
  393. if (!isIteratorType(InstCall->getCXXThisExpr()->getType()) ||
  394. !isIteratorType(Call.getArgExpr(0)->getType()))
  395. return;
  396. verifyMatch(C, InstCall->getCXXThisVal(), Call.getArgSVal(0));
  397. } else {
  398. if (Call.getNumArgs() < 2)
  399. return;
  400. if (!isIteratorType(Call.getArgExpr(0)->getType()) ||
  401. !isIteratorType(Call.getArgExpr(1)->getType()))
  402. return;
  403. verifyMatch(C, Call.getArgSVal(0), Call.getArgSVal(1));
  404. }
  405. }
  406. } else if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
  407. if (!ChecksEnabled[CK_MismatchedIteratorChecker])
  408. return;
  409. const auto *ContReg = InstCall->getCXXThisVal().getAsRegion();
  410. if (!ContReg)
  411. return;
  412. // Check for erase, insert and emplace using iterator of another container
  413. if (isEraseCall(Func) || isEraseAfterCall(Func)) {
  414. verifyMatch(C, Call.getArgSVal(0),
  415. InstCall->getCXXThisVal().getAsRegion());
  416. if (Call.getNumArgs() == 2) {
  417. verifyMatch(C, Call.getArgSVal(1),
  418. InstCall->getCXXThisVal().getAsRegion());
  419. }
  420. } else if (isInsertCall(Func)) {
  421. verifyMatch(C, Call.getArgSVal(0),
  422. InstCall->getCXXThisVal().getAsRegion());
  423. if (Call.getNumArgs() == 3 &&
  424. isIteratorType(Call.getArgExpr(1)->getType()) &&
  425. isIteratorType(Call.getArgExpr(2)->getType())) {
  426. verifyMatch(C, Call.getArgSVal(1), Call.getArgSVal(2));
  427. }
  428. } else if (isEmplaceCall(Func)) {
  429. verifyMatch(C, Call.getArgSVal(0),
  430. InstCall->getCXXThisVal().getAsRegion());
  431. }
  432. } else if (isa<CXXConstructorCall>(&Call)) {
  433. // Check match of first-last iterator pair in a constructor of a container
  434. if (Call.getNumArgs() < 2)
  435. return;
  436. const auto *Ctr = cast<CXXConstructorDecl>(Call.getDecl());
  437. if (Ctr->getNumParams() < 2)
  438. return;
  439. if (Ctr->getParamDecl(0)->getName() != "first" ||
  440. Ctr->getParamDecl(1)->getName() != "last")
  441. return;
  442. if (!isIteratorType(Call.getArgExpr(0)->getType()) ||
  443. !isIteratorType(Call.getArgExpr(1)->getType()))
  444. return;
  445. verifyMatch(C, Call.getArgSVal(0), Call.getArgSVal(1));
  446. } else {
  447. // The main purpose of iterators is to abstract away from different
  448. // containers and provide a (maybe limited) uniform access to them.
  449. // This implies that any correctly written template function that
  450. // works on multiple containers using iterators takes different
  451. // template parameters for different containers. So we can safely
  452. // assume that passing iterators of different containers as arguments
  453. // whose type replaces the same template parameter is a bug.
  454. //
  455. // Example:
  456. // template<typename I1, typename I2>
  457. // void f(I1 first1, I1 last1, I2 first2, I2 last2);
  458. //
  459. // In this case the first two arguments to f() must be iterators must belong
  460. // to the same container and the last to also to the same container but
  461. // not necessarily to the same as the first two.
  462. if (!ChecksEnabled[CK_MismatchedIteratorChecker])
  463. return;
  464. const auto *Templ = Func->getPrimaryTemplate();
  465. if (!Templ)
  466. return;
  467. const auto *TParams = Templ->getTemplateParameters();
  468. const auto *TArgs = Func->getTemplateSpecializationArgs();
  469. // Iterate over all the template parameters
  470. for (size_t I = 0; I < TParams->size(); ++I) {
  471. const auto *TPDecl = dyn_cast<TemplateTypeParmDecl>(TParams->getParam(I));
  472. if (!TPDecl)
  473. continue;
  474. if (TPDecl->isParameterPack())
  475. continue;
  476. const auto TAType = TArgs->get(I).getAsType();
  477. if (!isIteratorType(TAType))
  478. continue;
  479. SVal LHS = UndefinedVal();
  480. // For every template parameter which is an iterator type in the
  481. // instantiation look for all functions' parameters' type by it and
  482. // check whether they belong to the same container
  483. for (auto J = 0U; J < Func->getNumParams(); ++J) {
  484. const auto *Param = Func->getParamDecl(J);
  485. const auto *ParamType =
  486. Param->getType()->getAs<SubstTemplateTypeParmType>();
  487. if (!ParamType ||
  488. ParamType->getReplacedParameter()->getDecl() != TPDecl)
  489. continue;
  490. if (LHS.isUndef()) {
  491. LHS = Call.getArgSVal(J);
  492. } else {
  493. verifyMatch(C, LHS, Call.getArgSVal(J));
  494. }
  495. }
  496. }
  497. }
  498. }
  499. void IteratorChecker::checkPostCall(const CallEvent &Call,
  500. CheckerContext &C) const {
  501. // Record new iterator positions and iterator position changes
  502. const auto *Func = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
  503. if (!Func)
  504. return;
  505. if (Func->isOverloadedOperator()) {
  506. const auto Op = Func->getOverloadedOperator();
  507. if (isAssignmentOperator(Op)) {
  508. // Overloaded 'operator=' must be a non-static member function.
  509. const auto *InstCall = cast<CXXInstanceCall>(&Call);
  510. if (cast<CXXMethodDecl>(Func)->isMoveAssignmentOperator()) {
  511. handleAssign(C, InstCall->getCXXThisVal(), Call.getOriginExpr(),
  512. Call.getArgSVal(0));
  513. return;
  514. }
  515. handleAssign(C, InstCall->getCXXThisVal());
  516. return;
  517. } else if (isSimpleComparisonOperator(Op)) {
  518. const auto *OrigExpr = Call.getOriginExpr();
  519. if (!OrigExpr)
  520. return;
  521. if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
  522. handleComparison(C, OrigExpr, Call.getReturnValue(),
  523. InstCall->getCXXThisVal(), Call.getArgSVal(0), Op);
  524. return;
  525. }
  526. handleComparison(C, OrigExpr, Call.getReturnValue(), Call.getArgSVal(0),
  527. Call.getArgSVal(1), Op);
  528. return;
  529. } else if (isRandomIncrOrDecrOperator(Func->getOverloadedOperator())) {
  530. if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
  531. if (Call.getNumArgs() >= 1 &&
  532. Call.getArgExpr(0)->getType()->isIntegralOrEnumerationType()) {
  533. handleRandomIncrOrDecr(C, Func->getOverloadedOperator(),
  534. Call.getReturnValue(),
  535. InstCall->getCXXThisVal(), Call.getArgSVal(0));
  536. return;
  537. }
  538. } else {
  539. if (Call.getNumArgs() >= 2 &&
  540. Call.getArgExpr(1)->getType()->isIntegralOrEnumerationType()) {
  541. handleRandomIncrOrDecr(C, Func->getOverloadedOperator(),
  542. Call.getReturnValue(), Call.getArgSVal(0),
  543. Call.getArgSVal(1));
  544. return;
  545. }
  546. }
  547. } else if (isIncrementOperator(Func->getOverloadedOperator())) {
  548. if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
  549. handleIncrement(C, Call.getReturnValue(), InstCall->getCXXThisVal(),
  550. Call.getNumArgs());
  551. return;
  552. }
  553. handleIncrement(C, Call.getReturnValue(), Call.getArgSVal(0),
  554. Call.getNumArgs());
  555. return;
  556. } else if (isDecrementOperator(Func->getOverloadedOperator())) {
  557. if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
  558. handleDecrement(C, Call.getReturnValue(), InstCall->getCXXThisVal(),
  559. Call.getNumArgs());
  560. return;
  561. }
  562. handleDecrement(C, Call.getReturnValue(), Call.getArgSVal(0),
  563. Call.getNumArgs());
  564. return;
  565. }
  566. } else {
  567. if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
  568. if (isAssignCall(Func)) {
  569. handleAssign(C, InstCall->getCXXThisVal());
  570. return;
  571. }
  572. if (isClearCall(Func)) {
  573. handleClear(C, InstCall->getCXXThisVal());
  574. return;
  575. }
  576. if (isPushBackCall(Func) || isEmplaceBackCall(Func)) {
  577. handlePushBack(C, InstCall->getCXXThisVal());
  578. return;
  579. }
  580. if (isPopBackCall(Func)) {
  581. handlePopBack(C, InstCall->getCXXThisVal());
  582. return;
  583. }
  584. if (isPushFrontCall(Func) || isEmplaceFrontCall(Func)) {
  585. handlePushFront(C, InstCall->getCXXThisVal());
  586. return;
  587. }
  588. if (isPopFrontCall(Func)) {
  589. handlePopFront(C, InstCall->getCXXThisVal());
  590. return;
  591. }
  592. if (isInsertCall(Func) || isEmplaceCall(Func)) {
  593. handleInsert(C, Call.getArgSVal(0));
  594. return;
  595. }
  596. if (isEraseCall(Func)) {
  597. if (Call.getNumArgs() == 1) {
  598. handleErase(C, Call.getArgSVal(0));
  599. return;
  600. }
  601. if (Call.getNumArgs() == 2) {
  602. handleErase(C, Call.getArgSVal(0), Call.getArgSVal(1));
  603. return;
  604. }
  605. }
  606. if (isEraseAfterCall(Func)) {
  607. if (Call.getNumArgs() == 1) {
  608. handleEraseAfter(C, Call.getArgSVal(0));
  609. return;
  610. }
  611. if (Call.getNumArgs() == 2) {
  612. handleEraseAfter(C, Call.getArgSVal(0), Call.getArgSVal(1));
  613. return;
  614. }
  615. }
  616. }
  617. const auto *OrigExpr = Call.getOriginExpr();
  618. if (!OrigExpr)
  619. return;
  620. if (!isIteratorType(Call.getResultType()))
  621. return;
  622. auto State = C.getState();
  623. if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
  624. if (isBeginCall(Func)) {
  625. handleBegin(C, OrigExpr, Call.getReturnValue(),
  626. InstCall->getCXXThisVal());
  627. return;
  628. }
  629. if (isEndCall(Func)) {
  630. handleEnd(C, OrigExpr, Call.getReturnValue(),
  631. InstCall->getCXXThisVal());
  632. return;
  633. }
  634. }
  635. // Already bound to container?
  636. if (getIteratorPosition(State, Call.getReturnValue()))
  637. return;
  638. // Copy-like and move constructors
  639. if (isa<CXXConstructorCall>(&Call) && Call.getNumArgs() == 1) {
  640. if (const auto *Pos = getIteratorPosition(State, Call.getArgSVal(0))) {
  641. State = setIteratorPosition(State, Call.getReturnValue(), *Pos);
  642. if (cast<CXXConstructorDecl>(Func)->isMoveConstructor()) {
  643. State = removeIteratorPosition(State, Call.getArgSVal(0));
  644. }
  645. C.addTransition(State);
  646. return;
  647. }
  648. }
  649. // Assumption: if return value is an iterator which is not yet bound to a
  650. // container, then look for the first iterator argument, and
  651. // bind the return value to the same container. This approach
  652. // works for STL algorithms.
  653. // FIXME: Add a more conservative mode
  654. for (unsigned i = 0; i < Call.getNumArgs(); ++i) {
  655. if (isIteratorType(Call.getArgExpr(i)->getType())) {
  656. if (const auto *Pos = getIteratorPosition(State, Call.getArgSVal(i))) {
  657. assignToContainer(C, OrigExpr, Call.getReturnValue(),
  658. Pos->getContainer());
  659. return;
  660. }
  661. }
  662. }
  663. }
  664. }
  665. void IteratorChecker::checkBind(SVal Loc, SVal Val, const Stmt *S,
  666. CheckerContext &C) const {
  667. auto State = C.getState();
  668. const auto *Pos = getIteratorPosition(State, Val);
  669. if (Pos) {
  670. State = setIteratorPosition(State, Loc, *Pos);
  671. C.addTransition(State);
  672. } else {
  673. const auto *OldPos = getIteratorPosition(State, Loc);
  674. if (OldPos) {
  675. State = removeIteratorPosition(State, Loc);
  676. C.addTransition(State);
  677. }
  678. }
  679. }
  680. void IteratorChecker::checkPostStmt(const MaterializeTemporaryExpr *MTE,
  681. CheckerContext &C) const {
  682. /* Transfer iterator state to temporary objects */
  683. auto State = C.getState();
  684. const auto *Pos =
  685. getIteratorPosition(State, C.getSVal(MTE->GetTemporaryExpr()));
  686. if (!Pos)
  687. return;
  688. State = setIteratorPosition(State, C.getSVal(MTE), *Pos);
  689. C.addTransition(State);
  690. }
  691. void IteratorChecker::checkLiveSymbols(ProgramStateRef State,
  692. SymbolReaper &SR) const {
  693. // Keep symbolic expressions of iterator positions, container begins and ends
  694. // alive
  695. auto RegionMap = State->get<IteratorRegionMap>();
  696. for (const auto Reg : RegionMap) {
  697. const auto Offset = Reg.second.getOffset();
  698. for (auto i = Offset->symbol_begin(); i != Offset->symbol_end(); ++i)
  699. if (isa<SymbolData>(*i))
  700. SR.markLive(*i);
  701. }
  702. auto SymbolMap = State->get<IteratorSymbolMap>();
  703. for (const auto Sym : SymbolMap) {
  704. const auto Offset = Sym.second.getOffset();
  705. for (auto i = Offset->symbol_begin(); i != Offset->symbol_end(); ++i)
  706. if (isa<SymbolData>(*i))
  707. SR.markLive(*i);
  708. }
  709. auto ContMap = State->get<ContainerMap>();
  710. for (const auto Cont : ContMap) {
  711. const auto CData = Cont.second;
  712. if (CData.getBegin()) {
  713. SR.markLive(CData.getBegin());
  714. if(const auto *SIE = dyn_cast<SymIntExpr>(CData.getBegin()))
  715. SR.markLive(SIE->getLHS());
  716. }
  717. if (CData.getEnd()) {
  718. SR.markLive(CData.getEnd());
  719. if(const auto *SIE = dyn_cast<SymIntExpr>(CData.getEnd()))
  720. SR.markLive(SIE->getLHS());
  721. }
  722. }
  723. }
  724. void IteratorChecker::checkDeadSymbols(SymbolReaper &SR,
  725. CheckerContext &C) const {
  726. // Cleanup
  727. auto State = C.getState();
  728. auto RegionMap = State->get<IteratorRegionMap>();
  729. for (const auto Reg : RegionMap) {
  730. if (!SR.isLiveRegion(Reg.first)) {
  731. // The region behind the `LazyCompoundVal` is often cleaned up before
  732. // the `LazyCompoundVal` itself. If there are iterator positions keyed
  733. // by these regions their cleanup must be deferred.
  734. if (!isBoundThroughLazyCompoundVal(State->getEnvironment(), Reg.first)) {
  735. State = State->remove<IteratorRegionMap>(Reg.first);
  736. }
  737. }
  738. }
  739. auto SymbolMap = State->get<IteratorSymbolMap>();
  740. for (const auto Sym : SymbolMap) {
  741. if (!SR.isLive(Sym.first)) {
  742. State = State->remove<IteratorSymbolMap>(Sym.first);
  743. }
  744. }
  745. auto ContMap = State->get<ContainerMap>();
  746. for (const auto Cont : ContMap) {
  747. if (!SR.isLiveRegion(Cont.first)) {
  748. // We must keep the container data while it has live iterators to be able
  749. // to compare them to the begin and the end of the container.
  750. if (!hasLiveIterators(State, Cont.first)) {
  751. State = State->remove<ContainerMap>(Cont.first);
  752. }
  753. }
  754. }
  755. C.addTransition(State);
  756. }
  757. void IteratorChecker::handleComparison(CheckerContext &C, const Expr *CE,
  758. const SVal &RetVal, const SVal &LVal,
  759. const SVal &RVal,
  760. OverloadedOperatorKind Op) const {
  761. // Record the operands and the operator of the comparison for the next
  762. // evalAssume, if the result is a symbolic expression. If it is a concrete
  763. // value (only one branch is possible), then transfer the state between
  764. // the operands according to the operator and the result
  765. auto State = C.getState();
  766. const auto *LPos = getIteratorPosition(State, LVal);
  767. const auto *RPos = getIteratorPosition(State, RVal);
  768. const MemRegion *Cont = nullptr;
  769. if (LPos) {
  770. Cont = LPos->getContainer();
  771. } else if (RPos) {
  772. Cont = RPos->getContainer();
  773. }
  774. if (!Cont)
  775. return;
  776. // At least one of the iterators have recorded positions. If one of them has
  777. // not then create a new symbol for the offset.
  778. SymbolRef Sym;
  779. if (!LPos || !RPos) {
  780. auto &SymMgr = C.getSymbolManager();
  781. Sym = SymMgr.conjureSymbol(CE, C.getLocationContext(),
  782. C.getASTContext().LongTy, C.blockCount());
  783. State = assumeNoOverflow(State, Sym, 4);
  784. }
  785. if (!LPos) {
  786. State = setIteratorPosition(State, LVal,
  787. IteratorPosition::getPosition(Cont, Sym));
  788. LPos = getIteratorPosition(State, LVal);
  789. } else if (!RPos) {
  790. State = setIteratorPosition(State, RVal,
  791. IteratorPosition::getPosition(Cont, Sym));
  792. RPos = getIteratorPosition(State, RVal);
  793. }
  794. processComparison(C, State, LPos->getOffset(), RPos->getOffset(), RetVal, Op);
  795. }
  796. void IteratorChecker::processComparison(CheckerContext &C,
  797. ProgramStateRef State, SymbolRef Sym1,
  798. SymbolRef Sym2, const SVal &RetVal,
  799. OverloadedOperatorKind Op) const {
  800. if (const auto TruthVal = RetVal.getAs<nonloc::ConcreteInt>()) {
  801. if ((State = relateSymbols(State, Sym1, Sym2,
  802. (Op == OO_EqualEqual) ==
  803. (TruthVal->getValue() != 0)))) {
  804. C.addTransition(State);
  805. } else {
  806. C.generateSink(State, C.getPredecessor());
  807. }
  808. return;
  809. }
  810. const auto ConditionVal = RetVal.getAs<DefinedSVal>();
  811. if (!ConditionVal)
  812. return;
  813. if (auto StateTrue = relateSymbols(State, Sym1, Sym2, Op == OO_EqualEqual)) {
  814. StateTrue = StateTrue->assume(*ConditionVal, true);
  815. C.addTransition(StateTrue);
  816. }
  817. if (auto StateFalse = relateSymbols(State, Sym1, Sym2, Op != OO_EqualEqual)) {
  818. StateFalse = StateFalse->assume(*ConditionVal, false);
  819. C.addTransition(StateFalse);
  820. }
  821. }
  822. void IteratorChecker::verifyDereference(CheckerContext &C,
  823. const SVal &Val) const {
  824. auto State = C.getState();
  825. const auto *Pos = getIteratorPosition(State, Val);
  826. if (Pos && isPastTheEnd(State, *Pos)) {
  827. auto *N = C.generateErrorNode(State);
  828. if (!N)
  829. return;
  830. reportOutOfRangeBug("Past-the-end iterator dereferenced.", Val, C, N);
  831. return;
  832. }
  833. }
  834. void IteratorChecker::verifyAccess(CheckerContext &C, const SVal &Val) const {
  835. auto State = C.getState();
  836. const auto *Pos = getIteratorPosition(State, Val);
  837. if (Pos && !Pos->isValid()) {
  838. auto *N = C.generateErrorNode(State);
  839. if (!N) {
  840. return;
  841. }
  842. reportInvalidatedBug("Invalidated iterator accessed.", Val, C, N);
  843. }
  844. }
  845. void IteratorChecker::handleIncrement(CheckerContext &C, const SVal &RetVal,
  846. const SVal &Iter, bool Postfix) const {
  847. // Increment the symbolic expressions which represents the position of the
  848. // iterator
  849. auto State = C.getState();
  850. const auto *Pos = getIteratorPosition(State, Iter);
  851. if (Pos) {
  852. auto &SymMgr = C.getSymbolManager();
  853. auto &BVF = SymMgr.getBasicVals();
  854. const auto NewPos =
  855. advancePosition(C, OO_Plus, *Pos,
  856. nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))));
  857. State = setIteratorPosition(State, Iter, NewPos);
  858. State = setIteratorPosition(State, RetVal, Postfix ? *Pos : NewPos);
  859. C.addTransition(State);
  860. }
  861. }
  862. void IteratorChecker::handleDecrement(CheckerContext &C, const SVal &RetVal,
  863. const SVal &Iter, bool Postfix) const {
  864. // Decrement the symbolic expressions which represents the position of the
  865. // iterator
  866. auto State = C.getState();
  867. const auto *Pos = getIteratorPosition(State, Iter);
  868. if (Pos) {
  869. auto &SymMgr = C.getSymbolManager();
  870. auto &BVF = SymMgr.getBasicVals();
  871. const auto NewPos =
  872. advancePosition(C, OO_Minus, *Pos,
  873. nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))));
  874. State = setIteratorPosition(State, Iter, NewPos);
  875. State = setIteratorPosition(State, RetVal, Postfix ? *Pos : NewPos);
  876. C.addTransition(State);
  877. }
  878. }
  879. void IteratorChecker::handleRandomIncrOrDecr(CheckerContext &C,
  880. OverloadedOperatorKind Op,
  881. const SVal &RetVal,
  882. const SVal &LHS,
  883. const SVal &RHS) const {
  884. // Increment or decrement the symbolic expressions which represents the
  885. // position of the iterator
  886. auto State = C.getState();
  887. const auto *Pos = getIteratorPosition(State, LHS);
  888. if (!Pos)
  889. return;
  890. const auto *value = &RHS;
  891. if (auto loc = RHS.getAs<Loc>()) {
  892. const auto val = State->getRawSVal(*loc);
  893. value = &val;
  894. }
  895. auto &TgtVal = (Op == OO_PlusEqual || Op == OO_MinusEqual) ? LHS : RetVal;
  896. State =
  897. setIteratorPosition(State, TgtVal, advancePosition(C, Op, *Pos, *value));
  898. C.addTransition(State);
  899. }
  900. void IteratorChecker::verifyIncrement(CheckerContext &C,
  901. const SVal &Iter) const {
  902. auto &BVF = C.getSValBuilder().getBasicValueFactory();
  903. verifyRandomIncrOrDecr(C, OO_Plus, Iter,
  904. nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))));
  905. }
  906. void IteratorChecker::verifyDecrement(CheckerContext &C,
  907. const SVal &Iter) const {
  908. auto &BVF = C.getSValBuilder().getBasicValueFactory();
  909. verifyRandomIncrOrDecr(C, OO_Minus, Iter,
  910. nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))));
  911. }
  912. void IteratorChecker::verifyRandomIncrOrDecr(CheckerContext &C,
  913. OverloadedOperatorKind Op,
  914. const SVal &LHS,
  915. const SVal &RHS) const {
  916. auto State = C.getState();
  917. // If the iterator is initially inside its range, then the operation is valid
  918. const auto *Pos = getIteratorPosition(State, LHS);
  919. if (!Pos)
  920. return;
  921. auto Value = RHS;
  922. if (auto ValAsLoc = RHS.getAs<Loc>()) {
  923. Value = State->getRawSVal(*ValAsLoc);
  924. }
  925. if (Value.isUnknown())
  926. return;
  927. // Incremention or decremention by 0 is never a bug.
  928. if (isZero(State, Value.castAs<NonLoc>()))
  929. return;
  930. // The result may be the past-end iterator of the container, but any other
  931. // out of range position is undefined behaviour
  932. if (isAheadOfRange(State, advancePosition(C, Op, *Pos, Value))) {
  933. auto *N = C.generateErrorNode(State);
  934. if (!N)
  935. return;
  936. reportOutOfRangeBug("Iterator decremented ahead of its valid range.", LHS,
  937. C, N);
  938. }
  939. if (isBehindPastTheEnd(State, advancePosition(C, Op, *Pos, Value))) {
  940. auto *N = C.generateErrorNode(State);
  941. if (!N)
  942. return;
  943. reportOutOfRangeBug("Iterator incremented behind the past-the-end "
  944. "iterator.", LHS, C, N);
  945. }
  946. }
  947. void IteratorChecker::verifyMatch(CheckerContext &C, const SVal &Iter,
  948. const MemRegion *Cont) const {
  949. // Verify match between a container and the container of an iterator
  950. Cont = Cont->getMostDerivedObjectRegion();
  951. if (const auto *ContSym = Cont->getSymbolicBase()) {
  952. if (isa<SymbolConjured>(ContSym->getSymbol()))
  953. return;
  954. }
  955. auto State = C.getState();
  956. const auto *Pos = getIteratorPosition(State, Iter);
  957. if (!Pos)
  958. return;
  959. const auto *IterCont = Pos->getContainer();
  960. // Skip symbolic regions based on conjured symbols. Two conjured symbols
  961. // may or may not be the same. For example, the same function can return
  962. // the same or a different container but we get different conjured symbols
  963. // for each call. This may cause false positives so omit them from the check.
  964. if (const auto *ContSym = IterCont->getSymbolicBase()) {
  965. if (isa<SymbolConjured>(ContSym->getSymbol()))
  966. return;
  967. }
  968. if (IterCont != Cont) {
  969. auto *N = C.generateNonFatalErrorNode(State);
  970. if (!N) {
  971. return;
  972. }
  973. reportMismatchedBug("Container accessed using foreign iterator argument.",
  974. Iter, Cont, C, N);
  975. }
  976. }
  977. void IteratorChecker::verifyMatch(CheckerContext &C, const SVal &Iter1,
  978. const SVal &Iter2) const {
  979. // Verify match between the containers of two iterators
  980. auto State = C.getState();
  981. const auto *Pos1 = getIteratorPosition(State, Iter1);
  982. if (!Pos1)
  983. return;
  984. const auto *IterCont1 = Pos1->getContainer();
  985. // Skip symbolic regions based on conjured symbols. Two conjured symbols
  986. // may or may not be the same. For example, the same function can return
  987. // the same or a different container but we get different conjured symbols
  988. // for each call. This may cause false positives so omit them from the check.
  989. if (const auto *ContSym = IterCont1->getSymbolicBase()) {
  990. if (isa<SymbolConjured>(ContSym->getSymbol()))
  991. return;
  992. }
  993. const auto *Pos2 = getIteratorPosition(State, Iter2);
  994. if (!Pos2)
  995. return;
  996. const auto *IterCont2 = Pos2->getContainer();
  997. if (const auto *ContSym = IterCont2->getSymbolicBase()) {
  998. if (isa<SymbolConjured>(ContSym->getSymbol()))
  999. return;
  1000. }
  1001. if (IterCont1 != IterCont2) {
  1002. auto *N = C.generateNonFatalErrorNode(State);
  1003. if (!N)
  1004. return;
  1005. reportMismatchedBug("Iterators of different containers used where the "
  1006. "same container is expected.", Iter1, Iter2, C, N);
  1007. }
  1008. }
  1009. void IteratorChecker::handleBegin(CheckerContext &C, const Expr *CE,
  1010. const SVal &RetVal, const SVal &Cont) const {
  1011. const auto *ContReg = Cont.getAsRegion();
  1012. if (!ContReg)
  1013. return;
  1014. ContReg = ContReg->getMostDerivedObjectRegion();
  1015. // If the container already has a begin symbol then use it. Otherwise first
  1016. // create a new one.
  1017. auto State = C.getState();
  1018. auto BeginSym = getContainerBegin(State, ContReg);
  1019. if (!BeginSym) {
  1020. State = createContainerBegin(State, ContReg, CE, C.getASTContext().LongTy,
  1021. C.getLocationContext(), C.blockCount());
  1022. BeginSym = getContainerBegin(State, ContReg);
  1023. }
  1024. State = setIteratorPosition(State, RetVal,
  1025. IteratorPosition::getPosition(ContReg, BeginSym));
  1026. C.addTransition(State);
  1027. }
  1028. void IteratorChecker::handleEnd(CheckerContext &C, const Expr *CE,
  1029. const SVal &RetVal, const SVal &Cont) const {
  1030. const auto *ContReg = Cont.getAsRegion();
  1031. if (!ContReg)
  1032. return;
  1033. ContReg = ContReg->getMostDerivedObjectRegion();
  1034. // If the container already has an end symbol then use it. Otherwise first
  1035. // create a new one.
  1036. auto State = C.getState();
  1037. auto EndSym = getContainerEnd(State, ContReg);
  1038. if (!EndSym) {
  1039. State = createContainerEnd(State, ContReg, CE, C.getASTContext().LongTy,
  1040. C.getLocationContext(), C.blockCount());
  1041. EndSym = getContainerEnd(State, ContReg);
  1042. }
  1043. State = setIteratorPosition(State, RetVal,
  1044. IteratorPosition::getPosition(ContReg, EndSym));
  1045. C.addTransition(State);
  1046. }
  1047. void IteratorChecker::assignToContainer(CheckerContext &C, const Expr *CE,
  1048. const SVal &RetVal,
  1049. const MemRegion *Cont) const {
  1050. Cont = Cont->getMostDerivedObjectRegion();
  1051. auto State = C.getState();
  1052. auto &SymMgr = C.getSymbolManager();
  1053. auto Sym = SymMgr.conjureSymbol(CE, C.getLocationContext(),
  1054. C.getASTContext().LongTy, C.blockCount());
  1055. State = assumeNoOverflow(State, Sym, 4);
  1056. State = setIteratorPosition(State, RetVal,
  1057. IteratorPosition::getPosition(Cont, Sym));
  1058. C.addTransition(State);
  1059. }
  1060. void IteratorChecker::handleAssign(CheckerContext &C, const SVal &Cont,
  1061. const Expr *CE, const SVal &OldCont) const {
  1062. const auto *ContReg = Cont.getAsRegion();
  1063. if (!ContReg)
  1064. return;
  1065. ContReg = ContReg->getMostDerivedObjectRegion();
  1066. // Assignment of a new value to a container always invalidates all its
  1067. // iterators
  1068. auto State = C.getState();
  1069. const auto CData = getContainerData(State, ContReg);
  1070. if (CData) {
  1071. State = invalidateAllIteratorPositions(State, ContReg);
  1072. }
  1073. // In case of move, iterators of the old container (except the past-end
  1074. // iterators) remain valid but refer to the new container
  1075. if (!OldCont.isUndef()) {
  1076. const auto *OldContReg = OldCont.getAsRegion();
  1077. if (OldContReg) {
  1078. OldContReg = OldContReg->getMostDerivedObjectRegion();
  1079. const auto OldCData = getContainerData(State, OldContReg);
  1080. if (OldCData) {
  1081. if (const auto OldEndSym = OldCData->getEnd()) {
  1082. // If we already assigned an "end" symbol to the old container, then
  1083. // first reassign all iterator positions to the new container which
  1084. // are not past the container (thus not greater or equal to the
  1085. // current "end" symbol).
  1086. State = reassignAllIteratorPositionsUnless(State, OldContReg, ContReg,
  1087. OldEndSym, BO_GE);
  1088. auto &SymMgr = C.getSymbolManager();
  1089. auto &SVB = C.getSValBuilder();
  1090. // Then generate and assign a new "end" symbol for the new container.
  1091. auto NewEndSym =
  1092. SymMgr.conjureSymbol(CE, C.getLocationContext(),
  1093. C.getASTContext().LongTy, C.blockCount());
  1094. State = assumeNoOverflow(State, NewEndSym, 4);
  1095. if (CData) {
  1096. State = setContainerData(State, ContReg, CData->newEnd(NewEndSym));
  1097. } else {
  1098. State = setContainerData(State, ContReg,
  1099. ContainerData::fromEnd(NewEndSym));
  1100. }
  1101. // Finally, replace the old "end" symbol in the already reassigned
  1102. // iterator positions with the new "end" symbol.
  1103. State = rebaseSymbolInIteratorPositionsIf(
  1104. State, SVB, OldEndSym, NewEndSym, OldEndSym, BO_LT);
  1105. } else {
  1106. // There was no "end" symbol assigned yet to the old container,
  1107. // so reassign all iterator positions to the new container.
  1108. State = reassignAllIteratorPositions(State, OldContReg, ContReg);
  1109. }
  1110. if (const auto OldBeginSym = OldCData->getBegin()) {
  1111. // If we already assigned a "begin" symbol to the old container, then
  1112. // assign it to the new container and remove it from the old one.
  1113. if (CData) {
  1114. State =
  1115. setContainerData(State, ContReg, CData->newBegin(OldBeginSym));
  1116. } else {
  1117. State = setContainerData(State, ContReg,
  1118. ContainerData::fromBegin(OldBeginSym));
  1119. }
  1120. State =
  1121. setContainerData(State, OldContReg, OldCData->newEnd(nullptr));
  1122. }
  1123. } else {
  1124. // There was neither "begin" nor "end" symbol assigned yet to the old
  1125. // container, so reassign all iterator positions to the new container.
  1126. State = reassignAllIteratorPositions(State, OldContReg, ContReg);
  1127. }
  1128. }
  1129. }
  1130. C.addTransition(State);
  1131. }
  1132. void IteratorChecker::handleClear(CheckerContext &C, const SVal &Cont) const {
  1133. const auto *ContReg = Cont.getAsRegion();
  1134. if (!ContReg)
  1135. return;
  1136. ContReg = ContReg->getMostDerivedObjectRegion();
  1137. // The clear() operation invalidates all the iterators, except the past-end
  1138. // iterators of list-like containers
  1139. auto State = C.getState();
  1140. if (!hasSubscriptOperator(State, ContReg) ||
  1141. !backModifiable(State, ContReg)) {
  1142. const auto CData = getContainerData(State, ContReg);
  1143. if (CData) {
  1144. if (const auto EndSym = CData->getEnd()) {
  1145. State =
  1146. invalidateAllIteratorPositionsExcept(State, ContReg, EndSym, BO_GE);
  1147. C.addTransition(State);
  1148. return;
  1149. }
  1150. }
  1151. }
  1152. State = invalidateAllIteratorPositions(State, ContReg);
  1153. C.addTransition(State);
  1154. }
  1155. void IteratorChecker::handlePushBack(CheckerContext &C,
  1156. const SVal &Cont) const {
  1157. const auto *ContReg = Cont.getAsRegion();
  1158. if (!ContReg)
  1159. return;
  1160. ContReg = ContReg->getMostDerivedObjectRegion();
  1161. // For deque-like containers invalidate all iterator positions
  1162. auto State = C.getState();
  1163. if (hasSubscriptOperator(State, ContReg) && frontModifiable(State, ContReg)) {
  1164. State = invalidateAllIteratorPositions(State, ContReg);
  1165. C.addTransition(State);
  1166. return;
  1167. }
  1168. const auto CData = getContainerData(State, ContReg);
  1169. if (!CData)
  1170. return;
  1171. // For vector-like containers invalidate the past-end iterator positions
  1172. if (const auto EndSym = CData->getEnd()) {
  1173. if (hasSubscriptOperator(State, ContReg)) {
  1174. State = invalidateIteratorPositions(State, EndSym, BO_GE);
  1175. }
  1176. auto &SymMgr = C.getSymbolManager();
  1177. auto &BVF = SymMgr.getBasicVals();
  1178. auto &SVB = C.getSValBuilder();
  1179. const auto newEndSym =
  1180. SVB.evalBinOp(State, BO_Add,
  1181. nonloc::SymbolVal(EndSym),
  1182. nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))),
  1183. SymMgr.getType(EndSym)).getAsSymbol();
  1184. State = setContainerData(State, ContReg, CData->newEnd(newEndSym));
  1185. }
  1186. C.addTransition(State);
  1187. }
  1188. void IteratorChecker::handlePopBack(CheckerContext &C, const SVal &Cont) const {
  1189. const auto *ContReg = Cont.getAsRegion();
  1190. if (!ContReg)
  1191. return;
  1192. ContReg = ContReg->getMostDerivedObjectRegion();
  1193. auto State = C.getState();
  1194. const auto CData = getContainerData(State, ContReg);
  1195. if (!CData)
  1196. return;
  1197. if (const auto EndSym = CData->getEnd()) {
  1198. auto &SymMgr = C.getSymbolManager();
  1199. auto &BVF = SymMgr.getBasicVals();
  1200. auto &SVB = C.getSValBuilder();
  1201. const auto BackSym =
  1202. SVB.evalBinOp(State, BO_Sub,
  1203. nonloc::SymbolVal(EndSym),
  1204. nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))),
  1205. SymMgr.getType(EndSym)).getAsSymbol();
  1206. // For vector-like and deque-like containers invalidate the last and the
  1207. // past-end iterator positions. For list-like containers only invalidate
  1208. // the last position
  1209. if (hasSubscriptOperator(State, ContReg) &&
  1210. backModifiable(State, ContReg)) {
  1211. State = invalidateIteratorPositions(State, BackSym, BO_GE);
  1212. State = setContainerData(State, ContReg, CData->newEnd(nullptr));
  1213. } else {
  1214. State = invalidateIteratorPositions(State, BackSym, BO_EQ);
  1215. }
  1216. auto newEndSym = BackSym;
  1217. State = setContainerData(State, ContReg, CData->newEnd(newEndSym));
  1218. C.addTransition(State);
  1219. }
  1220. }
  1221. void IteratorChecker::handlePushFront(CheckerContext &C,
  1222. const SVal &Cont) const {
  1223. const auto *ContReg = Cont.getAsRegion();
  1224. if (!ContReg)
  1225. return;
  1226. ContReg = ContReg->getMostDerivedObjectRegion();
  1227. // For deque-like containers invalidate all iterator positions
  1228. auto State = C.getState();
  1229. if (hasSubscriptOperator(State, ContReg)) {
  1230. State = invalidateAllIteratorPositions(State, ContReg);
  1231. C.addTransition(State);
  1232. } else {
  1233. const auto CData = getContainerData(State, ContReg);
  1234. if (!CData)
  1235. return;
  1236. if (const auto BeginSym = CData->getBegin()) {
  1237. auto &SymMgr = C.getSymbolManager();
  1238. auto &BVF = SymMgr.getBasicVals();
  1239. auto &SVB = C.getSValBuilder();
  1240. const auto newBeginSym =
  1241. SVB.evalBinOp(State, BO_Sub,
  1242. nonloc::SymbolVal(BeginSym),
  1243. nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))),
  1244. SymMgr.getType(BeginSym)).getAsSymbol();
  1245. State = setContainerData(State, ContReg, CData->newBegin(newBeginSym));
  1246. C.addTransition(State);
  1247. }
  1248. }
  1249. }
  1250. void IteratorChecker::handlePopFront(CheckerContext &C,
  1251. const SVal &Cont) const {
  1252. const auto *ContReg = Cont.getAsRegion();
  1253. if (!ContReg)
  1254. return;
  1255. ContReg = ContReg->getMostDerivedObjectRegion();
  1256. auto State = C.getState();
  1257. const auto CData = getContainerData(State, ContReg);
  1258. if (!CData)
  1259. return;
  1260. // For deque-like containers invalidate all iterator positions. For list-like
  1261. // iterators only invalidate the first position
  1262. if (const auto BeginSym = CData->getBegin()) {
  1263. if (hasSubscriptOperator(State, ContReg)) {
  1264. State = invalidateIteratorPositions(State, BeginSym, BO_LE);
  1265. } else {
  1266. State = invalidateIteratorPositions(State, BeginSym, BO_EQ);
  1267. }
  1268. auto &SymMgr = C.getSymbolManager();
  1269. auto &BVF = SymMgr.getBasicVals();
  1270. auto &SVB = C.getSValBuilder();
  1271. const auto newBeginSym =
  1272. SVB.evalBinOp(State, BO_Add,
  1273. nonloc::SymbolVal(BeginSym),
  1274. nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))),
  1275. SymMgr.getType(BeginSym)).getAsSymbol();
  1276. State = setContainerData(State, ContReg, CData->newBegin(newBeginSym));
  1277. C.addTransition(State);
  1278. }
  1279. }
  1280. void IteratorChecker::handleInsert(CheckerContext &C, const SVal &Iter) const {
  1281. auto State = C.getState();
  1282. const auto *Pos = getIteratorPosition(State, Iter);
  1283. if (!Pos)
  1284. return;
  1285. // For deque-like containers invalidate all iterator positions. For
  1286. // vector-like containers invalidate iterator positions after the insertion.
  1287. const auto *Cont = Pos->getContainer();
  1288. if (hasSubscriptOperator(State, Cont) && backModifiable(State, Cont)) {
  1289. if (frontModifiable(State, Cont)) {
  1290. State = invalidateAllIteratorPositions(State, Cont);
  1291. } else {
  1292. State = invalidateIteratorPositions(State, Pos->getOffset(), BO_GE);
  1293. }
  1294. if (const auto *CData = getContainerData(State, Cont)) {
  1295. if (const auto EndSym = CData->getEnd()) {
  1296. State = invalidateIteratorPositions(State, EndSym, BO_GE);
  1297. State = setContainerData(State, Cont, CData->newEnd(nullptr));
  1298. }
  1299. }
  1300. C.addTransition(State);
  1301. }
  1302. }
  1303. void IteratorChecker::handleErase(CheckerContext &C, const SVal &Iter) const {
  1304. auto State = C.getState();
  1305. const auto *Pos = getIteratorPosition(State, Iter);
  1306. if (!Pos)
  1307. return;
  1308. // For deque-like containers invalidate all iterator positions. For
  1309. // vector-like containers invalidate iterator positions at and after the
  1310. // deletion. For list-like containers only invalidate the deleted position.
  1311. const auto *Cont = Pos->getContainer();
  1312. if (hasSubscriptOperator(State, Cont) && backModifiable(State, Cont)) {
  1313. if (frontModifiable(State, Cont)) {
  1314. State = invalidateAllIteratorPositions(State, Cont);
  1315. } else {
  1316. State = invalidateIteratorPositions(State, Pos->getOffset(), BO_GE);
  1317. }
  1318. if (const auto *CData = getContainerData(State, Cont)) {
  1319. if (const auto EndSym = CData->getEnd()) {
  1320. State = invalidateIteratorPositions(State, EndSym, BO_GE);
  1321. State = setContainerData(State, Cont, CData->newEnd(nullptr));
  1322. }
  1323. }
  1324. } else {
  1325. State = invalidateIteratorPositions(State, Pos->getOffset(), BO_EQ);
  1326. }
  1327. C.addTransition(State);
  1328. }
  1329. void IteratorChecker::handleErase(CheckerContext &C, const SVal &Iter1,
  1330. const SVal &Iter2) const {
  1331. auto State = C.getState();
  1332. const auto *Pos1 = getIteratorPosition(State, Iter1);
  1333. const auto *Pos2 = getIteratorPosition(State, Iter2);
  1334. if (!Pos1 || !Pos2)
  1335. return;
  1336. // For deque-like containers invalidate all iterator positions. For
  1337. // vector-like containers invalidate iterator positions at and after the
  1338. // deletion range. For list-like containers only invalidate the deleted
  1339. // position range [first..last].
  1340. const auto *Cont = Pos1->getContainer();
  1341. if (hasSubscriptOperator(State, Cont) && backModifiable(State, Cont)) {
  1342. if (frontModifiable(State, Cont)) {
  1343. State = invalidateAllIteratorPositions(State, Cont);
  1344. } else {
  1345. State = invalidateIteratorPositions(State, Pos1->getOffset(), BO_GE);
  1346. }
  1347. if (const auto *CData = getContainerData(State, Cont)) {
  1348. if (const auto EndSym = CData->getEnd()) {
  1349. State = invalidateIteratorPositions(State, EndSym, BO_GE);
  1350. State = setContainerData(State, Cont, CData->newEnd(nullptr));
  1351. }
  1352. }
  1353. } else {
  1354. State = invalidateIteratorPositions(State, Pos1->getOffset(), BO_GE,
  1355. Pos2->getOffset(), BO_LT);
  1356. }
  1357. C.addTransition(State);
  1358. }
  1359. void IteratorChecker::handleEraseAfter(CheckerContext &C,
  1360. const SVal &Iter) const {
  1361. auto State = C.getState();
  1362. const auto *Pos = getIteratorPosition(State, Iter);
  1363. if (!Pos)
  1364. return;
  1365. // Invalidate the deleted iterator position, which is the position of the
  1366. // parameter plus one.
  1367. auto &SymMgr = C.getSymbolManager();
  1368. auto &BVF = SymMgr.getBasicVals();
  1369. auto &SVB = C.getSValBuilder();
  1370. const auto NextSym =
  1371. SVB.evalBinOp(State, BO_Add,
  1372. nonloc::SymbolVal(Pos->getOffset()),
  1373. nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(1))),
  1374. SymMgr.getType(Pos->getOffset())).getAsSymbol();
  1375. State = invalidateIteratorPositions(State, NextSym, BO_EQ);
  1376. C.addTransition(State);
  1377. }
  1378. void IteratorChecker::handleEraseAfter(CheckerContext &C, const SVal &Iter1,
  1379. const SVal &Iter2) const {
  1380. auto State = C.getState();
  1381. const auto *Pos1 = getIteratorPosition(State, Iter1);
  1382. const auto *Pos2 = getIteratorPosition(State, Iter2);
  1383. if (!Pos1 || !Pos2)
  1384. return;
  1385. // Invalidate the deleted iterator position range (first..last)
  1386. State = invalidateIteratorPositions(State, Pos1->getOffset(), BO_GT,
  1387. Pos2->getOffset(), BO_LT);
  1388. C.addTransition(State);
  1389. }
  1390. IteratorPosition IteratorChecker::advancePosition(CheckerContext &C,
  1391. OverloadedOperatorKind Op,
  1392. const IteratorPosition &Pos,
  1393. const SVal &Distance) const {
  1394. auto State = C.getState();
  1395. auto &SymMgr = C.getSymbolManager();
  1396. auto &SVB = C.getSValBuilder();
  1397. assert ((Op == OO_Plus || Op == OO_PlusEqual ||
  1398. Op == OO_Minus || Op == OO_MinusEqual) &&
  1399. "Advance operator must be one of +, -, += and -=.");
  1400. auto BinOp = (Op == OO_Plus || Op == OO_PlusEqual) ? BO_Add : BO_Sub;
  1401. if (const auto IntDist = Distance.getAs<nonloc::ConcreteInt>()) {
  1402. // For concrete integers we can calculate the new position
  1403. return Pos.setTo(SVB.evalBinOp(State, BinOp,
  1404. nonloc::SymbolVal(Pos.getOffset()), *IntDist,
  1405. SymMgr.getType(Pos.getOffset()))
  1406. .getAsSymbol());
  1407. } else {
  1408. // For other symbols create a new symbol to keep expressions simple
  1409. const auto &LCtx = C.getLocationContext();
  1410. const auto NewPosSym = SymMgr.conjureSymbol(nullptr, LCtx,
  1411. SymMgr.getType(Pos.getOffset()),
  1412. C.blockCount());
  1413. State = assumeNoOverflow(State, NewPosSym, 4);
  1414. return Pos.setTo(NewPosSym);
  1415. }
  1416. }
  1417. void IteratorChecker::reportOutOfRangeBug(const StringRef &Message,
  1418. const SVal &Val, CheckerContext &C,
  1419. ExplodedNode *ErrNode) const {
  1420. auto R = std::make_unique<BugReport>(*OutOfRangeBugType, Message, ErrNode);
  1421. R->markInteresting(Val);
  1422. C.emitReport(std::move(R));
  1423. }
  1424. void IteratorChecker::reportMismatchedBug(const StringRef &Message,
  1425. const SVal &Val1, const SVal &Val2,
  1426. CheckerContext &C,
  1427. ExplodedNode *ErrNode) const {
  1428. auto R = std::make_unique<BugReport>(*MismatchedBugType, Message, ErrNode);
  1429. R->markInteresting(Val1);
  1430. R->markInteresting(Val2);
  1431. C.emitReport(std::move(R));
  1432. }
  1433. void IteratorChecker::reportMismatchedBug(const StringRef &Message,
  1434. const SVal &Val, const MemRegion *Reg,
  1435. CheckerContext &C,
  1436. ExplodedNode *ErrNode) const {
  1437. auto R = std::make_unique<BugReport>(*MismatchedBugType, Message, ErrNode);
  1438. R->markInteresting(Val);
  1439. R->markInteresting(Reg);
  1440. C.emitReport(std::move(R));
  1441. }
  1442. void IteratorChecker::reportInvalidatedBug(const StringRef &Message,
  1443. const SVal &Val, CheckerContext &C,
  1444. ExplodedNode *ErrNode) const {
  1445. auto R = std::make_unique<BugReport>(*InvalidatedBugType, Message, ErrNode);
  1446. R->markInteresting(Val);
  1447. C.emitReport(std::move(R));
  1448. }
  1449. namespace {
  1450. bool isLess(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2);
  1451. bool isGreater(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2);
  1452. bool isEqual(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2);
  1453. bool compare(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2,
  1454. BinaryOperator::Opcode Opc);
  1455. bool compare(ProgramStateRef State, NonLoc NL1, NonLoc NL2,
  1456. BinaryOperator::Opcode Opc);
  1457. const CXXRecordDecl *getCXXRecordDecl(ProgramStateRef State,
  1458. const MemRegion *Reg);
  1459. SymbolRef rebaseSymbol(ProgramStateRef State, SValBuilder &SVB, SymbolRef Expr,
  1460. SymbolRef OldSym, SymbolRef NewSym);
  1461. bool isIteratorType(const QualType &Type) {
  1462. if (Type->isPointerType())
  1463. return true;
  1464. const auto *CRD = Type->getUnqualifiedDesugaredType()->getAsCXXRecordDecl();
  1465. return isIterator(CRD);
  1466. }
  1467. bool isIterator(const CXXRecordDecl *CRD) {
  1468. if (!CRD)
  1469. return false;
  1470. const auto Name = CRD->getName();
  1471. if (!(Name.endswith_lower("iterator") || Name.endswith_lower("iter") ||
  1472. Name.endswith_lower("it")))
  1473. return false;
  1474. bool HasCopyCtor = false, HasCopyAssign = true, HasDtor = false,
  1475. HasPreIncrOp = false, HasPostIncrOp = false, HasDerefOp = false;
  1476. for (const auto *Method : CRD->methods()) {
  1477. if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Method)) {
  1478. if (Ctor->isCopyConstructor()) {
  1479. HasCopyCtor = !Ctor->isDeleted() && Ctor->getAccess() == AS_public;
  1480. }
  1481. continue;
  1482. }
  1483. if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(Method)) {
  1484. HasDtor = !Dtor->isDeleted() && Dtor->getAccess() == AS_public;
  1485. continue;
  1486. }
  1487. if (Method->isCopyAssignmentOperator()) {
  1488. HasCopyAssign = !Method->isDeleted() && Method->getAccess() == AS_public;
  1489. continue;
  1490. }
  1491. if (!Method->isOverloadedOperator())
  1492. continue;
  1493. const auto OPK = Method->getOverloadedOperator();
  1494. if (OPK == OO_PlusPlus) {
  1495. HasPreIncrOp = HasPreIncrOp || (Method->getNumParams() == 0);
  1496. HasPostIncrOp = HasPostIncrOp || (Method->getNumParams() == 1);
  1497. continue;
  1498. }
  1499. if (OPK == OO_Star) {
  1500. HasDerefOp = (Method->getNumParams() == 0);
  1501. continue;
  1502. }
  1503. }
  1504. return HasCopyCtor && HasCopyAssign && HasDtor && HasPreIncrOp &&
  1505. HasPostIncrOp && HasDerefOp;
  1506. }
  1507. bool isComparisonOperator(OverloadedOperatorKind OK) {
  1508. return OK == OO_EqualEqual || OK == OO_ExclaimEqual || OK == OO_Less ||
  1509. OK == OO_LessEqual || OK == OO_Greater || OK == OO_GreaterEqual;
  1510. }
  1511. bool isBeginCall(const FunctionDecl *Func) {
  1512. const auto *IdInfo = Func->getIdentifier();
  1513. if (!IdInfo)
  1514. return false;
  1515. return IdInfo->getName().endswith_lower("begin");
  1516. }
  1517. bool isEndCall(const FunctionDecl *Func) {
  1518. const auto *IdInfo = Func->getIdentifier();
  1519. if (!IdInfo)
  1520. return false;
  1521. return IdInfo->getName().endswith_lower("end");
  1522. }
  1523. bool isAssignCall(const FunctionDecl *Func) {
  1524. const auto *IdInfo = Func->getIdentifier();
  1525. if (!IdInfo)
  1526. return false;
  1527. if (Func->getNumParams() > 2)
  1528. return false;
  1529. return IdInfo->getName() == "assign";
  1530. }
  1531. bool isClearCall(const FunctionDecl *Func) {
  1532. const auto *IdInfo = Func->getIdentifier();
  1533. if (!IdInfo)
  1534. return false;
  1535. if (Func->getNumParams() > 0)
  1536. return false;
  1537. return IdInfo->getName() == "clear";
  1538. }
  1539. bool isPushBackCall(const FunctionDecl *Func) {
  1540. const auto *IdInfo = Func->getIdentifier();
  1541. if (!IdInfo)
  1542. return false;
  1543. if (Func->getNumParams() != 1)
  1544. return false;
  1545. return IdInfo->getName() == "push_back";
  1546. }
  1547. bool isEmplaceBackCall(const FunctionDecl *Func) {
  1548. const auto *IdInfo = Func->getIdentifier();
  1549. if (!IdInfo)
  1550. return false;
  1551. if (Func->getNumParams() < 1)
  1552. return false;
  1553. return IdInfo->getName() == "emplace_back";
  1554. }
  1555. bool isPopBackCall(const FunctionDecl *Func) {
  1556. const auto *IdInfo = Func->getIdentifier();
  1557. if (!IdInfo)
  1558. return false;
  1559. if (Func->getNumParams() > 0)
  1560. return false;
  1561. return IdInfo->getName() == "pop_back";
  1562. }
  1563. bool isPushFrontCall(const FunctionDecl *Func) {
  1564. const auto *IdInfo = Func->getIdentifier();
  1565. if (!IdInfo)
  1566. return false;
  1567. if (Func->getNumParams() != 1)
  1568. return false;
  1569. return IdInfo->getName() == "push_front";
  1570. }
  1571. bool isEmplaceFrontCall(const FunctionDecl *Func) {
  1572. const auto *IdInfo = Func->getIdentifier();
  1573. if (!IdInfo)
  1574. return false;
  1575. if (Func->getNumParams() < 1)
  1576. return false;
  1577. return IdInfo->getName() == "emplace_front";
  1578. }
  1579. bool isPopFrontCall(const FunctionDecl *Func) {
  1580. const auto *IdInfo = Func->getIdentifier();
  1581. if (!IdInfo)
  1582. return false;
  1583. if (Func->getNumParams() > 0)
  1584. return false;
  1585. return IdInfo->getName() == "pop_front";
  1586. }
  1587. bool isInsertCall(const FunctionDecl *Func) {
  1588. const auto *IdInfo = Func->getIdentifier();
  1589. if (!IdInfo)
  1590. return false;
  1591. if (Func->getNumParams() < 2 || Func->getNumParams() > 3)
  1592. return false;
  1593. if (!isIteratorType(Func->getParamDecl(0)->getType()))
  1594. return false;
  1595. return IdInfo->getName() == "insert";
  1596. }
  1597. bool isEmplaceCall(const FunctionDecl *Func) {
  1598. const auto *IdInfo = Func->getIdentifier();
  1599. if (!IdInfo)
  1600. return false;
  1601. if (Func->getNumParams() < 2)
  1602. return false;
  1603. if (!isIteratorType(Func->getParamDecl(0)->getType()))
  1604. return false;
  1605. return IdInfo->getName() == "emplace";
  1606. }
  1607. bool isEraseCall(const FunctionDecl *Func) {
  1608. const auto *IdInfo = Func->getIdentifier();
  1609. if (!IdInfo)
  1610. return false;
  1611. if (Func->getNumParams() < 1 || Func->getNumParams() > 2)
  1612. return false;
  1613. if (!isIteratorType(Func->getParamDecl(0)->getType()))
  1614. return false;
  1615. if (Func->getNumParams() == 2 &&
  1616. !isIteratorType(Func->getParamDecl(1)->getType()))
  1617. return false;
  1618. return IdInfo->getName() == "erase";
  1619. }
  1620. bool isEraseAfterCall(const FunctionDecl *Func) {
  1621. const auto *IdInfo = Func->getIdentifier();
  1622. if (!IdInfo)
  1623. return false;
  1624. if (Func->getNumParams() < 1 || Func->getNumParams() > 2)
  1625. return false;
  1626. if (!isIteratorType(Func->getParamDecl(0)->getType()))
  1627. return false;
  1628. if (Func->getNumParams() == 2 &&
  1629. !isIteratorType(Func->getParamDecl(1)->getType()))
  1630. return false;
  1631. return IdInfo->getName() == "erase_after";
  1632. }
  1633. bool isAssignmentOperator(OverloadedOperatorKind OK) { return OK == OO_Equal; }
  1634. bool isSimpleComparisonOperator(OverloadedOperatorKind OK) {
  1635. return OK == OO_EqualEqual || OK == OO_ExclaimEqual;
  1636. }
  1637. bool isAccessOperator(OverloadedOperatorKind OK) {
  1638. return isDereferenceOperator(OK) || isIncrementOperator(OK) ||
  1639. isDecrementOperator(OK) || isRandomIncrOrDecrOperator(OK);
  1640. }
  1641. bool isDereferenceOperator(OverloadedOperatorKind OK) {
  1642. return OK == OO_Star || OK == OO_Arrow || OK == OO_ArrowStar ||
  1643. OK == OO_Subscript;
  1644. }
  1645. bool isIncrementOperator(OverloadedOperatorKind OK) {
  1646. return OK == OO_PlusPlus;
  1647. }
  1648. bool isDecrementOperator(OverloadedOperatorKind OK) {
  1649. return OK == OO_MinusMinus;
  1650. }
  1651. bool isRandomIncrOrDecrOperator(OverloadedOperatorKind OK) {
  1652. return OK == OO_Plus || OK == OO_PlusEqual || OK == OO_Minus ||
  1653. OK == OO_MinusEqual;
  1654. }
  1655. bool hasSubscriptOperator(ProgramStateRef State, const MemRegion *Reg) {
  1656. const auto *CRD = getCXXRecordDecl(State, Reg);
  1657. if (!CRD)
  1658. return false;
  1659. for (const auto *Method : CRD->methods()) {
  1660. if (!Method->isOverloadedOperator())
  1661. continue;
  1662. const auto OPK = Method->getOverloadedOperator();
  1663. if (OPK == OO_Subscript) {
  1664. return true;
  1665. }
  1666. }
  1667. return false;
  1668. }
  1669. bool frontModifiable(ProgramStateRef State, const MemRegion *Reg) {
  1670. const auto *CRD = getCXXRecordDecl(State, Reg);
  1671. if (!CRD)
  1672. return false;
  1673. for (const auto *Method : CRD->methods()) {
  1674. if (!Method->getDeclName().isIdentifier())
  1675. continue;
  1676. if (Method->getName() == "push_front" || Method->getName() == "pop_front") {
  1677. return true;
  1678. }
  1679. }
  1680. return false;
  1681. }
  1682. bool backModifiable(ProgramStateRef State, const MemRegion *Reg) {
  1683. const auto *CRD = getCXXRecordDecl(State, Reg);
  1684. if (!CRD)
  1685. return false;
  1686. for (const auto *Method : CRD->methods()) {
  1687. if (!Method->getDeclName().isIdentifier())
  1688. continue;
  1689. if (Method->getName() == "push_back" || Method->getName() == "pop_back") {
  1690. return true;
  1691. }
  1692. }
  1693. return false;
  1694. }
  1695. const CXXRecordDecl *getCXXRecordDecl(ProgramStateRef State,
  1696. const MemRegion *Reg) {
  1697. auto TI = getDynamicTypeInfo(State, Reg);
  1698. if (!TI.isValid())
  1699. return nullptr;
  1700. auto Type = TI.getType();
  1701. if (const auto *RefT = Type->getAs<ReferenceType>()) {
  1702. Type = RefT->getPointeeType();
  1703. }
  1704. return Type->getUnqualifiedDesugaredType()->getAsCXXRecordDecl();
  1705. }
  1706. SymbolRef getContainerBegin(ProgramStateRef State, const MemRegion *Cont) {
  1707. const auto *CDataPtr = getContainerData(State, Cont);
  1708. if (!CDataPtr)
  1709. return nullptr;
  1710. return CDataPtr->getBegin();
  1711. }
  1712. SymbolRef getContainerEnd(ProgramStateRef State, const MemRegion *Cont) {
  1713. const auto *CDataPtr = getContainerData(State, Cont);
  1714. if (!CDataPtr)
  1715. return nullptr;
  1716. return CDataPtr->getEnd();
  1717. }
  1718. ProgramStateRef createContainerBegin(ProgramStateRef State,
  1719. const MemRegion *Cont, const Expr *E,
  1720. QualType T, const LocationContext *LCtx,
  1721. unsigned BlockCount) {
  1722. // Only create if it does not exist
  1723. const auto *CDataPtr = getContainerData(State, Cont);
  1724. if (CDataPtr && CDataPtr->getBegin())
  1725. return State;
  1726. auto &SymMgr = State->getSymbolManager();
  1727. const SymbolConjured *Sym = SymMgr.conjureSymbol(E, LCtx, T, BlockCount,
  1728. "begin");
  1729. State = assumeNoOverflow(State, Sym, 4);
  1730. if (CDataPtr) {
  1731. const auto CData = CDataPtr->newBegin(Sym);
  1732. return setContainerData(State, Cont, CData);
  1733. }
  1734. const auto CData = ContainerData::fromBegin(Sym);
  1735. return setContainerData(State, Cont, CData);
  1736. }
  1737. ProgramStateRef createContainerEnd(ProgramStateRef State, const MemRegion *Cont,
  1738. const Expr *E, QualType T,
  1739. const LocationContext *LCtx,
  1740. unsigned BlockCount) {
  1741. // Only create if it does not exist
  1742. const auto *CDataPtr = getContainerData(State, Cont);
  1743. if (CDataPtr && CDataPtr->getEnd())
  1744. return State;
  1745. auto &SymMgr = State->getSymbolManager();
  1746. const SymbolConjured *Sym = SymMgr.conjureSymbol(E, LCtx, T, BlockCount,
  1747. "end");
  1748. State = assumeNoOverflow(State, Sym, 4);
  1749. if (CDataPtr) {
  1750. const auto CData = CDataPtr->newEnd(Sym);
  1751. return setContainerData(State, Cont, CData);
  1752. }
  1753. const auto CData = ContainerData::fromEnd(Sym);
  1754. return setContainerData(State, Cont, CData);
  1755. }
  1756. const ContainerData *getContainerData(ProgramStateRef State,
  1757. const MemRegion *Cont) {
  1758. return State->get<ContainerMap>(Cont);
  1759. }
  1760. ProgramStateRef setContainerData(ProgramStateRef State, const MemRegion *Cont,
  1761. const ContainerData &CData) {
  1762. return State->set<ContainerMap>(Cont, CData);
  1763. }
  1764. const IteratorPosition *getIteratorPosition(ProgramStateRef State,
  1765. const SVal &Val) {
  1766. if (auto Reg = Val.getAsRegion()) {
  1767. Reg = Reg->getMostDerivedObjectRegion();
  1768. return State->get<IteratorRegionMap>(Reg);
  1769. } else if (const auto Sym = Val.getAsSymbol()) {
  1770. return State->get<IteratorSymbolMap>(Sym);
  1771. } else if (const auto LCVal = Val.getAs<nonloc::LazyCompoundVal>()) {
  1772. return State->get<IteratorRegionMap>(LCVal->getRegion());
  1773. }
  1774. return nullptr;
  1775. }
  1776. ProgramStateRef setIteratorPosition(ProgramStateRef State, const SVal &Val,
  1777. const IteratorPosition &Pos) {
  1778. if (auto Reg = Val.getAsRegion()) {
  1779. Reg = Reg->getMostDerivedObjectRegion();
  1780. return State->set<IteratorRegionMap>(Reg, Pos);
  1781. } else if (const auto Sym = Val.getAsSymbol()) {
  1782. return State->set<IteratorSymbolMap>(Sym, Pos);
  1783. } else if (const auto LCVal = Val.getAs<nonloc::LazyCompoundVal>()) {
  1784. return State->set<IteratorRegionMap>(LCVal->getRegion(), Pos);
  1785. }
  1786. return nullptr;
  1787. }
  1788. ProgramStateRef removeIteratorPosition(ProgramStateRef State, const SVal &Val) {
  1789. if (auto Reg = Val.getAsRegion()) {
  1790. Reg = Reg->getMostDerivedObjectRegion();
  1791. return State->remove<IteratorRegionMap>(Reg);
  1792. } else if (const auto Sym = Val.getAsSymbol()) {
  1793. return State->remove<IteratorSymbolMap>(Sym);
  1794. } else if (const auto LCVal = Val.getAs<nonloc::LazyCompoundVal>()) {
  1795. return State->remove<IteratorRegionMap>(LCVal->getRegion());
  1796. }
  1797. return nullptr;
  1798. }
  1799. ProgramStateRef relateSymbols(ProgramStateRef State, SymbolRef Sym1,
  1800. SymbolRef Sym2, bool Equal) {
  1801. auto &SVB = State->getStateManager().getSValBuilder();
  1802. // FIXME: This code should be reworked as follows:
  1803. // 1. Subtract the operands using evalBinOp().
  1804. // 2. Assume that the result doesn't overflow.
  1805. // 3. Compare the result to 0.
  1806. // 4. Assume the result of the comparison.
  1807. const auto comparison =
  1808. SVB.evalBinOp(State, BO_EQ, nonloc::SymbolVal(Sym1),
  1809. nonloc::SymbolVal(Sym2), SVB.getConditionType());
  1810. assert(comparison.getAs<DefinedSVal>() &&
  1811. "Symbol comparison must be a `DefinedSVal`");
  1812. auto NewState = State->assume(comparison.castAs<DefinedSVal>(), Equal);
  1813. if (!NewState)
  1814. return nullptr;
  1815. if (const auto CompSym = comparison.getAsSymbol()) {
  1816. assert(isa<SymIntExpr>(CompSym) &&
  1817. "Symbol comparison must be a `SymIntExpr`");
  1818. assert(BinaryOperator::isComparisonOp(
  1819. cast<SymIntExpr>(CompSym)->getOpcode()) &&
  1820. "Symbol comparison must be a comparison");
  1821. return assumeNoOverflow(NewState, cast<SymIntExpr>(CompSym)->getLHS(), 2);
  1822. }
  1823. return NewState;
  1824. }
  1825. bool hasLiveIterators(ProgramStateRef State, const MemRegion *Cont) {
  1826. auto RegionMap = State->get<IteratorRegionMap>();
  1827. for (const auto Reg : RegionMap) {
  1828. if (Reg.second.getContainer() == Cont)
  1829. return true;
  1830. }
  1831. auto SymbolMap = State->get<IteratorSymbolMap>();
  1832. for (const auto Sym : SymbolMap) {
  1833. if (Sym.second.getContainer() == Cont)
  1834. return true;
  1835. }
  1836. return false;
  1837. }
  1838. bool isBoundThroughLazyCompoundVal(const Environment &Env,
  1839. const MemRegion *Reg) {
  1840. for (const auto Binding: Env) {
  1841. if (const auto LCVal = Binding.second.getAs<nonloc::LazyCompoundVal>()) {
  1842. if (LCVal->getRegion() == Reg)
  1843. return true;
  1844. }
  1845. }
  1846. return false;
  1847. }
  1848. // This function tells the analyzer's engine that symbols produced by our
  1849. // checker, most notably iterator positions, are relatively small.
  1850. // A distance between items in the container should not be very large.
  1851. // By assuming that it is within around 1/8 of the address space,
  1852. // we can help the analyzer perform operations on these symbols
  1853. // without being afraid of integer overflows.
  1854. // FIXME: Should we provide it as an API, so that all checkers could use it?
  1855. ProgramStateRef assumeNoOverflow(ProgramStateRef State, SymbolRef Sym,
  1856. long Scale) {
  1857. SValBuilder &SVB = State->getStateManager().getSValBuilder();
  1858. BasicValueFactory &BV = SVB.getBasicValueFactory();
  1859. QualType T = Sym->getType();
  1860. assert(T->isSignedIntegerOrEnumerationType());
  1861. APSIntType AT = BV.getAPSIntType(T);
  1862. ProgramStateRef NewState = State;
  1863. llvm::APSInt Max = AT.getMaxValue() / AT.getValue(Scale);
  1864. SVal IsCappedFromAbove =
  1865. SVB.evalBinOpNN(State, BO_LE, nonloc::SymbolVal(Sym),
  1866. nonloc::ConcreteInt(Max), SVB.getConditionType());
  1867. if (auto DV = IsCappedFromAbove.getAs<DefinedSVal>()) {
  1868. NewState = NewState->assume(*DV, true);
  1869. if (!NewState)
  1870. return State;
  1871. }
  1872. llvm::APSInt Min = -Max;
  1873. SVal IsCappedFromBelow =
  1874. SVB.evalBinOpNN(State, BO_GE, nonloc::SymbolVal(Sym),
  1875. nonloc::ConcreteInt(Min), SVB.getConditionType());
  1876. if (auto DV = IsCappedFromBelow.getAs<DefinedSVal>()) {
  1877. NewState = NewState->assume(*DV, true);
  1878. if (!NewState)
  1879. return State;
  1880. }
  1881. return NewState;
  1882. }
  1883. template <typename Condition, typename Process>
  1884. ProgramStateRef processIteratorPositions(ProgramStateRef State, Condition Cond,
  1885. Process Proc) {
  1886. auto &RegionMapFactory = State->get_context<IteratorRegionMap>();
  1887. auto RegionMap = State->get<IteratorRegionMap>();
  1888. bool Changed = false;
  1889. for (const auto Reg : RegionMap) {
  1890. if (Cond(Reg.second)) {
  1891. RegionMap = RegionMapFactory.add(RegionMap, Reg.first, Proc(Reg.second));
  1892. Changed = true;
  1893. }
  1894. }
  1895. if (Changed)
  1896. State = State->set<IteratorRegionMap>(RegionMap);
  1897. auto &SymbolMapFactory = State->get_context<IteratorSymbolMap>();
  1898. auto SymbolMap = State->get<IteratorSymbolMap>();
  1899. Changed = false;
  1900. for (const auto Sym : SymbolMap) {
  1901. if (Cond(Sym.second)) {
  1902. SymbolMap = SymbolMapFactory.add(SymbolMap, Sym.first, Proc(Sym.second));
  1903. Changed = true;
  1904. }
  1905. }
  1906. if (Changed)
  1907. State = State->set<IteratorSymbolMap>(SymbolMap);
  1908. return State;
  1909. }
  1910. ProgramStateRef invalidateAllIteratorPositions(ProgramStateRef State,
  1911. const MemRegion *Cont) {
  1912. auto MatchCont = [&](const IteratorPosition &Pos) {
  1913. return Pos.getContainer() == Cont;
  1914. };
  1915. auto Invalidate = [&](const IteratorPosition &Pos) {
  1916. return Pos.invalidate();
  1917. };
  1918. return processIteratorPositions(State, MatchCont, Invalidate);
  1919. }
  1920. ProgramStateRef
  1921. invalidateAllIteratorPositionsExcept(ProgramStateRef State,
  1922. const MemRegion *Cont, SymbolRef Offset,
  1923. BinaryOperator::Opcode Opc) {
  1924. auto MatchContAndCompare = [&](const IteratorPosition &Pos) {
  1925. return Pos.getContainer() == Cont &&
  1926. !compare(State, Pos.getOffset(), Offset, Opc);
  1927. };
  1928. auto Invalidate = [&](const IteratorPosition &Pos) {
  1929. return Pos.invalidate();
  1930. };
  1931. return processIteratorPositions(State, MatchContAndCompare, Invalidate);
  1932. }
  1933. ProgramStateRef invalidateIteratorPositions(ProgramStateRef State,
  1934. SymbolRef Offset,
  1935. BinaryOperator::Opcode Opc) {
  1936. auto Compare = [&](const IteratorPosition &Pos) {
  1937. return compare(State, Pos.getOffset(), Offset, Opc);
  1938. };
  1939. auto Invalidate = [&](const IteratorPosition &Pos) {
  1940. return Pos.invalidate();
  1941. };
  1942. return processIteratorPositions(State, Compare, Invalidate);
  1943. }
  1944. ProgramStateRef invalidateIteratorPositions(ProgramStateRef State,
  1945. SymbolRef Offset1,
  1946. BinaryOperator::Opcode Opc1,
  1947. SymbolRef Offset2,
  1948. BinaryOperator::Opcode Opc2) {
  1949. auto Compare = [&](const IteratorPosition &Pos) {
  1950. return compare(State, Pos.getOffset(), Offset1, Opc1) &&
  1951. compare(State, Pos.getOffset(), Offset2, Opc2);
  1952. };
  1953. auto Invalidate = [&](const IteratorPosition &Pos) {
  1954. return Pos.invalidate();
  1955. };
  1956. return processIteratorPositions(State, Compare, Invalidate);
  1957. }
  1958. ProgramStateRef reassignAllIteratorPositions(ProgramStateRef State,
  1959. const MemRegion *Cont,
  1960. const MemRegion *NewCont) {
  1961. auto MatchCont = [&](const IteratorPosition &Pos) {
  1962. return Pos.getContainer() == Cont;
  1963. };
  1964. auto ReAssign = [&](const IteratorPosition &Pos) {
  1965. return Pos.reAssign(NewCont);
  1966. };
  1967. return processIteratorPositions(State, MatchCont, ReAssign);
  1968. }
  1969. ProgramStateRef reassignAllIteratorPositionsUnless(ProgramStateRef State,
  1970. const MemRegion *Cont,
  1971. const MemRegion *NewCont,
  1972. SymbolRef Offset,
  1973. BinaryOperator::Opcode Opc) {
  1974. auto MatchContAndCompare = [&](const IteratorPosition &Pos) {
  1975. return Pos.getContainer() == Cont &&
  1976. !compare(State, Pos.getOffset(), Offset, Opc);
  1977. };
  1978. auto ReAssign = [&](const IteratorPosition &Pos) {
  1979. return Pos.reAssign(NewCont);
  1980. };
  1981. return processIteratorPositions(State, MatchContAndCompare, ReAssign);
  1982. }
  1983. // This function rebases symbolic expression `OldSym + Int` to `NewSym + Int`,
  1984. // `OldSym - Int` to `NewSym - Int` and `OldSym` to `NewSym` in any iterator
  1985. // position offsets where `CondSym` is true.
  1986. ProgramStateRef rebaseSymbolInIteratorPositionsIf(
  1987. ProgramStateRef State, SValBuilder &SVB, SymbolRef OldSym,
  1988. SymbolRef NewSym, SymbolRef CondSym, BinaryOperator::Opcode Opc) {
  1989. auto LessThanEnd = [&](const IteratorPosition &Pos) {
  1990. return compare(State, Pos.getOffset(), CondSym, Opc);
  1991. };
  1992. auto RebaseSymbol = [&](const IteratorPosition &Pos) {
  1993. return Pos.setTo(rebaseSymbol(State, SVB, Pos.getOffset(), OldSym,
  1994. NewSym));
  1995. };
  1996. return processIteratorPositions(State, LessThanEnd, RebaseSymbol);
  1997. }
  1998. // This function rebases symbolic expression `OldExpr + Int` to `NewExpr + Int`,
  1999. // `OldExpr - Int` to `NewExpr - Int` and `OldExpr` to `NewExpr` in expression
  2000. // `OrigExpr`.
  2001. SymbolRef rebaseSymbol(ProgramStateRef State, SValBuilder &SVB,
  2002. SymbolRef OrigExpr, SymbolRef OldExpr,
  2003. SymbolRef NewSym) {
  2004. auto &SymMgr = SVB.getSymbolManager();
  2005. auto Diff = SVB.evalBinOpNN(State, BO_Sub, nonloc::SymbolVal(OrigExpr),
  2006. nonloc::SymbolVal(OldExpr),
  2007. SymMgr.getType(OrigExpr));
  2008. const auto DiffInt = Diff.getAs<nonloc::ConcreteInt>();
  2009. if (!DiffInt)
  2010. return OrigExpr;
  2011. return SVB.evalBinOpNN(State, BO_Add, *DiffInt, nonloc::SymbolVal(NewSym),
  2012. SymMgr.getType(OrigExpr)).getAsSymbol();
  2013. }
  2014. bool isZero(ProgramStateRef State, const NonLoc &Val) {
  2015. auto &BVF = State->getBasicVals();
  2016. return compare(State, Val,
  2017. nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(0))),
  2018. BO_EQ);
  2019. }
  2020. bool isPastTheEnd(ProgramStateRef State, const IteratorPosition &Pos) {
  2021. const auto *Cont = Pos.getContainer();
  2022. const auto *CData = getContainerData(State, Cont);
  2023. if (!CData)
  2024. return false;
  2025. const auto End = CData->getEnd();
  2026. if (End) {
  2027. if (isEqual(State, Pos.getOffset(), End)) {
  2028. return true;
  2029. }
  2030. }
  2031. return false;
  2032. }
  2033. bool isAheadOfRange(ProgramStateRef State, const IteratorPosition &Pos) {
  2034. const auto *Cont = Pos.getContainer();
  2035. const auto *CData = getContainerData(State, Cont);
  2036. if (!CData)
  2037. return false;
  2038. const auto Beg = CData->getBegin();
  2039. if (Beg) {
  2040. if (isLess(State, Pos.getOffset(), Beg)) {
  2041. return true;
  2042. }
  2043. }
  2044. return false;
  2045. }
  2046. bool isBehindPastTheEnd(ProgramStateRef State, const IteratorPosition &Pos) {
  2047. const auto *Cont = Pos.getContainer();
  2048. const auto *CData = getContainerData(State, Cont);
  2049. if (!CData)
  2050. return false;
  2051. const auto End = CData->getEnd();
  2052. if (End) {
  2053. if (isGreater(State, Pos.getOffset(), End)) {
  2054. return true;
  2055. }
  2056. }
  2057. return false;
  2058. }
  2059. bool isLess(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2) {
  2060. return compare(State, Sym1, Sym2, BO_LT);
  2061. }
  2062. bool isGreater(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2) {
  2063. return compare(State, Sym1, Sym2, BO_GT);
  2064. }
  2065. bool isEqual(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2) {
  2066. return compare(State, Sym1, Sym2, BO_EQ);
  2067. }
  2068. bool compare(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2,
  2069. BinaryOperator::Opcode Opc) {
  2070. return compare(State, nonloc::SymbolVal(Sym1), nonloc::SymbolVal(Sym2), Opc);
  2071. }
  2072. bool compare(ProgramStateRef State, NonLoc NL1, NonLoc NL2,
  2073. BinaryOperator::Opcode Opc) {
  2074. auto &SVB = State->getStateManager().getSValBuilder();
  2075. const auto comparison =
  2076. SVB.evalBinOp(State, Opc, NL1, NL2, SVB.getConditionType());
  2077. assert(comparison.getAs<DefinedSVal>() &&
  2078. "Symbol comparison must be a `DefinedSVal`");
  2079. return !State->assume(comparison.castAs<DefinedSVal>(), false);
  2080. }
  2081. } // namespace
  2082. void ento::registerIteratorModeling(CheckerManager &mgr) {
  2083. mgr.registerChecker<IteratorChecker>();
  2084. }
  2085. bool ento::shouldRegisterIteratorModeling(const LangOptions &LO) {
  2086. return true;
  2087. }
  2088. #define REGISTER_CHECKER(name) \
  2089. void ento::register##name(CheckerManager &Mgr) { \
  2090. auto *checker = Mgr.getChecker<IteratorChecker>(); \
  2091. checker->ChecksEnabled[IteratorChecker::CK_##name] = true; \
  2092. checker->CheckNames[IteratorChecker::CK_##name] = \
  2093. Mgr.getCurrentCheckName(); \
  2094. } \
  2095. \
  2096. bool ento::shouldRegister##name(const LangOptions &LO) { \
  2097. return true; \
  2098. }
  2099. REGISTER_CHECKER(IteratorRangeChecker)
  2100. REGISTER_CHECKER(MismatchedIteratorChecker)
  2101. REGISTER_CHECKER(InvalidatedIteratorChecker)