blob: 1c38ab0b184d78a05dcc592cd69224abbd65f44a [file] [log] [blame]
Zhongxing Xuc1960952010-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 Kyrtzidisa0decc92011-02-15 21:25:03 +000014#include "ClangSACheckers.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000017#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +000018#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek18c66fd2011-08-15 22:09:50 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
20#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
Zhongxing Xuc1960952010-06-16 05:38:05 +000022#include "llvm/ADT/ImmutableMap.h"
23
24using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000025using namespace ento;
Zhongxing Xuc1960952010-06-16 05:38:05 +000026
27namespace {
28
Zhongxing Xu9843ba92010-07-19 01:52:29 +000029struct StreamState {
Zhongxing Xu766c2012010-07-23 14:14:59 +000030 enum Kind { Opened, Closed, OpenFailed, Escaped } K;
Zhongxing Xu9843ba92010-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 Lattnerfae96222010-09-03 04:34:38 +000037 //bool isOpenFailed() const { return K == OpenFailed; }
38 //bool isEscaped() const { return K == Escaped; }
Zhongxing Xu9843ba92010-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 Xu47dc37f2010-07-22 14:01:01 +000046 static StreamState getOpenFailed(const Stmt *s) {
47 return StreamState(OpenFailed, s);
48 }
Zhongxing Xu766c2012010-07-23 14:14:59 +000049 static StreamState getEscaped(const Stmt *s) {
50 return StreamState(Escaped, s);
51 }
Zhongxing Xu9843ba92010-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 Kyrtzidisec8605f2011-03-01 01:16:21 +000059class StreamChecker : public Checker<eval::Call,
Jordan Rosef34a5792012-11-15 19:11:38 +000060 check::DeadSymbols > {
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +000061 mutable IdentifierInfo *II_fopen, *II_tmpfile, *II_fclose, *II_fread,
62 *II_fwrite,
Zhongxing Xuc7de88b2010-06-22 07:50:21 +000063 *II_fseek, *II_ftell, *II_rewind, *II_fgetpos, *II_fsetpos,
64 *II_clearerr, *II_feof, *II_ferror, *II_fileno;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000065 mutable OwningPtr<BuiltinBug> BT_nullfp, BT_illegalwhence,
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +000066 BT_doubleclose, BT_ResourceLeak;
Zhongxing Xuc1960952010-06-16 05:38:05 +000067
68public:
Zhongxing Xu23d90f92010-06-18 02:47:46 +000069 StreamChecker()
Zhongxing Xu47dc37f2010-07-22 14:01:01 +000070 : II_fopen(0), II_tmpfile(0) ,II_fclose(0), II_fread(0), II_fwrite(0),
Zhongxing Xuc7de88b2010-06-22 07:50:21 +000071 II_fseek(0), II_ftell(0), II_rewind(0), II_fgetpos(0), II_fsetpos(0),
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +000072 II_clearerr(0), II_feof(0), II_ferror(0), II_fileno(0) {}
Zhongxing Xuc1960952010-06-16 05:38:05 +000073
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +000074 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
75 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
Zhongxing Xuc1960952010-06-16 05:38:05 +000076
77private:
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +000078 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;
Zhongxing Xu47dc37f2010-07-22 14:01:01 +000092
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +000093 void OpenFileAux(CheckerContext &C, const CallExpr *CE) const;
Zhongxing Xuc7de88b2010-06-22 07:50:21 +000094
Ted Kremenek8bef8232012-01-26 21:29:00 +000095 ProgramStateRef CheckNullStream(SVal SV, ProgramStateRef state,
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +000096 CheckerContext &C) const;
Ted Kremenek8bef8232012-01-26 21:29:00 +000097 ProgramStateRef CheckDoubleClose(const CallExpr *CE, ProgramStateRef state,
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +000098 CheckerContext &C) const;
Zhongxing Xuc1960952010-06-16 05:38:05 +000099};
100
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000101} // end anonymous namespace
Zhongxing Xuc1960952010-06-16 05:38:05 +0000102
Jordan Rose466224f2012-11-02 01:54:42 +0000103REGISTER_MAP_WITH_PROGRAMSTATE(StreamMap, SymbolRef, StreamState)
104
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000105
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000106bool StreamChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
Anna Zaksb805c8f2011-12-01 05:57:37 +0000107 const FunctionDecl *FD = C.getCalleeDecl(CE);
Jordan Rose5ef6e942012-07-10 23:13:01 +0000108 if (!FD || FD->getKind() != Decl::Function)
Zhongxing Xuc1960952010-06-16 05:38:05 +0000109 return false;
110
111 ASTContext &Ctx = C.getASTContext();
112 if (!II_fopen)
113 II_fopen = &Ctx.Idents.get("fopen");
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000114 if (!II_tmpfile)
115 II_tmpfile = &Ctx.Idents.get("tmpfile");
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000116 if (!II_fclose)
117 II_fclose = &Ctx.Idents.get("fclose");
Zhongxing Xuc1960952010-06-16 05:38:05 +0000118 if (!II_fread)
119 II_fread = &Ctx.Idents.get("fread");
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000120 if (!II_fwrite)
121 II_fwrite = &Ctx.Idents.get("fwrite");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000122 if (!II_fseek)
123 II_fseek = &Ctx.Idents.get("fseek");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000124 if (!II_ftell)
125 II_ftell = &Ctx.Idents.get("ftell");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000126 if (!II_rewind)
127 II_rewind = &Ctx.Idents.get("rewind");
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000128 if (!II_fgetpos)
129 II_fgetpos = &Ctx.Idents.get("fgetpos");
130 if (!II_fsetpos)
131 II_fsetpos = &Ctx.Idents.get("fsetpos");
132 if (!II_clearerr)
133 II_clearerr = &Ctx.Idents.get("clearerr");
134 if (!II_feof)
135 II_feof = &Ctx.Idents.get("feof");
136 if (!II_ferror)
137 II_ferror = &Ctx.Idents.get("ferror");
138 if (!II_fileno)
139 II_fileno = &Ctx.Idents.get("fileno");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000140
Zhongxing Xuc1960952010-06-16 05:38:05 +0000141 if (FD->getIdentifier() == II_fopen) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000142 Fopen(C, CE);
Zhongxing Xuc1960952010-06-16 05:38:05 +0000143 return true;
144 }
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000145 if (FD->getIdentifier() == II_tmpfile) {
146 Tmpfile(C, CE);
147 return true;
148 }
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000149 if (FD->getIdentifier() == II_fclose) {
150 Fclose(C, CE);
151 return true;
152 }
Zhongxing Xuc1960952010-06-16 05:38:05 +0000153 if (FD->getIdentifier() == II_fread) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000154 Fread(C, CE);
Zhongxing Xuc1960952010-06-16 05:38:05 +0000155 return true;
156 }
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000157 if (FD->getIdentifier() == II_fwrite) {
158 Fwrite(C, CE);
159 return true;
160 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000161 if (FD->getIdentifier() == II_fseek) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000162 Fseek(C, CE);
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000163 return true;
164 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000165 if (FD->getIdentifier() == II_ftell) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000166 Ftell(C, CE);
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000167 return true;
168 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000169 if (FD->getIdentifier() == II_rewind) {
170 Rewind(C, CE);
171 return true;
172 }
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000173 if (FD->getIdentifier() == II_fgetpos) {
174 Fgetpos(C, CE);
175 return true;
176 }
177 if (FD->getIdentifier() == II_fsetpos) {
178 Fsetpos(C, CE);
179 return true;
180 }
181 if (FD->getIdentifier() == II_clearerr) {
182 Clearerr(C, CE);
183 return true;
184 }
185 if (FD->getIdentifier() == II_feof) {
186 Feof(C, CE);
187 return true;
188 }
189 if (FD->getIdentifier() == II_ferror) {
190 Ferror(C, CE);
191 return true;
192 }
193 if (FD->getIdentifier() == II_fileno) {
194 Fileno(C, CE);
195 return true;
196 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000197
Zhongxing Xuc1960952010-06-16 05:38:05 +0000198 return false;
199}
200
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000201void StreamChecker::Fopen(CheckerContext &C, const CallExpr *CE) const {
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000202 OpenFileAux(C, CE);
203}
204
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000205void StreamChecker::Tmpfile(CheckerContext &C, const CallExpr *CE) const {
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000206 OpenFileAux(C, CE);
207}
208
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000209void StreamChecker::OpenFileAux(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000210 ProgramStateRef state = C.getState();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000211 SValBuilder &svalBuilder = C.getSValBuilder();
Ted Kremenek3133f792012-02-17 23:13:45 +0000212 const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
David Blaikie5251abe2013-02-20 05:52:05 +0000213 DefinedSVal RetVal = svalBuilder.conjureSymbolVal(0, CE, LCtx, C.blockCount())
214 .castAs<DefinedSVal>();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000215 state = state->BindExpr(CE, C.getLocationContext(), RetVal);
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000216
Zhongxing Xuc1960952010-06-16 05:38:05 +0000217 ConstraintManager &CM = C.getConstraintManager();
218 // Bifurcate the state into two: one with a valid FILE* pointer, the other
219 // with a NULL.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000220 ProgramStateRef stateNotNull, stateNull;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000221 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, RetVal);
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000222
Ted Kremenek80460372010-09-03 01:07:04 +0000223 if (SymbolRef Sym = RetVal.getAsSymbol()) {
224 // if RetVal is not NULL, set the symbol's state to Opened.
225 stateNotNull =
Jordan Rose466224f2012-11-02 01:54:42 +0000226 stateNotNull->set<StreamMap>(Sym,StreamState::getOpened(CE));
Ted Kremenek80460372010-09-03 01:07:04 +0000227 stateNull =
Jordan Rose466224f2012-11-02 01:54:42 +0000228 stateNull->set<StreamMap>(Sym, StreamState::getOpenFailed(CE));
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000229
Anna Zaks0bd6b112011-10-26 21:06:34 +0000230 C.addTransition(stateNotNull);
231 C.addTransition(stateNull);
Ted Kremenek80460372010-09-03 01:07:04 +0000232 }
Zhongxing Xuc1960952010-06-16 05:38:05 +0000233}
234
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000235void StreamChecker::Fclose(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000236 ProgramStateRef state = CheckDoubleClose(CE, C.getState(), C);
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000237 if (state)
Anna Zaks0bd6b112011-10-26 21:06:34 +0000238 C.addTransition(state);
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000239}
240
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000241void StreamChecker::Fread(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000242 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000243 if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
244 state, C))
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000245 return;
246}
Zhongxing Xuc1960952010-06-16 05:38:05 +0000247
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000248void StreamChecker::Fwrite(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000249 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000250 if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
251 state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000252 return;
253}
254
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000255void StreamChecker::Fseek(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000256 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000257 if (!(state = CheckNullStream(state->getSVal(CE->getArg(0),
258 C.getLocationContext()), state, C)))
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000259 return;
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000260 // Check the legality of the 'whence' argument of 'fseek'.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000261 SVal Whence = state->getSVal(CE->getArg(2), C.getLocationContext());
David Blaikiedc84cd52013-02-20 22:23:23 +0000262 Optional<nonloc::ConcreteInt> CI = Whence.getAs<nonloc::ConcreteInt>();
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000263
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000264 if (!CI)
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000265 return;
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000266
267 int64_t x = CI->getValue().getSExtValue();
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000268 if (x >= 0 && x <= 2)
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000269 return;
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000270
Anna Zaks0bd6b112011-10-26 21:06:34 +0000271 if (ExplodedNode *N = C.addTransition(state)) {
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000272 if (!BT_illegalwhence)
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000273 BT_illegalwhence.reset(new BuiltinBug("Illegal whence argument",
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000274 "The whence argument to fseek() should be "
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000275 "SEEK_SET, SEEK_END, or SEEK_CUR."));
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000276 BugReport *R = new BugReport(*BT_illegalwhence,
277 BT_illegalwhence->getDescription(), N);
Jordan Rose785950e2012-11-02 01:53:40 +0000278 C.emitReport(R);
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000279 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000280}
Zhongxing Xuc1960952010-06-16 05:38:05 +0000281
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000282void StreamChecker::Ftell(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000283 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000284 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
285 state, C))
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000286 return;
287}
Zhongxing Xuc1960952010-06-16 05:38:05 +0000288
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000289void StreamChecker::Rewind(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000290 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000291 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
292 state, C))
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000293 return;
294}
295
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000296void StreamChecker::Fgetpos(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000297 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000298 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
299 state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000300 return;
301}
302
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000303void StreamChecker::Fsetpos(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000304 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000305 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
306 state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000307 return;
308}
309
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000310void StreamChecker::Clearerr(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000311 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000312 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
313 state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000314 return;
315}
316
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000317void StreamChecker::Feof(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000318 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000319 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
320 state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000321 return;
322}
323
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000324void StreamChecker::Ferror(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000325 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000326 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
327 state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000328 return;
329}
330
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000331void StreamChecker::Fileno(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000332 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000333 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
334 state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000335 return;
336}
337
Ted Kremenek8bef8232012-01-26 21:29:00 +0000338ProgramStateRef StreamChecker::CheckNullStream(SVal SV, ProgramStateRef state,
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000339 CheckerContext &C) const {
David Blaikiedc84cd52013-02-20 22:23:23 +0000340 Optional<DefinedSVal> DV = SV.getAs<DefinedSVal>();
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000341 if (!DV)
Ted Kremenek2b11fb22010-06-24 16:26:12 +0000342 return 0;
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000343
344 ConstraintManager &CM = C.getConstraintManager();
Ted Kremenek8bef8232012-01-26 21:29:00 +0000345 ProgramStateRef stateNotNull, stateNull;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000346 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000347
348 if (!stateNotNull && stateNull) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000349 if (ExplodedNode *N = C.generateSink(stateNull)) {
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000350 if (!BT_nullfp)
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000351 BT_nullfp.reset(new BuiltinBug("NULL stream pointer",
352 "Stream pointer might be NULL."));
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000353 BugReport *R =new BugReport(*BT_nullfp, BT_nullfp->getDescription(), N);
Jordan Rose785950e2012-11-02 01:53:40 +0000354 C.emitReport(R);
Zhongxing Xuc1960952010-06-16 05:38:05 +0000355 }
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000356 return 0;
Zhongxing Xuc1960952010-06-16 05:38:05 +0000357 }
Zhongxing Xuab421302010-06-24 13:09:02 +0000358 return stateNotNull;
Zhongxing Xuc1960952010-06-16 05:38:05 +0000359}
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000360
Ted Kremenek8bef8232012-01-26 21:29:00 +0000361ProgramStateRef StreamChecker::CheckDoubleClose(const CallExpr *CE,
362 ProgramStateRef state,
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000363 CheckerContext &C) const {
Ted Kremenek5eca4822012-01-06 22:09:28 +0000364 SymbolRef Sym =
365 state->getSVal(CE->getArg(0), C.getLocationContext()).getAsSymbol();
Ted Kremenek80460372010-09-03 01:07:04 +0000366 if (!Sym)
367 return state;
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000368
Jordan Rose466224f2012-11-02 01:54:42 +0000369 const StreamState *SS = state->get<StreamMap>(Sym);
Zhongxing Xu1c7370f2010-08-05 23:24:13 +0000370
371 // If the file stream is not tracked, return.
372 if (!SS)
373 return state;
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000374
375 // Check: Double close a File Descriptor could cause undefined behaviour.
376 // Conforming to man-pages
377 if (SS->isClosed()) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000378 ExplodedNode *N = C.generateSink();
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000379 if (N) {
380 if (!BT_doubleclose)
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000381 BT_doubleclose.reset(new BuiltinBug("Double fclose",
Eli Friedmana7e68452010-08-22 01:00:03 +0000382 "Try to close a file Descriptor already"
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000383 " closed. Cause undefined behaviour."));
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000384 BugReport *R = new BugReport(*BT_doubleclose,
Eli Friedmana7e68452010-08-22 01:00:03 +0000385 BT_doubleclose->getDescription(), N);
Jordan Rose785950e2012-11-02 01:53:40 +0000386 C.emitReport(R);
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000387 }
388 return NULL;
389 }
390
391 // Close the File Descriptor.
Jordan Rose466224f2012-11-02 01:54:42 +0000392 return state->set<StreamMap>(Sym, StreamState::getClosed(CE));
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000393}
Zhongxing Xu766c2012010-07-23 14:14:59 +0000394
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000395void StreamChecker::checkDeadSymbols(SymbolReaper &SymReaper,
396 CheckerContext &C) const {
Anna Zaks5ac1df32012-10-29 22:51:44 +0000397 // TODO: Clean up the state.
Zhongxing Xu766c2012010-07-23 14:14:59 +0000398 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
Eli Friedmana7e68452010-08-22 01:00:03 +0000399 E = SymReaper.dead_end(); I != E; ++I) {
Zhongxing Xu766c2012010-07-23 14:14:59 +0000400 SymbolRef Sym = *I;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000401 ProgramStateRef state = C.getState();
Jordan Rose466224f2012-11-02 01:54:42 +0000402 const StreamState *SS = state->get<StreamMap>(Sym);
Anna Zaks5ac1df32012-10-29 22:51:44 +0000403 // TODO: Shouldn't we have a continue here?
Zhongxing Xu766c2012010-07-23 14:14:59 +0000404 if (!SS)
405 return;
406
407 if (SS->isOpened()) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000408 ExplodedNode *N = C.generateSink();
Zhongxing Xu766c2012010-07-23 14:14:59 +0000409 if (N) {
Eli Friedmana7e68452010-08-22 01:00:03 +0000410 if (!BT_ResourceLeak)
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000411 BT_ResourceLeak.reset(new BuiltinBug("Resource Leak",
412 "Opened File never closed. Potential Resource leak."));
Eli Friedmana7e68452010-08-22 01:00:03 +0000413 BugReport *R = new BugReport(*BT_ResourceLeak,
414 BT_ResourceLeak->getDescription(), N);
Jordan Rose785950e2012-11-02 01:53:40 +0000415 C.emitReport(R);
Zhongxing Xu766c2012010-07-23 14:14:59 +0000416 }
417 }
418 }
419}
420
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000421void ento::registerStreamChecker(CheckerManager &mgr) {
422 mgr.registerChecker<StreamChecker>();
423}