blob: c06ba7c304e6d5319fb6da916d956007b876d68d [file] [log] [blame]
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +00001//===-- 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
Argyrios Kyrtzidis2d3905f2011-02-15 21:25:03 +000014#include "ClangSACheckers.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000015#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000016#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +000017#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000018#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Ted Kremenek001fd5b2011-08-15 22:09:50 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
20#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +000022#include "llvm/ADT/ImmutableMap.h"
23
24using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000025using namespace ento;
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +000026
27namespace {
28
Zhongxing Xuec562352010-07-19 01:52:29 +000029struct StreamState {
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +000030 enum Kind { Opened, Closed, OpenFailed, Escaped } K;
Zhongxing Xuec562352010-07-19 01:52:29 +000031 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; }
Chris Lattner3dd48bd2010-09-03 04:34:38 +000037 //bool isOpenFailed() const { return K == OpenFailed; }
38 //bool isEscaped() const { return K == Escaped; }
Zhongxing Xuec562352010-07-19 01:52:29 +000039
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); }
Zhongxing Xuf0c133f2010-07-22 14:01:01 +000046 static StreamState getOpenFailed(const Stmt *s) {
47 return StreamState(OpenFailed, s);
48 }
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +000049 static StreamState getEscaped(const Stmt *s) {
50 return StreamState(Escaped, s);
51 }
Zhongxing Xuec562352010-07-19 01:52:29 +000052
53 void Profile(llvm::FoldingSetNodeID &ID) const {
54 ID.AddInteger(K);
55 ID.AddPointer(S);
56 }
57};
58
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000059class StreamChecker : public Checker<eval::Call,
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +000060 check::DeadSymbols,
61 check::EndPath,
62 check::PreStmt<ReturnStmt> > {
63 mutable IdentifierInfo *II_fopen, *II_tmpfile, *II_fclose, *II_fread,
64 *II_fwrite,
Zhongxing Xu45057472010-06-22 07:50:21 +000065 *II_fseek, *II_ftell, *II_rewind, *II_fgetpos, *II_fsetpos,
66 *II_clearerr, *II_feof, *II_ferror, *II_fileno;
Dylan Noblesmithe2778992012-02-05 02:12:40 +000067 mutable OwningPtr<BuiltinBug> BT_nullfp, BT_illegalwhence,
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +000068 BT_doubleclose, BT_ResourceLeak;
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +000069
70public:
Zhongxing Xue96a9132010-06-18 02:47:46 +000071 StreamChecker()
Zhongxing Xuf0c133f2010-07-22 14:01:01 +000072 : II_fopen(0), II_tmpfile(0) ,II_fclose(0), II_fread(0), II_fwrite(0),
Zhongxing Xu45057472010-06-22 07:50:21 +000073 II_fseek(0), II_ftell(0), II_rewind(0), II_fgetpos(0), II_fsetpos(0),
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +000074 II_clearerr(0), II_feof(0), II_ferror(0), II_fileno(0) {}
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +000075
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +000076 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
77 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
Anna Zaks3eae3342011-10-25 19:56:48 +000078 void checkEndPath(CheckerContext &Ctx) const;
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +000079 void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +000080
81private:
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +000082 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;
Zhongxing Xuf0c133f2010-07-22 14:01:01 +000096
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +000097 void OpenFileAux(CheckerContext &C, const CallExpr *CE) const;
Zhongxing Xu45057472010-06-22 07:50:21 +000098
Ted Kremenek49b1e382012-01-26 21:29:00 +000099 ProgramStateRef CheckNullStream(SVal SV, ProgramStateRef state,
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000100 CheckerContext &C) const;
Ted Kremenek49b1e382012-01-26 21:29:00 +0000101 ProgramStateRef CheckDoubleClose(const CallExpr *CE, ProgramStateRef state,
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000102 CheckerContext &C) const;
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +0000103};
104
Zhongxing Xue96a9132010-06-18 02:47:46 +0000105} // end anonymous namespace
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +0000106
Jordan Roseb9ed61f2012-11-02 01:54:42 +0000107REGISTER_MAP_WITH_PROGRAMSTATE(StreamMap, SymbolRef, StreamState)
108
Zhongxing Xuec562352010-07-19 01:52:29 +0000109
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000110bool StreamChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
Anna Zaksc6aa5312011-12-01 05:57:37 +0000111 const FunctionDecl *FD = C.getCalleeDecl(CE);
Jordan Rose6cd16c52012-07-10 23:13:01 +0000112 if (!FD || FD->getKind() != Decl::Function)
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +0000113 return false;
114
115 ASTContext &Ctx = C.getASTContext();
116 if (!II_fopen)
117 II_fopen = &Ctx.Idents.get("fopen");
Zhongxing Xuf0c133f2010-07-22 14:01:01 +0000118 if (!II_tmpfile)
119 II_tmpfile = &Ctx.Idents.get("tmpfile");
Zhongxing Xuec562352010-07-19 01:52:29 +0000120 if (!II_fclose)
121 II_fclose = &Ctx.Idents.get("fclose");
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +0000122 if (!II_fread)
123 II_fread = &Ctx.Idents.get("fread");
Zhongxing Xu45057472010-06-22 07:50:21 +0000124 if (!II_fwrite)
125 II_fwrite = &Ctx.Idents.get("fwrite");
Zhongxing Xue96a9132010-06-18 02:47:46 +0000126 if (!II_fseek)
127 II_fseek = &Ctx.Idents.get("fseek");
Zhongxing Xue96a9132010-06-18 02:47:46 +0000128 if (!II_ftell)
129 II_ftell = &Ctx.Idents.get("ftell");
Zhongxing Xue96a9132010-06-18 02:47:46 +0000130 if (!II_rewind)
131 II_rewind = &Ctx.Idents.get("rewind");
Zhongxing Xu45057472010-06-22 07:50:21 +0000132 if (!II_fgetpos)
133 II_fgetpos = &Ctx.Idents.get("fgetpos");
134 if (!II_fsetpos)
135 II_fsetpos = &Ctx.Idents.get("fsetpos");
136 if (!II_clearerr)
137 II_clearerr = &Ctx.Idents.get("clearerr");
138 if (!II_feof)
139 II_feof = &Ctx.Idents.get("feof");
140 if (!II_ferror)
141 II_ferror = &Ctx.Idents.get("ferror");
142 if (!II_fileno)
143 II_fileno = &Ctx.Idents.get("fileno");
Zhongxing Xue96a9132010-06-18 02:47:46 +0000144
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +0000145 if (FD->getIdentifier() == II_fopen) {
Zhongxing Xu45057472010-06-22 07:50:21 +0000146 Fopen(C, CE);
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +0000147 return true;
148 }
Zhongxing Xuf0c133f2010-07-22 14:01:01 +0000149 if (FD->getIdentifier() == II_tmpfile) {
150 Tmpfile(C, CE);
151 return true;
152 }
Zhongxing Xuec562352010-07-19 01:52:29 +0000153 if (FD->getIdentifier() == II_fclose) {
154 Fclose(C, CE);
155 return true;
156 }
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +0000157 if (FD->getIdentifier() == II_fread) {
Zhongxing Xu45057472010-06-22 07:50:21 +0000158 Fread(C, CE);
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +0000159 return true;
160 }
Zhongxing Xu45057472010-06-22 07:50:21 +0000161 if (FD->getIdentifier() == II_fwrite) {
162 Fwrite(C, CE);
163 return true;
164 }
Zhongxing Xue96a9132010-06-18 02:47:46 +0000165 if (FD->getIdentifier() == II_fseek) {
Zhongxing Xu45057472010-06-22 07:50:21 +0000166 Fseek(C, CE);
Zhongxing Xue96a9132010-06-18 02:47:46 +0000167 return true;
168 }
Zhongxing Xue96a9132010-06-18 02:47:46 +0000169 if (FD->getIdentifier() == II_ftell) {
Zhongxing Xu45057472010-06-22 07:50:21 +0000170 Ftell(C, CE);
Zhongxing Xue96a9132010-06-18 02:47:46 +0000171 return true;
172 }
Zhongxing Xue96a9132010-06-18 02:47:46 +0000173 if (FD->getIdentifier() == II_rewind) {
174 Rewind(C, CE);
175 return true;
176 }
Zhongxing Xu45057472010-06-22 07:50:21 +0000177 if (FD->getIdentifier() == II_fgetpos) {
178 Fgetpos(C, CE);
179 return true;
180 }
181 if (FD->getIdentifier() == II_fsetpos) {
182 Fsetpos(C, CE);
183 return true;
184 }
185 if (FD->getIdentifier() == II_clearerr) {
186 Clearerr(C, CE);
187 return true;
188 }
189 if (FD->getIdentifier() == II_feof) {
190 Feof(C, CE);
191 return true;
192 }
193 if (FD->getIdentifier() == II_ferror) {
194 Ferror(C, CE);
195 return true;
196 }
197 if (FD->getIdentifier() == II_fileno) {
198 Fileno(C, CE);
199 return true;
200 }
Zhongxing Xue96a9132010-06-18 02:47:46 +0000201
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +0000202 return false;
203}
204
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000205void StreamChecker::Fopen(CheckerContext &C, const CallExpr *CE) const {
Zhongxing Xuf0c133f2010-07-22 14:01:01 +0000206 OpenFileAux(C, CE);
207}
208
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000209void StreamChecker::Tmpfile(CheckerContext &C, const CallExpr *CE) const {
Zhongxing Xuf0c133f2010-07-22 14:01:01 +0000210 OpenFileAux(C, CE);
211}
212
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000213void StreamChecker::OpenFileAux(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000214 ProgramStateRef state = C.getState();
Ted Kremenek90af9092010-12-02 07:49:45 +0000215 SValBuilder &svalBuilder = C.getSValBuilder();
Ted Kremenekd519cae2012-02-17 23:13:45 +0000216 const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
Ted Kremenek90af9092010-12-02 07:49:45 +0000217 DefinedSVal RetVal =
Ted Kremenekd94854a2012-08-22 06:26:15 +0000218 cast<DefinedSVal>(svalBuilder.conjureSymbolVal(0, CE, LCtx,
219 C.blockCount()));
Ted Kremenek632e3b72012-01-06 22:09:28 +0000220 state = state->BindExpr(CE, C.getLocationContext(), RetVal);
Zhongxing Xuf0c133f2010-07-22 14:01:01 +0000221
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +0000222 ConstraintManager &CM = C.getConstraintManager();
223 // Bifurcate the state into two: one with a valid FILE* pointer, the other
224 // with a NULL.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000225 ProgramStateRef stateNotNull, stateNull;
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000226 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, RetVal);
Zhongxing Xuf0c133f2010-07-22 14:01:01 +0000227
Ted Kremenek5c0969f2010-09-03 01:07:04 +0000228 if (SymbolRef Sym = RetVal.getAsSymbol()) {
229 // if RetVal is not NULL, set the symbol's state to Opened.
230 stateNotNull =
Jordan Roseb9ed61f2012-11-02 01:54:42 +0000231 stateNotNull->set<StreamMap>(Sym,StreamState::getOpened(CE));
Ted Kremenek5c0969f2010-09-03 01:07:04 +0000232 stateNull =
Jordan Roseb9ed61f2012-11-02 01:54:42 +0000233 stateNull->set<StreamMap>(Sym, StreamState::getOpenFailed(CE));
Zhongxing Xuec562352010-07-19 01:52:29 +0000234
Anna Zaksda4c8d62011-10-26 21:06:34 +0000235 C.addTransition(stateNotNull);
236 C.addTransition(stateNull);
Ted Kremenek5c0969f2010-09-03 01:07:04 +0000237 }
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +0000238}
239
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000240void StreamChecker::Fclose(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000241 ProgramStateRef state = CheckDoubleClose(CE, C.getState(), C);
Zhongxing Xuec562352010-07-19 01:52:29 +0000242 if (state)
Anna Zaksda4c8d62011-10-26 21:06:34 +0000243 C.addTransition(state);
Zhongxing Xuec562352010-07-19 01:52:29 +0000244}
245
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000246void StreamChecker::Fread(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000247 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +0000248 if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
249 state, C))
Zhongxing Xue96a9132010-06-18 02:47:46 +0000250 return;
251}
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +0000252
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000253void StreamChecker::Fwrite(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000254 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +0000255 if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
256 state, C))
Zhongxing Xu45057472010-06-22 07:50:21 +0000257 return;
258}
259
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000260void StreamChecker::Fseek(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000261 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +0000262 if (!(state = CheckNullStream(state->getSVal(CE->getArg(0),
263 C.getLocationContext()), state, C)))
Zhongxing Xue96a9132010-06-18 02:47:46 +0000264 return;
Zhongxing Xufacf8a82010-06-24 13:36:41 +0000265 // Check the legality of the 'whence' argument of 'fseek'.
Ted Kremenek632e3b72012-01-06 22:09:28 +0000266 SVal Whence = state->getSVal(CE->getArg(2), C.getLocationContext());
Zhongxing Xufacf8a82010-06-24 13:36:41 +0000267 const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Whence);
Ted Kremenekaba49582010-09-07 20:45:26 +0000268
Zhongxing Xufacf8a82010-06-24 13:36:41 +0000269 if (!CI)
Ted Kremenekaba49582010-09-07 20:45:26 +0000270 return;
Zhongxing Xufacf8a82010-06-24 13:36:41 +0000271
272 int64_t x = CI->getValue().getSExtValue();
Ted Kremenekaba49582010-09-07 20:45:26 +0000273 if (x >= 0 && x <= 2)
Zhongxing Xufacf8a82010-06-24 13:36:41 +0000274 return;
Zhongxing Xufacf8a82010-06-24 13:36:41 +0000275
Anna Zaksda4c8d62011-10-26 21:06:34 +0000276 if (ExplodedNode *N = C.addTransition(state)) {
Ted Kremenekaba49582010-09-07 20:45:26 +0000277 if (!BT_illegalwhence)
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000278 BT_illegalwhence.reset(new BuiltinBug("Illegal whence argument",
Ted Kremenekaba49582010-09-07 20:45:26 +0000279 "The whence argument to fseek() should be "
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000280 "SEEK_SET, SEEK_END, or SEEK_CUR."));
Ted Kremenekaba49582010-09-07 20:45:26 +0000281 BugReport *R = new BugReport(*BT_illegalwhence,
282 BT_illegalwhence->getDescription(), N);
Jordan Rosee10d5a72012-11-02 01:53:40 +0000283 C.emitReport(R);
Ted Kremenekaba49582010-09-07 20:45:26 +0000284 }
Zhongxing Xue96a9132010-06-18 02:47:46 +0000285}
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +0000286
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000287void StreamChecker::Ftell(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000288 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +0000289 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
290 state, C))
Zhongxing Xue96a9132010-06-18 02:47:46 +0000291 return;
292}
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +0000293
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000294void StreamChecker::Rewind(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000295 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +0000296 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
297 state, C))
Zhongxing Xue96a9132010-06-18 02:47:46 +0000298 return;
299}
300
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000301void StreamChecker::Fgetpos(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000302 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +0000303 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
304 state, C))
Zhongxing Xu45057472010-06-22 07:50:21 +0000305 return;
306}
307
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000308void StreamChecker::Fsetpos(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000309 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +0000310 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
311 state, C))
Zhongxing Xu45057472010-06-22 07:50:21 +0000312 return;
313}
314
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000315void StreamChecker::Clearerr(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000316 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +0000317 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
318 state, C))
Zhongxing Xu45057472010-06-22 07:50:21 +0000319 return;
320}
321
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000322void StreamChecker::Feof(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000323 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +0000324 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
325 state, C))
Zhongxing Xu45057472010-06-22 07:50:21 +0000326 return;
327}
328
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000329void StreamChecker::Ferror(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000330 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +0000331 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
332 state, C))
Zhongxing Xu45057472010-06-22 07:50:21 +0000333 return;
334}
335
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000336void StreamChecker::Fileno(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000337 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +0000338 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
339 state, C))
Zhongxing Xu45057472010-06-22 07:50:21 +0000340 return;
341}
342
Ted Kremenek49b1e382012-01-26 21:29:00 +0000343ProgramStateRef StreamChecker::CheckNullStream(SVal SV, ProgramStateRef state,
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000344 CheckerContext &C) const {
Zhongxing Xue96a9132010-06-18 02:47:46 +0000345 const DefinedSVal *DV = dyn_cast<DefinedSVal>(&SV);
346 if (!DV)
Ted Kremenek87f475a2010-06-24 16:26:12 +0000347 return 0;
Zhongxing Xue96a9132010-06-18 02:47:46 +0000348
349 ConstraintManager &CM = C.getConstraintManager();
Ted Kremenek49b1e382012-01-26 21:29:00 +0000350 ProgramStateRef stateNotNull, stateNull;
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000351 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
Zhongxing Xue96a9132010-06-18 02:47:46 +0000352
353 if (!stateNotNull && stateNull) {
Ted Kremenek750b7ac2010-12-20 21:19:09 +0000354 if (ExplodedNode *N = C.generateSink(stateNull)) {
Zhongxing Xue96a9132010-06-18 02:47:46 +0000355 if (!BT_nullfp)
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000356 BT_nullfp.reset(new BuiltinBug("NULL stream pointer",
357 "Stream pointer might be NULL."));
Zhongxing Xue96a9132010-06-18 02:47:46 +0000358 BugReport *R =new BugReport(*BT_nullfp, BT_nullfp->getDescription(), N);
Jordan Rosee10d5a72012-11-02 01:53:40 +0000359 C.emitReport(R);
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +0000360 }
Zhongxing Xuae451442010-06-24 12:52:28 +0000361 return 0;
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +0000362 }
Zhongxing Xua7e5dd62010-06-24 13:09:02 +0000363 return stateNotNull;
Zhongxing Xu0fa7cdd2010-06-16 05:38:05 +0000364}
Zhongxing Xuec562352010-07-19 01:52:29 +0000365
Ted Kremenek49b1e382012-01-26 21:29:00 +0000366ProgramStateRef StreamChecker::CheckDoubleClose(const CallExpr *CE,
367 ProgramStateRef state,
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000368 CheckerContext &C) const {
Ted Kremenek632e3b72012-01-06 22:09:28 +0000369 SymbolRef Sym =
370 state->getSVal(CE->getArg(0), C.getLocationContext()).getAsSymbol();
Ted Kremenek5c0969f2010-09-03 01:07:04 +0000371 if (!Sym)
372 return state;
Zhongxing Xuec562352010-07-19 01:52:29 +0000373
Jordan Roseb9ed61f2012-11-02 01:54:42 +0000374 const StreamState *SS = state->get<StreamMap>(Sym);
Zhongxing Xucf61a062010-08-05 23:24:13 +0000375
376 // If the file stream is not tracked, return.
377 if (!SS)
378 return state;
Zhongxing Xuec562352010-07-19 01:52:29 +0000379
380 // Check: Double close a File Descriptor could cause undefined behaviour.
381 // Conforming to man-pages
382 if (SS->isClosed()) {
Ted Kremenek750b7ac2010-12-20 21:19:09 +0000383 ExplodedNode *N = C.generateSink();
Zhongxing Xuec562352010-07-19 01:52:29 +0000384 if (N) {
385 if (!BT_doubleclose)
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000386 BT_doubleclose.reset(new BuiltinBug("Double fclose",
Eli Friedman04831922010-08-22 01:00:03 +0000387 "Try to close a file Descriptor already"
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000388 " closed. Cause undefined behaviour."));
Zhongxing Xuec562352010-07-19 01:52:29 +0000389 BugReport *R = new BugReport(*BT_doubleclose,
Eli Friedman04831922010-08-22 01:00:03 +0000390 BT_doubleclose->getDescription(), N);
Jordan Rosee10d5a72012-11-02 01:53:40 +0000391 C.emitReport(R);
Zhongxing Xuec562352010-07-19 01:52:29 +0000392 }
393 return NULL;
394 }
395
396 // Close the File Descriptor.
Jordan Roseb9ed61f2012-11-02 01:54:42 +0000397 return state->set<StreamMap>(Sym, StreamState::getClosed(CE));
Zhongxing Xuec562352010-07-19 01:52:29 +0000398}
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +0000399
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000400void StreamChecker::checkDeadSymbols(SymbolReaper &SymReaper,
401 CheckerContext &C) const {
Anna Zaks2ab03212012-10-29 22:51:44 +0000402 // TODO: Clean up the state.
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +0000403 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
Eli Friedman04831922010-08-22 01:00:03 +0000404 E = SymReaper.dead_end(); I != E; ++I) {
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +0000405 SymbolRef Sym = *I;
Ted Kremenek49b1e382012-01-26 21:29:00 +0000406 ProgramStateRef state = C.getState();
Jordan Roseb9ed61f2012-11-02 01:54:42 +0000407 const StreamState *SS = state->get<StreamMap>(Sym);
Anna Zaks2ab03212012-10-29 22:51:44 +0000408 // TODO: Shouldn't we have a continue here?
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +0000409 if (!SS)
410 return;
411
412 if (SS->isOpened()) {
Ted Kremenek750b7ac2010-12-20 21:19:09 +0000413 ExplodedNode *N = C.generateSink();
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +0000414 if (N) {
Eli Friedman04831922010-08-22 01:00:03 +0000415 if (!BT_ResourceLeak)
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000416 BT_ResourceLeak.reset(new BuiltinBug("Resource Leak",
417 "Opened File never closed. Potential Resource leak."));
Eli Friedman04831922010-08-22 01:00:03 +0000418 BugReport *R = new BugReport(*BT_ResourceLeak,
419 BT_ResourceLeak->getDescription(), N);
Jordan Rosee10d5a72012-11-02 01:53:40 +0000420 C.emitReport(R);
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +0000421 }
422 }
423 }
424}
425
Anna Zaks3eae3342011-10-25 19:56:48 +0000426void StreamChecker::checkEndPath(CheckerContext &Ctx) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +0000427 ProgramStateRef state = Ctx.getState();
Jordan Roseb9ed61f2012-11-02 01:54:42 +0000428 StreamMapTy M = state->get<StreamMap>();
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +0000429
Jordan Roseb9ed61f2012-11-02 01:54:42 +0000430 for (StreamMapTy::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +0000431 StreamState SS = I->second;
432 if (SS.isOpened()) {
Anna Zaksda4c8d62011-10-26 21:06:34 +0000433 ExplodedNode *N = Ctx.addTransition(state);
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +0000434 if (N) {
435 if (!BT_ResourceLeak)
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000436 BT_ResourceLeak.reset(new BuiltinBug("Resource Leak",
437 "Opened File never closed. Potential Resource leak."));
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +0000438 BugReport *R = new BugReport(*BT_ResourceLeak,
Eli Friedman04831922010-08-22 01:00:03 +0000439 BT_ResourceLeak->getDescription(), N);
Jordan Rosee10d5a72012-11-02 01:53:40 +0000440 Ctx.emitReport(R);
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +0000441 }
442 }
443 }
444}
445
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000446void StreamChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const {
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +0000447 const Expr *RetE = S->getRetValue();
448 if (!RetE)
449 return;
450
Ted Kremenek49b1e382012-01-26 21:29:00 +0000451 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +0000452 SymbolRef Sym = state->getSVal(RetE, C.getLocationContext()).getAsSymbol();
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +0000453
454 if (!Sym)
455 return;
456
Jordan Roseb9ed61f2012-11-02 01:54:42 +0000457 const StreamState *SS = state->get<StreamMap>(Sym);
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +0000458 if(!SS)
459 return;
460
461 if (SS->isOpened())
Jordan Roseb9ed61f2012-11-02 01:54:42 +0000462 state = state->set<StreamMap>(Sym, StreamState::getEscaped(S));
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +0000463
Anna Zaksda4c8d62011-10-26 21:06:34 +0000464 C.addTransition(state);
Zhongxing Xu5e6ef6d2010-07-23 14:14:59 +0000465}
Argyrios Kyrtzidis785459a2011-02-24 01:05:33 +0000466
467void ento::registerStreamChecker(CheckerManager &mgr) {
468 mgr.registerChecker<StreamChecker>();
469}