BugReporterVisitors.cpp 40 KB

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