1 //===- BugReporterVisitors.cpp - Helpers for reporting bugs ---------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines a set of BugReporter "visitors" which can be used to 10 // enhance the diagnostics reported for a bug. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/Decl.h" 17 #include "clang/AST/DeclBase.h" 18 #include "clang/AST/DeclCXX.h" 19 #include "clang/AST/Expr.h" 20 #include "clang/AST/ExprCXX.h" 21 #include "clang/AST/ExprObjC.h" 22 #include "clang/AST/Stmt.h" 23 #include "clang/AST/Type.h" 24 #include "clang/ASTMatchers/ASTMatchFinder.h" 25 #include "clang/Analysis/AnalysisDeclContext.h" 26 #include "clang/Analysis/CFG.h" 27 #include "clang/Analysis/CFGStmtMap.h" 28 #include "clang/Analysis/ProgramPoint.h" 29 #include "clang/Basic/IdentifierTable.h" 30 #include "clang/Basic/LLVM.h" 31 #include "clang/Basic/SourceLocation.h" 32 #include "clang/Basic/SourceManager.h" 33 #include "clang/Lex/Lexer.h" 34 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" 35 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" 36 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" 37 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" 38 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 39 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" 40 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 41 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" 42 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 43 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h" 44 #include "clang/StaticAnalyzer/Core/PathSensitive/SMTConv.h" 45 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h" 46 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" 47 #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h" 48 #include "llvm/ADT/ArrayRef.h" 49 #include "llvm/ADT/None.h" 50 #include "llvm/ADT/Optional.h" 51 #include "llvm/ADT/STLExtras.h" 52 #include "llvm/ADT/SmallPtrSet.h" 53 #include "llvm/ADT/SmallString.h" 54 #include "llvm/ADT/SmallVector.h" 55 #include "llvm/ADT/StringExtras.h" 56 #include "llvm/ADT/StringRef.h" 57 #include "llvm/Support/Casting.h" 58 #include "llvm/Support/ErrorHandling.h" 59 #include "llvm/Support/raw_ostream.h" 60 #include <cassert> 61 #include <deque> 62 #include <memory> 63 #include <string> 64 #include <utility> 65 66 using namespace clang; 67 using namespace ento; 68 69 //===----------------------------------------------------------------------===// 70 // Utility functions. 71 //===----------------------------------------------------------------------===// 72 73 static const Expr *peelOffPointerArithmetic(const BinaryOperator *B) { 74 if (B->isAdditiveOp() && B->getType()->isPointerType()) { 75 if (B->getLHS()->getType()->isPointerType()) { 76 return B->getLHS(); 77 } else if (B->getRHS()->getType()->isPointerType()) { 78 return B->getRHS(); 79 } 80 } 81 return nullptr; 82 } 83 84 /// Given that expression S represents a pointer that would be dereferenced, 85 /// try to find a sub-expression from which the pointer came from. 86 /// This is used for tracking down origins of a null or undefined value: 87 /// "this is null because that is null because that is null" etc. 88 /// We wipe away field and element offsets because they merely add offsets. 89 /// We also wipe away all casts except lvalue-to-rvalue casts, because the 90 /// latter represent an actual pointer dereference; however, we remove 91 /// the final lvalue-to-rvalue cast before returning from this function 92 /// because it demonstrates more clearly from where the pointer rvalue was 93 /// loaded. Examples: 94 /// x->y.z ==> x (lvalue) 95 /// foo()->y.z ==> foo() (rvalue) 96 const Expr *bugreporter::getDerefExpr(const Stmt *S) { 97 const auto *E = dyn_cast<Expr>(S); 98 if (!E) 99 return nullptr; 100 101 while (true) { 102 if (const auto *CE = dyn_cast<CastExpr>(E)) { 103 if (CE->getCastKind() == CK_LValueToRValue) { 104 // This cast represents the load we're looking for. 105 break; 106 } 107 E = CE->getSubExpr(); 108 } else if (const auto *B = dyn_cast<BinaryOperator>(E)) { 109 // Pointer arithmetic: '*(x + 2)' -> 'x') etc. 110 if (const Expr *Inner = peelOffPointerArithmetic(B)) { 111 E = Inner; 112 } else { 113 // Probably more arithmetic can be pattern-matched here, 114 // but for now give up. 115 break; 116 } 117 } else if (const auto *U = dyn_cast<UnaryOperator>(E)) { 118 if (U->getOpcode() == UO_Deref || U->getOpcode() == UO_AddrOf || 119 (U->isIncrementDecrementOp() && U->getType()->isPointerType())) { 120 // Operators '*' and '&' don't actually mean anything. 121 // We look at casts instead. 122 E = U->getSubExpr(); 123 } else { 124 // Probably more arithmetic can be pattern-matched here, 125 // but for now give up. 126 break; 127 } 128 } 129 // Pattern match for a few useful cases: a[0], p->f, *p etc. 130 else if (const auto *ME = dyn_cast<MemberExpr>(E)) { 131 E = ME->getBase(); 132 } else if (const auto *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) { 133 E = IvarRef->getBase(); 134 } else if (const auto *AE = dyn_cast<ArraySubscriptExpr>(E)) { 135 E = AE->getBase(); 136 } else if (const auto *PE = dyn_cast<ParenExpr>(E)) { 137 E = PE->getSubExpr(); 138 } else if (const auto *FE = dyn_cast<FullExpr>(E)) { 139 E = FE->getSubExpr(); 140 } else { 141 // Other arbitrary stuff. 142 break; 143 } 144 } 145 146 // Special case: remove the final lvalue-to-rvalue cast, but do not recurse 147 // deeper into the sub-expression. This way we return the lvalue from which 148 // our pointer rvalue was loaded. 149 if (const auto *CE = dyn_cast<ImplicitCastExpr>(E)) 150 if (CE->getCastKind() == CK_LValueToRValue) 151 E = CE->getSubExpr(); 152 153 return E; 154 } 155 156 /// Comparing internal representations of symbolic values (via 157 /// SVal::operator==()) is a valid way to check if the value was updated, 158 /// unless it's a LazyCompoundVal that may have a different internal 159 /// representation every time it is loaded from the state. In this function we 160 /// do an approximate comparison for lazy compound values, checking that they 161 /// are the immediate snapshots of the tracked region's bindings within the 162 /// node's respective states but not really checking that these snapshots 163 /// actually contain the same set of bindings. 164 static bool hasVisibleUpdate(const ExplodedNode *LeftNode, SVal LeftVal, 165 const ExplodedNode *RightNode, SVal RightVal) { 166 if (LeftVal == RightVal) 167 return true; 168 169 const auto LLCV = LeftVal.getAs<nonloc::LazyCompoundVal>(); 170 if (!LLCV) 171 return false; 172 173 const auto RLCV = RightVal.getAs<nonloc::LazyCompoundVal>(); 174 if (!RLCV) 175 return false; 176 177 return LLCV->getRegion() == RLCV->getRegion() && 178 LLCV->getStore() == LeftNode->getState()->getStore() && 179 RLCV->getStore() == RightNode->getState()->getStore(); 180 } 181 182 //===----------------------------------------------------------------------===// 183 // Definitions for bug reporter visitors. 184 //===----------------------------------------------------------------------===// 185 186 std::shared_ptr<PathDiagnosticPiece> 187 BugReporterVisitor::getEndPath(BugReporterContext &, 188 const ExplodedNode *, BugReport &) { 189 return nullptr; 190 } 191 192 void 193 BugReporterVisitor::finalizeVisitor(BugReporterContext &, 194 const ExplodedNode *, BugReport &) {} 195 196 std::shared_ptr<PathDiagnosticPiece> BugReporterVisitor::getDefaultEndPath( 197 BugReporterContext &BRC, const ExplodedNode *EndPathNode, BugReport &BR) { 198 PathDiagnosticLocation L = 199 PathDiagnosticLocation::createEndOfPath(EndPathNode,BRC.getSourceManager()); 200 201 const auto &Ranges = BR.getRanges(); 202 203 // Only add the statement itself as a range if we didn't specify any 204 // special ranges for this report. 205 auto P = std::make_shared<PathDiagnosticEventPiece>( 206 L, BR.getDescription(), Ranges.begin() == Ranges.end()); 207 for (SourceRange Range : Ranges) 208 P->addRange(Range); 209 210 return P; 211 } 212 213 /// \return name of the macro inside the location \p Loc. 214 static StringRef getMacroName(SourceLocation Loc, 215 BugReporterContext &BRC) { 216 return Lexer::getImmediateMacroName( 217 Loc, 218 BRC.getSourceManager(), 219 BRC.getASTContext().getLangOpts()); 220 } 221 222 /// \return Whether given spelling location corresponds to an expansion 223 /// of a function-like macro. 224 static bool isFunctionMacroExpansion(SourceLocation Loc, 225 const SourceManager &SM) { 226 if (!Loc.isMacroID()) 227 return false; 228 while (SM.isMacroArgExpansion(Loc)) 229 Loc = SM.getImmediateExpansionRange(Loc).getBegin(); 230 std::pair<FileID, unsigned> TLInfo = SM.getDecomposedLoc(Loc); 231 SrcMgr::SLocEntry SE = SM.getSLocEntry(TLInfo.first); 232 const SrcMgr::ExpansionInfo &EInfo = SE.getExpansion(); 233 return EInfo.isFunctionMacroExpansion(); 234 } 235 236 /// \return Whether \c RegionOfInterest was modified at \p N, 237 /// where \p ReturnState is a state associated with the return 238 /// from the current frame. 239 static bool wasRegionOfInterestModifiedAt( 240 const SubRegion *RegionOfInterest, 241 const ExplodedNode *N, 242 SVal ValueAfter) { 243 ProgramStateRef State = N->getState(); 244 ProgramStateManager &Mgr = N->getState()->getStateManager(); 245 246 if (!N->getLocationAs<PostStore>() 247 && !N->getLocationAs<PostInitializer>() 248 && !N->getLocationAs<PostStmt>()) 249 return false; 250 251 // Writing into region of interest. 252 if (auto PS = N->getLocationAs<PostStmt>()) 253 if (auto *BO = PS->getStmtAs<BinaryOperator>()) 254 if (BO->isAssignmentOp() && RegionOfInterest->isSubRegionOf( 255 N->getSVal(BO->getLHS()).getAsRegion())) 256 return true; 257 258 // SVal after the state is possibly different. 259 SVal ValueAtN = N->getState()->getSVal(RegionOfInterest); 260 if (!Mgr.getSValBuilder().areEqual(State, ValueAtN, ValueAfter).isConstrainedTrue() && 261 (!ValueAtN.isUndef() || !ValueAfter.isUndef())) 262 return true; 263 264 return false; 265 } 266 267 268 namespace { 269 270 /// Put a diagnostic on return statement of all inlined functions 271 /// for which the region of interest \p RegionOfInterest was passed into, 272 /// but not written inside, and it has caused an undefined read or a null 273 /// pointer dereference outside. 274 class NoStoreFuncVisitor final : public BugReporterVisitor { 275 const SubRegion *RegionOfInterest; 276 MemRegionManager &MmrMgr; 277 const SourceManager &SM; 278 const PrintingPolicy &PP; 279 280 /// Recursion limit for dereferencing fields when looking for the 281 /// region of interest. 282 /// The limit of two indicates that we will dereference fields only once. 283 static const unsigned DEREFERENCE_LIMIT = 2; 284 285 /// Frames writing into \c RegionOfInterest. 286 /// This visitor generates a note only if a function does not write into 287 /// a region of interest. This information is not immediately available 288 /// by looking at the node associated with the exit from the function 289 /// (usually the return statement). To avoid recomputing the same information 290 /// many times (going up the path for each node and checking whether the 291 /// region was written into) we instead lazily compute the 292 /// stack frames along the path which write into the region of interest. 293 llvm::SmallPtrSet<const StackFrameContext *, 32> FramesModifyingRegion; 294 llvm::SmallPtrSet<const StackFrameContext *, 32> FramesModifyingCalculated; 295 296 using RegionVector = SmallVector<const MemRegion *, 5>; 297 public: 298 NoStoreFuncVisitor(const SubRegion *R) 299 : RegionOfInterest(R), MmrMgr(*R->getMemRegionManager()), 300 SM(MmrMgr.getContext().getSourceManager()), 301 PP(MmrMgr.getContext().getPrintingPolicy()) {} 302 303 void Profile(llvm::FoldingSetNodeID &ID) const override { 304 static int Tag = 0; 305 ID.AddPointer(&Tag); 306 ID.AddPointer(RegionOfInterest); 307 } 308 309 void *getTag() const { 310 static int Tag = 0; 311 return static_cast<void *>(&Tag); 312 } 313 314 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N, 315 BugReporterContext &BR, 316 BugReport &R) override { 317 318 const LocationContext *Ctx = N->getLocationContext(); 319 const StackFrameContext *SCtx = Ctx->getStackFrame(); 320 ProgramStateRef State = N->getState(); 321 auto CallExitLoc = N->getLocationAs<CallExitBegin>(); 322 323 // No diagnostic if region was modified inside the frame. 324 if (!CallExitLoc || isRegionOfInterestModifiedInFrame(N)) 325 return nullptr; 326 327 CallEventRef<> Call = 328 BR.getStateManager().getCallEventManager().getCaller(SCtx, State); 329 330 // Region of interest corresponds to an IVar, exiting a method 331 // which could have written into that IVar, but did not. 332 if (const auto *MC = dyn_cast<ObjCMethodCall>(Call)) { 333 if (const auto *IvarR = dyn_cast<ObjCIvarRegion>(RegionOfInterest)) { 334 const MemRegion *SelfRegion = MC->getReceiverSVal().getAsRegion(); 335 if (RegionOfInterest->isSubRegionOf(SelfRegion) && 336 potentiallyWritesIntoIvar(Call->getRuntimeDefinition().getDecl(), 337 IvarR->getDecl())) 338 return maybeEmitNote(R, *Call, N, {}, SelfRegion, "self", 339 /*FirstIsReferenceType=*/false, 1); 340 } 341 } 342 343 if (const auto *CCall = dyn_cast<CXXConstructorCall>(Call)) { 344 const MemRegion *ThisR = CCall->getCXXThisVal().getAsRegion(); 345 if (RegionOfInterest->isSubRegionOf(ThisR) 346 && !CCall->getDecl()->isImplicit()) 347 return maybeEmitNote(R, *Call, N, {}, ThisR, "this", 348 /*FirstIsReferenceType=*/false, 1); 349 350 // Do not generate diagnostics for not modified parameters in 351 // constructors. 352 return nullptr; 353 } 354 355 ArrayRef<ParmVarDecl *> parameters = getCallParameters(Call); 356 for (unsigned I = 0; I < Call->getNumArgs() && I < parameters.size(); ++I) { 357 const ParmVarDecl *PVD = parameters[I]; 358 SVal V = Call->getArgSVal(I); 359 bool ParamIsReferenceType = PVD->getType()->isReferenceType(); 360 std::string ParamName = PVD->getNameAsString(); 361 362 int IndirectionLevel = 1; 363 QualType T = PVD->getType(); 364 while (const MemRegion *MR = V.getAsRegion()) { 365 if (RegionOfInterest->isSubRegionOf(MR) && !isPointerToConst(T)) 366 return maybeEmitNote(R, *Call, N, {}, MR, ParamName, 367 ParamIsReferenceType, IndirectionLevel); 368 369 QualType PT = T->getPointeeType(); 370 if (PT.isNull() || PT->isVoidType()) break; 371 372 if (const RecordDecl *RD = PT->getAsRecordDecl()) 373 if (auto P = findRegionOfInterestInRecord(RD, State, MR)) 374 return maybeEmitNote(R, *Call, N, *P, RegionOfInterest, ParamName, 375 ParamIsReferenceType, IndirectionLevel); 376 377 V = State->getSVal(MR, PT); 378 T = PT; 379 IndirectionLevel++; 380 } 381 } 382 383 return nullptr; 384 } 385 386 private: 387 /// Attempts to find the region of interest in a given CXX decl, 388 /// by either following the base classes or fields. 389 /// Dereferences fields up to a given recursion limit. 390 /// Note that \p Vec is passed by value, leading to quadratic copying cost, 391 /// but it's OK in practice since its length is limited to DEREFERENCE_LIMIT. 392 /// \return A chain fields leading to the region of interest or None. 393 const Optional<RegionVector> 394 findRegionOfInterestInRecord(const RecordDecl *RD, ProgramStateRef State, 395 const MemRegion *R, 396 const RegionVector &Vec = {}, 397 int depth = 0) { 398 399 if (depth == DEREFERENCE_LIMIT) // Limit the recursion depth. 400 return None; 401 402 if (const auto *RDX = dyn_cast<CXXRecordDecl>(RD)) 403 if (!RDX->hasDefinition()) 404 return None; 405 406 // Recursively examine the base classes. 407 // Note that following base classes does not increase the recursion depth. 408 if (const auto *RDX = dyn_cast<CXXRecordDecl>(RD)) 409 for (const auto II : RDX->bases()) 410 if (const RecordDecl *RRD = II.getType()->getAsRecordDecl()) 411 if (auto Out = findRegionOfInterestInRecord(RRD, State, R, Vec, depth)) 412 return Out; 413 414 for (const FieldDecl *I : RD->fields()) { 415 QualType FT = I->getType(); 416 const FieldRegion *FR = MmrMgr.getFieldRegion(I, cast<SubRegion>(R)); 417 const SVal V = State->getSVal(FR); 418 const MemRegion *VR = V.getAsRegion(); 419 420 RegionVector VecF = Vec; 421 VecF.push_back(FR); 422 423 if (RegionOfInterest == VR) 424 return VecF; 425 426 if (const RecordDecl *RRD = FT->getAsRecordDecl()) 427 if (auto Out = 428 findRegionOfInterestInRecord(RRD, State, FR, VecF, depth + 1)) 429 return Out; 430 431 QualType PT = FT->getPointeeType(); 432 if (PT.isNull() || PT->isVoidType() || !VR) continue; 433 434 if (const RecordDecl *RRD = PT->getAsRecordDecl()) 435 if (auto Out = 436 findRegionOfInterestInRecord(RRD, State, VR, VecF, depth + 1)) 437 return Out; 438 439 } 440 441 return None; 442 } 443 444 /// \return Whether the method declaration \p Parent 445 /// syntactically has a binary operation writing into the ivar \p Ivar. 446 bool potentiallyWritesIntoIvar(const Decl *Parent, 447 const ObjCIvarDecl *Ivar) { 448 using namespace ast_matchers; 449 const char * IvarBind = "Ivar"; 450 if (!Parent || !Parent->hasBody()) 451 return false; 452 StatementMatcher WriteIntoIvarM = binaryOperator( 453 hasOperatorName("="), 454 hasLHS(ignoringParenImpCasts( 455 objcIvarRefExpr(hasDeclaration(equalsNode(Ivar))).bind(IvarBind)))); 456 StatementMatcher ParentM = stmt(hasDescendant(WriteIntoIvarM)); 457 auto Matches = match(ParentM, *Parent->getBody(), Parent->getASTContext()); 458 for (BoundNodes &Match : Matches) { 459 auto IvarRef = Match.getNodeAs<ObjCIvarRefExpr>(IvarBind); 460 if (IvarRef->isFreeIvar()) 461 return true; 462 463 const Expr *Base = IvarRef->getBase(); 464 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(Base)) 465 Base = ICE->getSubExpr(); 466 467 if (const auto *DRE = dyn_cast<DeclRefExpr>(Base)) 468 if (const auto *ID = dyn_cast<ImplicitParamDecl>(DRE->getDecl())) 469 if (ID->getParameterKind() == ImplicitParamDecl::ObjCSelf) 470 return true; 471 472 return false; 473 } 474 return false; 475 } 476 477 /// Check and lazily calculate whether the region of interest is 478 /// modified in the stack frame to which \p N belongs. 479 /// The calculation is cached in FramesModifyingRegion. 480 bool isRegionOfInterestModifiedInFrame(const ExplodedNode *N) { 481 const LocationContext *Ctx = N->getLocationContext(); 482 const StackFrameContext *SCtx = Ctx->getStackFrame(); 483 if (!FramesModifyingCalculated.count(SCtx)) 484 findModifyingFrames(N); 485 return FramesModifyingRegion.count(SCtx); 486 } 487 488 489 /// Write to \c FramesModifyingRegion all stack frames along 490 /// the path in the current stack frame which modify \c RegionOfInterest. 491 void findModifyingFrames(const ExplodedNode *N) { 492 assert(N->getLocationAs<CallExitBegin>()); 493 ProgramStateRef LastReturnState = N->getState(); 494 SVal ValueAtReturn = LastReturnState->getSVal(RegionOfInterest); 495 const LocationContext *Ctx = N->getLocationContext(); 496 const StackFrameContext *OriginalSCtx = Ctx->getStackFrame(); 497 498 do { 499 ProgramStateRef State = N->getState(); 500 auto CallExitLoc = N->getLocationAs<CallExitBegin>(); 501 if (CallExitLoc) { 502 LastReturnState = State; 503 ValueAtReturn = LastReturnState->getSVal(RegionOfInterest); 504 } 505 506 FramesModifyingCalculated.insert( 507 N->getLocationContext()->getStackFrame()); 508 509 if (wasRegionOfInterestModifiedAt(RegionOfInterest, N, ValueAtReturn)) { 510 const StackFrameContext *SCtx = N->getStackFrame(); 511 while (!SCtx->inTopFrame()) { 512 auto p = FramesModifyingRegion.insert(SCtx); 513 if (!p.second) 514 break; // Frame and all its parents already inserted. 515 SCtx = SCtx->getParent()->getStackFrame(); 516 } 517 } 518 519 // Stop calculation at the call to the current function. 520 if (auto CE = N->getLocationAs<CallEnter>()) 521 if (CE->getCalleeContext() == OriginalSCtx) 522 break; 523 524 N = N->getFirstPred(); 525 } while (N); 526 } 527 528 /// Get parameters associated with runtime definition in order 529 /// to get the correct parameter name. 530 ArrayRef<ParmVarDecl *> getCallParameters(CallEventRef<> Call) { 531 // Use runtime definition, if available. 532 RuntimeDefinition RD = Call->getRuntimeDefinition(); 533 if (const auto *FD = dyn_cast_or_null<FunctionDecl>(RD.getDecl())) 534 return FD->parameters(); 535 if (const auto *MD = dyn_cast_or_null<ObjCMethodDecl>(RD.getDecl())) 536 return MD->parameters(); 537 538 return Call->parameters(); 539 } 540 541 /// \return whether \p Ty points to a const type, or is a const reference. 542 bool isPointerToConst(QualType Ty) { 543 return !Ty->getPointeeType().isNull() && 544 Ty->getPointeeType().getCanonicalType().isConstQualified(); 545 } 546 547 /// Consume the information on the no-store stack frame in order to 548 /// either emit a note or suppress the report enirely. 549 /// \return Diagnostics piece for region not modified in the current function, 550 /// if it decides to emit one. 551 std::shared_ptr<PathDiagnosticPiece> 552 maybeEmitNote(BugReport &R, const CallEvent &Call, const ExplodedNode *N, 553 const RegionVector &FieldChain, const MemRegion *MatchedRegion, 554 StringRef FirstElement, bool FirstIsReferenceType, 555 unsigned IndirectionLevel) { 556 // Optimistically suppress uninitialized value bugs that result 557 // from system headers having a chance to initialize the value 558 // but failing to do so. It's too unlikely a system header's fault. 559 // It's much more likely a situation in which the function has a failure 560 // mode that the user decided not to check. If we want to hunt such 561 // omitted checks, we should provide an explicit function-specific note 562 // describing the precondition under which the function isn't supposed to 563 // initialize its out-parameter, and additionally check that such 564 // precondition can actually be fulfilled on the current path. 565 if (Call.isInSystemHeader()) { 566 // We make an exception for system header functions that have no branches. 567 // Such functions unconditionally fail to initialize the variable. 568 // If they call other functions that have more paths within them, 569 // this suppression would still apply when we visit these inner functions. 570 // One common example of a standard function that doesn't ever initialize 571 // its out parameter is operator placement new; it's up to the follow-up 572 // constructor (if any) to initialize the memory. 573 if (!N->getStackFrame()->getCFG()->isLinear()) 574 R.markInvalid(getTag(), nullptr); 575 return nullptr; 576 } 577 578 PathDiagnosticLocation L = 579 PathDiagnosticLocation::create(N->getLocation(), SM); 580 581 // For now this shouldn't trigger, but once it does (as we add more 582 // functions to the body farm), we'll need to decide if these reports 583 // are worth suppressing as well. 584 if (!L.hasValidLocation()) 585 return nullptr; 586 587 SmallString<256> sbuf; 588 llvm::raw_svector_ostream os(sbuf); 589 os << "Returning without writing to '"; 590 591 // Do not generate the note if failed to pretty-print. 592 if (!prettyPrintRegionName(FirstElement, FirstIsReferenceType, 593 MatchedRegion, FieldChain, IndirectionLevel, os)) 594 return nullptr; 595 596 os << "'"; 597 return std::make_shared<PathDiagnosticEventPiece>(L, os.str()); 598 } 599 600 /// Pretty-print region \p MatchedRegion to \p os. 601 /// \return Whether printing succeeded. 602 bool prettyPrintRegionName(StringRef FirstElement, bool FirstIsReferenceType, 603 const MemRegion *MatchedRegion, 604 const RegionVector &FieldChain, 605 int IndirectionLevel, 606 llvm::raw_svector_ostream &os) { 607 608 if (FirstIsReferenceType) 609 IndirectionLevel--; 610 611 RegionVector RegionSequence; 612 613 // Add the regions in the reverse order, then reverse the resulting array. 614 assert(RegionOfInterest->isSubRegionOf(MatchedRegion)); 615 const MemRegion *R = RegionOfInterest; 616 while (R != MatchedRegion) { 617 RegionSequence.push_back(R); 618 R = cast<SubRegion>(R)->getSuperRegion(); 619 } 620 std::reverse(RegionSequence.begin(), RegionSequence.end()); 621 RegionSequence.append(FieldChain.begin(), FieldChain.end()); 622 623 StringRef Sep; 624 for (const MemRegion *R : RegionSequence) { 625 626 // Just keep going up to the base region. 627 // Element regions may appear due to casts. 628 if (isa<CXXBaseObjectRegion>(R) || isa<CXXTempObjectRegion>(R)) 629 continue; 630 631 if (Sep.empty()) 632 Sep = prettyPrintFirstElement(FirstElement, 633 /*MoreItemsExpected=*/true, 634 IndirectionLevel, os); 635 636 os << Sep; 637 638 // Can only reasonably pretty-print DeclRegions. 639 if (!isa<DeclRegion>(R)) 640 return false; 641 642 const auto *DR = cast<DeclRegion>(R); 643 Sep = DR->getValueType()->isAnyPointerType() ? "->" : "."; 644 DR->getDecl()->getDeclName().print(os, PP); 645 } 646 647 if (Sep.empty()) 648 prettyPrintFirstElement(FirstElement, 649 /*MoreItemsExpected=*/false, IndirectionLevel, 650 os); 651 return true; 652 } 653 654 /// Print first item in the chain, return new separator. 655 StringRef prettyPrintFirstElement(StringRef FirstElement, 656 bool MoreItemsExpected, 657 int IndirectionLevel, 658 llvm::raw_svector_ostream &os) { 659 StringRef Out = "."; 660 661 if (IndirectionLevel > 0 && MoreItemsExpected) { 662 IndirectionLevel--; 663 Out = "->"; 664 } 665 666 if (IndirectionLevel > 0 && MoreItemsExpected) 667 os << "("; 668 669 for (int i=0; i<IndirectionLevel; i++) 670 os << "*"; 671 os << FirstElement; 672 673 if (IndirectionLevel > 0 && MoreItemsExpected) 674 os << ")"; 675 676 return Out; 677 } 678 }; 679 680 /// Suppress null-pointer-dereference bugs where dereferenced null was returned 681 /// the macro. 682 class MacroNullReturnSuppressionVisitor final : public BugReporterVisitor { 683 const SubRegion *RegionOfInterest; 684 const SVal ValueAtDereference; 685 686 // Do not invalidate the reports where the value was modified 687 // after it got assigned to from the macro. 688 bool WasModified = false; 689 690 public: 691 MacroNullReturnSuppressionVisitor(const SubRegion *R, 692 const SVal V) : RegionOfInterest(R), 693 ValueAtDereference(V) {} 694 695 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N, 696 BugReporterContext &BRC, 697 BugReport &BR) override { 698 if (WasModified) 699 return nullptr; 700 701 auto BugPoint = BR.getErrorNode()->getLocation().getAs<StmtPoint>(); 702 if (!BugPoint) 703 return nullptr; 704 705 const SourceManager &SMgr = BRC.getSourceManager(); 706 if (auto Loc = matchAssignment(N)) { 707 if (isFunctionMacroExpansion(*Loc, SMgr)) { 708 std::string MacroName = getMacroName(*Loc, BRC); 709 SourceLocation BugLoc = BugPoint->getStmt()->getBeginLoc(); 710 if (!BugLoc.isMacroID() || getMacroName(BugLoc, BRC) != MacroName) 711 BR.markInvalid(getTag(), MacroName.c_str()); 712 } 713 } 714 715 if (wasRegionOfInterestModifiedAt(RegionOfInterest, N, ValueAtDereference)) 716 WasModified = true; 717 718 return nullptr; 719 } 720 721 static void addMacroVisitorIfNecessary( 722 const ExplodedNode *N, const MemRegion *R, 723 bool EnableNullFPSuppression, BugReport &BR, 724 const SVal V) { 725 AnalyzerOptions &Options = N->getState()->getAnalysisManager().options; 726 if (EnableNullFPSuppression && 727 Options.ShouldSuppressNullReturnPaths && V.getAs<Loc>()) 728 BR.addVisitor(llvm::make_unique<MacroNullReturnSuppressionVisitor>( 729 R->getAs<SubRegion>(), V)); 730 } 731 732 void* getTag() const { 733 static int Tag = 0; 734 return static_cast<void *>(&Tag); 735 } 736 737 void Profile(llvm::FoldingSetNodeID &ID) const override { 738 ID.AddPointer(getTag()); 739 } 740 741 private: 742 /// \return Source location of right hand side of an assignment 743 /// into \c RegionOfInterest, empty optional if none found. 744 Optional<SourceLocation> matchAssignment(const ExplodedNode *N) { 745 const Stmt *S = PathDiagnosticLocation::getStmt(N); 746 ProgramStateRef State = N->getState(); 747 auto *LCtx = N->getLocationContext(); 748 if (!S) 749 return None; 750 751 if (const auto *DS = dyn_cast<DeclStmt>(S)) { 752 if (const auto *VD = dyn_cast<VarDecl>(DS->getSingleDecl())) 753 if (const Expr *RHS = VD->getInit()) 754 if (RegionOfInterest->isSubRegionOf( 755 State->getLValue(VD, LCtx).getAsRegion())) 756 return RHS->getBeginLoc(); 757 } else if (const auto *BO = dyn_cast<BinaryOperator>(S)) { 758 const MemRegion *R = N->getSVal(BO->getLHS()).getAsRegion(); 759 const Expr *RHS = BO->getRHS(); 760 if (BO->isAssignmentOp() && RegionOfInterest->isSubRegionOf(R)) { 761 return RHS->getBeginLoc(); 762 } 763 } 764 return None; 765 } 766 }; 767 768 /// Emits an extra note at the return statement of an interesting stack frame. 769 /// 770 /// The returned value is marked as an interesting value, and if it's null, 771 /// adds a visitor to track where it became null. 772 /// 773 /// This visitor is intended to be used when another visitor discovers that an 774 /// interesting value comes from an inlined function call. 775 class ReturnVisitor : public BugReporterVisitor { 776 const StackFrameContext *StackFrame; 777 enum { 778 Initial, 779 MaybeUnsuppress, 780 Satisfied 781 } Mode = Initial; 782 783 bool EnableNullFPSuppression; 784 bool ShouldInvalidate = true; 785 AnalyzerOptions& Options; 786 787 public: 788 ReturnVisitor(const StackFrameContext *Frame, 789 bool Suppressed, 790 AnalyzerOptions &Options) 791 : StackFrame(Frame), EnableNullFPSuppression(Suppressed), 792 Options(Options) {} 793 794 static void *getTag() { 795 static int Tag = 0; 796 return static_cast<void *>(&Tag); 797 } 798 799 void Profile(llvm::FoldingSetNodeID &ID) const override { 800 ID.AddPointer(ReturnVisitor::getTag()); 801 ID.AddPointer(StackFrame); 802 ID.AddBoolean(EnableNullFPSuppression); 803 } 804 805 /// Adds a ReturnVisitor if the given statement represents a call that was 806 /// inlined. 807 /// 808 /// This will search back through the ExplodedGraph, starting from the given 809 /// node, looking for when the given statement was processed. If it turns out 810 /// the statement is a call that was inlined, we add the visitor to the 811 /// bug report, so it can print a note later. 812 static void addVisitorIfNecessary(const ExplodedNode *Node, const Stmt *S, 813 BugReport &BR, 814 bool InEnableNullFPSuppression) { 815 if (!CallEvent::isCallStmt(S)) 816 return; 817 818 // First, find when we processed the statement. 819 do { 820 if (auto CEE = Node->getLocationAs<CallExitEnd>()) 821 if (CEE->getCalleeContext()->getCallSite() == S) 822 break; 823 if (auto SP = Node->getLocationAs<StmtPoint>()) 824 if (SP->getStmt() == S) 825 break; 826 827 Node = Node->getFirstPred(); 828 } while (Node); 829 830 // Next, step over any post-statement checks. 831 while (Node && Node->getLocation().getAs<PostStmt>()) 832 Node = Node->getFirstPred(); 833 if (!Node) 834 return; 835 836 // Finally, see if we inlined the call. 837 Optional<CallExitEnd> CEE = Node->getLocationAs<CallExitEnd>(); 838 if (!CEE) 839 return; 840 841 const StackFrameContext *CalleeContext = CEE->getCalleeContext(); 842 if (CalleeContext->getCallSite() != S) 843 return; 844 845 // Check the return value. 846 ProgramStateRef State = Node->getState(); 847 SVal RetVal = Node->getSVal(S); 848 849 // Handle cases where a reference is returned and then immediately used. 850 if (cast<Expr>(S)->isGLValue()) 851 if (Optional<Loc> LValue = RetVal.getAs<Loc>()) 852 RetVal = State->getSVal(*LValue); 853 854 // See if the return value is NULL. If so, suppress the report. 855 AnalyzerOptions &Options = State->getAnalysisManager().options; 856 857 bool EnableNullFPSuppression = false; 858 if (InEnableNullFPSuppression && 859 Options.ShouldSuppressNullReturnPaths) 860 if (Optional<Loc> RetLoc = RetVal.getAs<Loc>()) 861 EnableNullFPSuppression = State->isNull(*RetLoc).isConstrainedTrue(); 862 863 BR.markInteresting(CalleeContext); 864 BR.addVisitor(llvm::make_unique<ReturnVisitor>(CalleeContext, 865 EnableNullFPSuppression, 866 Options)); 867 } 868 869 std::shared_ptr<PathDiagnosticPiece> 870 visitNodeInitial(const ExplodedNode *N, 871 BugReporterContext &BRC, BugReport &BR) { 872 // Only print a message at the interesting return statement. 873 if (N->getLocationContext() != StackFrame) 874 return nullptr; 875 876 Optional<StmtPoint> SP = N->getLocationAs<StmtPoint>(); 877 if (!SP) 878 return nullptr; 879 880 const auto *Ret = dyn_cast<ReturnStmt>(SP->getStmt()); 881 if (!Ret) 882 return nullptr; 883 884 // Okay, we're at the right return statement, but do we have the return 885 // value available? 886 ProgramStateRef State = N->getState(); 887 SVal V = State->getSVal(Ret, StackFrame); 888 if (V.isUnknownOrUndef()) 889 return nullptr; 890 891 // Don't print any more notes after this one. 892 Mode = Satisfied; 893 894 const Expr *RetE = Ret->getRetValue(); 895 assert(RetE && "Tracking a return value for a void function"); 896 897 // Handle cases where a reference is returned and then immediately used. 898 Optional<Loc> LValue; 899 if (RetE->isGLValue()) { 900 if ((LValue = V.getAs<Loc>())) { 901 SVal RValue = State->getRawSVal(*LValue, RetE->getType()); 902 if (RValue.getAs<DefinedSVal>()) 903 V = RValue; 904 } 905 } 906 907 // Ignore aggregate rvalues. 908 if (V.getAs<nonloc::LazyCompoundVal>() || 909 V.getAs<nonloc::CompoundVal>()) 910 return nullptr; 911 912 RetE = RetE->IgnoreParenCasts(); 913 914 // If we're returning 0, we should track where that 0 came from. 915 bugreporter::trackExpressionValue(N, RetE, BR, EnableNullFPSuppression); 916 917 // Build an appropriate message based on the return value. 918 SmallString<64> Msg; 919 llvm::raw_svector_ostream Out(Msg); 920 921 if (State->isNull(V).isConstrainedTrue()) { 922 if (V.getAs<Loc>()) { 923 924 // If we have counter-suppression enabled, make sure we keep visiting 925 // future nodes. We want to emit a path note as well, in case 926 // the report is resurrected as valid later on. 927 if (EnableNullFPSuppression && 928 Options.ShouldAvoidSuppressingNullArgumentPaths) 929 Mode = MaybeUnsuppress; 930 931 if (RetE->getType()->isObjCObjectPointerType()) { 932 Out << "Returning nil"; 933 } else { 934 Out << "Returning null pointer"; 935 } 936 } else { 937 Out << "Returning zero"; 938 } 939 940 } else { 941 if (auto CI = V.getAs<nonloc::ConcreteInt>()) { 942 Out << "Returning the value " << CI->getValue(); 943 } else if (V.getAs<Loc>()) { 944 Out << "Returning pointer"; 945 } else { 946 Out << "Returning value"; 947 } 948 } 949 950 if (LValue) { 951 if (const MemRegion *MR = LValue->getAsRegion()) { 952 if (MR->canPrintPretty()) { 953 Out << " (reference to "; 954 MR->printPretty(Out); 955 Out << ")"; 956 } 957 } 958 } else { 959 // FIXME: We should have a more generalized location printing mechanism. 960 if (const auto *DR = dyn_cast<DeclRefExpr>(RetE)) 961 if (const auto *DD = dyn_cast<DeclaratorDecl>(DR->getDecl())) 962 Out << " (loaded from '" << *DD << "')"; 963 } 964 965 PathDiagnosticLocation L(Ret, BRC.getSourceManager(), StackFrame); 966 if (!L.isValid() || !L.asLocation().isValid()) 967 return nullptr; 968 969 return std::make_shared<PathDiagnosticEventPiece>(L, Out.str()); 970 } 971 972 std::shared_ptr<PathDiagnosticPiece> 973 visitNodeMaybeUnsuppress(const ExplodedNode *N, 974 BugReporterContext &BRC, BugReport &BR) { 975 #ifndef NDEBUG 976 assert(Options.ShouldAvoidSuppressingNullArgumentPaths); 977 #endif 978 979 // Are we at the entry node for this call? 980 Optional<CallEnter> CE = N->getLocationAs<CallEnter>(); 981 if (!CE) 982 return nullptr; 983 984 if (CE->getCalleeContext() != StackFrame) 985 return nullptr; 986 987 Mode = Satisfied; 988 989 // Don't automatically suppress a report if one of the arguments is 990 // known to be a null pointer. Instead, start tracking /that/ null 991 // value back to its origin. 992 ProgramStateManager &StateMgr = BRC.getStateManager(); 993 CallEventManager &CallMgr = StateMgr.getCallEventManager(); 994 995 ProgramStateRef State = N->getState(); 996 CallEventRef<> Call = CallMgr.getCaller(StackFrame, State); 997 for (unsigned I = 0, E = Call->getNumArgs(); I != E; ++I) { 998 Optional<Loc> ArgV = Call->getArgSVal(I).getAs<Loc>(); 999 if (!ArgV) 1000 continue; 1001 1002 const Expr *ArgE = Call->getArgExpr(I); 1003 if (!ArgE) 1004 continue; 1005 1006 // Is it possible for this argument to be non-null? 1007 if (!State->isNull(*ArgV).isConstrainedTrue()) 1008 continue; 1009 1010 if (bugreporter::trackExpressionValue(N, ArgE, BR, EnableNullFPSuppression)) 1011 ShouldInvalidate = false; 1012 1013 // If we /can't/ track the null pointer, we should err on the side of 1014 // false negatives, and continue towards marking this report invalid. 1015 // (We will still look at the other arguments, though.) 1016 } 1017 1018 return nullptr; 1019 } 1020 1021 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N, 1022 BugReporterContext &BRC, 1023 BugReport &BR) override { 1024 switch (Mode) { 1025 case Initial: 1026 return visitNodeInitial(N, BRC, BR); 1027 case MaybeUnsuppress: 1028 return visitNodeMaybeUnsuppress(N, BRC, BR); 1029 case Satisfied: 1030 return nullptr; 1031 } 1032 1033 llvm_unreachable("Invalid visit mode!"); 1034 } 1035 1036 void finalizeVisitor(BugReporterContext &, const ExplodedNode *, 1037 BugReport &BR) override { 1038 if (EnableNullFPSuppression && ShouldInvalidate) 1039 BR.markInvalid(ReturnVisitor::getTag(), StackFrame); 1040 } 1041 }; 1042 1043 } // namespace 1044 1045 void FindLastStoreBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const { 1046 static int tag = 0; 1047 ID.AddPointer(&tag); 1048 ID.AddPointer(R); 1049 ID.Add(V); 1050 ID.AddBoolean(EnableNullFPSuppression); 1051 } 1052 1053 /// Returns true if \p N represents the DeclStmt declaring and initializing 1054 /// \p VR. 1055 static bool isInitializationOfVar(const ExplodedNode *N, const VarRegion *VR) { 1056 Optional<PostStmt> P = N->getLocationAs<PostStmt>(); 1057 if (!P) 1058 return false; 1059 1060 const DeclStmt *DS = P->getStmtAs<DeclStmt>(); 1061 if (!DS) 1062 return false; 1063 1064 if (DS->getSingleDecl() != VR->getDecl()) 1065 return false; 1066 1067 const MemSpaceRegion *VarSpace = VR->getMemorySpace(); 1068 const auto *FrameSpace = dyn_cast<StackSpaceRegion>(VarSpace); 1069 if (!FrameSpace) { 1070 // If we ever directly evaluate global DeclStmts, this assertion will be 1071 // invalid, but this still seems preferable to silently accepting an 1072 // initialization that may be for a path-sensitive variable. 1073 assert(VR->getDecl()->isStaticLocal() && "non-static stackless VarRegion"); 1074 return true; 1075 } 1076 1077 assert(VR->getDecl()->hasLocalStorage()); 1078 const LocationContext *LCtx = N->getLocationContext(); 1079 return FrameSpace->getStackFrame() == LCtx->getStackFrame(); 1080 } 1081 1082 /// Show diagnostics for initializing or declaring a region \p R with a bad value. 1083 static void showBRDiagnostics(const char *action, llvm::raw_svector_ostream &os, 1084 const MemRegion *R, SVal V, const DeclStmt *DS) { 1085 if (R->canPrintPretty()) { 1086 R->printPretty(os); 1087 os << " "; 1088 } 1089 1090 if (V.getAs<loc::ConcreteInt>()) { 1091 bool b = false; 1092 if (R->isBoundable()) { 1093 if (const auto *TR = dyn_cast<TypedValueRegion>(R)) { 1094 if (TR->getValueType()->isObjCObjectPointerType()) { 1095 os << action << "nil"; 1096 b = true; 1097 } 1098 } 1099 } 1100 if (!b) 1101 os << action << "a null pointer value"; 1102 1103 } else if (auto CVal = V.getAs<nonloc::ConcreteInt>()) { 1104 os << action << CVal->getValue(); 1105 } else if (DS) { 1106 if (V.isUndef()) { 1107 if (isa<VarRegion>(R)) { 1108 const auto *VD = cast<VarDecl>(DS->getSingleDecl()); 1109 if (VD->getInit()) { 1110 os << (R->canPrintPretty() ? "initialized" : "Initializing") 1111 << " to a garbage value"; 1112 } else { 1113 os << (R->canPrintPretty() ? "declared" : "Declaring") 1114 << " without an initial value"; 1115 } 1116 } 1117 } else { 1118 os << (R->canPrintPretty() ? "initialized" : "Initialized") 1119 << " here"; 1120 } 1121 } 1122 } 1123 1124 /// Display diagnostics for passing bad region as a parameter. 1125 static void showBRParamDiagnostics(llvm::raw_svector_ostream& os, 1126 const VarRegion *VR, 1127 SVal V) { 1128 const auto *Param = cast<ParmVarDecl>(VR->getDecl()); 1129 1130 os << "Passing "; 1131 1132 if (V.getAs<loc::ConcreteInt>()) { 1133 if (Param->getType()->isObjCObjectPointerType()) 1134 os << "nil object reference"; 1135 else 1136 os << "null pointer value"; 1137 } else if (V.isUndef()) { 1138 os << "uninitialized value"; 1139 } else if (auto CI = V.getAs<nonloc::ConcreteInt>()) { 1140 os << "the value " << CI->getValue(); 1141 } else { 1142 os << "value"; 1143 } 1144 1145 // Printed parameter indexes are 1-based, not 0-based. 1146 unsigned Idx = Param->getFunctionScopeIndex() + 1; 1147 os << " via " << Idx << llvm::getOrdinalSuffix(Idx) << " parameter"; 1148 if (VR->canPrintPretty()) { 1149 os << " "; 1150 VR->printPretty(os); 1151 } 1152 } 1153 1154 /// Show default diagnostics for storing bad region. 1155 static void showBRDefaultDiagnostics(llvm::raw_svector_ostream& os, 1156 const MemRegion *R, 1157 SVal V) { 1158 if (V.getAs<loc::ConcreteInt>()) { 1159 bool b = false; 1160 if (R->isBoundable()) { 1161 if (const auto *TR = dyn_cast<TypedValueRegion>(R)) { 1162 if (TR->getValueType()->isObjCObjectPointerType()) { 1163 os << "nil object reference stored"; 1164 b = true; 1165 } 1166 } 1167 } 1168 if (!b) { 1169 if (R->canPrintPretty()) 1170 os << "Null pointer value stored"; 1171 else 1172 os << "Storing null pointer value"; 1173 } 1174 1175 } else if (V.isUndef()) { 1176 if (R->canPrintPretty()) 1177 os << "Uninitialized value stored"; 1178 else 1179 os << "Storing uninitialized value"; 1180 1181 } else if (auto CV = V.getAs<nonloc::ConcreteInt>()) { 1182 if (R->canPrintPretty()) 1183 os << "The value " << CV->getValue() << " is assigned"; 1184 else 1185 os << "Assigning " << CV->getValue(); 1186 1187 } else { 1188 if (R->canPrintPretty()) 1189 os << "Value assigned"; 1190 else 1191 os << "Assigning value"; 1192 } 1193 1194 if (R->canPrintPretty()) { 1195 os << " to "; 1196 R->printPretty(os); 1197 } 1198 } 1199 1200 std::shared_ptr<PathDiagnosticPiece> 1201 FindLastStoreBRVisitor::VisitNode(const ExplodedNode *Succ, 1202 BugReporterContext &BRC, BugReport &BR) { 1203 if (Satisfied) 1204 return nullptr; 1205 1206 const ExplodedNode *StoreSite = nullptr; 1207 const ExplodedNode *Pred = Succ->getFirstPred(); 1208 const Expr *InitE = nullptr; 1209 bool IsParam = false; 1210 1211 // First see if we reached the declaration of the region. 1212 if (const auto *VR = dyn_cast<VarRegion>(R)) { 1213 if (isInitializationOfVar(Pred, VR)) { 1214 StoreSite = Pred; 1215 InitE = VR->getDecl()->getInit(); 1216 } 1217 } 1218 1219 // If this is a post initializer expression, initializing the region, we 1220 // should track the initializer expression. 1221 if (Optional<PostInitializer> PIP = Pred->getLocationAs<PostInitializer>()) { 1222 const MemRegion *FieldReg = (const MemRegion *)PIP->getLocationValue(); 1223 if (FieldReg && FieldReg == R) { 1224 StoreSite = Pred; 1225 InitE = PIP->getInitializer()->getInit(); 1226 } 1227 } 1228 1229 // Otherwise, see if this is the store site: 1230 // (1) Succ has this binding and Pred does not, i.e. this is 1231 // where the binding first occurred. 1232 // (2) Succ has this binding and is a PostStore node for this region, i.e. 1233 // the same binding was re-assigned here. 1234 if (!StoreSite) { 1235 if (Succ->getState()->getSVal(R) != V) 1236 return nullptr; 1237 1238 if (hasVisibleUpdate(Pred, Pred->getState()->getSVal(R), Succ, V)) { 1239 Optional<PostStore> PS = Succ->getLocationAs<PostStore>(); 1240 if (!PS || PS->getLocationValue() != R) 1241 return nullptr; 1242 } 1243 1244 StoreSite = Succ; 1245 1246 // If this is an assignment expression, we can track the value 1247 // being assigned. 1248 if (Optional<PostStmt> P = Succ->getLocationAs<PostStmt>()) 1249 if (const BinaryOperator *BO = P->getStmtAs<BinaryOperator>()) 1250 if (BO->isAssignmentOp()) 1251 InitE = BO->getRHS(); 1252 1253 // If this is a call entry, the variable should be a parameter. 1254 // FIXME: Handle CXXThisRegion as well. (This is not a priority because 1255 // 'this' should never be NULL, but this visitor isn't just for NULL and 1256 // UndefinedVal.) 1257 if (Optional<CallEnter> CE = Succ->getLocationAs<CallEnter>()) { 1258 if (const auto *VR = dyn_cast<VarRegion>(R)) { 1259 1260 const auto *Param = cast<ParmVarDecl>(VR->getDecl()); 1261 1262 ProgramStateManager &StateMgr = BRC.getStateManager(); 1263 CallEventManager &CallMgr = StateMgr.getCallEventManager(); 1264 1265 CallEventRef<> Call = CallMgr.getCaller(CE->getCalleeContext(), 1266 Succ->getState()); 1267 InitE = Call->getArgExpr(Param->getFunctionScopeIndex()); 1268 IsParam = true; 1269 } 1270 } 1271 1272 // If this is a CXXTempObjectRegion, the Expr responsible for its creation 1273 // is wrapped inside of it. 1274 if (const auto *TmpR = dyn_cast<CXXTempObjectRegion>(R)) 1275 InitE = TmpR->getExpr(); 1276 } 1277 1278 if (!StoreSite) 1279 return nullptr; 1280 Satisfied = true; 1281 1282 // If we have an expression that provided the value, try to track where it 1283 // came from. 1284 if (InitE) { 1285 if (V.isUndef() || 1286 V.getAs<loc::ConcreteInt>() || V.getAs<nonloc::ConcreteInt>()) { 1287 if (!IsParam) 1288 InitE = InitE->IgnoreParenCasts(); 1289 bugreporter::trackExpressionValue(StoreSite, InitE, BR, 1290 EnableNullFPSuppression); 1291 } 1292 ReturnVisitor::addVisitorIfNecessary(StoreSite, InitE->IgnoreParenCasts(), 1293 BR, EnableNullFPSuppression); 1294 } 1295 1296 // Okay, we've found the binding. Emit an appropriate message. 1297 SmallString<256> sbuf; 1298 llvm::raw_svector_ostream os(sbuf); 1299 1300 if (Optional<PostStmt> PS = StoreSite->getLocationAs<PostStmt>()) { 1301 const Stmt *S = PS->getStmt(); 1302 const char *action = nullptr; 1303 const auto *DS = dyn_cast<DeclStmt>(S); 1304 const auto *VR = dyn_cast<VarRegion>(R); 1305 1306 if (DS) { 1307 action = R->canPrintPretty() ? "initialized to " : 1308 "Initializing to "; 1309 } else if (isa<BlockExpr>(S)) { 1310 action = R->canPrintPretty() ? "captured by block as " : 1311 "Captured by block as "; 1312 if (VR) { 1313 // See if we can get the BlockVarRegion. 1314 ProgramStateRef State = StoreSite->getState(); 1315 SVal V = StoreSite->getSVal(S); 1316 if (const auto *BDR = 1317 dyn_cast_or_null<BlockDataRegion>(V.getAsRegion())) { 1318 if (const VarRegion *OriginalR = BDR->getOriginalRegion(VR)) { 1319 if (auto KV = State->getSVal(OriginalR).getAs<KnownSVal>()) 1320 BR.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>( 1321 *KV, OriginalR, EnableNullFPSuppression)); 1322 } 1323 } 1324 } 1325 } 1326 if (action) 1327 showBRDiagnostics(action, os, R, V, DS); 1328 1329 } else if (StoreSite->getLocation().getAs<CallEnter>()) { 1330 if (const auto *VR = dyn_cast<VarRegion>(R)) 1331 showBRParamDiagnostics(os, VR, V); 1332 } 1333 1334 if (os.str().empty()) 1335 showBRDefaultDiagnostics(os, R, V); 1336 1337 // Construct a new PathDiagnosticPiece. 1338 ProgramPoint P = StoreSite->getLocation(); 1339 PathDiagnosticLocation L; 1340 if (P.getAs<CallEnter>() && InitE) 1341 L = PathDiagnosticLocation(InitE, BRC.getSourceManager(), 1342 P.getLocationContext()); 1343 1344 if (!L.isValid() || !L.asLocation().isValid()) 1345 L = PathDiagnosticLocation::create(P, BRC.getSourceManager()); 1346 1347 if (!L.isValid() || !L.asLocation().isValid()) 1348 return nullptr; 1349 1350 return std::make_shared<PathDiagnosticEventPiece>(L, os.str()); 1351 } 1352 1353 void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const { 1354 static int tag = 0; 1355 ID.AddPointer(&tag); 1356 ID.AddBoolean(Assumption); 1357 ID.Add(Constraint); 1358 } 1359 1360 /// Return the tag associated with this visitor. This tag will be used 1361 /// to make all PathDiagnosticPieces created by this visitor. 1362 const char *TrackConstraintBRVisitor::getTag() { 1363 return "TrackConstraintBRVisitor"; 1364 } 1365 1366 bool TrackConstraintBRVisitor::isUnderconstrained(const ExplodedNode *N) const { 1367 if (IsZeroCheck) 1368 return N->getState()->isNull(Constraint).isUnderconstrained(); 1369 return (bool)N->getState()->assume(Constraint, !Assumption); 1370 } 1371 1372 std::shared_ptr<PathDiagnosticPiece> 1373 TrackConstraintBRVisitor::VisitNode(const ExplodedNode *N, 1374 BugReporterContext &BRC, BugReport &) { 1375 const ExplodedNode *PrevN = N->getFirstPred(); 1376 if (IsSatisfied) 1377 return nullptr; 1378 1379 // Start tracking after we see the first state in which the value is 1380 // constrained. 1381 if (!IsTrackingTurnedOn) 1382 if (!isUnderconstrained(N)) 1383 IsTrackingTurnedOn = true; 1384 if (!IsTrackingTurnedOn) 1385 return nullptr; 1386 1387 // Check if in the previous state it was feasible for this constraint 1388 // to *not* be true. 1389 if (isUnderconstrained(PrevN)) { 1390 IsSatisfied = true; 1391 1392 // As a sanity check, make sure that the negation of the constraint 1393 // was infeasible in the current state. If it is feasible, we somehow 1394 // missed the transition point. 1395 assert(!isUnderconstrained(N)); 1396 1397 // We found the transition point for the constraint. We now need to 1398 // pretty-print the constraint. (work-in-progress) 1399 SmallString<64> sbuf; 1400 llvm::raw_svector_ostream os(sbuf); 1401 1402 if (Constraint.getAs<Loc>()) { 1403 os << "Assuming pointer value is "; 1404 os << (Assumption ? "non-null" : "null"); 1405 } 1406 1407 if (os.str().empty()) 1408 return nullptr; 1409 1410 // Construct a new PathDiagnosticPiece. 1411 ProgramPoint P = N->getLocation(); 1412 PathDiagnosticLocation L = 1413 PathDiagnosticLocation::create(P, BRC.getSourceManager()); 1414 if (!L.isValid()) 1415 return nullptr; 1416 1417 auto X = std::make_shared<PathDiagnosticEventPiece>(L, os.str()); 1418 X->setTag(getTag()); 1419 return std::move(X); 1420 } 1421 1422 return nullptr; 1423 } 1424 1425 SuppressInlineDefensiveChecksVisitor:: 1426 SuppressInlineDefensiveChecksVisitor(DefinedSVal Value, const ExplodedNode *N) 1427 : V(Value) { 1428 // Check if the visitor is disabled. 1429 AnalyzerOptions &Options = N->getState()->getAnalysisManager().options; 1430 if (!Options.ShouldSuppressInlinedDefensiveChecks) 1431 IsSatisfied = true; 1432 1433 assert(N->getState()->isNull(V).isConstrainedTrue() && 1434 "The visitor only tracks the cases where V is constrained to 0"); 1435 } 1436 1437 void SuppressInlineDefensiveChecksVisitor::Profile( 1438 llvm::FoldingSetNodeID &ID) const { 1439 static int id = 0; 1440 ID.AddPointer(&id); 1441 ID.Add(V); 1442 } 1443 1444 const char *SuppressInlineDefensiveChecksVisitor::getTag() { 1445 return "IDCVisitor"; 1446 } 1447 1448 std::shared_ptr<PathDiagnosticPiece> 1449 SuppressInlineDefensiveChecksVisitor::VisitNode(const ExplodedNode *Succ, 1450 BugReporterContext &BRC, 1451 BugReport &BR) { 1452 const ExplodedNode *Pred = Succ->getFirstPred(); 1453 if (IsSatisfied) 1454 return nullptr; 1455 1456 // Start tracking after we see the first state in which the value is null. 1457 if (!IsTrackingTurnedOn) 1458 if (Succ->getState()->isNull(V).isConstrainedTrue()) 1459 IsTrackingTurnedOn = true; 1460 if (!IsTrackingTurnedOn) 1461 return nullptr; 1462 1463 // Check if in the previous state it was feasible for this value 1464 // to *not* be null. 1465 if (!Pred->getState()->isNull(V).isConstrainedTrue()) { 1466 IsSatisfied = true; 1467 1468 assert(Succ->getState()->isNull(V).isConstrainedTrue()); 1469 1470 // Check if this is inlined defensive checks. 1471 const LocationContext *CurLC =Succ->getLocationContext(); 1472 const LocationContext *ReportLC = BR.getErrorNode()->getLocationContext(); 1473 if (CurLC != ReportLC && !CurLC->isParentOf(ReportLC)) { 1474 BR.markInvalid("Suppress IDC", CurLC); 1475 return nullptr; 1476 } 1477 1478 // Treat defensive checks in function-like macros as if they were an inlined 1479 // defensive check. If the bug location is not in a macro and the 1480 // terminator for the current location is in a macro then suppress the 1481 // warning. 1482 auto BugPoint = BR.getErrorNode()->getLocation().getAs<StmtPoint>(); 1483 1484 if (!BugPoint) 1485 return nullptr; 1486 1487 ProgramPoint CurPoint = Succ->getLocation(); 1488 const Stmt *CurTerminatorStmt = nullptr; 1489 if (auto BE = CurPoint.getAs<BlockEdge>()) { 1490 CurTerminatorStmt = BE->getSrc()->getTerminator().getStmt(); 1491 } else if (auto SP = CurPoint.getAs<StmtPoint>()) { 1492 const Stmt *CurStmt = SP->getStmt(); 1493 if (!CurStmt->getBeginLoc().isMacroID()) 1494 return nullptr; 1495 1496 CFGStmtMap *Map = CurLC->getAnalysisDeclContext()->getCFGStmtMap(); 1497 CurTerminatorStmt = Map->getBlock(CurStmt)->getTerminator(); 1498 } else { 1499 return nullptr; 1500 } 1501 1502 if (!CurTerminatorStmt) 1503 return nullptr; 1504 1505 SourceLocation TerminatorLoc = CurTerminatorStmt->getBeginLoc(); 1506 if (TerminatorLoc.isMacroID()) { 1507 SourceLocation BugLoc = BugPoint->getStmt()->getBeginLoc(); 1508 1509 // Suppress reports unless we are in that same macro. 1510 if (!BugLoc.isMacroID() || 1511 getMacroName(BugLoc, BRC) != getMacroName(TerminatorLoc, BRC)) { 1512 BR.markInvalid("Suppress Macro IDC", CurLC); 1513 } 1514 return nullptr; 1515 } 1516 } 1517 return nullptr; 1518 } 1519 1520 static const MemRegion *getLocationRegionIfReference(const Expr *E, 1521 const ExplodedNode *N) { 1522 if (const auto *DR = dyn_cast<DeclRefExpr>(E)) { 1523 if (const auto *VD = dyn_cast<VarDecl>(DR->getDecl())) { 1524 if (!VD->getType()->isReferenceType()) 1525 return nullptr; 1526 ProgramStateManager &StateMgr = N->getState()->getStateManager(); 1527 MemRegionManager &MRMgr = StateMgr.getRegionManager(); 1528 return MRMgr.getVarRegion(VD, N->getLocationContext()); 1529 } 1530 } 1531 1532 // FIXME: This does not handle other kinds of null references, 1533 // for example, references from FieldRegions: 1534 // struct Wrapper { int &ref; }; 1535 // Wrapper w = { *(int *)0 }; 1536 // w.ref = 1; 1537 1538 return nullptr; 1539 } 1540 1541 /// \return A subexpression of {@code Ex} which represents the 1542 /// expression-of-interest. 1543 static const Expr *peelOffOuterExpr(const Expr *Ex, 1544 const ExplodedNode *N) { 1545 Ex = Ex->IgnoreParenCasts(); 1546 if (const auto *FE = dyn_cast<FullExpr>(Ex)) 1547 return peelOffOuterExpr(FE->getSubExpr(), N); 1548 if (const auto *OVE = dyn_cast<OpaqueValueExpr>(Ex)) 1549 return peelOffOuterExpr(OVE->getSourceExpr(), N); 1550 if (const auto *POE = dyn_cast<PseudoObjectExpr>(Ex)) { 1551 const auto *PropRef = dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm()); 1552 if (PropRef && PropRef->isMessagingGetter()) { 1553 const Expr *GetterMessageSend = 1554 POE->getSemanticExpr(POE->getNumSemanticExprs() - 1); 1555 assert(isa<ObjCMessageExpr>(GetterMessageSend->IgnoreParenCasts())); 1556 return peelOffOuterExpr(GetterMessageSend, N); 1557 } 1558 } 1559 1560 // Peel off the ternary operator. 1561 if (const auto *CO = dyn_cast<ConditionalOperator>(Ex)) { 1562 // Find a node where the branching occurred and find out which branch 1563 // we took (true/false) by looking at the ExplodedGraph. 1564 const ExplodedNode *NI = N; 1565 do { 1566 ProgramPoint ProgPoint = NI->getLocation(); 1567 if (Optional<BlockEdge> BE = ProgPoint.getAs<BlockEdge>()) { 1568 const CFGBlock *srcBlk = BE->getSrc(); 1569 if (const Stmt *term = srcBlk->getTerminator()) { 1570 if (term == CO) { 1571 bool TookTrueBranch = (*(srcBlk->succ_begin()) == BE->getDst()); 1572 if (TookTrueBranch) 1573 return peelOffOuterExpr(CO->getTrueExpr(), N); 1574 else 1575 return peelOffOuterExpr(CO->getFalseExpr(), N); 1576 } 1577 } 1578 } 1579 NI = NI->getFirstPred(); 1580 } while (NI); 1581 } 1582 1583 if (auto *BO = dyn_cast<BinaryOperator>(Ex)) 1584 if (const Expr *SubEx = peelOffPointerArithmetic(BO)) 1585 return peelOffOuterExpr(SubEx, N); 1586 1587 if (auto *UO = dyn_cast<UnaryOperator>(Ex)) { 1588 if (UO->getOpcode() == UO_LNot) 1589 return peelOffOuterExpr(UO->getSubExpr(), N); 1590 1591 // FIXME: There's a hack in our Store implementation that always computes 1592 // field offsets around null pointers as if they are always equal to 0. 1593 // The idea here is to report accesses to fields as null dereferences 1594 // even though the pointer value that's being dereferenced is actually 1595 // the offset of the field rather than exactly 0. 1596 // See the FIXME in StoreManager's getLValueFieldOrIvar() method. 1597 // This code interacts heavily with this hack; otherwise the value 1598 // would not be null at all for most fields, so we'd be unable to track it. 1599 if (UO->getOpcode() == UO_AddrOf && UO->getSubExpr()->isLValue()) 1600 if (const Expr *DerefEx = bugreporter::getDerefExpr(UO->getSubExpr())) 1601 return peelOffOuterExpr(DerefEx, N); 1602 } 1603 1604 return Ex; 1605 } 1606 1607 /// Find the ExplodedNode where the lvalue (the value of 'Ex') 1608 /// was computed. 1609 static const ExplodedNode* findNodeForExpression(const ExplodedNode *N, 1610 const Expr *Inner) { 1611 while (N) { 1612 if (PathDiagnosticLocation::getStmt(N) == Inner) 1613 return N; 1614 N = N->getFirstPred(); 1615 } 1616 return N; 1617 } 1618 1619 bool bugreporter::trackExpressionValue(const ExplodedNode *InputNode, 1620 const Expr *E, BugReport &report, 1621 bool EnableNullFPSuppression) { 1622 if (!E || !InputNode) 1623 return false; 1624 1625 const Expr *Inner = peelOffOuterExpr(E, InputNode); 1626 const ExplodedNode *LVNode = findNodeForExpression(InputNode, Inner); 1627 if (!LVNode) 1628 return false; 1629 1630 ProgramStateRef LVState = LVNode->getState(); 1631 1632 // The message send could be nil due to the receiver being nil. 1633 // At this point in the path, the receiver should be live since we are at the 1634 // message send expr. If it is nil, start tracking it. 1635 if (const Expr *Receiver = NilReceiverBRVisitor::getNilReceiver(Inner, LVNode)) 1636 trackExpressionValue(LVNode, Receiver, report, EnableNullFPSuppression); 1637 1638 // See if the expression we're interested refers to a variable. 1639 // If so, we can track both its contents and constraints on its value. 1640 if (ExplodedGraph::isInterestingLValueExpr(Inner)) { 1641 SVal LVal = LVNode->getSVal(Inner); 1642 1643 const MemRegion *RR = getLocationRegionIfReference(Inner, LVNode); 1644 bool LVIsNull = LVState->isNull(LVal).isConstrainedTrue(); 1645 1646 // If this is a C++ reference to a null pointer, we are tracking the 1647 // pointer. In addition, we should find the store at which the reference 1648 // got initialized. 1649 if (RR && !LVIsNull) 1650 if (auto KV = LVal.getAs<KnownSVal>()) 1651 report.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>( 1652 *KV, RR, EnableNullFPSuppression)); 1653 1654 // In case of C++ references, we want to differentiate between a null 1655 // reference and reference to null pointer. 1656 // If the LVal is null, check if we are dealing with null reference. 1657 // For those, we want to track the location of the reference. 1658 const MemRegion *R = (RR && LVIsNull) ? RR : 1659 LVNode->getSVal(Inner).getAsRegion(); 1660 1661 if (R) { 1662 1663 // Mark both the variable region and its contents as interesting. 1664 SVal V = LVState->getRawSVal(loc::MemRegionVal(R)); 1665 report.addVisitor( 1666 llvm::make_unique<NoStoreFuncVisitor>(cast<SubRegion>(R))); 1667 1668 MacroNullReturnSuppressionVisitor::addMacroVisitorIfNecessary( 1669 LVNode, R, EnableNullFPSuppression, report, V); 1670 1671 report.markInteresting(V); 1672 report.addVisitor(llvm::make_unique<UndefOrNullArgVisitor>(R)); 1673 1674 // If the contents are symbolic, find out when they became null. 1675 if (V.getAsLocSymbol(/*IncludeBaseRegions*/ true)) 1676 report.addVisitor(llvm::make_unique<TrackConstraintBRVisitor>( 1677 V.castAs<DefinedSVal>(), false)); 1678 1679 // Add visitor, which will suppress inline defensive checks. 1680 if (auto DV = V.getAs<DefinedSVal>()) 1681 if (!DV->isZeroConstant() && LVState->isNull(*DV).isConstrainedTrue() && 1682 EnableNullFPSuppression) 1683 report.addVisitor( 1684 llvm::make_unique<SuppressInlineDefensiveChecksVisitor>(*DV, 1685 LVNode)); 1686 1687 if (auto KV = V.getAs<KnownSVal>()) 1688 report.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>( 1689 *KV, R, EnableNullFPSuppression)); 1690 return true; 1691 } 1692 } 1693 1694 // If the expression is not an "lvalue expression", we can still 1695 // track the constraints on its contents. 1696 SVal V = LVState->getSValAsScalarOrLoc(Inner, LVNode->getLocationContext()); 1697 1698 ReturnVisitor::addVisitorIfNecessary( 1699 LVNode, Inner, report, EnableNullFPSuppression); 1700 1701 // Is it a symbolic value? 1702 if (auto L = V.getAs<loc::MemRegionVal>()) { 1703 report.addVisitor(llvm::make_unique<UndefOrNullArgVisitor>(L->getRegion())); 1704 1705 // FIXME: this is a hack for fixing a later crash when attempting to 1706 // dereference a void* pointer. 1707 // We should not try to dereference pointers at all when we don't care 1708 // what is written inside the pointer. 1709 bool CanDereference = true; 1710 if (const auto *SR = dyn_cast<SymbolicRegion>(L->getRegion())) 1711 if (SR->getSymbol()->getType()->getPointeeType()->isVoidType()) 1712 CanDereference = false; 1713 1714 // At this point we are dealing with the region's LValue. 1715 // However, if the rvalue is a symbolic region, we should track it as well. 1716 // Try to use the correct type when looking up the value. 1717 SVal RVal; 1718 if (ExplodedGraph::isInterestingLValueExpr(Inner)) { 1719 RVal = LVState->getRawSVal(L.getValue(), Inner->getType()); 1720 } else if (CanDereference) { 1721 RVal = LVState->getSVal(L->getRegion()); 1722 } 1723 1724 if (CanDereference) 1725 if (auto KV = RVal.getAs<KnownSVal>()) 1726 report.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>( 1727 *KV, L->getRegion(), EnableNullFPSuppression)); 1728 1729 const MemRegion *RegionRVal = RVal.getAsRegion(); 1730 if (RegionRVal && isa<SymbolicRegion>(RegionRVal)) { 1731 report.markInteresting(RegionRVal); 1732 report.addVisitor(llvm::make_unique<TrackConstraintBRVisitor>( 1733 loc::MemRegionVal(RegionRVal), /*assumption=*/false)); 1734 } 1735 } 1736 return true; 1737 } 1738 1739 const Expr *NilReceiverBRVisitor::getNilReceiver(const Stmt *S, 1740 const ExplodedNode *N) { 1741 const auto *ME = dyn_cast<ObjCMessageExpr>(S); 1742 if (!ME) 1743 return nullptr; 1744 if (const Expr *Receiver = ME->getInstanceReceiver()) { 1745 ProgramStateRef state = N->getState(); 1746 SVal V = N->getSVal(Receiver); 1747 if (state->isNull(V).isConstrainedTrue()) 1748 return Receiver; 1749 } 1750 return nullptr; 1751 } 1752 1753 std::shared_ptr<PathDiagnosticPiece> 1754 NilReceiverBRVisitor::VisitNode(const ExplodedNode *N, 1755 BugReporterContext &BRC, BugReport &BR) { 1756 Optional<PreStmt> P = N->getLocationAs<PreStmt>(); 1757 if (!P) 1758 return nullptr; 1759 1760 const Stmt *S = P->getStmt(); 1761 const Expr *Receiver = getNilReceiver(S, N); 1762 if (!Receiver) 1763 return nullptr; 1764 1765 llvm::SmallString<256> Buf; 1766 llvm::raw_svector_ostream OS(Buf); 1767 1768 if (const auto *ME = dyn_cast<ObjCMessageExpr>(S)) { 1769 OS << "'"; 1770 ME->getSelector().print(OS); 1771 OS << "' not called"; 1772 } 1773 else { 1774 OS << "No method is called"; 1775 } 1776 OS << " because the receiver is nil"; 1777 1778 // The receiver was nil, and hence the method was skipped. 1779 // Register a BugReporterVisitor to issue a message telling us how 1780 // the receiver was null. 1781 bugreporter::trackExpressionValue(N, Receiver, BR, 1782 /*EnableNullFPSuppression*/ false); 1783 // Issue a message saying that the method was skipped. 1784 PathDiagnosticLocation L(Receiver, BRC.getSourceManager(), 1785 N->getLocationContext()); 1786 return std::make_shared<PathDiagnosticEventPiece>(L, OS.str()); 1787 } 1788 1789 // Registers every VarDecl inside a Stmt with a last store visitor. 1790 void FindLastStoreBRVisitor::registerStatementVarDecls(BugReport &BR, 1791 const Stmt *S, 1792 bool EnableNullFPSuppression) { 1793 const ExplodedNode *N = BR.getErrorNode(); 1794 std::deque<const Stmt *> WorkList; 1795 WorkList.push_back(S); 1796 1797 while (!WorkList.empty()) { 1798 const Stmt *Head = WorkList.front(); 1799 WorkList.pop_front(); 1800 1801 ProgramStateManager &StateMgr = N->getState()->getStateManager(); 1802 1803 if (const auto *DR = dyn_cast<DeclRefExpr>(Head)) { 1804 if (const auto *VD = dyn_cast<VarDecl>(DR->getDecl())) { 1805 const VarRegion *R = 1806 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext()); 1807 1808 // What did we load? 1809 SVal V = N->getSVal(S); 1810 1811 if (V.getAs<loc::ConcreteInt>() || V.getAs<nonloc::ConcreteInt>()) { 1812 // Register a new visitor with the BugReport. 1813 BR.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>( 1814 V.castAs<KnownSVal>(), R, EnableNullFPSuppression)); 1815 } 1816 } 1817 } 1818 1819 for (const Stmt *SubStmt : Head->children()) 1820 WorkList.push_back(SubStmt); 1821 } 1822 } 1823 1824 //===----------------------------------------------------------------------===// 1825 // Visitor that tries to report interesting diagnostics from conditions. 1826 //===----------------------------------------------------------------------===// 1827 1828 /// Return the tag associated with this visitor. This tag will be used 1829 /// to make all PathDiagnosticPieces created by this visitor. 1830 const char *ConditionBRVisitor::getTag() { 1831 return "ConditionBRVisitor"; 1832 } 1833 1834 std::shared_ptr<PathDiagnosticPiece> 1835 ConditionBRVisitor::VisitNode(const ExplodedNode *N, 1836 BugReporterContext &BRC, BugReport &BR) { 1837 auto piece = VisitNodeImpl(N, BRC, BR); 1838 if (piece) { 1839 piece->setTag(getTag()); 1840 if (auto *ev = dyn_cast<PathDiagnosticEventPiece>(piece.get())) 1841 ev->setPrunable(true, /* override */ false); 1842 } 1843 return piece; 1844 } 1845 1846 std::shared_ptr<PathDiagnosticPiece> 1847 ConditionBRVisitor::VisitNodeImpl(const ExplodedNode *N, 1848 BugReporterContext &BRC, BugReport &BR) { 1849 ProgramPoint progPoint = N->getLocation(); 1850 1851 // If an assumption was made on a branch, it should be caught 1852 // here by looking at the state transition. 1853 if (Optional<BlockEdge> BE = progPoint.getAs<BlockEdge>()) { 1854 const CFGBlock *srcBlk = BE->getSrc(); 1855 if (const Stmt *term = srcBlk->getTerminator()) 1856 return VisitTerminator(term, N, srcBlk, BE->getDst(), BR, BRC); 1857 return nullptr; 1858 } 1859 1860 if (Optional<PostStmt> PS = progPoint.getAs<PostStmt>()) { 1861 const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags = 1862 ExprEngine::geteagerlyAssumeBinOpBifurcationTags(); 1863 1864 const ProgramPointTag *tag = PS->getTag(); 1865 if (tag == tags.first) 1866 return VisitTrueTest(cast<Expr>(PS->getStmt()), true, 1867 BRC, BR, N); 1868 if (tag == tags.second) 1869 return VisitTrueTest(cast<Expr>(PS->getStmt()), false, 1870 BRC, BR, N); 1871 1872 return nullptr; 1873 } 1874 1875 return nullptr; 1876 } 1877 1878 std::shared_ptr<PathDiagnosticPiece> ConditionBRVisitor::VisitTerminator( 1879 const Stmt *Term, const ExplodedNode *N, const CFGBlock *srcBlk, 1880 const CFGBlock *dstBlk, BugReport &R, BugReporterContext &BRC) { 1881 const Expr *Cond = nullptr; 1882 1883 // In the code below, Term is a CFG terminator and Cond is a branch condition 1884 // expression upon which the decision is made on this terminator. 1885 // 1886 // For example, in "if (x == 0)", the "if (x == 0)" statement is a terminator, 1887 // and "x == 0" is the respective condition. 1888 // 1889 // Another example: in "if (x && y)", we've got two terminators and two 1890 // conditions due to short-circuit nature of operator "&&": 1891 // 1. The "if (x && y)" statement is a terminator, 1892 // and "y" is the respective condition. 1893 // 2. Also "x && ..." is another terminator, 1894 // and "x" is its condition. 1895 1896 switch (Term->getStmtClass()) { 1897 // FIXME: Stmt::SwitchStmtClass is worth handling, however it is a bit 1898 // more tricky because there are more than two branches to account for. 1899 default: 1900 return nullptr; 1901 case Stmt::IfStmtClass: 1902 Cond = cast<IfStmt>(Term)->getCond(); 1903 break; 1904 case Stmt::ConditionalOperatorClass: 1905 Cond = cast<ConditionalOperator>(Term)->getCond(); 1906 break; 1907 case Stmt::BinaryOperatorClass: 1908 // When we encounter a logical operator (&& or ||) as a CFG terminator, 1909 // then the condition is actually its LHS; otherwise, we'd encounter 1910 // the parent, such as if-statement, as a terminator. 1911 const auto *BO = cast<BinaryOperator>(Term); 1912 assert(BO->isLogicalOp() && 1913 "CFG terminator is not a short-circuit operator!"); 1914 Cond = BO->getLHS(); 1915 break; 1916 } 1917 1918 Cond = Cond->IgnoreParens(); 1919 1920 // However, when we encounter a logical operator as a branch condition, 1921 // then the condition is actually its RHS, because LHS would be 1922 // the condition for the logical operator terminator. 1923 while (const auto *InnerBO = dyn_cast<BinaryOperator>(Cond)) { 1924 if (!InnerBO->isLogicalOp()) 1925 break; 1926 Cond = InnerBO->getRHS()->IgnoreParens(); 1927 } 1928 1929 assert(Cond); 1930 assert(srcBlk->succ_size() == 2); 1931 const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk; 1932 return VisitTrueTest(Cond, tookTrue, BRC, R, N); 1933 } 1934 1935 std::shared_ptr<PathDiagnosticPiece> 1936 ConditionBRVisitor::VisitTrueTest(const Expr *Cond, bool tookTrue, 1937 BugReporterContext &BRC, BugReport &R, 1938 const ExplodedNode *N) { 1939 ProgramStateRef CurrentState = N->getState(); 1940 ProgramStateRef PreviousState = N->getFirstPred()->getState(); 1941 const LocationContext *LCtx = N->getLocationContext(); 1942 1943 // If the constraint information is changed between the current and the 1944 // previous program state we assuming the newly seen constraint information. 1945 // If we cannot evaluate the condition (and the constraints are the same) 1946 // the analyzer has no information about the value and just assuming it. 1947 if (BRC.getStateManager().haveEqualConstraints(CurrentState, PreviousState) && 1948 CurrentState->getSVal(Cond, LCtx).isValid()) 1949 return nullptr; 1950 1951 // These will be modified in code below, but we need to preserve the original 1952 // values in case we want to throw the generic message. 1953 const Expr *CondTmp = Cond; 1954 bool tookTrueTmp = tookTrue; 1955 1956 while (true) { 1957 CondTmp = CondTmp->IgnoreParenCasts(); 1958 switch (CondTmp->getStmtClass()) { 1959 default: 1960 break; 1961 case Stmt::BinaryOperatorClass: 1962 if (auto P = VisitTrueTest(Cond, cast<BinaryOperator>(CondTmp), 1963 tookTrueTmp, BRC, R, N)) 1964 return P; 1965 break; 1966 case Stmt::DeclRefExprClass: 1967 if (auto P = VisitTrueTest(Cond, cast<DeclRefExpr>(CondTmp), 1968 tookTrueTmp, BRC, R, N)) 1969 return P; 1970 break; 1971 case Stmt::UnaryOperatorClass: { 1972 const auto *UO = cast<UnaryOperator>(CondTmp); 1973 if (UO->getOpcode() == UO_LNot) { 1974 tookTrueTmp = !tookTrueTmp; 1975 CondTmp = UO->getSubExpr(); 1976 continue; 1977 } 1978 break; 1979 } 1980 } 1981 break; 1982 } 1983 1984 // Condition too complex to explain? Just say something so that the user 1985 // knew we've made some path decision at this point. 1986 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx); 1987 if (!Loc.isValid() || !Loc.asLocation().isValid()) 1988 return nullptr; 1989 1990 return std::make_shared<PathDiagnosticEventPiece>( 1991 Loc, tookTrue ? GenericTrueMessage : GenericFalseMessage); 1992 } 1993 1994 bool ConditionBRVisitor::patternMatch(const Expr *Ex, 1995 const Expr *ParentEx, 1996 raw_ostream &Out, 1997 BugReporterContext &BRC, 1998 BugReport &report, 1999 const ExplodedNode *N, 2000 Optional<bool> &prunable) { 2001 const Expr *OriginalExpr = Ex; 2002 Ex = Ex->IgnoreParenCasts(); 2003 2004 if (isa<GNUNullExpr>(Ex) || isa<ObjCBoolLiteralExpr>(Ex) || 2005 isa<CXXBoolLiteralExpr>(Ex) || isa<IntegerLiteral>(Ex) || 2006 isa<FloatingLiteral>(Ex)) { 2007 // Use heuristics to determine if the expression is a macro 2008 // expanding to a literal and if so, use the macro's name. 2009 SourceLocation BeginLoc = OriginalExpr->getBeginLoc(); 2010 SourceLocation EndLoc = OriginalExpr->getEndLoc(); 2011 if (BeginLoc.isMacroID() && EndLoc.isMacroID()) { 2012 SourceManager &SM = BRC.getSourceManager(); 2013 const LangOptions &LO = BRC.getASTContext().getLangOpts(); 2014 if (Lexer::isAtStartOfMacroExpansion(BeginLoc, SM, LO) && 2015 Lexer::isAtEndOfMacroExpansion(EndLoc, SM, LO)) { 2016 CharSourceRange R = Lexer::getAsCharRange({BeginLoc, EndLoc}, SM, LO); 2017 Out << Lexer::getSourceText(R, SM, LO); 2018 return false; 2019 } 2020 } 2021 } 2022 2023 if (const auto *DR = dyn_cast<DeclRefExpr>(Ex)) { 2024 const bool quotes = isa<VarDecl>(DR->getDecl()); 2025 if (quotes) { 2026 Out << '\''; 2027 const LocationContext *LCtx = N->getLocationContext(); 2028 const ProgramState *state = N->getState().get(); 2029 if (const MemRegion *R = state->getLValue(cast<VarDecl>(DR->getDecl()), 2030 LCtx).getAsRegion()) { 2031 if (report.isInteresting(R)) 2032 prunable = false; 2033 else { 2034 const ProgramState *state = N->getState().get(); 2035 SVal V = state->getSVal(R); 2036 if (report.isInteresting(V)) 2037 prunable = false; 2038 } 2039 } 2040 } 2041 Out << DR->getDecl()->getDeclName().getAsString(); 2042 if (quotes) 2043 Out << '\''; 2044 return quotes; 2045 } 2046 2047 if (const auto *IL = dyn_cast<IntegerLiteral>(Ex)) { 2048 QualType OriginalTy = OriginalExpr->getType(); 2049 if (OriginalTy->isPointerType()) { 2050 if (IL->getValue() == 0) { 2051 Out << "null"; 2052 return false; 2053 } 2054 } 2055 else if (OriginalTy->isObjCObjectPointerType()) { 2056 if (IL->getValue() == 0) { 2057 Out << "nil"; 2058 return false; 2059 } 2060 } 2061 2062 Out << IL->getValue(); 2063 return false; 2064 } 2065 2066 return false; 2067 } 2068 2069 std::shared_ptr<PathDiagnosticPiece> 2070 ConditionBRVisitor::VisitTrueTest(const Expr *Cond, const BinaryOperator *BExpr, 2071 const bool tookTrue, BugReporterContext &BRC, 2072 BugReport &R, const ExplodedNode *N) { 2073 bool shouldInvert = false; 2074 Optional<bool> shouldPrune; 2075 2076 SmallString<128> LhsString, RhsString; 2077 { 2078 llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString); 2079 const bool isVarLHS = patternMatch(BExpr->getLHS(), BExpr, OutLHS, 2080 BRC, R, N, shouldPrune); 2081 const bool isVarRHS = patternMatch(BExpr->getRHS(), BExpr, OutRHS, 2082 BRC, R, N, shouldPrune); 2083 2084 shouldInvert = !isVarLHS && isVarRHS; 2085 } 2086 2087 BinaryOperator::Opcode Op = BExpr->getOpcode(); 2088 2089 if (BinaryOperator::isAssignmentOp(Op)) { 2090 // For assignment operators, all that we care about is that the LHS 2091 // evaluates to "true" or "false". 2092 return VisitConditionVariable(LhsString, BExpr->getLHS(), tookTrue, 2093 BRC, R, N); 2094 } 2095 2096 // For non-assignment operations, we require that we can understand 2097 // both the LHS and RHS. 2098 if (LhsString.empty() || RhsString.empty() || 2099 !BinaryOperator::isComparisonOp(Op) || Op == BO_Cmp) 2100 return nullptr; 2101 2102 // Should we invert the strings if the LHS is not a variable name? 2103 SmallString<256> buf; 2104 llvm::raw_svector_ostream Out(buf); 2105 Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is "; 2106 2107 // Do we need to invert the opcode? 2108 if (shouldInvert) 2109 switch (Op) { 2110 default: break; 2111 case BO_LT: Op = BO_GT; break; 2112 case BO_GT: Op = BO_LT; break; 2113 case BO_LE: Op = BO_GE; break; 2114 case BO_GE: Op = BO_LE; break; 2115 } 2116 2117 if (!tookTrue) 2118 switch (Op) { 2119 case BO_EQ: Op = BO_NE; break; 2120 case BO_NE: Op = BO_EQ; break; 2121 case BO_LT: Op = BO_GE; break; 2122 case BO_GT: Op = BO_LE; break; 2123 case BO_LE: Op = BO_GT; break; 2124 case BO_GE: Op = BO_LT; break; 2125 default: 2126 return nullptr; 2127 } 2128 2129 switch (Op) { 2130 case BO_EQ: 2131 Out << "equal to "; 2132 break; 2133 case BO_NE: 2134 Out << "not equal to "; 2135 break; 2136 default: 2137 Out << BinaryOperator::getOpcodeStr(Op) << ' '; 2138 break; 2139 } 2140 2141 Out << (shouldInvert ? LhsString : RhsString); 2142 const LocationContext *LCtx = N->getLocationContext(); 2143 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx); 2144 auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str()); 2145 if (shouldPrune.hasValue()) 2146 event->setPrunable(shouldPrune.getValue()); 2147 return event; 2148 } 2149 2150 std::shared_ptr<PathDiagnosticPiece> ConditionBRVisitor::VisitConditionVariable( 2151 StringRef LhsString, const Expr *CondVarExpr, const bool tookTrue, 2152 BugReporterContext &BRC, BugReport &report, const ExplodedNode *N) { 2153 // FIXME: If there's already a constraint tracker for this variable, 2154 // we shouldn't emit anything here (c.f. the double note in 2155 // test/Analysis/inlining/path-notes.c) 2156 SmallString<256> buf; 2157 llvm::raw_svector_ostream Out(buf); 2158 Out << "Assuming " << LhsString << " is "; 2159 2160 QualType Ty = CondVarExpr->getType(); 2161 2162 if (Ty->isPointerType()) 2163 Out << (tookTrue ? "not null" : "null"); 2164 else if (Ty->isObjCObjectPointerType()) 2165 Out << (tookTrue ? "not nil" : "nil"); 2166 else if (Ty->isBooleanType()) 2167 Out << (tookTrue ? "true" : "false"); 2168 else if (Ty->isIntegralOrEnumerationType()) 2169 Out << (tookTrue ? "non-zero" : "zero"); 2170 else 2171 return nullptr; 2172 2173 const LocationContext *LCtx = N->getLocationContext(); 2174 PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LCtx); 2175 auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str()); 2176 2177 if (const auto *DR = dyn_cast<DeclRefExpr>(CondVarExpr)) { 2178 if (const auto *VD = dyn_cast<VarDecl>(DR->getDecl())) { 2179 const ProgramState *state = N->getState().get(); 2180 if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) { 2181 if (report.isInteresting(R)) 2182 event->setPrunable(false); 2183 } 2184 } 2185 } 2186 2187 return event; 2188 } 2189 2190 std::shared_ptr<PathDiagnosticPiece> 2191 ConditionBRVisitor::VisitTrueTest(const Expr *Cond, const DeclRefExpr *DR, 2192 const bool tookTrue, BugReporterContext &BRC, 2193 BugReport &report, const ExplodedNode *N) { 2194 const auto *VD = dyn_cast<VarDecl>(DR->getDecl()); 2195 if (!VD) 2196 return nullptr; 2197 2198 SmallString<256> Buf; 2199 llvm::raw_svector_ostream Out(Buf); 2200 2201 Out << "Assuming '" << VD->getDeclName() << "' is "; 2202 2203 QualType VDTy = VD->getType(); 2204 2205 if (VDTy->isPointerType()) 2206 Out << (tookTrue ? "non-null" : "null"); 2207 else if (VDTy->isObjCObjectPointerType()) 2208 Out << (tookTrue ? "non-nil" : "nil"); 2209 else if (VDTy->isScalarType()) 2210 Out << (tookTrue ? "not equal to 0" : "0"); 2211 else 2212 return nullptr; 2213 2214 const LocationContext *LCtx = N->getLocationContext(); 2215 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx); 2216 auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str()); 2217 2218 const ProgramState *state = N->getState().get(); 2219 if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) { 2220 if (report.isInteresting(R)) 2221 event->setPrunable(false); 2222 else { 2223 SVal V = state->getSVal(R); 2224 if (report.isInteresting(V)) 2225 event->setPrunable(false); 2226 } 2227 } 2228 return std::move(event); 2229 } 2230 2231 const char *const ConditionBRVisitor::GenericTrueMessage = 2232 "Assuming the condition is true"; 2233 const char *const ConditionBRVisitor::GenericFalseMessage = 2234 "Assuming the condition is false"; 2235 2236 bool ConditionBRVisitor::isPieceMessageGeneric( 2237 const PathDiagnosticPiece *Piece) { 2238 return Piece->getString() == GenericTrueMessage || 2239 Piece->getString() == GenericFalseMessage; 2240 } 2241 2242 void LikelyFalsePositiveSuppressionBRVisitor::finalizeVisitor( 2243 BugReporterContext &BRC, const ExplodedNode *N, BugReport &BR) { 2244 // Here we suppress false positives coming from system headers. This list is 2245 // based on known issues. 2246 AnalyzerOptions &Options = BRC.getAnalyzerOptions(); 2247 const Decl *D = N->getLocationContext()->getDecl(); 2248 2249 if (AnalysisDeclContext::isInStdNamespace(D)) { 2250 // Skip reports within the 'std' namespace. Although these can sometimes be 2251 // the user's fault, we currently don't report them very well, and 2252 // Note that this will not help for any other data structure libraries, like 2253 // TR1, Boost, or llvm/ADT. 2254 if (Options.ShouldSuppressFromCXXStandardLibrary) { 2255 BR.markInvalid(getTag(), nullptr); 2256 return; 2257 } else { 2258 // If the complete 'std' suppression is not enabled, suppress reports 2259 // from the 'std' namespace that are known to produce false positives. 2260 2261 // The analyzer issues a false use-after-free when std::list::pop_front 2262 // or std::list::pop_back are called multiple times because we cannot 2263 // reason about the internal invariants of the data structure. 2264 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { 2265 const CXXRecordDecl *CD = MD->getParent(); 2266 if (CD->getName() == "list") { 2267 BR.markInvalid(getTag(), nullptr); 2268 return; 2269 } 2270 } 2271 2272 // The analyzer issues a false positive when the constructor of 2273 // std::__independent_bits_engine from algorithms is used. 2274 if (const auto *MD = dyn_cast<CXXConstructorDecl>(D)) { 2275 const CXXRecordDecl *CD = MD->getParent(); 2276 if (CD->getName() == "__independent_bits_engine") { 2277 BR.markInvalid(getTag(), nullptr); 2278 return; 2279 } 2280 } 2281 2282 for (const LocationContext *LCtx = N->getLocationContext(); LCtx; 2283 LCtx = LCtx->getParent()) { 2284 const auto *MD = dyn_cast<CXXMethodDecl>(LCtx->getDecl()); 2285 if (!MD) 2286 continue; 2287 2288 const CXXRecordDecl *CD = MD->getParent(); 2289 // The analyzer issues a false positive on 2290 // std::basic_string<uint8_t> v; v.push_back(1); 2291 // and 2292 // std::u16string s; s += u'a'; 2293 // because we cannot reason about the internal invariants of the 2294 // data structure. 2295 if (CD->getName() == "basic_string") { 2296 BR.markInvalid(getTag(), nullptr); 2297 return; 2298 } 2299 2300 // The analyzer issues a false positive on 2301 // std::shared_ptr<int> p(new int(1)); p = nullptr; 2302 // because it does not reason properly about temporary destructors. 2303 if (CD->getName() == "shared_ptr") { 2304 BR.markInvalid(getTag(), nullptr); 2305 return; 2306 } 2307 } 2308 } 2309 } 2310 2311 // Skip reports within the sys/queue.h macros as we do not have the ability to 2312 // reason about data structure shapes. 2313 SourceManager &SM = BRC.getSourceManager(); 2314 FullSourceLoc Loc = BR.getLocation(SM).asLocation(); 2315 while (Loc.isMacroID()) { 2316 Loc = Loc.getSpellingLoc(); 2317 if (SM.getFilename(Loc).endswith("sys/queue.h")) { 2318 BR.markInvalid(getTag(), nullptr); 2319 return; 2320 } 2321 } 2322 } 2323 2324 std::shared_ptr<PathDiagnosticPiece> 2325 UndefOrNullArgVisitor::VisitNode(const ExplodedNode *N, 2326 BugReporterContext &BRC, BugReport &BR) { 2327 ProgramStateRef State = N->getState(); 2328 ProgramPoint ProgLoc = N->getLocation(); 2329 2330 // We are only interested in visiting CallEnter nodes. 2331 Optional<CallEnter> CEnter = ProgLoc.getAs<CallEnter>(); 2332 if (!CEnter) 2333 return nullptr; 2334 2335 // Check if one of the arguments is the region the visitor is tracking. 2336 CallEventManager &CEMgr = BRC.getStateManager().getCallEventManager(); 2337 CallEventRef<> Call = CEMgr.getCaller(CEnter->getCalleeContext(), State); 2338 unsigned Idx = 0; 2339 ArrayRef<ParmVarDecl *> parms = Call->parameters(); 2340 2341 for (const auto ParamDecl : parms) { 2342 const MemRegion *ArgReg = Call->getArgSVal(Idx).getAsRegion(); 2343 ++Idx; 2344 2345 // Are we tracking the argument or its subregion? 2346 if ( !ArgReg || !R->isSubRegionOf(ArgReg->StripCasts())) 2347 continue; 2348 2349 // Check the function parameter type. 2350 assert(ParamDecl && "Formal parameter has no decl?"); 2351 QualType T = ParamDecl->getType(); 2352 2353 if (!(T->isAnyPointerType() || T->isReferenceType())) { 2354 // Function can only change the value passed in by address. 2355 continue; 2356 } 2357 2358 // If it is a const pointer value, the function does not intend to 2359 // change the value. 2360 if (T->getPointeeType().isConstQualified()) 2361 continue; 2362 2363 // Mark the call site (LocationContext) as interesting if the value of the 2364 // argument is undefined or '0'/'NULL'. 2365 SVal BoundVal = State->getSVal(R); 2366 if (BoundVal.isUndef() || BoundVal.isZeroConstant()) { 2367 BR.markInteresting(CEnter->getCalleeContext()); 2368 return nullptr; 2369 } 2370 } 2371 return nullptr; 2372 } 2373 2374 std::shared_ptr<PathDiagnosticPiece> 2375 CXXSelfAssignmentBRVisitor::VisitNode(const ExplodedNode *Succ, 2376 BugReporterContext &BRC, BugReport &) { 2377 if (Satisfied) 2378 return nullptr; 2379 2380 const auto Edge = Succ->getLocation().getAs<BlockEdge>(); 2381 if (!Edge.hasValue()) 2382 return nullptr; 2383 2384 auto Tag = Edge->getTag(); 2385 if (!Tag) 2386 return nullptr; 2387 2388 if (Tag->getTagDescription() != "cplusplus.SelfAssignment") 2389 return nullptr; 2390 2391 Satisfied = true; 2392 2393 const auto *Met = 2394 dyn_cast<CXXMethodDecl>(Succ->getCodeDecl().getAsFunction()); 2395 assert(Met && "Not a C++ method."); 2396 assert((Met->isCopyAssignmentOperator() || Met->isMoveAssignmentOperator()) && 2397 "Not a copy/move assignment operator."); 2398 2399 const auto *LCtx = Edge->getLocationContext(); 2400 2401 const auto &State = Succ->getState(); 2402 auto &SVB = State->getStateManager().getSValBuilder(); 2403 2404 const auto Param = 2405 State->getSVal(State->getRegion(Met->getParamDecl(0), LCtx)); 2406 const auto This = 2407 State->getSVal(SVB.getCXXThis(Met, LCtx->getStackFrame())); 2408 2409 auto L = PathDiagnosticLocation::create(Met, BRC.getSourceManager()); 2410 2411 if (!L.isValid() || !L.asLocation().isValid()) 2412 return nullptr; 2413 2414 SmallString<256> Buf; 2415 llvm::raw_svector_ostream Out(Buf); 2416 2417 Out << "Assuming " << Met->getParamDecl(0)->getName() << 2418 ((Param == This) ? " == " : " != ") << "*this"; 2419 2420 auto Piece = std::make_shared<PathDiagnosticEventPiece>(L, Out.str()); 2421 Piece->addRange(Met->getSourceRange()); 2422 2423 return std::move(Piece); 2424 } 2425 2426 2427 FalsePositiveRefutationBRVisitor::FalsePositiveRefutationBRVisitor() 2428 : Constraints(ConstraintRangeTy::Factory().getEmptyMap()) {} 2429 2430 void FalsePositiveRefutationBRVisitor::finalizeVisitor( 2431 BugReporterContext &BRC, const ExplodedNode *EndPathNode, BugReport &BR) { 2432 // Collect new constraints 2433 VisitNode(EndPathNode, BRC, BR); 2434 2435 // Create a refutation manager 2436 llvm::SMTSolverRef RefutationSolver = llvm::CreateZ3Solver(); 2437 ASTContext &Ctx = BRC.getASTContext(); 2438 2439 // Add constraints to the solver 2440 for (const auto &I : Constraints) { 2441 const SymbolRef Sym = I.first; 2442 auto RangeIt = I.second.begin(); 2443 2444 llvm::SMTExprRef Constraints = SMTConv::getRangeExpr( 2445 RefutationSolver, Ctx, Sym, RangeIt->From(), RangeIt->To(), 2446 /*InRange=*/true); 2447 while ((++RangeIt) != I.second.end()) { 2448 Constraints = RefutationSolver->mkOr( 2449 Constraints, SMTConv::getRangeExpr(RefutationSolver, Ctx, Sym, 2450 RangeIt->From(), RangeIt->To(), 2451 /*InRange=*/true)); 2452 } 2453 2454 RefutationSolver->addConstraint(Constraints); 2455 } 2456 2457 // And check for satisfiability 2458 Optional<bool> isSat = RefutationSolver->check(); 2459 if (!isSat.hasValue()) 2460 return; 2461 2462 if (!isSat.getValue()) 2463 BR.markInvalid("Infeasible constraints", EndPathNode->getLocationContext()); 2464 } 2465 2466 std::shared_ptr<PathDiagnosticPiece> 2467 FalsePositiveRefutationBRVisitor::VisitNode(const ExplodedNode *N, 2468 BugReporterContext &, 2469 BugReport &) { 2470 // Collect new constraints 2471 const ConstraintRangeTy &NewCs = N->getState()->get<ConstraintRange>(); 2472 ConstraintRangeTy::Factory &CF = 2473 N->getState()->get_context<ConstraintRange>(); 2474 2475 // Add constraints if we don't have them yet 2476 for (auto const &C : NewCs) { 2477 const SymbolRef &Sym = C.first; 2478 if (!Constraints.contains(Sym)) { 2479 Constraints = CF.add(Constraints, Sym, C.second); 2480 } 2481 } 2482 2483 return nullptr; 2484 } 2485 2486 int NoteTag::Kind = 0; 2487 2488 void TagVisitor::Profile(llvm::FoldingSetNodeID &ID) const { 2489 static int Tag = 0; 2490 ID.AddPointer(&Tag); 2491 } 2492 2493 std::shared_ptr<PathDiagnosticPiece> 2494 TagVisitor::VisitNode(const ExplodedNode *N, BugReporterContext &BRC, 2495 BugReport &R) { 2496 ProgramPoint PP = N->getLocation(); 2497 const NoteTag *T = dyn_cast_or_null<NoteTag>(PP.getTag()); 2498 if (!T) 2499 return nullptr; 2500 2501 if (Optional<std::string> Msg = T->generateMessage(BRC, R)) { 2502 PathDiagnosticLocation Loc = 2503 PathDiagnosticLocation::create(PP, BRC.getSourceManager()); 2504 return std::make_shared<PathDiagnosticEventPiece>(Loc, *Msg); 2505 } 2506 2507 return nullptr; 2508 } 2509 2510 void FalsePositiveRefutationBRVisitor::Profile( 2511 llvm::FoldingSetNodeID &ID) const { 2512 static int Tag = 0; 2513 ID.AddPointer(&Tag); 2514 } 2515