ArrayBoundChecker.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //== ArrayBoundChecker.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 ArrayBoundChecker, which is a path-sensitive check
  11. // which looks for an out-of-bound array element access.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "ClangSACheckers.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/BugReporter/BugType.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 llvm::OwningPtr<BuiltinBug> BT;
  26. public:
  27. void checkLocation(SVal l, bool isLoad, CheckerContext &C) const;
  28. };
  29. }
  30. void ArrayBoundChecker::checkLocation(SVal l, bool isLoad,
  31. CheckerContext &C) const {
  32. // Check for out of bound array element access.
  33. const MemRegion *R = l.getAsRegion();
  34. if (!R)
  35. return;
  36. const ElementRegion *ER = dyn_cast<ElementRegion>(R);
  37. if (!ER)
  38. return;
  39. // Get the index of the accessed element.
  40. DefinedOrUnknownSVal Idx = cast<DefinedOrUnknownSVal>(ER->getIndex());
  41. // Zero index is always in bound, this also passes ElementRegions created for
  42. // pointer casts.
  43. if (Idx.isZeroConstant())
  44. return;
  45. const ProgramState *state = C.getState();
  46. // Get the size of the array.
  47. DefinedOrUnknownSVal NumElements
  48. = C.getStoreManager().getSizeInElements(state, ER->getSuperRegion(),
  49. ER->getValueType());
  50. const ProgramState *StInBound = state->assumeInBound(Idx, NumElements, true);
  51. const ProgramState *StOutBound = state->assumeInBound(Idx, NumElements, false);
  52. if (StOutBound && !StInBound) {
  53. ExplodedNode *N = C.generateSink(StOutBound);
  54. if (!N)
  55. return;
  56. if (!BT)
  57. BT.reset(new BuiltinBug("Out-of-bound array access",
  58. "Access out-of-bound array element (buffer overflow)"));
  59. // FIXME: It would be nice to eventually make this diagnostic more clear,
  60. // e.g., by referencing the original declaration or by saying *why* this
  61. // reference is outside the range.
  62. // Generate a report for this bug.
  63. BugReport *report =
  64. new BugReport(*BT, BT->getDescription(), N);
  65. report->addRange(C.getStmt()->getSourceRange());
  66. C.EmitReport(report);
  67. return;
  68. }
  69. // Array bound check succeeded. From this point forward the array bound
  70. // should always succeed.
  71. assert(StInBound);
  72. C.addTransition(StInBound);
  73. }
  74. void ento::registerArrayBoundChecker(CheckerManager &mgr) {
  75. mgr.registerChecker<ArrayBoundChecker>();
  76. }