ArrayBoundChecker.cpp 2.9 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 "InternalChecks.h"
  15. #include "clang/StaticAnalyzer/BugReporter/BugType.h"
  16. #include "clang/StaticAnalyzer/PathSensitive/CheckerVisitor.h"
  17. #include "clang/StaticAnalyzer/PathSensitive/ExprEngine.h"
  18. using namespace clang;
  19. using namespace ento;
  20. namespace {
  21. class ArrayBoundChecker :
  22. public CheckerVisitor<ArrayBoundChecker> {
  23. BuiltinBug *BT;
  24. public:
  25. ArrayBoundChecker() : BT(0) {}
  26. static void *getTag() { static int x = 0; return &x; }
  27. void visitLocation(CheckerContext &C, const Stmt *S, SVal l, bool isLoad);
  28. };
  29. }
  30. void ento::RegisterArrayBoundChecker(ExprEngine &Eng) {
  31. Eng.registerCheck(new ArrayBoundChecker());
  32. }
  33. void ArrayBoundChecker::visitLocation(CheckerContext &C, const Stmt *S, SVal l,
  34. bool isLoad) {
  35. // Check for out of bound array element access.
  36. const MemRegion *R = l.getAsRegion();
  37. if (!R)
  38. return;
  39. const ElementRegion *ER = dyn_cast<ElementRegion>(R);
  40. if (!ER)
  41. return;
  42. // Get the index of the accessed element.
  43. DefinedOrUnknownSVal Idx = cast<DefinedOrUnknownSVal>(ER->getIndex());
  44. // Zero index is always in bound, this also passes ElementRegions created for
  45. // pointer casts.
  46. if (Idx.isZeroConstant())
  47. return;
  48. const GRState *state = C.getState();
  49. // Get the size of the array.
  50. DefinedOrUnknownSVal NumElements
  51. = C.getStoreManager().getSizeInElements(state, ER->getSuperRegion(),
  52. ER->getValueType());
  53. const GRState *StInBound = state->assumeInBound(Idx, NumElements, true);
  54. const GRState *StOutBound = state->assumeInBound(Idx, NumElements, false);
  55. if (StOutBound && !StInBound) {
  56. ExplodedNode *N = C.generateSink(StOutBound);
  57. if (!N)
  58. return;
  59. if (!BT)
  60. BT = new BuiltinBug("Out-of-bound array access",
  61. "Access out-of-bound array element (buffer overflow)");
  62. // FIXME: It would be nice to eventually make this diagnostic more clear,
  63. // e.g., by referencing the original declaration or by saying *why* this
  64. // reference is outside the range.
  65. // Generate a report for this bug.
  66. RangedBugReport *report =
  67. new RangedBugReport(*BT, BT->getDescription(), N);
  68. report->addRange(S->getSourceRange());
  69. C.EmitReport(report);
  70. return;
  71. }
  72. // Array bound check succeeded. From this point forward the array bound
  73. // should always succeed.
  74. assert(StInBound);
  75. C.addTransition(StInBound);
  76. }