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;
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<BugType> DoubleCloseBugType;
|
|
|
|
std::unique_ptr<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; }
|
|
|
|
|
2014-03-15 12:29:04 +08:00
|
|
|
bool VisitSymbol(SymbolRef sym) override {
|
2012-11-06 12:20:57 +08:00
|
|
|
state = state->remove<StreamMap>(sym);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2014-05-27 10:45:47 +08:00
|
|
|
SimpleStreamChecker::SimpleStreamChecker()
|
|
|
|
: IIfopen(nullptr), IIfclose(nullptr) {
|
2012-10-31 10:32:41 +08:00
|
|
|
// Initialize the bug types.
|
2014-02-12 05:49:21 +08:00
|
|
|
DoubleCloseBugType.reset(
|
|
|
|
new BugType(this, "Double fclose", "Unix Stream API Error"));
|
2012-10-31 10:32:41 +08:00
|
|
|
|
2014-02-12 05:49:21 +08:00
|
|
|
LeakBugType.reset(
|
|
|
|
new BugType(this, "Resource Leak", "Unix Stream API Error"));
|
2012-10-31 10:32:41 +08:00
|
|
|
// 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-07-04 11:08:24 +08:00
|
|
|
for (SmallVectorImpl<SymbolRef>::iterator
|
|
|
|
I = LeakedStreams.begin(), E = LeakedStreams.end(); I != E; ++I) {
|
2012-10-30 06:51:50 +08:00
|
|
|
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.
|
[analyzer] Indirect invalidation counts as an escape for leak checkers.
Consider this example:
char *p = malloc(sizeof(char));
systemFunction(&p);
free(p);
In this case, when we call systemFunction, we know (because it's a system
function) that it won't free 'p'. However, we /don't/ know whether or not
it will /change/ 'p', so the analyzer is forced to invalidate 'p', wiping
out any bindings it contains. But now the malloc'd region looks like a
leak, since there are no more bindings pointing to it, and we'll get a
spurious leak warning.
The fix for this is to notice when something is becoming inaccessible due
to invalidation (i.e. an imperfect model, as opposed to being explicitly
overwritten) and stop tracking it at that point. Currently, the best way
to determine this for a call is the "indirect escape" pointer-escape kind.
In practice, all the patch does is take the "system functions don't free
memory" special case and limit it to direct parameters, i.e. just the
arguments to a call and not other regions accessible to them. This is a
conservative change that should only cause us to escape regions more
eagerly, which means fewer leak warnings.
This isn't perfect for several reasons, the main one being that this
example is treated the same as the one above:
char **p = malloc(sizeof(char *));
systemFunction(p + 1);
// leak
Currently, "addresses accessible by offsets of the starting region" and
"addresses accessible through bindings of the starting region" are both
considered "indirect" regions, hence this uniform treatment.
Another issue is our longstanding problem of not distinguishing const and
non-const bindings; if in the first example systemFunction's parameter were
a char * const *, we should know that the function will not overwrite 'p',
and thus we can safely report the leak.
<rdar://problem/13758386>
llvm-svn: 181607
2013-05-11 01:07:16 +08:00
|
|
|
if (Kind == PSK_DirectEscapeOnCall && 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>();
|
|
|
|
}
|