VLASizeChecker.cpp 5.1 KB

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