1 // MallocSizeofChecker.cpp - Check for dubious malloc arguments ---*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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).
14 //===----------------------------------------------------------------------===//
16 #include "ClangSACheckers.h"
17 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
18 #include "clang/StaticAnalyzer/Core/Checker.h"
19 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
21 #include "clang/AST/StmtVisitor.h"
22 #include "clang/AST/TypeLoc.h"
23 #include "llvm/ADT/SmallString.h"
25 using namespace clang;
30 typedef std::pair<const TypeSourceInfo *, const CallExpr *> TypeCallPair;
31 typedef llvm::PointerUnion<const Stmt *, const VarDecl *> ExprParent;
33 class CastedAllocFinder
34 : public ConstStmtVisitor<CastedAllocFinder, TypeCallPair> {
35 IdentifierInfo *II_malloc, *II_calloc, *II_realloc;
39 ExprParent CastedExprParent;
40 const Expr *CastedExpr;
41 const TypeSourceInfo *ExplicitCastType;
42 const CallExpr *AllocCall;
44 CallRecord(ExprParent CastedExprParent, const Expr *CastedExpr,
45 const TypeSourceInfo *ExplicitCastType,
46 const CallExpr *AllocCall)
47 : CastedExprParent(CastedExprParent), CastedExpr(CastedExpr),
48 ExplicitCastType(ExplicitCastType), AllocCall(AllocCall) {}
51 typedef std::vector<CallRecord> CallVec;
54 CastedAllocFinder(ASTContext *Ctx) :
55 II_malloc(&Ctx->Idents.get("malloc")),
56 II_calloc(&Ctx->Idents.get("calloc")),
57 II_realloc(&Ctx->Idents.get("realloc")) {}
59 void VisitChild(ExprParent Parent, const Stmt *S) {
60 TypeCallPair AllocCall = Visit(S);
61 if (AllocCall.second && AllocCall.second != S)
62 Calls.push_back(CallRecord(Parent, cast<Expr>(S), AllocCall.first,
66 void VisitChildren(const Stmt *S) {
67 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
69 if (const Stmt *child = *I)
73 TypeCallPair VisitCastExpr(const CastExpr *E) {
74 return Visit(E->getSubExpr());
77 TypeCallPair VisitExplicitCastExpr(const ExplicitCastExpr *E) {
78 return TypeCallPair(E->getTypeInfoAsWritten(),
79 Visit(E->getSubExpr()).second);
82 TypeCallPair VisitParenExpr(const ParenExpr *E) {
83 return Visit(E->getSubExpr());
86 TypeCallPair VisitStmt(const Stmt *S) {
88 return TypeCallPair();
91 TypeCallPair VisitCallExpr(const CallExpr *E) {
93 const FunctionDecl *FD = E->getDirectCallee();
95 IdentifierInfo *II = FD->getIdentifier();
96 if (II == II_malloc || II == II_calloc || II == II_realloc)
97 return TypeCallPair((const TypeSourceInfo *)0, E);
99 return TypeCallPair();
102 TypeCallPair VisitDeclStmt(const DeclStmt *S) {
103 for (DeclStmt::const_decl_iterator I = S->decl_begin(), E = S->decl_end();
105 if (const VarDecl *VD = dyn_cast<VarDecl>(*I))
106 if (const Expr *Init = VD->getInit())
107 VisitChild(VD, Init);
108 return TypeCallPair();
112 class SizeofFinder : public ConstStmtVisitor<SizeofFinder> {
114 std::vector<const UnaryExprOrTypeTraitExpr *> Sizeofs;
116 void VisitBinMul(const BinaryOperator *E) {
121 void VisitBinAdd(const BinaryOperator *E) {
126 void VisitImplicitCastExpr(const ImplicitCastExpr *E) {
127 return Visit(E->getSubExpr());
130 void VisitParenExpr(const ParenExpr *E) {
131 return Visit(E->getSubExpr());
134 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E) {
135 if (E->getKind() != UETT_SizeOf)
138 Sizeofs.push_back(E);
142 class MallocSizeofChecker : public Checker<check::ASTCodeBody> {
144 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
145 BugReporter &BR) const {
146 AnalysisDeclContext *ADC = mgr.getAnalysisDeclContext(D);
147 CastedAllocFinder Finder(&BR.getContext());
148 Finder.Visit(D->getBody());
149 for (CastedAllocFinder::CallVec::iterator i = Finder.Calls.begin(),
150 e = Finder.Calls.end(); i != e; ++i) {
151 QualType CastedType = i->CastedExpr->getType();
152 if (!CastedType->isPointerType())
154 QualType PointeeType = CastedType->getAs<PointerType>()->getPointeeType();
155 if (PointeeType->isVoidType())
158 for (CallExpr::const_arg_iterator ai = i->AllocCall->arg_begin(),
159 ae = i->AllocCall->arg_end(); ai != ae; ++ai) {
160 if (!(*ai)->getType()->isIntegerType())
163 SizeofFinder SFinder;
165 if (SFinder.Sizeofs.size() != 1)
168 QualType SizeofType = SFinder.Sizeofs[0]->getTypeOfArgument();
169 if (!BR.getContext().hasSameUnqualifiedType(PointeeType, SizeofType)) {
170 const TypeSourceInfo *TSI = 0;
171 if (i->CastedExprParent.is<const VarDecl *>()) {
173 i->CastedExprParent.get<const VarDecl *>()->getTypeSourceInfo();
175 TSI = i->ExplicitCastType;
179 llvm::raw_svector_ostream OS(buf);
182 << i->AllocCall->getDirectCallee()->getIdentifier()->getName()
183 << "' is converted to type '"
184 << CastedType.getAsString() << "', whose pointee type '"
185 << PointeeType.getAsString() << "' is incompatible with "
186 << "sizeof operand type '" << SizeofType.getAsString() << "'";
187 llvm::SmallVector<SourceRange, 4> Ranges;
188 Ranges.push_back(i->AllocCall->getCallee()->getSourceRange());
189 Ranges.push_back(SFinder.Sizeofs[0]->getSourceRange());
191 Ranges.push_back(TSI->getTypeLoc().getSourceRange());
193 PathDiagnosticLocation L =
194 PathDiagnosticLocation::createBegin(i->AllocCall->getCallee(),
195 BR.getSourceManager(), ADC);
197 BR.EmitBasicReport(D, "allocator sizeof operand mismatch",
200 L, Ranges.data(), Ranges.size());
209 void ento::registerMallocSizeofChecker(CheckerManager &mgr) {
210 mgr.registerChecker<MallocSizeofChecker>();