VLASizeChecker.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //=== VLASizeChecker.cpp - Undefined dereference checker --------*- 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 defines VLASizeChecker, a builtin check in ExprEngine that
  11. // performs checks for declaration of VLA of undefined or zero size.
  12. // In addition, VLASizeChecker is responsible for defining the extent
  13. // of the MemRegion that represents a VLA.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "ClangSACheckers.h"
  17. #include "clang/AST/CharUnits.h"
  18. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  19. #include "clang/StaticAnalyzer/Core/Checker.h"
  20. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  21. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  22. #include "llvm/ADT/STLExtras.h"
  23. #include "llvm/ADT/SmallString.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. using namespace clang;
  26. using namespace ento;
  27. namespace {
  28. class VLASizeChecker : public Checker< check::PreStmt<DeclStmt> > {
  29. mutable std::unique_ptr<BugType> BT;
  30. enum VLASize_Kind { VLA_Garbage, VLA_Zero, VLA_Tainted, VLA_Negative };
  31. void reportBug(VLASize_Kind Kind,
  32. const Expr *SizeE,
  33. ProgramStateRef State,
  34. CheckerContext &C) const;
  35. public:
  36. void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
  37. };
  38. } // end anonymous namespace
  39. void VLASizeChecker::reportBug(VLASize_Kind Kind,
  40. const Expr *SizeE,
  41. ProgramStateRef State,
  42. CheckerContext &C) const {
  43. // Generate an error node.
  44. ExplodedNode *N = C.generateSink(State);
  45. if (!N)
  46. return;
  47. if (!BT)
  48. BT.reset(new BuiltinBug(
  49. this, "Dangerous variable-length array (VLA) declaration"));
  50. SmallString<256> buf;
  51. llvm::raw_svector_ostream os(buf);
  52. os << "Declared variable-length array (VLA) ";
  53. switch (Kind) {
  54. case VLA_Garbage:
  55. os << "uses a garbage value as its size";
  56. break;
  57. case VLA_Zero:
  58. os << "has zero size";
  59. break;
  60. case VLA_Tainted:
  61. os << "has tainted size";
  62. break;
  63. case VLA_Negative:
  64. os << "has negative size";
  65. break;
  66. }
  67. auto report = llvm::make_unique<BugReport>(*BT, os.str(), N);
  68. report->addRange(SizeE->getSourceRange());
  69. bugreporter::trackNullOrUndefValue(N, SizeE, *report);
  70. C.emitReport(std::move(report));
  71. return;
  72. }
  73. void VLASizeChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
  74. if (!DS->isSingleDecl())
  75. return;
  76. const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
  77. if (!VD)
  78. return;
  79. ASTContext &Ctx = C.getASTContext();
  80. const VariableArrayType *VLA = Ctx.getAsVariableArrayType(VD->getType());
  81. if (!VLA)
  82. return;
  83. // FIXME: Handle multi-dimensional VLAs.
  84. const Expr *SE = VLA->getSizeExpr();
  85. ProgramStateRef state = C.getState();
  86. SVal sizeV = state->getSVal(SE, C.getLocationContext());
  87. if (sizeV.isUndef()) {
  88. reportBug(VLA_Garbage, SE, state, C);
  89. return;
  90. }
  91. // See if the size value is known. It can't be undefined because we would have
  92. // warned about that already.
  93. if (sizeV.isUnknown())
  94. return;
  95. // Check if the size is tainted.
  96. if (state->isTainted(sizeV)) {
  97. reportBug(VLA_Tainted, SE, nullptr, C);
  98. return;
  99. }
  100. // Check if the size is zero.
  101. DefinedSVal sizeD = sizeV.castAs<DefinedSVal>();
  102. ProgramStateRef stateNotZero, stateZero;
  103. std::tie(stateNotZero, stateZero) = state->assume(sizeD);
  104. if (stateZero && !stateNotZero) {
  105. reportBug(VLA_Zero, SE, stateZero, C);
  106. return;
  107. }
  108. // From this point on, assume that the size is not zero.
  109. state = stateNotZero;
  110. // VLASizeChecker is responsible for defining the extent of the array being
  111. // declared. We do this by multiplying the array length by the element size,
  112. // then matching that with the array region's extent symbol.
  113. // Check if the size is negative.
  114. SValBuilder &svalBuilder = C.getSValBuilder();
  115. QualType Ty = SE->getType();
  116. DefinedOrUnknownSVal Zero = svalBuilder.makeZeroVal(Ty);
  117. SVal LessThanZeroVal = svalBuilder.evalBinOp(state, BO_LT, sizeD, Zero, Ty);
  118. if (Optional<DefinedSVal> LessThanZeroDVal =
  119. LessThanZeroVal.getAs<DefinedSVal>()) {
  120. ConstraintManager &CM = C.getConstraintManager();
  121. ProgramStateRef StatePos, StateNeg;
  122. std::tie(StateNeg, StatePos) = CM.assumeDual(state, *LessThanZeroDVal);
  123. if (StateNeg && !StatePos) {
  124. reportBug(VLA_Negative, SE, state, C);
  125. return;
  126. }
  127. state = StatePos;
  128. }
  129. // Convert the array length to size_t.
  130. QualType SizeTy = Ctx.getSizeType();
  131. NonLoc ArrayLength =
  132. svalBuilder.evalCast(sizeD, SizeTy, SE->getType()).castAs<NonLoc>();
  133. // Get the element size.
  134. CharUnits EleSize = Ctx.getTypeSizeInChars(VLA->getElementType());
  135. SVal EleSizeVal = svalBuilder.makeIntVal(EleSize.getQuantity(), SizeTy);
  136. // Multiply the array length by the element size.
  137. SVal ArraySizeVal = svalBuilder.evalBinOpNN(
  138. state, BO_Mul, ArrayLength, EleSizeVal.castAs<NonLoc>(), SizeTy);
  139. // Finally, assume that the array's extent matches the given size.
  140. const LocationContext *LC = C.getLocationContext();
  141. DefinedOrUnknownSVal Extent =
  142. state->getRegion(VD, LC)->getExtent(svalBuilder);
  143. DefinedOrUnknownSVal ArraySize = ArraySizeVal.castAs<DefinedOrUnknownSVal>();
  144. DefinedOrUnknownSVal sizeIsKnown =
  145. svalBuilder.evalEQ(state, Extent, ArraySize);
  146. state = state->assume(sizeIsKnown, true);
  147. // Assume should not fail at this point.
  148. assert(state);
  149. // Remember our assumptions!
  150. C.addTransition(state);
  151. }
  152. void ento::registerVLASizeChecker(CheckerManager &mgr) {
  153. mgr.registerChecker<VLASizeChecker>();
  154. }