UndefBranchChecker.cpp 4.0 KB

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