1 //===-- StreamChecker.cpp -----------------------------------------*- C++ -*--//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines checkers that model and check stream handling functions.
12 //===----------------------------------------------------------------------===//
14 #include "ClangSACheckers.h"
15 #include "clang/StaticAnalyzer/Core/Checker.h"
16 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
18 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.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"
24 using namespace clang;
30 enum Kind { Opened, Closed, OpenFailed, Escaped } K;
33 StreamState(Kind k, const Stmt *s) : K(k), S(s) {}
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; }
40 bool operator==(const StreamState &X) const {
41 return K == X.K && S == X.S;
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);
49 static StreamState getEscaped(const Stmt *s) {
50 return StreamState(Escaped, s);
53 void Profile(llvm::FoldingSetNodeID &ID) const {
59 class StreamChecker : public Checker<eval::Call,
62 check::PreStmt<ReturnStmt> > {
63 mutable IdentifierInfo *II_fopen, *II_tmpfile, *II_fclose, *II_fread,
65 *II_fseek, *II_ftell, *II_rewind, *II_fgetpos, *II_fsetpos,
66 *II_clearerr, *II_feof, *II_ferror, *II_fileno;
67 mutable OwningPtr<BuiltinBug> BT_nullfp, BT_illegalwhence,
68 BT_doubleclose, BT_ResourceLeak;
72 : II_fopen(0), II_tmpfile(0) ,II_fclose(0), II_fread(0), II_fwrite(0),
73 II_fseek(0), II_ftell(0), II_rewind(0), II_fgetpos(0), II_fsetpos(0),
74 II_clearerr(0), II_feof(0), II_ferror(0), II_fileno(0) {}
76 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
77 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
78 void checkEndPath(CheckerContext &Ctx) const;
79 void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
82 void Fopen(CheckerContext &C, const CallExpr *CE) const;
83 void Tmpfile(CheckerContext &C, const CallExpr *CE) const;
84 void Fclose(CheckerContext &C, const CallExpr *CE) const;
85 void Fread(CheckerContext &C, const CallExpr *CE) const;
86 void Fwrite(CheckerContext &C, const CallExpr *CE) const;
87 void Fseek(CheckerContext &C, const CallExpr *CE) const;
88 void Ftell(CheckerContext &C, const CallExpr *CE) const;
89 void Rewind(CheckerContext &C, const CallExpr *CE) const;
90 void Fgetpos(CheckerContext &C, const CallExpr *CE) const;
91 void Fsetpos(CheckerContext &C, const CallExpr *CE) const;
92 void Clearerr(CheckerContext &C, const CallExpr *CE) const;
93 void Feof(CheckerContext &C, const CallExpr *CE) const;
94 void Ferror(CheckerContext &C, const CallExpr *CE) const;
95 void Fileno(CheckerContext &C, const CallExpr *CE) const;
97 void OpenFileAux(CheckerContext &C, const CallExpr *CE) const;
99 ProgramStateRef CheckNullStream(SVal SV, ProgramStateRef state,
100 CheckerContext &C) const;
101 ProgramStateRef CheckDoubleClose(const CallExpr *CE, ProgramStateRef state,
102 CheckerContext &C) const;
105 } // end anonymous namespace
107 REGISTER_MAP_WITH_PROGRAMSTATE(StreamMap, SymbolRef, StreamState)
110 bool StreamChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
111 const FunctionDecl *FD = C.getCalleeDecl(CE);
112 if (!FD || FD->getKind() != Decl::Function)
115 ASTContext &Ctx = C.getASTContext();
117 II_fopen = &Ctx.Idents.get("fopen");
119 II_tmpfile = &Ctx.Idents.get("tmpfile");
121 II_fclose = &Ctx.Idents.get("fclose");
123 II_fread = &Ctx.Idents.get("fread");
125 II_fwrite = &Ctx.Idents.get("fwrite");
127 II_fseek = &Ctx.Idents.get("fseek");
129 II_ftell = &Ctx.Idents.get("ftell");
131 II_rewind = &Ctx.Idents.get("rewind");
133 II_fgetpos = &Ctx.Idents.get("fgetpos");
135 II_fsetpos = &Ctx.Idents.get("fsetpos");
137 II_clearerr = &Ctx.Idents.get("clearerr");
139 II_feof = &Ctx.Idents.get("feof");
141 II_ferror = &Ctx.Idents.get("ferror");
143 II_fileno = &Ctx.Idents.get("fileno");
145 if (FD->getIdentifier() == II_fopen) {
149 if (FD->getIdentifier() == II_tmpfile) {
153 if (FD->getIdentifier() == II_fclose) {
157 if (FD->getIdentifier() == II_fread) {
161 if (FD->getIdentifier() == II_fwrite) {
165 if (FD->getIdentifier() == II_fseek) {
169 if (FD->getIdentifier() == II_ftell) {
173 if (FD->getIdentifier() == II_rewind) {
177 if (FD->getIdentifier() == II_fgetpos) {
181 if (FD->getIdentifier() == II_fsetpos) {
185 if (FD->getIdentifier() == II_clearerr) {
189 if (FD->getIdentifier() == II_feof) {
193 if (FD->getIdentifier() == II_ferror) {
197 if (FD->getIdentifier() == II_fileno) {
205 void StreamChecker::Fopen(CheckerContext &C, const CallExpr *CE) const {
209 void StreamChecker::Tmpfile(CheckerContext &C, const CallExpr *CE) const {
213 void StreamChecker::OpenFileAux(CheckerContext &C, const CallExpr *CE) const {
214 ProgramStateRef state = C.getState();
215 SValBuilder &svalBuilder = C.getSValBuilder();
216 const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
218 cast<DefinedSVal>(svalBuilder.conjureSymbolVal(0, CE, LCtx,
220 state = state->BindExpr(CE, C.getLocationContext(), RetVal);
222 ConstraintManager &CM = C.getConstraintManager();
223 // Bifurcate the state into two: one with a valid FILE* pointer, the other
225 ProgramStateRef stateNotNull, stateNull;
226 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, RetVal);
228 if (SymbolRef Sym = RetVal.getAsSymbol()) {
229 // if RetVal is not NULL, set the symbol's state to Opened.
231 stateNotNull->set<StreamMap>(Sym,StreamState::getOpened(CE));
233 stateNull->set<StreamMap>(Sym, StreamState::getOpenFailed(CE));
235 C.addTransition(stateNotNull);
236 C.addTransition(stateNull);
240 void StreamChecker::Fclose(CheckerContext &C, const CallExpr *CE) const {
241 ProgramStateRef state = CheckDoubleClose(CE, C.getState(), C);
243 C.addTransition(state);
246 void StreamChecker::Fread(CheckerContext &C, const CallExpr *CE) const {
247 ProgramStateRef state = C.getState();
248 if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
253 void StreamChecker::Fwrite(CheckerContext &C, const CallExpr *CE) const {
254 ProgramStateRef state = C.getState();
255 if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
260 void StreamChecker::Fseek(CheckerContext &C, const CallExpr *CE) const {
261 ProgramStateRef state = C.getState();
262 if (!(state = CheckNullStream(state->getSVal(CE->getArg(0),
263 C.getLocationContext()), state, C)))
265 // Check the legality of the 'whence' argument of 'fseek'.
266 SVal Whence = state->getSVal(CE->getArg(2), C.getLocationContext());
267 const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Whence);
272 int64_t x = CI->getValue().getSExtValue();
273 if (x >= 0 && x <= 2)
276 if (ExplodedNode *N = C.addTransition(state)) {
277 if (!BT_illegalwhence)
278 BT_illegalwhence.reset(new BuiltinBug("Illegal whence argument",
279 "The whence argument to fseek() should be "
280 "SEEK_SET, SEEK_END, or SEEK_CUR."));
281 BugReport *R = new BugReport(*BT_illegalwhence,
282 BT_illegalwhence->getDescription(), N);
287 void StreamChecker::Ftell(CheckerContext &C, const CallExpr *CE) const {
288 ProgramStateRef state = C.getState();
289 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
294 void StreamChecker::Rewind(CheckerContext &C, const CallExpr *CE) const {
295 ProgramStateRef state = C.getState();
296 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
301 void StreamChecker::Fgetpos(CheckerContext &C, const CallExpr *CE) const {
302 ProgramStateRef state = C.getState();
303 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
308 void StreamChecker::Fsetpos(CheckerContext &C, const CallExpr *CE) const {
309 ProgramStateRef state = C.getState();
310 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
315 void StreamChecker::Clearerr(CheckerContext &C, const CallExpr *CE) const {
316 ProgramStateRef state = C.getState();
317 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
322 void StreamChecker::Feof(CheckerContext &C, const CallExpr *CE) const {
323 ProgramStateRef state = C.getState();
324 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
329 void StreamChecker::Ferror(CheckerContext &C, const CallExpr *CE) const {
330 ProgramStateRef state = C.getState();
331 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
336 void StreamChecker::Fileno(CheckerContext &C, const CallExpr *CE) const {
337 ProgramStateRef state = C.getState();
338 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
343 ProgramStateRef StreamChecker::CheckNullStream(SVal SV, ProgramStateRef state,
344 CheckerContext &C) const {
345 const DefinedSVal *DV = dyn_cast<DefinedSVal>(&SV);
349 ConstraintManager &CM = C.getConstraintManager();
350 ProgramStateRef stateNotNull, stateNull;
351 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
353 if (!stateNotNull && stateNull) {
354 if (ExplodedNode *N = C.generateSink(stateNull)) {
356 BT_nullfp.reset(new BuiltinBug("NULL stream pointer",
357 "Stream pointer might be NULL."));
358 BugReport *R =new BugReport(*BT_nullfp, BT_nullfp->getDescription(), N);
366 ProgramStateRef StreamChecker::CheckDoubleClose(const CallExpr *CE,
367 ProgramStateRef state,
368 CheckerContext &C) const {
370 state->getSVal(CE->getArg(0), C.getLocationContext()).getAsSymbol();
374 const StreamState *SS = state->get<StreamMap>(Sym);
376 // If the file stream is not tracked, return.
380 // Check: Double close a File Descriptor could cause undefined behaviour.
381 // Conforming to man-pages
382 if (SS->isClosed()) {
383 ExplodedNode *N = C.generateSink();
386 BT_doubleclose.reset(new BuiltinBug("Double fclose",
387 "Try to close a file Descriptor already"
388 " closed. Cause undefined behaviour."));
389 BugReport *R = new BugReport(*BT_doubleclose,
390 BT_doubleclose->getDescription(), N);
396 // Close the File Descriptor.
397 return state->set<StreamMap>(Sym, StreamState::getClosed(CE));
400 void StreamChecker::checkDeadSymbols(SymbolReaper &SymReaper,
401 CheckerContext &C) const {
402 // TODO: Clean up the state.
403 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
404 E = SymReaper.dead_end(); I != E; ++I) {
406 ProgramStateRef state = C.getState();
407 const StreamState *SS = state->get<StreamMap>(Sym);
408 // TODO: Shouldn't we have a continue here?
412 if (SS->isOpened()) {
413 ExplodedNode *N = C.generateSink();
415 if (!BT_ResourceLeak)
416 BT_ResourceLeak.reset(new BuiltinBug("Resource Leak",
417 "Opened File never closed. Potential Resource leak."));
418 BugReport *R = new BugReport(*BT_ResourceLeak,
419 BT_ResourceLeak->getDescription(), N);
426 void StreamChecker::checkEndPath(CheckerContext &Ctx) const {
427 ProgramStateRef state = Ctx.getState();
428 StreamMapTy M = state->get<StreamMap>();
430 for (StreamMapTy::iterator I = M.begin(), E = M.end(); I != E; ++I) {
431 StreamState SS = I->second;
433 ExplodedNode *N = Ctx.addTransition(state);
435 if (!BT_ResourceLeak)
436 BT_ResourceLeak.reset(new BuiltinBug("Resource Leak",
437 "Opened File never closed. Potential Resource leak."));
438 BugReport *R = new BugReport(*BT_ResourceLeak,
439 BT_ResourceLeak->getDescription(), N);
446 void StreamChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const {
447 const Expr *RetE = S->getRetValue();
451 ProgramStateRef state = C.getState();
452 SymbolRef Sym = state->getSVal(RetE, C.getLocationContext()).getAsSymbol();
457 const StreamState *SS = state->get<StreamMap>(Sym);
462 state = state->set<StreamMap>(Sym, StreamState::getEscaped(S));
464 C.addTransition(state);
467 void ento::registerStreamChecker(CheckerManager &mgr) {
468 mgr.registerChecker<StreamChecker>();