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/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"
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,
60 check::DeadSymbols > {
61 mutable IdentifierInfo *II_fopen, *II_tmpfile, *II_fclose, *II_fread,
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;
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) {}
74 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
75 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
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;
93 void OpenFileAux(CheckerContext &C, const CallExpr *CE) const;
95 ProgramStateRef CheckNullStream(SVal SV, ProgramStateRef state,
96 CheckerContext &C) const;
97 ProgramStateRef CheckDoubleClose(const CallExpr *CE, ProgramStateRef state,
98 CheckerContext &C) const;
101 } // end anonymous namespace
103 REGISTER_MAP_WITH_PROGRAMSTATE(StreamMap, SymbolRef, StreamState)
106 bool StreamChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
107 const FunctionDecl *FD = C.getCalleeDecl(CE);
108 if (!FD || FD->getKind() != Decl::Function)
111 ASTContext &Ctx = C.getASTContext();
113 II_fopen = &Ctx.Idents.get("fopen");
115 II_tmpfile = &Ctx.Idents.get("tmpfile");
117 II_fclose = &Ctx.Idents.get("fclose");
119 II_fread = &Ctx.Idents.get("fread");
121 II_fwrite = &Ctx.Idents.get("fwrite");
123 II_fseek = &Ctx.Idents.get("fseek");
125 II_ftell = &Ctx.Idents.get("ftell");
127 II_rewind = &Ctx.Idents.get("rewind");
129 II_fgetpos = &Ctx.Idents.get("fgetpos");
131 II_fsetpos = &Ctx.Idents.get("fsetpos");
133 II_clearerr = &Ctx.Idents.get("clearerr");
135 II_feof = &Ctx.Idents.get("feof");
137 II_ferror = &Ctx.Idents.get("ferror");
139 II_fileno = &Ctx.Idents.get("fileno");
141 if (FD->getIdentifier() == II_fopen) {
145 if (FD->getIdentifier() == II_tmpfile) {
149 if (FD->getIdentifier() == II_fclose) {
153 if (FD->getIdentifier() == II_fread) {
157 if (FD->getIdentifier() == II_fwrite) {
161 if (FD->getIdentifier() == II_fseek) {
165 if (FD->getIdentifier() == II_ftell) {
169 if (FD->getIdentifier() == II_rewind) {
173 if (FD->getIdentifier() == II_fgetpos) {
177 if (FD->getIdentifier() == II_fsetpos) {
181 if (FD->getIdentifier() == II_clearerr) {
185 if (FD->getIdentifier() == II_feof) {
189 if (FD->getIdentifier() == II_ferror) {
193 if (FD->getIdentifier() == II_fileno) {
201 void StreamChecker::Fopen(CheckerContext &C, const CallExpr *CE) const {
205 void StreamChecker::Tmpfile(CheckerContext &C, const CallExpr *CE) const {
209 void 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);
217 ConstraintManager &CM = C.getConstraintManager();
218 // Bifurcate the state into two: one with a valid FILE* pointer, the other
220 ProgramStateRef stateNotNull, stateNull;
221 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, RetVal);
223 if (SymbolRef Sym = RetVal.getAsSymbol()) {
224 // if RetVal is not NULL, set the symbol's state to Opened.
226 stateNotNull->set<StreamMap>(Sym,StreamState::getOpened(CE));
228 stateNull->set<StreamMap>(Sym, StreamState::getOpenFailed(CE));
230 C.addTransition(stateNotNull);
231 C.addTransition(stateNull);
235 void StreamChecker::Fclose(CheckerContext &C, const CallExpr *CE) const {
236 ProgramStateRef state = CheckDoubleClose(CE, C.getState(), C);
238 C.addTransition(state);
241 void StreamChecker::Fread(CheckerContext &C, const CallExpr *CE) const {
242 ProgramStateRef state = C.getState();
243 if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
248 void StreamChecker::Fwrite(CheckerContext &C, const CallExpr *CE) const {
249 ProgramStateRef state = C.getState();
250 if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
255 void 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)))
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>();
267 int64_t x = CI->getValue().getSExtValue();
268 if (x >= 0 && x <= 2)
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);
282 void StreamChecker::Ftell(CheckerContext &C, const CallExpr *CE) const {
283 ProgramStateRef state = C.getState();
284 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
289 void StreamChecker::Rewind(CheckerContext &C, const CallExpr *CE) const {
290 ProgramStateRef state = C.getState();
291 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
296 void StreamChecker::Fgetpos(CheckerContext &C, const CallExpr *CE) const {
297 ProgramStateRef state = C.getState();
298 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
303 void StreamChecker::Fsetpos(CheckerContext &C, const CallExpr *CE) const {
304 ProgramStateRef state = C.getState();
305 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
310 void StreamChecker::Clearerr(CheckerContext &C, const CallExpr *CE) const {
311 ProgramStateRef state = C.getState();
312 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
317 void StreamChecker::Feof(CheckerContext &C, const CallExpr *CE) const {
318 ProgramStateRef state = C.getState();
319 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
324 void StreamChecker::Ferror(CheckerContext &C, const CallExpr *CE) const {
325 ProgramStateRef state = C.getState();
326 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
331 void StreamChecker::Fileno(CheckerContext &C, const CallExpr *CE) const {
332 ProgramStateRef state = C.getState();
333 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
338 ProgramStateRef StreamChecker::CheckNullStream(SVal SV, ProgramStateRef state,
339 CheckerContext &C) const {
340 Optional<DefinedSVal> DV = SV.getAs<DefinedSVal>();
344 ConstraintManager &CM = C.getConstraintManager();
345 ProgramStateRef stateNotNull, stateNull;
346 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
348 if (!stateNotNull && stateNull) {
349 if (ExplodedNode *N = C.generateSink(stateNull)) {
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);
361 ProgramStateRef StreamChecker::CheckDoubleClose(const CallExpr *CE,
362 ProgramStateRef state,
363 CheckerContext &C) const {
365 state->getSVal(CE->getArg(0), C.getLocationContext()).getAsSymbol();
369 const StreamState *SS = state->get<StreamMap>(Sym);
371 // If the file stream is not tracked, return.
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();
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);
391 // Close the File Descriptor.
392 return state->set<StreamMap>(Sym, StreamState::getClosed(CE));
395 void 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) {
401 ProgramStateRef state = C.getState();
402 const StreamState *SS = state->get<StreamMap>(Sym);
406 if (SS->isOpened()) {
407 ExplodedNode *N = C.generateSink();
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);
420 void ento::registerStreamChecker(CheckerManager &mgr) {
421 mgr.registerChecker<StreamChecker>();