blob: 2f96bbfeac63c20b611d33a9c08ba3081195c03d [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));
Zhongxing Xuc1960952010-06-16 05:38:05 +0000226 state = state->BindExpr(CE, 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();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000254 if (!CheckNullStream(state->getSVal(CE->getArg(3)), state, C))
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000255 return;
256}
Zhongxing Xuc1960952010-06-16 05:38:05 +0000257
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000258void StreamChecker::Fwrite(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000259 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000260 if (!CheckNullStream(state->getSVal(CE->getArg(3)), state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000261 return;
262}
263
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000264void StreamChecker::Fseek(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000265 const ProgramState *state = C.getState();
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000266 if (!(state = CheckNullStream(state->getSVal(CE->getArg(0)), state, C)))
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000267 return;
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000268 // Check the legality of the 'whence' argument of 'fseek'.
269 SVal Whence = state->getSVal(CE->getArg(2));
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000270 const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Whence);
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000271
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000272 if (!CI)
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000273 return;
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000274
275 int64_t x = CI->getValue().getSExtValue();
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000276 if (x >= 0 && x <= 2)
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000277 return;
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000278
Anna Zaks0bd6b112011-10-26 21:06:34 +0000279 if (ExplodedNode *N = C.addTransition(state)) {
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000280 if (!BT_illegalwhence)
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000281 BT_illegalwhence.reset(new BuiltinBug("Illegal whence argument",
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000282 "The whence argument to fseek() should be "
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000283 "SEEK_SET, SEEK_END, or SEEK_CUR."));
Ted Kremenek02b49bb2010-09-07 20:45:26 +0000284 BugReport *R = new BugReport(*BT_illegalwhence,
285 BT_illegalwhence->getDescription(), N);
286 C.EmitReport(R);
287 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000288}
Zhongxing Xuc1960952010-06-16 05:38:05 +0000289
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000290void StreamChecker::Ftell(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000291 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000292 if (!CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000293 return;
294}
Zhongxing Xuc1960952010-06-16 05:38:05 +0000295
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000296void StreamChecker::Rewind(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000297 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000298 if (!CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000299 return;
300}
301
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000302void StreamChecker::Fgetpos(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000303 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000304 if (!CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000305 return;
306}
307
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000308void StreamChecker::Fsetpos(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000309 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000310 if (!CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000311 return;
312}
313
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000314void StreamChecker::Clearerr(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000315 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000316 if (!CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000317 return;
318}
319
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000320void StreamChecker::Feof(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000321 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000322 if (!CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000323 return;
324}
325
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000326void StreamChecker::Ferror(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000327 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000328 if (!CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000329 return;
330}
331
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000332void StreamChecker::Fileno(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000333 const ProgramState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000334 if (!CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000335 return;
336}
337
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000338const ProgramState *StreamChecker::CheckNullStream(SVal SV, const ProgramState *state,
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000339 CheckerContext &C) const {
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000340 const DefinedSVal *DV = dyn_cast<DefinedSVal>(&SV);
341 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 Kremenek18c66fd2011-08-15 22:09:50 +0000345 const ProgramState *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);
354 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 Kremenek18c66fd2011-08-15 22:09:50 +0000361const ProgramState *StreamChecker::CheckDoubleClose(const CallExpr *CE,
362 const ProgramState *state,
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000363 CheckerContext &C) const {
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000364 SymbolRef Sym = state->getSVal(CE->getArg(0)).getAsSymbol();
Ted Kremenek80460372010-09-03 01:07:04 +0000365 if (!Sym)
366 return state;
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000367
368 const StreamState *SS = state->get<StreamState>(Sym);
Zhongxing Xu1c7370f2010-08-05 23:24:13 +0000369
370 // If the file stream is not tracked, return.
371 if (!SS)
372 return state;
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000373
374 // Check: Double close a File Descriptor could cause undefined behaviour.
375 // Conforming to man-pages
376 if (SS->isClosed()) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000377 ExplodedNode *N = C.generateSink();
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000378 if (N) {
379 if (!BT_doubleclose)
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000380 BT_doubleclose.reset(new BuiltinBug("Double fclose",
Eli Friedmana7e68452010-08-22 01:00:03 +0000381 "Try to close a file Descriptor already"
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000382 " closed. Cause undefined behaviour."));
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000383 BugReport *R = new BugReport(*BT_doubleclose,
Eli Friedmana7e68452010-08-22 01:00:03 +0000384 BT_doubleclose->getDescription(), N);
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000385 C.EmitReport(R);
386 }
387 return NULL;
388 }
389
390 // Close the File Descriptor.
391 return state->set<StreamState>(Sym, StreamState::getClosed(CE));
392}
Zhongxing Xu766c2012010-07-23 14:14:59 +0000393
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000394void StreamChecker::checkDeadSymbols(SymbolReaper &SymReaper,
395 CheckerContext &C) const {
Zhongxing Xu766c2012010-07-23 14:14:59 +0000396 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
Eli Friedmana7e68452010-08-22 01:00:03 +0000397 E = SymReaper.dead_end(); I != E; ++I) {
Zhongxing Xu766c2012010-07-23 14:14:59 +0000398 SymbolRef Sym = *I;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000399 const ProgramState *state = C.getState();
Zhongxing Xu766c2012010-07-23 14:14:59 +0000400 const StreamState *SS = state->get<StreamState>(Sym);
401 if (!SS)
402 return;
403
404 if (SS->isOpened()) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000405 ExplodedNode *N = C.generateSink();
Zhongxing Xu766c2012010-07-23 14:14:59 +0000406 if (N) {
Eli Friedmana7e68452010-08-22 01:00:03 +0000407 if (!BT_ResourceLeak)
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000408 BT_ResourceLeak.reset(new BuiltinBug("Resource Leak",
409 "Opened File never closed. Potential Resource leak."));
Eli Friedmana7e68452010-08-22 01:00:03 +0000410 BugReport *R = new BugReport(*BT_ResourceLeak,
411 BT_ResourceLeak->getDescription(), N);
412 C.EmitReport(R);
Zhongxing Xu766c2012010-07-23 14:14:59 +0000413 }
414 }
415 }
416}
417
Anna Zaksaf498a22011-10-25 19:56:48 +0000418void StreamChecker::checkEndPath(CheckerContext &Ctx) const {
419 const ProgramState *state = Ctx.getState();
Zhongxing Xu766c2012010-07-23 14:14:59 +0000420 typedef llvm::ImmutableMap<SymbolRef, StreamState> SymMap;
421 SymMap M = state->get<StreamState>();
422
423 for (SymMap::iterator I = M.begin(), E = M.end(); I != E; ++I) {
424 StreamState SS = I->second;
425 if (SS.isOpened()) {
Anna Zaks0bd6b112011-10-26 21:06:34 +0000426 ExplodedNode *N = Ctx.addTransition(state);
Zhongxing Xu766c2012010-07-23 14:14:59 +0000427 if (N) {
428 if (!BT_ResourceLeak)
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000429 BT_ResourceLeak.reset(new BuiltinBug("Resource Leak",
430 "Opened File never closed. Potential Resource leak."));
Zhongxing Xu766c2012010-07-23 14:14:59 +0000431 BugReport *R = new BugReport(*BT_ResourceLeak,
Eli Friedmana7e68452010-08-22 01:00:03 +0000432 BT_ResourceLeak->getDescription(), N);
Anna Zaksaf498a22011-10-25 19:56:48 +0000433 Ctx.EmitReport(R);
Zhongxing Xu766c2012010-07-23 14:14:59 +0000434 }
435 }
436 }
437}
438
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000439void StreamChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const {
Zhongxing Xu766c2012010-07-23 14:14:59 +0000440 const Expr *RetE = S->getRetValue();
441 if (!RetE)
442 return;
443
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000444 const ProgramState *state = C.getState();
Zhongxing Xu766c2012010-07-23 14:14:59 +0000445 SymbolRef Sym = state->getSVal(RetE).getAsSymbol();
446
447 if (!Sym)
448 return;
449
450 const StreamState *SS = state->get<StreamState>(Sym);
451 if(!SS)
452 return;
453
454 if (SS->isOpened())
455 state = state->set<StreamState>(Sym, StreamState::getEscaped(S));
456
Anna Zaks0bd6b112011-10-26 21:06:34 +0000457 C.addTransition(state);
Zhongxing Xu766c2012010-07-23 14:14:59 +0000458}
Argyrios Kyrtzidis699bbf92011-02-24 01:05:33 +0000459
460void ento::registerStreamChecker(CheckerManager &mgr) {
461 mgr.registerChecker<StreamChecker>();
462}