Peter Collingbourne | 266e3dd | 2011-12-08 08:31:14 +0000 | [diff] [blame] | 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 | |
| 16 | #include "ClangSACheckers.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
| 18 | #include "clang/AST/TypeLoc.h" |
Peter Collingbourne | 266e3dd | 2011-12-08 08:31:14 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
| 20 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 21 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Peter Collingbourne | 266e3dd | 2011-12-08 08:31:14 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
| 27 | using namespace ento; |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | typedef std::pair<const TypeSourceInfo *, const CallExpr *> TypeCallPair; |
| 32 | typedef llvm::PointerUnion<const Stmt *, const VarDecl *> ExprParent; |
| 33 | |
| 34 | class CastedAllocFinder |
| 35 | : public ConstStmtVisitor<CastedAllocFinder, TypeCallPair> { |
| 36 | IdentifierInfo *II_malloc, *II_calloc, *II_realloc; |
| 37 | |
| 38 | public: |
| 39 | struct CallRecord { |
| 40 | ExprParent CastedExprParent; |
| 41 | const Expr *CastedExpr; |
| 42 | const TypeSourceInfo *ExplicitCastType; |
| 43 | const CallExpr *AllocCall; |
| 44 | |
| 45 | CallRecord(ExprParent CastedExprParent, const Expr *CastedExpr, |
| 46 | const TypeSourceInfo *ExplicitCastType, |
| 47 | const CallExpr *AllocCall) |
| 48 | : CastedExprParent(CastedExprParent), CastedExpr(CastedExpr), |
| 49 | ExplicitCastType(ExplicitCastType), AllocCall(AllocCall) {} |
| 50 | }; |
| 51 | |
| 52 | typedef std::vector<CallRecord> CallVec; |
| 53 | CallVec Calls; |
| 54 | |
| 55 | CastedAllocFinder(ASTContext *Ctx) : |
| 56 | II_malloc(&Ctx->Idents.get("malloc")), |
| 57 | II_calloc(&Ctx->Idents.get("calloc")), |
| 58 | II_realloc(&Ctx->Idents.get("realloc")) {} |
| 59 | |
| 60 | void VisitChild(ExprParent Parent, const Stmt *S) { |
| 61 | TypeCallPair AllocCall = Visit(S); |
| 62 | if (AllocCall.second && AllocCall.second != S) |
| 63 | Calls.push_back(CallRecord(Parent, cast<Expr>(S), AllocCall.first, |
| 64 | AllocCall.second)); |
| 65 | } |
| 66 | |
| 67 | void VisitChildren(const Stmt *S) { |
| 68 | for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); |
| 69 | I!=E; ++I) |
| 70 | if (const Stmt *child = *I) |
| 71 | VisitChild(S, child); |
| 72 | } |
| 73 | |
| 74 | TypeCallPair VisitCastExpr(const CastExpr *E) { |
| 75 | return Visit(E->getSubExpr()); |
| 76 | } |
| 77 | |
| 78 | TypeCallPair VisitExplicitCastExpr(const ExplicitCastExpr *E) { |
| 79 | return TypeCallPair(E->getTypeInfoAsWritten(), |
| 80 | Visit(E->getSubExpr()).second); |
| 81 | } |
| 82 | |
| 83 | TypeCallPair VisitParenExpr(const ParenExpr *E) { |
| 84 | return Visit(E->getSubExpr()); |
| 85 | } |
| 86 | |
| 87 | TypeCallPair VisitStmt(const Stmt *S) { |
| 88 | VisitChildren(S); |
| 89 | return TypeCallPair(); |
| 90 | } |
| 91 | |
| 92 | TypeCallPair VisitCallExpr(const CallExpr *E) { |
| 93 | VisitChildren(E); |
| 94 | const FunctionDecl *FD = E->getDirectCallee(); |
| 95 | if (FD) { |
| 96 | IdentifierInfo *II = FD->getIdentifier(); |
| 97 | if (II == II_malloc || II == II_calloc || II == II_realloc) |
Francois Pichet | 3110847 | 2011-12-08 09:32:22 +0000 | [diff] [blame] | 98 | return TypeCallPair((const TypeSourceInfo *)0, E); |
Peter Collingbourne | 266e3dd | 2011-12-08 08:31:14 +0000 | [diff] [blame] | 99 | } |
| 100 | return TypeCallPair(); |
| 101 | } |
| 102 | |
| 103 | TypeCallPair VisitDeclStmt(const DeclStmt *S) { |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame^] | 104 | for (const auto *I : S->decls()) |
| 105 | if (const VarDecl *VD = dyn_cast<VarDecl>(I)) |
Peter Collingbourne | 266e3dd | 2011-12-08 08:31:14 +0000 | [diff] [blame] | 106 | if (const Expr *Init = VD->getInit()) |
| 107 | VisitChild(VD, Init); |
| 108 | return TypeCallPair(); |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | class SizeofFinder : public ConstStmtVisitor<SizeofFinder> { |
| 113 | public: |
| 114 | std::vector<const UnaryExprOrTypeTraitExpr *> Sizeofs; |
| 115 | |
| 116 | void VisitBinMul(const BinaryOperator *E) { |
| 117 | Visit(E->getLHS()); |
| 118 | Visit(E->getRHS()); |
| 119 | } |
| 120 | |
Peter Collingbourne | 266e3dd | 2011-12-08 08:31:14 +0000 | [diff] [blame] | 121 | void VisitImplicitCastExpr(const ImplicitCastExpr *E) { |
| 122 | return Visit(E->getSubExpr()); |
| 123 | } |
| 124 | |
| 125 | void VisitParenExpr(const ParenExpr *E) { |
| 126 | return Visit(E->getSubExpr()); |
| 127 | } |
| 128 | |
| 129 | void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E) { |
| 130 | if (E->getKind() != UETT_SizeOf) |
| 131 | return; |
| 132 | |
| 133 | Sizeofs.push_back(E); |
| 134 | } |
| 135 | }; |
| 136 | |
Ted Kremenek | ad8cd30 | 2012-05-01 00:10:19 +0000 | [diff] [blame] | 137 | // Determine if the pointee and sizeof types are compatible. Here |
| 138 | // we ignore constness of pointer types. |
| 139 | static bool typesCompatible(ASTContext &C, QualType A, QualType B) { |
| 140 | while (true) { |
| 141 | A = A.getCanonicalType(); |
| 142 | B = B.getCanonicalType(); |
| 143 | |
| 144 | if (A.getTypePtr() == B.getTypePtr()) |
| 145 | return true; |
| 146 | |
| 147 | if (const PointerType *ptrA = A->getAs<PointerType>()) |
| 148 | if (const PointerType *ptrB = B->getAs<PointerType>()) { |
Ted Kremenek | a065879 | 2012-09-04 22:48:59 +0000 | [diff] [blame] | 149 | A = ptrA->getPointeeType(); |
| 150 | B = ptrB->getPointeeType(); |
| 151 | continue; |
Ted Kremenek | ad8cd30 | 2012-05-01 00:10:19 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | return false; |
| 158 | } |
| 159 | |
Anna Zaks | 044f3e2 | 2012-09-08 00:09:02 +0000 | [diff] [blame] | 160 | static bool compatibleWithArrayType(ASTContext &C, QualType PT, QualType T) { |
| 161 | // Ex: 'int a[10][2]' is compatible with 'int', 'int[2]', 'int[10][2]'. |
| 162 | while (const ArrayType *AT = T->getAsArrayTypeUnsafe()) { |
| 163 | QualType ElemType = AT->getElementType(); |
| 164 | if (typesCompatible(C, PT, AT->getElementType())) |
| 165 | return true; |
| 166 | T = ElemType; |
| 167 | } |
| 168 | |
| 169 | return false; |
| 170 | } |
| 171 | |
Peter Collingbourne | 266e3dd | 2011-12-08 08:31:14 +0000 | [diff] [blame] | 172 | class MallocSizeofChecker : public Checker<check::ASTCodeBody> { |
| 173 | public: |
| 174 | void checkASTCodeBody(const Decl *D, AnalysisManager& mgr, |
| 175 | BugReporter &BR) const { |
| 176 | AnalysisDeclContext *ADC = mgr.getAnalysisDeclContext(D); |
| 177 | CastedAllocFinder Finder(&BR.getContext()); |
| 178 | Finder.Visit(D->getBody()); |
| 179 | for (CastedAllocFinder::CallVec::iterator i = Finder.Calls.begin(), |
| 180 | e = Finder.Calls.end(); i != e; ++i) { |
| 181 | QualType CastedType = i->CastedExpr->getType(); |
| 182 | if (!CastedType->isPointerType()) |
| 183 | continue; |
| 184 | QualType PointeeType = CastedType->getAs<PointerType>()->getPointeeType(); |
| 185 | if (PointeeType->isVoidType()) |
| 186 | continue; |
| 187 | |
| 188 | for (CallExpr::const_arg_iterator ai = i->AllocCall->arg_begin(), |
| 189 | ae = i->AllocCall->arg_end(); ai != ae; ++ai) { |
Jordan Rose | 61e221f | 2013-04-09 02:30:33 +0000 | [diff] [blame] | 190 | if (!(*ai)->getType()->isIntegralOrUnscopedEnumerationType()) |
Peter Collingbourne | 266e3dd | 2011-12-08 08:31:14 +0000 | [diff] [blame] | 191 | continue; |
| 192 | |
| 193 | SizeofFinder SFinder; |
| 194 | SFinder.Visit(*ai); |
| 195 | if (SFinder.Sizeofs.size() != 1) |
| 196 | continue; |
| 197 | |
| 198 | QualType SizeofType = SFinder.Sizeofs[0]->getTypeOfArgument(); |
Peter Collingbourne | 266e3dd | 2011-12-08 08:31:14 +0000 | [diff] [blame] | 199 | |
Anna Zaks | 694be01 | 2012-09-07 19:20:13 +0000 | [diff] [blame] | 200 | if (typesCompatible(BR.getContext(), PointeeType, SizeofType)) |
| 201 | continue; |
Peter Collingbourne | 266e3dd | 2011-12-08 08:31:14 +0000 | [diff] [blame] | 202 | |
Anna Zaks | 694be01 | 2012-09-07 19:20:13 +0000 | [diff] [blame] | 203 | // If the argument to sizeof is an array, the result could be a |
Anna Zaks | 044f3e2 | 2012-09-08 00:09:02 +0000 | [diff] [blame] | 204 | // pointer to any array element. |
| 205 | if (compatibleWithArrayType(BR.getContext(), PointeeType, SizeofType)) |
| 206 | continue; |
Anna Zaks | 694be01 | 2012-09-07 19:20:13 +0000 | [diff] [blame] | 207 | |
| 208 | const TypeSourceInfo *TSI = 0; |
| 209 | if (i->CastedExprParent.is<const VarDecl *>()) { |
| 210 | TSI = |
| 211 | i->CastedExprParent.get<const VarDecl *>()->getTypeSourceInfo(); |
| 212 | } else { |
| 213 | TSI = i->ExplicitCastType; |
| 214 | } |
| 215 | |
| 216 | SmallString<64> buf; |
| 217 | llvm::raw_svector_ostream OS(buf); |
| 218 | |
| 219 | OS << "Result of "; |
| 220 | const FunctionDecl *Callee = i->AllocCall->getDirectCallee(); |
| 221 | if (Callee && Callee->getIdentifier()) |
| 222 | OS << '\'' << Callee->getIdentifier()->getName() << '\''; |
| 223 | else |
| 224 | OS << "call"; |
| 225 | OS << " is converted to a pointer of type '" |
| 226 | << PointeeType.getAsString() << "', which is incompatible with " |
| 227 | << "sizeof operand type '" << SizeofType.getAsString() << "'"; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 228 | SmallVector<SourceRange, 4> Ranges; |
Anna Zaks | 694be01 | 2012-09-07 19:20:13 +0000 | [diff] [blame] | 229 | Ranges.push_back(i->AllocCall->getCallee()->getSourceRange()); |
| 230 | Ranges.push_back(SFinder.Sizeofs[0]->getSourceRange()); |
| 231 | if (TSI) |
| 232 | Ranges.push_back(TSI->getTypeLoc().getSourceRange()); |
| 233 | |
| 234 | PathDiagnosticLocation L = |
| 235 | PathDiagnosticLocation::createBegin(i->AllocCall->getCallee(), |
| 236 | BR.getSourceManager(), ADC); |
| 237 | |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 238 | BR.EmitBasicReport(D, this, "Allocator sizeof operand mismatch", |
| 239 | categories::UnixAPI, OS.str(), L, Ranges); |
Peter Collingbourne | 266e3dd | 2011-12-08 08:31:14 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | } |
| 243 | }; |
| 244 | |
| 245 | } |
| 246 | |
| 247 | void ento::registerMallocSizeofChecker(CheckerManager &mgr) { |
| 248 | mgr.registerChecker<MallocSizeofChecker>(); |
| 249 | } |