SimpleStreamChecker.cpp revision 32133cfb333510ba94aff040067713c0b32d58c5
1//===-- SimpleStreamChecker.cpp -----------------------------------------*- C++ -*--//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Defines a checker for proper use of fopen/fclose APIs.
11//   - If a file has been closed with fclose, it should not be accessed again.
12//   Accessing a closed file results in undefined behavior.
13//   - If a file was opened with fopen, it must be closed with fclose before
14//   the execution ends. Failing to do so results in a resource leak.
15//
16//===----------------------------------------------------------------------===//
17
18#include "ClangSACheckers.h"
19#include "clang/StaticAnalyzer/Core/Checker.h"
20#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
21#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
22
23using namespace clang;
24using namespace ento;
25
26namespace {
27typedef llvm::SmallVector<SymbolRef, 2> SymbolVector;
28
29struct StreamState {
30private:
31  enum Kind { Opened, Closed } K;
32  StreamState(Kind InK) : K(InK) { }
33
34public:
35  bool isOpened() const { return K == Opened; }
36  bool isClosed() const { return K == Closed; }
37
38  static StreamState getOpened() { return StreamState(Opened); }
39  static StreamState getClosed() { return StreamState(Closed); }
40
41  bool operator==(const StreamState &X) const {
42    return K == X.K;
43  }
44  void Profile(llvm::FoldingSetNodeID &ID) const {
45    ID.AddInteger(K);
46  }
47};
48
49class SimpleStreamChecker: public Checker<check::PostStmt<CallExpr>,
50                                          check::PreStmt<CallExpr>,
51                                          check::DeadSymbols > {
52
53  mutable IdentifierInfo *IIfopen, *IIfclose;
54
55  mutable OwningPtr<BugType> DoubleCloseBugType;
56  mutable OwningPtr<BugType> LeakBugType;
57
58  void initIdentifierInfo(ASTContext &Ctx) const;
59
60  void reportDoubleClose(SymbolRef FileDescSym,
61                         const CallExpr *Call,
62                         CheckerContext &C) const;
63
64   void reportLeaks(SymbolVector LeakedStreams,
65                    CheckerContext &C,
66                    ExplodedNode *ErrNode) const;
67
68public:
69  SimpleStreamChecker();
70
71  /// Process fopen.
72  void checkPostStmt(const CallExpr *Call, CheckerContext &C) const;
73  /// Process fclose.
74  void checkPreStmt(const CallExpr *Call, CheckerContext &C) const;
75
76  void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
77};
78
79} // end anonymous namespace
80
81/// The state of the checker is a map from tracked stream symbols to their
82/// state. Let's store it in the ProgramState.
83REGISTER_MAP_WITH_PROGRAMSTATE(StreamMap, SymbolRef, StreamState)
84
85SimpleStreamChecker::SimpleStreamChecker() : IIfopen(0), IIfclose(0) {
86  // Initialize the bug types.
87  DoubleCloseBugType.reset(new BugType("Double fclose",
88                                       "Unix Stream API Error"));
89
90  LeakBugType.reset(new BugType("Resource Leak",
91                                "Unix Stream API Error"));
92  // Sinks are higher importance bugs as well as calls to assert() or exit(0).
93  LeakBugType->setSuppressOnSink(true);
94}
95
96void SimpleStreamChecker::checkPostStmt(const CallExpr *Call,
97                                        CheckerContext &C) const {
98  initIdentifierInfo(C.getASTContext());
99
100  if (C.getCalleeIdentifier(Call) != IIfopen)
101    return;
102
103  // Get the symbolic value corresponding to the file handle.
104  SymbolRef FileDesc = C.getSVal(Call).getAsSymbol();
105  if (!FileDesc)
106    return;
107
108  // Generate the next transition (an edge in the exploded graph).
109  ProgramStateRef State = C.getState();
110  State = State->set<StreamMap>(FileDesc, StreamState::getOpened());
111  C.addTransition(State);
112}
113
114void SimpleStreamChecker::checkPreStmt(const CallExpr *Call,
115                                       CheckerContext &C) const {
116  initIdentifierInfo(C.getASTContext());
117
118  if (C.getCalleeIdentifier(Call) != IIfclose || Call->getNumArgs() != 1)
119    return;
120
121  // Get the symbolic value corresponding to the file handle.
122  SymbolRef FileDesc = C.getSVal(Call->getArg(0)).getAsSymbol();
123  if (!FileDesc)
124    return;
125
126  // Check if the stream has already been closed.
127  ProgramStateRef State = C.getState();
128  const StreamState *SS = State->get<StreamMap>(FileDesc);
129  if (SS && SS->isClosed())
130    reportDoubleClose(FileDesc, Call, C);
131
132  // Generate the next transition, in which the stream is closed.
133  State = State->set<StreamMap>(FileDesc, StreamState::getClosed());
134  C.addTransition(State);
135}
136
137void SimpleStreamChecker::checkDeadSymbols(SymbolReaper &SymReaper,
138                                           CheckerContext &C) const {
139  ProgramStateRef State = C.getState();
140  StreamMapTy TrackedStreams = State->get<StreamMap>();
141  SymbolVector LeakedStreams;
142  for (StreamMapTy::iterator I = TrackedStreams.begin(),
143                           E = TrackedStreams.end(); I != E; ++I) {
144    SymbolRef Sym = I->first;
145    if (SymReaper.isDead(Sym)) {
146      const StreamState &SS = I->second;
147      if (SS.isOpened()) {
148        // If a symbolic region is NULL, assume that allocation failed on
149        // this path and do not report a leak.
150        if (!State->getConstraintManager().isNull(State, Sym).isTrue())
151          LeakedStreams.push_back(Sym);
152      }
153
154      // Remove the dead symbol from the streams map.
155      State = State->remove<StreamMap>(Sym);
156    }
157  }
158
159  ExplodedNode *N = C.addTransition(State);
160  reportLeaks(LeakedStreams, C, N);
161}
162
163void SimpleStreamChecker::reportDoubleClose(SymbolRef FileDescSym,
164                                            const CallExpr *CallExpr,
165                                            CheckerContext &C) const {
166  // We reached a bug, stop exploring the path here by generating a sink.
167  ExplodedNode *ErrNode = C.generateSink();
168  // If we've already reached this node on another path, return.
169  if (!ErrNode)
170    return;
171
172  // Generate the report.
173  BugReport *R = new BugReport(*DoubleCloseBugType,
174      "Closing a previously closed file stream", ErrNode);
175  R->addRange(CallExpr->getSourceRange());
176  R->markInteresting(FileDescSym);
177  C.EmitReport(R);
178}
179
180void SimpleStreamChecker::reportLeaks(SymbolVector LeakedStreams,
181                                               CheckerContext &C,
182                                               ExplodedNode *ErrNode) const {
183  // Attach bug reports to the leak node.
184  // TODO: Identify the leaked file descriptor.
185  for (llvm::SmallVector<SymbolRef, 2>::iterator
186      I = LeakedStreams.begin(), E = LeakedStreams.end(); I != E; ++I) {
187    BugReport *R = new BugReport(*LeakBugType,
188        "Opened file is never closed; potential resource leak", ErrNode);
189    R->markInteresting(*I);
190    C.EmitReport(R);
191  }
192}
193
194void SimpleStreamChecker::initIdentifierInfo(ASTContext &Ctx) const {
195  if (IIfopen)
196    return;
197  IIfopen = &Ctx.Idents.get("fopen");
198  IIfclose = &Ctx.Idents.get("fclose");
199}
200
201void ento::registerSimpleStreamChecker(CheckerManager &mgr) {
202  mgr.registerChecker<SimpleStreamChecker>();
203}
204