blob: 8010e8dbaca0e6e16235b817fbfe71de91809106 [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 {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000118 const ProgramState *state = C.getState();
Zhongxing Xuc1960952010-06-16 05:38:05 +0000119 const Expr *Callee = CE->getCallee();
120 SVal L = state->getSVal(Callee);
121 const FunctionDecl *FD = L.getAsFunctionDecl();
122 if (!FD)
123 return false;
124
125 ASTContext &Ctx = C.getASTContext();
126 if (!II_fopen)
127 II_fopen = &Ctx.Idents.get("fopen");
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000128 if (!II_tmpfile)
129 II_tmpfile = &Ctx.Idents.get("tmpfile");
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000130 if (!II_fclose)
131 II_fclose = &Ctx.Idents.get("fclose");
Zhongxing Xuc1960952010-06-16 05:38:05 +0000132 if (!II_fread)
133 II_fread = &Ctx.Idents.get("fread");
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000134 if (!II_fwrite)
135 II_fwrite = &Ctx.Idents.get("fwrite");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000136 if (!II_fseek)
137 II_fseek = &Ctx.Idents.get("fseek");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000138 if (!II_ftell)
139 II_ftell = &Ctx.Idents.get("ftell");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000140 if (!II_rewind)
141 II_rewind = &Ctx.Idents.get("rewind");
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000142 if (!II_fgetpos)
143 II_fgetpos = &Ctx.Idents.get("fgetpos");
144 if (!II_fsetpos)
145 II_fsetpos = &Ctx.Idents.get("fsetpos");
146 if (!II_clearerr)
147 II_clearerr = &Ctx.Idents.get("clearerr");
148 if (!II_feof)
149 II_feof = &Ctx.Idents.get("feof");
150 if (!II_ferror)
151 II_ferror = &Ctx.Idents.get("ferror");
152 if (!II_fileno)
153 II_fileno = &Ctx.Idents.get("fileno");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000154
Zhongxing Xuc1960952010-06-16 05:38:05 +0000155 if (FD->getIdentifier() == II_fopen) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000156 Fopen(C, CE);
Zhongxing Xuc1960952010-06-16 05:38:05 +0000157 return true;
158 }
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000159 if (FD->getIdentifier() == II_tmpfile) {
160 Tmpfile(C, CE);
161 return true;
162 }
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000163 if (FD->getIdentifier() == II_fclose) {
164 Fclose(C, CE);
165 return true;
166 }
Zhongxing Xuc1960952010-06-16 05:38:05 +0000167 if (FD->getIdentifier() == II_fread) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000168 Fread(C, CE);
Zhongxing Xuc1960952010-06-16 05:38:05 +0000169 return true;
170 }
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000171 if (FD->getIdentifier() == II_fwrite) {
172 Fwrite(C, CE);
173 return true;
174 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000175 if (FD->getIdentifier() == II_fseek) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000176 Fseek(C, CE);
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000177 return true;
178 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000179 if (FD->getIdentifier() == II_ftell) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000180 Ftell(C, CE);
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000181 return true;
182 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000183 if (FD->getIdentifier() == II_rewind) {
184 Rewind(C, CE);
185 return true;
186 }
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000187 if (FD->getIdentifier() == II_fgetpos) {
188 Fgetpos(C, CE);
189 return true;
190 }
191 if (FD->getIdentifier() == II_fsetpos) {
192 Fsetpos(C, CE);
193 return true;
194 }
195 if (FD->getIdentifier() == II_clearerr) {
196 Clearerr(C, CE);
197 return true;
198 }
199 if (FD->getIdentifier() == II_feof) {
200 Feof(C, CE);
201 return true;
202 }
203 if (FD->getIdentifier() == II_ferror) {
204 Ferror(C, CE);
205 return true;
206 }
207 if (FD->getIdentifier() == II_fileno) {
208 Fileno(C, CE);
209 return true;
210 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000211
Zhongxing Xuc1960952010-06-16 05:38:05 +0000212 return false;
213}
214
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000215void StreamChecker::Fopen(CheckerContext &C, const CallExpr *CE) const {
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000216 OpenFileAux(C, CE);
217}
218
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000219void StreamChecker::Tmpfile(CheckerContext &C, const CallExpr *CE) const {
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000220 OpenFileAux(C, CE);
221}
222
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000223void StreamChecker::OpenFileAux(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000224 const ProgramState *state = C.getState();
Anna Zaks5d0ea6d2011-10-04 20:43:05 +0000225 unsigned Count = C.getCurrentBlockCount();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000226 SValBuilder &svalBuilder = C.getSValBuilder();
227 DefinedSVal RetVal =
228 cast<DefinedSVal>(svalBuilder.getConjuredSymbolVal(0, CE, Count));
Zhongxing Xuc1960952010-06-16 05:38:05 +0000229 state = state->BindExpr(CE, RetVal);
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000230
Zhongxing Xuc1960952010-06-16 05:38:05 +0000231 ConstraintManager &CM = C.getConstraintManager();
232 // Bifurcate the state into two: one with a valid FILE* pointer, the other
233 // with a NULL.
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000234 const ProgramState *stateNotNull, *stateNull;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000235 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, RetVal);
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000236
Ted Kremenek80460372010-09-03 01:07:04 +0000237 if (SymbolRef Sym = RetVal.getAsSymbol()) {
238 // if RetVal is not NULL, set the symbol's state to Opened.
239 stateNotNull =
240 stateNotNull->set<StreamState>(Sym,StreamState::getOpened(CE));
241 stateNull =
242 stateNull->set<StreamState>(Sym, StreamState::getOpenFailed(CE));
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000243
Ted Kremenek80460372010-09-03 01:07:04 +0000244 C.addTransition(stateNotNull);
245 C.addTransition(stateNull);
246 }
Zhongxing Xuc1960952010-06-16 05:38:05 +0000247}
248
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000249void StreamChecker::Fclose(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000250 const ProgramState *state = CheckDoubleClose(CE, C.getState(), C);
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000251 if (state)
252 C.addTransition(state);
253}
254
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000255void StreamChecker::Fread(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000256 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000257 if (!CheckNullStream(state->getSVal(CE->getArg(3)), state, C))
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000258 return;
259}
Zhongxing Xuc1960952010-06-16 05:38:05 +0000260
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000261void StreamChecker::Fwrite(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000262 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000263 if (!CheckNullStream(state->getSVal(CE->getArg(3)), state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000264 return;
265}
266
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000267void StreamChecker::Fseek(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000268 const ProgramState *state = C.getState();
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000269 if (!(state = CheckNullStream(state->getSVal(CE->getArg(0)), 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'.
272 SVal Whence = state->getSVal(CE->getArg(2));
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
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000282 if (ExplodedNode *N = C.generateNode(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();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000295 if (!CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000296 return;
297}
Zhongxing Xuc1960952010-06-16 05:38:05 +0000298
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000299void StreamChecker::Rewind(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000300 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000301 if (!CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000302 return;
303}
304
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000305void StreamChecker::Fgetpos(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000306 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000307 if (!CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000308 return;
309}
310
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000311void StreamChecker::Fsetpos(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000312 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000313 if (!CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000314 return;
315}
316
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000317void StreamChecker::Clearerr(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000318 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000319 if (!CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000320 return;
321}
322
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000323void StreamChecker::Feof(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000324 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000325 if (!CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000326 return;
327}
328
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000329void StreamChecker::Ferror(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000330 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000331 if (!CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000332 return;
333}
334
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000335void StreamChecker::Fileno(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000336 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000337 if (!CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000338 return;
339}
340
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000341const ProgramState *StreamChecker::CheckNullStream(SVal SV, const ProgramState *state,
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000342 CheckerContext &C) const {
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000343 const DefinedSVal *DV = dyn_cast<DefinedSVal>(&SV);
344 if (!DV)
Ted Kremenek2b11fb22010-06-24 16:26:12 +0000345 return 0;
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000346
347 ConstraintManager &CM = C.getConstraintManager();
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000348 const ProgramState *stateNotNull, *stateNull;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000349 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000350
351 if (!stateNotNull && stateNull) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000352 if (ExplodedNode *N = C.generateSink(stateNull)) {
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000353 if (!BT_nullfp)
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000354 BT_nullfp.reset(new BuiltinBug("NULL stream pointer",
355 "Stream pointer might be NULL."));
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000356 BugReport *R =new BugReport(*BT_nullfp, BT_nullfp->getDescription(), N);
357 C.EmitReport(R);
Zhongxing Xuc1960952010-06-16 05:38:05 +0000358 }
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000359 return 0;
Zhongxing Xuc1960952010-06-16 05:38:05 +0000360 }
Zhongxing Xuab421302010-06-24 13:09:02 +0000361 return stateNotNull;
Zhongxing Xuc1960952010-06-16 05:38:05 +0000362}
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000363
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000364const ProgramState *StreamChecker::CheckDoubleClose(const CallExpr *CE,
365 const ProgramState *state,
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000366 CheckerContext &C) const {
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000367 SymbolRef Sym = state->getSVal(CE->getArg(0)).getAsSymbol();
Ted Kremenek80460372010-09-03 01:07:04 +0000368 if (!Sym)
369 return state;
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000370
371 const StreamState *SS = state->get<StreamState>(Sym);
Zhongxing Xu1c7370f2010-08-05 23:24:13 +0000372
373 // If the file stream is not tracked, return.
374 if (!SS)
375 return state;
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000376
377 // Check: Double close a File Descriptor could cause undefined behaviour.
378 // Conforming to man-pages
379 if (SS->isClosed()) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000380 ExplodedNode *N = C.generateSink();
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000381 if (N) {
382 if (!BT_doubleclose)
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000383 BT_doubleclose.reset(new BuiltinBug("Double fclose",
Eli Friedmana7e68452010-08-22 01:00:03 +0000384 "Try to close a file Descriptor already"
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000385 " closed. Cause undefined behaviour."));
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000386 BugReport *R = new BugReport(*BT_doubleclose,
Eli Friedmana7e68452010-08-22 01:00:03 +0000387 BT_doubleclose->getDescription(), N);
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000388 C.EmitReport(R);
389 }
390 return NULL;
391 }
392
393 // Close the File Descriptor.
394 return state->set<StreamState>(Sym, StreamState::getClosed(CE));
395}
Zhongxing Xu766c2012010-07-23 14:14:59 +0000396
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000397void StreamChecker::checkDeadSymbols(SymbolReaper &SymReaper,
398 CheckerContext &C) const {
Zhongxing Xu766c2012010-07-23 14:14:59 +0000399 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
Eli Friedmana7e68452010-08-22 01:00:03 +0000400 E = SymReaper.dead_end(); I != E; ++I) {
Zhongxing Xu766c2012010-07-23 14:14:59 +0000401 SymbolRef Sym = *I;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000402 const ProgramState *state = C.getState();
Zhongxing Xu766c2012010-07-23 14:14:59 +0000403 const StreamState *SS = state->get<StreamState>(Sym);
404 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);
415 C.EmitReport(R);
Zhongxing Xu766c2012010-07-23 14:14:59 +0000416 }
417 }
418 }
419}
420
Anna Zaksaf498a22011-10-25 19:56:48 +0000421void StreamChecker::checkEndPath(CheckerContext &Ctx) const {
422 const ProgramState *state = Ctx.getState();
Zhongxing Xu766c2012010-07-23 14:14:59 +0000423 typedef llvm::ImmutableMap<SymbolRef, StreamState> SymMap;
424 SymMap M = state->get<StreamState>();
425
426 for (SymMap::iterator I = M.begin(), E = M.end(); I != E; ++I) {
427 StreamState SS = I->second;
428 if (SS.isOpened()) {
Anna Zaksaf498a22011-10-25 19:56:48 +0000429 ExplodedNode *N = Ctx.generateNode(state);
Zhongxing Xu766c2012010-07-23 14:14:59 +0000430 if (N) {
431 if (!BT_ResourceLeak)
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000432 BT_ResourceLeak.reset(new BuiltinBug("Resource Leak",
433 "Opened File never closed. Potential Resource leak."));
Zhongxing Xu766c2012010-07-23 14:14:59 +0000434 BugReport *R = new BugReport(*BT_ResourceLeak,
Eli Friedmana7e68452010-08-22 01:00:03 +0000435 BT_ResourceLeak->getDescription(), N);
Anna Zaksaf498a22011-10-25 19:56:48 +0000436 Ctx.EmitReport(R);
Zhongxing Xu766c2012010-07-23 14:14:59 +0000437 }
438 }
439 }
440}
441
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000442void StreamChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const {
Zhongxing Xu766c2012010-07-23 14:14:59 +0000443 const Expr *RetE = S->getRetValue();
444 if (!RetE)
445 return;
446
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000447 const ProgramState *state = C.getState();
Zhongxing Xu766c2012010-07-23 14:14:59 +0000448 SymbolRef Sym = state->getSVal(RetE).getAsSymbol();
449
450 if (!Sym)
451 return;
452
453 const StreamState *SS = state->get<StreamState>(Sym);
454 if(!SS)
455 return;
456
457 if (SS->isOpened())
458 state = state->set<StreamState>(Sym, StreamState::getEscaped(S));
459
460 C.addTransition(state);
461}
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000462
463void ento::registerStreamChecker(CheckerManager &mgr) {
464 mgr.registerChecker<StreamChecker>();
465}