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"
23 using namespace clang;
29 enum Kind { Opened, Closed, OpenFailed, Escaped } K;
32 StreamState(Kind k, const Stmt *s) : K(k), S(s) {}
34 bool isOpened() const { return K == Opened; }
35 bool isClosed() const { return K == Closed; }
36 //bool isOpenFailed() const { return K == OpenFailed; }
37 //bool isEscaped() const { return K == Escaped; }
39 bool operator==(const StreamState &X) const {
40 return K == X.K && S == X.S;
43 static StreamState getOpened(const Stmt *s) { return StreamState(Opened, s); }
44 static StreamState getClosed(const Stmt *s) { return StreamState(Closed, s); }
45 static StreamState getOpenFailed(const Stmt *s) {
46 return StreamState(OpenFailed, s);
48 static StreamState getEscaped(const Stmt *s) {
49 return StreamState(Escaped, s);
52 void Profile(llvm::FoldingSetNodeID &ID) const {
58 class StreamChecker : public Checker<eval::Call,
59 check::DeadSymbols > {
60 mutable IdentifierInfo *II_fopen, *II_tmpfile, *II_fclose, *II_fread,
62 *II_fseek, *II_ftell, *II_rewind, *II_fgetpos, *II_fsetpos,
63 *II_clearerr, *II_feof, *II_ferror, *II_fileno;
64 mutable std::unique_ptr<BuiltinBug> BT_nullfp, BT_illegalwhence,
65 BT_doubleclose, BT_ResourceLeak;
69 : II_fopen(nullptr), II_tmpfile(nullptr), II_fclose(nullptr),
70 II_fread(nullptr), II_fwrite(nullptr), II_fseek(nullptr),
71 II_ftell(nullptr), II_rewind(nullptr), II_fgetpos(nullptr),
72 II_fsetpos(nullptr), II_clearerr(nullptr), II_feof(nullptr),
73 II_ferror(nullptr), II_fileno(nullptr) {}
75 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
76 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
79 void Fopen(CheckerContext &C, const CallExpr *CE) const;
80 void Tmpfile(CheckerContext &C, const CallExpr *CE) const;
81 void Fclose(CheckerContext &C, const CallExpr *CE) const;
82 void Fread(CheckerContext &C, const CallExpr *CE) const;
83 void Fwrite(CheckerContext &C, const CallExpr *CE) const;
84 void Fseek(CheckerContext &C, const CallExpr *CE) const;
85 void Ftell(CheckerContext &C, const CallExpr *CE) const;
86 void Rewind(CheckerContext &C, const CallExpr *CE) const;
87 void Fgetpos(CheckerContext &C, const CallExpr *CE) const;
88 void Fsetpos(CheckerContext &C, const CallExpr *CE) const;
89 void Clearerr(CheckerContext &C, const CallExpr *CE) const;
90 void Feof(CheckerContext &C, const CallExpr *CE) const;
91 void Ferror(CheckerContext &C, const CallExpr *CE) const;
92 void Fileno(CheckerContext &C, const CallExpr *CE) const;
94 void OpenFileAux(CheckerContext &C, const CallExpr *CE) const;
96 ProgramStateRef CheckNullStream(SVal SV, ProgramStateRef state,
97 CheckerContext &C) const;
98 ProgramStateRef CheckDoubleClose(const CallExpr *CE, ProgramStateRef state,
99 CheckerContext &C) const;
102 } // end anonymous namespace
104 REGISTER_MAP_WITH_PROGRAMSTATE(StreamMap, SymbolRef, StreamState)
107 bool StreamChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
108 const FunctionDecl *FD = C.getCalleeDecl(CE);
109 if (!FD || FD->getKind() != Decl::Function)
112 ASTContext &Ctx = C.getASTContext();
114 II_fopen = &Ctx.Idents.get("fopen");
116 II_tmpfile = &Ctx.Idents.get("tmpfile");
118 II_fclose = &Ctx.Idents.get("fclose");
120 II_fread = &Ctx.Idents.get("fread");
122 II_fwrite = &Ctx.Idents.get("fwrite");
124 II_fseek = &Ctx.Idents.get("fseek");
126 II_ftell = &Ctx.Idents.get("ftell");
128 II_rewind = &Ctx.Idents.get("rewind");
130 II_fgetpos = &Ctx.Idents.get("fgetpos");
132 II_fsetpos = &Ctx.Idents.get("fsetpos");
134 II_clearerr = &Ctx.Idents.get("clearerr");
136 II_feof = &Ctx.Idents.get("feof");
138 II_ferror = &Ctx.Idents.get("ferror");
140 II_fileno = &Ctx.Idents.get("fileno");
142 if (FD->getIdentifier() == II_fopen) {
146 if (FD->getIdentifier() == II_tmpfile) {
150 if (FD->getIdentifier() == II_fclose) {
154 if (FD->getIdentifier() == II_fread) {
158 if (FD->getIdentifier() == II_fwrite) {
162 if (FD->getIdentifier() == II_fseek) {
166 if (FD->getIdentifier() == II_ftell) {
170 if (FD->getIdentifier() == II_rewind) {
174 if (FD->getIdentifier() == II_fgetpos) {
178 if (FD->getIdentifier() == II_fsetpos) {
182 if (FD->getIdentifier() == II_clearerr) {
186 if (FD->getIdentifier() == II_feof) {
190 if (FD->getIdentifier() == II_ferror) {
194 if (FD->getIdentifier() == II_fileno) {
202 void StreamChecker::Fopen(CheckerContext &C, const CallExpr *CE) const {
206 void StreamChecker::Tmpfile(CheckerContext &C, const CallExpr *CE) const {
210 void StreamChecker::OpenFileAux(CheckerContext &C, const CallExpr *CE) const {
211 ProgramStateRef state = C.getState();
212 SValBuilder &svalBuilder = C.getSValBuilder();
213 const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
214 DefinedSVal RetVal = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx,
216 .castAs<DefinedSVal>();
217 state = state->BindExpr(CE, C.getLocationContext(), RetVal);
219 ConstraintManager &CM = C.getConstraintManager();
220 // Bifurcate the state into two: one with a valid FILE* pointer, the other
222 ProgramStateRef stateNotNull, stateNull;
223 std::tie(stateNotNull, stateNull) = CM.assumeDual(state, RetVal);
225 if (SymbolRef Sym = RetVal.getAsSymbol()) {
226 // if RetVal is not NULL, set the symbol's state to Opened.
228 stateNotNull->set<StreamMap>(Sym,StreamState::getOpened(CE));
230 stateNull->set<StreamMap>(Sym, StreamState::getOpenFailed(CE));
232 C.addTransition(stateNotNull);
233 C.addTransition(stateNull);
237 void StreamChecker::Fclose(CheckerContext &C, const CallExpr *CE) const {
238 ProgramStateRef state = CheckDoubleClose(CE, C.getState(), C);
240 C.addTransition(state);
243 void StreamChecker::Fread(CheckerContext &C, const CallExpr *CE) const {
244 ProgramStateRef state = C.getState();
245 if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
250 void StreamChecker::Fwrite(CheckerContext &C, const CallExpr *CE) const {
251 ProgramStateRef state = C.getState();
252 if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
257 void StreamChecker::Fseek(CheckerContext &C, const CallExpr *CE) const {
258 ProgramStateRef state = C.getState();
259 if (!(state = CheckNullStream(state->getSVal(CE->getArg(0),
260 C.getLocationContext()), state, C)))
262 // Check the legality of the 'whence' argument of 'fseek'.
263 SVal Whence = state->getSVal(CE->getArg(2), C.getLocationContext());
264 Optional<nonloc::ConcreteInt> CI = Whence.getAs<nonloc::ConcreteInt>();
269 int64_t x = CI->getValue().getSExtValue();
270 if (x >= 0 && x <= 2)
273 if (ExplodedNode *N = C.generateNonFatalErrorNode(state)) {
274 if (!BT_illegalwhence)
275 BT_illegalwhence.reset(
276 new BuiltinBug(this, "Illegal whence argument",
277 "The whence argument to fseek() should be "
278 "SEEK_SET, SEEK_END, or SEEK_CUR."));
279 C.emitReport(llvm::make_unique<BugReport>(
280 *BT_illegalwhence, BT_illegalwhence->getDescription(), N));
284 void StreamChecker::Ftell(CheckerContext &C, const CallExpr *CE) const {
285 ProgramStateRef state = C.getState();
286 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
291 void StreamChecker::Rewind(CheckerContext &C, const CallExpr *CE) const {
292 ProgramStateRef state = C.getState();
293 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
298 void StreamChecker::Fgetpos(CheckerContext &C, const CallExpr *CE) const {
299 ProgramStateRef state = C.getState();
300 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
305 void StreamChecker::Fsetpos(CheckerContext &C, const CallExpr *CE) const {
306 ProgramStateRef state = C.getState();
307 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
312 void StreamChecker::Clearerr(CheckerContext &C, const CallExpr *CE) const {
313 ProgramStateRef state = C.getState();
314 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
319 void StreamChecker::Feof(CheckerContext &C, const CallExpr *CE) const {
320 ProgramStateRef state = C.getState();
321 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
326 void StreamChecker::Ferror(CheckerContext &C, const CallExpr *CE) const {
327 ProgramStateRef state = C.getState();
328 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
333 void StreamChecker::Fileno(CheckerContext &C, const CallExpr *CE) const {
334 ProgramStateRef state = C.getState();
335 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
340 ProgramStateRef StreamChecker::CheckNullStream(SVal SV, ProgramStateRef state,
341 CheckerContext &C) const {
342 Optional<DefinedSVal> DV = SV.getAs<DefinedSVal>();
346 ConstraintManager &CM = C.getConstraintManager();
347 ProgramStateRef stateNotNull, stateNull;
348 std::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
350 if (!stateNotNull && stateNull) {
351 if (ExplodedNode *N = C.generateErrorNode(stateNull)) {
353 BT_nullfp.reset(new BuiltinBug(this, "NULL stream pointer",
354 "Stream pointer might be NULL."));
355 C.emitReport(llvm::make_unique<BugReport>(
356 *BT_nullfp, BT_nullfp->getDescription(), N));
363 ProgramStateRef StreamChecker::CheckDoubleClose(const CallExpr *CE,
364 ProgramStateRef state,
365 CheckerContext &C) const {
367 state->getSVal(CE->getArg(0), C.getLocationContext()).getAsSymbol();
371 const StreamState *SS = state->get<StreamMap>(Sym);
373 // If the file stream is not tracked, return.
377 // Check: Double close a File Descriptor could cause undefined behaviour.
378 // Conforming to man-pages
379 if (SS->isClosed()) {
380 ExplodedNode *N = C.generateErrorNode();
383 BT_doubleclose.reset(new BuiltinBug(
384 this, "Double fclose", "Try to close a file Descriptor already"
385 " closed. Cause undefined behaviour."));
386 C.emitReport(llvm::make_unique<BugReport>(
387 *BT_doubleclose, BT_doubleclose->getDescription(), N));
392 // Close the File Descriptor.
393 return state->set<StreamMap>(Sym, StreamState::getClosed(CE));
396 void StreamChecker::checkDeadSymbols(SymbolReaper &SymReaper,
397 CheckerContext &C) const {
398 // TODO: Clean up the state.
399 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
400 E = SymReaper.dead_end(); I != E; ++I) {
402 ProgramStateRef state = C.getState();
403 const StreamState *SS = state->get<StreamMap>(Sym);
407 if (SS->isOpened()) {
408 ExplodedNode *N = C.generateErrorNode();
410 if (!BT_ResourceLeak)
411 BT_ResourceLeak.reset(new BuiltinBug(
412 this, "Resource Leak",
413 "Opened File never closed. Potential Resource leak."));
414 C.emitReport(llvm::make_unique<BugReport>(
415 *BT_ResourceLeak, BT_ResourceLeak->getDescription(), N));
421 void ento::registerStreamChecker(CheckerManager &mgr) {
422 mgr.registerChecker<StreamChecker>();