blob: cfb94344f616f98d07f02a8eff896b4fe3848d4e [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
14#include "GRExprEngineExperimentalChecks.h"
15#include "clang/Checker/BugReporter/BugType.h"
16#include "clang/Checker/PathSensitive/CheckerVisitor.h"
17#include "clang/Checker/PathSensitive/GRState.h"
18#include "clang/Checker/PathSensitive/GRStateTrait.h"
19#include "clang/Checker/PathSensitive/SymbolManager.h"
20#include "llvm/ADT/ImmutableMap.h"
21
22using namespace clang;
23
24namespace {
25
Zhongxing Xu9843ba92010-07-19 01:52:29 +000026struct StreamState {
27 enum Kind { Opened, Closed, OpenFailed } K;
28 const Stmt *S;
29
30 StreamState(Kind k, const Stmt *s) : K(k), S(s) {}
31
32 bool isOpened() const { return K == Opened; }
33 bool isClosed() const { return K == Closed; }
34 bool isOpenFailed() const { return K == OpenFailed; }
35
36 bool operator==(const StreamState &X) const {
37 return K == X.K && S == X.S;
38 }
39
40 static StreamState getOpened(const Stmt *s) { return StreamState(Opened, s); }
41 static StreamState getClosed(const Stmt *s) { return StreamState(Closed, s); }
Zhongxing Xu47dc37f2010-07-22 14:01:01 +000042 static StreamState getOpenFailed(const Stmt *s) {
43 return StreamState(OpenFailed, s);
44 }
Zhongxing Xu9843ba92010-07-19 01:52:29 +000045
46 void Profile(llvm::FoldingSetNodeID &ID) const {
47 ID.AddInteger(K);
48 ID.AddPointer(S);
49 }
50};
51
Zhongxing Xuc1960952010-06-16 05:38:05 +000052class StreamChecker : public CheckerVisitor<StreamChecker> {
Zhongxing Xu47dc37f2010-07-22 14:01:01 +000053 IdentifierInfo *II_fopen, *II_tmpfile, *II_fclose, *II_fread, *II_fwrite,
Zhongxing Xuc7de88b2010-06-22 07:50:21 +000054 *II_fseek, *II_ftell, *II_rewind, *II_fgetpos, *II_fsetpos,
55 *II_clearerr, *II_feof, *II_ferror, *II_fileno;
Zhongxing Xu9843ba92010-07-19 01:52:29 +000056 BuiltinBug *BT_nullfp, *BT_illegalwhence, *BT_doubleclose;
Zhongxing Xuc1960952010-06-16 05:38:05 +000057
58public:
Zhongxing Xu23d90f92010-06-18 02:47:46 +000059 StreamChecker()
Zhongxing Xu47dc37f2010-07-22 14:01:01 +000060 : II_fopen(0), II_tmpfile(0) ,II_fclose(0), II_fread(0), II_fwrite(0),
Zhongxing Xuc7de88b2010-06-22 07:50:21 +000061 II_fseek(0), II_ftell(0), II_rewind(0), II_fgetpos(0), II_fsetpos(0),
62 II_clearerr(0), II_feof(0), II_ferror(0), II_fileno(0),
Zhongxing Xu9843ba92010-07-19 01:52:29 +000063 BT_nullfp(0), BT_illegalwhence(0), BT_doubleclose(0) {}
Zhongxing Xuc1960952010-06-16 05:38:05 +000064
65 static void *getTag() {
66 static int x;
67 return &x;
68 }
69
70 virtual bool EvalCallExpr(CheckerContext &C, const CallExpr *CE);
71
72private:
Zhongxing Xuc7de88b2010-06-22 07:50:21 +000073 void Fopen(CheckerContext &C, const CallExpr *CE);
Zhongxing Xu47dc37f2010-07-22 14:01:01 +000074 void Tmpfile(CheckerContext &C, const CallExpr *CE);
Zhongxing Xu9843ba92010-07-19 01:52:29 +000075 void Fclose(CheckerContext &C, const CallExpr *CE);
Zhongxing Xuc7de88b2010-06-22 07:50:21 +000076 void Fread(CheckerContext &C, const CallExpr *CE);
77 void Fwrite(CheckerContext &C, const CallExpr *CE);
78 void Fseek(CheckerContext &C, const CallExpr *CE);
79 void Ftell(CheckerContext &C, const CallExpr *CE);
Zhongxing Xu23d90f92010-06-18 02:47:46 +000080 void Rewind(CheckerContext &C, const CallExpr *CE);
Zhongxing Xuc7de88b2010-06-22 07:50:21 +000081 void Fgetpos(CheckerContext &C, const CallExpr *CE);
82 void Fsetpos(CheckerContext &C, const CallExpr *CE);
83 void Clearerr(CheckerContext &C, const CallExpr *CE);
84 void Feof(CheckerContext &C, const CallExpr *CE);
85 void Ferror(CheckerContext &C, const CallExpr *CE);
86 void Fileno(CheckerContext &C, const CallExpr *CE);
Zhongxing Xu47dc37f2010-07-22 14:01:01 +000087
88 void OpenFileAux(CheckerContext &C, const CallExpr *CE);
Zhongxing Xuc7de88b2010-06-22 07:50:21 +000089
Zhongxing Xu12d213d2010-06-24 12:52:28 +000090 const GRState *CheckNullStream(SVal SV, const GRState *state,
91 CheckerContext &C);
Zhongxing Xu9843ba92010-07-19 01:52:29 +000092 const GRState *CheckDoubleClose(const CallExpr *CE, const GRState *state,
93 CheckerContext &C);
Zhongxing Xuc1960952010-06-16 05:38:05 +000094};
95
Zhongxing Xu23d90f92010-06-18 02:47:46 +000096} // end anonymous namespace
Zhongxing Xuc1960952010-06-16 05:38:05 +000097
Zhongxing Xu9843ba92010-07-19 01:52:29 +000098namespace clang {
99 template <>
100 struct GRStateTrait<StreamState>
101 : public GRStatePartialTrait<llvm::ImmutableMap<SymbolRef, StreamState> > {
102 static void *GDMIndex() { return StreamChecker::getTag(); }
103 };
104}
105
Zhongxing Xuc1960952010-06-16 05:38:05 +0000106void clang::RegisterStreamChecker(GRExprEngine &Eng) {
107 Eng.registerCheck(new StreamChecker());
108}
109
110bool StreamChecker::EvalCallExpr(CheckerContext &C, const CallExpr *CE) {
111 const GRState *state = C.getState();
112 const Expr *Callee = CE->getCallee();
113 SVal L = state->getSVal(Callee);
114 const FunctionDecl *FD = L.getAsFunctionDecl();
115 if (!FD)
116 return false;
117
118 ASTContext &Ctx = C.getASTContext();
119 if (!II_fopen)
120 II_fopen = &Ctx.Idents.get("fopen");
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000121 if (!II_tmpfile)
122 II_tmpfile = &Ctx.Idents.get("tmpfile");
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000123 if (!II_fclose)
124 II_fclose = &Ctx.Idents.get("fclose");
Zhongxing Xuc1960952010-06-16 05:38:05 +0000125 if (!II_fread)
126 II_fread = &Ctx.Idents.get("fread");
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000127 if (!II_fwrite)
128 II_fwrite = &Ctx.Idents.get("fwrite");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000129 if (!II_fseek)
130 II_fseek = &Ctx.Idents.get("fseek");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000131 if (!II_ftell)
132 II_ftell = &Ctx.Idents.get("ftell");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000133 if (!II_rewind)
134 II_rewind = &Ctx.Idents.get("rewind");
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000135 if (!II_fgetpos)
136 II_fgetpos = &Ctx.Idents.get("fgetpos");
137 if (!II_fsetpos)
138 II_fsetpos = &Ctx.Idents.get("fsetpos");
139 if (!II_clearerr)
140 II_clearerr = &Ctx.Idents.get("clearerr");
141 if (!II_feof)
142 II_feof = &Ctx.Idents.get("feof");
143 if (!II_ferror)
144 II_ferror = &Ctx.Idents.get("ferror");
145 if (!II_fileno)
146 II_fileno = &Ctx.Idents.get("fileno");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000147
Zhongxing Xuc1960952010-06-16 05:38:05 +0000148 if (FD->getIdentifier() == II_fopen) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000149 Fopen(C, CE);
Zhongxing Xuc1960952010-06-16 05:38:05 +0000150 return true;
151 }
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000152 if (FD->getIdentifier() == II_tmpfile) {
153 Tmpfile(C, CE);
154 return true;
155 }
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000156 if (FD->getIdentifier() == II_fclose) {
157 Fclose(C, CE);
158 return true;
159 }
Zhongxing Xuc1960952010-06-16 05:38:05 +0000160 if (FD->getIdentifier() == II_fread) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000161 Fread(C, CE);
Zhongxing Xuc1960952010-06-16 05:38:05 +0000162 return true;
163 }
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000164 if (FD->getIdentifier() == II_fwrite) {
165 Fwrite(C, CE);
166 return true;
167 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000168 if (FD->getIdentifier() == II_fseek) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000169 Fseek(C, CE);
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000170 return true;
171 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000172 if (FD->getIdentifier() == II_ftell) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000173 Ftell(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_rewind) {
177 Rewind(C, CE);
178 return true;
179 }
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000180 if (FD->getIdentifier() == II_fgetpos) {
181 Fgetpos(C, CE);
182 return true;
183 }
184 if (FD->getIdentifier() == II_fsetpos) {
185 Fsetpos(C, CE);
186 return true;
187 }
188 if (FD->getIdentifier() == II_clearerr) {
189 Clearerr(C, CE);
190 return true;
191 }
192 if (FD->getIdentifier() == II_feof) {
193 Feof(C, CE);
194 return true;
195 }
196 if (FD->getIdentifier() == II_ferror) {
197 Ferror(C, CE);
198 return true;
199 }
200 if (FD->getIdentifier() == II_fileno) {
201 Fileno(C, CE);
202 return true;
203 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000204
Zhongxing Xuc1960952010-06-16 05:38:05 +0000205 return false;
206}
207
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000208void StreamChecker::Fopen(CheckerContext &C, const CallExpr *CE) {
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000209 OpenFileAux(C, CE);
210}
211
212void StreamChecker::Tmpfile(CheckerContext &C, const CallExpr *CE) {
213 OpenFileAux(C, CE);
214}
215
216void StreamChecker::OpenFileAux(CheckerContext &C, const CallExpr *CE) {
Zhongxing Xuc1960952010-06-16 05:38:05 +0000217 const GRState *state = C.getState();
218 unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
219 ValueManager &ValMgr = C.getValueManager();
Zhongxing Xu70154852010-06-16 05:52:03 +0000220 DefinedSVal RetVal = cast<DefinedSVal>(ValMgr.getConjuredSymbolVal(0, CE,
221 Count));
Zhongxing Xuc1960952010-06-16 05:38:05 +0000222 state = state->BindExpr(CE, RetVal);
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000223
Zhongxing Xuc1960952010-06-16 05:38:05 +0000224 ConstraintManager &CM = C.getConstraintManager();
225 // Bifurcate the state into two: one with a valid FILE* pointer, the other
226 // with a NULL.
227 const GRState *stateNotNull, *stateNull;
Zhongxing Xu70154852010-06-16 05:52:03 +0000228 llvm::tie(stateNotNull, stateNull) = CM.AssumeDual(state, RetVal);
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000229
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000230 SymbolRef Sym = RetVal.getAsSymbol();
231 assert(Sym);
232
233 // if RetVal is not NULL, set the symbol's state to Opened.
Zhongxing Xu47dc37f2010-07-22 14:01:01 +0000234 stateNotNull = stateNotNull->set<StreamState>(Sym,StreamState::getOpened(CE));
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000235 stateNull = stateNull->set<StreamState>(Sym, StreamState::getOpenFailed(CE));
236
Zhongxing Xuc1960952010-06-16 05:38:05 +0000237 C.addTransition(stateNotNull);
238 C.addTransition(stateNull);
239}
240
Zhongxing Xu9843ba92010-07-19 01:52:29 +0000241void StreamChecker::Fclose(CheckerContext &C, const CallExpr *CE) {
242 const GRState *state = CheckDoubleClose(CE, C.getState(), C);
243 if (state)
244 C.addTransition(state);
245}
246
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000247void StreamChecker::Fread(CheckerContext &C, const CallExpr *CE) {
Zhongxing Xuc1960952010-06-16 05:38:05 +0000248 const GRState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000249 if (!CheckNullStream(state->getSVal(CE->getArg(3)), state, C))
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000250 return;
251}
Zhongxing Xuc1960952010-06-16 05:38:05 +0000252
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000253void StreamChecker::Fwrite(CheckerContext &C, const CallExpr *CE) {
254 const GRState *state = C.getState();
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000255 if (!CheckNullStream(state->getSVal(CE->getArg(3)), state, C))
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000256 return;
257}
258
259void StreamChecker::Fseek(CheckerContext &C, const CallExpr *CE) {
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000260 const GRState *state = C.getState();
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000261 if (!(state = CheckNullStream(state->getSVal(CE->getArg(0)), state, C)))
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000262 return;
Zhongxing Xu0c2e8c82010-06-24 13:36:41 +0000263 // Check the legality of the 'whence' argument of 'fseek'.
264 SVal Whence = state->getSVal(CE->getArg(2));
265 bool WhenceIsLegal = true;
266 const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Whence);
267 if (!CI)
268 WhenceIsLegal = false;
269
270 int64_t x = CI->getValue().getSExtValue();
271 if (!(x == 0 || x == 1 || x == 2))
272 WhenceIsLegal = false;
273
274 if (!WhenceIsLegal) {
275 if (ExplodedNode *N = C.GenerateSink(state)) {
276 if (!BT_illegalwhence)
277 BT_illegalwhence = new BuiltinBug("Illegal whence argument",
278 "The whence argument to fseek() should be "
279 "SEEK_SET, SEEK_END, or SEEK_CUR.");
280 BugReport *R = new BugReport(*BT_illegalwhence,
281 BT_illegalwhence->getDescription(), N);
282 C.EmitReport(R);
283 }
284 return;
285 }
286
287 C.addTransition(state);
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000288}
Zhongxing Xuc1960952010-06-16 05:38:05 +0000289
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000290void StreamChecker::Ftell(CheckerContext &C, const CallExpr *CE) {
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000291 const GRState *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
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000296void StreamChecker::Rewind(CheckerContext &C, const CallExpr *CE) {
297 const GRState *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
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000302void StreamChecker::Fgetpos(CheckerContext &C, const CallExpr *CE) {
303 const GRState *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
308void StreamChecker::Fsetpos(CheckerContext &C, const CallExpr *CE) {
309 const GRState *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
314void StreamChecker::Clearerr(CheckerContext &C, const CallExpr *CE) {
315 const GRState *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
320void StreamChecker::Feof(CheckerContext &C, const CallExpr *CE) {
321 const GRState *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
326void StreamChecker::Ferror(CheckerContext &C, const CallExpr *CE) {
327 const GRState *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
332void StreamChecker::Fileno(CheckerContext &C, const CallExpr *CE) {
333 const GRState *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
Zhongxing Xu12d213d2010-06-24 12:52:28 +0000338const GRState *StreamChecker::CheckNullStream(SVal SV, const GRState *state,
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000339 CheckerContext &C) {
340 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();
345 const GRState *stateNotNull, *stateNull;
346 llvm::tie(stateNotNull, stateNull) = CM.AssumeDual(state, *DV);
347
348 if (!stateNotNull && stateNull) {
349 if (ExplodedNode *N = C.GenerateSink(stateNull)) {
350 if (!BT_nullfp)
351 BT_nullfp = new BuiltinBug("NULL stream pointer",
Zhongxing Xub3f40312010-06-16 05:56:39 +0000352 "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
361const GRState *StreamChecker::CheckDoubleClose(const CallExpr *CE,
362 const GRState *state,
363 CheckerContext &C) {
364 SymbolRef Sym = state->getSVal(CE->getArg(0)).getAsSymbol();
365 assert(Sym);
366
367 const StreamState *SS = state->get<StreamState>(Sym);
368 assert(SS);
369
370 // Check: Double close a File Descriptor could cause undefined behaviour.
371 // Conforming to man-pages
372 if (SS->isClosed()) {
373 ExplodedNode *N = C.GenerateSink();
374 if (N) {
375 if (!BT_doubleclose)
376 BT_doubleclose = new BuiltinBug("Double fclose",
377 "Try to close a file Descriptor already"
378 " closed. Cause undefined behaviour.");
379 BugReport *R = new BugReport(*BT_doubleclose,
380 BT_doubleclose->getDescription(), N);
381 C.EmitReport(R);
382 }
383 return NULL;
384 }
385
386 // Close the File Descriptor.
387 return state->set<StreamState>(Sym, StreamState::getClosed(CE));
388}