blob: 674454c7cd361254b6f612afc241bb6702a1023f [file] [log] [blame]
Ted Kremenek50df4f42008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenekc48b8e42008-01-31 02:35:41 +00002//
Ted Kremenek2e160602008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenek68d70a82008-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 Kremenek50df4f42008-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 Kremenek68d70a82008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek50df4f42008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek3862eb12008-02-14 22:36:46 +000017#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
18
19#include "llvm/Support/Streams.h"
Ted Kremenekd4467432008-02-14 22:16:04 +000020
21using namespace clang;
22using llvm::dyn_cast;
23using llvm::cast;
24using llvm::APSInt;
Ted Kremenekf031b872008-01-23 19:59:44 +000025
Ted Kremenek30fa28b2008-02-13 17:41:41 +000026GRExprEngine::StateTy
Ted Kremenek07baa252008-02-21 18:02:17 +000027GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal& V) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +000028
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000029 if (!StateCleaned) {
30 St = RemoveDeadBindings(CurrentStmt, St);
31 StateCleaned = true;
32 }
Ted Kremenek9b32cd02008-02-07 04:16:04 +000033
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000034 bool isBlkExpr = false;
Ted Kremenek9b32cd02008-02-07 04:16:04 +000035
Ted Kremenek07baa252008-02-21 18:02:17 +000036 if (Ex == CurrentStmt) {
37 isBlkExpr = getCFG().isBlkExpr(Ex);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000038
39 if (!isBlkExpr)
40 return St;
41 }
Ted Kremenek9b32cd02008-02-07 04:16:04 +000042
Ted Kremenek07baa252008-02-21 18:02:17 +000043 return StateMgr.SetRVal(St, Ex, isBlkExpr, V);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000044}
45
Ted Kremenek30fa28b2008-02-13 17:41:41 +000046const GRExprEngine::StateTy::BufferTy&
Ted Kremenek07baa252008-02-21 18:02:17 +000047GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal::BufferTy& RB,
Ted Kremenekf20c2012008-02-05 19:35:18 +000048 StateTy::BufferTy& RetBuf) {
49
50 assert (RetBuf.empty());
51
Ted Kremenek07baa252008-02-21 18:02:17 +000052 for (RVal::BufferTy::const_iterator I = RB.begin(), E = RB.end(); I!=E; ++I)
53 RetBuf.push_back(SetRVal(St, Ex, *I));
Ted Kremenekf20c2012008-02-05 19:35:18 +000054
55 return RetBuf;
56}
57
Ted Kremenek30fa28b2008-02-13 17:41:41 +000058GRExprEngine::StateTy
Ted Kremenek07baa252008-02-21 18:02:17 +000059GRExprEngine::SetRVal(StateTy St, const LVal& LV, const RVal& RV) {
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000060
61 if (!StateCleaned) {
62 St = RemoveDeadBindings(CurrentStmt, St);
63 StateCleaned = true;
64 }
65
Ted Kremenek07baa252008-02-21 18:02:17 +000066 return StateMgr.SetRVal(St, LV, RV);
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000067}
68
Ted Kremenek99ecce72008-02-26 19:05:15 +000069GRExprEngine::StateTy
70GRExprEngine::MarkBranch(StateTy St, Stmt* Terminator, bool branchTaken) {
71
72 switch (Terminator->getStmtClass()) {
73 default:
74 return St;
75
76 case Stmt::BinaryOperatorClass: { // '&&' and '||'
77
78 BinaryOperator* B = cast<BinaryOperator>(Terminator);
79 BinaryOperator::Opcode Op = B->getOpcode();
80
81 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
82
83 // For &&, if we take the true branch, then the value of the whole
84 // expression is that of the RHS expression.
85 //
86 // For ||, if we take the false branch, then the value of the whole
87 // expression is that of the RHS expression.
88
89 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
90 (Op == BinaryOperator::LOr && !branchTaken)
91 ? B->getRHS() : B->getLHS();
92
93 return SetBlkExprRVal(St, B, UninitializedVal(Ex));
94 }
95
96 case Stmt::ConditionalOperatorClass: { // ?:
97
98 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
99
100 // For ?, if branchTaken == true then the value is either the LHS or
101 // the condition itself. (GNU extension).
102
103 Expr* Ex;
104
105 if (branchTaken)
106 Ex = C->getLHS() ? C->getLHS() : C->getCond();
107 else
108 Ex = C->getRHS();
109
110 return SetBlkExprRVal(St, C, UninitializedVal(Ex));
111 }
112
113 case Stmt::ChooseExprClass: { // ?:
114
115 ChooseExpr* C = cast<ChooseExpr>(Terminator);
116
117 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
118 return SetBlkExprRVal(St, C, UninitializedVal(Ex));
119 }
120 }
121}
122
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000123void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek07baa252008-02-21 18:02:17 +0000124 BranchNodeBuilder& builder) {
Ted Kremenek90960972008-01-30 23:03:39 +0000125
Ted Kremenek17c5f112008-02-11 19:21:59 +0000126 // Remove old bindings for subexpressions.
127 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000128
Ted Kremenek022b6052008-02-15 22:29:00 +0000129 // Check for NULL conditions; e.g. "for(;;)"
130 if (!Condition) {
131 builder.markInfeasible(false);
132
133 // Get the current block counter.
134 GRBlockCounter BC = builder.getBlockCounter();
135 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
136 unsigned NumVisited = BC.getNumVisited(BlockID);
137
138 if (NumVisited < 1) builder.generateNode(PrevState, true);
139 else builder.markInfeasible(true);
140
141 return;
142 }
143
Ted Kremenek07baa252008-02-21 18:02:17 +0000144 RVal V = GetRVal(PrevState, Condition);
Ted Kremenek90960972008-01-30 23:03:39 +0000145
146 switch (V.getBaseKind()) {
147 default:
148 break;
149
Ted Kremenek07baa252008-02-21 18:02:17 +0000150 case RVal::UnknownKind:
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000151 builder.generateNode(MarkBranch(PrevState, Term, true), true);
152 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenek90960972008-01-30 23:03:39 +0000153 return;
154
Ted Kremenek07baa252008-02-21 18:02:17 +0000155 case RVal::UninitializedKind: {
Ted Kremenek90960972008-01-30 23:03:39 +0000156 NodeTy* N = builder.generateNode(PrevState, true);
157
158 if (N) {
159 N->markAsSink();
160 UninitBranches.insert(N);
161 }
162
163 builder.markInfeasible(false);
164 return;
165 }
166 }
Ted Kremenek022b6052008-02-15 22:29:00 +0000167
Ted Kremenek4b170e52008-02-12 18:08:17 +0000168 // Get the current block counter.
169 GRBlockCounter BC = builder.getBlockCounter();
Ted Kremenekfd85f292008-02-12 19:49:57 +0000170 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
171 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000172
Ted Kremenek4b170e52008-02-12 18:08:17 +0000173 if (isa<nonlval::ConcreteInt>(V) ||
174 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
175
176 // Process the true branch.
Ted Kremenek90960972008-01-30 23:03:39 +0000177
Ted Kremenek4b170e52008-02-12 18:08:17 +0000178 bool isFeasible = true;
179
180 StateTy St = Assume(PrevState, V, true, isFeasible);
181
182 if (isFeasible)
Ted Kremenek99ecce72008-02-26 19:05:15 +0000183 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek4b170e52008-02-12 18:08:17 +0000184 else
185 builder.markInfeasible(true);
Ted Kremenek90960972008-01-30 23:03:39 +0000186 }
Ted Kremenek4b170e52008-02-12 18:08:17 +0000187 else
188 builder.markInfeasible(true);
Ted Kremenek90960972008-01-30 23:03:39 +0000189
Ted Kremenekfd85f292008-02-12 19:49:57 +0000190 BlockID = builder.getTargetBlock(false)->getBlockID();
191 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenek90960972008-01-30 23:03:39 +0000192
Ted Kremenek4b170e52008-02-12 18:08:17 +0000193 if (isa<nonlval::ConcreteInt>(V) ||
194 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
195
196 // Process the false branch.
197
198 bool isFeasible = false;
199
200 StateTy St = Assume(PrevState, V, false, isFeasible);
201
202 if (isFeasible)
Ted Kremenek99ecce72008-02-26 19:05:15 +0000203 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenek4b170e52008-02-12 18:08:17 +0000204 else
205 builder.markInfeasible(false);
206 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000207 else
208 builder.markInfeasible(false);
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000209}
210
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000211/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000212/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000213void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000214
215 StateTy St = builder.getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000216 RVal V = GetRVal(St, builder.getTarget());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000217
218 // Three possibilities:
219 //
220 // (1) We know the computed label.
221 // (2) The label is NULL (or some other constant), or Uninitialized.
222 // (3) We have no clue about the label. Dispatch to all targets.
223 //
224
225 typedef IndirectGotoNodeBuilder::iterator iterator;
226
227 if (isa<lval::GotoLabel>(V)) {
228 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
229
230 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek79f63f52008-02-13 17:27:37 +0000231 if (I.getLabel() == L) {
232 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000233 return;
234 }
235 }
236
237 assert (false && "No block with label.");
238 return;
239 }
240
241 if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) {
242 // Dispatch to the first target and mark it as a sink.
Ted Kremenek79f63f52008-02-13 17:27:37 +0000243 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000244 UninitBranches.insert(N);
245 return;
246 }
247
248 // This is really a catch-all. We don't support symbolics yet.
249
Ted Kremenek07baa252008-02-21 18:02:17 +0000250 assert (V.isUnknown());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000251
252 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek79f63f52008-02-13 17:27:37 +0000253 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000254}
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000255
Ted Kremenekaee121c2008-02-13 23:08:21 +0000256/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
257/// nodes by processing the 'effects' of a switch statement.
258void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
259
260 typedef SwitchNodeBuilder::iterator iterator;
261
262 StateTy St = builder.getState();
Ted Kremenekbc965a62008-02-18 22:57:02 +0000263 Expr* CondE = builder.getCondition();
Ted Kremenek07baa252008-02-21 18:02:17 +0000264 RVal CondV = GetRVal(St, CondE);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000265
Ted Kremenek07baa252008-02-21 18:02:17 +0000266 if (CondV.isUninit()) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000267 NodeTy* N = builder.generateDefaultCaseNode(St, true);
268 UninitBranches.insert(N);
269 return;
270 }
271
272 StateTy DefaultSt = St;
273
274 // While most of this can be assumed (such as the signedness), having it
275 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenekbc965a62008-02-18 22:57:02 +0000276
277 unsigned bits = getContext().getTypeSize(CondE->getType(),
278 CondE->getExprLoc());
279
Ted Kremenekaee121c2008-02-13 23:08:21 +0000280 APSInt V1(bits, false);
281 APSInt V2 = V1;
282
Ted Kremenek07baa252008-02-21 18:02:17 +0000283 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000284
285 CaseStmt* Case = cast<CaseStmt>(I.getCase());
286
287 // Evaluate the case.
288 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
289 assert (false && "Case condition must evaluate to an integer constant.");
290 return;
291 }
292
293 // Get the RHS of the case, if it exists.
294
295 if (Expr* E = Case->getRHS()) {
296 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
297 assert (false &&
298 "Case condition (RHS) must evaluate to an integer constant.");
299 return ;
300 }
301
302 assert (V1 <= V2);
303 }
304 else V2 = V1;
305
306 // FIXME: Eventually we should replace the logic below with a range
307 // comparison, rather than concretize the values within the range.
Ted Kremenek07baa252008-02-21 18:02:17 +0000308 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekaee121c2008-02-13 23:08:21 +0000309
310 do {
311 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
312
Ted Kremenek07baa252008-02-21 18:02:17 +0000313 RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000314
315 // Now "assume" that the case matches.
Ted Kremenekbc965a62008-02-18 22:57:02 +0000316 bool isFeasible = false;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000317
Ted Kremenekb1934132008-02-14 19:37:24 +0000318 StateTy StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000319
320 if (isFeasible) {
321 builder.generateCaseStmtNode(I, StNew);
322
323 // If CondV evaluates to a constant, then we know that this
324 // is the *only* case that we can take, so stop evaluating the
325 // others.
326 if (isa<nonlval::ConcreteInt>(CondV))
327 return;
328 }
329
330 // Now "assume" that the case doesn't match. Add this state
331 // to the default state (if it is feasible).
332
Ted Kremenekb1934132008-02-14 19:37:24 +0000333 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000334
335 if (isFeasible)
336 DefaultSt = StNew;
337
338 // Concretize the next value in the range.
339 ++V1;
340
341 } while (V1 < V2);
342 }
343
344 // If we reach here, than we know that the default branch is
345 // possible.
346 builder.generateDefaultCaseNode(DefaultSt);
347}
348
349
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000350void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000351 NodeSet& Dst) {
Ted Kremenekbf988d02008-02-19 00:22:37 +0000352
Ted Kremenek99ecce72008-02-26 19:05:15 +0000353 assert (B->getOpcode() == BinaryOperator::LAnd ||
354 B->getOpcode() == BinaryOperator::LOr);
355
356 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
357
358 StateTy St = Pred->getState();
359 RVal X = GetBlkExprRVal(St, B);
360
361 assert (X.isUninit());
362
363 Expr* Ex = (Expr*) cast<UninitializedVal>(X).getData();
364
365 assert (Ex);
366
367 if (Ex == B->getRHS()) {
368
369 X = GetBlkExprRVal(St, Ex);
370
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000371 // Handle uninitialized values.
372
373 if (X.isUninit()) {
374 Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
375 return;
376 }
377
Ted Kremenek99ecce72008-02-26 19:05:15 +0000378 // We took the RHS. Because the value of the '&&' or '||' expression must
379 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
380 // or 1. Alternatively, we could take a lazy approach, and calculate this
381 // value later when necessary. We don't have the machinery in place for
382 // this right now, and since most logical expressions are used for branches,
383 // the payoff is not likely to be large. Instead, we do eager evaluation.
384
385 bool isFeasible = false;
386 StateTy NewState = Assume(St, X, true, isFeasible);
387
388 if (isFeasible)
389 Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
390
391 isFeasible = false;
392 NewState = Assume(St, X, false, isFeasible);
393
394 if (isFeasible)
395 Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000396 }
397 else {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000398 // We took the LHS expression. Depending on whether we are '&&' or
399 // '||' we know what the value of the expression is via properties of
400 // the short-circuiting.
401
402 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
403 Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000404 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000405}
Ted Kremenek99ecce72008-02-26 19:05:15 +0000406
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000407
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000408void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000409
Ted Kremenek07baa252008-02-21 18:02:17 +0000410 Builder = &builder;
Ted Kremenekf031b872008-01-23 19:59:44 +0000411 StmtEntryNode = builder.getLastNode();
412 CurrentStmt = S;
413 NodeSet Dst;
414 StateCleaned = false;
415
416 Visit(S, StmtEntryNode, Dst);
417
418 // If no nodes were generated, generate a new node that has all the
419 // dead mappings removed.
Ted Kremenek07baa252008-02-21 18:02:17 +0000420
Ted Kremenekf031b872008-01-23 19:59:44 +0000421 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
422 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
423 builder.generateNode(S, St, StmtEntryNode);
424 }
Ted Kremeneka57214f2008-01-18 00:41:32 +0000425
Ted Kremenek07baa252008-02-21 18:02:17 +0000426 // For safety, NULL out these variables.
427
Ted Kremenekf031b872008-01-23 19:59:44 +0000428 CurrentStmt = NULL;
429 StmtEntryNode = NULL;
430 Builder = NULL;
Ted Kremenek68d70a82008-01-15 23:55:06 +0000431}
432
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000433GRExprEngine::NodeTy*
434GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000435
436 // If the state hasn't changed, don't generate a new node.
Ted Kremenek02b5b402008-02-07 15:20:13 +0000437 if (St == Pred->getState())
Ted Kremenek80d52d02008-02-07 05:48:01 +0000438 return NULL;
Ted Kremenekafba4b22008-01-16 00:53:15 +0000439
Ted Kremenek80d52d02008-02-07 05:48:01 +0000440 NodeTy* N = Builder->generateNode(S, St, Pred);
441 Dst.Add(N);
Ted Kremenek07baa252008-02-21 18:02:17 +0000442
Ted Kremenek80d52d02008-02-07 05:48:01 +0000443 return N;
Ted Kremenekafba4b22008-01-16 00:53:15 +0000444}
Ted Kremenek68d70a82008-01-15 23:55:06 +0000445
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000446void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekf20c2012008-02-05 19:35:18 +0000447 const StateTy::BufferTy& SB) {
448
449 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
450 Nodify(Dst, S, Pred, *I);
451}
452
Ted Kremenek6d409922008-02-13 18:06:44 +0000453void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek07baa252008-02-21 18:02:17 +0000454
Ted Kremenekef0007f2008-02-26 02:15:56 +0000455 if (VarDecl* VD = dyn_cast<VarDecl>(D->getDecl()))
456 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) {
457
458 StateTy StOld = Pred->getState();
459 StateTy St = Symbolicate(StOld, VD);
460
461 if (!(St == StOld)) {
462 if (D != CurrentStmt)
463 Nodify(Dst, D, Pred, St);
464 else
465 Nodify(Dst, D, Pred, SetRVal(St, D, GetRVal(St, D)));
466
467 return;
468 }
469 }
470
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000471 if (D != CurrentStmt) {
472 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
473 return;
474 }
475
476 // If we are here, we are loading the value of the decl and binding
477 // it to the block-level expression.
478
Ted Kremenek07baa252008-02-21 18:02:17 +0000479 StateTy St = Pred->getState();
480 Nodify(Dst, D, Pred, SetRVal(St, D, GetRVal(St, D)));
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000481}
482
Ted Kremenekd9268e32008-02-19 01:44:53 +0000483void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000484 CallExpr::arg_iterator AI,
485 CallExpr::arg_iterator AE,
Ted Kremenekd9268e32008-02-19 01:44:53 +0000486 NodeSet& Dst) {
487
Ted Kremenek07baa252008-02-21 18:02:17 +0000488 // Process the arguments.
489
490 if (AI != AE) {
Ted Kremenekd9268e32008-02-19 01:44:53 +0000491
Ted Kremenek07baa252008-02-21 18:02:17 +0000492 NodeSet DstTmp;
493 Visit(*AI, Pred, DstTmp);
494 ++AI;
495
496 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
497 VisitCall(CE, *DI, AI, AE, Dst);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000498
499 return;
500 }
501
502 // If we reach here we have processed all of the arguments. Evaluate
503 // the callee expression.
Ted Kremenekc71901d2008-02-25 21:16:03 +0000504 NodeSet DstTmp;
505 Expr* Callee = CE->getCallee()->IgnoreParenCasts();
506
507 VisitLVal(Callee, Pred, DstTmp);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000508
509 // Finally, evaluate the function call.
Ted Kremenek07baa252008-02-21 18:02:17 +0000510 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
511
Ted Kremenekd9268e32008-02-19 01:44:53 +0000512 StateTy St = (*DI)->getState();
Ted Kremenekc71901d2008-02-25 21:16:03 +0000513 RVal L = GetLVal(St, Callee);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000514
515 // Check for uninitialized control-flow.
Ted Kremenek07baa252008-02-21 18:02:17 +0000516
517 if (L.isUninit()) {
518
Ted Kremenekd9268e32008-02-19 01:44:53 +0000519 NodeTy* N = Builder->generateNode(CE, St, *DI);
520 N->markAsSink();
521 UninitBranches.insert(N);
522 continue;
523 }
524
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000525 if (L.isUnknown()) {
526 // Invalidate all arguments passed in by reference (LVals).
527 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
528 I != E; ++I) {
529 RVal V = GetRVal(St, *I);
Ted Kremenek07baa252008-02-21 18:02:17 +0000530
Ted Kremenekb9a20e02008-02-21 19:46:04 +0000531 if (isa<LVal>(V))
532 St = SetRVal(St, cast<LVal>(V), UnknownVal());
533 }
534 }
535 else
536 St = EvalCall(CE, cast<LVal>(L), (*DI)->getState());
537
538 Nodify(Dst, CE, *DI, St);
Ted Kremenekd9268e32008-02-19 01:44:53 +0000539 }
540}
541
Ted Kremenek07baa252008-02-21 18:02:17 +0000542void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek54eddae2008-01-24 02:02:54 +0000543
Ted Kremenek5f585b02008-02-19 18:52:54 +0000544 NodeSet S1;
Ted Kremenek07baa252008-02-21 18:02:17 +0000545 Visit(Ex, Pred, S1);
Ted Kremenek54eddae2008-01-24 02:02:54 +0000546
Ted Kremenek5f585b02008-02-19 18:52:54 +0000547 QualType T = CastE->getType();
548
Ted Kremenek5a9b6aa2008-02-19 18:47:04 +0000549 // Check for redundant casts or casting to "void"
550 if (T->isVoidType() ||
Ted Kremenek07baa252008-02-21 18:02:17 +0000551 Ex->getType() == T ||
552 (T->isPointerType() && Ex->getType()->isFunctionType())) {
Ted Kremenek5f585b02008-02-19 18:52:54 +0000553
Ted Kremenek07baa252008-02-21 18:02:17 +0000554 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5f585b02008-02-19 18:52:54 +0000555 Dst.Add(*I1);
556
Ted Kremenek54eddae2008-01-24 02:02:54 +0000557 return;
558 }
559
Ted Kremenek07baa252008-02-21 18:02:17 +0000560 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek54eddae2008-01-24 02:02:54 +0000561 NodeTy* N = *I1;
562 StateTy St = N->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000563 RVal V = GetRVal(St, Ex);
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000564 Nodify(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek54eddae2008-01-24 02:02:54 +0000565 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000566}
567
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000568void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000569 GRExprEngine::NodeSet& Dst) {
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000570
571 StateTy St = Pred->getState();
572
573 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000574 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremeneke0258002008-02-19 00:29:51 +0000575
576 // FIXME: Add support for local arrays.
577 if (VD->getType()->isArrayType())
578 continue;
579
Ted Kremenek1fc70b32008-02-26 00:20:52 +0000580 // FIXME: static variables have an initializer, but the second
581 // time a function is called those values may not be current.
Ted Kremenek07baa252008-02-21 18:02:17 +0000582 const Expr* Ex = VD->getInit();
583
584 St = SetRVal(St, lval::DeclVal(VD),
585 Ex ? GetRVal(St, Ex) : UninitializedVal());
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000586 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000587
588 Nodify(Dst, DS, Pred, St);
589
Ted Kremenek07baa252008-02-21 18:02:17 +0000590 if (Dst.empty()) { Dst.Add(Pred); }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000591}
Ted Kremenek54eddae2008-01-24 02:02:54 +0000592
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000593
Ted Kremenek07baa252008-02-21 18:02:17 +0000594void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000595 NodeTy* Pred, NodeSet& Dst) {
596
Ted Kremenek99ecce72008-02-26 19:05:15 +0000597 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
598
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000599 StateTy St = Pred->getState();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000600 RVal X = GetBlkExprRVal(St, Ex);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000601
Ted Kremenek99ecce72008-02-26 19:05:15 +0000602 assert (X.isUninit());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000603
Ted Kremenek99ecce72008-02-26 19:05:15 +0000604 Expr* SE = (Expr*) cast<UninitializedVal>(X).getData();
605
606 assert (SE);
607
608 X = GetBlkExprRVal(St, SE);
609 Nodify(Dst, Ex, Pred, SetBlkExprRVal(St, Ex, X));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000610}
611
Ted Kremenekfd85f292008-02-12 19:49:57 +0000612/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek07baa252008-02-21 18:02:17 +0000613void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
614 NodeTy* Pred,
615 NodeSet& Dst) {
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000616
617 assert (Ex->isSizeOf() && "FIXME: AlignOf(Expr) not yet implemented.");
Ted Kremenekfd85f292008-02-12 19:49:57 +0000618
619 // 6.5.3.4 sizeof: "The result type is an integer."
620
Ted Kremenek07baa252008-02-21 18:02:17 +0000621 QualType T = Ex->getArgumentType();
Ted Kremenek9cfda3f2008-02-21 18:43:30 +0000622
Ted Kremenek07baa252008-02-21 18:02:17 +0000623
Ted Kremenek571f5192008-02-21 18:15:29 +0000624 // FIXME: Add support for VLAs.
Eli Friedman82ce9a82008-02-15 12:28:27 +0000625 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekfd85f292008-02-12 19:49:57 +0000626 return;
627
Ted Kremenek571f5192008-02-21 18:15:29 +0000628
629 uint64_t size = 1; // Handle sizeof(void)
630
631 if (T != getContext().VoidTy) {
632 SourceLocation Loc = Ex->getExprLoc();
633 size = getContext().getTypeSize(T, Loc) / 8;
634 }
Ted Kremenekfd85f292008-02-12 19:49:57 +0000635
Ted Kremenek07baa252008-02-21 18:02:17 +0000636 Nodify(Dst, Ex, Pred,
637 SetRVal(Pred->getState(), Ex,
Ted Kremenek571f5192008-02-21 18:15:29 +0000638 NonLVal::MakeVal(ValMgr, size, Ex->getType())));
Ted Kremenekfd85f292008-02-12 19:49:57 +0000639
640}
641
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000642void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, NodeSet& Dst) {
643
Ted Kremenek07baa252008-02-21 18:02:17 +0000644 Expr* Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000645
646 NodeSet DstTmp;
647
Ted Kremenek56a89992008-02-26 03:44:25 +0000648 if (isa<DeclRefExpr>(Ex))
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000649 DstTmp.Add(Pred);
650 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000651 Visit(Ex, Pred, DstTmp);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000652
Ted Kremenek07baa252008-02-21 18:02:17 +0000653 for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000654
655 NodeTy* N = *I;
656 StateTy St = N->getState();
657
658 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
659
Ted Kremenek07baa252008-02-21 18:02:17 +0000660 RVal V = GetRVal(St, Ex);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000661
Ted Kremenek07baa252008-02-21 18:02:17 +0000662 // Check for dereferences of uninitialized values.
663
664 if (V.isUninit()) {
665
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000666 NodeTy* Succ = Builder->generateNode(U, St, N);
667
668 if (Succ) {
669 Succ->markAsSink();
670 UninitDeref.insert(Succ);
671 }
672
673 continue;
674 }
675
Ted Kremenek07baa252008-02-21 18:02:17 +0000676 // Check for dereferences of unknown values. Treat as No-Ops.
677
678 if (V.isUnknown()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000679 Dst.Add(N);
680 continue;
681 }
682
683 // After a dereference, one of two possible situations arise:
684 // (1) A crash, because the pointer was NULL.
685 // (2) The pointer is not NULL, and the dereference works.
686 //
687 // We add these assumptions.
688
Ted Kremenek07baa252008-02-21 18:02:17 +0000689 LVal LV = cast<LVal>(V);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000690 bool isFeasibleNotNull;
691
692 // "Assume" that the pointer is Not-NULL.
Ted Kremenek07baa252008-02-21 18:02:17 +0000693
694 StateTy StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000695
696 if (isFeasibleNotNull) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000697
698 // FIXME: Currently symbolic analysis "generates" new symbols
699 // for the contents of values. We need a better approach.
700
Ted Kremenek07baa252008-02-21 18:02:17 +0000701 Nodify(Dst, U, N, SetRVal(StNotNull, U,
702 GetRVal(StNotNull, LV, U->getType())));
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000703 }
704
705 bool isFeasibleNull;
706
Ted Kremenek07baa252008-02-21 18:02:17 +0000707 // Now "assume" that the pointer is NULL.
708
709 StateTy StNull = Assume(St, LV, false, isFeasibleNull);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000710
711 if (isFeasibleNull) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000712
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000713 // We don't use "Nodify" here because the node will be a sink
714 // and we have no intention of processing it later.
Ted Kremenek07baa252008-02-21 18:02:17 +0000715
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000716 NodeTy* NullNode = Builder->generateNode(U, StNull, N);
717
718 if (NullNode) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000719
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000720 NullNode->markAsSink();
721
Ted Kremenek07baa252008-02-21 18:02:17 +0000722 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
723 else ExplicitNullDeref.insert(NullNode);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000724 }
725 }
726 }
727}
728
Ted Kremenek07baa252008-02-21 18:02:17 +0000729void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
730 NodeSet& Dst) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000731
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000732 NodeSet S1;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000733
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000734 assert (U->getOpcode() != UnaryOperator::Deref);
Ted Kremenek07baa252008-02-21 18:02:17 +0000735 assert (U->getOpcode() != UnaryOperator::SizeOf);
736 assert (U->getOpcode() != UnaryOperator::AlignOf);
737
738 bool use_GetLVal = false;
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000739
740 switch (U->getOpcode()) {
741 case UnaryOperator::PostInc:
742 case UnaryOperator::PostDec:
743 case UnaryOperator::PreInc:
744 case UnaryOperator::PreDec:
745 case UnaryOperator::AddrOf:
Ted Kremenek07baa252008-02-21 18:02:17 +0000746 // Evalue subexpression as an LVal.
747 use_GetLVal = true;
748 VisitLVal(U->getSubExpr(), Pred, S1);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000749 break;
750
751 default:
752 Visit(U->getSubExpr(), Pred, S1);
753 break;
754 }
755
Ted Kremenek07baa252008-02-21 18:02:17 +0000756 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000757
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000758 NodeTy* N1 = *I1;
759 StateTy St = N1->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +0000760
761 RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
762 GetRVal(St, U->getSubExpr());
763
764 if (SubV.isUnknown()) {
765 Dst.Add(N1);
766 continue;
767 }
768
769 if (SubV.isUninit()) {
770 Nodify(Dst, U, N1, SetRVal(St, U, SubV));
771 continue;
772 }
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000773
Ted Kremenek22640ce2008-02-15 22:09:30 +0000774 if (U->isIncrementDecrementOp()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000775
776 // Handle ++ and -- (both pre- and post-increment).
777
Ted Kremenek07baa252008-02-21 18:02:17 +0000778 LVal SubLV = cast<LVal>(SubV);
779 RVal V = GetRVal(St, SubLV, U->getType());
780
Ted Kremenekb8782e12008-02-21 19:15:37 +0000781 if (V.isUnknown()) {
782 Dst.Add(N1);
783 continue;
784 }
Ted Kremenek07baa252008-02-21 18:02:17 +0000785
786 // Propagate uninitialized values.
787 if (V.isUninit()) {
788 Nodify(Dst, U, N1, SetRVal(St, U, V));
789 continue;
790 }
791
Ted Kremenekb1669d42008-02-21 19:29:23 +0000792 // Handle all other values.
Ted Kremenek22640ce2008-02-15 22:09:30 +0000793
794 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
795 : BinaryOperator::Sub;
796
Ted Kremenekb1669d42008-02-21 19:29:23 +0000797 RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
Ted Kremenek22640ce2008-02-15 22:09:30 +0000798
799 if (U->isPostfix())
Ted Kremenek07baa252008-02-21 18:02:17 +0000800 St = SetRVal(SetRVal(St, U, V), SubLV, Result);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000801 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000802 St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000803
Ted Kremenek07baa252008-02-21 18:02:17 +0000804 Nodify(Dst, U, N1, St);
Ted Kremenek22640ce2008-02-15 22:09:30 +0000805 continue;
806 }
807
808 // Handle all other unary operators.
809
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000810 switch (U->getOpcode()) {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000811
Ted Kremenek07baa252008-02-21 18:02:17 +0000812 case UnaryOperator::Minus:
813 St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
Ted Kremenekcacd7362008-01-24 08:20:02 +0000814 break;
Ted Kremenekcacd7362008-01-24 08:20:02 +0000815
Ted Kremenek07baa252008-02-21 18:02:17 +0000816 case UnaryOperator::Not:
817 St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
Ted Kremenek0cd96352008-02-20 04:12:31 +0000818 break;
Ted Kremenek0cd96352008-02-20 04:12:31 +0000819
Ted Kremenek07baa252008-02-21 18:02:17 +0000820 case UnaryOperator::LNot:
Ted Kremenek2cb46642008-02-04 16:58:30 +0000821
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000822 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
823 //
824 // Note: technically we do "E == 0", but this is the same in the
825 // transfer functions as "0 == E".
Ted Kremenek07baa252008-02-21 18:02:17 +0000826
827 if (isa<LVal>(SubV)) {
828 lval::ConcreteInt V(ValMgr.getZeroWithPtrWidth());
829 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
830 St = SetRVal(St, U, Result);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000831 }
832 else {
Ted Kremeneka0894672008-02-22 00:42:36 +0000833 Expr* Ex = U->getSubExpr();
834 nonlval::ConcreteInt V(ValMgr.getValue(0, Ex->getType()));
Ted Kremenek07baa252008-02-21 18:02:17 +0000835 RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
836 St = SetRVal(St, U, Result);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000837 }
838
839 break;
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000840
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000841 case UnaryOperator::AddrOf: {
Ted Kremenek07baa252008-02-21 18:02:17 +0000842 assert (isa<LVal>(SubV));
843 St = SetRVal(St, U, SubV);
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000844 break;
845 }
Ted Kremenek80d52d02008-02-07 05:48:01 +0000846
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000847 default: ;
848 assert (false && "Not implemented.");
Ted Kremenek07baa252008-02-21 18:02:17 +0000849 }
850
851 Nodify(Dst, U, N1, St);
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000852 }
853}
854
Ted Kremenek07baa252008-02-21 18:02:17 +0000855void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
856 NodeSet& Dst) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000857
Ted Kremenek07baa252008-02-21 18:02:17 +0000858 QualType T = U->getSubExpr()->getType();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000859
Ted Kremenek07baa252008-02-21 18:02:17 +0000860 // FIXME: Add support for VLAs.
861 if (!T.getTypePtr()->isConstantSizeType())
862 return;
863
864 SourceLocation Loc = U->getExprLoc();
865 uint64_t size = getContext().getTypeSize(T, Loc) / 8;
866 StateTy St = Pred->getState();
867 St = SetRVal(St, U, NonLVal::MakeVal(ValMgr, size, U->getType(), Loc));
868
869 Nodify(Dst, U, Pred, St);
870}
871
872void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
873
874 assert (Ex != CurrentStmt && !getCFG().isBlkExpr(Ex));
875
876 Ex = Ex->IgnoreParens();
877
Ted Kremenekef0007f2008-02-26 02:15:56 +0000878 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex)) {
879
880 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
881 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) {
882
883 StateTy StOld = Pred->getState();
884 StateTy St = Symbolicate(StOld, VD);
885
886 if (!(St == StOld)) {
887 Nodify(Dst, Ex, Pred, St);
888 return;
889 }
890 }
891
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000892 Dst.Add(Pred);
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000893 return;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000894 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000895
Ted Kremenek07baa252008-02-21 18:02:17 +0000896 if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex)) {
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000897 if (U->getOpcode() == UnaryOperator::Deref) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000898 Ex = U->getSubExpr()->IgnoreParens();
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000899
Ted Kremenek07baa252008-02-21 18:02:17 +0000900 if (isa<DeclRefExpr>(Ex))
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000901 Dst.Add(Pred);
902 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000903 Visit(Ex, Pred, Dst);
Ted Kremenekb996ebc2008-02-20 04:02:35 +0000904
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000905 return;
906 }
907 }
908
Ted Kremenek07baa252008-02-21 18:02:17 +0000909 Visit(Ex, Pred, Dst);
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000910}
911
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000912void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekaee121c2008-02-13 23:08:21 +0000913 GRExprEngine::NodeTy* Pred,
914 GRExprEngine::NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000915 NodeSet S1;
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000916
917 if (B->isAssignmentOp())
Ted Kremenek07baa252008-02-21 18:02:17 +0000918 VisitLVal(B->getLHS(), Pred, S1);
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000919 else
920 Visit(B->getLHS(), Pred, S1);
Ted Kremenekafba4b22008-01-16 00:53:15 +0000921
Ted Kremenekf031b872008-01-23 19:59:44 +0000922 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000923
Ted Kremenekf031b872008-01-23 19:59:44 +0000924 NodeTy* N1 = *I1;
Ted Kremeneke860db82008-01-17 00:52:48 +0000925
Ted Kremenekf031b872008-01-23 19:59:44 +0000926 // When getting the value for the LHS, check if we are in an assignment.
Ted Kremenek07baa252008-02-21 18:02:17 +0000927 // In such cases, we want to (initially) treat the LHS as an LVal,
928 // so we use GetLVal instead of GetRVal so that DeclRefExpr's are
929 // evaluated to LValDecl's instead of to an NonLVal.
930
931 RVal LeftV = B->isAssignmentOp() ? GetLVal(N1->getState(), B->getLHS())
932 : GetRVal(N1->getState(), B->getLHS());
Ted Kremenekafba4b22008-01-16 00:53:15 +0000933
Ted Kremenek07baa252008-02-21 18:02:17 +0000934 // Visit the RHS...
935
936 NodeSet S2;
Ted Kremenekf031b872008-01-23 19:59:44 +0000937 Visit(B->getRHS(), N1, S2);
Ted Kremenek07baa252008-02-21 18:02:17 +0000938
939 // Process the binary operator.
Ted Kremenekf031b872008-01-23 19:59:44 +0000940
Ted Kremenek07baa252008-02-21 18:02:17 +0000941 for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000942
Ted Kremenekf031b872008-01-23 19:59:44 +0000943 NodeTy* N2 = *I2;
Ted Kremenek07baa252008-02-21 18:02:17 +0000944 StateTy St = N2->getState();
Ted Kremenek2c369792008-02-25 18:42:54 +0000945 Expr* RHS = B->getRHS();
946 RVal RightV = GetRVal(St, RHS);
Ted Kremenekf031b872008-01-23 19:59:44 +0000947
Ted Kremenek15cb0782008-02-06 22:50:25 +0000948 BinaryOperator::Opcode Op = B->getOpcode();
949
Ted Kremenek2c369792008-02-25 18:42:54 +0000950 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
951 && RHS->getType()->isIntegerType()) {
Ted Kremenekef0007f2008-02-26 02:15:56 +0000952
953 // Check if the denominator is uninitialized.
Ted Kremenek2c369792008-02-25 18:42:54 +0000954
Ted Kremenek5a600862008-02-26 22:27:51 +0000955 if (!RightV.isUnknown()) {
956
957 if (RightV.isUninit()) {
958 NodeTy* DivUninit = Builder->generateNode(B, St, N2);
959
960 if (DivUninit) {
961 DivUninit->markAsSink();
962 BadDivides.insert(DivUninit);
963 }
964
965 continue;
966 }
967
968 // Check for divide/remainder-by-zero.
969 //
970 // First, "assume" that the denominator is 0 or uninitialized.
Ted Kremenekef0007f2008-02-26 02:15:56 +0000971
Ted Kremenek5a600862008-02-26 22:27:51 +0000972 bool isFeasible = false;
973 StateTy ZeroSt = Assume(St, RightV, false, isFeasible);
974
975 if (isFeasible) {
976 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
977
978 if (DivZeroNode) {
979 DivZeroNode->markAsSink();
980 BadDivides.insert(DivZeroNode);
981 }
Ted Kremenekef0007f2008-02-26 02:15:56 +0000982 }
983
Ted Kremenek5a600862008-02-26 22:27:51 +0000984 // Second, "assume" that the denominator cannot be 0.
Ted Kremenekef0007f2008-02-26 02:15:56 +0000985
Ted Kremenek5a600862008-02-26 22:27:51 +0000986 isFeasible = false;
987 St = Assume(St, RightV, true, isFeasible);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +0000988
Ted Kremenek5a600862008-02-26 22:27:51 +0000989 if (!isFeasible)
990 continue;
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +0000991 }
992
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +0000993 // Fall-through. The logic below processes the divide.
994 }
995
Ted Kremenek15cb0782008-02-06 22:50:25 +0000996 if (Op <= BinaryOperator::Or) {
997
Ted Kremenek07baa252008-02-21 18:02:17 +0000998 // Process non-assignements except commas or short-circuited
999 // logical expressions (LAnd and LOr).
1000
1001 RVal Result = EvalBinOp(Op, LeftV, RightV);
1002
1003 if (Result.isUnknown()) {
1004 Dst.Add(N2);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001005 continue;
1006 }
1007
Ted Kremenek07baa252008-02-21 18:02:17 +00001008 Nodify(Dst, B, N2, SetRVal(St, B, Result));
Ted Kremenek15cb0782008-02-06 22:50:25 +00001009 continue;
1010 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001011
1012 // Process assignments.
1013
1014 switch (Op) {
1015
Ted Kremenekf031b872008-01-23 19:59:44 +00001016 case BinaryOperator::Assign: {
Ted Kremenek07baa252008-02-21 18:02:17 +00001017
1018 // Simple assignments.
Ted Kremenekbf988d02008-02-19 00:22:37 +00001019
Ted Kremenek07baa252008-02-21 18:02:17 +00001020 if (LeftV.isUninit()) {
Ted Kremenekbf988d02008-02-19 00:22:37 +00001021 HandleUninitializedStore(B, N2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001022 continue;
1023 }
1024
1025 if (LeftV.isUnknown()) {
1026 St = SetRVal(St, B, RightV);
1027 break;
1028 }
Ted Kremenekbf988d02008-02-19 00:22:37 +00001029
Ted Kremenek07baa252008-02-21 18:02:17 +00001030 St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV);
Ted Kremenekf031b872008-01-23 19:59:44 +00001031 break;
1032 }
1033
Ted Kremenek07baa252008-02-21 18:02:17 +00001034 // Compound assignment operators.
Ted Kremenek19072e02008-01-29 19:43:15 +00001035
Ted Kremenek07baa252008-02-21 18:02:17 +00001036 default: {
Ted Kremenekbf988d02008-02-19 00:22:37 +00001037
Ted Kremenek5a600862008-02-26 22:27:51 +00001038 assert (B->isCompoundAssignmentOp());
1039
1040 if (Op >= BinaryOperator::AndAssign)
1041 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
1042 else
1043 ((int&) Op) -= BinaryOperator::MulAssign;
1044
1045 // Check if the LHS is uninitialized.
Ted Kremenek07baa252008-02-21 18:02:17 +00001046
1047 if (LeftV.isUninit()) {
Ted Kremenekbf988d02008-02-19 00:22:37 +00001048 HandleUninitializedStore(B, N2);
Ted Kremenek07baa252008-02-21 18:02:17 +00001049 continue;
1050 }
1051
1052 if (LeftV.isUnknown()) {
1053
1054 // While we do not know the location to store RightV,
1055 // the entire expression does evaluate to RightV.
1056
1057 if (RightV.isUnknown()) {
1058 Dst.Add(N2);
1059 continue;
1060 }
1061
1062 St = SetRVal(St, B, RightV);
Ted Kremenekbf988d02008-02-19 00:22:37 +00001063 break;
1064 }
1065
Ted Kremenek07baa252008-02-21 18:02:17 +00001066 // At this pointer we know that the LHS evaluates to an LVal
1067 // that is neither "Unknown" or "Unintialized."
1068
1069 LVal LeftLV = cast<LVal>(LeftV);
1070
Ted Kremenek07baa252008-02-21 18:02:17 +00001071 // Fetch the value of the LHS (the value of the variable, etc.).
1072
1073 RVal V = GetRVal(N1->getState(), LeftLV, B->getLHS()->getType());
1074
Ted Kremenek5a600862008-02-26 22:27:51 +00001075 // Propagate uninitialized value (left-side). We
1076 // propogate uninitialized values for the RHS below when
1077 // we also check for divide-by-zero.
Ted Kremenek07baa252008-02-21 18:02:17 +00001078
1079 if (V.isUninit()) {
1080 St = SetRVal(St, B, V);
1081 break;
1082 }
1083
1084 // Propagate unknown values.
1085
Ted Kremenekb8782e12008-02-21 19:15:37 +00001086 if (V.isUnknown()) {
1087 Dst.Add(N2);
1088 continue;
1089 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001090
1091 if (RightV.isUnknown()) {
1092 St = SetRVal(SetRVal(St, LeftLV, RightV), B, RightV);
1093 break;
1094 }
1095
Ted Kremenek5a600862008-02-26 22:27:51 +00001096 // At this point:
1097 //
1098 // The LHS is not Uninit/Unknown.
1099 // The RHS is not Unknown.
Ted Kremenek15cb0782008-02-06 22:50:25 +00001100
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001101 // Get the computation type.
1102 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
1103
1104 // Perform promotions.
1105 V = EvalCast(V, CTy);
Ted Kremenek666ab922008-02-21 18:46:24 +00001106 RightV = EvalCast(RightV, CTy);
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001107
1108 // Evaluate operands and promote to result type.
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001109
Ted Kremenek2c369792008-02-25 18:42:54 +00001110 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
1111 && RHS->getType()->isIntegerType()) {
1112
Ted Kremenekef0007f2008-02-26 02:15:56 +00001113 // Check if the denominator is uninitialized.
1114
1115 if (RightV.isUninit()) {
1116 NodeTy* DivUninit = Builder->generateNode(B, St, N2);
1117
1118 if (DivUninit) {
1119 DivUninit->markAsSink();
1120 BadDivides.insert(DivUninit);
1121 }
1122
1123 continue;
1124 }
Ted Kremenek5a600862008-02-26 22:27:51 +00001125
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001126 // First, "assume" that the denominator is 0.
1127
1128 bool isFeasible = false;
1129 StateTy ZeroSt = Assume(St, RightV, false, isFeasible);
1130
1131 if (isFeasible) {
1132 NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
1133
1134 if (DivZeroNode) {
1135 DivZeroNode->markAsSink();
Ted Kremenekef0007f2008-02-26 02:15:56 +00001136 BadDivides.insert(DivZeroNode);
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001137 }
1138 }
1139
1140 // Second, "assume" that the denominator cannot be 0.
1141
1142 isFeasible = false;
1143 St = Assume(St, RightV, true, isFeasible);
1144
1145 if (!isFeasible)
1146 continue;
1147
1148 // Fall-through. The logic below processes the divide.
1149 }
Ted Kremenek5a600862008-02-26 22:27:51 +00001150 else {
1151
1152 // Propagate uninitialized values (right-side).
1153
1154 if (RightV.isUninit()) {
1155 St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
1156 break;
1157 }
1158
1159 }
Ted Kremenek6ff5f7f2008-02-25 17:51:31 +00001160
Ted Kremenek9cfda3f2008-02-21 18:43:30 +00001161 RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek07baa252008-02-21 18:02:17 +00001162 St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
Ted Kremenek15cb0782008-02-06 22:50:25 +00001163 }
Ted Kremenekf031b872008-01-23 19:59:44 +00001164 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001165
1166 Nodify(Dst, B, N2, St);
Ted Kremenekafba4b22008-01-16 00:53:15 +00001167 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00001168 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00001169}
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001170
Ted Kremenek07baa252008-02-21 18:02:17 +00001171void GRExprEngine::HandleUninitializedStore(Stmt* S, NodeTy* Pred) {
Ted Kremenekbf988d02008-02-19 00:22:37 +00001172 NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred);
1173 N->markAsSink();
1174 UninitStores.insert(N);
1175}
Ted Kremenekbe962452008-01-16 19:42:59 +00001176
Ted Kremenekbf988d02008-02-19 00:22:37 +00001177void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +00001178
1179 // FIXME: add metadata to the CFG so that we can disable
1180 // this check when we KNOW that there is no block-level subexpression.
1181 // The motivation is that this check requires a hashtable lookup.
1182
1183 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
1184 Dst.Add(Pred);
1185 return;
1186 }
1187
1188 switch (S->getStmtClass()) {
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001189
1190 default:
1191 // Cases we intentionally have "default" handle:
Ted Kremenek9b496f92008-02-26 19:17:09 +00001192 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001193
1194 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
1195 break;
1196
Ted Kremenek744a7862008-02-08 20:29:23 +00001197 case Stmt::BinaryOperatorClass: {
1198 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001199
Ted Kremenek744a7862008-02-08 20:29:23 +00001200 if (B->isLogicalOp()) {
1201 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001202 break;
1203 }
Ted Kremenek744a7862008-02-08 20:29:23 +00001204 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek106f37c2008-02-08 07:05:39 +00001205 StateTy St = Pred->getState();
Ted Kremenek07baa252008-02-21 18:02:17 +00001206 Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
Ted Kremenek106f37c2008-02-08 07:05:39 +00001207 break;
1208 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001209
1210 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1211 break;
1212 }
Ted Kremenekd9268e32008-02-19 01:44:53 +00001213
1214 case Stmt::CallExprClass: {
1215 CallExpr* C = cast<CallExpr>(S);
1216 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
1217 break;
1218 }
Ted Kremenekfd85f292008-02-12 19:49:57 +00001219
1220 case Stmt::CastExprClass: {
1221 CastExpr* C = cast<CastExpr>(S);
1222 VisitCast(C, C->getSubExpr(), Pred, Dst);
1223 break;
Ted Kremenek744a7862008-02-08 20:29:23 +00001224 }
Ted Kremenek9b496f92008-02-26 19:17:09 +00001225
Ted Kremenek07baa252008-02-21 18:02:17 +00001226 // FIXME: ChooseExpr is really a constant. We need to fix
1227 // the CFG do not model them as explicit control-flow.
1228
Ted Kremenekfd85f292008-02-12 19:49:57 +00001229 case Stmt::ChooseExprClass: { // __builtin_choose_expr
1230 ChooseExpr* C = cast<ChooseExpr>(S);
1231 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1232 break;
1233 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +00001234
Ted Kremenek9914e9c2008-01-23 23:38:00 +00001235 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekf031b872008-01-23 19:59:44 +00001236 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
1237 break;
1238
Ted Kremenekfd85f292008-02-12 19:49:57 +00001239 case Stmt::ConditionalOperatorClass: { // '?' operator
1240 ConditionalOperator* C = cast<ConditionalOperator>(S);
1241 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
1242 break;
1243 }
1244
1245 case Stmt::DeclRefExprClass:
1246 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
1247 break;
1248
1249 case Stmt::DeclStmtClass:
1250 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
1251 break;
1252
1253 case Stmt::ImplicitCastExprClass: {
1254 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
1255 VisitCast(C, C->getSubExpr(), Pred, Dst);
1256 break;
1257 }
1258
1259 case Stmt::ParenExprClass:
1260 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
1261 break;
1262
1263 case Stmt::SizeOfAlignOfTypeExprClass:
1264 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
1265 break;
1266
Ted Kremenek106f37c2008-02-08 07:05:39 +00001267 case Stmt::StmtExprClass: {
Ted Kremenek744a7862008-02-08 20:29:23 +00001268 StmtExpr* SE = cast<StmtExpr>(S);
1269
Ted Kremenek106f37c2008-02-08 07:05:39 +00001270 StateTy St = Pred->getState();
Ted Kremenek744a7862008-02-08 20:29:23 +00001271 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
Ted Kremenek07baa252008-02-21 18:02:17 +00001272 Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
Ted Kremenek106f37c2008-02-08 07:05:39 +00001273 break;
1274 }
1275
Ted Kremenek07baa252008-02-21 18:02:17 +00001276 // FIXME: We may wish to always bind state to ReturnStmts so
1277 // that users can quickly query what was the state at the
1278 // exit points of a function.
1279
Ted Kremenekfd85f292008-02-12 19:49:57 +00001280 case Stmt::ReturnStmtClass: {
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001281 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
1282 Visit(R, Pred, Dst);
1283 else
1284 Dst.Add(Pred);
1285
1286 break;
Ted Kremenekfd85f292008-02-12 19:49:57 +00001287 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00001288
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001289 case Stmt::UnaryOperatorClass: {
1290 UnaryOperator* U = cast<UnaryOperator>(S);
1291
Ted Kremenek07baa252008-02-21 18:02:17 +00001292 switch (U->getOpcode()) {
1293 case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
1294 case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
1295 case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
1296 default: VisitUnaryOperator(U, Pred, Dst); break;
1297 }
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001298
Ted Kremenekb9c30e32008-01-24 20:55:43 +00001299 break;
Ted Kremenekb996ebc2008-02-20 04:02:35 +00001300 }
Ted Kremenekab8ed952008-01-17 18:25:22 +00001301 }
Ted Kremenekbe962452008-01-16 19:42:59 +00001302}
1303
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001304//===----------------------------------------------------------------------===//
Ted Kremenek90960972008-01-30 23:03:39 +00001305// "Assume" logic.
1306//===----------------------------------------------------------------------===//
1307
Ted Kremenek07baa252008-02-21 18:02:17 +00001308GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LVal Cond,
Ted Kremenekbc965a62008-02-18 22:57:02 +00001309 bool Assumption,
Ted Kremenek07baa252008-02-21 18:02:17 +00001310 bool& isFeasible) {
Ted Kremenek6e24a802008-02-01 06:36:40 +00001311 switch (Cond.getSubKind()) {
1312 default:
Ted Kremenek07baa252008-02-21 18:02:17 +00001313 assert (false && "'Assume' not implemented for this LVal.");
Ted Kremenek6e24a802008-02-01 06:36:40 +00001314 return St;
1315
Ted Kremenek13f31562008-02-06 00:54:14 +00001316 case lval::SymbolValKind:
1317 if (Assumption)
1318 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1319 ValMgr.getZeroWithPtrWidth(), isFeasible);
1320 else
1321 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
1322 ValMgr.getZeroWithPtrWidth(), isFeasible);
1323
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001324
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001325 case lval::DeclValKind:
Ted Kremenek38706192008-02-22 00:54:56 +00001326 case lval::FuncValKind:
1327 case lval::GotoLabelKind:
Ted Kremenek6e24a802008-02-01 06:36:40 +00001328 isFeasible = Assumption;
1329 return St;
Ted Kremenek13f31562008-02-06 00:54:14 +00001330
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001331 case lval::ConcreteIntKind: {
1332 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek6e24a802008-02-01 06:36:40 +00001333 isFeasible = b ? Assumption : !Assumption;
1334 return St;
1335 }
1336 }
Ted Kremenek90960972008-01-30 23:03:39 +00001337}
1338
Ted Kremenek07baa252008-02-21 18:02:17 +00001339GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLVal Cond,
Ted Kremenek13f31562008-02-06 00:54:14 +00001340 bool Assumption,
Ted Kremenek07baa252008-02-21 18:02:17 +00001341 bool& isFeasible) {
Ted Kremenek90960972008-01-30 23:03:39 +00001342 switch (Cond.getSubKind()) {
1343 default:
Ted Kremenek07baa252008-02-21 18:02:17 +00001344 assert (false && "'Assume' not implemented for this NonLVal.");
Ted Kremenek90960972008-01-30 23:03:39 +00001345 return St;
1346
Ted Kremenekab359c12008-02-06 17:32:17 +00001347
1348 case nonlval::SymbolValKind: {
Ted Kremenek5bde36b2008-02-12 21:37:25 +00001349 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekab359c12008-02-06 17:32:17 +00001350 SymbolID sym = SV.getSymbol();
1351
1352 if (Assumption)
1353 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1354 isFeasible);
1355 else
1356 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1357 isFeasible);
1358 }
1359
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001360 case nonlval::SymIntConstraintValKind:
1361 return
1362 AssumeSymInt(St, Assumption,
1363 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1364 isFeasible);
1365
Ted Kremenek1b63a3b2008-02-05 21:52:21 +00001366 case nonlval::ConcreteIntKind: {
1367 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek90960972008-01-30 23:03:39 +00001368 isFeasible = b ? Assumption : !Assumption;
1369 return St;
1370 }
1371 }
1372}
1373
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001374GRExprEngine::StateTy
1375GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001376 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenekbc965a62008-02-18 22:57:02 +00001377
Ted Kremenek13f31562008-02-06 00:54:14 +00001378 // First, determine if sym == X, where X != V.
1379 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1380 isFeasible = *X != V;
1381 return St;
1382 }
1383
1384 // Second, determine if sym != V.
1385 if (St.isNotEqual(sym, V)) {
1386 isFeasible = true;
1387 return St;
1388 }
1389
1390 // If we reach here, sym is not a constant and we don't know if it is != V.
1391 // Make that assumption.
1392
1393 isFeasible = true;
1394 return StateMgr.AddNE(St, sym, V);
1395}
1396
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001397GRExprEngine::StateTy
1398GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001399 const llvm::APSInt& V, bool& isFeasible) {
1400
1401 // First, determine if sym == X, where X != V.
1402 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1403 isFeasible = *X == V;
1404 return St;
1405 }
1406
1407 // Second, determine if sym != V.
1408 if (St.isNotEqual(sym, V)) {
1409 isFeasible = false;
1410 return St;
1411 }
1412
1413 // If we reach here, sym is not a constant and we don't know if it is == V.
1414 // Make that assumption.
1415
1416 isFeasible = true;
1417 return StateMgr.AddEQ(St, sym, V);
1418}
Ted Kremenek90960972008-01-30 23:03:39 +00001419
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001420GRExprEngine::StateTy
1421GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001422 const SymIntConstraint& C, bool& isFeasible) {
1423
1424 switch (C.getOpcode()) {
1425 default:
1426 // No logic yet for other operators.
1427 return St;
1428
1429 case BinaryOperator::EQ:
1430 if (Assumption)
1431 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1432 else
1433 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1434
1435 case BinaryOperator::NE:
1436 if (Assumption)
1437 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1438 else
1439 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1440 }
1441}
1442
Ted Kremenek90960972008-01-30 23:03:39 +00001443//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +00001444// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001445//===----------------------------------------------------------------------===//
1446
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001447#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001448static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek428d39e2008-01-30 23:24:39 +00001449
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001450namespace llvm {
1451template<>
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001452struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001453 public DefaultDOTGraphTraits {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001454
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001455 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001456
1457 Out << "Variables:\\l";
1458
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001459 bool isFirst = true;
1460
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001461 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek08cfd832008-02-08 21:10:02 +00001462 E=St.vb_end(); I!=E;++I) {
1463
1464 if (isFirst)
1465 isFirst = false;
1466 else
1467 Out << "\\l";
1468
1469 Out << ' ' << I.getKey()->getName() << " : ";
1470 I.getData().print(Out);
1471 }
1472
1473 }
1474
Ted Kremenek17c5f112008-02-11 19:21:59 +00001475
Ted Kremenek6d409922008-02-13 18:06:44 +00001476 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001477
1478 bool isFirst = true;
1479
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001480 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremenek17c5f112008-02-11 19:21:59 +00001481 I != E;++I) {
1482
1483 if (isFirst) {
1484 Out << "\\l\\lSub-Expressions:\\l";
1485 isFirst = false;
1486 }
1487 else
1488 Out << "\\l";
1489
1490 Out << " (" << (void*) I.getKey() << ") ";
1491 I.getKey()->printPretty(Out);
1492 Out << " : ";
1493 I.getData().print(Out);
1494 }
1495 }
1496
Ted Kremenek6d409922008-02-13 18:06:44 +00001497 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001498
Ted Kremenek08cfd832008-02-08 21:10:02 +00001499 bool isFirst = true;
1500
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001501 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremenek17c5f112008-02-11 19:21:59 +00001502 I != E; ++I) {
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001503 if (isFirst) {
Ted Kremenek17c5f112008-02-11 19:21:59 +00001504 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001505 isFirst = false;
1506 }
1507 else
1508 Out << "\\l";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001509
Ted Kremenek17c5f112008-02-11 19:21:59 +00001510 Out << " (" << (void*) I.getKey() << ") ";
1511 I.getKey()->printPretty(Out);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001512 Out << " : ";
1513 I.getData().print(Out);
1514 }
1515 }
1516
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001517 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001518 ValueState::ConstEqTy CE = St.getImpl()->ConstEq;
Ted Kremeneke6536692008-02-06 03:56:15 +00001519
1520 if (CE.isEmpty())
1521 return;
1522
1523 Out << "\\l\\|'==' constraints:";
1524
Ted Kremenek07baa252008-02-21 18:02:17 +00001525 for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
Ted Kremeneke6536692008-02-06 03:56:15 +00001526 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1527 }
1528
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001529 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001530 ValueState::ConstNotEqTy NE = St.getImpl()->ConstNotEq;
Ted Kremeneke6536692008-02-06 03:56:15 +00001531
1532 if (NE.isEmpty())
1533 return;
1534
1535 Out << "\\l\\|'!=' constraints:";
1536
Ted Kremenek07baa252008-02-21 18:02:17 +00001537 for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
Ted Kremeneke6536692008-02-06 03:56:15 +00001538 I != EI; ++I){
1539
1540 Out << "\\l $" << I.getKey() << " : ";
1541 bool isFirst = true;
1542
1543 ValueState::IntSetTy::iterator J=I.getData().begin(),
1544 EJ=I.getData().end();
1545 for ( ; J != EJ; ++J) {
1546 if (isFirst) isFirst = false;
1547 else Out << ", ";
1548
1549 Out << (*J)->toString();
1550 }
1551 }
Ted Kremeneka853de62008-02-14 22:54:53 +00001552 }
1553
1554 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1555
1556 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00001557 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek9d4895c2008-02-19 20:53:06 +00001558 GraphPrintCheckerState->isUninitDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00001559 GraphPrintCheckerState->isUninitStore(N) ||
Ted Kremenekf567a822008-02-26 21:31:18 +00001560 GraphPrintCheckerState->isUninitControlFlow(N) ||
1561 GraphPrintCheckerState->isBadDivide(N))
Ted Kremeneka853de62008-02-14 22:54:53 +00001562 return "color=\"red\",style=\"filled\"";
1563
1564 return "";
1565 }
Ted Kremeneke6536692008-02-06 03:56:15 +00001566
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001567 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001568 std::ostringstream Out;
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001569
1570 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001571 ProgramPoint Loc = N->getLocation();
1572
1573 switch (Loc.getKind()) {
1574 case ProgramPoint::BlockEntranceKind:
1575 Out << "Block Entrance: B"
1576 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1577 break;
1578
1579 case ProgramPoint::BlockExitKind:
1580 assert (false);
1581 break;
1582
1583 case ProgramPoint::PostStmtKind: {
1584 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001585 Out << L.getStmt()->getStmtClassName() << ':'
1586 << (void*) L.getStmt() << ' ';
1587
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001588 L.getStmt()->printPretty(Out);
Ted Kremenek80d52d02008-02-07 05:48:01 +00001589
1590 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1591 Out << "\\|Implicit-Null Dereference.\\l";
1592 }
Ted Kremenekae7bdc12008-02-07 06:04:18 +00001593 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1594 Out << "\\|Explicit-Null Dereference.\\l";
1595 }
Ted Kremenek9d4895c2008-02-19 20:53:06 +00001596 else if (GraphPrintCheckerState->isUninitDeref(N)) {
1597 Out << "\\|Dereference of uninitialied value.\\l";
1598 }
Ted Kremenekbf988d02008-02-19 00:22:37 +00001599 else if (GraphPrintCheckerState->isUninitStore(N)) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001600 Out << "\\|Store to Uninitialized LVal.";
Ted Kremenekbf988d02008-02-19 00:22:37 +00001601 }
Ted Kremenekf567a822008-02-26 21:31:18 +00001602 else if (GraphPrintCheckerState->isBadDivide(N)) {
1603 Out << "\\|Divide-by zero or uninitialized value.";
1604 }
Ted Kremenek80d52d02008-02-07 05:48:01 +00001605
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001606 break;
1607 }
1608
1609 default: {
1610 const BlockEdge& E = cast<BlockEdge>(Loc);
1611 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1612 << E.getDst()->getBlockID() << ')';
Ted Kremenek90960972008-01-30 23:03:39 +00001613
1614 if (Stmt* T = E.getSrc()->getTerminator()) {
1615 Out << "\\|Terminator: ";
1616 E.getSrc()->printTerminator(Out);
1617
Ted Kremenekaee121c2008-02-13 23:08:21 +00001618 if (isa<SwitchStmt>(T)) {
1619 Stmt* Label = E.getDst()->getLabel();
1620
1621 if (Label) {
1622 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1623 Out << "\\lcase ";
1624 C->getLHS()->printPretty(Out);
1625
1626 if (Stmt* RHS = C->getRHS()) {
1627 Out << " .. ";
1628 RHS->printPretty(Out);
1629 }
1630
1631 Out << ":";
1632 }
1633 else {
1634 assert (isa<DefaultStmt>(Label));
1635 Out << "\\ldefault:";
1636 }
1637 }
1638 else
1639 Out << "\\l(implicit) default:";
1640 }
1641 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00001642 // FIXME
1643 }
1644 else {
1645 Out << "\\lCondition: ";
1646 if (*E.getSrc()->succ_begin() == E.getDst())
1647 Out << "true";
1648 else
1649 Out << "false";
1650 }
1651
1652 Out << "\\l";
1653 }
Ted Kremenek428d39e2008-01-30 23:24:39 +00001654
1655 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1656 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1657 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001658 }
1659 }
1660
Ted Kremenek2d8dce32008-02-05 07:17:49 +00001661 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001662
Ted Kremenek17c5f112008-02-11 19:21:59 +00001663 N->getState().printDOT(Out);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001664
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001665 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001666 return Out.str();
1667 }
1668};
1669} // end llvm namespace
1670#endif
1671
Ted Kremenek3862eb12008-02-14 22:36:46 +00001672void GRExprEngine::ViewGraph() {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001673#ifndef NDEBUG
Ted Kremenek3862eb12008-02-14 22:36:46 +00001674 GraphPrintCheckerState = this;
1675 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek428d39e2008-01-30 23:24:39 +00001676 GraphPrintCheckerState = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00001677#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001678}