blob: c746c775ab6400c6c56b730751051bfdb0050cef [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"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000015#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000016#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +000017#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000018#include "clang/StaticAnalyzer/Core/BugReporter/BugType.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,
Argyrios Kyrtzidis699bbf92011-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 Xuc7de88b2010-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;
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +000067 mutable llvm::OwningPtr<BuiltinBug> BT_nullfp, BT_illegalwhence,
68 BT_doubleclose, BT_ResourceLeak;
Zhongxing Xuc1960952010-06-16 05:38:05 +000069
70public:
Zhongxing Xu23d90f92010-06-18 02:47:46 +000071 StreamChecker()
Zhongxing Xu47dc37f2010-07-22 14:01:01 +000072 : II_fopen(0), II_tmpfile(0) ,II_fclose(0), II_fread(0), II_fwrite(0),
Zhongxing Xuc7de88b2010-06-22 07:50:21 +000073 II_fseek(0), II_ftell(0), II_rewind(0), II_fgetpos(0), II_fsetpos(0),
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +000074 II_clearerr(0), II_feof(0), II_ferror(0), II_fileno(0) {}
Zhongxing Xuc1960952010-06-16 05:38:05 +000075
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +000076 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
77 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
Anna Zaksaf498a22011-10-25 19:56:48 +000078 void checkEndPath(CheckerContext &Ctx) const;
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +000079 void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
Zhongxing Xuc1960952010-06-16 05:38:05 +000080
81private:
Argyrios Kyrtzidis699bbf92011-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 Xu47dc37f2010-07-22 14:01:01 +000096
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +000097 void OpenFileAux(CheckerContext &C, const CallExpr *CE) const;
Zhongxing Xuc7de88b2010-06-22 07:50:21 +000098
Ted Kremenek18c66fd2011-08-15 22:09:50 +000099 const ProgramState *CheckNullStream(SVal SV, const ProgramState *state,
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000100 CheckerContext &C) const;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000101 const ProgramState *CheckDoubleClose(const CallExpr *CE, const ProgramState *state,
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000102 CheckerContext &C) const;
Zhongxing Xuc1960952010-06-16 05:38:05 +0000103};
104
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000105} // end anonymous namespace
Zhongxing Xuc1960952010-06-16 05:38:05 +0000106
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000107namespace clang {
Ted Kremenek9ef65372010-12-23 07:20:52 +0000108namespace ento {
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000109 template <>
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000110 struct ProgramStateTrait<StreamState>
111 : public ProgramStatePartialTrait<llvm::ImmutableMap<SymbolRef, StreamState> > {
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000112 static void *GDMIndex() { static int x; return &x; }
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000113 };
114}
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000115}
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000116
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000117bool StreamChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
Anna Zaksb805c8f2011-12-01 05:57:37 +0000118 const FunctionDecl *FD = C.getCalleeDecl(CE);
Zhongxing Xuc1960952010-06-16 05:38:05 +0000119 if (!FD)
120 return false;
121
122 ASTContext &Ctx = C.getASTContext();
123 if (!II_fopen)
124 II_fopen = &Ctx.Idents.get("fopen");
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000125 if (!II_tmpfile)
126 II_tmpfile = &Ctx.Idents.get("tmpfile");
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000127 if (!II_fclose)
128 II_fclose = &Ctx.Idents.get("fclose");
Zhongxing Xuc1960952010-06-16 05:38:05 +0000129 if (!II_fread)
130 II_fread = &Ctx.Idents.get("fread");
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000131 if (!II_fwrite)
132 II_fwrite = &Ctx.Idents.get("fwrite");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000133 if (!II_fseek)
134 II_fseek = &Ctx.Idents.get("fseek");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000135 if (!II_ftell)
136 II_ftell = &Ctx.Idents.get("ftell");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000137 if (!II_rewind)
138 II_rewind = &Ctx.Idents.get("rewind");
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000139 if (!II_fgetpos)
140 II_fgetpos = &Ctx.Idents.get("fgetpos");
141 if (!II_fsetpos)
142 II_fsetpos = &Ctx.Idents.get("fsetpos");
143 if (!II_clearerr)
144 II_clearerr = &Ctx.Idents.get("clearerr");
145 if (!II_feof)
146 II_feof = &Ctx.Idents.get("feof");
147 if (!II_ferror)
148 II_ferror = &Ctx.Idents.get("ferror");
149 if (!II_fileno)
150 II_fileno = &Ctx.Idents.get("fileno");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000151
Zhongxing Xuc1960952010-06-16 05:38:05 +0000152 if (FD->getIdentifier() == II_fopen) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000153 Fopen(C, CE);
Zhongxing Xuc1960952010-06-16 05:38:05 +0000154 return true;
155 }
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000156 if (FD->getIdentifier() == II_tmpfile) {
157 Tmpfile(C, CE);
158 return true;
159 }
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000160 if (FD->getIdentifier() == II_fclose) {
161 Fclose(C, CE);
162 return true;
163 }
Zhongxing Xuc1960952010-06-16 05:38:05 +0000164 if (FD->getIdentifier() == II_fread) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000165 Fread(C, CE);
Zhongxing Xuc1960952010-06-16 05:38:05 +0000166 return true;
167 }
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000168 if (FD->getIdentifier() == II_fwrite) {
169 Fwrite(C, CE);
170 return true;
171 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000172 if (FD->getIdentifier() == II_fseek) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000173 Fseek(C, CE);
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000174 return true;
175 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000176 if (FD->getIdentifier() == II_ftell) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000177 Ftell(C, CE);
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000178 return true;
179 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000180 if (FD->getIdentifier() == II_rewind) {
181 Rewind(C, CE);
182 return true;
183 }
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000184 if (FD->getIdentifier() == II_fgetpos) {
185 Fgetpos(C, CE);
186 return true;
187 }
188 if (FD->getIdentifier() == II_fsetpos) {
189 Fsetpos(C, CE);
190 return true;
191 }
192 if (FD->getIdentifier() == II_clearerr) {
193 Clearerr(C, CE);
194 return true;
195 }
196 if (FD->getIdentifier() == II_feof) {
197 Feof(C, CE);
198 return true;
199 }
200 if (FD->getIdentifier() == II_ferror) {
201 Ferror(C, CE);
202 return true;
203 }
204 if (FD->getIdentifier() == II_fileno) {
205 Fileno(C, CE);
206 return true;
207 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000208
Zhongxing Xuc1960952010-06-16 05:38:05 +0000209 return false;
210}
211
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000212void StreamChecker::Fopen(CheckerContext &C, const CallExpr *CE) const {
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000213 OpenFileAux(C, CE);
214}
215
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000216void StreamChecker::Tmpfile(CheckerContext &C, const CallExpr *CE) const {
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000217 OpenFileAux(C, CE);
218}
219
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000220void StreamChecker::OpenFileAux(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000221 const ProgramState *state = C.getState();
Anna Zaks5d0ea6d2011-10-04 20:43:05 +0000222 unsigned Count = C.getCurrentBlockCount();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000223 SValBuilder &svalBuilder = C.getSValBuilder();
224 DefinedSVal RetVal =
225 cast<DefinedSVal>(svalBuilder.getConjuredSymbolVal(0, CE, Count));
Ted Kremenek5eca4822012-01-06 22:09:28 +0000226 state = state->BindExpr(CE, C.getLocationContext(), RetVal);
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000227
Zhongxing Xuc1960952010-06-16 05:38:05 +0000228 ConstraintManager &CM = C.getConstraintManager();
229 // Bifurcate the state into two: one with a valid FILE* pointer, the other
230 // with a NULL.
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000231 const ProgramState *stateNotNull, *stateNull;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000232 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, RetVal);
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000233
Ted Kremenek80460372010-09-03 01:07:04 +0000234 if (SymbolRef Sym = RetVal.getAsSymbol()) {
235 // if RetVal is not NULL, set the symbol's state to Opened.
236 stateNotNull =
237 stateNotNull->set<StreamState>(Sym,StreamState::getOpened(CE));
238 stateNull =
239 stateNull->set<StreamState>(Sym, StreamState::getOpenFailed(CE));
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000240
Anna Zaks0bd6b112011-10-26 21:06:34 +0000241 C.addTransition(stateNotNull);
242 C.addTransition(stateNull);
Ted Kremenek80460372010-09-03 01:07:04 +0000243 }
Zhongxing Xuc1960952010-06-16 05:38:05 +0000244}
245
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000246void StreamChecker::Fclose(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000247 const ProgramState *state = CheckDoubleClose(CE, C.getState(), C);
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000248 if (state)
Anna Zaks0bd6b112011-10-26 21:06:34 +0000249 C.addTransition(state);
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000250}
251
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000252void StreamChecker::Fread(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000253 const ProgramState *state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000254 if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
255 state, C))
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000256 return;
257}
Zhongxing Xuc1960952010-06-16 05:38:05 +0000258
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000259void StreamChecker::Fwrite(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000260 const ProgramState *state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000261 if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
262 state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000263 return;
264}
265
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000266void StreamChecker::Fseek(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000267 const ProgramState *state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000268 if (!(state = CheckNullStream(state->getSVal(CE->getArg(0),
269 C.getLocationContext()), state, C)))
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000270 return;
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000271 // Check the legality of the 'whence' argument of 'fseek'.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000272 SVal Whence = state->getSVal(CE->getArg(2), C.getLocationContext());
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000273 const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Whence);
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000274
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000275 if (!CI)
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000276 return;
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000277
278 int64_t x = CI->getValue().getSExtValue();
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000279 if (x >= 0 && x <= 2)
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000280 return;
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000281
Anna Zaks0bd6b112011-10-26 21:06:34 +0000282 if (ExplodedNode *N = C.addTransition(state)) {
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000283 if (!BT_illegalwhence)
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000284 BT_illegalwhence.reset(new BuiltinBug("Illegal whence argument",
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000285 "The whence argument to fseek() should be "
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000286 "SEEK_SET, SEEK_END, or SEEK_CUR."));
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000287 BugReport *R = new BugReport(*BT_illegalwhence,
288 BT_illegalwhence->getDescription(), N);
289 C.EmitReport(R);
290 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000291}
Zhongxing Xuc1960952010-06-16 05:38:05 +0000292
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000293void StreamChecker::Ftell(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000294 const ProgramState *state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000295 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
296 state, C))
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000297 return;
298}
Zhongxing Xuc1960952010-06-16 05:38:05 +0000299
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000300void StreamChecker::Rewind(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000301 const ProgramState *state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000302 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
303 state, C))
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000304 return;
305}
306
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000307void StreamChecker::Fgetpos(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000308 const ProgramState *state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000309 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
310 state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000311 return;
312}
313
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000314void StreamChecker::Fsetpos(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000315 const ProgramState *state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000316 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
317 state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000318 return;
319}
320
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000321void StreamChecker::Clearerr(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000322 const ProgramState *state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000323 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
324 state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000325 return;
326}
327
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000328void StreamChecker::Feof(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000329 const ProgramState *state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000330 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
331 state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000332 return;
333}
334
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000335void StreamChecker::Ferror(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000336 const ProgramState *state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000337 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
338 state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000339 return;
340}
341
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000342void StreamChecker::Fileno(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000343 const ProgramState *state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000344 if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
345 state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000346 return;
347}
348
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000349const ProgramState *StreamChecker::CheckNullStream(SVal SV, const ProgramState *state,
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000350 CheckerContext &C) const {
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000351 const DefinedSVal *DV = dyn_cast<DefinedSVal>(&SV);
352 if (!DV)
Ted Kremenek2b11fb22010-06-24 16:26:12 +0000353 return 0;
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000354
355 ConstraintManager &CM = C.getConstraintManager();
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000356 const ProgramState *stateNotNull, *stateNull;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000357 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000358
359 if (!stateNotNull && stateNull) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000360 if (ExplodedNode *N = C.generateSink(stateNull)) {
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000361 if (!BT_nullfp)
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000362 BT_nullfp.reset(new BuiltinBug("NULL stream pointer",
363 "Stream pointer might be NULL."));
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000364 BugReport *R =new BugReport(*BT_nullfp, BT_nullfp->getDescription(), N);
365 C.EmitReport(R);
Zhongxing Xuc1960952010-06-16 05:38:05 +0000366 }
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000367 return 0;
Zhongxing Xuc1960952010-06-16 05:38:05 +0000368 }
Zhongxing Xuab421302010-06-24 13:09:02 +0000369 return stateNotNull;
Zhongxing Xuc1960952010-06-16 05:38:05 +0000370}
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000371
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000372const ProgramState *StreamChecker::CheckDoubleClose(const CallExpr *CE,
373 const ProgramState *state,
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000374 CheckerContext &C) const {
Ted Kremenek5eca4822012-01-06 22:09:28 +0000375 SymbolRef Sym =
376 state->getSVal(CE->getArg(0), C.getLocationContext()).getAsSymbol();
Ted Kremenek80460372010-09-03 01:07:04 +0000377 if (!Sym)
378 return state;
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000379
380 const StreamState *SS = state->get<StreamState>(Sym);
Zhongxing Xu1c7370f2010-08-05 23:24:13 +0000381
382 // If the file stream is not tracked, return.
383 if (!SS)
384 return state;
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000385
386 // Check: Double close a File Descriptor could cause undefined behaviour.
387 // Conforming to man-pages
388 if (SS->isClosed()) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000389 ExplodedNode *N = C.generateSink();
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000390 if (N) {
391 if (!BT_doubleclose)
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000392 BT_doubleclose.reset(new BuiltinBug("Double fclose",
Eli Friedmana7e68452010-08-22 01:00:03 +0000393 "Try to close a file Descriptor already"
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000394 " closed. Cause undefined behaviour."));
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000395 BugReport *R = new BugReport(*BT_doubleclose,
Eli Friedmana7e68452010-08-22 01:00:03 +0000396 BT_doubleclose->getDescription(), N);
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000397 C.EmitReport(R);
398 }
399 return NULL;
400 }
401
402 // Close the File Descriptor.
403 return state->set<StreamState>(Sym, StreamState::getClosed(CE));
404}
Zhongxing Xu766c2012010-07-23 14:14:59 +0000405
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000406void StreamChecker::checkDeadSymbols(SymbolReaper &SymReaper,
407 CheckerContext &C) const {
Zhongxing Xu766c2012010-07-23 14:14:59 +0000408 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
Eli Friedmana7e68452010-08-22 01:00:03 +0000409 E = SymReaper.dead_end(); I != E; ++I) {
Zhongxing Xu766c2012010-07-23 14:14:59 +0000410 SymbolRef Sym = *I;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000411 const ProgramState *state = C.getState();
Zhongxing Xu766c2012010-07-23 14:14:59 +0000412 const StreamState *SS = state->get<StreamState>(Sym);
413 if (!SS)
414 return;
415
416 if (SS->isOpened()) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000417 ExplodedNode *N = C.generateSink();
Zhongxing Xu766c2012010-07-23 14:14:59 +0000418 if (N) {
Eli Friedmana7e68452010-08-22 01:00:03 +0000419 if (!BT_ResourceLeak)
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000420 BT_ResourceLeak.reset(new BuiltinBug("Resource Leak",
421 "Opened File never closed. Potential Resource leak."));
Eli Friedmana7e68452010-08-22 01:00:03 +0000422 BugReport *R = new BugReport(*BT_ResourceLeak,
423 BT_ResourceLeak->getDescription(), N);
424 C.EmitReport(R);
Zhongxing Xu766c2012010-07-23 14:14:59 +0000425 }
426 }
427 }
428}
429
Anna Zaksaf498a22011-10-25 19:56:48 +0000430void StreamChecker::checkEndPath(CheckerContext &Ctx) const {
431 const ProgramState *state = Ctx.getState();
Zhongxing Xu766c2012010-07-23 14:14:59 +0000432 typedef llvm::ImmutableMap<SymbolRef, StreamState> SymMap;
433 SymMap M = state->get<StreamState>();
434
435 for (SymMap::iterator I = M.begin(), E = M.end(); I != E; ++I) {
436 StreamState SS = I->second;
437 if (SS.isOpened()) {
Anna Zaks0bd6b112011-10-26 21:06:34 +0000438 ExplodedNode *N = Ctx.addTransition(state);
Zhongxing Xu766c2012010-07-23 14:14:59 +0000439 if (N) {
440 if (!BT_ResourceLeak)
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000441 BT_ResourceLeak.reset(new BuiltinBug("Resource Leak",
442 "Opened File never closed. Potential Resource leak."));
Zhongxing Xu766c2012010-07-23 14:14:59 +0000443 BugReport *R = new BugReport(*BT_ResourceLeak,
Eli Friedmana7e68452010-08-22 01:00:03 +0000444 BT_ResourceLeak->getDescription(), N);
Anna Zaksaf498a22011-10-25 19:56:48 +0000445 Ctx.EmitReport(R);
Zhongxing Xu766c2012010-07-23 14:14:59 +0000446 }
447 }
448 }
449}
450
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000451void StreamChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const {
Zhongxing Xu766c2012010-07-23 14:14:59 +0000452 const Expr *RetE = S->getRetValue();
453 if (!RetE)
454 return;
455
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000456 const ProgramState *state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000457 SymbolRef Sym = state->getSVal(RetE, C.getLocationContext()).getAsSymbol();
Zhongxing Xu766c2012010-07-23 14:14:59 +0000458
459 if (!Sym)
460 return;
461
462 const StreamState *SS = state->get<StreamState>(Sym);
463 if(!SS)
464 return;
465
466 if (SS->isOpened())
467 state = state->set<StreamState>(Sym, StreamState::getEscaped(S));
468
Anna Zaks0bd6b112011-10-26 21:06:34 +0000469 C.addTransition(state);
Zhongxing Xu766c2012010-07-23 14:14:59 +0000470}
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000471
472void ento::registerStreamChecker(CheckerManager &mgr) {
473 mgr.registerChecker<StreamChecker>();
474}