Untitled diff

बनाया गया Diff कभी समाप्त नहीं होता
60 हटाए गए
255 लाइनें
31 जोड़े गए
223 लाइनें
//===-- SimpleStreamChecker.cpp -----------------------------------------*- C++ -*--//
//===-- SimpleStreamChecker.cpp -----------------------------------------*- C++ -*--//
//
//
// The LLVM Compiler Infrastructure
// The LLVM Compiler Infrastructure
//
//
// This file is distributed under the University of Illinois Open Source
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
// License. See LICENSE.TXT for details.
//
//
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
//
//
// Defines a checker for proper use of fopen/fclose APIs.
// Defines a checker for proper use of fopen/fclose APIs.
// - If a file has been closed with fclose, it should not be accessed again.
// - If a file has been closed with fclose, it should not be accessed again.
// Accessing a closed file results in undefined behavior.
// Accessing a closed file results in undefined behavior.
// - If a file was opened with fopen, it must be closed with fclose before
// - 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.
// the execution ends. Failing to do so results in a resource leak.
//
//
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//


#include "ClangSACheckers.h"
#include "ClangSACheckers.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"


using namespace clang;
using namespace clang;
using namespace ento;
using namespace ento;


namespace {
namespace {
enum SimpleStreamState { Closed, Opened };
enum SimpleStreamState { Closed, Opened };
SmartStateTrait SimpleStreamTrait;
SmartStateTrait SimpleStreamTrait;
typedef SmallVector<SymbolRef, 2> SymbolVector;
typedef SmallVector<const MemRegion *, 2> RegionVector;


class SimpleStreamModel
class SimpleStreamModel
: public Checker<check::ASTDecl<TranslationUnitDecl>, check::PostCall,
: public Checker<check::ASTDecl<TranslationUnitDecl>, check::PostCall> {
check::PointerEscape> {
CallDescription OpenFn, CloseFn;
CallDescription OpenFn, CloseFn;
bool guaranteedNotToCloseFile(const CallEvent &Call) const;
bool guaranteedNotToCloseFile(const CallEvent &Call) const;


public:
public:
SimpleStreamModel() : OpenFn("fopen"), CloseFn("fclose") {}
SimpleStreamModel() : OpenFn("fopen"), CloseFn("fclose") {}


void checkASTDecl(const TranslationUnitDecl *D, AnalysisManager &AMgr,
void checkASTDecl(const TranslationUnitDecl *D, AnalysisManager &AMgr,
BugReporter &BR) const;
BugReporter &BR) const;
void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
ProgramStateRef checkPointerEscape(ProgramStateRef State,
ProgramStateRef checkPointerEscape(ProgramStateRef State,
const InvalidatedSymbols &Escaped,
const InvalidatedSymbols &Escaped,
const CallEvent *Call,
const CallEvent *Call,
PointerEscapeKind Kind) const;
PointerEscapeKind Kind) const;
};
};
} // end of anonymous namespace
} // end of anonymous namespace


bool SimpleStreamModel::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;
}

void SimpleStreamModel::checkASTDecl(const TranslationUnitDecl *D,
void SimpleStreamModel::checkASTDecl(const TranslationUnitDecl *D,
AnalysisManager &AMgr,
AnalysisManager &AMgr,
BugReporter &BR) const {
BugReporter &BR) const {
// Once the AST context is available, we can express the fact that our trait
// Once the AST context is available, we can express the fact that our trait
// has type 'int'.
// has type 'int'.
// FIXME: This is ugly. Maybe make a separate callback, or delay registering
// FIXME: This is ugly. Maybe make a separate callback, or delay registering
// checkers until AST is constructed?
// checkers until AST is constructed?
SimpleStreamTrait.initialize("SimpleStream", AMgr.getASTContext().IntTy);
SimpleStreamTrait.initialize("SimpleStream", AMgr.getASTContext().IntTy);
}
}


void SimpleStreamModel::checkPostCall(const CallEvent &Call,
void SimpleStreamModel::checkPostCall(const CallEvent &Call,
CheckerContext &C) const {
CheckerContext &C) const {
if (!Call.isGlobalCFunction())
if (!Call.isGlobalCFunction())
return;
return;


if (Call.isCalled(OpenFn)) {
if (Call.isCalled(OpenFn)) {


// Get the symbolic value corresponding to the file handle.
// Get the symbolic value corresponding to the file handle.
SymbolRef FileDesc = Call.getReturnValue().getAsSymbol();
const MemRegion *FileDesc = Call.getReturnValue().getAsRegion();
if (!FileDesc)
if (!FileDesc)
return;
return;


// Generate the next transition (an edge in the exploded graph).
// Generate the next transition (an edge in the exploded graph).
ProgramStateRef State = C.getState();
ProgramStateRef State = C.getState();
State = State->bindLoc(SimpleStreamTrait, FileDesc, Opened);
State = State->bindLoc(SimpleStreamTrait, FileDesc, Opened);
C.addTransition(State);
C.addTransition(State);


} else if (Call.isCalled(CloseFn)) {
} else if (Call.isCalled(CloseFn)) {


// Get the symbolic value corresponding to the file handle.
// Get the symbolic value corresponding to the file handle.
SymbolRef FileDesc = Call.getArgSVal(0).getAsSymbol();
const MemRegion *FileDesc = Call.getArgSVal(0).getAsRegion();
if (!FileDesc)
if (!FileDesc)
return;
return;


// Generate the next transition, in which the stream is closed.
// Generate the next transition, in which the stream is closed.
ProgramStateRef State = C.getState();
ProgramStateRef State = C.getState();
State = State->bindLoc(SimpleStreamTrait, FileDesc, Closed);
State = State->bindLoc(SimpleStreamTrait, FileDesc, Closed);
C.addTransition(State);
C.addTransition(State);
}
}
}
}


ProgramStateRef SimpleStreamModel::checkPointerEscape(
ProgramStateRef State, const InvalidatedSymbols &Escaped,
const CallEvent *Call, PointerEscapeKind Kind) const {
// If we know that the call cannot close a file, there is nothing to do.
if (Kind == PSK_DirectEscapeOnCall && guaranteedNotToCloseFile(*Call))
return State;

for (auto I = Escaped.begin(), E = Escaped.end(); I != E; ++I) {
SymbolRef Sym = *I;

// The symbol escaped. Optimistically, assume that the corresponding file
// handle will be closed somewhere else.
// FIXME: We're doing double lookup here, and we could have probably just
// deleted the binding, because our checker doesn't discriminate between
// different kind of unknowns.
if (State->hasAnyBinding(SimpleStreamTrait, Sym))
State = State->bindLoc(SimpleStreamTrait, Sym, UnknownVal());
}
return State;
}

namespace {
namespace {
class SimpleStreamChecker
class SimpleStreamChecker
: public Checker<check::PreCall, check::DeadSymbols> {
: public Checker<check::PreCall, check::DeadSymbols> {
CallDescription CloseFn;
CallDescription CloseFn;


std::unique_ptr<BugType> DoubleCloseBugType;
std::unique_ptr<BugType> DoubleCloseBugType;
std::unique_ptr<BugType> LeakBugType;
std::unique_ptr<BugType> LeakBugType;