MallocSizeofChecker.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // MallocSizeofChecker.cpp - Check for dubious malloc arguments ---*- 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. // Reports inconsistencies between the casted type of the return value of a
  11. // malloc/calloc/realloc call and the operand of any sizeof expressions
  12. // contained within its argument(s).
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "ClangSACheckers.h"
  16. #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
  17. #include "clang/StaticAnalyzer/Core/Checker.h"
  18. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  19. #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
  20. #include "clang/AST/StmtVisitor.h"
  21. #include "clang/AST/TypeLoc.h"
  22. #include "llvm/ADT/SmallString.h"
  23. using namespace clang;
  24. using namespace ento;
  25. namespace {
  26. typedef std::pair<const TypeSourceInfo *, const CallExpr *> TypeCallPair;
  27. typedef llvm::PointerUnion<const Stmt *, const VarDecl *> ExprParent;
  28. class CastedAllocFinder
  29. : public ConstStmtVisitor<CastedAllocFinder, TypeCallPair> {
  30. IdentifierInfo *II_malloc, *II_calloc, *II_realloc;
  31. public:
  32. struct CallRecord {
  33. ExprParent CastedExprParent;
  34. const Expr *CastedExpr;
  35. const TypeSourceInfo *ExplicitCastType;
  36. const CallExpr *AllocCall;
  37. CallRecord(ExprParent CastedExprParent, const Expr *CastedExpr,
  38. const TypeSourceInfo *ExplicitCastType,
  39. const CallExpr *AllocCall)
  40. : CastedExprParent(CastedExprParent), CastedExpr(CastedExpr),
  41. ExplicitCastType(ExplicitCastType), AllocCall(AllocCall) {}
  42. };
  43. typedef std::vector<CallRecord> CallVec;
  44. CallVec Calls;
  45. CastedAllocFinder(ASTContext *Ctx) :
  46. II_malloc(&Ctx->Idents.get("malloc")),
  47. II_calloc(&Ctx->Idents.get("calloc")),
  48. II_realloc(&Ctx->Idents.get("realloc")) {}
  49. void VisitChild(ExprParent Parent, const Stmt *S) {
  50. TypeCallPair AllocCall = Visit(S);
  51. if (AllocCall.second && AllocCall.second != S)
  52. Calls.push_back(CallRecord(Parent, cast<Expr>(S), AllocCall.first,
  53. AllocCall.second));
  54. }
  55. void VisitChildren(const Stmt *S) {
  56. for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
  57. I!=E; ++I)
  58. if (const Stmt *child = *I)
  59. VisitChild(S, child);
  60. }
  61. TypeCallPair VisitCastExpr(const CastExpr *E) {
  62. return Visit(E->getSubExpr());
  63. }
  64. TypeCallPair VisitExplicitCastExpr(const ExplicitCastExpr *E) {
  65. return TypeCallPair(E->getTypeInfoAsWritten(),
  66. Visit(E->getSubExpr()).second);
  67. }
  68. TypeCallPair VisitParenExpr(const ParenExpr *E) {
  69. return Visit(E->getSubExpr());
  70. }
  71. TypeCallPair VisitStmt(const Stmt *S) {
  72. VisitChildren(S);
  73. return TypeCallPair();
  74. }
  75. TypeCallPair VisitCallExpr(const CallExpr *E) {
  76. VisitChildren(E);
  77. const FunctionDecl *FD = E->getDirectCallee();
  78. if (FD) {
  79. IdentifierInfo *II = FD->getIdentifier();
  80. if (II == II_malloc || II == II_calloc || II == II_realloc)
  81. return TypeCallPair((const TypeSourceInfo *)0, E);
  82. }
  83. return TypeCallPair();
  84. }
  85. TypeCallPair VisitDeclStmt(const DeclStmt *S) {
  86. for (DeclStmt::const_decl_iterator I = S->decl_begin(), E = S->decl_end();
  87. I!=E; ++I)
  88. if (const VarDecl *VD = dyn_cast<VarDecl>(*I))
  89. if (const Expr *Init = VD->getInit())
  90. VisitChild(VD, Init);
  91. return TypeCallPair();
  92. }
  93. };
  94. class SizeofFinder : public ConstStmtVisitor<SizeofFinder> {
  95. public:
  96. std::vector<const UnaryExprOrTypeTraitExpr *> Sizeofs;
  97. void VisitBinMul(const BinaryOperator *E) {
  98. Visit(E->getLHS());
  99. Visit(E->getRHS());
  100. }
  101. void VisitBinAdd(const BinaryOperator *E) {
  102. Visit(E->getLHS());
  103. Visit(E->getRHS());
  104. }
  105. void VisitImplicitCastExpr(const ImplicitCastExpr *E) {
  106. return Visit(E->getSubExpr());
  107. }
  108. void VisitParenExpr(const ParenExpr *E) {
  109. return Visit(E->getSubExpr());
  110. }
  111. void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E) {
  112. if (E->getKind() != UETT_SizeOf)
  113. return;
  114. Sizeofs.push_back(E);
  115. }
  116. };
  117. class MallocSizeofChecker : public Checker<check::ASTCodeBody> {
  118. public:
  119. void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
  120. BugReporter &BR) const {
  121. AnalysisDeclContext *ADC = mgr.getAnalysisDeclContext(D);
  122. CastedAllocFinder Finder(&BR.getContext());
  123. Finder.Visit(D->getBody());
  124. for (CastedAllocFinder::CallVec::iterator i = Finder.Calls.begin(),
  125. e = Finder.Calls.end(); i != e; ++i) {
  126. QualType CastedType = i->CastedExpr->getType();
  127. if (!CastedType->isPointerType())
  128. continue;
  129. QualType PointeeType = CastedType->getAs<PointerType>()->getPointeeType();
  130. if (PointeeType->isVoidType())
  131. continue;
  132. for (CallExpr::const_arg_iterator ai = i->AllocCall->arg_begin(),
  133. ae = i->AllocCall->arg_end(); ai != ae; ++ai) {
  134. if (!(*ai)->getType()->isIntegerType())
  135. continue;
  136. SizeofFinder SFinder;
  137. SFinder.Visit(*ai);
  138. if (SFinder.Sizeofs.size() != 1)
  139. continue;
  140. QualType SizeofType = SFinder.Sizeofs[0]->getTypeOfArgument();
  141. if (!BR.getContext().hasSameUnqualifiedType(PointeeType, SizeofType)) {
  142. const TypeSourceInfo *TSI = 0;
  143. if (i->CastedExprParent.is<const VarDecl *>()) {
  144. TSI =
  145. i->CastedExprParent.get<const VarDecl *>()->getTypeSourceInfo();
  146. } else {
  147. TSI = i->ExplicitCastType;
  148. }
  149. SmallString<64> buf;
  150. llvm::raw_svector_ostream OS(buf);
  151. OS << "Result of '"
  152. << i->AllocCall->getDirectCallee()->getIdentifier()->getName()
  153. << "' is converted to type '"
  154. << CastedType.getAsString() << "', whose pointee type '"
  155. << PointeeType.getAsString() << "' is incompatible with "
  156. << "sizeof operand type '" << SizeofType.getAsString() << "'";
  157. llvm::SmallVector<SourceRange, 4> Ranges;
  158. Ranges.push_back(i->AllocCall->getCallee()->getSourceRange());
  159. Ranges.push_back(SFinder.Sizeofs[0]->getSourceRange());
  160. if (TSI)
  161. Ranges.push_back(TSI->getTypeLoc().getSourceRange());
  162. PathDiagnosticLocation L =
  163. PathDiagnosticLocation::createBegin(i->AllocCall->getCallee(),
  164. BR.getSourceManager(), ADC);
  165. BR.EmitBasicReport("allocator sizeof operand mismatch", OS.str(), L,
  166. Ranges.data(), Ranges.size());
  167. }
  168. }
  169. }
  170. }
  171. };
  172. }
  173. void ento::registerMallocSizeofChecker(CheckerManager &mgr) {
  174. mgr.registerChecker<MallocSizeofChecker>();
  175. }