Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 1 | // BugReporterVisitors.cpp - Helpers for reporting bugs -----------*- C++ -*--// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a set of BugReporter "visitors" which can be used to |
| 11 | // enhance the diagnostics reported for a bug. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/AST/Expr.h" |
| 16 | #include "clang/AST/ExprObjC.h" |
Argyrios Kyrtzidis | 98cabba | 2010-12-22 18:51:49 +0000 | [diff] [blame] | 17 | #include "clang/GR/BugReporter/BugReporter.h" |
| 18 | #include "clang/GR/BugReporter/PathDiagnostic.h" |
| 19 | #include "clang/GR/PathSensitive/ExplodedGraph.h" |
| 20 | #include "clang/GR/PathSensitive/GRState.h" |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame^] | 23 | using namespace GR; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // Utility functions. |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame^] | 29 | const Stmt *bugreporter::GetDerefExpr(const ExplodedNode *N) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 30 | // Pattern match for a few useful cases (do something smarter later): |
| 31 | // a[0], p->f, *p |
| 32 | const Stmt *S = N->getLocationAs<PostStmt>()->getStmt(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 34 | if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 35 | if (U->getOpcode() == UO_Deref) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 36 | return U->getSubExpr()->IgnoreParenCasts(); |
| 37 | } |
| 38 | else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) { |
| 39 | return ME->getBase()->IgnoreParenCasts(); |
| 40 | } |
| 41 | else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) { |
| 42 | // Retrieve the base for arrays since BasicStoreManager doesn't know how |
| 43 | // to reason about them. |
| 44 | return AE->getBase(); |
| 45 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | |
| 47 | return NULL; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame^] | 50 | const Stmt *bugreporter::GetDenomExpr(const ExplodedNode *N) { |
Zhongxing Xu | 6403b57 | 2009-09-02 13:26:26 +0000 | [diff] [blame] | 51 | const Stmt *S = N->getLocationAs<PreStmt>()->getStmt(); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 52 | if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S)) |
| 53 | return BE->getRHS(); |
| 54 | return NULL; |
| 55 | } |
| 56 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame^] | 57 | const Stmt *bugreporter::GetCalleeExpr(const ExplodedNode *N) { |
Zhongxing Xu | d99f361 | 2009-09-02 08:10:35 +0000 | [diff] [blame] | 58 | // Callee is checked as a PreVisit to the CallExpr. |
| 59 | const Stmt *S = N->getLocationAs<PreStmt>()->getStmt(); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 60 | if (const CallExpr *CE = dyn_cast<CallExpr>(S)) |
| 61 | return CE->getCallee(); |
| 62 | return NULL; |
| 63 | } |
| 64 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame^] | 65 | const Stmt *bugreporter::GetRetValExpr(const ExplodedNode *N) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 66 | const Stmt *S = N->getLocationAs<PostStmt>()->getStmt(); |
| 67 | if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S)) |
| 68 | return RS->getRetValue(); |
| 69 | return NULL; |
| 70 | } |
| 71 | |
| 72 | //===----------------------------------------------------------------------===// |
| 73 | // Definitions for bug reporter visitors. |
| 74 | //===----------------------------------------------------------------------===// |
| 75 | |
| 76 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 77 | class FindLastStoreBRVisitor : public BugReporterVisitor { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 78 | const MemRegion *R; |
| 79 | SVal V; |
| 80 | bool satisfied; |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 81 | const ExplodedNode *StoreSite; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 82 | public: |
| 83 | FindLastStoreBRVisitor(SVal v, const MemRegion *r) |
| 84 | : R(r), V(v), satisfied(false), StoreSite(0) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | |
Ted Kremenek | 1b43102 | 2010-03-20 18:01:57 +0000 | [diff] [blame] | 86 | virtual void Profile(llvm::FoldingSetNodeID &ID) const { |
| 87 | static int tag = 0; |
| 88 | ID.AddPointer(&tag); |
| 89 | ID.AddPointer(R); |
| 90 | ID.Add(V); |
| 91 | } |
| 92 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 93 | PathDiagnosticPiece* VisitNode(const ExplodedNode *N, |
| 94 | const ExplodedNode *PrevN, |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 95 | BugReporterContext& BRC) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 97 | if (satisfied) |
| 98 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | |
| 100 | if (!StoreSite) { |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 101 | const ExplodedNode *Node = N, *Last = NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 103 | for ( ; Node ; Last = Node, Node = Node->getFirstPred()) { |
Daniel Dunbar | 0f2c907 | 2010-03-20 04:28:39 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 105 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
| 106 | if (const PostStmt *P = Node->getLocationAs<PostStmt>()) |
| 107 | if (const DeclStmt *DS = P->getStmtAs<DeclStmt>()) |
| 108 | if (DS->getSingleDecl() == VR->getDecl()) { |
| 109 | Last = Node; |
| 110 | break; |
| 111 | } |
| 112 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 114 | if (Node->getState()->getSVal(R) != V) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 115 | break; |
| 116 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 118 | if (!Node || !Last) { |
| 119 | satisfied = true; |
| 120 | return NULL; |
| 121 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 123 | StoreSite = Last; |
| 124 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 126 | if (StoreSite != N) |
| 127 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 129 | satisfied = true; |
Ted Kremenek | 1b43102 | 2010-03-20 18:01:57 +0000 | [diff] [blame] | 130 | llvm::SmallString<256> sbuf; |
| 131 | llvm::raw_svector_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 133 | if (const PostStmt *PS = N->getLocationAs<PostStmt>()) { |
| 134 | if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 136 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 137 | os << "Variable '" << VR->getDecl() << "' "; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 138 | } |
| 139 | else |
| 140 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 142 | if (isa<loc::ConcreteInt>(V)) { |
| 143 | bool b = false; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 144 | if (R->isBoundable()) { |
| 145 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 146 | if (TR->getValueType()->isObjCObjectPointerType()) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 147 | os << "initialized to nil"; |
| 148 | b = true; |
| 149 | } |
| 150 | } |
| 151 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 153 | if (!b) |
| 154 | os << "initialized to a null pointer value"; |
| 155 | } |
| 156 | else if (isa<nonloc::ConcreteInt>(V)) { |
| 157 | os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue(); |
| 158 | } |
| 159 | else if (V.isUndef()) { |
| 160 | if (isa<VarRegion>(R)) { |
| 161 | const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl()); |
| 162 | if (VD->getInit()) |
| 163 | os << "initialized to a garbage value"; |
| 164 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | os << "declared without an initial value"; |
| 166 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | |
| 171 | if (os.str().empty()) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 172 | if (isa<loc::ConcreteInt>(V)) { |
| 173 | bool b = false; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 174 | if (R->isBoundable()) { |
| 175 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 176 | if (TR->getValueType()->isObjCObjectPointerType()) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 177 | os << "nil object reference stored to "; |
| 178 | b = true; |
| 179 | } |
| 180 | } |
| 181 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 183 | if (!b) |
| 184 | os << "Null pointer value stored to "; |
| 185 | } |
| 186 | else if (V.isUndef()) { |
| 187 | os << "Uninitialized value stored to "; |
| 188 | } |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 189 | else if (isa<nonloc::ConcreteInt>(V)) { |
| 190 | os << "The value " << cast<nonloc::ConcreteInt>(V).getValue() |
| 191 | << " is assigned to "; |
| 192 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 193 | else |
| 194 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 196 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 197 | os << '\'' << VR->getDecl() << '\''; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 198 | } |
| 199 | else |
| 200 | return NULL; |
| 201 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 203 | // FIXME: Refactor this into BugReporterContext. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | const Stmt *S = 0; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 205 | ProgramPoint P = N->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 207 | if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
Zhongxing Xu | 03509ae | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 208 | const CFGBlock *BSrc = BE->getSrc(); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 209 | S = BSrc->getTerminatorCondition(); |
| 210 | } |
| 211 | else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) { |
| 212 | S = PS->getStmt(); |
| 213 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 215 | if (!S) |
| 216 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 218 | // Construct a new PathDiagnosticPiece. |
| 219 | PathDiagnosticLocation L(S, BRC.getSourceManager()); |
| 220 | return new PathDiagnosticEventPiece(L, os.str()); |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | |
| 225 | static void registerFindLastStore(BugReporterContext& BRC, const MemRegion *R, |
| 226 | SVal V) { |
| 227 | BRC.addVisitor(new FindLastStoreBRVisitor(V, R)); |
| 228 | } |
| 229 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 230 | class TrackConstraintBRVisitor : public BugReporterVisitor { |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 231 | DefinedSVal Constraint; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 232 | const bool Assumption; |
| 233 | bool isSatisfied; |
| 234 | public: |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 235 | TrackConstraintBRVisitor(DefinedSVal constraint, bool assumption) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 236 | : Constraint(constraint), Assumption(assumption), isSatisfied(false) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | |
Ted Kremenek | 1b43102 | 2010-03-20 18:01:57 +0000 | [diff] [blame] | 238 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 239 | static int tag = 0; |
| 240 | ID.AddPointer(&tag); |
| 241 | ID.AddBoolean(Assumption); |
| 242 | ID.Add(Constraint); |
| 243 | } |
| 244 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 245 | PathDiagnosticPiece* VisitNode(const ExplodedNode *N, |
| 246 | const ExplodedNode *PrevN, |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 247 | BugReporterContext& BRC) { |
| 248 | if (isSatisfied) |
| 249 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 250 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 251 | // Check if in the previous state it was feasible for this constraint |
| 252 | // to *not* be true. |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 253 | if (PrevN->getState()->assume(Constraint, !Assumption)) { |
Daniel Dunbar | 0f2c907 | 2010-03-20 04:28:39 +0000 | [diff] [blame] | 254 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 255 | isSatisfied = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 257 | // As a sanity check, make sure that the negation of the constraint |
| 258 | // was infeasible in the current state. If it is feasible, we somehow |
| 259 | // missed the transition point. |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 260 | if (N->getState()->assume(Constraint, !Assumption)) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 261 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 263 | // We found the transition point for the constraint. We now need to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | // pretty-print the constraint. (work-in-progress) |
Daniel Dunbar | 0f2c907 | 2010-03-20 04:28:39 +0000 | [diff] [blame] | 265 | std::string sbuf; |
| 266 | llvm::raw_string_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 268 | if (isa<Loc>(Constraint)) { |
| 269 | os << "Assuming pointer value is "; |
| 270 | os << (Assumption ? "non-null" : "null"); |
| 271 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 273 | if (os.str().empty()) |
| 274 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 275 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 276 | // FIXME: Refactor this into BugReporterContext. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | const Stmt *S = 0; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 278 | ProgramPoint P = N->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 280 | if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
Zhongxing Xu | 03509ae | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 281 | const CFGBlock *BSrc = BE->getSrc(); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 282 | S = BSrc->getTerminatorCondition(); |
| 283 | } |
| 284 | else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) { |
| 285 | S = PS->getStmt(); |
| 286 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 288 | if (!S) |
| 289 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 291 | // Construct a new PathDiagnosticPiece. |
| 292 | PathDiagnosticLocation L(S, BRC.getSourceManager()); |
| 293 | return new PathDiagnosticEventPiece(L, os.str()); |
| 294 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 296 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 298 | }; |
| 299 | } // end anonymous namespace |
| 300 | |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 301 | static void registerTrackConstraint(BugReporterContext& BRC, |
| 302 | DefinedSVal Constraint, |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 303 | bool Assumption) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption)); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame^] | 307 | void bugreporter::registerTrackNullOrUndefValue(BugReporterContext& BRC, |
| 308 | const void *data, |
| 309 | const ExplodedNode* N) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 310 | |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 311 | const Stmt *S = static_cast<const Stmt*>(data); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 313 | if (!S) |
| 314 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 316 | GRStateManager &StateMgr = BRC.getStateManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | const GRState *state = N->getState(); |
| 318 | |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 319 | // Walk through lvalue-to-rvalue conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) { |
| 321 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 322 | const VarRegion *R = |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 323 | StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 325 | // What did we load? |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 326 | SVal V = state->getSVal(loc::MemRegionVal(R)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 327 | |
| 328 | if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 329 | || V.isUndef()) { |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 330 | ::registerFindLastStore(BRC, R, V); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 335 | SVal V = state->getSValAsScalarOrLoc(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 337 | // Uncomment this to find cases where we aren't properly getting the |
| 338 | // base value that was dereferenced. |
| 339 | // assert(!V.isUnknownOrUndef()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 341 | // Is it a symbolic value? |
| 342 | if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) { |
| 343 | const SubRegion *R = cast<SubRegion>(L->getRegion()); |
| 344 | while (R && !isa<SymbolicRegion>(R)) { |
| 345 | R = dyn_cast<SubRegion>(R->getSuperRegion()); |
| 346 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 348 | if (R) { |
| 349 | assert(isa<SymbolicRegion>(R)); |
| 350 | registerTrackConstraint(BRC, loc::MemRegionVal(R), false); |
| 351 | } |
| 352 | } |
| 353 | } |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 354 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame^] | 355 | void bugreporter::registerFindLastStore(BugReporterContext& BRC, |
| 356 | const void *data, |
| 357 | const ExplodedNode* N) { |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 358 | |
| 359 | const MemRegion *R = static_cast<const MemRegion*>(data); |
| 360 | |
| 361 | if (!R) |
| 362 | return; |
| 363 | |
| 364 | const GRState *state = N->getState(); |
| 365 | SVal V = state->getSVal(R); |
| 366 | |
| 367 | if (V.isUnknown()) |
| 368 | return; |
| 369 | |
| 370 | BRC.addVisitor(new FindLastStoreBRVisitor(V, R)); |
| 371 | } |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 372 | |
| 373 | |
| 374 | namespace { |
| 375 | class NilReceiverVisitor : public BugReporterVisitor { |
| 376 | public: |
| 377 | NilReceiverVisitor() {} |
| 378 | |
| 379 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 380 | static int x = 0; |
| 381 | ID.AddPointer(&x); |
| 382 | } |
| 383 | |
| 384 | PathDiagnosticPiece* VisitNode(const ExplodedNode *N, |
| 385 | const ExplodedNode *PrevN, |
| 386 | BugReporterContext& BRC) { |
| 387 | |
| 388 | const PostStmt *P = N->getLocationAs<PostStmt>(); |
| 389 | if (!P) |
| 390 | return 0; |
| 391 | const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>(); |
| 392 | if (!ME) |
| 393 | return 0; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 394 | const Expr *Receiver = ME->getInstanceReceiver(); |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 395 | if (!Receiver) |
| 396 | return 0; |
| 397 | const GRState *state = N->getState(); |
| 398 | const SVal &V = state->getSVal(Receiver); |
| 399 | const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V); |
| 400 | if (!DV) |
| 401 | return 0; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 402 | state = state->assume(*DV, true); |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 403 | if (state) |
| 404 | return 0; |
| 405 | |
| 406 | // The receiver was nil, and hence the method was skipped. |
| 407 | // Register a BugReporterVisitor to issue a message telling us how |
| 408 | // the receiver was null. |
| 409 | bugreporter::registerTrackNullOrUndefValue(BRC, Receiver, N); |
| 410 | // Issue a message saying that the method was skipped. |
| 411 | PathDiagnosticLocation L(Receiver, BRC.getSourceManager()); |
| 412 | return new PathDiagnosticEventPiece(L, "No method actually called " |
| 413 | "because the receiver is nil"); |
| 414 | } |
| 415 | }; |
| 416 | } // end anonymous namespace |
| 417 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame^] | 418 | void bugreporter::registerNilReceiverVisitor(BugReporterContext &BRC) { |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 419 | BRC.addVisitor(new NilReceiverVisitor()); |
| 420 | } |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 421 | |
| 422 | // Registers every VarDecl inside a Stmt with a last store vistor. |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame^] | 423 | void bugreporter::registerVarDeclsLastStore(BugReporterContext &BRC, |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 424 | const void *stmt, |
| 425 | const ExplodedNode *N) { |
| 426 | const Stmt *S = static_cast<const Stmt *>(stmt); |
| 427 | |
| 428 | std::deque<const Stmt *> WorkList; |
| 429 | |
| 430 | WorkList.push_back(S); |
| 431 | |
| 432 | while (!WorkList.empty()) { |
| 433 | const Stmt *Head = WorkList.front(); |
| 434 | WorkList.pop_front(); |
| 435 | |
| 436 | GRStateManager &StateMgr = BRC.getStateManager(); |
| 437 | const GRState *state = N->getState(); |
| 438 | |
| 439 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Head)) { |
| 440 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 441 | const VarRegion *R = |
| 442 | StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext()); |
| 443 | |
| 444 | // What did we load? |
| 445 | SVal V = state->getSVal(S); |
| 446 | |
| 447 | if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)) { |
| 448 | ::registerFindLastStore(BRC, R, V); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | for (Stmt::const_child_iterator I = Head->child_begin(); |
| 454 | I != Head->child_end(); ++I) |
| 455 | WorkList.push_back(*I); |
| 456 | } |
| 457 | } |