StreamChecker.cpp revision 0621c45dcd4c5f43df0de5a2febae525d3287b74
1//===-- StreamChecker.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// This file defines checkers that model and check stream handling functions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ClangSACheckers.h"
15#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
16#include "clang/StaticAnalyzer/Core/Checker.h"
17#include "clang/StaticAnalyzer/Core/CheckerManager.h"
18#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
20#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
21#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
22#include "llvm/ADT/ImmutableMap.h"
23
24using namespace clang;
25using namespace ento;
26
27namespace {
28
29struct StreamState {
30  enum Kind { Opened, Closed, OpenFailed, Escaped } K;
31  const Stmt *S;
32
33  StreamState(Kind k, const Stmt *s) : K(k), S(s) {}
34
35  bool isOpened() const { return K == Opened; }
36  bool isClosed() const { return K == Closed; }
37  //bool isOpenFailed() const { return K == OpenFailed; }
38  //bool isEscaped() const { return K == Escaped; }
39
40  bool operator==(const StreamState &X) const {
41    return K == X.K && S == X.S;
42  }
43
44  static StreamState getOpened(const Stmt *s) { return StreamState(Opened, s); }
45  static StreamState getClosed(const Stmt *s) { return StreamState(Closed, s); }
46  static StreamState getOpenFailed(const Stmt *s) {
47    return StreamState(OpenFailed, s);
48  }
49  static StreamState getEscaped(const Stmt *s) {
50    return StreamState(Escaped, s);
51  }
52
53  void Profile(llvm::FoldingSetNodeID &ID) const {
54    ID.AddInteger(K);
55    ID.AddPointer(S);
56  }
57};
58
59class StreamChecker : public Checker<eval::Call,
60                                     check::DeadSymbols > {
61  mutable IdentifierInfo *II_fopen, *II_tmpfile, *II_fclose, *II_fread,
62                 *II_fwrite,
63                 *II_fseek, *II_ftell, *II_rewind, *II_fgetpos, *II_fsetpos,
64                 *II_clearerr, *II_feof, *II_ferror, *II_fileno;
65  mutable OwningPtr<BuiltinBug> BT_nullfp, BT_illegalwhence,
66                                      BT_doubleclose, BT_ResourceLeak;
67
68public:
69  StreamChecker()
70    : II_fopen(0), II_tmpfile(0) ,II_fclose(0), II_fread(0), II_fwrite(0),
71      II_fseek(0), II_ftell(0), II_rewind(0), II_fgetpos(0), II_fsetpos(0),
72      II_clearerr(0), II_feof(0), II_ferror(0), II_fileno(0) {}
73
74  bool evalCall(const CallExpr *CE, CheckerContext &C) const;
75  void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
76
77private:
78  void Fopen(CheckerContext &C, const CallExpr *CE) const;
79  void Tmpfile(CheckerContext &C, const CallExpr *CE) const;
80  void Fclose(CheckerContext &C, const CallExpr *CE) const;
81  void Fread(CheckerContext &C, const CallExpr *CE) const;
82  void Fwrite(CheckerContext &C, const CallExpr *CE) const;
83  void Fseek(CheckerContext &C, const CallExpr *CE) const;
84  void Ftell(CheckerContext &C, const CallExpr *CE) const;
85  void Rewind(CheckerContext &C, const CallExpr *CE) const;
86  void Fgetpos(CheckerContext &C, const CallExpr *CE) const;
87  void Fsetpos(CheckerContext &C, const CallExpr *CE) const;
88  void Clearerr(CheckerContext &C, const CallExpr *CE) const;
89  void Feof(CheckerContext &C, const CallExpr *CE) const;
90  void Ferror(CheckerContext &C, const CallExpr *CE) const;
91  void Fileno(CheckerContext &C, const CallExpr *CE) const;
92
93  void OpenFileAux(CheckerContext &C, const CallExpr *CE) const;
94
95  ProgramStateRef CheckNullStream(SVal SV, ProgramStateRef state,
96                                 CheckerContext &C) const;
97  ProgramStateRef CheckDoubleClose(const CallExpr *CE, ProgramStateRef state,
98                                 CheckerContext &C) const;
99};
100
101} // end anonymous namespace
102
103REGISTER_MAP_WITH_PROGRAMSTATE(StreamMap, SymbolRef, StreamState)
104
105
106bool StreamChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
107  const FunctionDecl *FD = C.getCalleeDecl(CE);
108  if (!FD || FD->getKind() != Decl::Function)
109    return false;
110
111  ASTContext &Ctx = C.getASTContext();
112  if (!II_fopen)
113    II_fopen = &Ctx.Idents.get("fopen");
114  if (!II_tmpfile)
115    II_tmpfile = &Ctx.Idents.get("tmpfile");
116  if (!II_fclose)
117    II_fclose = &Ctx.Idents.get("fclose");
118  if (!II_fread)
119    II_fread = &Ctx.Idents.get("fread");
120  if (!II_fwrite)
121    II_fwrite = &Ctx.Idents.get("fwrite");
122  if (!II_fseek)
123    II_fseek = &Ctx.Idents.get("fseek");
124  if (!II_ftell)
125    II_ftell = &Ctx.Idents.get("ftell");
126  if (!II_rewind)
127    II_rewind = &Ctx.Idents.get("rewind");
128  if (!II_fgetpos)
129    II_fgetpos = &Ctx.Idents.get("fgetpos");
130  if (!II_fsetpos)
131    II_fsetpos = &Ctx.Idents.get("fsetpos");
132  if (!II_clearerr)
133    II_clearerr = &Ctx.Idents.get("clearerr");
134  if (!II_feof)
135    II_feof = &Ctx.Idents.get("feof");
136  if (!II_ferror)
137    II_ferror = &Ctx.Idents.get("ferror");
138  if (!II_fileno)
139    II_fileno = &Ctx.Idents.get("fileno");
140
141  if (FD->getIdentifier() == II_fopen) {
142    Fopen(C, CE);
143    return true;
144  }
145  if (FD->getIdentifier() == II_tmpfile) {
146    Tmpfile(C, CE);
147    return true;
148  }
149  if (FD->getIdentifier() == II_fclose) {
150    Fclose(C, CE);
151    return true;
152  }
153  if (FD->getIdentifier() == II_fread) {
154    Fread(C, CE);
155    return true;
156  }
157  if (FD->getIdentifier() == II_fwrite) {
158    Fwrite(C, CE);
159    return true;
160  }
161  if (FD->getIdentifier() == II_fseek) {
162    Fseek(C, CE);
163    return true;
164  }
165  if (FD->getIdentifier() == II_ftell) {
166    Ftell(C, CE);
167    return true;
168  }
169  if (FD->getIdentifier() == II_rewind) {
170    Rewind(C, CE);
171    return true;
172  }
173  if (FD->getIdentifier() == II_fgetpos) {
174    Fgetpos(C, CE);
175    return true;
176  }
177  if (FD->getIdentifier() == II_fsetpos) {
178    Fsetpos(C, CE);
179    return true;
180  }
181  if (FD->getIdentifier() == II_clearerr) {
182    Clearerr(C, CE);
183    return true;
184  }
185  if (FD->getIdentifier() == II_feof) {
186    Feof(C, CE);
187    return true;
188  }
189  if (FD->getIdentifier() == II_ferror) {
190    Ferror(C, CE);
191    return true;
192  }
193  if (FD->getIdentifier() == II_fileno) {
194    Fileno(C, CE);
195    return true;
196  }
197
198  return false;
199}
200
201void StreamChecker::Fopen(CheckerContext &C, const CallExpr *CE) const {
202  OpenFileAux(C, CE);
203}
204
205void StreamChecker::Tmpfile(CheckerContext &C, const CallExpr *CE) const {
206  OpenFileAux(C, CE);
207}
208
209void StreamChecker::OpenFileAux(CheckerContext &C, const CallExpr *CE) const {
210  ProgramStateRef state = C.getState();
211  SValBuilder &svalBuilder = C.getSValBuilder();
212  const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
213  DefinedSVal RetVal = svalBuilder.conjureSymbolVal(0, CE, LCtx, C.blockCount())
214      .castAs<DefinedSVal>();
215  state = state->BindExpr(CE, C.getLocationContext(), RetVal);
216
217  ConstraintManager &CM = C.getConstraintManager();
218  // Bifurcate the state into two: one with a valid FILE* pointer, the other
219  // with a NULL.
220  ProgramStateRef stateNotNull, stateNull;
221  llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, RetVal);
222
223  if (SymbolRef Sym = RetVal.getAsSymbol()) {
224    // if RetVal is not NULL, set the symbol's state to Opened.
225    stateNotNull =
226      stateNotNull->set<StreamMap>(Sym,StreamState::getOpened(CE));
227    stateNull =
228      stateNull->set<StreamMap>(Sym, StreamState::getOpenFailed(CE));
229
230    C.addTransition(stateNotNull);
231    C.addTransition(stateNull);
232  }
233}
234
235void StreamChecker::Fclose(CheckerContext &C, const CallExpr *CE) const {
236  ProgramStateRef state = CheckDoubleClose(CE, C.getState(), C);
237  if (state)
238    C.addTransition(state);
239}
240
241void StreamChecker::Fread(CheckerContext &C, const CallExpr *CE) const {
242  ProgramStateRef state = C.getState();
243  if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
244                       state, C))
245    return;
246}
247
248void StreamChecker::Fwrite(CheckerContext &C, const CallExpr *CE) const {
249  ProgramStateRef state = C.getState();
250  if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
251                       state, C))
252    return;
253}
254
255void StreamChecker::Fseek(CheckerContext &C, const CallExpr *CE) const {
256  ProgramStateRef state = C.getState();
257  if (!(state = CheckNullStream(state->getSVal(CE->getArg(0),
258                                               C.getLocationContext()), state, C)))
259    return;
260  // Check the legality of the 'whence' argument of 'fseek'.
261  SVal Whence = state->getSVal(CE->getArg(2), C.getLocationContext());
262  Optional<nonloc::ConcreteInt> CI = Whence.getAs<nonloc::ConcreteInt>();
263
264  if (!CI)
265    return;
266
267  int64_t x = CI->getValue().getSExtValue();
268  if (x >= 0 && x <= 2)
269    return;
270
271  if (ExplodedNode *N = C.addTransition(state)) {
272    if (!BT_illegalwhence)
273      BT_illegalwhence.reset(new BuiltinBug("Illegal whence argument",
274					"The whence argument to fseek() should be "
275					"SEEK_SET, SEEK_END, or SEEK_CUR."));
276    BugReport *R = new BugReport(*BT_illegalwhence,
277				 BT_illegalwhence->getDescription(), N);
278    C.emitReport(R);
279  }
280}
281
282void StreamChecker::Ftell(CheckerContext &C, const CallExpr *CE) const {
283  ProgramStateRef state = C.getState();
284  if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
285                       state, C))
286    return;
287}
288
289void StreamChecker::Rewind(CheckerContext &C, const CallExpr *CE) const {
290  ProgramStateRef state = C.getState();
291  if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
292                       state, C))
293    return;
294}
295
296void StreamChecker::Fgetpos(CheckerContext &C, const CallExpr *CE) const {
297  ProgramStateRef state = C.getState();
298  if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
299                       state, C))
300    return;
301}
302
303void StreamChecker::Fsetpos(CheckerContext &C, const CallExpr *CE) const {
304  ProgramStateRef state = C.getState();
305  if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
306                       state, C))
307    return;
308}
309
310void StreamChecker::Clearerr(CheckerContext &C, const CallExpr *CE) const {
311  ProgramStateRef state = C.getState();
312  if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
313                       state, C))
314    return;
315}
316
317void StreamChecker::Feof(CheckerContext &C, const CallExpr *CE) const {
318  ProgramStateRef state = C.getState();
319  if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
320                       state, C))
321    return;
322}
323
324void StreamChecker::Ferror(CheckerContext &C, const CallExpr *CE) const {
325  ProgramStateRef state = C.getState();
326  if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
327                       state, C))
328    return;
329}
330
331void StreamChecker::Fileno(CheckerContext &C, const CallExpr *CE) const {
332  ProgramStateRef state = C.getState();
333  if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
334                       state, C))
335    return;
336}
337
338ProgramStateRef StreamChecker::CheckNullStream(SVal SV, ProgramStateRef state,
339                                    CheckerContext &C) const {
340  Optional<DefinedSVal> DV = SV.getAs<DefinedSVal>();
341  if (!DV)
342    return 0;
343
344  ConstraintManager &CM = C.getConstraintManager();
345  ProgramStateRef stateNotNull, stateNull;
346  llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
347
348  if (!stateNotNull && stateNull) {
349    if (ExplodedNode *N = C.generateSink(stateNull)) {
350      if (!BT_nullfp)
351        BT_nullfp.reset(new BuiltinBug("NULL stream pointer",
352                                     "Stream pointer might be NULL."));
353      BugReport *R =new BugReport(*BT_nullfp, BT_nullfp->getDescription(), N);
354      C.emitReport(R);
355    }
356    return 0;
357  }
358  return stateNotNull;
359}
360
361ProgramStateRef StreamChecker::CheckDoubleClose(const CallExpr *CE,
362                                               ProgramStateRef state,
363                                               CheckerContext &C) const {
364  SymbolRef Sym =
365    state->getSVal(CE->getArg(0), C.getLocationContext()).getAsSymbol();
366  if (!Sym)
367    return state;
368
369  const StreamState *SS = state->get<StreamMap>(Sym);
370
371  // If the file stream is not tracked, return.
372  if (!SS)
373    return state;
374
375  // Check: Double close a File Descriptor could cause undefined behaviour.
376  // Conforming to man-pages
377  if (SS->isClosed()) {
378    ExplodedNode *N = C.generateSink();
379    if (N) {
380      if (!BT_doubleclose)
381        BT_doubleclose.reset(new BuiltinBug("Double fclose",
382                                        "Try to close a file Descriptor already"
383                                        " closed. Cause undefined behaviour."));
384      BugReport *R = new BugReport(*BT_doubleclose,
385                                   BT_doubleclose->getDescription(), N);
386      C.emitReport(R);
387    }
388    return NULL;
389  }
390
391  // Close the File Descriptor.
392  return state->set<StreamMap>(Sym, StreamState::getClosed(CE));
393}
394
395void StreamChecker::checkDeadSymbols(SymbolReaper &SymReaper,
396                                     CheckerContext &C) const {
397  // TODO: Clean up the state.
398  for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
399         E = SymReaper.dead_end(); I != E; ++I) {
400    SymbolRef Sym = *I;
401    ProgramStateRef state = C.getState();
402    const StreamState *SS = state->get<StreamMap>(Sym);
403    if (!SS)
404      continue;
405
406    if (SS->isOpened()) {
407      ExplodedNode *N = C.generateSink();
408      if (N) {
409        if (!BT_ResourceLeak)
410          BT_ResourceLeak.reset(new BuiltinBug("Resource Leak",
411                         "Opened File never closed. Potential Resource leak."));
412        BugReport *R = new BugReport(*BT_ResourceLeak,
413                                     BT_ResourceLeak->getDescription(), N);
414        C.emitReport(R);
415      }
416    }
417  }
418}
419
420void ento::registerStreamChecker(CheckerManager &mgr) {
421  mgr.registerChecker<StreamChecker>();
422}
423