17330f729Sjoerg //===-- SimpleStreamChecker.cpp -----------------------------------------*- C++ -*--//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // Defines a checker for proper use of fopen/fclose APIs.
107330f729Sjoerg // - If a file has been closed with fclose, it should not be accessed again.
117330f729Sjoerg // Accessing a closed file results in undefined behavior.
127330f729Sjoerg // - If a file was opened with fopen, it must be closed with fclose before
137330f729Sjoerg // the execution ends. Failing to do so results in a resource leak.
147330f729Sjoerg //
157330f729Sjoerg //===----------------------------------------------------------------------===//
167330f729Sjoerg
177330f729Sjoerg #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
187330f729Sjoerg #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
197330f729Sjoerg #include "clang/StaticAnalyzer/Core/Checker.h"
207330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
217330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
227330f729Sjoerg #include <utility>
237330f729Sjoerg
247330f729Sjoerg using namespace clang;
257330f729Sjoerg using namespace ento;
267330f729Sjoerg
277330f729Sjoerg namespace {
287330f729Sjoerg typedef SmallVector<SymbolRef, 2> SymbolVector;
297330f729Sjoerg
307330f729Sjoerg struct StreamState {
317330f729Sjoerg private:
327330f729Sjoerg enum Kind { Opened, Closed } K;
StreamState__anonf5f2bee40111::StreamState337330f729Sjoerg StreamState(Kind InK) : K(InK) { }
347330f729Sjoerg
357330f729Sjoerg public:
isOpened__anonf5f2bee40111::StreamState367330f729Sjoerg bool isOpened() const { return K == Opened; }
isClosed__anonf5f2bee40111::StreamState377330f729Sjoerg bool isClosed() const { return K == Closed; }
387330f729Sjoerg
getOpened__anonf5f2bee40111::StreamState397330f729Sjoerg static StreamState getOpened() { return StreamState(Opened); }
getClosed__anonf5f2bee40111::StreamState407330f729Sjoerg static StreamState getClosed() { return StreamState(Closed); }
417330f729Sjoerg
operator ==__anonf5f2bee40111::StreamState427330f729Sjoerg bool operator==(const StreamState &X) const {
437330f729Sjoerg return K == X.K;
447330f729Sjoerg }
Profile__anonf5f2bee40111::StreamState457330f729Sjoerg void Profile(llvm::FoldingSetNodeID &ID) const {
467330f729Sjoerg ID.AddInteger(K);
477330f729Sjoerg }
487330f729Sjoerg };
497330f729Sjoerg
507330f729Sjoerg class SimpleStreamChecker : public Checker<check::PostCall,
517330f729Sjoerg check::PreCall,
527330f729Sjoerg check::DeadSymbols,
537330f729Sjoerg check::PointerEscape> {
547330f729Sjoerg CallDescription OpenFn, CloseFn;
557330f729Sjoerg
567330f729Sjoerg std::unique_ptr<BugType> DoubleCloseBugType;
577330f729Sjoerg std::unique_ptr<BugType> LeakBugType;
587330f729Sjoerg
597330f729Sjoerg void reportDoubleClose(SymbolRef FileDescSym,
607330f729Sjoerg const CallEvent &Call,
617330f729Sjoerg CheckerContext &C) const;
627330f729Sjoerg
637330f729Sjoerg void reportLeaks(ArrayRef<SymbolRef> LeakedStreams, CheckerContext &C,
647330f729Sjoerg ExplodedNode *ErrNode) const;
657330f729Sjoerg
667330f729Sjoerg bool guaranteedNotToCloseFile(const CallEvent &Call) const;
677330f729Sjoerg
687330f729Sjoerg public:
697330f729Sjoerg SimpleStreamChecker();
707330f729Sjoerg
717330f729Sjoerg /// Process fopen.
727330f729Sjoerg void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
737330f729Sjoerg /// Process fclose.
747330f729Sjoerg void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
757330f729Sjoerg
767330f729Sjoerg void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
777330f729Sjoerg
787330f729Sjoerg /// Stop tracking addresses which escape.
797330f729Sjoerg ProgramStateRef checkPointerEscape(ProgramStateRef State,
807330f729Sjoerg const InvalidatedSymbols &Escaped,
817330f729Sjoerg const CallEvent *Call,
827330f729Sjoerg PointerEscapeKind Kind) const;
837330f729Sjoerg };
847330f729Sjoerg
857330f729Sjoerg } // end anonymous namespace
867330f729Sjoerg
877330f729Sjoerg /// The state of the checker is a map from tracked stream symbols to their
887330f729Sjoerg /// state. Let's store it in the ProgramState.
897330f729Sjoerg REGISTER_MAP_WITH_PROGRAMSTATE(StreamMap, SymbolRef, StreamState)
907330f729Sjoerg
917330f729Sjoerg namespace {
927330f729Sjoerg class StopTrackingCallback final : public SymbolVisitor {
937330f729Sjoerg ProgramStateRef state;
947330f729Sjoerg public:
StopTrackingCallback(ProgramStateRef st)957330f729Sjoerg StopTrackingCallback(ProgramStateRef st) : state(std::move(st)) {}
getState() const967330f729Sjoerg ProgramStateRef getState() const { return state; }
977330f729Sjoerg
VisitSymbol(SymbolRef sym)987330f729Sjoerg bool VisitSymbol(SymbolRef sym) override {
997330f729Sjoerg state = state->remove<StreamMap>(sym);
1007330f729Sjoerg return true;
1017330f729Sjoerg }
1027330f729Sjoerg };
1037330f729Sjoerg } // end anonymous namespace
1047330f729Sjoerg
SimpleStreamChecker()1057330f729Sjoerg SimpleStreamChecker::SimpleStreamChecker()
1067330f729Sjoerg : OpenFn("fopen"), CloseFn("fclose", 1) {
1077330f729Sjoerg // Initialize the bug types.
1087330f729Sjoerg DoubleCloseBugType.reset(
1097330f729Sjoerg new BugType(this, "Double fclose", "Unix Stream API Error"));
1107330f729Sjoerg
1117330f729Sjoerg // Sinks are higher importance bugs as well as calls to assert() or exit(0).
1127330f729Sjoerg LeakBugType.reset(
1137330f729Sjoerg new BugType(this, "Resource Leak", "Unix Stream API Error",
1147330f729Sjoerg /*SuppressOnSink=*/true));
1157330f729Sjoerg }
1167330f729Sjoerg
checkPostCall(const CallEvent & Call,CheckerContext & C) const1177330f729Sjoerg void SimpleStreamChecker::checkPostCall(const CallEvent &Call,
1187330f729Sjoerg CheckerContext &C) const {
1197330f729Sjoerg if (!Call.isGlobalCFunction())
1207330f729Sjoerg return;
1217330f729Sjoerg
1227330f729Sjoerg if (!Call.isCalled(OpenFn))
1237330f729Sjoerg return;
1247330f729Sjoerg
1257330f729Sjoerg // Get the symbolic value corresponding to the file handle.
1267330f729Sjoerg SymbolRef FileDesc = Call.getReturnValue().getAsSymbol();
1277330f729Sjoerg if (!FileDesc)
1287330f729Sjoerg return;
1297330f729Sjoerg
1307330f729Sjoerg // Generate the next transition (an edge in the exploded graph).
1317330f729Sjoerg ProgramStateRef State = C.getState();
1327330f729Sjoerg State = State->set<StreamMap>(FileDesc, StreamState::getOpened());
1337330f729Sjoerg C.addTransition(State);
1347330f729Sjoerg }
1357330f729Sjoerg
checkPreCall(const CallEvent & Call,CheckerContext & C) const1367330f729Sjoerg void SimpleStreamChecker::checkPreCall(const CallEvent &Call,
1377330f729Sjoerg CheckerContext &C) const {
1387330f729Sjoerg if (!Call.isGlobalCFunction())
1397330f729Sjoerg return;
1407330f729Sjoerg
1417330f729Sjoerg if (!Call.isCalled(CloseFn))
1427330f729Sjoerg return;
1437330f729Sjoerg
1447330f729Sjoerg // Get the symbolic value corresponding to the file handle.
1457330f729Sjoerg SymbolRef FileDesc = Call.getArgSVal(0).getAsSymbol();
1467330f729Sjoerg if (!FileDesc)
1477330f729Sjoerg return;
1487330f729Sjoerg
1497330f729Sjoerg // Check if the stream has already been closed.
1507330f729Sjoerg ProgramStateRef State = C.getState();
1517330f729Sjoerg const StreamState *SS = State->get<StreamMap>(FileDesc);
1527330f729Sjoerg if (SS && SS->isClosed()) {
1537330f729Sjoerg reportDoubleClose(FileDesc, Call, C);
1547330f729Sjoerg return;
1557330f729Sjoerg }
1567330f729Sjoerg
1577330f729Sjoerg // Generate the next transition, in which the stream is closed.
1587330f729Sjoerg State = State->set<StreamMap>(FileDesc, StreamState::getClosed());
1597330f729Sjoerg C.addTransition(State);
1607330f729Sjoerg }
1617330f729Sjoerg
isLeaked(SymbolRef Sym,const StreamState & SS,bool IsSymDead,ProgramStateRef State)1627330f729Sjoerg static bool isLeaked(SymbolRef Sym, const StreamState &SS,
1637330f729Sjoerg bool IsSymDead, ProgramStateRef State) {
1647330f729Sjoerg if (IsSymDead && SS.isOpened()) {
1657330f729Sjoerg // If a symbol is NULL, assume that fopen failed on this path.
1667330f729Sjoerg // A symbol should only be considered leaked if it is non-null.
1677330f729Sjoerg ConstraintManager &CMgr = State->getConstraintManager();
1687330f729Sjoerg ConditionTruthVal OpenFailed = CMgr.isNull(State, Sym);
1697330f729Sjoerg return !OpenFailed.isConstrainedTrue();
1707330f729Sjoerg }
1717330f729Sjoerg return false;
1727330f729Sjoerg }
1737330f729Sjoerg
checkDeadSymbols(SymbolReaper & SymReaper,CheckerContext & C) const1747330f729Sjoerg void SimpleStreamChecker::checkDeadSymbols(SymbolReaper &SymReaper,
1757330f729Sjoerg CheckerContext &C) const {
1767330f729Sjoerg ProgramStateRef State = C.getState();
1777330f729Sjoerg SymbolVector LeakedStreams;
1787330f729Sjoerg StreamMapTy TrackedStreams = State->get<StreamMap>();
1797330f729Sjoerg for (StreamMapTy::iterator I = TrackedStreams.begin(),
1807330f729Sjoerg E = TrackedStreams.end(); I != E; ++I) {
1817330f729Sjoerg SymbolRef Sym = I->first;
1827330f729Sjoerg bool IsSymDead = SymReaper.isDead(Sym);
1837330f729Sjoerg
1847330f729Sjoerg // Collect leaked symbols.
1857330f729Sjoerg if (isLeaked(Sym, I->second, IsSymDead, State))
1867330f729Sjoerg LeakedStreams.push_back(Sym);
1877330f729Sjoerg
1887330f729Sjoerg // Remove the dead symbol from the streams map.
1897330f729Sjoerg if (IsSymDead)
1907330f729Sjoerg State = State->remove<StreamMap>(Sym);
1917330f729Sjoerg }
1927330f729Sjoerg
1937330f729Sjoerg ExplodedNode *N = C.generateNonFatalErrorNode(State);
1947330f729Sjoerg if (!N)
1957330f729Sjoerg return;
1967330f729Sjoerg reportLeaks(LeakedStreams, C, N);
1977330f729Sjoerg }
1987330f729Sjoerg
reportDoubleClose(SymbolRef FileDescSym,const CallEvent & Call,CheckerContext & C) const1997330f729Sjoerg void SimpleStreamChecker::reportDoubleClose(SymbolRef FileDescSym,
2007330f729Sjoerg const CallEvent &Call,
2017330f729Sjoerg CheckerContext &C) const {
2027330f729Sjoerg // We reached a bug, stop exploring the path here by generating a sink.
2037330f729Sjoerg ExplodedNode *ErrNode = C.generateErrorNode();
2047330f729Sjoerg // If we've already reached this node on another path, return.
2057330f729Sjoerg if (!ErrNode)
2067330f729Sjoerg return;
2077330f729Sjoerg
2087330f729Sjoerg // Generate the report.
2097330f729Sjoerg auto R = std::make_unique<PathSensitiveBugReport>(
2107330f729Sjoerg *DoubleCloseBugType, "Closing a previously closed file stream", ErrNode);
2117330f729Sjoerg R->addRange(Call.getSourceRange());
2127330f729Sjoerg R->markInteresting(FileDescSym);
2137330f729Sjoerg C.emitReport(std::move(R));
2147330f729Sjoerg }
2157330f729Sjoerg
reportLeaks(ArrayRef<SymbolRef> LeakedStreams,CheckerContext & C,ExplodedNode * ErrNode) const2167330f729Sjoerg void SimpleStreamChecker::reportLeaks(ArrayRef<SymbolRef> LeakedStreams,
2177330f729Sjoerg CheckerContext &C,
2187330f729Sjoerg ExplodedNode *ErrNode) const {
2197330f729Sjoerg // Attach bug reports to the leak node.
2207330f729Sjoerg // TODO: Identify the leaked file descriptor.
2217330f729Sjoerg for (SymbolRef LeakedStream : LeakedStreams) {
2227330f729Sjoerg auto R = std::make_unique<PathSensitiveBugReport>(
2237330f729Sjoerg *LeakBugType, "Opened file is never closed; potential resource leak",
2247330f729Sjoerg ErrNode);
2257330f729Sjoerg R->markInteresting(LeakedStream);
2267330f729Sjoerg C.emitReport(std::move(R));
2277330f729Sjoerg }
2287330f729Sjoerg }
2297330f729Sjoerg
guaranteedNotToCloseFile(const CallEvent & Call) const2307330f729Sjoerg bool SimpleStreamChecker::guaranteedNotToCloseFile(const CallEvent &Call) const{
2317330f729Sjoerg // If it's not in a system header, assume it might close a file.
2327330f729Sjoerg if (!Call.isInSystemHeader())
2337330f729Sjoerg return false;
2347330f729Sjoerg
2357330f729Sjoerg // Handle cases where we know a buffer's /address/ can escape.
2367330f729Sjoerg if (Call.argumentsMayEscape())
2377330f729Sjoerg return false;
2387330f729Sjoerg
2397330f729Sjoerg // Note, even though fclose closes the file, we do not list it here
2407330f729Sjoerg // since the checker is modeling the call.
2417330f729Sjoerg
2427330f729Sjoerg return true;
2437330f729Sjoerg }
2447330f729Sjoerg
2457330f729Sjoerg // If the pointer we are tracking escaped, do not track the symbol as
2467330f729Sjoerg // we cannot reason about it anymore.
2477330f729Sjoerg ProgramStateRef
checkPointerEscape(ProgramStateRef State,const InvalidatedSymbols & Escaped,const CallEvent * Call,PointerEscapeKind Kind) const2487330f729Sjoerg SimpleStreamChecker::checkPointerEscape(ProgramStateRef State,
2497330f729Sjoerg const InvalidatedSymbols &Escaped,
2507330f729Sjoerg const CallEvent *Call,
2517330f729Sjoerg PointerEscapeKind Kind) const {
2527330f729Sjoerg // If we know that the call cannot close a file, there is nothing to do.
2537330f729Sjoerg if (Kind == PSK_DirectEscapeOnCall && guaranteedNotToCloseFile(*Call)) {
2547330f729Sjoerg return State;
2557330f729Sjoerg }
2567330f729Sjoerg
2577330f729Sjoerg for (InvalidatedSymbols::const_iterator I = Escaped.begin(),
2587330f729Sjoerg E = Escaped.end();
2597330f729Sjoerg I != E; ++I) {
2607330f729Sjoerg SymbolRef Sym = *I;
2617330f729Sjoerg
2627330f729Sjoerg // The symbol escaped. Optimistically, assume that the corresponding file
2637330f729Sjoerg // handle will be closed somewhere else.
2647330f729Sjoerg State = State->remove<StreamMap>(Sym);
2657330f729Sjoerg }
2667330f729Sjoerg return State;
2677330f729Sjoerg }
2687330f729Sjoerg
registerSimpleStreamChecker(CheckerManager & mgr)2697330f729Sjoerg void ento::registerSimpleStreamChecker(CheckerManager &mgr) {
2707330f729Sjoerg mgr.registerChecker<SimpleStreamChecker>();
2717330f729Sjoerg }
2727330f729Sjoerg
2737330f729Sjoerg // This checker should be enabled regardless of how language options are set.
shouldRegisterSimpleStreamChecker(const CheckerManager & mgr)274*e038c9c4Sjoerg bool ento::shouldRegisterSimpleStreamChecker(const CheckerManager &mgr) {
2757330f729Sjoerg return true;
2767330f729Sjoerg }
277