ArrayBoundChecker.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //== ArrayBoundChecker.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 ArrayBoundChecker, which is a path-sensitive check
  10. // which looks for an out-of-bound array element access.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  14. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  15. #include "clang/StaticAnalyzer/Core/Checker.h"
  16. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h"
  19. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  20. using namespace clang;
  21. using namespace ento;
  22. namespace {
  23. class ArrayBoundChecker :
  24. public Checker<check::Location> {
  25. mutable std::unique_ptr<BuiltinBug> BT;
  26. public:
  27. void checkLocation(SVal l, bool isLoad, const Stmt* S,
  28. CheckerContext &C) const;
  29. };
  30. }
  31. void ArrayBoundChecker::checkLocation(SVal l, bool isLoad, const Stmt* LoadS,
  32. CheckerContext &C) const {
  33. // Check for out of bound array element access.
  34. const MemRegion *R = l.getAsRegion();
  35. if (!R)
  36. return;
  37. const ElementRegion *ER = dyn_cast<ElementRegion>(R);
  38. if (!ER)
  39. return;
  40. // Get the index of the accessed element.
  41. DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
  42. // Zero index is always in bound, this also passes ElementRegions created for
  43. // pointer casts.
  44. if (Idx.isZeroConstant())
  45. return;
  46. ProgramStateRef state = C.getState();
  47. // Get the size of the array.
  48. DefinedOrUnknownSVal ElementCount = getDynamicElementCount(
  49. state, ER->getSuperRegion(), C.getSValBuilder(), ER->getValueType());
  50. ProgramStateRef StInBound = state->assumeInBound(Idx, ElementCount, true);
  51. ProgramStateRef StOutBound = state->assumeInBound(Idx, ElementCount, false);
  52. if (StOutBound && !StInBound) {
  53. ExplodedNode *N = C.generateErrorNode(StOutBound);
  54. if (!N)
  55. return;
  56. if (!BT)
  57. BT.reset(new BuiltinBug(
  58. this, "Out-of-bound array access",
  59. "Access out-of-bound array element (buffer overflow)"));
  60. // FIXME: It would be nice to eventually make this diagnostic more clear,
  61. // e.g., by referencing the original declaration or by saying *why* this
  62. // reference is outside the range.
  63. // Generate a report for this bug.
  64. auto report =
  65. std::make_unique<PathSensitiveBugReport>(*BT, BT->getDescription(), N);
  66. report->addRange(LoadS->getSourceRange());
  67. C.emitReport(std::move(report));
  68. return;
  69. }
  70. // Array bound check succeeded. From this point forward the array bound
  71. // should always succeed.
  72. C.addTransition(StInBound);
  73. }
  74. void ento::registerArrayBoundChecker(CheckerManager &mgr) {
  75. mgr.registerChecker<ArrayBoundChecker>();
  76. }
  77. bool ento::shouldRegisterArrayBoundChecker(const CheckerManager &mgr) {
  78. return true;
  79. }