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 std::unique_ptr<BuiltinBug> BT_nullfp, BT_illegalwhence,
66 BT_doubleclose, BT_ResourceLeak;
70 : II_fopen(nullptr), II_tmpfile(nullptr), II_fclose(nullptr),
71 II_fread(nullptr), II_fwrite(nullptr), II_fseek(nullptr),
72 II_ftell(nullptr), II_rewind(nullptr), II_fgetpos(nullptr),
73 II_fsetpos(nullptr), II_clearerr(nullptr), II_feof(nullptr),
74 II_ferror(nullptr), II_fileno(nullptr) {}
76 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
77 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
80 void Fopen(CheckerContext &C, const CallExpr *CE) const;
81 void Tmpfile(CheckerContext &C, const CallExpr *CE) const;
82 void Fclose(CheckerContext &C, const CallExpr *CE) const;
83 void Fread(CheckerContext &C, const CallExpr *CE) const;
84 void Fwrite(CheckerContext &C, const CallExpr *CE) const;
85 void Fseek(CheckerContext &C, const CallExpr *CE) const;
86 void Ftell(CheckerContext &C, const CallExpr *CE) const;
87 void Rewind(CheckerContext &C, const CallExpr *CE) const;
88 void Fgetpos(CheckerContext &C, const CallExpr *CE) const;
89 void Fsetpos(CheckerContext &C, const CallExpr *CE) const;
90 void Clearerr(CheckerContext &C, const CallExpr *CE) const;
91 void Feof(CheckerContext &C, const CallExpr *CE) const;
92 void Ferror(CheckerContext &C, const CallExpr *CE) const;
93 void Fileno(CheckerContext &C, const CallExpr *CE) const;
95 void OpenFileAux(CheckerContext &C, const CallExpr *CE) const;
97 ProgramStateRef CheckNullStream(SVal SV, ProgramStateRef state,
98 CheckerContext &C) const;
99 ProgramStateRef CheckDoubleClose(const CallExpr *CE, ProgramStateRef state,
100 CheckerContext &C) const;
103 } // end anonymous namespace
105 REGISTER_MAP_WITH_PROGRAMSTATE(StreamMap, SymbolRef, StreamState)
108 bool StreamChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
109 const FunctionDecl *FD = C.getCalleeDecl(CE);
110 if (!FD || FD->getKind() != Decl::Function)
113 ASTContext &Ctx = C.getASTContext();
115 II_fopen = &Ctx.Idents.get("fopen");
117 II_tmpfile = &Ctx.Idents.get("tmpfile");
119 II_fclose = &Ctx.Idents.get("fclose");
121 II_fread = &Ctx.Idents.get("fread");
123 II_fwrite = &Ctx.Idents.get("fwrite");
125 II_fseek = &Ctx.Idents.get("fseek");
127 II_ftell = &Ctx.Idents.get("ftell");
129 II_rewind = &Ctx.Idents.get("rewind");
131 II_fgetpos = &Ctx.Idents.get("fgetpos");
133 II_fsetpos = &Ctx.Idents.get("fsetpos");
135 II_clearerr = &Ctx.Idents.get("clearerr");
137 II_feof = &Ctx.Idents.get("feof");
139 II_ferror = &Ctx.Idents.get("ferror");
141 II_fileno = &Ctx.Idents.get("fileno");
143 if (FD->getIdentifier() == II_fopen) {
147 if (FD->getIdentifier() == II_tmpfile) {
151 if (FD->getIdentifier() == II_fclose) {
155 if (FD->getIdentifier() == II_fread) {
159 if (FD->getIdentifier() == II_fwrite) {
163 if (FD->getIdentifier() == II_fseek) {
167 if (FD->getIdentifier() == II_ftell) {
171 if (FD->getIdentifier() == II_rewind) {
175 if (FD->getIdentifier() == II_fgetpos) {
179 if (FD->getIdentifier() == II_fsetpos) {
183 if (FD->getIdentifier() == II_clearerr) {
187 if (FD->getIdentifier() == II_feof) {
191 if (FD->getIdentifier() == II_ferror) {
195 if (FD->getIdentifier() == II_fileno) {
203 void StreamChecker::Fopen(CheckerContext &C, const CallExpr *CE) const {
207 void StreamChecker::Tmpfile(CheckerContext &C, const CallExpr *CE) const {
211 void StreamChecker::OpenFileAux(CheckerContext &C, const CallExpr *CE) const {
212 ProgramStateRef state = C.getState();
213 SValBuilder &svalBuilder = C.getSValBuilder();
214 const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
215 DefinedSVal RetVal = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx,
217 .castAs<DefinedSVal>();
218 state = state->BindExpr(CE, C.getLocationContext(), RetVal);
220 ConstraintManager &CM = C.getConstraintManager();
221 // Bifurcate the state into two: one with a valid FILE* pointer, the other
223 ProgramStateRef stateNotNull, stateNull;
224 std::tie(stateNotNull, stateNull) = CM.assumeDual(state, RetVal);
226 if (SymbolRef Sym = RetVal.getAsSymbol()) {
227 // if RetVal is not NULL, set the symbol's state to Opened.
229 stateNotNull->set<StreamMap>(Sym,StreamState::getOpened(CE));
231 stateNull->set<StreamMap>(Sym, StreamState::getOpenFailed(CE));
233 C.addTransition(stateNotNull);
234 C.addTransition(stateNull);
238 void StreamChecker::Fclose(CheckerContext &C, const CallExpr *CE) const {
239 ProgramStateRef state = CheckDoubleClose(CE, C.getState(), C);
241 C.addTransition(state);
244 void StreamChecker::Fread(CheckerContext &C, const CallExpr *CE) const {
245 ProgramStateRef state = C.getState();
246 if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
251 void StreamChecker::Fwrite(CheckerContext &C, const CallExpr *CE) const {
252 ProgramStateRef state = C.getState();
253 if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
258 void StreamChecker::Fseek(CheckerContext &C, const CallExpr *CE) const {
259 ProgramStateRef state = C.getState();
260 if (!(state = CheckNullStream(state->getSVal(CE->getArg(0),
261 C.getLocationContext()), state, C)))
263 // Check the legality of the 'whence' argument of 'fseek'.
264 SVal Whence = state->getSVal(CE->getArg(2), C.getLocationContext());
265 Optional<nonloc::ConcreteInt> CI = Whence.getAs<nonloc::ConcreteInt>();
270 int64_t x = CI->getValue().getSExtValue();
271 if (x >= 0 && x <= 2)
274 if (ExplodedNode *N = C.generateNonFatalErrorNode(state)) {
275 if (!BT_illegalwhence)
276 BT_illegalwhence.reset(
277 new BuiltinBug(this, "Illegal whence argument",
278 "The whence argument to fseek() should be "
279 "SEEK_SET, SEEK_END, or SEEK_CUR."));
280 C.emitReport(llvm::make_unique<BugReport>(
281 *BT_illegalwhence, BT_illegalwhence->getDescription(), N));
285 void StreamChecker::Ftell(CheckerContext &C, const CallExpr *CE) const {
286 ProgramStateRef state = C.getState();
287 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
292 void StreamChecker::Rewind(CheckerContext &C, const CallExpr *CE) const {
293 ProgramStateRef state = C.getState();
294 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
299 void StreamChecker::Fgetpos(CheckerContext &C, const CallExpr *CE) const {
300 ProgramStateRef state = C.getState();
301 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
306 void StreamChecker::Fsetpos(CheckerContext &C, const CallExpr *CE) const {
307 ProgramStateRef state = C.getState();
308 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
313 void StreamChecker::Clearerr(CheckerContext &C, const CallExpr *CE) const {
314 ProgramStateRef state = C.getState();
315 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
320 void StreamChecker::Feof(CheckerContext &C, const CallExpr *CE) const {
321 ProgramStateRef state = C.getState();
322 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
327 void StreamChecker::Ferror(CheckerContext &C, const CallExpr *CE) const {
328 ProgramStateRef state = C.getState();
329 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
334 void StreamChecker::Fileno(CheckerContext &C, const CallExpr *CE) const {
335 ProgramStateRef state = C.getState();
336 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
341 ProgramStateRef StreamChecker::CheckNullStream(SVal SV, ProgramStateRef state,
342 CheckerContext &C) const {
343 Optional<DefinedSVal> DV = SV.getAs<DefinedSVal>();
347 ConstraintManager &CM = C.getConstraintManager();
348 ProgramStateRef stateNotNull, stateNull;
349 std::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
351 if (!stateNotNull && stateNull) {
352 if (ExplodedNode *N = C.generateErrorNode(stateNull)) {
354 BT_nullfp.reset(new BuiltinBug(this, "NULL stream pointer",
355 "Stream pointer might be NULL."));
356 C.emitReport(llvm::make_unique<BugReport>(
357 *BT_nullfp, BT_nullfp->getDescription(), N));
364 ProgramStateRef StreamChecker::CheckDoubleClose(const CallExpr *CE,
365 ProgramStateRef state,
366 CheckerContext &C) const {
368 state->getSVal(CE->getArg(0), C.getLocationContext()).getAsSymbol();
372 const StreamState *SS = state->get<StreamMap>(Sym);
374 // If the file stream is not tracked, return.
378 // Check: Double close a File Descriptor could cause undefined behaviour.
379 // Conforming to man-pages
380 if (SS->isClosed()) {
381 ExplodedNode *N = C.generateErrorNode();
384 BT_doubleclose.reset(new BuiltinBug(
385 this, "Double fclose", "Try to close a file Descriptor already"
386 " closed. Cause undefined behaviour."));
387 C.emitReport(llvm::make_unique<BugReport>(
388 *BT_doubleclose, BT_doubleclose->getDescription(), N));
393 // Close the File Descriptor.
394 return state->set<StreamMap>(Sym, StreamState::getClosed(CE));
397 void StreamChecker::checkDeadSymbols(SymbolReaper &SymReaper,
398 CheckerContext &C) const {
399 // TODO: Clean up the state.
400 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
401 E = SymReaper.dead_end(); I != E; ++I) {
403 ProgramStateRef state = C.getState();
404 const StreamState *SS = state->get<StreamMap>(Sym);
408 if (SS->isOpened()) {
409 ExplodedNode *N = C.generateErrorNode();
411 if (!BT_ResourceLeak)
412 BT_ResourceLeak.reset(new BuiltinBug(
413 this, "Resource Leak",
414 "Opened File never closed. Potential Resource leak."));
415 C.emitReport(llvm::make_unique<BugReport>(
416 *BT_ResourceLeak, BT_ResourceLeak->getDescription(), N));
422 void ento::registerStreamChecker(CheckerManager &mgr) {
423 mgr.registerChecker<StreamChecker>();