UndefBranchChecker.cpp 4.0 KB

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