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