VLASizeChecker.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.generateErrorNode(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. }
  72. void VLASizeChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
  73. if (!DS->isSingleDecl())
  74. return;
  75. const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
  76. if (!VD)
  77. return;
  78. ASTContext &Ctx = C.getASTContext();
  79. const VariableArrayType *VLA = Ctx.getAsVariableArrayType(VD->getType());
  80. if (!VLA)
  81. return;
  82. // FIXME: Handle multi-dimensional VLAs.
  83. const Expr *SE = VLA->getSizeExpr();
  84. ProgramStateRef state = C.getState();
  85. SVal sizeV = C.getSVal(SE);
  86. if (sizeV.isUndef()) {
  87. reportBug(VLA_Garbage, SE, state, C);
  88. return;
  89. }
  90. // See if the size value is known. It can't be undefined because we would have
  91. // warned about that already.
  92. if (sizeV.isUnknown())
  93. return;
  94. // Check if the size is tainted.
  95. if (state->isTainted(sizeV)) {
  96. reportBug(VLA_Tainted, SE, nullptr, C);
  97. return;
  98. }
  99. // Check if the size is zero.
  100. DefinedSVal sizeD = sizeV.castAs<DefinedSVal>();
  101. ProgramStateRef stateNotZero, stateZero;
  102. std::tie(stateNotZero, stateZero) = state->assume(sizeD);
  103. if (stateZero && !stateNotZero) {
  104. reportBug(VLA_Zero, SE, stateZero, C);
  105. return;
  106. }
  107. // From this point on, assume that the size is not zero.
  108. state = stateNotZero;
  109. // VLASizeChecker is responsible for defining the extent of the array being
  110. // declared. We do this by multiplying the array length by the element size,
  111. // then matching that with the array region's extent symbol.
  112. // Check if the size is negative.
  113. SValBuilder &svalBuilder = C.getSValBuilder();
  114. QualType Ty = SE->getType();
  115. DefinedOrUnknownSVal Zero = svalBuilder.makeZeroVal(Ty);
  116. SVal LessThanZeroVal = svalBuilder.evalBinOp(state, BO_LT, sizeD, Zero, Ty);
  117. if (Optional<DefinedSVal> LessThanZeroDVal =
  118. LessThanZeroVal.getAs<DefinedSVal>()) {
  119. ConstraintManager &CM = C.getConstraintManager();
  120. ProgramStateRef StatePos, StateNeg;
  121. std::tie(StateNeg, StatePos) = CM.assumeDual(state, *LessThanZeroDVal);
  122. if (StateNeg && !StatePos) {
  123. reportBug(VLA_Negative, SE, state, C);
  124. return;
  125. }
  126. state = StatePos;
  127. }
  128. // Convert the array length to size_t.
  129. QualType SizeTy = Ctx.getSizeType();
  130. NonLoc ArrayLength =
  131. svalBuilder.evalCast(sizeD, SizeTy, SE->getType()).castAs<NonLoc>();
  132. // Get the element size.
  133. CharUnits EleSize = Ctx.getTypeSizeInChars(VLA->getElementType());
  134. SVal EleSizeVal = svalBuilder.makeIntVal(EleSize.getQuantity(), SizeTy);
  135. // Multiply the array length by the element size.
  136. SVal ArraySizeVal = svalBuilder.evalBinOpNN(
  137. state, BO_Mul, ArrayLength, EleSizeVal.castAs<NonLoc>(), SizeTy);
  138. // Finally, assume that the array's extent matches the given size.
  139. const LocationContext *LC = C.getLocationContext();
  140. DefinedOrUnknownSVal Extent =
  141. state->getRegion(VD, LC)->getExtent(svalBuilder);
  142. DefinedOrUnknownSVal ArraySize = ArraySizeVal.castAs<DefinedOrUnknownSVal>();
  143. DefinedOrUnknownSVal sizeIsKnown =
  144. svalBuilder.evalEQ(state, Extent, ArraySize);
  145. state = state->assume(sizeIsKnown, true);
  146. // Assume should not fail at this point.
  147. assert(state);
  148. // Remember our assumptions!
  149. C.addTransition(state);
  150. }
  151. void ento::registerVLASizeChecker(CheckerManager &mgr) {
  152. mgr.registerChecker<VLASizeChecker>();
  153. }