Beispiel #1
0
void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G,
                                            BugReporter &B,
                                            ExprEngine &Eng) const {
  const CFG *C  = 0;
  const SourceManager &SM = B.getSourceManager();
  llvm::SmallPtrSet<const CFGBlock*, 256> reachable;

  // Root node should have the location context of the top most function.
  const ExplodedNode *GraphRoot = *G.roots_begin();
  const LocationContext *LC = GraphRoot->getLocation().getLocationContext();

  const Decl *D = LC->getDecl();

  // Iterate over the exploded graph.
  for (ExplodedGraph::node_iterator I = G.nodes_begin();
      I != G.nodes_end(); ++I) {
    const ProgramPoint &P = I->getLocation();

    // Only check the coverage in the top level function (optimization).
    if (D != P.getLocationContext()->getDecl())
      continue;

    if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
      const CFGBlock *CB = BE->getBlock();
      reachable.insert(CB);
    }
  }

  // Get the CFG and the Decl of this block.
  C = LC->getCFG();

  unsigned total = 0, unreachable = 0;

  // Find CFGBlocks that were not covered by any node
  for (CFG::const_iterator I = C->begin(); I != C->end(); ++I) {
    const CFGBlock *CB = *I;
    ++total;
    // Check if the block is unreachable
    if (!reachable.count(CB)) {
      ++unreachable;
    }
  }

  // We never 'reach' the entry block, so correct the unreachable count
  unreachable--;
  // There is no BlockEntrance corresponding to the exit block as well, so
  // assume it is reached as well.
  unreachable--;

  // Generate the warning string
  SmallString<128> buf;
  llvm::raw_svector_ostream output(buf);
  PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
  if (!Loc.isValid())
    return;

  if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
    const NamedDecl *ND = cast<NamedDecl>(D);
    output << *ND;
  }
  else if (isa<BlockDecl>(D)) {
    output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn();
  }

  NumBlocksUnreachable += unreachable;
  NumBlocks += total;
  std::string NameOfRootFunction = output.str();

  output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: "
      << unreachable << " | Exhausted Block: "
      << (Eng.wasBlocksExhausted() ? "yes" : "no")
      << " | Empty WorkList: "
      << (Eng.hasEmptyWorkList() ? "yes" : "no");

  B.EmitBasicReport(D, "Analyzer Statistics", "Internal Statistics",
                    output.str(), PathDiagnosticLocation(D, SM));

  // Emit warning for each block we bailed out on.
  typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator;
  const CoreEngine &CE = Eng.getCoreEngine();
  for (ExhaustedIterator I = CE.blocks_exhausted_begin(),
      E = CE.blocks_exhausted_end(); I != E; ++I) {
    const BlockEdge &BE =  I->first;
    const CFGBlock *Exit = BE.getDst();
    const CFGElement &CE = Exit->front();
    if (const CFGStmt *CS = dyn_cast<CFGStmt>(&CE)) {
      SmallString<128> bufI;
      llvm::raw_svector_ostream outputI(bufI);
      outputI << "(" << NameOfRootFunction << ")" <<
                 ": The analyzer generated a sink at this point";
      B.EmitBasicReport(D, "Sink Point", "Internal Statistics", outputI.str(),
                        PathDiagnosticLocation::createBegin(CS->getStmt(),
                                                            SM, LC));
    }
  }
}
Beispiel #2
0
void IdempotentOperationChecker::checkEndAnalysis(ExplodedGraph &G,
                                                  BugReporter &BR,
                                                  ExprEngine &Eng) const {
  BugType *BT = new BugType("Idempotent operation", "Dead code");
  // Iterate over the hash to see if we have any paths with definite
  // idempotent operations.
  for (AssumptionMap::const_iterator i = hash.begin(); i != hash.end(); ++i) {
    // Unpack the hash contents
    const BinaryOperatorData &Data = i->second;
    const Assumption &A = Data.assumption;
    const ExplodedNodeSet &ES = Data.explodedNodes;

    // If there are no nodes accosted with the expression, nothing to report.
    // FIXME: This is possible because the checker does part of processing in
    // checkPreStmt and part in checkPostStmt.
    if (ES.begin() == ES.end())
      continue;

    const BinaryOperator *B = i->first;

    if (A == Impossible)
      continue;

    // If the analyzer did not finish, check to see if we can still emit this
    // warning
    if (Eng.hasWorkRemaining()) {
      // If we can trace back
      AnalysisDeclContext *AC = (*ES.begin())->getLocationContext()
                                         ->getAnalysisDeclContext();
      if (!pathWasCompletelyAnalyzed(AC,
                                     AC->getCFGStmtMap()->getBlock(B),
                                     Eng.getCoreEngine()))
        continue;
    }

    // Select the error message and SourceRanges to report.
    llvm::SmallString<128> buf;
    llvm::raw_svector_ostream os(buf);
    bool LHSRelevant = false, RHSRelevant = false;
    switch (A) {
    case Equal:
      LHSRelevant = true;
      RHSRelevant = true;
      if (B->getOpcode() == BO_Assign)
        os << "Assigned value is always the same as the existing value";
      else
        os << "Both operands to '" << B->getOpcodeStr()
           << "' always have the same value";
      break;
    case LHSis1:
      LHSRelevant = true;
      os << "The left operand to '" << B->getOpcodeStr() << "' is always 1";
      break;
    case RHSis1:
      RHSRelevant = true;
      os << "The right operand to '" << B->getOpcodeStr() << "' is always 1";
      break;
    case LHSis0:
      LHSRelevant = true;
      os << "The left operand to '" << B->getOpcodeStr() << "' is always 0";
      break;
    case RHSis0:
      RHSRelevant = true;
      os << "The right operand to '" << B->getOpcodeStr() << "' is always 0";
      break;
    case Possible:
      llvm_unreachable("Operation was never marked with an assumption");
    case Impossible:
      llvm_unreachable(0);
    }

    // Add a report for each ExplodedNode
    for (ExplodedNodeSet::iterator I = ES.begin(), E = ES.end(); I != E; ++I) {
      BugReport *report = new BugReport(*BT, os.str(), *I);

      // Add source ranges and visitor hooks
      if (LHSRelevant) {
        const Expr *LHS = i->first->getLHS();
        report->addRange(LHS->getSourceRange());
        FindLastStoreBRVisitor::registerStatementVarDecls(*report, LHS);
      }
      if (RHSRelevant) {
        const Expr *RHS = i->first->getRHS();
        report->addRange(i->first->getRHS()->getSourceRange());
        FindLastStoreBRVisitor::registerStatementVarDecls(*report, RHS);
      }

      BR.EmitReport(report);
    }
  }

  hash.clear();
}
Beispiel #3
0
void AnalyzerStatsChecker::checkEndAnalysis(ExplodedGraph &G,
        BugReporter &B,
        ExprEngine &Eng) const {
    const CFG *C  = 0;
    const Decl *D = 0;
    const LocationContext *LC = 0;
    const SourceManager &SM = B.getSourceManager();
    llvm::SmallPtrSet<const CFGBlock*, 256> reachable;

    // Iterate over explodedgraph
    for (ExplodedGraph::node_iterator I = G.nodes_begin();
            I != G.nodes_end(); ++I) {
        const ProgramPoint &P = I->getLocation();
        // Save the LocationContext if we don't have it already
        if (!LC)
            LC = P.getLocationContext();

        if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) {
            const CFGBlock *CB = BE->getBlock();
            reachable.insert(CB);
        }
    }

    // Get the CFG and the Decl of this block
    C = LC->getCFG();
    D = LC->getAnalysisContext()->getDecl();

    unsigned total = 0, unreachable = 0;

    // Find CFGBlocks that were not covered by any node
    for (CFG::const_iterator I = C->begin(); I != C->end(); ++I) {
        const CFGBlock *CB = *I;
        ++total;
        // Check if the block is unreachable
        if (!reachable.count(CB)) {
            ++unreachable;
        }
    }

    // We never 'reach' the entry block, so correct the unreachable count
    unreachable--;

    // Generate the warning string
    llvm::SmallString<128> buf;
    llvm::raw_svector_ostream output(buf);
    PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
    if (Loc.isValid()) {
        output << Loc.getFilename() << " : ";

        if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
            const NamedDecl *ND = cast<NamedDecl>(D);
            output << ND;
        }
        else if (isa<BlockDecl>(D)) {
            output << "block(line:" << Loc.getLine() << ":col:" << Loc.getColumn();
        }
    }

    output << " -> Total CFGBlocks: " << total << " | Unreachable CFGBlocks: "
           << unreachable << " | Exhausted Block: "
           << (Eng.wasBlocksExhausted() ? "yes" : "no")
           << " | Empty WorkList: "
           << (Eng.hasEmptyWorkList() ? "yes" : "no");

    B.EmitBasicReport("Analyzer Statistics", "Internal Statistics", output.str(),
                      PathDiagnosticLocation(D, SM));

    // Emit warning for each block we bailed out on
    typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator;
    const CoreEngine &CE = Eng.getCoreEngine();
    for (ExhaustedIterator I = CE.blocks_exhausted_begin(),
            E = CE.blocks_exhausted_end(); I != E; ++I) {
        const BlockEdge &BE =  I->first;
        const CFGBlock *Exit = BE.getDst();
        const CFGElement &CE = Exit->front();
        if (const CFGStmt *CS = dyn_cast<CFGStmt>(&CE))
            B.EmitBasicReport("Bailout Point", "Internal Statistics", "The analyzer "
                              "stopped analyzing at this point",
                              PathDiagnosticLocation::createBegin(CS->getStmt(), SM, LC));
    }
}