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 | //===----------------------------------------------------------------------===// |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 14 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitor.h" |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 15 | |
| 16 | #include "clang/AST/Expr.h" |
| 17 | #include "clang/AST/ExprObjC.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
| 19 | #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" |
Anna Zaks | 80de487 | 2012-08-29 21:22:37 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 23 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 24 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Jordan Rose | 85e9937 | 2012-09-22 01:24:46 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringExtras.h" |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 29 | using namespace ento; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 30 | |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | // Utility functions. |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Anna Zaks | 9b925ac | 2012-09-05 23:41:54 +0000 | [diff] [blame] | 35 | bool bugreporter::isDeclRefExprToReference(const Expr *E) { |
| 36 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 37 | return DRE->getDecl()->getType()->isReferenceType(); |
| 38 | } |
| 39 | return false; |
| 40 | } |
| 41 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 42 | const Stmt *bugreporter::GetDerefExpr(const ExplodedNode *N) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 43 | // Pattern match for a few useful cases (do something smarter later): |
| 44 | // a[0], p->f, *p |
Jordan Rose | 3682f1e | 2012-08-25 01:06:23 +0000 | [diff] [blame] | 45 | const PostStmt *Loc = N->getLocationAs<PostStmt>(); |
| 46 | if (!Loc) |
| 47 | return 0; |
| 48 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame] | 49 | const Expr *S = dyn_cast<Expr>(Loc->getStmt()); |
| 50 | if (!S) |
| 51 | return 0; |
| 52 | S = S->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | |
Ted Kremenek | 11abcec | 2012-05-02 00:31:29 +0000 | [diff] [blame] | 54 | while (true) { |
| 55 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S)) { |
| 56 | assert(B->isAssignmentOp()); |
| 57 | S = B->getLHS()->IgnoreParenCasts(); |
| 58 | continue; |
| 59 | } |
| 60 | else if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) { |
| 61 | if (U->getOpcode() == UO_Deref) |
| 62 | return U->getSubExpr()->IgnoreParenCasts(); |
| 63 | } |
| 64 | else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) { |
Anna Zaks | 9b925ac | 2012-09-05 23:41:54 +0000 | [diff] [blame] | 65 | if (ME->isArrow() || isDeclRefExprToReference(ME->getBase())) { |
Anna Zaks | d91696e | 2012-09-05 22:31:55 +0000 | [diff] [blame] | 66 | return ME->getBase()->IgnoreParenCasts(); |
| 67 | } |
Ted Kremenek | 11abcec | 2012-05-02 00:31:29 +0000 | [diff] [blame] | 68 | } |
Jordan Rose | 991bcb4 | 2012-09-22 01:24:38 +0000 | [diff] [blame] | 69 | else if (const ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 70 | return IvarRef->getBase()->IgnoreParenCasts(); |
| 71 | } |
Ted Kremenek | 11abcec | 2012-05-02 00:31:29 +0000 | [diff] [blame] | 72 | else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) { |
| 73 | return AE->getBase(); |
| 74 | } |
| 75 | break; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 76 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
| 78 | return NULL; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 81 | const Stmt *bugreporter::GetDenomExpr(const ExplodedNode *N) { |
Zhongxing Xu | 6403b57 | 2009-09-02 13:26:26 +0000 | [diff] [blame] | 82 | const Stmt *S = N->getLocationAs<PreStmt>()->getStmt(); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 83 | if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S)) |
| 84 | return BE->getRHS(); |
| 85 | return NULL; |
| 86 | } |
| 87 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 88 | const Stmt *bugreporter::GetRetValExpr(const ExplodedNode *N) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 89 | const Stmt *S = N->getLocationAs<PostStmt>()->getStmt(); |
| 90 | if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S)) |
| 91 | return RS->getRetValue(); |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | //===----------------------------------------------------------------------===// |
| 96 | // Definitions for bug reporter visitors. |
| 97 | //===----------------------------------------------------------------------===// |
Anna Zaks | 23f395e | 2011-08-20 01:27:22 +0000 | [diff] [blame] | 98 | |
| 99 | PathDiagnosticPiece* |
| 100 | BugReporterVisitor::getEndPath(BugReporterContext &BRC, |
| 101 | const ExplodedNode *EndPathNode, |
| 102 | BugReport &BR) { |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | PathDiagnosticPiece* |
| 107 | BugReporterVisitor::getDefaultEndPath(BugReporterContext &BRC, |
| 108 | const ExplodedNode *EndPathNode, |
| 109 | BugReport &BR) { |
Anna Zaks | 5a0917d | 2012-02-16 03:41:01 +0000 | [diff] [blame] | 110 | PathDiagnosticLocation L = |
| 111 | PathDiagnosticLocation::createEndOfPath(EndPathNode,BRC.getSourceManager()); |
Anna Zaks | 23f395e | 2011-08-20 01:27:22 +0000 | [diff] [blame] | 112 | |
| 113 | BugReport::ranges_iterator Beg, End; |
| 114 | llvm::tie(Beg, End) = BR.getRanges(); |
| 115 | |
| 116 | // Only add the statement itself as a range if we didn't specify any |
| 117 | // special ranges for this report. |
| 118 | PathDiagnosticPiece *P = new PathDiagnosticEventPiece(L, |
| 119 | BR.getDescription(), |
| 120 | Beg == End); |
| 121 | for (; Beg != End; ++Beg) |
| 122 | P->addRange(*Beg); |
| 123 | |
| 124 | return P; |
| 125 | } |
| 126 | |
| 127 | |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 128 | namespace { |
| 129 | /// Emits an extra note at the return statement of an interesting stack frame. |
| 130 | /// |
| 131 | /// The returned value is marked as an interesting value, and if it's null, |
| 132 | /// adds a visitor to track where it became null. |
| 133 | /// |
| 134 | /// This visitor is intended to be used when another visitor discovers that an |
| 135 | /// interesting value comes from an inlined function call. |
| 136 | class ReturnVisitor : public BugReporterVisitorImpl<ReturnVisitor> { |
| 137 | const StackFrameContext *StackFrame; |
Jordan Rose | 6a329ee | 2012-10-29 17:31:59 +0000 | [diff] [blame^] | 138 | enum { |
| 139 | Initial, |
| 140 | MaybeSuppress, |
| 141 | Satisfied |
| 142 | } Mode; |
| 143 | |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 144 | public: |
| 145 | ReturnVisitor(const StackFrameContext *Frame) |
Jordan Rose | 6a329ee | 2012-10-29 17:31:59 +0000 | [diff] [blame^] | 146 | : StackFrame(Frame), Mode(Initial) {} |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 147 | |
Jordan Rose | b9d4e5e | 2012-09-22 01:25:06 +0000 | [diff] [blame] | 148 | static void *getTag() { |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 149 | static int Tag = 0; |
Jordan Rose | b9d4e5e | 2012-09-22 01:25:06 +0000 | [diff] [blame] | 150 | return static_cast<void *>(&Tag); |
| 151 | } |
| 152 | |
| 153 | virtual void Profile(llvm::FoldingSetNodeID &ID) const { |
| 154 | ID.AddPointer(ReturnVisitor::getTag()); |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 155 | ID.AddPointer(StackFrame); |
| 156 | } |
| 157 | |
| 158 | /// Adds a ReturnVisitor if the given statement represents a call that was |
| 159 | /// inlined. |
| 160 | /// |
| 161 | /// This will search back through the ExplodedGraph, starting from the given |
| 162 | /// node, looking for when the given statement was processed. If it turns out |
| 163 | /// the statement is a call that was inlined, we add the visitor to the |
| 164 | /// bug report, so it can print a note later. |
| 165 | static void addVisitorIfNecessary(const ExplodedNode *Node, const Stmt *S, |
| 166 | BugReport &BR) { |
| 167 | if (!CallEvent::isCallStmt(S)) |
| 168 | return; |
| 169 | |
| 170 | // First, find when we processed the statement. |
| 171 | do { |
| 172 | if (const CallExitEnd *CEE = Node->getLocationAs<CallExitEnd>()) |
| 173 | if (CEE->getCalleeContext()->getCallSite() == S) |
| 174 | break; |
| 175 | if (const StmtPoint *SP = Node->getLocationAs<StmtPoint>()) |
| 176 | if (SP->getStmt() == S) |
| 177 | break; |
| 178 | |
| 179 | Node = Node->getFirstPred(); |
| 180 | } while (Node); |
| 181 | |
| 182 | // Next, step over any post-statement checks. |
| 183 | while (Node && isa<PostStmt>(Node->getLocation())) |
| 184 | Node = Node->getFirstPred(); |
| 185 | |
| 186 | // Finally, see if we inlined the call. |
Jordan Rose | 53221da | 2012-09-22 01:25:00 +0000 | [diff] [blame] | 187 | if (Node) { |
| 188 | if (const CallExitEnd *CEE = Node->getLocationAs<CallExitEnd>()) { |
| 189 | const StackFrameContext *CalleeContext = CEE->getCalleeContext(); |
| 190 | if (CalleeContext->getCallSite() == S) { |
| 191 | BR.markInteresting(CalleeContext); |
| 192 | BR.addVisitor(new ReturnVisitor(CalleeContext)); |
| 193 | } |
| 194 | } |
| 195 | } |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Jordan Rose | 6a329ee | 2012-10-29 17:31:59 +0000 | [diff] [blame^] | 198 | /// Returns true if any counter-suppression heuristics are enabled for |
| 199 | /// ReturnVisitor. |
| 200 | static bool hasCounterSuppression(AnalyzerOptions &Options) { |
| 201 | return Options.shouldAvoidSuppressingNullArgumentPaths(); |
| 202 | } |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 203 | |
Jordan Rose | 6a329ee | 2012-10-29 17:31:59 +0000 | [diff] [blame^] | 204 | PathDiagnosticPiece *visitNodeInitial(const ExplodedNode *N, |
| 205 | const ExplodedNode *PrevN, |
| 206 | BugReporterContext &BRC, |
| 207 | BugReport &BR) { |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 208 | // Only print a message at the interesting return statement. |
| 209 | if (N->getLocationContext() != StackFrame) |
| 210 | return 0; |
| 211 | |
| 212 | const StmtPoint *SP = N->getLocationAs<StmtPoint>(); |
| 213 | if (!SP) |
| 214 | return 0; |
| 215 | |
| 216 | const ReturnStmt *Ret = dyn_cast<ReturnStmt>(SP->getStmt()); |
| 217 | if (!Ret) |
| 218 | return 0; |
| 219 | |
| 220 | // Okay, we're at the right return statement, but do we have the return |
| 221 | // value available? |
| 222 | ProgramStateRef State = N->getState(); |
| 223 | SVal V = State->getSVal(Ret, StackFrame); |
| 224 | if (V.isUnknownOrUndef()) |
| 225 | return 0; |
| 226 | |
| 227 | // Don't print any more notes after this one. |
Jordan Rose | 6a329ee | 2012-10-29 17:31:59 +0000 | [diff] [blame^] | 228 | Mode = Satisfied; |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 229 | |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 230 | const Expr *RetE = Ret->getRetValue(); |
| 231 | assert(RetE && "Tracking a return value for a void function"); |
| 232 | RetE = RetE->IgnoreParenCasts(); |
| 233 | |
Jordan Rose | 53221da | 2012-09-22 01:25:00 +0000 | [diff] [blame] | 234 | // If we can't prove the return value is 0, just mark it interesting, and |
| 235 | // make sure to track it into any further inner functions. |
| 236 | if (State->assume(cast<DefinedSVal>(V), true)) { |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 237 | BR.markInteresting(V); |
Jordan Rose | 53221da | 2012-09-22 01:25:00 +0000 | [diff] [blame] | 238 | ReturnVisitor::addVisitorIfNecessary(N, RetE, BR); |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | // If we're returning 0, we should track where that 0 came from. |
| 243 | bugreporter::trackNullOrUndefValue(N, RetE, BR); |
| 244 | |
Jordan Rose | b9d4e5e | 2012-09-22 01:25:06 +0000 | [diff] [blame] | 245 | // Build an appropriate message based on the return value. |
| 246 | SmallString<64> Msg; |
| 247 | llvm::raw_svector_ostream Out(Msg); |
| 248 | |
Jordan Rose | 53221da | 2012-09-22 01:25:00 +0000 | [diff] [blame] | 249 | if (isa<Loc>(V)) { |
Jordan Rose | b9d4e5e | 2012-09-22 01:25:06 +0000 | [diff] [blame] | 250 | // If we are pruning null-return paths as unlikely error paths, mark the |
| 251 | // report invalid. We still want to emit a path note, however, in case |
| 252 | // the report is resurrected as valid later on. |
| 253 | ExprEngine &Eng = BRC.getBugReporter().getEngine(); |
Jordan Rose | 6a329ee | 2012-10-29 17:31:59 +0000 | [diff] [blame^] | 254 | AnalyzerOptions &Options = Eng.getAnalysisManager().options; |
| 255 | if (Options.shouldPruneNullReturnPaths()) { |
| 256 | if (hasCounterSuppression(Options)) |
| 257 | Mode = MaybeSuppress; |
| 258 | else |
| 259 | BR.markInvalid(ReturnVisitor::getTag(), StackFrame); |
| 260 | } |
Jordan Rose | b9d4e5e | 2012-09-22 01:25:06 +0000 | [diff] [blame] | 261 | |
Jordan Rose | 53221da | 2012-09-22 01:25:00 +0000 | [diff] [blame] | 262 | if (RetE->getType()->isObjCObjectPointerType()) |
| 263 | Out << "Returning nil"; |
| 264 | else |
| 265 | Out << "Returning null pointer"; |
| 266 | } else { |
| 267 | Out << "Returning zero"; |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // FIXME: We should have a more generalized location printing mechanism. |
| 271 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(RetE)) |
| 272 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(DR->getDecl())) |
| 273 | Out << " (loaded from '" << *DD << "')"; |
| 274 | |
| 275 | PathDiagnosticLocation L(Ret, BRC.getSourceManager(), StackFrame); |
| 276 | return new PathDiagnosticEventPiece(L, Out.str()); |
| 277 | } |
Jordan Rose | 6a329ee | 2012-10-29 17:31:59 +0000 | [diff] [blame^] | 278 | |
| 279 | PathDiagnosticPiece *visitNodeMaybeSuppress(const ExplodedNode *N, |
| 280 | const ExplodedNode *PrevN, |
| 281 | BugReporterContext &BRC, |
| 282 | BugReport &BR) { |
| 283 | // Are we at the entry node for this call? |
| 284 | const CallEnter *CE = N->getLocationAs<CallEnter>(); |
| 285 | if (!CE) |
| 286 | return 0; |
| 287 | |
| 288 | if (CE->getCalleeContext() != StackFrame) |
| 289 | return 0; |
| 290 | |
| 291 | Mode = Satisfied; |
| 292 | |
| 293 | ExprEngine &Eng = BRC.getBugReporter().getEngine(); |
| 294 | AnalyzerOptions &Options = Eng.getAnalysisManager().options; |
| 295 | if (Options.shouldAvoidSuppressingNullArgumentPaths()) { |
| 296 | // Don't automatically suppress a report if one of the arguments is |
| 297 | // known to be a null pointer. Instead, start tracking /that/ null |
| 298 | // value back to its origin. |
| 299 | ProgramStateManager &StateMgr = BRC.getStateManager(); |
| 300 | CallEventManager &CallMgr = StateMgr.getCallEventManager(); |
| 301 | |
| 302 | ProgramStateRef State = N->getState(); |
| 303 | CallEventRef<> Call = CallMgr.getCaller(StackFrame, State); |
| 304 | for (unsigned I = 0, E = Call->getNumArgs(); I != E; ++I) { |
| 305 | SVal ArgV = Call->getArgSVal(I); |
| 306 | if (!isa<Loc>(ArgV)) |
| 307 | continue; |
| 308 | |
| 309 | const Expr *ArgE = Call->getArgExpr(I); |
| 310 | if (!ArgE) |
| 311 | continue; |
| 312 | |
| 313 | // Is it possible for this argument to be non-null? |
| 314 | if (State->assume(cast<Loc>(ArgV), true)) |
| 315 | continue; |
| 316 | |
| 317 | if (bugreporter::trackNullOrUndefValue(N, ArgE, BR, /*IsArg=*/true)) |
| 318 | return 0; |
| 319 | |
| 320 | // If we /can't/ track the null pointer, we should err on the side of |
| 321 | // false negatives, and continue towards marking this report invalid. |
| 322 | // (We will still look at the other arguments, though.) |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | // There is no reason not to suppress this report; go ahead and do it. |
| 327 | BR.markInvalid(ReturnVisitor::getTag(), StackFrame); |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | PathDiagnosticPiece *VisitNode(const ExplodedNode *N, |
| 332 | const ExplodedNode *PrevN, |
| 333 | BugReporterContext &BRC, |
| 334 | BugReport &BR) { |
| 335 | switch (Mode) { |
| 336 | case Initial: |
| 337 | return visitNodeInitial(N, PrevN, BRC, BR); |
| 338 | case MaybeSuppress: |
| 339 | return visitNodeMaybeSuppress(N, PrevN, BRC, BR); |
| 340 | case Satisfied: |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | llvm_unreachable("Invalid visit mode!"); |
| 345 | } |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 346 | }; |
| 347 | } // end anonymous namespace |
| 348 | |
| 349 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 350 | void FindLastStoreBRVisitor ::Profile(llvm::FoldingSetNodeID &ID) const { |
| 351 | static int tag = 0; |
| 352 | ID.AddPointer(&tag); |
| 353 | ID.AddPointer(R); |
| 354 | ID.Add(V); |
| 355 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 356 | |
Jordan Rose | 166b7bd | 2012-08-28 00:50:45 +0000 | [diff] [blame] | 357 | PathDiagnosticPiece *FindLastStoreBRVisitor::VisitNode(const ExplodedNode *Succ, |
| 358 | const ExplodedNode *Pred, |
| 359 | BugReporterContext &BRC, |
| 360 | BugReport &BR) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 362 | if (satisfied) |
| 363 | return NULL; |
| 364 | |
Jordan Rose | 166b7bd | 2012-08-28 00:50:45 +0000 | [diff] [blame] | 365 | const ExplodedNode *StoreSite = 0; |
| 366 | const Expr *InitE = 0; |
Jordan Rose | 09f7bf1 | 2012-10-29 17:31:53 +0000 | [diff] [blame] | 367 | bool IsParam = false; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 368 | |
Jordan Rose | 166b7bd | 2012-08-28 00:50:45 +0000 | [diff] [blame] | 369 | // First see if we reached the declaration of the region. |
| 370 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
| 371 | if (const PostStmt *P = Pred->getLocationAs<PostStmt>()) { |
| 372 | if (const DeclStmt *DS = P->getStmtAs<DeclStmt>()) { |
| 373 | if (DS->getSingleDecl() == VR->getDecl()) { |
| 374 | StoreSite = Pred; |
| 375 | InitE = VR->getDecl()->getInit(); |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 376 | } |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 377 | } |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 378 | } |
Ted Kremenek | 1b43102 | 2010-03-20 18:01:57 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Jordan Rose | 166b7bd | 2012-08-28 00:50:45 +0000 | [diff] [blame] | 381 | // Otherwise, check that Succ has this binding and Pred does not, i.e. this is |
| 382 | // where the binding first occurred. |
| 383 | if (!StoreSite) { |
| 384 | if (Succ->getState()->getSVal(R) != V) |
| 385 | return NULL; |
| 386 | if (Pred->getState()->getSVal(R) == V) |
| 387 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
Jordan Rose | 166b7bd | 2012-08-28 00:50:45 +0000 | [diff] [blame] | 389 | StoreSite = Succ; |
| 390 | |
| 391 | // If this is an assignment expression, we can track the value |
| 392 | // being assigned. |
| 393 | if (const PostStmt *P = Succ->getLocationAs<PostStmt>()) |
| 394 | if (const BinaryOperator *BO = P->getStmtAs<BinaryOperator>()) |
| 395 | if (BO->isAssignmentOp()) |
| 396 | InitE = BO->getRHS(); |
Jordan Rose | 85e9937 | 2012-09-22 01:24:46 +0000 | [diff] [blame] | 397 | |
| 398 | // If this is a call entry, the variable should be a parameter. |
| 399 | // FIXME: Handle CXXThisRegion as well. (This is not a priority because |
| 400 | // 'this' should never be NULL, but this visitor isn't just for NULL and |
| 401 | // UndefinedVal.) |
| 402 | if (const CallEnter *CE = Succ->getLocationAs<CallEnter>()) { |
| 403 | const VarRegion *VR = cast<VarRegion>(R); |
| 404 | const ParmVarDecl *Param = cast<ParmVarDecl>(VR->getDecl()); |
| 405 | |
| 406 | ProgramStateManager &StateMgr = BRC.getStateManager(); |
| 407 | CallEventManager &CallMgr = StateMgr.getCallEventManager(); |
| 408 | |
| 409 | CallEventRef<> Call = CallMgr.getCaller(CE->getCalleeContext(), |
| 410 | Succ->getState()); |
| 411 | InitE = Call->getArgExpr(Param->getFunctionScopeIndex()); |
Jordan Rose | 09f7bf1 | 2012-10-29 17:31:53 +0000 | [diff] [blame] | 412 | IsParam = true; |
Jordan Rose | 85e9937 | 2012-09-22 01:24:46 +0000 | [diff] [blame] | 413 | } |
Jordan Rose | 166b7bd | 2012-08-28 00:50:45 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | if (!StoreSite) |
| 417 | return NULL; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 418 | satisfied = true; |
Jordan Rose | 166b7bd | 2012-08-28 00:50:45 +0000 | [diff] [blame] | 419 | |
Jordan Rose | 53221da | 2012-09-22 01:25:00 +0000 | [diff] [blame] | 420 | // If we have an expression that provided the value, try to track where it |
| 421 | // came from. |
Jordan Rose | 166b7bd | 2012-08-28 00:50:45 +0000 | [diff] [blame] | 422 | if (InitE) { |
Jordan Rose | 09f7bf1 | 2012-10-29 17:31:53 +0000 | [diff] [blame] | 423 | if (V.isUndef() || isa<loc::ConcreteInt>(V)) { |
| 424 | if (!IsParam) |
| 425 | InitE = InitE->IgnoreParenCasts(); |
| 426 | bugreporter::trackNullOrUndefValue(StoreSite, InitE, BR, IsParam); |
| 427 | } else { |
| 428 | ReturnVisitor::addVisitorIfNecessary(StoreSite, InitE->IgnoreParenCasts(), |
| 429 | BR); |
| 430 | } |
Jordan Rose | 166b7bd | 2012-08-28 00:50:45 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Jordan Rose | 85e9937 | 2012-09-22 01:24:46 +0000 | [diff] [blame] | 433 | if (!R->canPrintPretty()) |
| 434 | return 0; |
| 435 | |
Jordan Rose | 166b7bd | 2012-08-28 00:50:45 +0000 | [diff] [blame] | 436 | // Okay, we've found the binding. Emit an appropriate message. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 437 | SmallString<256> sbuf; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 438 | llvm::raw_svector_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Jordan Rose | 166b7bd | 2012-08-28 00:50:45 +0000 | [diff] [blame] | 440 | if (const PostStmt *PS = StoreSite->getLocationAs<PostStmt>()) { |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 441 | if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 442 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 443 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 444 | os << "Variable '" << *VR->getDecl() << "' "; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 445 | } |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 446 | else |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 447 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 448 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 449 | if (isa<loc::ConcreteInt>(V)) { |
| 450 | bool b = false; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 451 | if (R->isBoundable()) { |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 452 | if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 453 | if (TR->getValueType()->isObjCObjectPointerType()) { |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 454 | os << "initialized to nil"; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 455 | b = true; |
| 456 | } |
| 457 | } |
| 458 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 460 | if (!b) |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 461 | os << "initialized to a null pointer value"; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 462 | } |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 463 | else if (isa<nonloc::ConcreteInt>(V)) { |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 464 | os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue(); |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 465 | } |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 466 | else if (V.isUndef()) { |
| 467 | if (isa<VarRegion>(R)) { |
| 468 | const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl()); |
| 469 | if (VD->getInit()) |
| 470 | os << "initialized to a garbage value"; |
| 471 | else |
| 472 | os << "declared without an initial value"; |
| 473 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 474 | } |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 475 | else { |
| 476 | os << "initialized here"; |
| 477 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 478 | } |
Jordan Rose | 85e9937 | 2012-09-22 01:24:46 +0000 | [diff] [blame] | 479 | } else if (isa<CallEnter>(StoreSite->getLocation())) { |
| 480 | const ParmVarDecl *Param = cast<ParmVarDecl>(cast<VarRegion>(R)->getDecl()); |
| 481 | |
| 482 | os << "Passing "; |
| 483 | |
| 484 | if (isa<loc::ConcreteInt>(V)) { |
| 485 | if (Param->getType()->isObjCObjectPointerType()) |
| 486 | os << "nil object reference"; |
| 487 | else |
| 488 | os << "null pointer value"; |
| 489 | } else if (V.isUndef()) { |
| 490 | os << "uninitialized value"; |
| 491 | } else if (isa<nonloc::ConcreteInt>(V)) { |
| 492 | os << "the value " << cast<nonloc::ConcreteInt>(V).getValue(); |
| 493 | } else { |
| 494 | os << "value"; |
| 495 | } |
| 496 | |
| 497 | // Printed parameter indexes are 1-based, not 0-based. |
| 498 | unsigned Idx = Param->getFunctionScopeIndex() + 1; |
| 499 | os << " via " << Idx << llvm::getOrdinalSuffix(Idx) << " parameter '"; |
| 500 | |
| 501 | R->printPretty(os); |
| 502 | os << '\''; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | if (os.str().empty()) { |
| 506 | if (isa<loc::ConcreteInt>(V)) { |
| 507 | bool b = false; |
| 508 | if (R->isBoundable()) { |
| 509 | if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) { |
| 510 | if (TR->getValueType()->isObjCObjectPointerType()) { |
| 511 | os << "nil object reference stored to "; |
| 512 | b = true; |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | if (!b) |
| 518 | os << "Null pointer value stored to "; |
| 519 | } |
| 520 | else if (V.isUndef()) { |
| 521 | os << "Uninitialized value stored to "; |
| 522 | } |
| 523 | else if (isa<nonloc::ConcreteInt>(V)) { |
| 524 | os << "The value " << cast<nonloc::ConcreteInt>(V).getValue() |
| 525 | << " is assigned to "; |
| 526 | } |
| 527 | else |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 528 | os << "Value assigned to "; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 529 | |
Jordan Rose | 85e9937 | 2012-09-22 01:24:46 +0000 | [diff] [blame] | 530 | os << '\''; |
| 531 | R->printPretty(os); |
| 532 | os << '\''; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 535 | // Construct a new PathDiagnosticPiece. |
Jordan Rose | 166b7bd | 2012-08-28 00:50:45 +0000 | [diff] [blame] | 536 | ProgramPoint P = StoreSite->getLocation(); |
Jordan Rose | 85e9937 | 2012-09-22 01:24:46 +0000 | [diff] [blame] | 537 | PathDiagnosticLocation L; |
| 538 | if (isa<CallEnter>(P)) |
| 539 | L = PathDiagnosticLocation(InitE, BRC.getSourceManager(), |
| 540 | P.getLocationContext()); |
| 541 | else |
| 542 | L = PathDiagnosticLocation::create(P, BRC.getSourceManager()); |
Anna Zaks | 4fdf97b | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 543 | if (!L.isValid()) |
| 544 | return NULL; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 545 | return new PathDiagnosticEventPiece(L, os.str()); |
| 546 | } |
| 547 | |
| 548 | void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const { |
| 549 | static int tag = 0; |
| 550 | ID.AddPointer(&tag); |
| 551 | ID.AddBoolean(Assumption); |
| 552 | ID.Add(Constraint); |
| 553 | } |
| 554 | |
Ted Kremenek | b85cce0 | 2012-10-25 22:07:10 +0000 | [diff] [blame] | 555 | /// Return the tag associated with this visitor. This tag will be used |
| 556 | /// to make all PathDiagnosticPieces created by this visitor. |
| 557 | const char *TrackConstraintBRVisitor::getTag() { |
| 558 | return "TrackConstraintBRVisitor"; |
| 559 | } |
| 560 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 561 | PathDiagnosticPiece * |
| 562 | TrackConstraintBRVisitor::VisitNode(const ExplodedNode *N, |
| 563 | const ExplodedNode *PrevN, |
| 564 | BugReporterContext &BRC, |
| 565 | BugReport &BR) { |
| 566 | if (isSatisfied) |
| 567 | return NULL; |
| 568 | |
| 569 | // Check if in the previous state it was feasible for this constraint |
| 570 | // to *not* be true. |
| 571 | if (PrevN->getState()->assume(Constraint, !Assumption)) { |
| 572 | |
| 573 | isSatisfied = true; |
| 574 | |
| 575 | // As a sanity check, make sure that the negation of the constraint |
| 576 | // was infeasible in the current state. If it is feasible, we somehow |
| 577 | // missed the transition point. |
| 578 | if (N->getState()->assume(Constraint, !Assumption)) |
| 579 | return NULL; |
| 580 | |
| 581 | // We found the transition point for the constraint. We now need to |
| 582 | // pretty-print the constraint. (work-in-progress) |
| 583 | std::string sbuf; |
| 584 | llvm::raw_string_ostream os(sbuf); |
| 585 | |
| 586 | if (isa<Loc>(Constraint)) { |
| 587 | os << "Assuming pointer value is "; |
| 588 | os << (Assumption ? "non-null" : "null"); |
| 589 | } |
| 590 | |
| 591 | if (os.str().empty()) |
| 592 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 593 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 594 | // Construct a new PathDiagnosticPiece. |
Anna Zaks | 4fdf97b | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 595 | ProgramPoint P = N->getLocation(); |
Anna Zaks | 1531bb0 | 2011-09-20 01:38:47 +0000 | [diff] [blame] | 596 | PathDiagnosticLocation L = |
| 597 | PathDiagnosticLocation::create(P, BRC.getSourceManager()); |
Anna Zaks | 4fdf97b | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 598 | if (!L.isValid()) |
| 599 | return NULL; |
Ted Kremenek | b85cce0 | 2012-10-25 22:07:10 +0000 | [diff] [blame] | 600 | |
| 601 | PathDiagnosticEventPiece *X = new PathDiagnosticEventPiece(L, os.str()); |
| 602 | X->setTag(getTag()); |
| 603 | return X; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 604 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 605 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 606 | return NULL; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Jordan Rose | 6a329ee | 2012-10-29 17:31:59 +0000 | [diff] [blame^] | 609 | bool bugreporter::trackNullOrUndefValue(const ExplodedNode *N, const Stmt *S, |
Jordan Rose | 09f7bf1 | 2012-10-29 17:31:53 +0000 | [diff] [blame] | 610 | BugReport &report, bool IsArg) { |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 611 | if (!S || !N) |
Jordan Rose | 6a329ee | 2012-10-29 17:31:59 +0000 | [diff] [blame^] | 612 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 613 | |
Jordan Rose | 6686b66 | 2012-09-22 01:24:49 +0000 | [diff] [blame] | 614 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(S)) |
| 615 | S = OVE->getSourceExpr(); |
| 616 | |
Jordan Rose | 09f7bf1 | 2012-10-29 17:31:53 +0000 | [diff] [blame] | 617 | if (IsArg) { |
| 618 | assert(isa<CallEnter>(N->getLocation()) && "Tracking arg but not at call"); |
| 619 | } else { |
| 620 | // Walk through nodes until we get one that matches the statement exactly. |
| 621 | do { |
| 622 | const ProgramPoint &pp = N->getLocation(); |
| 623 | if (const PostStmt *ps = dyn_cast<PostStmt>(&pp)) { |
| 624 | if (ps->getStmt() == S) |
| 625 | break; |
| 626 | } else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&pp)) { |
| 627 | if (CEE->getCalleeContext()->getCallSite() == S) |
| 628 | break; |
| 629 | } |
| 630 | N = N->getFirstPred(); |
| 631 | } while (N); |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame] | 632 | |
Jordan Rose | 09f7bf1 | 2012-10-29 17:31:53 +0000 | [diff] [blame] | 633 | if (!N) |
Jordan Rose | 6a329ee | 2012-10-29 17:31:59 +0000 | [diff] [blame^] | 634 | return false; |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 635 | } |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 636 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 637 | ProgramStateRef state = N->getState(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame] | 639 | // See if the expression we're interested refers to a variable. |
| 640 | // If so, we can track both its contents and constraints on its value. |
| 641 | if (const Expr *Ex = dyn_cast<Expr>(S)) { |
| 642 | // Strip off parens and casts. Note that this will never have issues with |
| 643 | // C++ user-defined implicit conversions, because those have a constructor |
| 644 | // or function call inside. |
Jordan Rose | e6cd054 | 2012-08-16 00:03:33 +0000 | [diff] [blame] | 645 | Ex = Ex->IgnoreParenCasts(); |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 646 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) { |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame] | 647 | // FIXME: Right now we only track VarDecls because it's non-trivial to |
| 648 | // get a MemRegion for any other DeclRefExprs. <rdar://problem/12114812> |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 649 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
Jordan Rose | 09f7bf1 | 2012-10-29 17:31:53 +0000 | [diff] [blame] | 650 | ProgramStateManager &StateMgr = state->getStateManager(); |
| 651 | MemRegionManager &MRMgr = StateMgr.getRegionManager(); |
| 652 | const VarRegion *R = MRMgr.getVarRegion(VD, N->getLocationContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 653 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame] | 654 | // Mark both the variable region and its contents as interesting. |
Ted Kremenek | 11abcec | 2012-05-02 00:31:29 +0000 | [diff] [blame] | 655 | SVal V = state->getRawSVal(loc::MemRegionVal(R)); |
Jordan Rose | 09f7bf1 | 2012-10-29 17:31:53 +0000 | [diff] [blame] | 656 | |
| 657 | // If the value matches the default for the variable region, that |
| 658 | // might mean that it's been cleared out of the state. Fall back to |
| 659 | // the full argument expression (with casts and such intact). |
| 660 | if (IsArg) { |
| 661 | bool UseArgValue = V.isUnknownOrUndef() || V.isZeroConstant(); |
| 662 | if (!UseArgValue) { |
| 663 | const SymbolRegionValue *SRV = |
| 664 | dyn_cast_or_null<SymbolRegionValue>(V.getAsLocSymbol()); |
| 665 | if (SRV) |
| 666 | UseArgValue = (SRV->getRegion() == R); |
| 667 | } |
| 668 | if (UseArgValue) |
| 669 | V = state->getSValAsScalarOrLoc(S, N->getLocationContext()); |
| 670 | } |
| 671 | |
Jordan Rose | a1f81bb | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 672 | report.markInteresting(R); |
| 673 | report.markInteresting(V); |
Anna Zaks | 80de487 | 2012-08-29 21:22:37 +0000 | [diff] [blame] | 674 | report.addVisitor(new UndefOrNullArgVisitor(R)); |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 675 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame] | 676 | // If the contents are symbolic, find out when they became null. |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 677 | if (V.getAsLocSymbol()) { |
| 678 | BugReporterVisitor *ConstraintTracker |
Jordan Rose | 53221da | 2012-09-22 01:25:00 +0000 | [diff] [blame] | 679 | = new TrackConstraintBRVisitor(cast<DefinedSVal>(V), false); |
Jordan Rose | a1f81bb | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 680 | report.addVisitor(ConstraintTracker); |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Jordan Rose | a1f81bb | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 683 | report.addVisitor(new FindLastStoreBRVisitor(V, R)); |
Jordan Rose | 6a329ee | 2012-10-29 17:31:59 +0000 | [diff] [blame^] | 684 | return true; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 688 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame] | 689 | // If the expression does NOT refer to a variable, we can still track |
| 690 | // constraints on its contents. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 691 | SVal V = state->getSValAsScalarOrLoc(S, N->getLocationContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 692 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 693 | // Uncomment this to find cases where we aren't properly getting the |
| 694 | // base value that was dereferenced. |
| 695 | // assert(!V.isUnknownOrUndef()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 697 | // Is it a symbolic value? |
| 698 | if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) { |
Anna Zaks | d91696e | 2012-09-05 22:31:55 +0000 | [diff] [blame] | 699 | // At this point we are dealing with the region's LValue. |
| 700 | // However, if the rvalue is a symbolic region, we should track it as well. |
| 701 | SVal RVal = state->getSVal(L->getRegion()); |
| 702 | const MemRegion *RegionRVal = RVal.getAsRegion(); |
Anna Zaks | 522fc21 | 2012-09-12 22:57:30 +0000 | [diff] [blame] | 703 | report.addVisitor(new UndefOrNullArgVisitor(L->getRegion())); |
| 704 | |
Anna Zaks | d91696e | 2012-09-05 22:31:55 +0000 | [diff] [blame] | 705 | |
| 706 | if (RegionRVal && isa<SymbolicRegion>(RegionRVal)) { |
| 707 | report.markInteresting(RegionRVal); |
| 708 | report.addVisitor(new TrackConstraintBRVisitor( |
| 709 | loc::MemRegionVal(RegionRVal), false)); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 710 | } |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame] | 711 | } else { |
| 712 | // Otherwise, if the value came from an inlined function call, |
| 713 | // we should at least make sure that function isn't pruned in our output. |
Jordan Rose | 53221da | 2012-09-22 01:25:00 +0000 | [diff] [blame] | 714 | if (const Expr *E = dyn_cast<Expr>(S)) |
| 715 | S = E->IgnoreParenCasts(); |
Jordan Rose | a1f81bb | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 716 | ReturnVisitor::addVisitorIfNecessary(N, S, report); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 717 | } |
Jordan Rose | 6a329ee | 2012-10-29 17:31:59 +0000 | [diff] [blame^] | 718 | |
| 719 | return true; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 720 | } |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 721 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 722 | BugReporterVisitor * |
| 723 | FindLastStoreBRVisitor::createVisitorObject(const ExplodedNode *N, |
| 724 | const MemRegion *R) { |
| 725 | assert(R && "The memory region is null."); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 726 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 727 | ProgramStateRef state = N->getState(); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 728 | SVal V = state->getSVal(R); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 729 | if (V.isUnknown()) |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 730 | return 0; |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 731 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 732 | return new FindLastStoreBRVisitor(V, R); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 733 | } |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 734 | |
| 735 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 736 | PathDiagnosticPiece *NilReceiverBRVisitor::VisitNode(const ExplodedNode *N, |
| 737 | const ExplodedNode *PrevN, |
| 738 | BugReporterContext &BRC, |
| 739 | BugReport &BR) { |
| 740 | const PostStmt *P = N->getLocationAs<PostStmt>(); |
| 741 | if (!P) |
| 742 | return 0; |
| 743 | const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>(); |
| 744 | if (!ME) |
| 745 | return 0; |
| 746 | const Expr *Receiver = ME->getInstanceReceiver(); |
| 747 | if (!Receiver) |
| 748 | return 0; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 749 | ProgramStateRef state = N->getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 750 | const SVal &V = state->getSVal(Receiver, N->getLocationContext()); |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 751 | const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V); |
| 752 | if (!DV) |
| 753 | return 0; |
| 754 | state = state->assume(*DV, true); |
| 755 | if (state) |
| 756 | return 0; |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 757 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 758 | // The receiver was nil, and hence the method was skipped. |
| 759 | // Register a BugReporterVisitor to issue a message telling us how |
| 760 | // the receiver was null. |
Jordan Rose | a1f81bb | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 761 | bugreporter::trackNullOrUndefValue(N, Receiver, BR); |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 762 | // Issue a message saying that the method was skipped. |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 763 | PathDiagnosticLocation L(Receiver, BRC.getSourceManager(), |
| 764 | N->getLocationContext()); |
Anna Zaks | 907344e | 2012-04-05 02:10:19 +0000 | [diff] [blame] | 765 | return new PathDiagnosticEventPiece(L, "No method is called " |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 766 | "because the receiver is nil"); |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 767 | } |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 768 | |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame] | 769 | // Registers every VarDecl inside a Stmt with a last store visitor. |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 770 | void FindLastStoreBRVisitor::registerStatementVarDecls(BugReport &BR, |
| 771 | const Stmt *S) { |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame] | 772 | const ExplodedNode *N = BR.getErrorNode(); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 773 | std::deque<const Stmt *> WorkList; |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 774 | WorkList.push_back(S); |
| 775 | |
| 776 | while (!WorkList.empty()) { |
| 777 | const Stmt *Head = WorkList.front(); |
| 778 | WorkList.pop_front(); |
| 779 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 780 | ProgramStateRef state = N->getState(); |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame] | 781 | ProgramStateManager &StateMgr = state->getStateManager(); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 782 | |
| 783 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Head)) { |
| 784 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 785 | const VarRegion *R = |
| 786 | StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext()); |
| 787 | |
| 788 | // What did we load? |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 789 | SVal V = state->getSVal(S, N->getLocationContext()); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 790 | |
| 791 | if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)) { |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 792 | // Register a new visitor with the BugReport. |
| 793 | BR.addVisitor(new FindLastStoreBRVisitor(V, R)); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 794 | } |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | for (Stmt::const_child_iterator I = Head->child_begin(); |
| 799 | I != Head->child_end(); ++I) |
| 800 | WorkList.push_back(*I); |
| 801 | } |
| 802 | } |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 803 | |
| 804 | //===----------------------------------------------------------------------===// |
| 805 | // Visitor that tries to report interesting diagnostics from conditions. |
| 806 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b85cce0 | 2012-10-25 22:07:10 +0000 | [diff] [blame] | 807 | |
| 808 | /// Return the tag associated with this visitor. This tag will be used |
| 809 | /// to make all PathDiagnosticPieces created by this visitor. |
| 810 | const char *ConditionBRVisitor::getTag() { |
| 811 | return "ConditionBRVisitor"; |
| 812 | } |
| 813 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 814 | PathDiagnosticPiece *ConditionBRVisitor::VisitNode(const ExplodedNode *N, |
| 815 | const ExplodedNode *Prev, |
| 816 | BugReporterContext &BRC, |
| 817 | BugReport &BR) { |
Ted Kremenek | c89f4b0 | 2012-02-28 23:06:21 +0000 | [diff] [blame] | 818 | PathDiagnosticPiece *piece = VisitNodeImpl(N, Prev, BRC, BR); |
Ted Kremenek | b85cce0 | 2012-10-25 22:07:10 +0000 | [diff] [blame] | 819 | if (piece) { |
| 820 | piece->setTag(getTag()); |
| 821 | if (PathDiagnosticEventPiece *ev=dyn_cast<PathDiagnosticEventPiece>(piece)) |
| 822 | ev->setPrunable(true, /* override */ false); |
| 823 | } |
Ted Kremenek | c89f4b0 | 2012-02-28 23:06:21 +0000 | [diff] [blame] | 824 | return piece; |
| 825 | } |
| 826 | |
| 827 | PathDiagnosticPiece *ConditionBRVisitor::VisitNodeImpl(const ExplodedNode *N, |
| 828 | const ExplodedNode *Prev, |
| 829 | BugReporterContext &BRC, |
| 830 | BugReport &BR) { |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 831 | |
Ted Kremenek | 22505ef | 2012-09-08 07:18:18 +0000 | [diff] [blame] | 832 | ProgramPoint progPoint = N->getLocation(); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 833 | ProgramStateRef CurrentState = N->getState(); |
| 834 | ProgramStateRef PrevState = Prev->getState(); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 835 | |
| 836 | // Compare the GDMs of the state, because that is where constraints |
| 837 | // are managed. Note that ensure that we only look at nodes that |
| 838 | // were generated by the analyzer engine proper, not checkers. |
| 839 | if (CurrentState->getGDM().getRoot() == |
| 840 | PrevState->getGDM().getRoot()) |
| 841 | return 0; |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 842 | |
| 843 | // If an assumption was made on a branch, it should be caught |
| 844 | // here by looking at the state transition. |
| 845 | if (const BlockEdge *BE = dyn_cast<BlockEdge>(&progPoint)) { |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 846 | const CFGBlock *srcBlk = BE->getSrc(); |
| 847 | if (const Stmt *term = srcBlk->getTerminator()) |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 848 | return VisitTerminator(term, N, srcBlk, BE->getDst(), BR, BRC); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | if (const PostStmt *PS = dyn_cast<PostStmt>(&progPoint)) { |
| 853 | // FIXME: Assuming that BugReporter is a GRBugReporter is a layering |
| 854 | // violation. |
| 855 | const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags = |
| 856 | cast<GRBugReporter>(BRC.getBugReporter()). |
Ted Kremenek | 0caa2d4 | 2012-08-30 19:26:48 +0000 | [diff] [blame] | 857 | getEngine().geteagerlyAssumeBinOpBifurcationTags(); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 858 | |
| 859 | const ProgramPointTag *tag = PS->getTag(); |
| 860 | if (tag == tags.first) |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 861 | return VisitTrueTest(cast<Expr>(PS->getStmt()), true, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 862 | BRC, BR, N); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 863 | if (tag == tags.second) |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 864 | return VisitTrueTest(cast<Expr>(PS->getStmt()), false, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 865 | BRC, BR, N); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 866 | |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | PathDiagnosticPiece * |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 874 | ConditionBRVisitor::VisitTerminator(const Stmt *Term, |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 875 | const ExplodedNode *N, |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 876 | const CFGBlock *srcBlk, |
| 877 | const CFGBlock *dstBlk, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 878 | BugReport &R, |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 879 | BugReporterContext &BRC) { |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 880 | const Expr *Cond = 0; |
| 881 | |
| 882 | switch (Term->getStmtClass()) { |
| 883 | default: |
| 884 | return 0; |
| 885 | case Stmt::IfStmtClass: |
| 886 | Cond = cast<IfStmt>(Term)->getCond(); |
| 887 | break; |
| 888 | case Stmt::ConditionalOperatorClass: |
| 889 | Cond = cast<ConditionalOperator>(Term)->getCond(); |
| 890 | break; |
| 891 | } |
| 892 | |
| 893 | assert(Cond); |
| 894 | assert(srcBlk->succ_size() == 2); |
| 895 | const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk; |
Ted Kremenek | c47dc1b | 2012-09-07 06:51:37 +0000 | [diff] [blame] | 896 | return VisitTrueTest(Cond, tookTrue, BRC, R, N); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | PathDiagnosticPiece * |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 900 | ConditionBRVisitor::VisitTrueTest(const Expr *Cond, |
| 901 | bool tookTrue, |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 902 | BugReporterContext &BRC, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 903 | BugReport &R, |
| 904 | const ExplodedNode *N) { |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 905 | |
| 906 | const Expr *Ex = Cond; |
| 907 | |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 908 | while (true) { |
Ted Kremenek | c47dc1b | 2012-09-07 06:51:37 +0000 | [diff] [blame] | 909 | Ex = Ex->IgnoreParenCasts(); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 910 | switch (Ex->getStmtClass()) { |
| 911 | default: |
| 912 | return 0; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 913 | case Stmt::BinaryOperatorClass: |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 914 | return VisitTrueTest(Cond, cast<BinaryOperator>(Ex), tookTrue, BRC, |
| 915 | R, N); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 916 | case Stmt::DeclRefExprClass: |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 917 | return VisitTrueTest(Cond, cast<DeclRefExpr>(Ex), tookTrue, BRC, |
| 918 | R, N); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 919 | case Stmt::UnaryOperatorClass: { |
| 920 | const UnaryOperator *UO = cast<UnaryOperator>(Ex); |
| 921 | if (UO->getOpcode() == UO_LNot) { |
| 922 | tookTrue = !tookTrue; |
Ted Kremenek | c47dc1b | 2012-09-07 06:51:37 +0000 | [diff] [blame] | 923 | Ex = UO->getSubExpr(); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 924 | continue; |
| 925 | } |
| 926 | return 0; |
| 927 | } |
| 928 | } |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 932 | bool ConditionBRVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 933 | BugReporterContext &BRC, |
| 934 | BugReport &report, |
| 935 | const ExplodedNode *N, |
| 936 | llvm::Optional<bool> &prunable) { |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 937 | const Expr *OriginalExpr = Ex; |
| 938 | Ex = Ex->IgnoreParenCasts(); |
| 939 | |
| 940 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) { |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 941 | const bool quotes = isa<VarDecl>(DR->getDecl()); |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 942 | if (quotes) { |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 943 | Out << '\''; |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 944 | const LocationContext *LCtx = N->getLocationContext(); |
| 945 | const ProgramState *state = N->getState().getPtr(); |
| 946 | if (const MemRegion *R = state->getLValue(cast<VarDecl>(DR->getDecl()), |
| 947 | LCtx).getAsRegion()) { |
| 948 | if (report.isInteresting(R)) |
| 949 | prunable = false; |
| 950 | else { |
| 951 | const ProgramState *state = N->getState().getPtr(); |
| 952 | SVal V = state->getSVal(R); |
| 953 | if (report.isInteresting(V)) |
| 954 | prunable = false; |
| 955 | } |
| 956 | } |
| 957 | } |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 958 | Out << DR->getDecl()->getDeclName().getAsString(); |
| 959 | if (quotes) |
| 960 | Out << '\''; |
| 961 | return quotes; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(Ex)) { |
| 965 | QualType OriginalTy = OriginalExpr->getType(); |
| 966 | if (OriginalTy->isPointerType()) { |
| 967 | if (IL->getValue() == 0) { |
| 968 | Out << "null"; |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 969 | return false; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | else if (OriginalTy->isObjCObjectPointerType()) { |
| 973 | if (IL->getValue() == 0) { |
| 974 | Out << "nil"; |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 975 | return false; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | |
| 979 | Out << IL->getValue(); |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 980 | return false; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 981 | } |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 982 | |
| 983 | return false; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | PathDiagnosticPiece * |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 987 | ConditionBRVisitor::VisitTrueTest(const Expr *Cond, |
| 988 | const BinaryOperator *BExpr, |
| 989 | const bool tookTrue, |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 990 | BugReporterContext &BRC, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 991 | BugReport &R, |
| 992 | const ExplodedNode *N) { |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 993 | |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 994 | bool shouldInvert = false; |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 995 | llvm::Optional<bool> shouldPrune; |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 996 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 997 | SmallString<128> LhsString, RhsString; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 998 | { |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 999 | llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString); |
| 1000 | const bool isVarLHS = patternMatch(BExpr->getLHS(), OutLHS, BRC, R, N, |
| 1001 | shouldPrune); |
| 1002 | const bool isVarRHS = patternMatch(BExpr->getRHS(), OutRHS, BRC, R, N, |
| 1003 | shouldPrune); |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 1004 | |
| 1005 | shouldInvert = !isVarLHS && isVarRHS; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 1008 | BinaryOperator::Opcode Op = BExpr->getOpcode(); |
| 1009 | |
| 1010 | if (BinaryOperator::isAssignmentOp(Op)) { |
| 1011 | // For assignment operators, all that we care about is that the LHS |
| 1012 | // evaluates to "true" or "false". |
| 1013 | return VisitConditionVariable(LhsString, BExpr->getLHS(), tookTrue, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 1014 | BRC, R, N); |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | // For non-assignment operations, we require that we can understand |
| 1018 | // both the LHS and RHS. |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 1019 | if (LhsString.empty() || RhsString.empty()) |
| 1020 | return 0; |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 1021 | |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 1022 | // Should we invert the strings if the LHS is not a variable name? |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1023 | SmallString<256> buf; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 1024 | llvm::raw_svector_ostream Out(buf); |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 1025 | Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is "; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 1026 | |
| 1027 | // Do we need to invert the opcode? |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 1028 | if (shouldInvert) |
| 1029 | switch (Op) { |
| 1030 | default: break; |
| 1031 | case BO_LT: Op = BO_GT; break; |
| 1032 | case BO_GT: Op = BO_LT; break; |
| 1033 | case BO_LE: Op = BO_GE; break; |
| 1034 | case BO_GE: Op = BO_LE; break; |
| 1035 | } |
| 1036 | |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 1037 | if (!tookTrue) |
| 1038 | switch (Op) { |
| 1039 | case BO_EQ: Op = BO_NE; break; |
| 1040 | case BO_NE: Op = BO_EQ; break; |
| 1041 | case BO_LT: Op = BO_GE; break; |
| 1042 | case BO_GT: Op = BO_LE; break; |
| 1043 | case BO_LE: Op = BO_GT; break; |
Ted Kremenek | 4ee7c9c | 2011-08-16 03:44:38 +0000 | [diff] [blame] | 1044 | case BO_GE: Op = BO_LT; break; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 1045 | default: |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
Ted Kremenek | 6ae3257 | 2011-12-20 22:00:25 +0000 | [diff] [blame] | 1049 | switch (Op) { |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 1050 | case BO_EQ: |
| 1051 | Out << "equal to "; |
| 1052 | break; |
| 1053 | case BO_NE: |
| 1054 | Out << "not equal to "; |
| 1055 | break; |
| 1056 | default: |
| 1057 | Out << BinaryOperator::getOpcodeStr(Op) << ' '; |
| 1058 | break; |
| 1059 | } |
| 1060 | |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 1061 | Out << (shouldInvert ? LhsString : RhsString); |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 1062 | const LocationContext *LCtx = N->getLocationContext(); |
| 1063 | PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx); |
| 1064 | PathDiagnosticEventPiece *event = |
| 1065 | new PathDiagnosticEventPiece(Loc, Out.str()); |
| 1066 | if (shouldPrune.hasValue()) |
| 1067 | event->setPrunable(shouldPrune.getValue()); |
| 1068 | return event; |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 1069 | } |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 1070 | |
| 1071 | PathDiagnosticPiece * |
| 1072 | ConditionBRVisitor::VisitConditionVariable(StringRef LhsString, |
| 1073 | const Expr *CondVarExpr, |
| 1074 | const bool tookTrue, |
| 1075 | BugReporterContext &BRC, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 1076 | BugReport &report, |
| 1077 | const ExplodedNode *N) { |
Jordan Rose | b0e1bad | 2012-08-03 23:08:54 +0000 | [diff] [blame] | 1078 | // FIXME: If there's already a constraint tracker for this variable, |
| 1079 | // we shouldn't emit anything here (c.f. the double note in |
| 1080 | // test/Analysis/inlining/path-notes.c) |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1081 | SmallString<256> buf; |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 1082 | llvm::raw_svector_ostream Out(buf); |
| 1083 | Out << "Assuming " << LhsString << " is "; |
| 1084 | |
| 1085 | QualType Ty = CondVarExpr->getType(); |
| 1086 | |
| 1087 | if (Ty->isPointerType()) |
| 1088 | Out << (tookTrue ? "not null" : "null"); |
| 1089 | else if (Ty->isObjCObjectPointerType()) |
| 1090 | Out << (tookTrue ? "not nil" : "nil"); |
| 1091 | else if (Ty->isBooleanType()) |
| 1092 | Out << (tookTrue ? "true" : "false"); |
| 1093 | else if (Ty->isIntegerType()) |
| 1094 | Out << (tookTrue ? "non-zero" : "zero"); |
| 1095 | else |
| 1096 | return 0; |
| 1097 | |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 1098 | const LocationContext *LCtx = N->getLocationContext(); |
| 1099 | PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LCtx); |
| 1100 | PathDiagnosticEventPiece *event = |
| 1101 | new PathDiagnosticEventPiece(Loc, Out.str()); |
| 1102 | |
| 1103 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(CondVarExpr)) { |
| 1104 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 1105 | const ProgramState *state = N->getState().getPtr(); |
| 1106 | if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) { |
| 1107 | if (report.isInteresting(R)) |
| 1108 | event->setPrunable(false); |
| 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | return event; |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 1114 | } |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 1115 | |
| 1116 | PathDiagnosticPiece * |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 1117 | ConditionBRVisitor::VisitTrueTest(const Expr *Cond, |
| 1118 | const DeclRefExpr *DR, |
| 1119 | const bool tookTrue, |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 1120 | BugReporterContext &BRC, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 1121 | BugReport &report, |
| 1122 | const ExplodedNode *N) { |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 1123 | |
| 1124 | const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 1125 | if (!VD) |
| 1126 | return 0; |
| 1127 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1128 | SmallString<256> Buf; |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 1129 | llvm::raw_svector_ostream Out(Buf); |
| 1130 | |
| 1131 | Out << "Assuming '"; |
| 1132 | VD->getDeclName().printName(Out); |
| 1133 | Out << "' is "; |
| 1134 | |
| 1135 | QualType VDTy = VD->getType(); |
| 1136 | |
| 1137 | if (VDTy->isPointerType()) |
| 1138 | Out << (tookTrue ? "non-null" : "null"); |
| 1139 | else if (VDTy->isObjCObjectPointerType()) |
| 1140 | Out << (tookTrue ? "non-nil" : "nil"); |
| 1141 | else if (VDTy->isScalarType()) |
| 1142 | Out << (tookTrue ? "not equal to 0" : "0"); |
| 1143 | else |
| 1144 | return 0; |
| 1145 | |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 1146 | const LocationContext *LCtx = N->getLocationContext(); |
| 1147 | PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx); |
| 1148 | PathDiagnosticEventPiece *event = |
| 1149 | new PathDiagnosticEventPiece(Loc, Out.str()); |
| 1150 | |
| 1151 | const ProgramState *state = N->getState().getPtr(); |
| 1152 | if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) { |
| 1153 | if (report.isInteresting(R)) |
| 1154 | event->setPrunable(false); |
| 1155 | else { |
| 1156 | SVal V = state->getSVal(R); |
| 1157 | if (report.isInteresting(V)) |
| 1158 | event->setPrunable(false); |
| 1159 | } |
| 1160 | } |
| 1161 | return event; |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 1162 | } |
Ted Kremenek | 0cf3d47 | 2012-02-07 00:24:33 +0000 | [diff] [blame] | 1163 | |
Anna Zaks | 80de487 | 2012-08-29 21:22:37 +0000 | [diff] [blame] | 1164 | PathDiagnosticPiece * |
| 1165 | UndefOrNullArgVisitor::VisitNode(const ExplodedNode *N, |
| 1166 | const ExplodedNode *PrevN, |
| 1167 | BugReporterContext &BRC, |
| 1168 | BugReport &BR) { |
| 1169 | |
| 1170 | ProgramStateRef State = N->getState(); |
| 1171 | ProgramPoint ProgLoc = N->getLocation(); |
| 1172 | |
| 1173 | // We are only interested in visiting CallEnter nodes. |
| 1174 | CallEnter *CEnter = dyn_cast<CallEnter>(&ProgLoc); |
| 1175 | if (!CEnter) |
| 1176 | return 0; |
| 1177 | |
| 1178 | // Check if one of the arguments is the region the visitor is tracking. |
| 1179 | CallEventManager &CEMgr = BRC.getStateManager().getCallEventManager(); |
| 1180 | CallEventRef<> Call = CEMgr.getCaller(CEnter->getCalleeContext(), State); |
| 1181 | unsigned Idx = 0; |
| 1182 | for (CallEvent::param_iterator I = Call->param_begin(), |
| 1183 | E = Call->param_end(); I != E; ++I, ++Idx) { |
| 1184 | const MemRegion *ArgReg = Call->getArgSVal(Idx).getAsRegion(); |
| 1185 | |
Anna Zaks | 522fc21 | 2012-09-12 22:57:30 +0000 | [diff] [blame] | 1186 | // Are we tracking the argument or its subregion? |
| 1187 | if ( !ArgReg || (ArgReg != R && !R->isSubRegionOf(ArgReg->StripCasts()))) |
Anna Zaks | 28694c1 | 2012-08-29 23:23:39 +0000 | [diff] [blame] | 1188 | continue; |
Anna Zaks | 80de487 | 2012-08-29 21:22:37 +0000 | [diff] [blame] | 1189 | |
| 1190 | // Check the function parameter type. |
| 1191 | const ParmVarDecl *ParamDecl = *I; |
| 1192 | assert(ParamDecl && "Formal parameter has no decl?"); |
| 1193 | QualType T = ParamDecl->getType(); |
| 1194 | |
| 1195 | if (!(T->isAnyPointerType() || T->isReferenceType())) { |
| 1196 | // Function can only change the value passed in by address. |
Anna Zaks | 28694c1 | 2012-08-29 23:23:39 +0000 | [diff] [blame] | 1197 | continue; |
Anna Zaks | 80de487 | 2012-08-29 21:22:37 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | // If it is a const pointer value, the function does not intend to |
| 1201 | // change the value. |
| 1202 | if (T->getPointeeType().isConstQualified()) |
Anna Zaks | 28694c1 | 2012-08-29 23:23:39 +0000 | [diff] [blame] | 1203 | continue; |
Anna Zaks | 80de487 | 2012-08-29 21:22:37 +0000 | [diff] [blame] | 1204 | |
| 1205 | // Mark the call site (LocationContext) as interesting if the value of the |
| 1206 | // argument is undefined or '0'/'NULL'. |
Anna Zaks | 522fc21 | 2012-09-12 22:57:30 +0000 | [diff] [blame] | 1207 | SVal BoundVal = State->getSVal(R); |
Anna Zaks | 80de487 | 2012-08-29 21:22:37 +0000 | [diff] [blame] | 1208 | if (BoundVal.isUndef() || BoundVal.isZeroConstant()) { |
| 1209 | BR.markInteresting(CEnter->getCalleeContext()); |
| 1210 | return 0; |
| 1211 | } |
| 1212 | } |
| 1213 | return 0; |
| 1214 | } |