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