1 //=== MallocChecker.cpp - A malloc/free checker -------------------*- 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 // This file defines malloc/free checker, which checks for potential memory
11 // leaks, double free, and use-after-free problems.
13 //===----------------------------------------------------------------------===//
15 #include "ClangSACheckers.h"
16 #include "clang/StaticAnalyzer/Core/Checker.h"
17 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/GRState.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/GRStateTrait.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
23 #include "llvm/ADT/ImmutableMap.h"
24 using namespace clang;
30 enum Kind { AllocateUnchecked, AllocateFailed, Released, Escaped,
35 RefState(Kind k, const Stmt *s) : K(k), S(s) {}
37 bool isAllocated() const { return K == AllocateUnchecked; }
38 //bool isFailed() const { return K == AllocateFailed; }
39 bool isReleased() const { return K == Released; }
40 //bool isEscaped() const { return K == Escaped; }
41 //bool isRelinquished() const { return K == Relinquished; }
43 bool operator==(const RefState &X) const {
44 return K == X.K && S == X.S;
47 static RefState getAllocateUnchecked(const Stmt *s) {
48 return RefState(AllocateUnchecked, s);
50 static RefState getAllocateFailed() {
51 return RefState(AllocateFailed, 0);
53 static RefState getReleased(const Stmt *s) { return RefState(Released, s); }
54 static RefState getEscaped(const Stmt *s) { return RefState(Escaped, s); }
55 static RefState getRelinquished(const Stmt *s) {
56 return RefState(Relinquished, s);
59 void Profile(llvm::FoldingSetNodeID &ID) const {
67 class MallocChecker : public Checker<eval::Call, check::DeadSymbols, check::EndPath, check::PreStmt<ReturnStmt>, check::Location,
68 check::Bind, eval::Assume> {
69 mutable llvm::OwningPtr<BuiltinBug> BT_DoubleFree;
70 mutable llvm::OwningPtr<BuiltinBug> BT_Leak;
71 mutable llvm::OwningPtr<BuiltinBug> BT_UseFree;
72 mutable llvm::OwningPtr<BuiltinBug> BT_UseRelinquished;
73 mutable llvm::OwningPtr<BuiltinBug> BT_BadFree;
74 mutable IdentifierInfo *II_malloc, *II_free, *II_realloc, *II_calloc;
77 MallocChecker() : II_malloc(0), II_free(0), II_realloc(0), II_calloc(0) {}
79 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
80 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
81 void checkEndPath(EndOfFunctionNodeBuilder &B, ExprEngine &Eng) const;
82 void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
83 const GRState *evalAssume(const GRState *state, SVal Cond,
84 bool Assumption) const;
85 void checkLocation(SVal l, bool isLoad, CheckerContext &C) const;
86 void checkBind(SVal location, SVal val, CheckerContext &C) const;
89 static void MallocMem(CheckerContext &C, const CallExpr *CE);
90 static void MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE,
91 const OwnershipAttr* Att);
92 static const GRState *MallocMemAux(CheckerContext &C, const CallExpr *CE,
93 const Expr *SizeEx, SVal Init,
94 const GRState *state) {
95 return MallocMemAux(C, CE, state->getSVal(SizeEx), Init, state);
97 static const GRState *MallocMemAux(CheckerContext &C, const CallExpr *CE,
98 SVal SizeEx, SVal Init,
99 const GRState *state);
101 void FreeMem(CheckerContext &C, const CallExpr *CE) const;
102 void FreeMemAttr(CheckerContext &C, const CallExpr *CE,
103 const OwnershipAttr* Att) const;
104 const GRState *FreeMemAux(CheckerContext &C, const CallExpr *CE,
105 const GRState *state, unsigned Num, bool Hold) const;
107 void ReallocMem(CheckerContext &C, const CallExpr *CE) const;
108 static void CallocMem(CheckerContext &C, const CallExpr *CE);
110 static bool SummarizeValue(llvm::raw_ostream& os, SVal V);
111 static bool SummarizeRegion(llvm::raw_ostream& os, const MemRegion *MR);
112 void ReportBadFree(CheckerContext &C, SVal ArgVal, SourceRange range) const;
114 } // end anonymous namespace
116 typedef llvm::ImmutableMap<SymbolRef, RefState> RegionStateTy;
121 struct GRStateTrait<RegionState>
122 : public GRStatePartialTrait<RegionStateTy> {
123 static void *GDMIndex() { static int x; return &x; }
128 bool MallocChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
129 const GRState *state = C.getState();
130 const Expr *Callee = CE->getCallee();
131 SVal L = state->getSVal(Callee);
133 const FunctionDecl *FD = L.getAsFunctionDecl();
137 ASTContext &Ctx = C.getASTContext();
139 II_malloc = &Ctx.Idents.get("malloc");
141 II_free = &Ctx.Idents.get("free");
143 II_realloc = &Ctx.Idents.get("realloc");
145 II_calloc = &Ctx.Idents.get("calloc");
147 if (FD->getIdentifier() == II_malloc) {
152 if (FD->getIdentifier() == II_free) {
157 if (FD->getIdentifier() == II_realloc) {
162 if (FD->getIdentifier() == II_calloc) {
167 // Check all the attributes, if there are any.
168 // There can be multiple of these attributes.
170 if (FD->hasAttrs()) {
171 for (specific_attr_iterator<OwnershipAttr>
172 i = FD->specific_attr_begin<OwnershipAttr>(),
173 e = FD->specific_attr_end<OwnershipAttr>();
175 switch ((*i)->getOwnKind()) {
176 case OwnershipAttr::Returns: {
177 MallocMemReturnsAttr(C, CE, *i);
181 case OwnershipAttr::Takes:
182 case OwnershipAttr::Holds: {
183 FreeMemAttr(C, CE, *i);
195 void MallocChecker::MallocMem(CheckerContext &C, const CallExpr *CE) {
196 const GRState *state = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(),
198 C.addTransition(state);
201 void MallocChecker::MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE,
202 const OwnershipAttr* Att) {
203 if (Att->getModule() != "malloc")
206 OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
208 const GRState *state =
209 MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(), C.getState());
210 C.addTransition(state);
213 const GRState *state = MallocMemAux(C, CE, UnknownVal(), UndefinedVal(),
215 C.addTransition(state);
218 const GRState *MallocChecker::MallocMemAux(CheckerContext &C,
220 SVal Size, SVal Init,
221 const GRState *state) {
222 unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
223 SValBuilder &svalBuilder = C.getSValBuilder();
225 // Set the return value.
226 SVal retVal = svalBuilder.getConjuredSymbolVal(NULL, CE, CE->getType(), Count);
227 state = state->BindExpr(CE, retVal);
229 // Fill the region with the initialization value.
230 state = state->bindDefault(retVal, Init);
232 // Set the region's extent equal to the Size parameter.
233 const SymbolicRegion *R = cast<SymbolicRegion>(retVal.getAsRegion());
234 DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
235 DefinedOrUnknownSVal DefinedSize = cast<DefinedOrUnknownSVal>(Size);
236 DefinedOrUnknownSVal extentMatchesSize =
237 svalBuilder.evalEQ(state, Extent, DefinedSize);
239 state = state->assume(extentMatchesSize, true);
242 SymbolRef Sym = retVal.getAsLocSymbol();
245 // Set the symbol's state to Allocated.
246 return state->set<RegionState>(Sym, RefState::getAllocateUnchecked(CE));
249 void MallocChecker::FreeMem(CheckerContext &C, const CallExpr *CE) const {
250 const GRState *state = FreeMemAux(C, CE, C.getState(), 0, false);
253 C.addTransition(state);
256 void MallocChecker::FreeMemAttr(CheckerContext &C, const CallExpr *CE,
257 const OwnershipAttr* Att) const {
258 if (Att->getModule() != "malloc")
261 for (OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
263 const GRState *state = FreeMemAux(C, CE, C.getState(), *I,
264 Att->getOwnKind() == OwnershipAttr::Holds);
266 C.addTransition(state);
270 const GRState *MallocChecker::FreeMemAux(CheckerContext &C, const CallExpr *CE,
271 const GRState *state, unsigned Num,
273 const Expr *ArgExpr = CE->getArg(Num);
274 SVal ArgVal = state->getSVal(ArgExpr);
276 DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(ArgVal);
278 // Check for null dereferences.
279 if (!isa<Loc>(location))
282 // FIXME: Technically using 'Assume' here can result in a path
283 // bifurcation. In such cases we need to return two states, not just one.
284 const GRState *notNullState, *nullState;
285 llvm::tie(notNullState, nullState) = state->assume(location);
287 // The explicit NULL case, no operation is performed.
288 if (nullState && !notNullState)
291 assert(notNullState);
293 // Unknown values could easily be okay
294 // Undefined values are handled elsewhere
295 if (ArgVal.isUnknownOrUndef())
298 const MemRegion *R = ArgVal.getAsRegion();
300 // Nonlocs can't be freed, of course.
301 // Non-region locations (labels and fixed addresses) also shouldn't be freed.
303 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
309 // Blocks might show up as heap data, but should not be free()d
310 if (isa<BlockDataRegion>(R)) {
311 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
315 const MemSpaceRegion *MS = R->getMemorySpace();
317 // Parameters, locals, statics, and globals shouldn't be freed.
318 if (!(isa<UnknownSpaceRegion>(MS) || isa<HeapSpaceRegion>(MS))) {
319 // FIXME: at the time this code was written, malloc() regions were
320 // represented by conjured symbols, which are all in UnknownSpaceRegion.
321 // This means that there isn't actually anything from HeapSpaceRegion
322 // that should be freed, even though we allow it here.
323 // Of course, free() can work on memory allocated outside the current
324 // function, so UnknownSpaceRegion is always a possibility.
325 // False negatives are better than false positives.
327 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
331 const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R);
332 // Various cases could lead to non-symbol values here.
333 // For now, ignore them.
337 SymbolRef Sym = SR->getSymbol();
338 const RefState *RS = state->get<RegionState>(Sym);
340 // If the symbol has not been tracked, return. This is possible when free() is
341 // called on a pointer that does not get its pointee directly from malloc().
342 // Full support of this requires inter-procedural analysis.
346 // Check double free.
347 if (RS->isReleased()) {
348 if (ExplodedNode *N = C.generateSink()) {
351 new BuiltinBug("Double free",
352 "Try to free a memory block that has been released"));
353 // FIXME: should find where it's freed last time.
354 BugReport *R = new BugReport(*BT_DoubleFree,
355 BT_DoubleFree->getDescription(), N);
363 return notNullState->set<RegionState>(Sym, RefState::getRelinquished(CE));
364 return notNullState->set<RegionState>(Sym, RefState::getReleased(CE));
367 bool MallocChecker::SummarizeValue(llvm::raw_ostream& os, SVal V) {
368 if (nonloc::ConcreteInt *IntVal = dyn_cast<nonloc::ConcreteInt>(&V))
369 os << "an integer (" << IntVal->getValue() << ")";
370 else if (loc::ConcreteInt *ConstAddr = dyn_cast<loc::ConcreteInt>(&V))
371 os << "a constant address (" << ConstAddr->getValue() << ")";
372 else if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&V))
373 os << "the address of the label '" << Label->getLabel()->getName() << "'";
380 bool MallocChecker::SummarizeRegion(llvm::raw_ostream& os,
381 const MemRegion *MR) {
382 switch (MR->getKind()) {
383 case MemRegion::FunctionTextRegionKind: {
384 const FunctionDecl *FD = cast<FunctionTextRegion>(MR)->getDecl();
386 os << "the address of the function '" << FD << "'";
388 os << "the address of a function";
391 case MemRegion::BlockTextRegionKind:
394 case MemRegion::BlockDataRegionKind:
395 // FIXME: where the block came from?
399 const MemSpaceRegion *MS = MR->getMemorySpace();
401 switch (MS->getKind()) {
402 case MemRegion::StackLocalsSpaceRegionKind: {
403 const VarRegion *VR = dyn_cast<VarRegion>(MR);
411 os << "the address of the local variable '" << VD->getName() << "'";
413 os << "the address of a local stack variable";
416 case MemRegion::StackArgumentsSpaceRegionKind: {
417 const VarRegion *VR = dyn_cast<VarRegion>(MR);
425 os << "the address of the parameter '" << VD->getName() << "'";
427 os << "the address of a parameter";
430 case MemRegion::NonStaticGlobalSpaceRegionKind:
431 case MemRegion::StaticGlobalSpaceRegionKind: {
432 const VarRegion *VR = dyn_cast<VarRegion>(MR);
440 if (VD->isStaticLocal())
441 os << "the address of the static variable '" << VD->getName() << "'";
443 os << "the address of the global variable '" << VD->getName() << "'";
445 os << "the address of a global variable";
455 void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal,
456 SourceRange range) const {
457 if (ExplodedNode *N = C.generateSink()) {
459 BT_BadFree.reset(new BuiltinBug("Bad free"));
461 llvm::SmallString<100> buf;
462 llvm::raw_svector_ostream os(buf);
464 const MemRegion *MR = ArgVal.getAsRegion();
466 while (const ElementRegion *ER = dyn_cast<ElementRegion>(MR))
467 MR = ER->getSuperRegion();
469 // Special case for alloca()
470 if (isa<AllocaRegion>(MR))
471 os << "Argument to free() was allocated by alloca(), not malloc()";
473 os << "Argument to free() is ";
474 if (SummarizeRegion(os, MR))
475 os << ", which is not memory allocated by malloc()";
477 os << "not memory allocated by malloc()";
480 os << "Argument to free() is ";
481 if (SummarizeValue(os, ArgVal))
482 os << ", which is not memory allocated by malloc()";
484 os << "not memory allocated by malloc()";
487 EnhancedBugReport *R = new EnhancedBugReport(*BT_BadFree, os.str(), N);
493 void MallocChecker::ReallocMem(CheckerContext &C, const CallExpr *CE) const {
494 const GRState *state = C.getState();
495 const Expr *arg0Expr = CE->getArg(0);
496 DefinedOrUnknownSVal arg0Val
497 = cast<DefinedOrUnknownSVal>(state->getSVal(arg0Expr));
499 SValBuilder &svalBuilder = C.getSValBuilder();
501 DefinedOrUnknownSVal PtrEQ =
502 svalBuilder.evalEQ(state, arg0Val, svalBuilder.makeNull());
504 // Get the size argument. If there is no size arg then give up.
505 const Expr *Arg1 = CE->getArg(1);
509 // Get the value of the size argument.
510 DefinedOrUnknownSVal Arg1Val =
511 cast<DefinedOrUnknownSVal>(state->getSVal(Arg1));
513 // Compare the size argument to 0.
514 DefinedOrUnknownSVal SizeZero =
515 svalBuilder.evalEQ(state, Arg1Val,
516 svalBuilder.makeIntValWithPtrWidth(0, false));
518 // If the ptr is NULL and the size is not 0, the call is equivalent to
520 const GRState *stateEqual = state->assume(PtrEQ, true);
521 if (stateEqual && state->assume(SizeZero, false)) {
522 // Hack: set the NULL symbolic region to released to suppress false warning.
523 // In the future we should add more states for allocated regions, e.g.,
524 // CheckedNull, CheckedNonNull.
526 SymbolRef Sym = arg0Val.getAsLocSymbol();
528 stateEqual = stateEqual->set<RegionState>(Sym, RefState::getReleased(CE));
530 const GRState *stateMalloc = MallocMemAux(C, CE, CE->getArg(1),
531 UndefinedVal(), stateEqual);
532 C.addTransition(stateMalloc);
535 if (const GRState *stateNotEqual = state->assume(PtrEQ, false)) {
536 // If the size is 0, free the memory.
537 if (const GRState *stateSizeZero = stateNotEqual->assume(SizeZero, true))
538 if (const GRState *stateFree =
539 FreeMemAux(C, CE, stateSizeZero, 0, false)) {
541 // Add the state transition to set input pointer argument to be free.
542 C.addTransition(stateFree);
544 // Bind the return value to UndefinedVal because it is now free.
545 C.addTransition(stateFree->BindExpr(CE, UndefinedVal(), true));
547 if (const GRState *stateSizeNotZero = stateNotEqual->assume(SizeZero,false))
548 if (const GRState *stateFree = FreeMemAux(C, CE, stateSizeNotZero,
550 // FIXME: We should copy the content of the original buffer.
551 const GRState *stateRealloc = MallocMemAux(C, CE, CE->getArg(1),
552 UnknownVal(), stateFree);
553 C.addTransition(stateRealloc);
558 void MallocChecker::CallocMem(CheckerContext &C, const CallExpr *CE) {
559 const GRState *state = C.getState();
560 SValBuilder &svalBuilder = C.getSValBuilder();
562 SVal count = state->getSVal(CE->getArg(0));
563 SVal elementSize = state->getSVal(CE->getArg(1));
564 SVal TotalSize = svalBuilder.evalBinOp(state, BO_Mul, count, elementSize,
565 svalBuilder.getContext().getSizeType());
566 SVal zeroVal = svalBuilder.makeZeroVal(svalBuilder.getContext().CharTy);
568 C.addTransition(MallocMemAux(C, CE, TotalSize, zeroVal, state));
571 void MallocChecker::checkDeadSymbols(SymbolReaper &SymReaper,
572 CheckerContext &C) const
574 if (!SymReaper.hasDeadSymbols())
577 const GRState *state = C.getState();
578 RegionStateTy RS = state->get<RegionState>();
579 RegionStateTy::Factory &F = state->get_context<RegionState>();
581 for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
582 if (SymReaper.isDead(I->first)) {
583 if (I->second.isAllocated()) {
584 if (ExplodedNode *N = C.generateNode()) {
586 BT_Leak.reset(new BuiltinBug("Memory leak",
587 "Allocated memory never released. Potential memory leak."));
588 // FIXME: where it is allocated.
589 BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
594 // Remove the dead symbol from the map.
595 RS = F.remove(RS, I->first);
598 C.generateNode(state->set<RegionState>(RS));
601 void MallocChecker::checkEndPath(EndOfFunctionNodeBuilder &B,
602 ExprEngine &Eng) const {
603 const GRState *state = B.getState();
604 RegionStateTy M = state->get<RegionState>();
606 for (RegionStateTy::iterator I = M.begin(), E = M.end(); I != E; ++I) {
607 RefState RS = I->second;
608 if (RS.isAllocated()) {
609 ExplodedNode *N = B.generateNode(state);
612 BT_Leak.reset(new BuiltinBug("Memory leak",
613 "Allocated memory never released. Potential memory leak."));
614 BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
615 Eng.getBugReporter().EmitReport(R);
621 void MallocChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const {
622 const Expr *retExpr = S->getRetValue();
626 const GRState *state = C.getState();
628 SymbolRef Sym = state->getSVal(retExpr).getAsSymbol();
632 const RefState *RS = state->get<RegionState>(Sym);
636 // FIXME: check other cases.
637 if (RS->isAllocated())
638 state = state->set<RegionState>(Sym, RefState::getEscaped(S));
640 C.addTransition(state);
643 const GRState *MallocChecker::evalAssume(const GRState *state, SVal Cond,
644 bool Assumption) const {
645 // If a symblic region is assumed to NULL, set its state to AllocateFailed.
646 // FIXME: should also check symbols assumed to non-null.
648 RegionStateTy RS = state->get<RegionState>();
650 for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
651 // If the symbol is assumed to NULL, this will return an APSInt*.
652 if (state->getSymVal(I.getKey()))
653 state = state->set<RegionState>(I.getKey(),RefState::getAllocateFailed());
659 // Check if the location is a freed symbolic region.
660 void MallocChecker::checkLocation(SVal l, bool isLoad,CheckerContext &C) const {
661 SymbolRef Sym = l.getLocSymbolInBase();
663 const RefState *RS = C.getState()->get<RegionState>(Sym);
664 if (RS && RS->isReleased()) {
665 if (ExplodedNode *N = C.generateNode()) {
667 BT_UseFree.reset(new BuiltinBug("Use dynamically allocated memory "
668 "after it is freed."));
670 BugReport *R = new BugReport(*BT_UseFree, BT_UseFree->getDescription(),
678 void MallocChecker::checkBind(SVal location, SVal val,CheckerContext &C) const {
679 // The PreVisitBind implements the same algorithm as already used by the
680 // Objective C ownership checker: if the pointer escaped from this scope by
681 // assignment, let it go. However, assigning to fields of a stack-storage
682 // structure does not transfer ownership.
684 const GRState *state = C.getState();
685 DefinedOrUnknownSVal l = cast<DefinedOrUnknownSVal>(location);
687 // Check for null dereferences.
691 // Before checking if the state is null, check if 'val' has a RefState.
692 // Only then should we check for null and bifurcate the state.
693 SymbolRef Sym = val.getLocSymbolInBase();
695 if (const RefState *RS = state->get<RegionState>(Sym)) {
696 // If ptr is NULL, no operation is performed.
697 const GRState *notNullState, *nullState;
698 llvm::tie(notNullState, nullState) = state->assume(l);
700 // Generate a transition for 'nullState' to record the assumption
701 // that the state was null.
703 C.addTransition(nullState);
708 if (RS->isAllocated()) {
709 // Something we presently own is being assigned somewhere.
710 const MemRegion *AR = location.getAsRegion();
713 AR = AR->StripCasts()->getBaseRegion();
715 // If it is on the stack, we still own it.
716 if (AR->hasStackNonParametersStorage())
719 // If the state can't represent this binding, we still own it.
720 if (notNullState == (notNullState->bindLoc(cast<Loc>(location),
724 // We no longer own this pointer.
726 notNullState->set<RegionState>(Sym,
727 RefState::getRelinquished(C.getStmt()));
731 C.addTransition(notNullState);
736 void ento::registerMallocChecker(CheckerManager &mgr) {
737 mgr.registerChecker<MallocChecker>();