blob: 09ffcd47a5d8322b40e89121f602d6048c69c3c9 [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
27GRExprEngine::SetValue(StateTy St, Expr* S, const RValue& 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 Kremenek7f5ebc72008-02-04 21:59:01 +000036 if (S == CurrentStmt) {
37 isBlkExpr = getCFG().isBlkExpr(S);
38
39 if (!isBlkExpr)
40 return St;
41 }
Ted Kremenek9b32cd02008-02-07 04:16:04 +000042
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000043 return StateMgr.SetValue(St, S, isBlkExpr, V);
44}
45
Ted Kremenek30fa28b2008-02-13 17:41:41 +000046const GRExprEngine::StateTy::BufferTy&
47GRExprEngine::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB,
Ted Kremenekf20c2012008-02-05 19:35:18 +000048 StateTy::BufferTy& RetBuf) {
49
50 assert (RetBuf.empty());
51
52 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
53 RetBuf.push_back(SetValue(St, S, *I));
54
55 return RetBuf;
56}
57
Ted Kremenek30fa28b2008-02-13 17:41:41 +000058GRExprEngine::StateTy
59GRExprEngine::SetValue(StateTy St, const LValue& LV, const RValue& V) {
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000060
Ted Kremenek0428e022008-02-08 03:02:48 +000061 if (LV.isUnknown())
Ted Kremenek7f5ebc72008-02-04 21:59:01 +000062 return St;
63
64 if (!StateCleaned) {
65 St = RemoveDeadBindings(CurrentStmt, St);
66 StateCleaned = true;
67 }
68
69 return StateMgr.SetValue(St, LV, V);
70}
71
Ted Kremenek30fa28b2008-02-13 17:41:41 +000072void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek6ff3cea2008-01-29 23:32:35 +000073 BranchNodeBuilder& builder) {
Ted Kremenek90960972008-01-30 23:03:39 +000074
Ted Kremenek17c5f112008-02-11 19:21:59 +000075 // Remove old bindings for subexpressions.
76 StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenek1f0eb992008-02-05 00:26:40 +000077
Ted Kremenek022b6052008-02-15 22:29:00 +000078 // Check for NULL conditions; e.g. "for(;;)"
79 if (!Condition) {
80 builder.markInfeasible(false);
81
82 // Get the current block counter.
83 GRBlockCounter BC = builder.getBlockCounter();
84 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
85 unsigned NumVisited = BC.getNumVisited(BlockID);
86
87 if (NumVisited < 1) builder.generateNode(PrevState, true);
88 else builder.markInfeasible(true);
89
90 return;
91 }
92
Ted Kremenek90960972008-01-30 23:03:39 +000093 RValue V = GetValue(PrevState, Condition);
94
95 switch (V.getBaseKind()) {
96 default:
97 break;
98
Ted Kremenek0428e022008-02-08 03:02:48 +000099 case RValue::UnknownKind:
Ted Kremenek90960972008-01-30 23:03:39 +0000100 builder.generateNode(PrevState, true);
101 builder.generateNode(PrevState, false);
102 return;
103
104 case RValue::UninitializedKind: {
105 NodeTy* N = builder.generateNode(PrevState, true);
106
107 if (N) {
108 N->markAsSink();
109 UninitBranches.insert(N);
110 }
111
112 builder.markInfeasible(false);
113 return;
114 }
115 }
Ted Kremenek022b6052008-02-15 22:29:00 +0000116
Ted Kremenek4b170e52008-02-12 18:08:17 +0000117 // Get the current block counter.
118 GRBlockCounter BC = builder.getBlockCounter();
Ted Kremenekfd85f292008-02-12 19:49:57 +0000119 unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
120 unsigned NumVisited = BC.getNumVisited(BlockID);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000121
Ted Kremenek4b170e52008-02-12 18:08:17 +0000122 if (isa<nonlval::ConcreteInt>(V) ||
123 BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
124
125 // Process the true branch.
Ted Kremenek90960972008-01-30 23:03:39 +0000126
Ted Kremenek4b170e52008-02-12 18:08:17 +0000127 bool isFeasible = true;
128
129 StateTy St = Assume(PrevState, V, true, isFeasible);
130
131 if (isFeasible)
132 builder.generateNode(St, true);
133 else
134 builder.markInfeasible(true);
Ted Kremenek90960972008-01-30 23:03:39 +0000135 }
Ted Kremenek4b170e52008-02-12 18:08:17 +0000136 else
137 builder.markInfeasible(true);
Ted Kremenek90960972008-01-30 23:03:39 +0000138
Ted Kremenekfd85f292008-02-12 19:49:57 +0000139 BlockID = builder.getTargetBlock(false)->getBlockID();
140 NumVisited = BC.getNumVisited(BlockID);
Ted Kremenek90960972008-01-30 23:03:39 +0000141
Ted Kremenek4b170e52008-02-12 18:08:17 +0000142 if (isa<nonlval::ConcreteInt>(V) ||
143 BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
144
145 // Process the false branch.
146
147 bool isFeasible = false;
148
149 StateTy St = Assume(PrevState, V, false, isFeasible);
150
151 if (isFeasible)
152 builder.generateNode(St, false);
153 else
154 builder.markInfeasible(false);
155 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000156 else
157 builder.markInfeasible(false);
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000158}
159
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000160/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000161/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000162void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000163
164 StateTy St = builder.getState();
165 LValue V = cast<LValue>(GetValue(St, builder.getTarget()));
166
167 // Three possibilities:
168 //
169 // (1) We know the computed label.
170 // (2) The label is NULL (or some other constant), or Uninitialized.
171 // (3) We have no clue about the label. Dispatch to all targets.
172 //
173
174 typedef IndirectGotoNodeBuilder::iterator iterator;
175
176 if (isa<lval::GotoLabel>(V)) {
177 LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
178
179 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek79f63f52008-02-13 17:27:37 +0000180 if (I.getLabel() == L) {
181 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000182 return;
183 }
184 }
185
186 assert (false && "No block with label.");
187 return;
188 }
189
190 if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) {
191 // Dispatch to the first target and mark it as a sink.
Ted Kremenek79f63f52008-02-13 17:27:37 +0000192 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000193 UninitBranches.insert(N);
194 return;
195 }
196
197 // This is really a catch-all. We don't support symbolics yet.
198
199 assert (isa<UnknownVal>(V));
200
201 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek79f63f52008-02-13 17:27:37 +0000202 builder.generateNode(I, St);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000203}
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000204
Ted Kremenekaee121c2008-02-13 23:08:21 +0000205/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
206/// nodes by processing the 'effects' of a switch statement.
207void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
208
209 typedef SwitchNodeBuilder::iterator iterator;
210
211 StateTy St = builder.getState();
Ted Kremenekbc965a62008-02-18 22:57:02 +0000212 Expr* CondE = builder.getCondition();
213 NonLValue CondV = cast<NonLValue>(GetValue(St, CondE));
Ted Kremenekaee121c2008-02-13 23:08:21 +0000214
215 if (isa<UninitializedVal>(CondV)) {
216 NodeTy* N = builder.generateDefaultCaseNode(St, true);
217 UninitBranches.insert(N);
218 return;
219 }
220
221 StateTy DefaultSt = St;
222
223 // While most of this can be assumed (such as the signedness), having it
224 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenekbc965a62008-02-18 22:57:02 +0000225
226 unsigned bits = getContext().getTypeSize(CondE->getType(),
227 CondE->getExprLoc());
228
Ted Kremenekaee121c2008-02-13 23:08:21 +0000229 APSInt V1(bits, false);
230 APSInt V2 = V1;
231
232 for (iterator I=builder.begin(), E=builder.end(); I!=E; ++I) {
233
234 CaseStmt* Case = cast<CaseStmt>(I.getCase());
235
236 // Evaluate the case.
237 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
238 assert (false && "Case condition must evaluate to an integer constant.");
239 return;
240 }
241
242 // Get the RHS of the case, if it exists.
243
244 if (Expr* E = Case->getRHS()) {
245 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
246 assert (false &&
247 "Case condition (RHS) must evaluate to an integer constant.");
248 return ;
249 }
250
251 assert (V1 <= V2);
252 }
253 else V2 = V1;
254
255 // FIXME: Eventually we should replace the logic below with a range
256 // comparison, rather than concretize the values within the range.
257 // This should be easy once we have "ranges" for NonLValues.
258
259 do {
260 nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
261
Ted Kremenekb1934132008-02-14 19:37:24 +0000262 NonLValue Res = EvalBinaryOp(ValMgr, BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000263
264 // Now "assume" that the case matches.
Ted Kremenekbc965a62008-02-18 22:57:02 +0000265 bool isFeasible = false;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000266
Ted Kremenekb1934132008-02-14 19:37:24 +0000267 StateTy StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000268
269 if (isFeasible) {
270 builder.generateCaseStmtNode(I, StNew);
271
272 // If CondV evaluates to a constant, then we know that this
273 // is the *only* case that we can take, so stop evaluating the
274 // others.
275 if (isa<nonlval::ConcreteInt>(CondV))
276 return;
277 }
278
279 // Now "assume" that the case doesn't match. Add this state
280 // to the default state (if it is feasible).
281
Ted Kremenekb1934132008-02-14 19:37:24 +0000282 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000283
284 if (isFeasible)
285 DefaultSt = StNew;
286
287 // Concretize the next value in the range.
288 ++V1;
289
290 } while (V1 < V2);
291 }
292
293 // If we reach here, than we know that the default branch is
294 // possible.
295 builder.generateDefaultCaseNode(DefaultSt);
296}
297
298
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000299void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000300 NodeSet& Dst) {
301
302 bool hasR2;
303 StateTy PrevState = Pred->getState();
304
305 RValue R1 = GetValue(PrevState, B->getLHS());
306 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
Ted Kremenekbf988d02008-02-19 00:22:37 +0000307
308 if (hasR2) {
309 if (isa<UninitializedVal>(R2) || isa<UnknownVal>(R2)) {
310 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
311 return;
312 }
313 }
314 else if (isa<UninitializedVal>(R1) || isa<UnknownVal>(R1)) {
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000315 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
316 return;
317 }
318
319 // R1 is an expression that can evaluate to either 'true' or 'false'.
320 if (B->getOpcode() == BinaryOperator::LAnd) {
321 // hasR2 == 'false' means that LHS evaluated to 'false' and that
322 // we short-circuited, leading to a value of '0' for the '&&' expression.
323 if (hasR2 == false) {
324 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
325 return;
326 }
327 }
328 else {
329 assert (B->getOpcode() == BinaryOperator::LOr);
330 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
331 // we short-circuited, leading to a value of '1' for the '||' expression.
332 if (hasR2 == false) {
333 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
334 return;
335 }
336 }
337
338 // If we reach here we did not short-circuit. Assume R2 == true and
339 // R2 == false.
340
341 bool isFeasible;
342 StateTy St = Assume(PrevState, R2, true, isFeasible);
343
344 if (isFeasible)
345 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
346
347 St = Assume(PrevState, R2, false, isFeasible);
348
349 if (isFeasible)
350 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
351}
352
353
354
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000355void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenek68d70a82008-01-15 23:55:06 +0000356 Builder = &builder;
Ted Kremenekf031b872008-01-23 19:59:44 +0000357
358 StmtEntryNode = builder.getLastNode();
359 CurrentStmt = S;
360 NodeSet Dst;
361 StateCleaned = false;
362
363 Visit(S, StmtEntryNode, Dst);
364
365 // If no nodes were generated, generate a new node that has all the
366 // dead mappings removed.
367 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
368 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
369 builder.generateNode(S, St, StmtEntryNode);
370 }
Ted Kremeneka57214f2008-01-18 00:41:32 +0000371
Ted Kremenekf031b872008-01-23 19:59:44 +0000372 CurrentStmt = NULL;
373 StmtEntryNode = NULL;
374 Builder = NULL;
Ted Kremenek68d70a82008-01-15 23:55:06 +0000375}
376
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000377GRExprEngine::NodeTy*
378GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000379
380 // If the state hasn't changed, don't generate a new node.
Ted Kremenek02b5b402008-02-07 15:20:13 +0000381 if (St == Pred->getState())
Ted Kremenek80d52d02008-02-07 05:48:01 +0000382 return NULL;
Ted Kremenekafba4b22008-01-16 00:53:15 +0000383
Ted Kremenek80d52d02008-02-07 05:48:01 +0000384 NodeTy* N = Builder->generateNode(S, St, Pred);
385 Dst.Add(N);
386 return N;
Ted Kremenekafba4b22008-01-16 00:53:15 +0000387}
Ted Kremenek68d70a82008-01-15 23:55:06 +0000388
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000389void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
Ted Kremenekf20c2012008-02-05 19:35:18 +0000390 const StateTy::BufferTy& SB) {
391
392 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
393 Nodify(Dst, S, Pred, *I);
394}
395
Ted Kremenek6d409922008-02-13 18:06:44 +0000396void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000397 if (D != CurrentStmt) {
398 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
399 return;
400 }
401
402 // If we are here, we are loading the value of the decl and binding
403 // it to the block-level expression.
404
405 StateTy St = Pred->getState();
406
Ted Kremenek22640ce2008-02-15 22:09:30 +0000407 Nodify(Dst, D, Pred, SetValue(St, D, GetValue(St, D)));
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000408}
409
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000410void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek54eddae2008-01-24 02:02:54 +0000411
412 QualType T = CastE->getType();
413
414 // Check for redundant casts.
415 if (E->getType() == T) {
416 Dst.Add(Pred);
417 return;
418 }
419
420 NodeSet S1;
421 Visit(E, Pred, S1);
422
423 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
424 NodeTy* N = *I1;
425 StateTy St = N->getState();
Ted Kremenek0922ec82008-01-28 22:09:13 +0000426 const RValue& V = GetValue(St, E);
Ted Kremenek25a484d2008-02-14 18:28:23 +0000427 Nodify(Dst, CastE, N, SetValue(St, CastE, EvalCast(ValMgr, V, CastE)));
Ted Kremenek54eddae2008-01-24 02:02:54 +0000428 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000429}
430
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000431void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
432 GRExprEngine::NodeSet& Dst) {
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000433
434 StateTy St = Pred->getState();
435
436 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000437 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremeneke0258002008-02-19 00:29:51 +0000438
439 // FIXME: Add support for local arrays.
440 if (VD->getType()->isArrayType())
441 continue;
442
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000443 const Expr* E = VD->getInit();
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000444 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenekadec14b2008-02-08 02:57:34 +0000445 E ? GetValue(St, E) : UninitializedVal());
Ted Kremenek8c9bc342008-01-28 22:51:57 +0000446 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000447
448 Nodify(Dst, DS, Pred, St);
449
450 if (Dst.empty())
451 Dst.Add(Pred);
452}
Ted Kremenek54eddae2008-01-24 02:02:54 +0000453
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000454
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000455void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS,
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000456 NodeTy* Pred, NodeSet& Dst) {
457
458 StateTy St = Pred->getState();
459
460 RValue R = GetValue(St, LHS);
Ted Kremenekadec14b2008-02-08 02:57:34 +0000461 if (isa<UnknownVal>(R)) R = GetValue(St, RHS);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000462
463 Nodify(Dst, S, Pred, SetValue(St, S, R));
464}
465
Ted Kremenekfd85f292008-02-12 19:49:57 +0000466/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000467void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S,
Ted Kremenekfd85f292008-02-12 19:49:57 +0000468 NodeTy* Pred,
469 NodeSet& Dst) {
470
471 // 6.5.3.4 sizeof: "The result type is an integer."
472
473 QualType T = S->getArgumentType();
474
475 // FIXME: Add support for VLAs.
Eli Friedman82ce9a82008-02-15 12:28:27 +0000476 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekfd85f292008-02-12 19:49:57 +0000477 return;
478
479 SourceLocation L = S->getExprLoc();
480 uint64_t size = getContext().getTypeSize(T, L) / 8;
481
482 Nodify(Dst, S, Pred,
483 SetValue(Pred->getState(), S,
Ted Kremenek521d9722008-02-15 23:15:23 +0000484 NonLValue::GetValue(ValMgr, size, S->getType(), L)));
Ted Kremenekfd85f292008-02-12 19:49:57 +0000485
486}
487
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000488void GRExprEngine::VisitUnaryOperator(UnaryOperator* U,
489 GRExprEngine::NodeTy* Pred,
490 GRExprEngine::NodeSet& Dst) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000491
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000492 NodeSet S1;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000493 UnaryOperator::Opcode Op = U->getOpcode();
494
495 // FIXME: This is a hack so that for '*' and '&' we don't recurse
496 // on visiting the subexpression if it is a DeclRefExpr. We should
497 // probably just handle AddrOf and Deref in their own methods to make
498 // this cleaner.
499 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
500 isa<DeclRefExpr>(U->getSubExpr()))
501 S1.Add(Pred);
502 else
503 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000504
505 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
506 NodeTy* N1 = *I1;
507 StateTy St = N1->getState();
508
Ted Kremenek22640ce2008-02-15 22:09:30 +0000509 // Handle ++ and -- (both pre- and post-increment).
510
511 if (U->isIncrementDecrementOp()) {
512 const LValue& L1 = GetLValue(St, U->getSubExpr());
513 RValue R1 = GetValue(St, L1);
514
515 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
516 : BinaryOperator::Sub;
517
518 RValue Result = EvalBinaryOp(ValMgr, Op, R1, GetRValueConstant(1U, U));
519
520 if (U->isPostfix())
521 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
522 else
523 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
524
525 continue;
526 }
527
528 // Handle all other unary operators.
529
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000530 switch (U->getOpcode()) {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000531
Ted Kremenekcacd7362008-01-24 08:20:02 +0000532 case UnaryOperator::Minus: {
Ted Kremenek0922ec82008-01-28 22:09:13 +0000533 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc9922fd2008-02-14 18:40:24 +0000534 Nodify(Dst, U, N1, SetValue(St, U, EvalMinus(ValMgr, U, R1)));
Ted Kremenekcacd7362008-01-24 08:20:02 +0000535 break;
536 }
537
Ted Kremenek2cb46642008-02-04 16:58:30 +0000538 case UnaryOperator::Not: {
539 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekc9922fd2008-02-14 18:40:24 +0000540 Nodify(Dst, U, N1, SetValue(St, U, EvalComplement(ValMgr, R1)));
Ted Kremenek2cb46642008-02-04 16:58:30 +0000541 break;
542 }
543
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000544 case UnaryOperator::LNot: {
545 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
546 //
547 // Note: technically we do "E == 0", but this is the same in the
548 // transfer functions as "0 == E".
549
550 RValue V1 = GetValue(St, U->getSubExpr());
551
552 if (isa<LValue>(V1)) {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000553 const LValue& L1 = cast<LValue>(V1);
554 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
555 Nodify(Dst, U, N1,
Ted Kremenekb1934132008-02-14 19:37:24 +0000556 SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ,
557 L1, V2)));
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000558 }
559 else {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000560 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000561 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenek15cb0782008-02-06 22:50:25 +0000562 Nodify(Dst, U, N1,
Ted Kremenekb1934132008-02-14 19:37:24 +0000563 SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ,
564 R1, V2)));
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000565 }
566
567 break;
568 }
Ted Kremenekfd85f292008-02-12 19:49:57 +0000569
570 case UnaryOperator::SizeOf: {
571 // 6.5.3.4 sizeof: "The result type is an integer."
572
573 QualType T = U->getSubExpr()->getType();
574
575 // FIXME: Add support for VLAs.
Eli Friedman82ce9a82008-02-15 12:28:27 +0000576 if (!T.getTypePtr()->isConstantSizeType())
Ted Kremenekfd85f292008-02-12 19:49:57 +0000577 return;
578
579 SourceLocation L = U->getExprLoc();
580 uint64_t size = getContext().getTypeSize(T, L) / 8;
581
582 Nodify(Dst, U, N1,
583 SetValue(St, U, NonLValue::GetValue(ValMgr, size,
Ted Kremenek521d9722008-02-15 23:15:23 +0000584 U->getType(), L)));
Ted Kremenekfd85f292008-02-12 19:49:57 +0000585
586 break;
587 }
Ted Kremeneka6683bf2008-02-06 17:56:00 +0000588
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000589 case UnaryOperator::AddrOf: {
590 const LValue& L1 = GetLValue(St, U->getSubExpr());
591 Nodify(Dst, U, N1, SetValue(St, U, L1));
592 break;
593 }
594
595 case UnaryOperator::Deref: {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000596 // FIXME: Stop when dereferencing an uninitialized value.
597 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
598
Ted Kremenekbc965a62008-02-18 22:57:02 +0000599 const RValue& V = GetValue(St, U->getSubExpr());
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000600 const LValue& L1 = cast<LValue>(V);
601
Ted Kremenek80d52d02008-02-07 05:48:01 +0000602 // After a dereference, one of two possible situations arise:
603 // (1) A crash, because the pointer was NULL.
604 // (2) The pointer is not NULL, and the dereference works.
605 //
606 // We add these assumptions.
607
Ted Kremenekae7bdc12008-02-07 06:04:18 +0000608 bool isFeasibleNotNull;
609
Ted Kremenek80d52d02008-02-07 05:48:01 +0000610 // "Assume" that the pointer is Not-NULL.
Ted Kremenekae7bdc12008-02-07 06:04:18 +0000611 StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull);
612
613 if (isFeasibleNotNull) {
Ted Kremenek80d52d02008-02-07 05:48:01 +0000614 QualType T = U->getType();
615 Nodify(Dst, U, N1, SetValue(StNotNull, U,
616 GetValue(StNotNull, L1, &T)));
617 }
618
Ted Kremenekbc965a62008-02-18 22:57:02 +0000619 if (V.isUnknown())
620 return;
621
Ted Kremenekae7bdc12008-02-07 06:04:18 +0000622 bool isFeasibleNull;
623
624 // "Assume" that the pointer is NULL.
625 StateTy StNull = Assume(St, L1, false, isFeasibleNull);
626
627 if (isFeasibleNull) {
Ted Kremenek02b5b402008-02-07 15:20:13 +0000628 // We don't use "Nodify" here because the node will be a sink
629 // and we have no intention of processing it later.
630 NodeTy* NullNode = Builder->generateNode(U, StNull, N1);
631
Ted Kremenekae7bdc12008-02-07 06:04:18 +0000632 if (NullNode) {
633 NullNode->markAsSink();
634
635 if (isFeasibleNotNull)
636 ImplicitNullDeref.insert(NullNode);
637 else
638 ExplicitNullDeref.insert(NullNode);
639 }
640 }
641
Ted Kremenekc48b8e42008-01-31 02:35:41 +0000642 break;
643 }
644
Ted Kremenek1be5eb92008-01-24 02:28:56 +0000645 default: ;
646 assert (false && "Not implemented.");
647 }
648 }
649}
650
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000651void GRExprEngine::VisitAssignmentLHS(Expr* E, GRExprEngine::NodeTy* Pred,
652 GRExprEngine::NodeSet& Dst) {
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000653
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000654 if (isa<DeclRefExpr>(E)) {
655 Dst.Add(Pred);
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000656 return;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000657 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000658
659 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
660 if (U->getOpcode() == UnaryOperator::Deref) {
661 Visit(U->getSubExpr(), Pred, Dst);
662 return;
663 }
664 }
665
666 Visit(E, Pred, Dst);
667}
668
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000669void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekaee121c2008-02-13 23:08:21 +0000670 GRExprEngine::NodeTy* Pred,
671 GRExprEngine::NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000672 NodeSet S1;
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000673
674 if (B->isAssignmentOp())
675 VisitAssignmentLHS(B->getLHS(), Pred, S1);
676 else
677 Visit(B->getLHS(), Pred, S1);
Ted Kremenekafba4b22008-01-16 00:53:15 +0000678
Ted Kremenekf031b872008-01-23 19:59:44 +0000679 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
680 NodeTy* N1 = *I1;
Ted Kremeneke860db82008-01-17 00:52:48 +0000681
Ted Kremenekf031b872008-01-23 19:59:44 +0000682 // When getting the value for the LHS, check if we are in an assignment.
683 // In such cases, we want to (initially) treat the LHS as an LValue,
684 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenek0922ec82008-01-28 22:09:13 +0000685 // evaluated to LValueDecl's instead of to an NonLValue.
686 const RValue& V1 =
Ted Kremenekf031b872008-01-23 19:59:44 +0000687 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
688 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekafba4b22008-01-16 00:53:15 +0000689
Ted Kremenekf031b872008-01-23 19:59:44 +0000690 NodeSet S2;
691 Visit(B->getRHS(), N1, S2);
692
693 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000694
Ted Kremenekf031b872008-01-23 19:59:44 +0000695 NodeTy* N2 = *I2;
696 StateTy St = N2->getState();
Ted Kremenek0922ec82008-01-28 22:09:13 +0000697 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekf031b872008-01-23 19:59:44 +0000698
Ted Kremenek15cb0782008-02-06 22:50:25 +0000699 BinaryOperator::Opcode Op = B->getOpcode();
700
701 if (Op <= BinaryOperator::Or) {
702
Ted Kremenekadec14b2008-02-08 02:57:34 +0000703 if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) {
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000704 Nodify(Dst, B, N2, SetValue(St, B, V1));
705 continue;
706 }
707
Ted Kremenek22640ce2008-02-15 22:09:30 +0000708 Nodify(Dst, B, N2, SetValue(St, B, EvalBinaryOp(ValMgr, Op, V1, V2)));
Ted Kremenek15cb0782008-02-06 22:50:25 +0000709 continue;
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000710
Ted Kremenek15cb0782008-02-06 22:50:25 +0000711 }
712
713 switch (Op) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000714 case BinaryOperator::Assign: {
715 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbf988d02008-02-19 00:22:37 +0000716
717 if (isa<UninitializedVal>(L1))
718 HandleUninitializedStore(B, N2);
719 else
720 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
721
Ted Kremenekf031b872008-01-23 19:59:44 +0000722 break;
723 }
724
Ted Kremenek15cb0782008-02-06 22:50:25 +0000725 default: { // Compound assignment operators.
Ted Kremenek19072e02008-01-29 19:43:15 +0000726
Ted Kremenek15cb0782008-02-06 22:50:25 +0000727 assert (B->isCompoundAssignmentOp());
728
729 const LValue& L1 = cast<LValue>(V1);
Ted Kremenekbf988d02008-02-19 00:22:37 +0000730
731 if (isa<UninitializedVal>(L1)) {
732 HandleUninitializedStore(B, N2);
733 break;
734 }
735
Ted Kremenekadec14b2008-02-08 02:57:34 +0000736 RValue Result = cast<NonLValue>(UnknownVal());
Ted Kremenek15cb0782008-02-06 22:50:25 +0000737
Ted Kremenek106f37c2008-02-08 07:05:39 +0000738 if (Op >= BinaryOperator::AndAssign)
739 ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
740 else
Ted Kremenek22640ce2008-02-15 22:09:30 +0000741 ((int&) Op) -= BinaryOperator::MulAssign;
Ted Kremenek15cb0782008-02-06 22:50:25 +0000742
Ted Kremenek22640ce2008-02-15 22:09:30 +0000743 if (B->getType()->isPointerType()) { // Perform pointer arithmetic.
744 const NonLValue& R2 = cast<NonLValue>(V2);
745 Result = EvalBinaryOp(ValMgr, Op, L1, R2);
746 }
Ted Kremenek022b6052008-02-15 22:29:00 +0000747 else if (isa<LValue>(V2)) {
Ted Kremenek19072e02008-01-29 19:43:15 +0000748 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek022b6052008-02-15 22:29:00 +0000749
750 if (B->getRHS()->getType()->isPointerType()) {
751 // LValue comparison.
752 Result = EvalBinaryOp(ValMgr, Op, L1, L2);
753 }
754 else {
Ted Kremenek521d9722008-02-15 23:15:23 +0000755 QualType T1 = B->getLHS()->getType();
756 QualType T2 = B->getRHS()->getType();
757
Ted Kremenek022b6052008-02-15 22:29:00 +0000758 // An operation between two variables of a non-lvalue type.
759 Result =
760 EvalBinaryOp(ValMgr, Op,
Ted Kremenek521d9722008-02-15 23:15:23 +0000761 cast<NonLValue>(GetValue(N1->getState(), L1, &T1)),
762 cast<NonLValue>(GetValue(N2->getState(), L2, &T2)));
Ted Kremenek022b6052008-02-15 22:29:00 +0000763 }
Ted Kremenek19072e02008-01-29 19:43:15 +0000764 }
Ted Kremenek022b6052008-02-15 22:29:00 +0000765 else { // Any other operation between two Non-LValues.
Ted Kremenek521d9722008-02-15 23:15:23 +0000766 QualType T = B->getLHS()->getType();
767 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(),
768 L1, &T));
Ted Kremenek19072e02008-01-29 19:43:15 +0000769 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekb1934132008-02-14 19:37:24 +0000770 Result = EvalBinaryOp(ValMgr, Op, R1, R2);
Ted Kremenek19072e02008-01-29 19:43:15 +0000771 }
772
Ted Kremenek15cb0782008-02-06 22:50:25 +0000773 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekf031b872008-01-23 19:59:44 +0000774 break;
Ted Kremenek15cb0782008-02-06 22:50:25 +0000775 }
Ted Kremenekf031b872008-01-23 19:59:44 +0000776 }
Ted Kremenekafba4b22008-01-16 00:53:15 +0000777 }
Ted Kremenek68d70a82008-01-15 23:55:06 +0000778 }
Ted Kremenek68d70a82008-01-15 23:55:06 +0000779}
Ted Kremenekd2500ab2008-01-16 18:18:48 +0000780
Ted Kremenekbf988d02008-02-19 00:22:37 +0000781void GRExprEngine::HandleUninitializedStore(Stmt* S, NodeTy* Pred) {
782
783 NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred);
784 N->markAsSink();
785 UninitStores.insert(N);
786}
Ted Kremenekbe962452008-01-16 19:42:59 +0000787
Ted Kremenekbf988d02008-02-19 00:22:37 +0000788void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekf031b872008-01-23 19:59:44 +0000789
790 // FIXME: add metadata to the CFG so that we can disable
791 // this check when we KNOW that there is no block-level subexpression.
792 // The motivation is that this check requires a hashtable lookup.
793
794 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
795 Dst.Add(Pred);
796 return;
797 }
798
799 switch (S->getStmtClass()) {
Ted Kremenek5bde36b2008-02-12 21:37:25 +0000800
801 default:
802 // Cases we intentionally have "default" handle:
803 // AddrLabelExpr, CharacterLiteral, IntegerLiteral
804
805 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
806 break;
807
Ted Kremenek744a7862008-02-08 20:29:23 +0000808 case Stmt::BinaryOperatorClass: {
809 BinaryOperator* B = cast<BinaryOperator>(S);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000810
Ted Kremenek744a7862008-02-08 20:29:23 +0000811 if (B->isLogicalOp()) {
812 VisitLogicalExpr(B, Pred, Dst);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000813 break;
814 }
Ted Kremenek744a7862008-02-08 20:29:23 +0000815 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek106f37c2008-02-08 07:05:39 +0000816 StateTy St = Pred->getState();
Ted Kremenek744a7862008-02-08 20:29:23 +0000817 Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS())));
Ted Kremenek106f37c2008-02-08 07:05:39 +0000818 break;
819 }
Ted Kremenekfd85f292008-02-12 19:49:57 +0000820
821 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
822 break;
823 }
824
825 case Stmt::CastExprClass: {
826 CastExpr* C = cast<CastExpr>(S);
827 VisitCast(C, C->getSubExpr(), Pred, Dst);
828 break;
Ted Kremenek744a7862008-02-08 20:29:23 +0000829 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000830
Ted Kremenekfd85f292008-02-12 19:49:57 +0000831 case Stmt::ChooseExprClass: { // __builtin_choose_expr
832 ChooseExpr* C = cast<ChooseExpr>(S);
833 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
834 break;
835 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000836
Ted Kremenek9914e9c2008-01-23 23:38:00 +0000837 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekf031b872008-01-23 19:59:44 +0000838 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
839 break;
840
Ted Kremenekfd85f292008-02-12 19:49:57 +0000841 case Stmt::ConditionalOperatorClass: { // '?' operator
842 ConditionalOperator* C = cast<ConditionalOperator>(S);
843 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
844 break;
845 }
846
847 case Stmt::DeclRefExprClass:
848 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
849 break;
850
851 case Stmt::DeclStmtClass:
852 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
853 break;
854
855 case Stmt::ImplicitCastExprClass: {
856 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
857 VisitCast(C, C->getSubExpr(), Pred, Dst);
858 break;
859 }
860
861 case Stmt::ParenExprClass:
862 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
863 break;
864
865 case Stmt::SizeOfAlignOfTypeExprClass:
866 VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
867 break;
868
Ted Kremenek106f37c2008-02-08 07:05:39 +0000869 case Stmt::StmtExprClass: {
Ted Kremenek744a7862008-02-08 20:29:23 +0000870 StmtExpr* SE = cast<StmtExpr>(S);
871
Ted Kremenek106f37c2008-02-08 07:05:39 +0000872 StateTy St = Pred->getState();
Ted Kremenek744a7862008-02-08 20:29:23 +0000873 Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
874 Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr)));
Ted Kremenek106f37c2008-02-08 07:05:39 +0000875 break;
876 }
877
Ted Kremenekfd85f292008-02-12 19:49:57 +0000878 case Stmt::ReturnStmtClass: {
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000879 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
880 Visit(R, Pred, Dst);
881 else
882 Dst.Add(Pred);
883
884 break;
Ted Kremenekfd85f292008-02-12 19:49:57 +0000885 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000886
Ted Kremenekfd85f292008-02-12 19:49:57 +0000887 case Stmt::UnaryOperatorClass:
888 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
Ted Kremenekb9c30e32008-01-24 20:55:43 +0000889 break;
Ted Kremenekab8ed952008-01-17 18:25:22 +0000890 }
Ted Kremenekbe962452008-01-16 19:42:59 +0000891}
892
Ted Kremenekd2500ab2008-01-16 18:18:48 +0000893//===----------------------------------------------------------------------===//
Ted Kremenek90960972008-01-30 23:03:39 +0000894// "Assume" logic.
895//===----------------------------------------------------------------------===//
896
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000897GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond,
Ted Kremenekbc965a62008-02-18 22:57:02 +0000898 bool Assumption,
899 bool& isFeasible) {
900
901 assert (!isa<UninitializedVal>(Cond));
902
903 if (isa<UnknownVal>(Cond)) {
904 isFeasible = true;
905 return St;
906 }
Ted Kremenek6e24a802008-02-01 06:36:40 +0000907
908 switch (Cond.getSubKind()) {
909 default:
Ted Kremenek13f31562008-02-06 00:54:14 +0000910 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremenek6e24a802008-02-01 06:36:40 +0000911 return St;
912
Ted Kremenek13f31562008-02-06 00:54:14 +0000913 case lval::SymbolValKind:
914 if (Assumption)
915 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
916 ValMgr.getZeroWithPtrWidth(), isFeasible);
917 else
918 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
919 ValMgr.getZeroWithPtrWidth(), isFeasible);
920
Ted Kremenek0033fbb2008-02-06 04:31:33 +0000921
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000922 case lval::DeclValKind:
Ted Kremenek6e24a802008-02-01 06:36:40 +0000923 isFeasible = Assumption;
924 return St;
Ted Kremenek13f31562008-02-06 00:54:14 +0000925
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000926 case lval::ConcreteIntKind: {
927 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek6e24a802008-02-01 06:36:40 +0000928 isFeasible = b ? Assumption : !Assumption;
929 return St;
930 }
931 }
Ted Kremenek90960972008-01-30 23:03:39 +0000932}
933
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000934GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond,
Ted Kremenek13f31562008-02-06 00:54:14 +0000935 bool Assumption,
Ted Kremenek72197902008-01-31 19:34:24 +0000936 bool& isFeasible) {
Ted Kremenek90960972008-01-30 23:03:39 +0000937
Ted Kremenekbc965a62008-02-18 22:57:02 +0000938 assert (!isa<UninitializedVal>(Cond));
939
940 if (isa<UnknownVal>(Cond)) {
941 isFeasible = true;
942 return St;
943 }
944
Ted Kremenek90960972008-01-30 23:03:39 +0000945 switch (Cond.getSubKind()) {
946 default:
947 assert (false && "'Assume' not implemented for this NonLValue.");
948 return St;
949
Ted Kremenekab359c12008-02-06 17:32:17 +0000950
951 case nonlval::SymbolValKind: {
Ted Kremenek5bde36b2008-02-12 21:37:25 +0000952 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
Ted Kremenekab359c12008-02-06 17:32:17 +0000953 SymbolID sym = SV.getSymbol();
954
955 if (Assumption)
956 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
957 isFeasible);
958 else
959 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
960 isFeasible);
961 }
962
Ted Kremenek0033fbb2008-02-06 04:31:33 +0000963 case nonlval::SymIntConstraintValKind:
964 return
965 AssumeSymInt(St, Assumption,
966 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
967 isFeasible);
968
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000969 case nonlval::ConcreteIntKind: {
970 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenek90960972008-01-30 23:03:39 +0000971 isFeasible = b ? Assumption : !Assumption;
972 return St;
973 }
974 }
975}
976
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000977GRExprEngine::StateTy
978GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +0000979 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenekbc965a62008-02-18 22:57:02 +0000980
Ted Kremenek13f31562008-02-06 00:54:14 +0000981 // First, determine if sym == X, where X != V.
982 if (const llvm::APSInt* X = St.getSymVal(sym)) {
983 isFeasible = *X != V;
984 return St;
985 }
986
987 // Second, determine if sym != V.
988 if (St.isNotEqual(sym, V)) {
989 isFeasible = true;
990 return St;
991 }
992
993 // If we reach here, sym is not a constant and we don't know if it is != V.
994 // Make that assumption.
995
996 isFeasible = true;
997 return StateMgr.AddNE(St, sym, V);
998}
999
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001000GRExprEngine::StateTy
1001GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
Ted Kremenek13f31562008-02-06 00:54:14 +00001002 const llvm::APSInt& V, bool& isFeasible) {
1003
1004 // First, determine if sym == X, where X != V.
1005 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1006 isFeasible = *X == V;
1007 return St;
1008 }
1009
1010 // Second, determine if sym != V.
1011 if (St.isNotEqual(sym, V)) {
1012 isFeasible = false;
1013 return St;
1014 }
1015
1016 // If we reach here, sym is not a constant and we don't know if it is == V.
1017 // Make that assumption.
1018
1019 isFeasible = true;
1020 return StateMgr.AddEQ(St, sym, V);
1021}
Ted Kremenek90960972008-01-30 23:03:39 +00001022
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001023GRExprEngine::StateTy
1024GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
Ted Kremenek0033fbb2008-02-06 04:31:33 +00001025 const SymIntConstraint& C, bool& isFeasible) {
1026
1027 switch (C.getOpcode()) {
1028 default:
1029 // No logic yet for other operators.
1030 return St;
1031
1032 case BinaryOperator::EQ:
1033 if (Assumption)
1034 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1035 else
1036 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1037
1038 case BinaryOperator::NE:
1039 if (Assumption)
1040 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1041 else
1042 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1043 }
1044}
1045
Ted Kremenek90960972008-01-30 23:03:39 +00001046//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +00001047// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001048//===----------------------------------------------------------------------===//
1049
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001050#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001051static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek428d39e2008-01-30 23:24:39 +00001052
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001053namespace llvm {
1054template<>
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001055struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001056 public DefaultDOTGraphTraits {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001057
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001058 static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremenek08cfd832008-02-08 21:10:02 +00001059
1060 Out << "Variables:\\l";
1061
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001062 bool isFirst = true;
1063
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001064 for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
Ted Kremenek08cfd832008-02-08 21:10:02 +00001065 E=St.vb_end(); I!=E;++I) {
1066
1067 if (isFirst)
1068 isFirst = false;
1069 else
1070 Out << "\\l";
1071
1072 Out << ' ' << I.getKey()->getName() << " : ";
1073 I.getData().print(Out);
1074 }
1075
1076 }
1077
Ted Kremenek17c5f112008-02-11 19:21:59 +00001078
Ted Kremenek6d409922008-02-13 18:06:44 +00001079 static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001080
1081 bool isFirst = true;
1082
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001083 for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
Ted Kremenek17c5f112008-02-11 19:21:59 +00001084 I != E;++I) {
1085
1086 if (isFirst) {
1087 Out << "\\l\\lSub-Expressions:\\l";
1088 isFirst = false;
1089 }
1090 else
1091 Out << "\\l";
1092
1093 Out << " (" << (void*) I.getKey() << ") ";
1094 I.getKey()->printPretty(Out);
1095 Out << " : ";
1096 I.getData().print(Out);
1097 }
1098 }
1099
Ted Kremenek6d409922008-02-13 18:06:44 +00001100 static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
Ted Kremenek17c5f112008-02-11 19:21:59 +00001101
Ted Kremenek08cfd832008-02-08 21:10:02 +00001102 bool isFirst = true;
1103
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001104 for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
Ted Kremenek17c5f112008-02-11 19:21:59 +00001105 I != E; ++I) {
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001106 if (isFirst) {
Ted Kremenek17c5f112008-02-11 19:21:59 +00001107 Out << "\\l\\lBlock-level Expressions:\\l";
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001108 isFirst = false;
1109 }
1110 else
1111 Out << "\\l";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001112
Ted Kremenek17c5f112008-02-11 19:21:59 +00001113 Out << " (" << (void*) I.getKey() << ") ";
1114 I.getKey()->printPretty(Out);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001115 Out << " : ";
1116 I.getData().print(Out);
1117 }
1118 }
1119
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001120 static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneke6536692008-02-06 03:56:15 +00001121 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1122
1123 if (CE.isEmpty())
1124 return;
1125
1126 Out << "\\l\\|'==' constraints:";
1127
1128 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1129 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1130 }
1131
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001132 static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
Ted Kremeneke6536692008-02-06 03:56:15 +00001133 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1134
1135 if (NE.isEmpty())
1136 return;
1137
1138 Out << "\\l\\|'!=' constraints:";
1139
1140 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1141 I != EI; ++I){
1142
1143 Out << "\\l $" << I.getKey() << " : ";
1144 bool isFirst = true;
1145
1146 ValueState::IntSetTy::iterator J=I.getData().begin(),
1147 EJ=I.getData().end();
1148 for ( ; J != EJ; ++J) {
1149 if (isFirst) isFirst = false;
1150 else Out << ", ";
1151
1152 Out << (*J)->toString();
1153 }
1154 }
Ted Kremeneka853de62008-02-14 22:54:53 +00001155 }
1156
1157 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
1158
1159 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00001160 GraphPrintCheckerState->isExplicitNullDeref(N) ||
1161 GraphPrintCheckerState->isUninitStore(N) ||
1162 GraphPrintCheckerState->isUninitControlFlow(N))
Ted Kremeneka853de62008-02-14 22:54:53 +00001163 return "color=\"red\",style=\"filled\"";
1164
1165 return "";
1166 }
Ted Kremeneke6536692008-02-06 03:56:15 +00001167
Ted Kremenek30fa28b2008-02-13 17:41:41 +00001168 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001169 std::ostringstream Out;
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001170
1171 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001172 ProgramPoint Loc = N->getLocation();
1173
1174 switch (Loc.getKind()) {
1175 case ProgramPoint::BlockEntranceKind:
1176 Out << "Block Entrance: B"
1177 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1178 break;
1179
1180 case ProgramPoint::BlockExitKind:
1181 assert (false);
1182 break;
1183
1184 case ProgramPoint::PostStmtKind: {
1185 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek3ec7be92008-01-24 22:27:20 +00001186 Out << L.getStmt()->getStmtClassName() << ':'
1187 << (void*) L.getStmt() << ' ';
1188
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001189 L.getStmt()->printPretty(Out);
Ted Kremenek80d52d02008-02-07 05:48:01 +00001190
1191 if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
1192 Out << "\\|Implicit-Null Dereference.\\l";
1193 }
Ted Kremenekae7bdc12008-02-07 06:04:18 +00001194 else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
1195 Out << "\\|Explicit-Null Dereference.\\l";
1196 }
Ted Kremenekbf988d02008-02-19 00:22:37 +00001197 else if (GraphPrintCheckerState->isUninitStore(N)) {
1198 Out << "\\|Store to Uninitialized LValue.";
1199 }
Ted Kremenek80d52d02008-02-07 05:48:01 +00001200
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001201 break;
1202 }
1203
1204 default: {
1205 const BlockEdge& E = cast<BlockEdge>(Loc);
1206 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1207 << E.getDst()->getBlockID() << ')';
Ted Kremenek90960972008-01-30 23:03:39 +00001208
1209 if (Stmt* T = E.getSrc()->getTerminator()) {
1210 Out << "\\|Terminator: ";
1211 E.getSrc()->printTerminator(Out);
1212
Ted Kremenekaee121c2008-02-13 23:08:21 +00001213 if (isa<SwitchStmt>(T)) {
1214 Stmt* Label = E.getDst()->getLabel();
1215
1216 if (Label) {
1217 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
1218 Out << "\\lcase ";
1219 C->getLHS()->printPretty(Out);
1220
1221 if (Stmt* RHS = C->getRHS()) {
1222 Out << " .. ";
1223 RHS->printPretty(Out);
1224 }
1225
1226 Out << ":";
1227 }
1228 else {
1229 assert (isa<DefaultStmt>(Label));
1230 Out << "\\ldefault:";
1231 }
1232 }
1233 else
1234 Out << "\\l(implicit) default:";
1235 }
1236 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00001237 // FIXME
1238 }
1239 else {
1240 Out << "\\lCondition: ";
1241 if (*E.getSrc()->succ_begin() == E.getDst())
1242 Out << "true";
1243 else
1244 Out << "false";
1245 }
1246
1247 Out << "\\l";
1248 }
Ted Kremenek428d39e2008-01-30 23:24:39 +00001249
1250 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1251 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1252 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001253 }
1254 }
1255
Ted Kremenek2d8dce32008-02-05 07:17:49 +00001256 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00001257
Ted Kremenek17c5f112008-02-11 19:21:59 +00001258 N->getState().printDOT(Out);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001259
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00001260 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001261 return Out.str();
1262 }
1263};
1264} // end llvm namespace
1265#endif
1266
Ted Kremenek3862eb12008-02-14 22:36:46 +00001267void GRExprEngine::ViewGraph() {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00001268#ifndef NDEBUG
Ted Kremenek3862eb12008-02-14 22:36:46 +00001269 GraphPrintCheckerState = this;
1270 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek428d39e2008-01-30 23:24:39 +00001271 GraphPrintCheckerState = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00001272#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00001273}