ArrayBoundCheckerV2.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //== ArrayBoundCheckerV2.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 ArrayBoundCheckerV2, 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. #include "clang/AST/CharUnits.h"
  19. using namespace clang;
  20. using namespace ento;
  21. namespace {
  22. class ArrayBoundCheckerV2 :
  23. public CheckerVisitor<ArrayBoundCheckerV2> {
  24. BuiltinBug *BT;
  25. enum OOB_Kind { OOB_Precedes, OOB_Excedes };
  26. void reportOOB(CheckerContext &C, const GRState *errorState,
  27. OOB_Kind kind);
  28. public:
  29. ArrayBoundCheckerV2() : BT(0) {}
  30. static void *getTag() { static int x = 0; return &x; }
  31. void visitLocation(CheckerContext &C, const Stmt *S, SVal l, bool isLoad);
  32. };
  33. // FIXME: Eventually replace RegionRawOffset with this class.
  34. class RegionRawOffsetV2 {
  35. private:
  36. const SubRegion *baseRegion;
  37. SVal byteOffset;
  38. RegionRawOffsetV2()
  39. : baseRegion(0), byteOffset(UnknownVal()) {}
  40. public:
  41. RegionRawOffsetV2(const SubRegion* base, SVal offset)
  42. : baseRegion(base), byteOffset(offset) {}
  43. NonLoc getByteOffset() const { return cast<NonLoc>(byteOffset); }
  44. const SubRegion *getRegion() const { return baseRegion; }
  45. static RegionRawOffsetV2 computeOffset(const GRState *state,
  46. SValBuilder &svalBuilder,
  47. SVal location);
  48. void dump() const;
  49. void dumpToStream(llvm::raw_ostream& os) const;
  50. };
  51. }
  52. void ento::RegisterArrayBoundCheckerV2(ExprEngine &Eng) {
  53. Eng.registerCheck(new ArrayBoundCheckerV2());
  54. }
  55. void ArrayBoundCheckerV2::visitLocation(CheckerContext &checkerContext,
  56. const Stmt *S,
  57. SVal location, bool isLoad) {
  58. // NOTE: Instead of using GRState::assumeInBound(), we are prototyping
  59. // some new logic here that reasons directly about memory region extents.
  60. // Once that logic is more mature, we can bring it back to assumeInBound()
  61. // for all clients to use.
  62. //
  63. // The algorithm we are using here for bounds checking is to see if the
  64. // memory access is within the extent of the base region. Since we
  65. // have some flexibility in defining the base region, we can achieve
  66. // various levels of conservatism in our buffer overflow checking.
  67. const GRState *state = checkerContext.getState();
  68. const GRState *originalState = state;
  69. SValBuilder &svalBuilder = checkerContext.getSValBuilder();
  70. const RegionRawOffsetV2 &rawOffset =
  71. RegionRawOffsetV2::computeOffset(state, svalBuilder, location);
  72. if (!rawOffset.getRegion())
  73. return;
  74. // CHECK LOWER BOUND: Is byteOffset < 0? If so, we are doing a load/store
  75. // before the first valid offset in the memory region.
  76. SVal lowerBound
  77. = svalBuilder.evalBinOpNN(state, BO_LT, rawOffset.getByteOffset(),
  78. svalBuilder.makeZeroArrayIndex(),
  79. svalBuilder.getConditionType());
  80. NonLoc *lowerBoundToCheck = dyn_cast<NonLoc>(&lowerBound);
  81. if (!lowerBoundToCheck)
  82. return;
  83. const GRState *state_precedesLowerBound, *state_withinLowerBound;
  84. llvm::tie(state_precedesLowerBound, state_withinLowerBound) =
  85. state->assume(*lowerBoundToCheck);
  86. // Are we constrained enough to definitely precede the lower bound?
  87. if (state_precedesLowerBound && !state_withinLowerBound) {
  88. reportOOB(checkerContext, state_precedesLowerBound, OOB_Precedes);
  89. return;
  90. }
  91. // Otherwise, assume the constraint of the lower bound.
  92. assert(state_withinLowerBound);
  93. state = state_withinLowerBound;
  94. do {
  95. // CHECK UPPER BOUND: Is byteOffset >= extent(baseRegion)? If so,
  96. // we are doing a load/store after the last valid offset.
  97. DefinedOrUnknownSVal extentVal =
  98. rawOffset.getRegion()->getExtent(svalBuilder);
  99. if (!isa<NonLoc>(extentVal))
  100. break;
  101. SVal upperbound
  102. = svalBuilder.evalBinOpNN(state, BO_GE, rawOffset.getByteOffset(),
  103. cast<NonLoc>(extentVal),
  104. svalBuilder.getConditionType());
  105. NonLoc *upperboundToCheck = dyn_cast<NonLoc>(&upperbound);
  106. if (!upperboundToCheck)
  107. break;
  108. const GRState *state_exceedsUpperBound, *state_withinUpperBound;
  109. llvm::tie(state_exceedsUpperBound, state_withinUpperBound) =
  110. state->assume(*upperboundToCheck);
  111. // Are we constrained enough to definitely exceed the upper bound?
  112. if (state_exceedsUpperBound && !state_withinUpperBound) {
  113. reportOOB(checkerContext, state_exceedsUpperBound, OOB_Excedes);
  114. return;
  115. }
  116. assert(state_withinUpperBound);
  117. state = state_withinUpperBound;
  118. }
  119. while (false);
  120. if (state != originalState)
  121. checkerContext.generateNode(state);
  122. }
  123. void ArrayBoundCheckerV2::reportOOB(CheckerContext &checkerContext,
  124. const GRState *errorState,
  125. OOB_Kind kind) {
  126. ExplodedNode *errorNode = checkerContext.generateSink(errorState);
  127. if (!errorNode)
  128. return;
  129. if (!BT)
  130. BT = new BuiltinBug("Out-of-bound access");
  131. // FIXME: This diagnostics are preliminary. We should get far better
  132. // diagnostics for explaining buffer overruns.
  133. llvm::SmallString<256> buf;
  134. llvm::raw_svector_ostream os(buf);
  135. os << "Out of bound memory access "
  136. << (kind == OOB_Precedes ? "(accessed memory precedes memory block)"
  137. : "(access exceeds upper limit of memory block)");
  138. checkerContext.EmitReport(new RangedBugReport(*BT, os.str(), errorNode));
  139. }
  140. void RegionRawOffsetV2::dump() const {
  141. dumpToStream(llvm::errs());
  142. }
  143. void RegionRawOffsetV2::dumpToStream(llvm::raw_ostream& os) const {
  144. os << "raw_offset_v2{" << getRegion() << ',' << getByteOffset() << '}';
  145. }
  146. // FIXME: Merge with the implementation of the same method in Store.cpp
  147. static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
  148. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  149. const RecordDecl *D = RT->getDecl();
  150. if (!D->getDefinition())
  151. return false;
  152. }
  153. return true;
  154. }
  155. // Lazily computes a value to be used by 'computeOffset'. If 'val'
  156. // is unknown or undefined, we lazily substitute '0'. Otherwise,
  157. // return 'val'.
  158. static inline SVal getValue(SVal val, SValBuilder &svalBuilder) {
  159. return isa<UndefinedVal>(val) ? svalBuilder.makeArrayIndex(0) : val;
  160. }
  161. // Scale a base value by a scaling factor, and return the scaled
  162. // value as an SVal. Used by 'computeOffset'.
  163. static inline SVal scaleValue(const GRState *state,
  164. NonLoc baseVal, CharUnits scaling,
  165. SValBuilder &sb) {
  166. return sb.evalBinOpNN(state, BO_Mul, baseVal,
  167. sb.makeArrayIndex(scaling.getQuantity()),
  168. sb.getArrayIndexType());
  169. }
  170. // Add an SVal to another, treating unknown and undefined values as
  171. // summing to UnknownVal. Used by 'computeOffset'.
  172. static SVal addValue(const GRState *state, SVal x, SVal y,
  173. SValBuilder &svalBuilder) {
  174. // We treat UnknownVals and UndefinedVals the same here because we
  175. // only care about computing offsets.
  176. if (x.isUnknownOrUndef() || y.isUnknownOrUndef())
  177. return UnknownVal();
  178. return svalBuilder.evalBinOpNN(state, BO_Add,
  179. cast<NonLoc>(x), cast<NonLoc>(y),
  180. svalBuilder.getArrayIndexType());
  181. }
  182. /// Compute a raw byte offset from a base region. Used for array bounds
  183. /// checking.
  184. RegionRawOffsetV2 RegionRawOffsetV2::computeOffset(const GRState *state,
  185. SValBuilder &svalBuilder,
  186. SVal location)
  187. {
  188. const MemRegion *region = location.getAsRegion();
  189. SVal offset = UndefinedVal();
  190. while (region) {
  191. switch (region->getKind()) {
  192. default: {
  193. if (const SubRegion *subReg = dyn_cast<SubRegion>(region))
  194. if (!offset.isUnknownOrUndef())
  195. return RegionRawOffsetV2(subReg, offset);
  196. return RegionRawOffsetV2();
  197. }
  198. case MemRegion::ElementRegionKind: {
  199. const ElementRegion *elemReg = cast<ElementRegion>(region);
  200. SVal index = elemReg->getIndex();
  201. if (!isa<NonLoc>(index))
  202. return RegionRawOffsetV2();
  203. QualType elemType = elemReg->getElementType();
  204. // If the element is an incomplete type, go no further.
  205. ASTContext &astContext = svalBuilder.getContext();
  206. if (!IsCompleteType(astContext, elemType))
  207. return RegionRawOffsetV2();
  208. // Update the offset.
  209. offset = addValue(state,
  210. getValue(offset, svalBuilder),
  211. scaleValue(state,
  212. cast<NonLoc>(index),
  213. astContext.getTypeSizeInChars(elemType),
  214. svalBuilder),
  215. svalBuilder);
  216. if (offset.isUnknownOrUndef())
  217. return RegionRawOffsetV2();
  218. region = elemReg->getSuperRegion();
  219. continue;
  220. }
  221. }
  222. }
  223. return RegionRawOffsetV2();
  224. }