UndefBranchChecker.cpp 3.6 KB

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