1e5dd7070Spatrick //===-- InvalidatedIteratorChecker.cpp ----------------------------*- C++ -*--//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // Defines a checker for access of invalidated iterators.
10e5dd7070Spatrick //
11e5dd7070Spatrick //===----------------------------------------------------------------------===//
12e5dd7070Spatrick 
13e5dd7070Spatrick #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
14e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
15e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/Checker.h"
16e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
17e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
18e5dd7070Spatrick 
19e5dd7070Spatrick 
20e5dd7070Spatrick #include "Iterator.h"
21e5dd7070Spatrick 
22e5dd7070Spatrick using namespace clang;
23e5dd7070Spatrick using namespace ento;
24e5dd7070Spatrick using namespace iterator;
25e5dd7070Spatrick 
26e5dd7070Spatrick namespace {
27e5dd7070Spatrick 
28e5dd7070Spatrick class InvalidatedIteratorChecker
29*ec727ea7Spatrick   : public Checker<check::PreCall, check::PreStmt<UnaryOperator>,
30*ec727ea7Spatrick                    check::PreStmt<BinaryOperator>,
31*ec727ea7Spatrick                    check::PreStmt<ArraySubscriptExpr>,
32*ec727ea7Spatrick                    check::PreStmt<MemberExpr>> {
33e5dd7070Spatrick 
34e5dd7070Spatrick   std::unique_ptr<BugType> InvalidatedBugType;
35e5dd7070Spatrick 
36e5dd7070Spatrick   void verifyAccess(CheckerContext &C, const SVal &Val) const;
37e5dd7070Spatrick   void reportBug(const StringRef &Message, const SVal &Val,
38e5dd7070Spatrick                  CheckerContext &C, ExplodedNode *ErrNode) const;
39e5dd7070Spatrick public:
40e5dd7070Spatrick   InvalidatedIteratorChecker();
41e5dd7070Spatrick 
42e5dd7070Spatrick   void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
43*ec727ea7Spatrick   void checkPreStmt(const UnaryOperator *UO, CheckerContext &C) const;
44*ec727ea7Spatrick   void checkPreStmt(const BinaryOperator *BO, CheckerContext &C) const;
45*ec727ea7Spatrick   void checkPreStmt(const ArraySubscriptExpr *ASE, CheckerContext &C) const;
46*ec727ea7Spatrick   void checkPreStmt(const MemberExpr *ME, CheckerContext &C) const;
47e5dd7070Spatrick 
48e5dd7070Spatrick };
49e5dd7070Spatrick 
50e5dd7070Spatrick } //namespace
51e5dd7070Spatrick 
InvalidatedIteratorChecker()52e5dd7070Spatrick InvalidatedIteratorChecker::InvalidatedIteratorChecker() {
53e5dd7070Spatrick   InvalidatedBugType.reset(
54e5dd7070Spatrick       new BugType(this, "Iterator invalidated", "Misuse of STL APIs"));
55e5dd7070Spatrick }
56e5dd7070Spatrick 
checkPreCall(const CallEvent & Call,CheckerContext & C) const57e5dd7070Spatrick void InvalidatedIteratorChecker::checkPreCall(const CallEvent &Call,
58e5dd7070Spatrick                                               CheckerContext &C) const {
59e5dd7070Spatrick   // Check for access of invalidated position
60e5dd7070Spatrick   const auto *Func = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
61e5dd7070Spatrick   if (!Func)
62e5dd7070Spatrick     return;
63e5dd7070Spatrick 
64e5dd7070Spatrick   if (Func->isOverloadedOperator() &&
65e5dd7070Spatrick       isAccessOperator(Func->getOverloadedOperator())) {
66e5dd7070Spatrick     // Check for any kind of access of invalidated iterator positions
67e5dd7070Spatrick     if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
68e5dd7070Spatrick       verifyAccess(C, InstCall->getCXXThisVal());
69e5dd7070Spatrick     } else {
70e5dd7070Spatrick       verifyAccess(C, Call.getArgSVal(0));
71e5dd7070Spatrick     }
72e5dd7070Spatrick   }
73e5dd7070Spatrick }
74e5dd7070Spatrick 
checkPreStmt(const UnaryOperator * UO,CheckerContext & C) const75*ec727ea7Spatrick void InvalidatedIteratorChecker::checkPreStmt(const UnaryOperator *UO,
76*ec727ea7Spatrick                                               CheckerContext &C) const {
77*ec727ea7Spatrick   if (isa<CXXThisExpr>(UO->getSubExpr()))
78*ec727ea7Spatrick     return;
79*ec727ea7Spatrick 
80*ec727ea7Spatrick   ProgramStateRef State = C.getState();
81*ec727ea7Spatrick   UnaryOperatorKind OK = UO->getOpcode();
82*ec727ea7Spatrick   SVal SubVal = State->getSVal(UO->getSubExpr(), C.getLocationContext());
83*ec727ea7Spatrick 
84*ec727ea7Spatrick   if (isAccessOperator(OK)) {
85*ec727ea7Spatrick     verifyAccess(C, SubVal);
86*ec727ea7Spatrick   }
87*ec727ea7Spatrick }
88*ec727ea7Spatrick 
checkPreStmt(const BinaryOperator * BO,CheckerContext & C) const89*ec727ea7Spatrick void InvalidatedIteratorChecker::checkPreStmt(const BinaryOperator *BO,
90*ec727ea7Spatrick                                               CheckerContext &C) const {
91*ec727ea7Spatrick   ProgramStateRef State = C.getState();
92*ec727ea7Spatrick   BinaryOperatorKind OK = BO->getOpcode();
93*ec727ea7Spatrick   SVal LVal = State->getSVal(BO->getLHS(), C.getLocationContext());
94*ec727ea7Spatrick 
95*ec727ea7Spatrick   if (isAccessOperator(OK)) {
96*ec727ea7Spatrick     verifyAccess(C, LVal);
97*ec727ea7Spatrick   }
98*ec727ea7Spatrick }
99*ec727ea7Spatrick 
checkPreStmt(const ArraySubscriptExpr * ASE,CheckerContext & C) const100*ec727ea7Spatrick void InvalidatedIteratorChecker::checkPreStmt(const ArraySubscriptExpr *ASE,
101*ec727ea7Spatrick                                               CheckerContext &C) const {
102*ec727ea7Spatrick   ProgramStateRef State = C.getState();
103*ec727ea7Spatrick   SVal LVal = State->getSVal(ASE->getLHS(), C.getLocationContext());
104*ec727ea7Spatrick   verifyAccess(C, LVal);
105*ec727ea7Spatrick }
106*ec727ea7Spatrick 
checkPreStmt(const MemberExpr * ME,CheckerContext & C) const107*ec727ea7Spatrick void InvalidatedIteratorChecker::checkPreStmt(const MemberExpr *ME,
108*ec727ea7Spatrick                                               CheckerContext &C) const {
109*ec727ea7Spatrick   if (!ME->isArrow() || ME->isImplicitAccess())
110*ec727ea7Spatrick     return;
111*ec727ea7Spatrick 
112*ec727ea7Spatrick   ProgramStateRef State = C.getState();
113*ec727ea7Spatrick   SVal BaseVal = State->getSVal(ME->getBase(), C.getLocationContext());
114*ec727ea7Spatrick   verifyAccess(C, BaseVal);
115*ec727ea7Spatrick }
116*ec727ea7Spatrick 
verifyAccess(CheckerContext & C,const SVal & Val) const117e5dd7070Spatrick void InvalidatedIteratorChecker::verifyAccess(CheckerContext &C, const SVal &Val) const {
118e5dd7070Spatrick   auto State = C.getState();
119e5dd7070Spatrick   const auto *Pos = getIteratorPosition(State, Val);
120e5dd7070Spatrick   if (Pos && !Pos->isValid()) {
121e5dd7070Spatrick     auto *N = C.generateErrorNode(State);
122e5dd7070Spatrick     if (!N) {
123e5dd7070Spatrick       return;
124e5dd7070Spatrick     }
125e5dd7070Spatrick     reportBug("Invalidated iterator accessed.", Val, C, N);
126e5dd7070Spatrick   }
127e5dd7070Spatrick }
128e5dd7070Spatrick 
reportBug(const StringRef & Message,const SVal & Val,CheckerContext & C,ExplodedNode * ErrNode) const129e5dd7070Spatrick void InvalidatedIteratorChecker::reportBug(const StringRef &Message,
130e5dd7070Spatrick                                            const SVal &Val, CheckerContext &C,
131e5dd7070Spatrick                                            ExplodedNode *ErrNode) const {
132e5dd7070Spatrick   auto R = std::make_unique<PathSensitiveBugReport>(*InvalidatedBugType,
133e5dd7070Spatrick                                                     Message, ErrNode);
134e5dd7070Spatrick   R->markInteresting(Val);
135e5dd7070Spatrick   C.emitReport(std::move(R));
136e5dd7070Spatrick }
137e5dd7070Spatrick 
registerInvalidatedIteratorChecker(CheckerManager & mgr)138e5dd7070Spatrick void ento::registerInvalidatedIteratorChecker(CheckerManager &mgr) {
139e5dd7070Spatrick   mgr.registerChecker<InvalidatedIteratorChecker>();
140e5dd7070Spatrick }
141e5dd7070Spatrick 
shouldRegisterInvalidatedIteratorChecker(const CheckerManager & mgr)142*ec727ea7Spatrick bool ento::shouldRegisterInvalidatedIteratorChecker(const CheckerManager &mgr) {
143e5dd7070Spatrick   return true;
144e5dd7070Spatrick }
145