blob: a7b2ced2b7847889826868f46acaa0b5b5f005b3 [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
26class StreamChecker : public CheckerVisitor<StreamChecker> {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +000027 IdentifierInfo *II_fopen, *II_fread, *II_fwrite,
28 *II_fseek, *II_ftell, *II_rewind, *II_fgetpos, *II_fsetpos,
29 *II_clearerr, *II_feof, *II_ferror, *II_fileno;
Zhongxing Xuc1960952010-06-16 05:38:05 +000030 BuiltinBug *BT_nullfp;
31
32public:
Zhongxing Xu23d90f92010-06-18 02:47:46 +000033 StreamChecker()
Zhongxing Xuc7de88b2010-06-22 07:50:21 +000034 : II_fopen(0), II_fread(0), II_fwrite(0),
35 II_fseek(0), II_ftell(0), II_rewind(0), II_fgetpos(0), II_fsetpos(0),
36 II_clearerr(0), II_feof(0), II_ferror(0), II_fileno(0),
Zhongxing Xu23d90f92010-06-18 02:47:46 +000037 BT_nullfp(0) {}
Zhongxing Xuc1960952010-06-16 05:38:05 +000038
39 static void *getTag() {
40 static int x;
41 return &x;
42 }
43
44 virtual bool EvalCallExpr(CheckerContext &C, const CallExpr *CE);
45
46private:
Zhongxing Xuc7de88b2010-06-22 07:50:21 +000047 void Fopen(CheckerContext &C, const CallExpr *CE);
48 void Fread(CheckerContext &C, const CallExpr *CE);
49 void Fwrite(CheckerContext &C, const CallExpr *CE);
50 void Fseek(CheckerContext &C, const CallExpr *CE);
51 void Ftell(CheckerContext &C, const CallExpr *CE);
Zhongxing Xu23d90f92010-06-18 02:47:46 +000052 void Rewind(CheckerContext &C, const CallExpr *CE);
Zhongxing Xuc7de88b2010-06-22 07:50:21 +000053 void Fgetpos(CheckerContext &C, const CallExpr *CE);
54 void Fsetpos(CheckerContext &C, const CallExpr *CE);
55 void Clearerr(CheckerContext &C, const CallExpr *CE);
56 void Feof(CheckerContext &C, const CallExpr *CE);
57 void Ferror(CheckerContext &C, const CallExpr *CE);
58 void Fileno(CheckerContext &C, const CallExpr *CE);
59
60 // Return true indicates the stream pointer is NULL.
Zhongxing Xu23d90f92010-06-18 02:47:46 +000061 bool CheckNullStream(SVal SV, const GRState *state, CheckerContext &C);
Zhongxing Xuc1960952010-06-16 05:38:05 +000062};
63
Zhongxing Xu23d90f92010-06-18 02:47:46 +000064} // end anonymous namespace
Zhongxing Xuc1960952010-06-16 05:38:05 +000065
66void clang::RegisterStreamChecker(GRExprEngine &Eng) {
67 Eng.registerCheck(new StreamChecker());
68}
69
70bool StreamChecker::EvalCallExpr(CheckerContext &C, const CallExpr *CE) {
71 const GRState *state = C.getState();
72 const Expr *Callee = CE->getCallee();
73 SVal L = state->getSVal(Callee);
74 const FunctionDecl *FD = L.getAsFunctionDecl();
75 if (!FD)
76 return false;
77
78 ASTContext &Ctx = C.getASTContext();
79 if (!II_fopen)
80 II_fopen = &Ctx.Idents.get("fopen");
Zhongxing Xuc1960952010-06-16 05:38:05 +000081 if (!II_fread)
82 II_fread = &Ctx.Idents.get("fread");
Zhongxing Xuc7de88b2010-06-22 07:50:21 +000083 if (!II_fwrite)
84 II_fwrite = &Ctx.Idents.get("fwrite");
Zhongxing Xu23d90f92010-06-18 02:47:46 +000085 if (!II_fseek)
86 II_fseek = &Ctx.Idents.get("fseek");
Zhongxing Xu23d90f92010-06-18 02:47:46 +000087 if (!II_ftell)
88 II_ftell = &Ctx.Idents.get("ftell");
Zhongxing Xu23d90f92010-06-18 02:47:46 +000089 if (!II_rewind)
90 II_rewind = &Ctx.Idents.get("rewind");
Zhongxing Xuc7de88b2010-06-22 07:50:21 +000091 if (!II_fgetpos)
92 II_fgetpos = &Ctx.Idents.get("fgetpos");
93 if (!II_fsetpos)
94 II_fsetpos = &Ctx.Idents.get("fsetpos");
95 if (!II_clearerr)
96 II_clearerr = &Ctx.Idents.get("clearerr");
97 if (!II_feof)
98 II_feof = &Ctx.Idents.get("feof");
99 if (!II_ferror)
100 II_ferror = &Ctx.Idents.get("ferror");
101 if (!II_fileno)
102 II_fileno = &Ctx.Idents.get("fileno");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000103
Zhongxing Xuc1960952010-06-16 05:38:05 +0000104 if (FD->getIdentifier() == II_fopen) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000105 Fopen(C, CE);
Zhongxing Xuc1960952010-06-16 05:38:05 +0000106 return true;
107 }
Zhongxing Xuc1960952010-06-16 05:38:05 +0000108 if (FD->getIdentifier() == II_fread) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000109 Fread(C, CE);
Zhongxing Xuc1960952010-06-16 05:38:05 +0000110 return true;
111 }
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000112 if (FD->getIdentifier() == II_fwrite) {
113 Fwrite(C, CE);
114 return true;
115 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000116 if (FD->getIdentifier() == II_fseek) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000117 Fseek(C, CE);
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000118 return true;
119 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000120 if (FD->getIdentifier() == II_ftell) {
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000121 Ftell(C, CE);
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000122 return true;
123 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000124 if (FD->getIdentifier() == II_rewind) {
125 Rewind(C, CE);
126 return true;
127 }
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000128 if (FD->getIdentifier() == II_fgetpos) {
129 Fgetpos(C, CE);
130 return true;
131 }
132 if (FD->getIdentifier() == II_fsetpos) {
133 Fsetpos(C, CE);
134 return true;
135 }
136 if (FD->getIdentifier() == II_clearerr) {
137 Clearerr(C, CE);
138 return true;
139 }
140 if (FD->getIdentifier() == II_feof) {
141 Feof(C, CE);
142 return true;
143 }
144 if (FD->getIdentifier() == II_ferror) {
145 Ferror(C, CE);
146 return true;
147 }
148 if (FD->getIdentifier() == II_fileno) {
149 Fileno(C, CE);
150 return true;
151 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000152
Zhongxing Xuc1960952010-06-16 05:38:05 +0000153 return false;
154}
155
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000156void StreamChecker::Fopen(CheckerContext &C, const CallExpr *CE) {
Zhongxing Xuc1960952010-06-16 05:38:05 +0000157 const GRState *state = C.getState();
158 unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
159 ValueManager &ValMgr = C.getValueManager();
Zhongxing Xu70154852010-06-16 05:52:03 +0000160 DefinedSVal RetVal = cast<DefinedSVal>(ValMgr.getConjuredSymbolVal(0, CE,
161 Count));
Zhongxing Xuc1960952010-06-16 05:38:05 +0000162 state = state->BindExpr(CE, RetVal);
163
164 ConstraintManager &CM = C.getConstraintManager();
165 // Bifurcate the state into two: one with a valid FILE* pointer, the other
166 // with a NULL.
167 const GRState *stateNotNull, *stateNull;
Zhongxing Xu70154852010-06-16 05:52:03 +0000168 llvm::tie(stateNotNull, stateNull) = CM.AssumeDual(state, RetVal);
Zhongxing Xuc1960952010-06-16 05:38:05 +0000169
170 C.addTransition(stateNotNull);
171 C.addTransition(stateNull);
172}
173
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000174void StreamChecker::Fread(CheckerContext &C, const CallExpr *CE) {
Zhongxing Xuc1960952010-06-16 05:38:05 +0000175 const GRState *state = C.getState();
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000176 if (CheckNullStream(state->getSVal(CE->getArg(3)), state, C))
177 return;
178}
Zhongxing Xuc1960952010-06-16 05:38:05 +0000179
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000180void StreamChecker::Fwrite(CheckerContext &C, const CallExpr *CE) {
181 const GRState *state = C.getState();
182 if (CheckNullStream(state->getSVal(CE->getArg(3)), state, C))
183 return;
184}
185
186void StreamChecker::Fseek(CheckerContext &C, const CallExpr *CE) {
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000187 const GRState *state = C.getState();
188 if (CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
189 return;
190}
Zhongxing Xuc1960952010-06-16 05:38:05 +0000191
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000192void StreamChecker::Ftell(CheckerContext &C, const CallExpr *CE) {
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000193 const GRState *state = C.getState();
194 if (CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
195 return;
196}
Zhongxing Xuc1960952010-06-16 05:38:05 +0000197
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000198void StreamChecker::Rewind(CheckerContext &C, const CallExpr *CE) {
199 const GRState *state = C.getState();
200 if (CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
201 return;
202}
203
Zhongxing Xuc7de88b2010-06-22 07:50:21 +0000204void StreamChecker::Fgetpos(CheckerContext &C, const CallExpr *CE) {
205 const GRState *state = C.getState();
206 if (CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
207 return;
208}
209
210void StreamChecker::Fsetpos(CheckerContext &C, const CallExpr *CE) {
211 const GRState *state = C.getState();
212 if (CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
213 return;
214}
215
216void StreamChecker::Clearerr(CheckerContext &C, const CallExpr *CE) {
217 const GRState *state = C.getState();
218 if (CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
219 return;
220}
221
222void StreamChecker::Feof(CheckerContext &C, const CallExpr *CE) {
223 const GRState *state = C.getState();
224 if (CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
225 return;
226}
227
228void StreamChecker::Ferror(CheckerContext &C, const CallExpr *CE) {
229 const GRState *state = C.getState();
230 if (CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
231 return;
232}
233
234void StreamChecker::Fileno(CheckerContext &C, const CallExpr *CE) {
235 const GRState *state = C.getState();
236 if (CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
237 return;
238}
239
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000240bool StreamChecker::CheckNullStream(SVal SV, const GRState *state,
241 CheckerContext &C) {
242 const DefinedSVal *DV = dyn_cast<DefinedSVal>(&SV);
243 if (!DV)
244 return false;
245
246 ConstraintManager &CM = C.getConstraintManager();
247 const GRState *stateNotNull, *stateNull;
248 llvm::tie(stateNotNull, stateNull) = CM.AssumeDual(state, *DV);
249
250 if (!stateNotNull && stateNull) {
251 if (ExplodedNode *N = C.GenerateSink(stateNull)) {
252 if (!BT_nullfp)
253 BT_nullfp = new BuiltinBug("NULL stream pointer",
Zhongxing Xub3f40312010-06-16 05:56:39 +0000254 "Stream pointer might be NULL.");
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000255 BugReport *R =new BugReport(*BT_nullfp, BT_nullfp->getDescription(), N);
256 C.EmitReport(R);
Zhongxing Xuc1960952010-06-16 05:38:05 +0000257 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000258 return true;
Zhongxing Xuc1960952010-06-16 05:38:05 +0000259 }
Zhongxing Xu23d90f92010-06-18 02:47:46 +0000260 return false;
Zhongxing Xuc1960952010-06-16 05:38:05 +0000261}