blob: 758e038865fd88e63a182e36f1a3b097e31a2f30 [file] [log] [blame]
Ted Kremenek77349cb2008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekd27f8162008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek77349cb2008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenekd27f8162008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek77349cb2008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000017#include "llvm/Support/Streams.h"
Ted Kremenekb387a3f2008-02-14 22:16:04 +000018
Ted Kremenek0f5f0592008-02-27 06:07:00 +000019#ifndef NDEBUG
20#include "llvm/Support/GraphWriter.h"
21#include <sstream>
22#endif
23
Ted Kremenekb387a3f2008-02-14 22:16:04 +000024using namespace clang;
25using llvm::dyn_cast;
26using llvm::cast;
27using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000028
Ted Kremenek07932632008-02-27 06:47:26 +000029GRExprEngine::StateTy GRExprEngine::getInitialState() {
30
31 // The LiveVariables information already has a compilation of all VarDecls
32 // used in the function. Iterate through this set, and "symbolicate"
33 // any VarDecl whose value originally comes from outside the function.
34
35 typedef LiveVariables::AnalysisDataTy LVDataTy;
36 LVDataTy& D = Liveness.getAnalysisData();
37
38 ValueStateImpl StateImpl = *StateMgr.getInitialState().getImpl();
39
40 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
41
42 VarDecl* VD = cast<VarDecl>(const_cast<ScopedDecl*>(I->first));
43
44 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) {
45 RVal X = RVal::GetSymbolValue(SymMgr, VD);
46 StateMgr.BindVar(StateImpl, VD, X);
47 }
48 }
49
50 return StateMgr.getPersistentState(StateImpl);
51}
52
Ted Kremenek4d4dd852008-02-13 17:41:41 +000053GRExprEngine::StateTy
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000054GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +000055
Ted Kremeneke070a1d2008-02-04 21:59:01 +000056 if (!StateCleaned) {
57 St = RemoveDeadBindings(CurrentStmt, St);
58 StateCleaned = true;
59 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +000060
Ted Kremeneke070a1d2008-02-04 21:59:01 +000061 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +000062
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000063 if (Ex == CurrentStmt) {
64 isBlkExpr = getCFG().isBlkExpr(Ex);
Ted Kremeneke070a1d2008-02-04 21:59:01 +000065
66 if (!isBlkExpr)
67 return St;
68 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +000069
Ted Kremenek5a7b3822008-02-26 23:37:01 +000070 return StateMgr.SetRVal(St, Ex, V, isBlkExpr, false);
Ted Kremeneke070a1d2008-02-04 21:59:01 +000071}
72
Ted Kremenek4d4dd852008-02-13 17:41:41 +000073const GRExprEngine::StateTy::BufferTy&
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000074GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal::BufferTy& RB,
Ted Kremenekcba2e432008-02-05 19:35:18 +000075 StateTy::BufferTy& RetBuf) {
76
77 assert (RetBuf.empty());
78
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000079 for (RVal::BufferTy::const_iterator I = RB.begin(), E = RB.end(); I!=E; ++I)
80 RetBuf.push_back(SetRVal(St, Ex, *I));
Ted Kremenekcba2e432008-02-05 19:35:18 +000081
82 return RetBuf;
83}
84
Ted Kremenek4d4dd852008-02-13 17:41:41 +000085GRExprEngine::StateTy
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000086GRExprEngine::SetRVal(StateTy St, const LVal& LV, const RVal& RV) {
Ted Kremeneke070a1d2008-02-04 21:59:01 +000087
88 if (!StateCleaned) {
89 St = RemoveDeadBindings(CurrentStmt, St);
90 StateCleaned = true;
91 }
92
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000093 return StateMgr.SetRVal(St, LV, RV);
Ted Kremeneke070a1d2008-02-04 21:59:01 +000094}
95
Ted Kremenek05a23782008-02-26 19:05:15 +000096GRExprEngine::StateTy
97GRExprEngine::MarkBranch(StateTy St, Stmt* Terminator, bool branchTaken) {
98
99 switch (Terminator->getStmtClass()) {
100 default:
101 return St;
102
103 case Stmt::BinaryOperatorClass: { // '&&' and '||'
104
105 BinaryOperator* B = cast<BinaryOperator>(Terminator);
106 BinaryOperator::Opcode Op = B->getOpcode();
107
108 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
109
110 // For &&, if we take the true branch, then the value of the whole
111 // expression is that of the RHS expression.
112 //
113 // For ||, if we take the false branch, then the value of the whole
114 // expression is that of the RHS expression.
115
116 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
117 (Op == BinaryOperator::LOr && !branchTaken)
118 ? B->getRHS() : B->getLHS();
119
120 return SetBlkExprRVal(St, B, UninitializedVal(Ex));
121 }
122
123 case Stmt::ConditionalOperatorClass: { // ?:
124
125 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
126
127 // For ?, if branchTaken == true then the value is either the LHS or
128 // the condition itself. (GNU extension).
129
130 Expr* Ex;
131
132 if (branchTaken)
133 Ex = C->getLHS() ? C->getLHS() : C->getCond();
134 else
135 Ex = C->getRHS();
136
137 return SetBlkExprRVal(St, C, UninitializedVal(Ex));
138 }
139
140 case Stmt::ChooseExprClass: { // ?:
141
142 ChooseExpr* C = cast<ChooseExpr>(Terminator);
143
144 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
145 return SetBlkExprRVal(St, C, UninitializedVal(Ex));
146 }
147 }
148}
149
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000150void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000151 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000152
Ted Kremeneke7d22112008-02-11 19:21:59 +0000153 // Remove old bindings for subexpressions.
154 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000155
Ted Kremenekb2331832008-02-15 22:29:00 +0000156 // Check for NULL conditions; e.g. "for(;;)"
157 if (!Condition) {
158 builder.markInfeasible(false);
159
160 // Get the current block counter.
161 GRBlockCounter BC = builder.getBlockCounter();
162 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
163 unsigned NumVisited = BC.getNumVisited(BlockID);
164
165 if (NumVisited < 1) builder.generateNode(PrevState, true);
166 else builder.markInfeasible(true);
167
168 return;
169 }
170
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000171 RVal V = GetRVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000172
173 switch (V.getBaseKind()) {
174 default:
175 break;
176
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000177 case RVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000178 builder.generateNode(MarkBranch(PrevState, Term, true), true);
179 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000180 return;
181
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000182 case RVal::UninitializedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000183 NodeTy* N = builder.generateNode(PrevState, true);
184
185 if (N) {
186 N->markAsSink();
187 UninitBranches.insert(N);
188 }
189
190 builder.markInfeasible(false);
191 return;
192 }
193 }
Ted Kremenekb2331832008-02-15 22:29:00 +0000194
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000195 // Get the current block counter.
196 GRBlockCounter BC = builder.getBlockCounter();
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000197 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
198 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekf233d482008-02-05 00:26:40 +0000199
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000200 if (isa<nonlval::ConcreteInt>(V) ||
201 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
202
203 // Process the true branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000204
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000205 bool isFeasible = true;
206
207 StateTy St = Assume(PrevState, V, true, isFeasible);
208
209 if (isFeasible)
Ted Kremenek05a23782008-02-26 19:05:15 +0000210 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000211 else
212 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000213 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000214 else
215 builder.markInfeasible(true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000216
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000217 BlockID = builder.getTargetBlock(false)->getBlockID();
218 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000219
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000220 if (isa<nonlval::ConcreteInt>(V) ||
221 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
222
223 // Process the false branch.
224
225 bool isFeasible = false;
226
227 StateTy St = Assume(PrevState, V, false, isFeasible);
228
229 if (isFeasible)
Ted Kremenek05a23782008-02-26 19:05:15 +0000230 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000231 else
232 builder.markInfeasible(false);
233 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000234 else
235 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000236}
237
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000238/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000239/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000240void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000241
242 StateTy St = builder.getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000243 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000244
245 // Three possibilities:
246 //
247 // (1) We know the computed label.
248 // (2) The label is NULL (or some other constant), or Uninitialized.
249 // (3) We have no clue about the label. Dispatch to all targets.
250 //
251
252 typedef IndirectGotoNodeBuilder::iterator iterator;
253
254 if (isa<lval::GotoLabel>(V)) {
255 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
256
257 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000258 if (I.getLabel() == L) {
259 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000260 return;
261 }
262 }
263
264 assert (false && "No block with label.");
265 return;
266 }
267
268 if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) {
269 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000270 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek754607e2008-02-13 00:24:44 +0000271 UninitBranches.insert(N);
272 return;
273 }
274
275 // This is really a catch-all. We don't support symbolics yet.
276
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000277 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000278
279 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000280 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000281}
Ted Kremenekf233d482008-02-05 00:26:40 +0000282
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000283/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
284/// nodes by processing the 'effects' of a switch statement.
285void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
286
287 typedef SwitchNodeBuilder::iterator iterator;
288
289 StateTy St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000290 Expr* CondE = builder.getCondition();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000291 RVal CondV = GetRVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000292
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000293 if (CondV.isUninit()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000294 NodeTy* N = builder.generateDefaultCaseNode(St, true);
295 UninitBranches.insert(N);
296 return;
297 }
298
299 StateTy DefaultSt = St;
300
301 // While most of this can be assumed (such as the signedness), having it
302 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000303
304 unsigned bits = getContext().getTypeSize(CondE->getType(),
305 CondE->getExprLoc());
306
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000307 APSInt V1(bits, false);
308 APSInt V2 = V1;
309
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000310 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000311
312 CaseStmt* Case = cast<CaseStmt>(I.getCase());
313
314 // Evaluate the case.
315 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
316 assert (false && "Case condition must evaluate to an integer constant.");
317 return;
318 }
319
320 // Get the RHS of the case, if it exists.
321
322 if (Expr* E = Case->getRHS()) {
323 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
324 assert (false &&
325 "Case condition (RHS) must evaluate to an integer constant.");
326 return ;
327 }
328
329 assert (V1 <= V2);
330 }
331 else V2 = V1;
332
333 // FIXME: Eventually we should replace the logic below with a range
334 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000335 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000336
337 do {
338 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
339
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000340 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000341
342 // Now "assume" that the case matches.
Ted Kremenek692416c2008-02-18 22:57:02 +0000343 bool isFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000344
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000345 StateTy StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000346
347 if (isFeasible) {
348 builder.generateCaseStmtNode(I, StNew);
349
350 // If CondV evaluates to a constant, then we know that this
351 // is the *only* case that we can take, so stop evaluating the
352 // others.
353 if (isa<nonlval::ConcreteInt>(CondV))
354 return;
355 }
356
357 // Now "assume" that the case doesn't match. Add this state
358 // to the default state (if it is feasible).
359
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000360 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000361
362 if (isFeasible)
363 DefaultSt = StNew;
364
365 // Concretize the next value in the range.
366 ++V1;
367
368 } while (V1 < V2);
369 }
370
371 // If we reach here, than we know that the default branch is
372 // possible.
373 builder.generateDefaultCaseNode(DefaultSt);
374}
375
376
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000377void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000378 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000379
Ted Kremenek05a23782008-02-26 19:05:15 +0000380 assert (B->getOpcode() == BinaryOperator::LAnd ||
381 B->getOpcode() == BinaryOperator::LOr);
382
383 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
384
385 StateTy St = Pred->getState();
386 RVal X = GetBlkExprRVal(St, B);
387
388 assert (X.isUninit());
389
390 Expr* Ex = (Expr*) cast<UninitializedVal>(X).getData();
391
392 assert (Ex);
393
394 if (Ex == B->getRHS()) {
395
396 X = GetBlkExprRVal(St, Ex);
397
Ted Kremenek58b33212008-02-26 19:40:44 +0000398 // Handle uninitialized values.
399
400 if (X.isUninit()) {
401 Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
402 return;
403 }
404
Ted Kremenek05a23782008-02-26 19:05:15 +0000405 // We took the RHS. Because the value of the '&&' or '||' expression must
406 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
407 // or 1. Alternatively, we could take a lazy approach, and calculate this
408 // value later when necessary. We don't have the machinery in place for
409 // this right now, and since most logical expressions are used for branches,
410 // the payoff is not likely to be large. Instead, we do eager evaluation.
411
412 bool isFeasible = false;
413 StateTy NewState = Assume(St, X, true, isFeasible);
414
415 if (isFeasible)
416 Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
417
418 isFeasible = false;
419 NewState = Assume(St, X, false, isFeasible);
420
421 if (isFeasible)
422 Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000423 }
424 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000425 // We took the LHS expression. Depending on whether we are '&&' or
426 // '||' we know what the value of the expression is via properties of
427 // the short-circuiting.
428
429 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
430 Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000431 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000432}
Ted Kremenek05a23782008-02-26 19:05:15 +0000433
Ted Kremenekf233d482008-02-05 00:26:40 +0000434
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000435void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000436
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000437 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000438 StmtEntryNode = builder.getLastNode();
439 CurrentStmt = S;
440 NodeSet Dst;
441 StateCleaned = false;
442
443 Visit(S, StmtEntryNode, Dst);
444
445 // If no nodes were generated, generate a new node that has all the
446 // dead mappings removed.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000447
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000448 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
449 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
450 builder.generateNode(S, St, StmtEntryNode);
451 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000452
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000453 // For safety, NULL out these variables.
454
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000455 CurrentStmt = NULL;
456 StmtEntryNode = NULL;
457 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000458}
459
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000460GRExprEngine::NodeTy*
461GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000462
463 // If the state hasn't changed, don't generate a new node.
Ted Kremenek7e593362008-02-07 15:20:13 +0000464 if (St == Pred->getState())
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000465 return NULL;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000466
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000467 NodeTy* N = Builder->generateNode(S, St, Pred);
468 Dst.Add(N);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000469
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000470 return N;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000471}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000472
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000473void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekcba2e432008-02-05 19:35:18 +0000474 const StateTy::BufferTy& SB) {
475
476 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
477 Nodify(Dst, S, Pred, *I);
478}
479
Ted Kremenek44842c22008-02-13 18:06:44 +0000480void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000481
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000482 if (D != CurrentStmt) {
483 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
484 return;
485 }
486
487 // If we are here, we are loading the value of the decl and binding
488 // it to the block-level expression.
489
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000490 StateTy St = Pred->getState();
491 Nodify(Dst, D, Pred, SetRVal(St, D, GetRVal(St, D)));
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000492}
493
Ted Kremenekde434242008-02-19 01:44:53 +0000494void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000495 CallExpr::arg_iterator AI,
496 CallExpr::arg_iterator AE,
Ted Kremenekde434242008-02-19 01:44:53 +0000497 NodeSet& Dst) {
498
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000499 // Process the arguments.
500
501 if (AI != AE) {
Ted Kremenekde434242008-02-19 01:44:53 +0000502
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000503 NodeSet DstTmp;
Ted Kremeneka6fbe802008-02-27 00:44:11 +0000504
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000505 Visit(*AI, Pred, DstTmp);
Ted Kremeneka6fbe802008-02-27 00:44:11 +0000506 if (DstTmp.empty()) DstTmp.Add(Pred);
507
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000508 ++AI;
509
510 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
511 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekde434242008-02-19 01:44:53 +0000512
513 return;
514 }
515
516 // If we reach here we have processed all of the arguments. Evaluate
517 // the callee expression.
Ted Kremenek994a09b2008-02-25 21:16:03 +0000518 NodeSet DstTmp;
519 Expr* Callee = CE->getCallee()->IgnoreParenCasts();
520
521 VisitLVal(Callee, Pred, DstTmp);
Ted Kremenek3ef15122008-02-27 00:46:25 +0000522 if (DstTmp.empty()) DstTmp.Add(Pred);
Ted Kremenekde434242008-02-19 01:44:53 +0000523
524 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000525 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
526
Ted Kremenekde434242008-02-19 01:44:53 +0000527 StateTy St = (*DI)->getState();
Ted Kremenek994a09b2008-02-25 21:16:03 +0000528 RVal L = GetLVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +0000529
530 // Check for uninitialized control-flow.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000531
532 if (L.isUninit()) {
533
Ted Kremenekde434242008-02-19 01:44:53 +0000534 NodeTy* N = Builder->generateNode(CE, St, *DI);
535 N->markAsSink();
536 UninitBranches.insert(N);
537 continue;
538 }
539
Ted Kremenek03da0d72008-02-21 19:46:04 +0000540 if (L.isUnknown()) {
541 // Invalidate all arguments passed in by reference (LVals).
542 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
543 I != E; ++I) {
544 RVal V = GetRVal(St, *I);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000545
Ted Kremenek03da0d72008-02-21 19:46:04 +0000546 if (isa<LVal>(V))
547 St = SetRVal(St, cast<LVal>(V), UnknownVal());
548 }
549 }
550 else
551 St = EvalCall(CE, cast<LVal>(L), (*DI)->getState());
552
553 Nodify(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +0000554 }
555}
556
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000557void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek874d63f2008-01-24 02:02:54 +0000558
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000559 NodeSet S1;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000560 Visit(Ex, Pred, S1);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000561
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000562 QualType T = CastE->getType();
563
Ted Kremenek402563b2008-02-19 18:47:04 +0000564 // Check for redundant casts or casting to "void"
565 if (T->isVoidType() ||
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000566 Ex->getType() == T ||
567 (T->isPointerType() && Ex->getType()->isFunctionType())) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000568
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000569 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +0000570 Dst.Add(*I1);
571
Ted Kremenek874d63f2008-01-24 02:02:54 +0000572 return;
573 }
574
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000575 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000576 NodeTy* N = *I1;
577 StateTy St = N->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000578 RVal V = GetRVal(St, Ex);
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000579 Nodify(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000580 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000581}
582
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000583void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000584 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000585
586 StateTy St = Pred->getState();
587
588 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000589 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremenekc2c95b02008-02-19 00:29:51 +0000590
591 // FIXME: Add support for local arrays.
592 if (VD->getType()->isArrayType())
593 continue;
594
Ted Kremenek4dc35222008-02-26 00:20:52 +0000595 // FIXME: static variables have an initializer, but the second
596 // time a function is called those values may not be current.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000597 const Expr* Ex = VD->getInit();
598
599 St = SetRVal(St, lval::DeclVal(VD),
600 Ex ? GetRVal(St, Ex) : UninitializedVal());
Ted Kremenek403c1812008-01-28 22:51:57 +0000601 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000602
603 Nodify(Dst, DS, Pred, St);
604
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000605 if (Dst.empty()) { Dst.Add(Pred); }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000606}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000607
Ted Kremenekf233d482008-02-05 00:26:40 +0000608
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000609void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Ted Kremenekf233d482008-02-05 00:26:40 +0000610 NodeTy* Pred, NodeSet& Dst) {
611
Ted Kremenek05a23782008-02-26 19:05:15 +0000612 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
613
Ted Kremenekf233d482008-02-05 00:26:40 +0000614 StateTy St = Pred->getState();
Ted Kremenek05a23782008-02-26 19:05:15 +0000615 RVal X = GetBlkExprRVal(St, Ex);
Ted Kremenekf233d482008-02-05 00:26:40 +0000616
Ted Kremenek05a23782008-02-26 19:05:15 +0000617 assert (X.isUninit());
Ted Kremenekf233d482008-02-05 00:26:40 +0000618
Ted Kremenek05a23782008-02-26 19:05:15 +0000619 Expr* SE = (Expr*) cast<UninitializedVal>(X).getData();
620
621 assert (SE);
622
623 X = GetBlkExprRVal(St, SE);
Ted Kremenek5a7b3822008-02-26 23:37:01 +0000624
625 // Make sure that we invalidate the previous binding.
626 Nodify(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
Ted Kremenekf233d482008-02-05 00:26:40 +0000627}
628
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000629/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000630void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
631 NodeTy* Pred,
632 NodeSet& Dst) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000633
634 assert (Ex->isSizeOf() && "FIXME: AlignOf(Expr) not yet implemented.");
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000635
636 // 6.5.3.4 sizeof: "The result type is an integer."
637
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000638 QualType T = Ex->getArgumentType();
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000639
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000640
Ted Kremenek297d0d72008-02-21 18:15:29 +0000641 // FIXME: Add support for VLAs.
Eli Friedmand8688562008-02-15 12:28:27 +0000642 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000643 return;
644
Ted Kremenek297d0d72008-02-21 18:15:29 +0000645
646 uint64_t size = 1; // Handle sizeof(void)
647
648 if (T != getContext().VoidTy) {
649 SourceLocation Loc = Ex->getExprLoc();
650 size = getContext().getTypeSize(T, Loc) / 8;
651 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000652
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000653 Nodify(Dst, Ex, Pred,
654 SetRVal(Pred->getState(), Ex,
Ted Kremenek297d0d72008-02-21 18:15:29 +0000655 NonLVal::MakeVal(ValMgr, size, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +0000656
657}
658
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000659void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, NodeSet& Dst) {
660
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000661 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000662
663 NodeSet DstTmp;
664
Ted Kremenek018c15f2008-02-26 03:44:25 +0000665 if (isa<DeclRefExpr>(Ex))
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000666 DstTmp.Add(Pred);
667 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000668 Visit(Ex, Pred, DstTmp);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000669
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000670 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000671
672 NodeTy* N = *I;
673 StateTy St = N->getState();
674
675 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
676
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000677 RVal V = GetRVal(St, Ex);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000678
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000679 // Check for dereferences of uninitialized values.
680
681 if (V.isUninit()) {
682
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000683 NodeTy* Succ = Builder->generateNode(U, St, N);
684
685 if (Succ) {
686 Succ->markAsSink();
687 UninitDeref.insert(Succ);
688 }
689
690 continue;
691 }
692
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000693 // Check for dereferences of unknown values. Treat as No-Ops.
694
695 if (V.isUnknown()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000696 Dst.Add(N);
697 continue;
698 }
699
700 // After a dereference, one of two possible situations arise:
701 // (1) A crash, because the pointer was NULL.
702 // (2) The pointer is not NULL, and the dereference works.
703 //
704 // We add these assumptions.
705
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000706 LVal LV = cast<LVal>(V);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000707 bool isFeasibleNotNull;
708
709 // "Assume" that the pointer is Not-NULL.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000710
711 StateTy StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000712
713 if (isFeasibleNotNull) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000714
715 // FIXME: Currently symbolic analysis "generates" new symbols
716 // for the contents of values. We need a better approach.
717
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000718 Nodify(Dst, U, N, SetRVal(StNotNull, U,
719 GetRVal(StNotNull, LV, U->getType())));
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000720 }
721
722 bool isFeasibleNull;
723
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000724 // Now "assume" that the pointer is NULL.
725
726 StateTy StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000727
728 if (isFeasibleNull) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000729
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000730 // We don't use "Nodify" here because the node will be a sink
731 // and we have no intention of processing it later.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000732
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000733 NodeTy* NullNode = Builder->generateNode(U, StNull, N);
734
735 if (NullNode) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000736
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000737 NullNode->markAsSink();
738
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000739 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
740 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000741 }
742 }
743 }
744}
745
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000746void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
747 NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000748
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000749 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000750
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000751 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000752 assert (U->getOpcode() != UnaryOperator::SizeOf);
753 assert (U->getOpcode() != UnaryOperator::AlignOf);
754
755 bool use_GetLVal = false;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000756
757 switch (U->getOpcode()) {
758 case UnaryOperator::PostInc:
759 case UnaryOperator::PostDec:
760 case UnaryOperator::PreInc:
761 case UnaryOperator::PreDec:
762 case UnaryOperator::AddrOf:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000763 // Evalue subexpression as an LVal.
764 use_GetLVal = true;
765 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000766 break;
767
768 default:
769 Visit(U->getSubExpr(), Pred, S1);
770 break;
771 }
772
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000773 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000774
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000775 NodeTy* N1 = *I1;
776 StateTy St = N1->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000777
778 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
779 GetRVal(St, U->getSubExpr());
780
781 if (SubV.isUnknown()) {
782 Dst.Add(N1);
783 continue;
784 }
785
786 if (SubV.isUninit()) {
787 Nodify(Dst, U, N1, SetRVal(St, U, SubV));
788 continue;
789 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000790
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000791 if (U->isIncrementDecrementOp()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000792
793 // Handle ++ and -- (both pre- and post-increment).
794
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000795 LVal SubLV = cast<LVal>(SubV);
796 RVal V = GetRVal(St, SubLV, U->getType());
797
Ted Kremenek89063af2008-02-21 19:15:37 +0000798 if (V.isUnknown()) {
799 Dst.Add(N1);
800 continue;
801 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000802
803 // Propagate uninitialized values.
804 if (V.isUninit()) {
805 Nodify(Dst, U, N1, SetRVal(St, U, V));
806 continue;
807 }
808
Ted Kremenek443003b2008-02-21 19:29:23 +0000809 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000810
811 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
812 : BinaryOperator::Sub;
813
Ted Kremenek443003b2008-02-21 19:29:23 +0000814 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000815
816 if (U->isPostfix())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000817 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000818 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000819 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000820
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000821 Nodify(Dst, U, N1, St);
Ted Kremenek50d0ac22008-02-15 22:09:30 +0000822 continue;
823 }
824
825 // Handle all other unary operators.
826
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000827 switch (U->getOpcode()) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000828
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000829 case UnaryOperator::Minus:
830 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000831 break;
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000832
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000833 case UnaryOperator::Not:
834 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek90e42032008-02-20 04:12:31 +0000835 break;
Ted Kremenek90e42032008-02-20 04:12:31 +0000836
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000837 case UnaryOperator::LNot:
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000838
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000839 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
840 //
841 // Note: technically we do "E == 0", but this is the same in the
842 // transfer functions as "0 == E".
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000843
844 if (isa<LVal>(SubV)) {
845 lval::ConcreteInt V(ValMgr.getZeroWithPtrWidth());
846 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
847 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000848 }
849 else {
Ted Kremenekf7ca6962008-02-22 00:42:36 +0000850 Expr* Ex = U->getSubExpr();
851 nonlval::ConcreteInt V(ValMgr.getValue(0, Ex->getType()));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000852 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
853 St = SetRVal(St, U, Result);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000854 }
855
856 break;
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000857
Ted Kremenek64924852008-01-31 02:35:41 +0000858 case UnaryOperator::AddrOf: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000859 assert (isa<LVal>(SubV));
860 St = SetRVal(St, U, SubV);
Ted Kremenek64924852008-01-31 02:35:41 +0000861 break;
862 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +0000863
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000864 default: ;
865 assert (false && "Not implemented.");
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000866 }
867
868 Nodify(Dst, U, N1, St);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000869 }
870}
871
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000872void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
873 NodeSet& Dst) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000874
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000875 QualType T = U->getSubExpr()->getType();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000876
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000877 // FIXME: Add support for VLAs.
878 if (!T.getTypePtr()->isConstantSizeType())
879 return;
880
881 SourceLocation Loc = U->getExprLoc();
882 uint64_t size = getContext().getTypeSize(T, Loc) / 8;
883 StateTy St = Pred->getState();
884 St = SetRVal(St, U, NonLVal::MakeVal(ValMgr, size, U->getType(), Loc));
885
886 Nodify(Dst, U, Pred, St);
887}
888
889void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
890
891 assert (Ex != CurrentStmt && !getCFG().isBlkExpr(Ex));
892
893 Ex = Ex->IgnoreParens();
894
Ted Kremenek07932632008-02-27 06:47:26 +0000895 if (isa<DeclRefExpr>(Ex)) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000896 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000897 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000898 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000899
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000900 if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex)) {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000901 if (U->getOpcode() == UnaryOperator::Deref) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000902 Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000903
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000904 if (isa<DeclRefExpr>(Ex))
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000905 Dst.Add(Pred);
906 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000907 Visit(Ex, Pred, Dst);
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +0000908
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000909 return;
910 }
911 }
912
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000913 Visit(Ex, Pred, Dst);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000914}
915
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000916void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000917 GRExprEngine::NodeTy* Pred,
918 GRExprEngine::NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000919 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000920
921 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000922 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000923 else
924 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000925
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000926 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000927
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000928 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000929
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000930 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000931 // In such cases, we want to (initially) treat the LHS as an LVal,
932 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
933 // evaluated to LValDecl's instead of to an NonLVal.
934
935 RVal LeftV = B->isAssignmentOp() ? GetLVal(N1->getState(), B->getLHS())
936 : GetRVal(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000937
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000938 // Visit the RHS...
939
940 NodeSet S2;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000941 Visit(B->getRHS(), N1, S2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000942
943 // Process the binary operator.
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000944
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000945 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000946
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000947 NodeTy* N2 = *I2;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000948 StateTy St = N2->getState();
Ted Kremenek3c8d0c52008-02-25 18:42:54 +0000949 Expr* RHS = B->getRHS();
950 RVal RightV = GetRVal(St, RHS);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000951
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000952 BinaryOperator::Opcode Op = B->getOpcode();
953
Ted Kremenek3c8d0c52008-02-25 18:42:54 +0000954 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
955 && RHS->getType()->isIntegerType()) {
Ted Kremenekd763eb92008-02-26 02:15:56 +0000956
957 // Check if the denominator is uninitialized.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +0000958
Ted Kremenek00155412008-02-26 22:27:51 +0000959 if (!RightV.isUnknown()) {
960
961 if (RightV.isUninit()) {
962 NodeTy* DivUninit = Builder->generateNode(B, St, N2);
963
964 if (DivUninit) {
965 DivUninit->markAsSink();
966 BadDivides.insert(DivUninit);
967 }
968
969 continue;
970 }
971
972 // Check for divide/remainder-by-zero.
973 //
974 // First, "assume" that the denominator is 0 or uninitialized.
Ted Kremenekd763eb92008-02-26 02:15:56 +0000975
Ted Kremenek00155412008-02-26 22:27:51 +0000976 bool isFeasible = false;
977 StateTy ZeroSt = Assume(St, RightV, false, isFeasible);
978
979 if (isFeasible) {
980 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
981
982 if (DivZeroNode) {
983 DivZeroNode->markAsSink();
984 BadDivides.insert(DivZeroNode);
985 }
Ted Kremenekd763eb92008-02-26 02:15:56 +0000986 }
987
Ted Kremenek00155412008-02-26 22:27:51 +0000988 // Second, "assume" that the denominator cannot be 0.
Ted Kremenekd763eb92008-02-26 02:15:56 +0000989
Ted Kremenek00155412008-02-26 22:27:51 +0000990 isFeasible = false;
991 St = Assume(St, RightV, true, isFeasible);
Ted Kremenek07d83aa2008-02-25 17:51:31 +0000992
Ted Kremenek00155412008-02-26 22:27:51 +0000993 if (!isFeasible)
994 continue;
Ted Kremenek07d83aa2008-02-25 17:51:31 +0000995 }
996
Ted Kremenek07d83aa2008-02-25 17:51:31 +0000997 // Fall-through. The logic below processes the divide.
998 }
999
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001000 if (Op <= BinaryOperator::Or) {
1001
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001002 // Process non-assignements except commas or short-circuited
1003 // logical expressions (LAnd and LOr).
1004
1005 RVal Result = EvalBinOp(Op, LeftV, RightV);
1006
1007 if (Result.isUnknown()) {
1008 Dst.Add(N2);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001009 continue;
1010 }
1011
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001012 Nodify(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001013 continue;
1014 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001015
1016 // Process assignments.
1017
1018 switch (Op) {
1019
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001020 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001021
1022 // Simple assignments.
Ted Kremenek9dca0622008-02-19 00:22:37 +00001023
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001024 if (LeftV.isUninit()) {
Ted Kremenek9dca0622008-02-19 00:22:37 +00001025 HandleUninitializedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001026 continue;
1027 }
1028
1029 if (LeftV.isUnknown()) {
1030 St = SetRVal(St, B, RightV);
1031 break;
1032 }
Ted Kremenek9dca0622008-02-19 00:22:37 +00001033
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001034 St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV);
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001035 break;
1036 }
1037
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001038 // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +00001039
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001040 default: {
Ted Kremenek9dca0622008-02-19 00:22:37 +00001041
Ted Kremenek00155412008-02-26 22:27:51 +00001042 assert (B->isCompoundAssignmentOp());
1043
1044 if (Op >= BinaryOperator::AndAssign)
1045 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1046 else
1047 ((int&) Op) -= BinaryOperator::MulAssign;
1048
1049 // Check if the LHS is uninitialized.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001050
1051 if (LeftV.isUninit()) {
Ted Kremenek9dca0622008-02-19 00:22:37 +00001052 HandleUninitializedStore(B, N2);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001053 continue;
1054 }
1055
1056 if (LeftV.isUnknown()) {
1057
1058 // While we do not know the location to store RightV,
1059 // the entire expression does evaluate to RightV.
1060
1061 if (RightV.isUnknown()) {
1062 Dst.Add(N2);
1063 continue;
1064 }
1065
1066 St = SetRVal(St, B, RightV);
Ted Kremenek9dca0622008-02-19 00:22:37 +00001067 break;
1068 }
1069
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001070 // At this pointer we know that the LHS evaluates to an LVal
1071 // that is neither "Unknown" or "Unintialized."
1072
1073 LVal LeftLV = cast<LVal>(LeftV);
1074
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001075 // Fetch the value of the LHS (the value of the variable, etc.).
1076
1077 RVal V = GetRVal(N1->getState(), LeftLV, B->getLHS()->getType());
1078
Ted Kremenek00155412008-02-26 22:27:51 +00001079 // Propagate uninitialized value (left-side). We
1080 // propogate uninitialized values for the RHS below when
1081 // we also check for divide-by-zero.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001082
1083 if (V.isUninit()) {
1084 St = SetRVal(St, B, V);
1085 break;
1086 }
1087
1088 // Propagate unknown values.
1089
Ted Kremenek89063af2008-02-21 19:15:37 +00001090 if (V.isUnknown()) {
1091 Dst.Add(N2);
1092 continue;
1093 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001094
1095 if (RightV.isUnknown()) {
1096 St = SetRVal(SetRVal(St, LeftLV, RightV), B, RightV);
1097 break;
1098 }
1099
Ted Kremenek00155412008-02-26 22:27:51 +00001100 // At this point:
1101 //
1102 // The LHS is not Uninit/Unknown.
1103 // The RHS is not Unknown.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001104
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001105 // Get the computation type.
1106 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
1107
1108 // Perform promotions.
1109 V = EvalCast(V, CTy);
Ted Kremenek61e090c2008-02-21 18:46:24 +00001110 RightV = EvalCast(RightV, CTy);
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001111
1112 // Evaluate operands and promote to result type.
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001113
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00001114 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1115 && RHS->getType()->isIntegerType()) {
1116
Ted Kremenekd763eb92008-02-26 02:15:56 +00001117 // Check if the denominator is uninitialized.
1118
1119 if (RightV.isUninit()) {
1120 NodeTy* DivUninit = Builder->generateNode(B, St, N2);
1121
1122 if (DivUninit) {
1123 DivUninit->markAsSink();
1124 BadDivides.insert(DivUninit);
1125 }
1126
1127 continue;
1128 }
Ted Kremenek00155412008-02-26 22:27:51 +00001129
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001130 // First, "assume" that the denominator is 0.
1131
1132 bool isFeasible = false;
1133 StateTy ZeroSt = Assume(St, RightV, false, isFeasible);
1134
1135 if (isFeasible) {
1136 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
1137
1138 if (DivZeroNode) {
1139 DivZeroNode->markAsSink();
Ted Kremenekd763eb92008-02-26 02:15:56 +00001140 BadDivides.insert(DivZeroNode);
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001141 }
1142 }
1143
1144 // Second, "assume" that the denominator cannot be 0.
1145
1146 isFeasible = false;
1147 St = Assume(St, RightV, true, isFeasible);
1148
1149 if (!isFeasible)
1150 continue;
1151
1152 // Fall-through. The logic below processes the divide.
1153 }
Ted Kremenek00155412008-02-26 22:27:51 +00001154 else {
1155
1156 // Propagate uninitialized values (right-side).
1157
1158 if (RightV.isUninit()) {
1159 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
1160 break;
1161 }
1162
1163 }
Ted Kremenek07d83aa2008-02-25 17:51:31 +00001164
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001165 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001166 St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00001167 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001168 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001169
1170 Nodify(Dst, B, N2, St);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00001171 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001172 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00001173}
Ted Kremenekee985462008-01-16 18:18:48 +00001174
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001175void GRExprEngine::HandleUninitializedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenek9dca0622008-02-19 00:22:37 +00001176 NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred);
1177 N->markAsSink();
1178 UninitStores.insert(N);
1179}
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001180
Ted Kremenek9dca0622008-02-19 00:22:37 +00001181void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001182
1183 // FIXME: add metadata to the CFG so that we can disable
1184 // this check when we KNOW that there is no block-level subexpression.
1185 // The motivation is that this check requires a hashtable lookup.
1186
1187 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1188 Dst.Add(Pred);
1189 return;
1190 }
1191
1192 switch (S->getStmtClass()) {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001193
1194 default:
1195 // Cases we intentionally have "default" handle:
Ted Kremenek189c3052008-02-26 19:17:09 +00001196 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
Ted Kremenek230aaab2008-02-12 21:37:25 +00001197
1198 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1199 break;
1200
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001201 case Stmt::BinaryOperatorClass: {
1202 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenekf233d482008-02-05 00:26:40 +00001203
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001204 if (B->isLogicalOp()) {
1205 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenekf233d482008-02-05 00:26:40 +00001206 break;
1207 }
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001208 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenekda9bd092008-02-08 07:05:39 +00001209 StateTy St = Pred->getState();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001210 Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001211 break;
1212 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001213
1214 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1215 break;
1216 }
Ted Kremenekde434242008-02-19 01:44:53 +00001217
1218 case Stmt::CallExprClass: {
1219 CallExpr* C = cast<CallExpr>(S);
1220 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
1221 break;
1222 }
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001223
1224 case Stmt::CastExprClass: {
1225 CastExpr* C = cast<CastExpr>(S);
1226 VisitCast(C, C->getSubExpr(), Pred, Dst);
1227 break;
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001228 }
Ted Kremenek189c3052008-02-26 19:17:09 +00001229
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001230 // FIXME: ChooseExpr is really a constant. We need to fix
1231 // the CFG do not model them as explicit control-flow.
1232
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001233 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1234 ChooseExpr* C = cast<ChooseExpr>(S);
1235 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1236 break;
1237 }
Ted Kremenekf233d482008-02-05 00:26:40 +00001238
Ted Kremenekb4ae33f2008-01-23 23:38:00 +00001239 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001240 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1241 break;
1242
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001243 case Stmt::ConditionalOperatorClass: { // '?' operator
1244 ConditionalOperator* C = cast<ConditionalOperator>(S);
1245 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1246 break;
1247 }
1248
1249 case Stmt::DeclRefExprClass:
1250 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1251 break;
1252
1253 case Stmt::DeclStmtClass:
1254 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1255 break;
1256
1257 case Stmt::ImplicitCastExprClass: {
1258 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1259 VisitCast(C, C->getSubExpr(), Pred, Dst);
1260 break;
1261 }
1262
1263 case Stmt::ParenExprClass:
1264 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1265 break;
1266
1267 case Stmt::SizeOfAlignOfTypeExprClass:
1268 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1269 break;
1270
Ted Kremenekda9bd092008-02-08 07:05:39 +00001271 case Stmt::StmtExprClass: {
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001272 StmtExpr* SE = cast<StmtExpr>(S);
1273
Ted Kremenekda9bd092008-02-08 07:05:39 +00001274 StateTy St = Pred->getState();
Ted Kremenekd70b62e2008-02-08 20:29:23 +00001275 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001276 Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
Ted Kremenekda9bd092008-02-08 07:05:39 +00001277 break;
1278 }
1279
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001280 // FIXME: We may wish to always bind state to ReturnStmts so
1281 // that users can quickly query what was the state at the
1282 // exit points of a function.
1283
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001284 case Stmt::ReturnStmtClass: {
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001285 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1286 Visit(R, Pred, Dst);
1287 else
1288 Dst.Add(Pred);
1289
1290 break;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001291 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00001292
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001293 case Stmt::UnaryOperatorClass: {
1294 UnaryOperator* U = cast<UnaryOperator>(S);
1295
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001296 switch (U->getOpcode()) {
1297 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
1298 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
1299 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
1300 default: VisitUnaryOperator(U, Pred, Dst); break;
1301 }
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001302
Ted Kremenek9de04c42008-01-24 20:55:43 +00001303 break;
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001304 }
Ted Kremenek79649df2008-01-17 18:25:22 +00001305 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +00001306}
1307
Ted Kremenekee985462008-01-16 18:18:48 +00001308//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +00001309// "Assume" logic.
1310//===----------------------------------------------------------------------===//
1311
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001312GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LVal Cond,
Ted Kremenek692416c2008-02-18 22:57:02 +00001313 bool Assumption,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001314 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001315 switch (Cond.getSubKind()) {
1316 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001317 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001318 return St;
1319
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001320 case lval::SymbolValKind:
1321 if (Assumption)
1322 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1323 ValMgr.getZeroWithPtrWidth(), isFeasible);
1324 else
1325 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1326 ValMgr.getZeroWithPtrWidth(), isFeasible);
1327
Ted Kremenek08b66252008-02-06 04:31:33 +00001328
Ted Kremenek329f8542008-02-05 21:52:21 +00001329 case lval::DeclValKind:
Ted Kremenekdc3936b2008-02-22 00:54:56 +00001330 case lval::FuncValKind:
1331 case lval::GotoLabelKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001332 isFeasible = Assumption;
1333 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001334
Ted Kremenek329f8542008-02-05 21:52:21 +00001335 case lval::ConcreteIntKind: {
1336 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +00001337 isFeasible = b ? Assumption : !Assumption;
1338 return St;
1339 }
1340 }
Ted Kremenekb38911f2008-01-30 23:03:39 +00001341}
1342
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001343GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLVal Cond,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001344 bool Assumption,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001345 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001346 switch (Cond.getSubKind()) {
1347 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001348 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenekb38911f2008-01-30 23:03:39 +00001349 return St;
1350
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001351
1352 case nonlval::SymbolValKind: {
Ted Kremenek230aaab2008-02-12 21:37:25 +00001353 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001354 SymbolID sym = SV.getSymbol();
1355
1356 if (Assumption)
1357 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1358 isFeasible);
1359 else
1360 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1361 isFeasible);
1362 }
1363
Ted Kremenek08b66252008-02-06 04:31:33 +00001364 case nonlval::SymIntConstraintValKind:
1365 return
1366 AssumeSymInt(St, Assumption,
1367 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1368 isFeasible);
1369
Ted Kremenek329f8542008-02-05 21:52:21 +00001370 case nonlval::ConcreteIntKind: {
1371 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001372 isFeasible = b ? Assumption : !Assumption;
1373 return St;
1374 }
1375 }
1376}
1377
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001378GRExprEngine::StateTy
1379GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001380 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenek692416c2008-02-18 22:57:02 +00001381
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001382 // First, determine if sym == X, where X != V.
1383 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1384 isFeasible = *X != V;
1385 return St;
1386 }
1387
1388 // Second, determine if sym != V.
1389 if (St.isNotEqual(sym, V)) {
1390 isFeasible = true;
1391 return St;
1392 }
1393
1394 // If we reach here, sym is not a constant and we don't know if it is != V.
1395 // Make that assumption.
1396
1397 isFeasible = true;
1398 return StateMgr.AddNE(St, sym, V);
1399}
1400
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001401GRExprEngine::StateTy
1402GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001403 const llvm::APSInt& V, bool& isFeasible) {
1404
1405 // First, determine if sym == X, where X != V.
1406 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1407 isFeasible = *X == V;
1408 return St;
1409 }
1410
1411 // Second, determine if sym != V.
1412 if (St.isNotEqual(sym, V)) {
1413 isFeasible = false;
1414 return St;
1415 }
1416
1417 // If we reach here, sym is not a constant and we don't know if it is == V.
1418 // Make that assumption.
1419
1420 isFeasible = true;
1421 return StateMgr.AddEQ(St, sym, V);
1422}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001423
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001424GRExprEngine::StateTy
1425GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek08b66252008-02-06 04:31:33 +00001426 const SymIntConstraint& C, bool& isFeasible) {
1427
1428 switch (C.getOpcode()) {
1429 default:
1430 // No logic yet for other operators.
1431 return St;
1432
1433 case BinaryOperator::EQ:
1434 if (Assumption)
1435 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1436 else
1437 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1438
1439 case BinaryOperator::NE:
1440 if (Assumption)
1441 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1442 else
1443 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1444 }
1445}
1446
Ted Kremenekb38911f2008-01-30 23:03:39 +00001447//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00001448// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00001449//===----------------------------------------------------------------------===//
1450
Ted Kremenekaa66a322008-01-16 21:46:15 +00001451#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001452static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001453
Ted Kremenekaa66a322008-01-16 21:46:15 +00001454namespace llvm {
1455template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001456struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00001457 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001458
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001459 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek016f52f2008-02-08 21:10:02 +00001460
1461 Out << "Variables:\\l";
1462
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001463 bool isFirst = true;
1464
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001465 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek016f52f2008-02-08 21:10:02 +00001466 E=St.vb_end(); I!=E;++I) {
1467
1468 if (isFirst)
1469 isFirst = false;
1470 else
1471 Out << "\\l";
1472
1473 Out << ' ' << I.getKey()->getName() << " : ";
1474 I.getData().print(Out);
1475 }
1476
1477 }
1478
Ted Kremeneke7d22112008-02-11 19:21:59 +00001479
Ted Kremenek44842c22008-02-13 18:06:44 +00001480 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001481
1482 bool isFirst = true;
1483
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001484 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001485 I != E;++I) {
1486
1487 if (isFirst) {
1488 Out << "\\l\\lSub-Expressions:\\l";
1489 isFirst = false;
1490 }
1491 else
1492 Out << "\\l";
1493
1494 Out << " (" << (void*) I.getKey() << ") ";
1495 I.getKey()->printPretty(Out);
1496 Out << " : ";
1497 I.getData().print(Out);
1498 }
1499 }
1500
Ted Kremenek44842c22008-02-13 18:06:44 +00001501 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremeneke7d22112008-02-11 19:21:59 +00001502
Ted Kremenek016f52f2008-02-08 21:10:02 +00001503 bool isFirst = true;
1504
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001505 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremeneke7d22112008-02-11 19:21:59 +00001506 I != E; ++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001507 if (isFirst) {
Ted Kremeneke7d22112008-02-11 19:21:59 +00001508 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001509 isFirst = false;
1510 }
1511 else
1512 Out << "\\l";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001513
Ted Kremeneke7d22112008-02-11 19:21:59 +00001514 Out << " (" << (void*) I.getKey() << ") ";
1515 I.getKey()->printPretty(Out);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001516 Out << " : ";
1517 I.getData().print(Out);
1518 }
1519 }
1520
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001521 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001522 ValueState::ConstEqTy CE = St.getImpl()->ConstEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00001523
1524 if (CE.isEmpty())
1525 return;
1526
1527 Out << "\\l\\|'==' constraints:";
1528
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001529 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneked4de312008-02-06 03:56:15 +00001530 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1531 }
1532
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001533 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001534 ValueState::ConstNotEqTy NE = St.getImpl()->ConstNotEq;
Ted Kremeneked4de312008-02-06 03:56:15 +00001535
1536 if (NE.isEmpty())
1537 return;
1538
1539 Out << "\\l\\|'!=' constraints:";
1540
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001541 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneked4de312008-02-06 03:56:15 +00001542 I != EI; ++I){
1543
1544 Out << "\\l $" << I.getKey() << " : ";
1545 bool isFirst = true;
1546
1547 ValueState::IntSetTy::iterator J=I.getData().begin(),
1548 EJ=I.getData().end();
1549 for ( ; J != EJ; ++J) {
1550 if (isFirst) isFirst = false;
1551 else Out << ", ";
1552
1553 Out << (*J)->toString();
1554 }
1555 }
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001556 }
1557
1558 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1559
1560 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00001561 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenekb5339122008-02-19 20:53:06 +00001562 GraphPrintCheckerState->isUninitDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00001563 GraphPrintCheckerState->isUninitStore(N) ||
Ted Kremenekd87a3212008-02-26 21:31:18 +00001564 GraphPrintCheckerState->isUninitControlFlow(N) ||
1565 GraphPrintCheckerState->isBadDivide(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00001566 return "color=\"red\",style=\"filled\"";
1567
1568 return "";
1569 }
Ted Kremeneked4de312008-02-06 03:56:15 +00001570
Ted Kremenek4d4dd852008-02-13 17:41:41 +00001571 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001572 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001573
1574 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001575 ProgramPoint Loc = N->getLocation();
1576
1577 switch (Loc.getKind()) {
1578 case ProgramPoint::BlockEntranceKind:
1579 Out << "Block Entrance: B"
1580 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1581 break;
1582
1583 case ProgramPoint::BlockExitKind:
1584 assert (false);
1585 break;
1586
1587 case ProgramPoint::PostStmtKind: {
1588 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001589 Out << L.getStmt()->getStmtClassName() << ':'
1590 << (void*) L.getStmt() << ' ';
1591
Ted Kremenekaa66a322008-01-16 21:46:15 +00001592 L.getStmt()->printPretty(Out);
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001593
1594 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1595 Out << "\\|Implicit-Null Dereference.\\l";
1596 }
Ted Kremenek63a4f692008-02-07 06:04:18 +00001597 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1598 Out << "\\|Explicit-Null Dereference.\\l";
1599 }
Ted Kremenekb5339122008-02-19 20:53:06 +00001600 else if (GraphPrintCheckerState->isUninitDeref(N)) {
1601 Out << "\\|Dereference of uninitialied value.\\l";
1602 }
Ted Kremenek9dca0622008-02-19 00:22:37 +00001603 else if (GraphPrintCheckerState->isUninitStore(N)) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001604 Out << "\\|Store to Uninitialized LVal.";
Ted Kremenek9dca0622008-02-19 00:22:37 +00001605 }
Ted Kremenekd87a3212008-02-26 21:31:18 +00001606 else if (GraphPrintCheckerState->isBadDivide(N)) {
1607 Out << "\\|Divide-by zero or uninitialized value.";
1608 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00001609
Ted Kremenekaa66a322008-01-16 21:46:15 +00001610 break;
1611 }
1612
1613 default: {
1614 const BlockEdge& E = cast<BlockEdge>(Loc);
1615 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1616 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001617
1618 if (Stmt* T = E.getSrc()->getTerminator()) {
1619 Out << "\\|Terminator: ";
1620 E.getSrc()->printTerminator(Out);
1621
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00001622 if (isa<SwitchStmt>(T)) {
1623 Stmt* Label = E.getDst()->getLabel();
1624
1625 if (Label) {
1626 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1627 Out << "\\lcase ";
1628 C->getLHS()->printPretty(Out);
1629
1630 if (Stmt* RHS = C->getRHS()) {
1631 Out << " .. ";
1632 RHS->printPretty(Out);
1633 }
1634
1635 Out << ":";
1636 }
1637 else {
1638 assert (isa<DefaultStmt>(Label));
1639 Out << "\\ldefault:";
1640 }
1641 }
1642 else
1643 Out << "\\l(implicit) default:";
1644 }
1645 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00001646 // FIXME
1647 }
1648 else {
1649 Out << "\\lCondition: ";
1650 if (*E.getSrc()->succ_begin() == E.getDst())
1651 Out << "true";
1652 else
1653 Out << "false";
1654 }
1655
1656 Out << "\\l";
1657 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001658
1659 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1660 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1661 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001662 }
1663 }
1664
Ted Kremenek9153f732008-02-05 07:17:49 +00001665 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00001666
Ted Kremeneke7d22112008-02-11 19:21:59 +00001667 N->getState().printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001668
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001669 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001670 return Out.str();
1671 }
1672};
1673} // end llvm namespace
1674#endif
1675
Ted Kremeneke01c9872008-02-14 22:36:46 +00001676void GRExprEngine::ViewGraph() {
Ted Kremenekaa66a322008-01-16 21:46:15 +00001677#ifndef NDEBUG
Ted Kremeneke01c9872008-02-14 22:36:46 +00001678 GraphPrintCheckerState = this;
1679 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001680 GraphPrintCheckerState = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00001681#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001682}