UndefBranchChecker.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //=== UndefBranchChecker.cpp -----------------------------------*- C++ -*--===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines UndefBranchChecker, which checks for undefined branch
  11. // condition.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "ClangSACheckers.h"
  15. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  16. #include "clang/StaticAnalyzer/Core/Checker.h"
  17. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  19. using namespace clang;
  20. using namespace ento;
  21. namespace {
  22. class UndefBranchChecker : public Checker<check::BranchCondition> {
  23. mutable std::unique_ptr<BuiltinBug> BT;
  24. struct FindUndefExpr {
  25. ProgramStateRef St;
  26. const LocationContext *LCtx;
  27. FindUndefExpr(ProgramStateRef S, const LocationContext *L)
  28. : St(S), LCtx(L) {}
  29. const Expr *FindExpr(const Expr *Ex) {
  30. if (!MatchesCriteria(Ex))
  31. return nullptr;
  32. for (const Stmt *SubStmt : Ex->children())
  33. if (const Expr *ExI = dyn_cast_or_null<Expr>(SubStmt))
  34. if (const Expr *E2 = FindExpr(ExI))
  35. return E2;
  36. return Ex;
  37. }
  38. bool MatchesCriteria(const Expr *Ex) {
  39. return St->getSVal(Ex, LCtx).isUndef();
  40. }
  41. };
  42. public:
  43. void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const;
  44. };
  45. }
  46. void UndefBranchChecker::checkBranchCondition(const Stmt *Condition,
  47. CheckerContext &Ctx) const {
  48. SVal X = Ctx.getState()->getSVal(Condition, Ctx.getLocationContext());
  49. if (X.isUndef()) {
  50. // Generate a sink node, which implicitly marks both outgoing branches as
  51. // infeasible.
  52. ExplodedNode *N = Ctx.generateSink();
  53. if (N) {
  54. if (!BT)
  55. BT.reset(new BuiltinBug(
  56. this, "Branch condition evaluates to a garbage value"));
  57. // What's going on here: we want to highlight the subexpression of the
  58. // condition that is the most likely source of the "uninitialized
  59. // branch condition." We do a recursive walk of the condition's
  60. // subexpressions and roughly look for the most nested subexpression
  61. // that binds to Undefined. We then highlight that expression's range.
  62. // Get the predecessor node and check if is a PostStmt with the Stmt
  63. // being the terminator condition. We want to inspect the state
  64. // of that node instead because it will contain main information about
  65. // the subexpressions.
  66. // Note: any predecessor will do. They should have identical state,
  67. // since all the BlockEdge did was act as an error sink since the value
  68. // had to already be undefined.
  69. assert (!N->pred_empty());
  70. const Expr *Ex = cast<Expr>(Condition);
  71. ExplodedNode *PrevN = *N->pred_begin();
  72. ProgramPoint P = PrevN->getLocation();
  73. ProgramStateRef St = N->getState();
  74. if (Optional<PostStmt> PS = P.getAs<PostStmt>())
  75. if (PS->getStmt() == Ex)
  76. St = PrevN->getState();
  77. FindUndefExpr FindIt(St, Ctx.getLocationContext());
  78. Ex = FindIt.FindExpr(Ex);
  79. // Emit the bug report.
  80. auto R = llvm::make_unique<BugReport>(*BT, BT->getDescription(), N);
  81. bugreporter::trackNullOrUndefValue(N, Ex, *R);
  82. R->addRange(Ex->getSourceRange());
  83. Ctx.emitReport(std::move(R));
  84. }
  85. }
  86. }
  87. void ento::registerUndefBranchChecker(CheckerManager &mgr) {
  88. mgr.registerChecker<UndefBranchChecker>();
  89. }