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