2012-10-30 06:51:50 +08:00
|
|
|
//===-- SimpleStreamChecker.cpp -----------------------------------------*- C++ -*--//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Defines a checker for proper use of fopen/fclose APIs.
|
|
|
|
// - If a file has been closed with fclose, it should not be accessed again.
|
|
|
|
// Accessing a closed file results in undefined behavior.
|
|
|
|
// - If a file was opened with fopen, it must be closed with fclose before
|
|
|
|
// the execution ends. Failing to do so results in a resource leak.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ClangSACheckers.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2012-11-03 07:49:35 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
2012-10-30 06:51:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
namespace {
|
2013-01-13 03:30:44 +08:00
|
|
|
typedef SmallVector<SymbolRef, 2> SymbolVector;
|
2012-10-30 06:51:50 +08:00
|
|
|
|
|
|
|
struct StreamState {
|
2012-10-31 10:32:41 +08:00
|
|
|
private:
|
2012-10-30 06:51:50 +08:00
|
|
|
enum Kind { Opened, Closed } K;
|
|
|
|
StreamState(Kind InK) : K(InK) { }
|
|
|
|
|
2012-10-31 10:32:41 +08:00
|
|
|
public:
|
2012-10-30 06:51:50 +08:00
|
|
|
bool isOpened() const { return K == Opened; }
|
|
|
|
bool isClosed() const { return K == Closed; }
|
|
|
|
|
|
|
|
static StreamState getOpened() { return StreamState(Opened); }
|
|
|
|
static StreamState getClosed() { return StreamState(Closed); }
|
|
|
|
|
|
|
|
bool operator==(const StreamState &X) const {
|
|
|
|
return K == X.K;
|
|
|
|
}
|
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
ID.AddInteger(K);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-03 07:49:35 +08:00
|
|
|
class SimpleStreamChecker : public Checker<check::PostCall,
|
|
|
|
check::PreCall,
|
2012-11-06 12:20:57 +08:00
|
|
|
check::DeadSymbols,
|
2012-12-22 08:18:39 +08:00
|
|
|
check::PointerEscape> {
|
2012-10-30 06:51:50 +08:00
|
|
|
|
|
|
|
mutable IdentifierInfo *IIfopen, *IIfclose;
|
|
|
|
|
2012-11-01 08:18:41 +08:00
|
|
|
OwningPtr<BugType> DoubleCloseBugType;
|
|
|
|
OwningPtr<BugType> LeakBugType;
|
2012-10-30 06:51:50 +08:00
|
|
|
|
|
|
|
void initIdentifierInfo(ASTContext &Ctx) const;
|
|
|
|
|
|
|
|
void reportDoubleClose(SymbolRef FileDescSym,
|
2012-11-03 07:49:35 +08:00
|
|
|
const CallEvent &Call,
|
2012-10-30 06:51:50 +08:00
|
|
|
CheckerContext &C) const;
|
|
|
|
|
2012-11-01 08:18:41 +08:00
|
|
|
void reportLeaks(SymbolVector LeakedStreams,
|
|
|
|
CheckerContext &C,
|
|
|
|
ExplodedNode *ErrNode) const;
|
2012-10-30 06:51:50 +08:00
|
|
|
|
2012-11-06 12:20:57 +08:00
|
|
|
bool guaranteedNotToCloseFile(const CallEvent &Call) const;
|
|
|
|
|
2012-10-30 06:51:50 +08:00
|
|
|
public:
|
2012-10-31 10:32:41 +08:00
|
|
|
SimpleStreamChecker();
|
2012-10-30 06:51:50 +08:00
|
|
|
|
|
|
|
/// Process fopen.
|
2012-11-03 07:49:35 +08:00
|
|
|
void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
|
2012-10-30 06:51:50 +08:00
|
|
|
/// Process fclose.
|
2012-11-03 07:49:35 +08:00
|
|
|
void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
|
2012-10-30 06:51:50 +08:00
|
|
|
|
|
|
|
void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
|
2012-11-06 12:20:57 +08:00
|
|
|
|
2012-12-22 08:18:39 +08:00
|
|
|
/// Stop tracking addresses which escape.
|
|
|
|
ProgramStateRef checkPointerEscape(ProgramStateRef State,
|
|
|
|
const InvalidatedSymbols &Escaped,
|
2013-02-08 07:05:43 +08:00
|
|
|
const CallEvent *Call,
|
|
|
|
PointerEscapeKind Kind) const;
|
2012-10-30 06:51:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
/// The state of the checker is a map from tracked stream symbols to their
|
2012-10-30 12:17:18 +08:00
|
|
|
/// state. Let's store it in the ProgramState.
|
|
|
|
REGISTER_MAP_WITH_PROGRAMSTATE(StreamMap, SymbolRef, StreamState)
|
2012-10-30 06:51:50 +08:00
|
|
|
|
2012-11-06 12:20:57 +08:00
|
|
|
namespace {
|
|
|
|
class StopTrackingCallback : public SymbolVisitor {
|
|
|
|
ProgramStateRef state;
|
|
|
|
public:
|
|
|
|
StopTrackingCallback(ProgramStateRef st) : state(st) {}
|
|
|
|
ProgramStateRef getState() const { return state; }
|
|
|
|
|
|
|
|
bool VisitSymbol(SymbolRef sym) {
|
|
|
|
state = state->remove<StreamMap>(sym);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
2012-10-31 10:32:41 +08:00
|
|
|
SimpleStreamChecker::SimpleStreamChecker() : IIfopen(0), IIfclose(0) {
|
|
|
|
// Initialize the bug types.
|
|
|
|
DoubleCloseBugType.reset(new BugType("Double fclose",
|
|
|
|
"Unix Stream API Error"));
|
|
|
|
|
|
|
|
LeakBugType.reset(new BugType("Resource Leak",
|
|
|
|
"Unix Stream API Error"));
|
|
|
|
// Sinks are higher importance bugs as well as calls to assert() or exit(0).
|
|
|
|
LeakBugType->setSuppressOnSink(true);
|
|
|
|
}
|
|
|
|
|
2012-11-03 07:49:35 +08:00
|
|
|
void SimpleStreamChecker::checkPostCall(const CallEvent &Call,
|
2012-10-30 06:51:50 +08:00
|
|
|
CheckerContext &C) const {
|
|
|
|
initIdentifierInfo(C.getASTContext());
|
|
|
|
|
2012-11-03 07:49:35 +08:00
|
|
|
if (!Call.isGlobalCFunction())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Call.getCalleeIdentifier() != IIfopen)
|
2012-10-30 06:51:50 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the symbolic value corresponding to the file handle.
|
2012-11-03 07:49:35 +08:00
|
|
|
SymbolRef FileDesc = Call.getReturnValue().getAsSymbol();
|
2012-10-30 06:51:50 +08:00
|
|
|
if (!FileDesc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Generate the next transition (an edge in the exploded graph).
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
State = State->set<StreamMap>(FileDesc, StreamState::getOpened());
|
|
|
|
C.addTransition(State);
|
|
|
|
}
|
|
|
|
|
2012-11-03 07:49:35 +08:00
|
|
|
void SimpleStreamChecker::checkPreCall(const CallEvent &Call,
|
2012-10-30 06:51:50 +08:00
|
|
|
CheckerContext &C) const {
|
|
|
|
initIdentifierInfo(C.getASTContext());
|
|
|
|
|
2012-11-03 07:49:35 +08:00
|
|
|
if (!Call.isGlobalCFunction())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Call.getCalleeIdentifier() != IIfclose)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Call.getNumArgs() != 1)
|
2012-10-30 06:51:50 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the symbolic value corresponding to the file handle.
|
2012-11-03 07:49:35 +08:00
|
|
|
SymbolRef FileDesc = Call.getArgSVal(0).getAsSymbol();
|
2012-10-30 06:51:50 +08:00
|
|
|
if (!FileDesc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check if the stream has already been closed.
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
const StreamState *SS = State->get<StreamMap>(FileDesc);
|
2012-11-01 06:17:48 +08:00
|
|
|
if (SS && SS->isClosed()) {
|
2012-10-30 06:51:50 +08:00
|
|
|
reportDoubleClose(FileDesc, Call, C);
|
2012-11-01 06:17:48 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-10-30 06:51:50 +08:00
|
|
|
|
|
|
|
// Generate the next transition, in which the stream is closed.
|
|
|
|
State = State->set<StreamMap>(FileDesc, StreamState::getClosed());
|
|
|
|
C.addTransition(State);
|
|
|
|
}
|
|
|
|
|
2012-11-03 05:30:04 +08:00
|
|
|
static bool isLeaked(SymbolRef Sym, const StreamState &SS,
|
|
|
|
bool IsSymDead, ProgramStateRef State) {
|
|
|
|
if (IsSymDead && SS.isOpened()) {
|
|
|
|
// If a symbol is NULL, assume that fopen failed on this path.
|
|
|
|
// A symbol should only be considered leaked if it is non-null.
|
|
|
|
ConstraintManager &CMgr = State->getConstraintManager();
|
|
|
|
ConditionTruthVal OpenFailed = CMgr.isNull(State, Sym);
|
|
|
|
return !OpenFailed.isConstrainedTrue();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-30 06:51:50 +08:00
|
|
|
void SimpleStreamChecker::checkDeadSymbols(SymbolReaper &SymReaper,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
SymbolVector LeakedStreams;
|
2012-11-03 05:30:04 +08:00
|
|
|
StreamMapTy TrackedStreams = State->get<StreamMap>();
|
2012-10-30 12:17:40 +08:00
|
|
|
for (StreamMapTy::iterator I = TrackedStreams.begin(),
|
2012-11-01 08:18:27 +08:00
|
|
|
E = TrackedStreams.end(); I != E; ++I) {
|
2012-10-30 06:51:50 +08:00
|
|
|
SymbolRef Sym = I->first;
|
2012-11-03 05:30:04 +08:00
|
|
|
bool IsSymDead = SymReaper.isDead(Sym);
|
|
|
|
|
|
|
|
// Collect leaked symbols.
|
|
|
|
if (isLeaked(Sym, I->second, IsSymDead, State))
|
|
|
|
LeakedStreams.push_back(Sym);
|
|
|
|
|
|
|
|
// Remove the dead symbol from the streams map.
|
|
|
|
if (IsSymDead)
|
2012-10-30 06:51:50 +08:00
|
|
|
State = State->remove<StreamMap>(Sym);
|
|
|
|
}
|
|
|
|
|
2012-10-31 10:32:41 +08:00
|
|
|
ExplodedNode *N = C.addTransition(State);
|
|
|
|
reportLeaks(LeakedStreams, C, N);
|
2012-10-30 06:51:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleStreamChecker::reportDoubleClose(SymbolRef FileDescSym,
|
2012-11-03 07:49:35 +08:00
|
|
|
const CallEvent &Call,
|
2012-10-30 06:51:50 +08:00
|
|
|
CheckerContext &C) const {
|
|
|
|
// We reached a bug, stop exploring the path here by generating a sink.
|
|
|
|
ExplodedNode *ErrNode = C.generateSink();
|
2012-10-31 10:32:41 +08:00
|
|
|
// If we've already reached this node on another path, return.
|
2012-10-30 06:51:50 +08:00
|
|
|
if (!ErrNode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Generate the report.
|
|
|
|
BugReport *R = new BugReport(*DoubleCloseBugType,
|
|
|
|
"Closing a previously closed file stream", ErrNode);
|
2012-11-03 07:49:35 +08:00
|
|
|
R->addRange(Call.getSourceRange());
|
2012-10-30 06:51:50 +08:00
|
|
|
R->markInteresting(FileDescSym);
|
2012-11-02 09:53:40 +08:00
|
|
|
C.emitReport(R);
|
2012-10-30 06:51:50 +08:00
|
|
|
}
|
|
|
|
|
2012-10-31 10:32:41 +08:00
|
|
|
void SimpleStreamChecker::reportLeaks(SymbolVector LeakedStreams,
|
|
|
|
CheckerContext &C,
|
|
|
|
ExplodedNode *ErrNode) const {
|
2012-10-30 06:51:50 +08:00
|
|
|
// Attach bug reports to the leak node.
|
2012-10-30 12:18:21 +08:00
|
|
|
// TODO: Identify the leaked file descriptor.
|
2013-01-13 03:30:44 +08:00
|
|
|
for (SmallVector<SymbolRef, 2>::iterator
|
2012-10-30 06:51:50 +08:00
|
|
|
I = LeakedStreams.begin(), E = LeakedStreams.end(); I != E; ++I) {
|
|
|
|
BugReport *R = new BugReport(*LeakBugType,
|
|
|
|
"Opened file is never closed; potential resource leak", ErrNode);
|
2012-10-30 12:18:21 +08:00
|
|
|
R->markInteresting(*I);
|
2012-11-02 09:53:40 +08:00
|
|
|
C.emitReport(R);
|
2012-10-30 06:51:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-06 12:20:57 +08:00
|
|
|
bool SimpleStreamChecker::guaranteedNotToCloseFile(const CallEvent &Call) const{
|
|
|
|
// If it's not in a system header, assume it might close a file.
|
|
|
|
if (!Call.isInSystemHeader())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Handle cases where we know a buffer's /address/ can escape.
|
|
|
|
if (Call.argumentsMayEscape())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Note, even though fclose closes the file, we do not list it here
|
|
|
|
// since the checker is modeling the call.
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-22 08:18:39 +08:00
|
|
|
// If the pointer we are tracking escaped, do not track the symbol as
|
2012-11-06 12:20:57 +08:00
|
|
|
// we cannot reason about it anymore.
|
|
|
|
ProgramStateRef
|
2012-12-22 08:18:39 +08:00
|
|
|
SimpleStreamChecker::checkPointerEscape(ProgramStateRef State,
|
|
|
|
const InvalidatedSymbols &Escaped,
|
2013-02-08 07:05:43 +08:00
|
|
|
const CallEvent *Call,
|
|
|
|
PointerEscapeKind Kind) const {
|
2012-12-22 08:18:39 +08:00
|
|
|
// If we know that the call cannot close a file, there is nothing to do.
|
2013-02-08 07:05:43 +08:00
|
|
|
if ((Kind == PSK_DirectEscapeOnCall ||
|
|
|
|
Kind == PSK_IndirectEscapeOnCall) &&
|
|
|
|
guaranteedNotToCloseFile(*Call)) {
|
2012-11-06 12:20:57 +08:00
|
|
|
return State;
|
2013-02-08 07:05:43 +08:00
|
|
|
}
|
2012-11-06 12:20:57 +08:00
|
|
|
|
2012-12-22 08:18:39 +08:00
|
|
|
for (InvalidatedSymbols::const_iterator I = Escaped.begin(),
|
|
|
|
E = Escaped.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
SymbolRef Sym = *I;
|
2012-11-06 12:20:57 +08:00
|
|
|
|
|
|
|
// The symbol escaped. Optimistically, assume that the corresponding file
|
|
|
|
// handle will be closed somewhere else.
|
2012-12-22 08:18:39 +08:00
|
|
|
State = State->remove<StreamMap>(Sym);
|
2012-11-06 12:20:57 +08:00
|
|
|
}
|
|
|
|
return State;
|
|
|
|
}
|
|
|
|
|
2012-10-30 06:51:50 +08:00
|
|
|
void SimpleStreamChecker::initIdentifierInfo(ASTContext &Ctx) const {
|
|
|
|
if (IIfopen)
|
|
|
|
return;
|
|
|
|
IIfopen = &Ctx.Idents.get("fopen");
|
|
|
|
IIfclose = &Ctx.Idents.get("fclose");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ento::registerSimpleStreamChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<SimpleStreamChecker>();
|
|
|
|
}
|