blob: 7054f3c0bb502f49aa84a89aa72426cebe73df72 [file] [log] [blame]
Ted Kremenek77349cb2008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekd27f8162008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek77349cb2008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenekd27f8162008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek77349cb2008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek41573eb2009-02-14 01:43:44 +000017#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000018#include "clang/Analysis/PathSensitive/BugReporter.h"
Chris Lattner16f00492009-04-26 01:32:48 +000019#include "clang/AST/ParentMap.h"
20#include "clang/AST/StmtObjC.h"
21#include "clang/Basic/SourceManager.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000022#include "clang/Basic/SourceManager.h"
Ted Kremenek0bed8a12009-03-11 02:41:36 +000023#include "clang/Basic/PrettyStackTrace.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000024#include "llvm/Support/Streams.h"
Ted Kremenekbdb435d2008-07-11 18:37:32 +000025#include "llvm/ADT/ImmutableList.h"
26#include "llvm/Support/Compiler.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000028
Ted Kremenek0f5f0592008-02-27 06:07:00 +000029#ifndef NDEBUG
30#include "llvm/Support/GraphWriter.h"
31#include <sstream>
32#endif
33
Ted Kremenekb387a3f2008-02-14 22:16:04 +000034using namespace clang;
35using llvm::dyn_cast;
36using llvm::cast;
37using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000038
Ted Kremeneke695e1c2008-04-15 23:06:53 +000039//===----------------------------------------------------------------------===//
40// Engine construction and deletion.
41//===----------------------------------------------------------------------===//
42
Ted Kremenekbdb435d2008-07-11 18:37:32 +000043namespace {
44
45class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
46 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
47 typedef llvm::DenseMap<void*,Checks> MapTy;
48
49 MapTy M;
50 Checks::Factory F;
Ted Kremenek536aa022009-03-30 17:53:05 +000051 Checks AllStmts;
Ted Kremenekbdb435d2008-07-11 18:37:32 +000052
53public:
Ted Kremenek536aa022009-03-30 17:53:05 +000054 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
55 F(Alloc), AllStmts(F.GetEmptyList()) {}
Ted Kremenekbdb435d2008-07-11 18:37:32 +000056
57 virtual ~MappedBatchAuditor() {
58 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
59
60 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
61 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
62
63 GRSimpleAPICheck* check = *I;
64
65 if (AlreadyVisited.count(check))
66 continue;
67
68 AlreadyVisited.insert(check);
69 delete check;
70 }
71 }
72
Ted Kremenek536aa022009-03-30 17:53:05 +000073 void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
Ted Kremenekbdb435d2008-07-11 18:37:32 +000074 assert (A && "Check cannot be null.");
75 void* key = reinterpret_cast<void*>((uintptr_t) C);
76 MapTy::iterator I = M.find(key);
77 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
78 }
Ted Kremenek536aa022009-03-30 17:53:05 +000079
80 void AddCheck(GRSimpleAPICheck *A) {
81 assert (A && "Check cannot be null.");
82 AllStmts = F.Concat(A, AllStmts);
83 }
Ted Kremenekcf118d42009-02-04 23:49:09 +000084
Ted Kremenek4adc81e2008-08-13 04:27:00 +000085 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenek536aa022009-03-30 17:53:05 +000086 // First handle the auditors that accept all statements.
87 bool isSink = false;
88 for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I)
89 isSink |= (*I)->Audit(N, VMgr);
90
91 // Next handle the auditors that accept only specific statements.
Ted Kremenekbdb435d2008-07-11 18:37:32 +000092 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
93 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
94 MapTy::iterator MI = M.find(key);
Ted Kremenek536aa022009-03-30 17:53:05 +000095 if (MI != M.end()) {
96 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
97 isSink |= (*I)->Audit(N, VMgr);
98 }
Ted Kremenekbdb435d2008-07-11 18:37:32 +000099
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000100 return isSink;
101 }
102};
103
104} // end anonymous namespace
105
106//===----------------------------------------------------------------------===//
107// Engine construction and deletion.
108//===----------------------------------------------------------------------===//
109
Ted Kremeneke448ab42008-05-01 18:33:28 +0000110static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
111 IdentifierInfo* II = &Ctx.Idents.get(name);
112 return Ctx.Selectors.getSelector(0, &II);
113}
114
Ted Kremenekdaa497e2008-03-09 18:05:48 +0000115
Ted Kremenek8b233612008-07-02 20:13:38 +0000116GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000117 LiveVariables& L, BugReporterData& BRD,
Ted Kremenek48af2a92009-02-25 22:32:02 +0000118 bool purgeDead, bool eagerlyAssume,
Zhongxing Xu22438a82008-11-27 01:55:08 +0000119 StoreManagerCreator SMC,
120 ConstraintManagerCreator CMC)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000121 : CoreEngine(cfg, CD, Ctx, *this),
122 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000123 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000124 Builder(NULL),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000125 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000126 SymMgr(StateMgr.getSymbolManager()),
Ted Kremenek8e5fb282009-04-09 16:46:55 +0000127 ValMgr(StateMgr.getValueManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000128 CurrentStmt(NULL),
Zhongxing Xua5a41662008-12-22 08:30:52 +0000129 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
130 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekcf118d42009-02-04 23:49:09 +0000131 PurgeDead(purgeDead),
Ted Kremenek48af2a92009-02-25 22:32:02 +0000132 BR(BRD, *this),
133 EagerlyAssume(eagerlyAssume) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000134
Ted Kremenek1a654b62008-06-20 21:45:25 +0000135GRExprEngine::~GRExprEngine() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000136 BR.FlushReports();
Ted Kremeneke448ab42008-05-01 18:33:28 +0000137 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000138}
139
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000140//===----------------------------------------------------------------------===//
141// Utility methods.
142//===----------------------------------------------------------------------===//
143
Ted Kremenek186350f2008-04-23 20:12:28 +0000144
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000145void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000146 StateMgr.TF = tf;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000147 tf->RegisterChecks(getBugReporter());
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000148 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000149}
150
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000151void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
152 if (!BatchAuditor)
153 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
154
155 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000156}
157
Ted Kremenek536aa022009-03-30 17:53:05 +0000158void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
159 if (!BatchAuditor)
160 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
161
162 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
163}
164
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000165const GRState* GRExprEngine::getInitialState() {
Ted Kremenek52e56022009-04-10 00:59:50 +0000166 const GRState *state = StateMgr.getInitialState();
167
168 // Precondition: the first argument of 'main' is an integer guaranteed
169 // to be > 0.
170 // FIXME: It would be nice if we had a more general mechanism to add
171 // such preconditions. Some day.
172 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(&StateMgr.getCodeDecl()))
173 if (strcmp(FD->getIdentifier()->getName(), "main") == 0 &&
174 FD->getNumParams() > 0) {
175 const ParmVarDecl *PD = FD->getParamDecl(0);
176 QualType T = PD->getType();
177 if (T->isIntegerType())
178 if (const MemRegion *R = StateMgr.getRegion(PD)) {
179 SVal V = GetSVal(state, loc::MemRegionVal(R));
180 SVal Constraint = EvalBinOp(BinaryOperator::GT, V,
181 ValMgr.makeZeroVal(T),
182 getContext().IntTy);
183 bool isFeasible = false;
184 const GRState *newState = Assume(state, Constraint, true,
185 isFeasible);
186 if (newState) state = newState;
187 }
188 }
189
190 return state;
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000191}
192
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000193//===----------------------------------------------------------------------===//
194// Top-level transfer function logic (Dispatcher).
195//===----------------------------------------------------------------------===//
196
197void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
198
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000199 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
200 S->getLocStart(),
201 "Error evaluating statement");
202
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000203 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000204 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000205
206 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000207 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000208 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000209
210 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000211 if (BatchAuditor)
212 Builder->setAuditor(BatchAuditor.get());
Ted Kremenek241677a2009-01-21 22:26:05 +0000213
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000214 // Create the cleaned state.
Ted Kremenek241677a2009-01-21 22:26:05 +0000215 SymbolReaper SymReaper(Liveness, SymMgr);
216 CleanedState = PurgeDead ? StateMgr.RemoveDeadBindings(EntryNode->getState(),
217 CurrentStmt, SymReaper)
218 : EntryNode->getState();
219
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000220 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000221 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000222
Ted Kremenek241677a2009-01-21 22:26:05 +0000223 if (!SymReaper.hasDeadSymbols())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000224 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000225 else {
226 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000227 SaveOr OldHasGen(Builder->HasGeneratedNode);
228
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000229 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
230 Builder->PurgingDeadSymbols = true;
231
Ted Kremenek729a9a22008-07-17 23:15:45 +0000232 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek241677a2009-01-21 22:26:05 +0000233 CleanedState, SymReaper);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000234
235 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
236 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000237 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000238
239 bool HasAutoGenerated = false;
240
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000241 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000242
243 NodeSet Dst;
244
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000245 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000246 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
247
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000248 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000249 Visit(S, *I, Dst);
250
251 // Do we need to auto-generate a node? We only need to do this to generate
252 // a node with a "cleaned" state; GRCoreEngine will actually handle
253 // auto-transitions for other cases.
254 if (Dst.size() == 1 && *Dst.begin() == EntryNode
255 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
256 HasAutoGenerated = true;
257 builder.generateNode(S, GetState(EntryNode), *I);
258 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000259 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000260
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000261 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000262 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000263 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000264
265 // FIXME: Consolidate.
266 StateMgr.CurrentStmt = 0;
267 CurrentStmt = 0;
268
Ted Kremenek846d4e92008-04-24 23:35:58 +0000269 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000270}
271
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000272void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
273 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
274 S->getLocStart(),
275 "Error evaluating statement");
276
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000277 // FIXME: add metadata to the CFG so that we can disable
278 // this check when we KNOW that there is no block-level subexpression.
279 // The motivation is that this check requires a hashtable lookup.
280
281 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
282 Dst.Add(Pred);
283 return;
284 }
285
286 switch (S->getStmtClass()) {
287
288 default:
289 // Cases we intentionally have "default" handle:
290 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
291
292 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
293 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000294
295 case Stmt::ArraySubscriptExprClass:
296 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
297 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000298
299 case Stmt::AsmStmtClass:
300 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
301 break;
302
303 case Stmt::BinaryOperatorClass: {
304 BinaryOperator* B = cast<BinaryOperator>(S);
305
306 if (B->isLogicalOp()) {
307 VisitLogicalExpr(B, Pred, Dst);
308 break;
309 }
310 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000311 const GRState* state = GetState(Pred);
312 MakeNode(Dst, B, Pred, BindExpr(state, B, GetSVal(state, B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000313 break;
314 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000315
Ted Kremenek48af2a92009-02-25 22:32:02 +0000316 if (EagerlyAssume && (B->isRelationalOp() || B->isEqualityOp())) {
317 NodeSet Tmp;
318 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Ted Kremenekb2939022009-02-25 23:32:10 +0000319 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenek48af2a92009-02-25 22:32:02 +0000320 }
321 else
322 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
323
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000324 break;
325 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000326
Douglas Gregorb4609802008-11-14 16:09:21 +0000327 case Stmt::CallExprClass:
328 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000329 CallExpr* C = cast<CallExpr>(S);
330 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000331 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000332 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000333
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000334 // FIXME: ChooseExpr is really a constant. We need to fix
335 // the CFG do not model them as explicit control-flow.
336
337 case Stmt::ChooseExprClass: { // __builtin_choose_expr
338 ChooseExpr* C = cast<ChooseExpr>(S);
339 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
340 break;
341 }
342
343 case Stmt::CompoundAssignOperatorClass:
344 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
345 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000346
347 case Stmt::CompoundLiteralExprClass:
348 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
349 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000350
351 case Stmt::ConditionalOperatorClass: { // '?' operator
352 ConditionalOperator* C = cast<ConditionalOperator>(S);
353 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
354 break;
355 }
356
357 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000358 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000359 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000360 break;
361
362 case Stmt::DeclStmtClass:
363 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
364 break;
365
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000366 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000367 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000368 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000369 VisitCast(C, C->getSubExpr(), Pred, Dst);
370 break;
371 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000372
373 case Stmt::InitListExprClass:
374 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
375 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000376
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000377 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000378 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
379 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000380
381 case Stmt::ObjCIvarRefExprClass:
382 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
383 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000384
385 case Stmt::ObjCForCollectionStmtClass:
386 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
387 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000388
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000389 case Stmt::ObjCMessageExprClass: {
390 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
391 break;
392 }
393
Ted Kremenekbbfd07a2008-12-09 20:18:58 +0000394 case Stmt::ObjCAtThrowStmtClass: {
395 // FIXME: This is not complete. We basically treat @throw as
396 // an abort.
397 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
398 Builder->BuildSinks = true;
399 MakeNode(Dst, S, Pred, GetState(Pred));
400 break;
401 }
402
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000403 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000404 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000405 break;
406
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000407 case Stmt::ReturnStmtClass:
408 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
409 break;
410
Sebastian Redl05189992008-11-11 17:56:53 +0000411 case Stmt::SizeOfAlignOfExprClass:
412 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000413 break;
414
415 case Stmt::StmtExprClass: {
416 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000417
418 if (SE->getSubStmt()->body_empty()) {
419 // Empty statement expression.
420 assert(SE->getType() == getContext().VoidTy
421 && "Empty statement expression must have void type.");
422 Dst.Add(Pred);
423 break;
424 }
425
426 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
427 const GRState* state = GetState(Pred);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000428 MakeNode(Dst, SE, Pred, BindExpr(state, SE, GetSVal(state, LastExpr)));
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000429 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000430 else
431 Dst.Add(Pred);
432
433 break;
434 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000435
436 case Stmt::StringLiteralClass:
437 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
438 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000439
Ted Kremenek72374592009-03-18 23:49:26 +0000440 case Stmt::UnaryOperatorClass: {
441 UnaryOperator *U = cast<UnaryOperator>(S);
442 if (EagerlyAssume && (U->getOpcode() == UnaryOperator::LNot)) {
443 NodeSet Tmp;
444 VisitUnaryOperator(U, Pred, Tmp, false);
445 EvalEagerlyAssume(Dst, Tmp, U);
446 }
447 else
448 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000449 break;
Ted Kremenek72374592009-03-18 23:49:26 +0000450 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000451 }
452}
453
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000454void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000455
456 Ex = Ex->IgnoreParens();
457
458 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
459 Dst.Add(Pred);
460 return;
461 }
462
463 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000464
465 case Stmt::ArraySubscriptExprClass:
466 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
467 return;
468
469 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000470 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000471 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
472 return;
473
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000474 case Stmt::ObjCIvarRefExprClass:
475 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
476 return;
477
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000478 case Stmt::UnaryOperatorClass:
479 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
480 return;
481
482 case Stmt::MemberExprClass:
483 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
484 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000485
Ted Kremenek4f090272008-10-27 21:54:31 +0000486 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000487 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000488 return;
489
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000490 case Stmt::ObjCPropertyRefExprClass:
Ted Kremenek868210e2009-04-21 23:53:32 +0000491 case Stmt::ObjCKVCRefExprClass:
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000492 // FIXME: Property assignments are lvalues, but not really "locations".
493 // e.g.: self.x = something;
494 // Here the "self.x" really can translate to a method call (setter) when
495 // the assignment is made. Moreover, the entire assignment expression
496 // evaluate to whatever "something" is, not calling the "getter" for
497 // the property (which would make sense since it can have side effects).
498 // We'll probably treat this as a location, but not one that we can
499 // take the address of. Perhaps we need a new SVal class for cases
500 // like thsis?
501 // Note that we have a similar problem for bitfields, since they don't
502 // have "locations" in the sense that we can take their address.
503 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000504 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000505
506 case Stmt::StringLiteralClass: {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000507 const GRState* state = GetState(Pred);
508 SVal V = StateMgr.GetLValue(state, cast<StringLiteral>(Ex));
509 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000510 return;
511 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000512
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000513 default:
514 // Arbitrary subexpressions can return aggregate temporaries that
515 // can be used in a lvalue context. We need to enhance our support
516 // of such temporaries in both the environment and the store, so right
517 // now we just do a regular visit.
Douglas Gregord7eb8462009-01-30 17:31:00 +0000518 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000519 "Other kinds of expressions with non-aggregate/union types do"
520 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000521
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000522 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000523 }
524}
525
526//===----------------------------------------------------------------------===//
527// Block entrance. (Update counters).
528//===----------------------------------------------------------------------===//
529
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000530bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000531 GRBlockCounter BC) {
532
533 return BC.getNumVisited(B->getBlockID()) < 3;
534}
535
536//===----------------------------------------------------------------------===//
Ted Kremenek1670e402009-04-11 00:11:10 +0000537// Generic node creation.
538//===----------------------------------------------------------------------===//
539
540GRExprEngine::NodeTy* GRExprEngine::MakeNode(NodeSet& Dst, Stmt* S,
541 NodeTy* Pred,
542 const GRState* St,
543 ProgramPoint::Kind K,
544 const void *tag) {
545
546 assert (Builder && "GRStmtNodeBuilder not present.");
547 SaveAndRestore<const void*> OldTag(Builder->Tag);
548 Builder->Tag = tag;
549 return Builder->MakeNode(Dst, S, Pred, St, K);
550}
551
552//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000553// Branch processing.
554//===----------------------------------------------------------------------===//
555
Ted Kremeneka8538d92009-02-13 01:45:31 +0000556const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenek4323a572008-07-10 22:03:41 +0000557 Stmt* Terminator,
558 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000559
560 switch (Terminator->getStmtClass()) {
561 default:
Ted Kremeneka8538d92009-02-13 01:45:31 +0000562 return state;
Ted Kremenek05a23782008-02-26 19:05:15 +0000563
564 case Stmt::BinaryOperatorClass: { // '&&' and '||'
565
566 BinaryOperator* B = cast<BinaryOperator>(Terminator);
567 BinaryOperator::Opcode Op = B->getOpcode();
568
569 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
570
571 // For &&, if we take the true branch, then the value of the whole
572 // expression is that of the RHS expression.
573 //
574 // For ||, if we take the false branch, then the value of the whole
575 // expression is that of the RHS expression.
576
577 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
578 (Op == BinaryOperator::LOr && !branchTaken)
579 ? B->getRHS() : B->getLHS();
580
Ted Kremeneka8538d92009-02-13 01:45:31 +0000581 return BindBlkExpr(state, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000582 }
583
584 case Stmt::ConditionalOperatorClass: { // ?:
585
586 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
587
588 // For ?, if branchTaken == true then the value is either the LHS or
589 // the condition itself. (GNU extension).
590
591 Expr* Ex;
592
593 if (branchTaken)
594 Ex = C->getLHS() ? C->getLHS() : C->getCond();
595 else
596 Ex = C->getRHS();
597
Ted Kremeneka8538d92009-02-13 01:45:31 +0000598 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000599 }
600
601 case Stmt::ChooseExprClass: { // ?:
602
603 ChooseExpr* C = cast<ChooseExpr>(Terminator);
604
605 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000606 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000607 }
608 }
609}
610
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000611/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
612/// to try to recover some path-sensitivity for casts of symbolic
613/// integers that promote their values (which are currently not tracked well).
614/// This function returns the SVal bound to Condition->IgnoreCasts if all the
615// cast(s) did was sign-extend the original value.
616static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
617 Stmt* Condition, ASTContext& Ctx) {
618
619 Expr *Ex = dyn_cast<Expr>(Condition);
620 if (!Ex)
621 return UnknownVal();
622
623 uint64_t bits = 0;
624 bool bitsInit = false;
625
626 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
627 QualType T = CE->getType();
628
629 if (!T->isIntegerType())
630 return UnknownVal();
631
632 uint64_t newBits = Ctx.getTypeSize(T);
633 if (!bitsInit || newBits < bits) {
634 bitsInit = true;
635 bits = newBits;
636 }
637
638 Ex = CE->getSubExpr();
639 }
640
641 // We reached a non-cast. Is it a symbolic value?
642 QualType T = Ex->getType();
643
644 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
645 return UnknownVal();
646
647 return StateMgr.GetSVal(state, Ex);
648}
649
Ted Kremenekaf337412008-11-12 19:24:17 +0000650void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000651 BranchNodeBuilder& builder) {
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000652
Ted Kremeneke7d22112008-02-11 19:21:59 +0000653 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000654 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000655 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000656
Ted Kremenekb2331832008-02-15 22:29:00 +0000657 // Check for NULL conditions; e.g. "for(;;)"
658 if (!Condition) {
659 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000660 return;
661 }
662
Ted Kremenek21028dd2009-03-11 03:54:24 +0000663 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
664 Condition->getLocStart(),
665 "Error evaluating branch");
666
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000667 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000668
669 switch (V.getBaseKind()) {
670 default:
671 break;
672
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000673 case SVal::UnknownKind: {
674 if (Expr *Ex = dyn_cast<Expr>(Condition)) {
675 if (Ex->getType()->isIntegerType()) {
676 // Try to recover some path-sensitivity. Right now casts of symbolic
677 // integers that promote their values are currently not tracked well.
678 // If 'Condition' is such an expression, try and recover the
679 // underlying value and use that instead.
680 SVal recovered = RecoverCastedSymbol(getStateManager(),
681 builder.getState(), Condition,
682 getContext());
683
684 if (!recovered.isUnknown()) {
685 V = recovered;
686 break;
687 }
688 }
689 }
690
Ted Kremenek58b33212008-02-26 19:40:44 +0000691 builder.generateNode(MarkBranch(PrevState, Term, true), true);
692 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000693 return;
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000694 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000695
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000696 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000697 NodeTy* N = builder.generateNode(PrevState, true);
698
699 if (N) {
700 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000701 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000702 }
703
704 builder.markInfeasible(false);
705 return;
706 }
707 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000708
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000709 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000710
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000711 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000712 const GRState* state = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000713
714 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000715 builder.generateNode(MarkBranch(state, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000716 else
717 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000718
719 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000720
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000721 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000722 state = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000723
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000724 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000725 builder.generateNode(MarkBranch(state, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000726 else
727 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000728}
729
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000730/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000731/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000732void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000733
Ted Kremeneka8538d92009-02-13 01:45:31 +0000734 const GRState* state = builder.getState();
735 SVal V = GetSVal(state, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000736
737 // Three possibilities:
738 //
739 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000740 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000741 // (3) We have no clue about the label. Dispatch to all targets.
742 //
743
744 typedef IndirectGotoNodeBuilder::iterator iterator;
745
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000746 if (isa<loc::GotoLabel>(V)) {
747 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000748
749 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000750 if (I.getLabel() == L) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000751 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000752 return;
753 }
754 }
755
756 assert (false && "No block with label.");
757 return;
758 }
759
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000760 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000761 // Dispatch to the first target and mark it as a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000762 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000763 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000764 return;
765 }
766
767 // This is really a catch-all. We don't support symbolics yet.
Ted Kremenekb3cfd582009-04-23 17:49:43 +0000768 // FIXME: Implement dispatch for symbolic pointers.
Ted Kremenek754607e2008-02-13 00:24:44 +0000769
770 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000771 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000772}
Ted Kremenekf233d482008-02-05 00:26:40 +0000773
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000774
775void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
776 NodeTy* Pred, NodeSet& Dst) {
777
778 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
779
Ted Kremeneka8538d92009-02-13 01:45:31 +0000780 const GRState* state = GetState(Pred);
781 SVal X = GetBlkExprSVal(state, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000782
783 assert (X.isUndef());
784
785 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
786
787 assert (SE);
788
Ted Kremeneka8538d92009-02-13 01:45:31 +0000789 X = GetBlkExprSVal(state, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000790
791 // Make sure that we invalidate the previous binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000792 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(state, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000793}
794
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000795/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
796/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek72afb372009-01-17 01:54:16 +0000797void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
798 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000799 const GRState* state = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000800 Expr* CondE = builder.getCondition();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000801 SVal CondV = GetSVal(state, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000802
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000803 if (CondV.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000804 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000805 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000806 return;
807 }
Ted Kremenek692416c2008-02-18 22:57:02 +0000808
Ted Kremeneka8538d92009-02-13 01:45:31 +0000809 const GRState* DefaultSt = state;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000810 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000811
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000812 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000813 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek72afb372009-01-17 01:54:16 +0000814
815 // Evaluate the LHS of the case value.
816 Expr::EvalResult V1;
817 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000818
Ted Kremenek72afb372009-01-17 01:54:16 +0000819 // Sanity checks. These go away in Release builds.
820 assert(b && V1.Val.isInt() && !V1.HasSideEffects
821 && "Case condition must evaluate to an integer constant.");
822 b = b; // silence unused variable warning
823 assert(V1.Val.getInt().getBitWidth() ==
824 getContext().getTypeSize(CondE->getType()));
825
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000826 // Get the RHS of the case, if it exists.
Ted Kremenek72afb372009-01-17 01:54:16 +0000827 Expr::EvalResult V2;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000828
829 if (Expr* E = Case->getRHS()) {
Ted Kremenek72afb372009-01-17 01:54:16 +0000830 b = E->Evaluate(V2, getContext());
831 assert(b && V2.Val.isInt() && !V2.HasSideEffects
832 && "Case condition must evaluate to an integer constant.");
833 b = b; // silence unused variable warning
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000834 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000835 else
836 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000837
838 // FIXME: Eventually we should replace the logic below with a range
839 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000840 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000841
Ted Kremenek14a11402008-03-17 22:17:56 +0000842 do {
Ted Kremenek72afb372009-01-17 01:54:16 +0000843 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000844 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal,
845 getContext().IntTy);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000846
Ted Kremenek72afb372009-01-17 01:54:16 +0000847 // Now "assume" that the case matches.
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000848 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000849 const GRState* StNew = Assume(state, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000850
851 if (isFeasible) {
852 builder.generateCaseStmtNode(I, StNew);
853
854 // If CondV evaluates to a constant, then we know that this
855 // is the *only* case that we can take, so stop evaluating the
856 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000857 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000858 return;
859 }
860
861 // Now "assume" that the case doesn't match. Add this state
862 // to the default state (if it is feasible).
863
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000864 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000865 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000866
Ted Kremenek5014ab12008-04-23 05:03:18 +0000867 if (isFeasible) {
868 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000869 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000870 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000871
Ted Kremenek14a11402008-03-17 22:17:56 +0000872 // Concretize the next value in the range.
Ted Kremenek72afb372009-01-17 01:54:16 +0000873 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek14a11402008-03-17 22:17:56 +0000874 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000875
Ted Kremenek72afb372009-01-17 01:54:16 +0000876 ++V1.Val.getInt();
877 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenek14a11402008-03-17 22:17:56 +0000878
879 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000880 }
881
882 // If we reach here, than we know that the default branch is
883 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000884 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000885}
886
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000887//===----------------------------------------------------------------------===//
888// Transfer functions: logical operations ('&&', '||').
889//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000890
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000891void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000892 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000893
Ted Kremenek05a23782008-02-26 19:05:15 +0000894 assert (B->getOpcode() == BinaryOperator::LAnd ||
895 B->getOpcode() == BinaryOperator::LOr);
896
897 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
898
Ted Kremeneka8538d92009-02-13 01:45:31 +0000899 const GRState* state = GetState(Pred);
900 SVal X = GetBlkExprSVal(state, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000901
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000902 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000903
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000904 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000905
906 assert (Ex);
907
908 if (Ex == B->getRHS()) {
909
Ted Kremeneka8538d92009-02-13 01:45:31 +0000910 X = GetBlkExprSVal(state, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000911
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000912 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000913
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000914 if (X.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000915 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000916 return;
917 }
918
Ted Kremenek05a23782008-02-26 19:05:15 +0000919 // We took the RHS. Because the value of the '&&' or '||' expression must
920 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
921 // or 1. Alternatively, we could take a lazy approach, and calculate this
922 // value later when necessary. We don't have the machinery in place for
923 // this right now, and since most logical expressions are used for branches,
924 // the payoff is not likely to be large. Instead, we do eager evaluation.
925
926 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000927 const GRState* NewState = Assume(state, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000928
929 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000930 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000931 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000932
933 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000934 NewState = Assume(state, X, false, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000935
936 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000937 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000938 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000939 }
940 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000941 // We took the LHS expression. Depending on whether we are '&&' or
942 // '||' we know what the value of the expression is via properties of
943 // the short-circuiting.
944
945 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000946 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000947 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000948}
Ted Kremenek05a23782008-02-26 19:05:15 +0000949
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000950//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000951// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000952//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000953
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000954void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
955 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000956
Ted Kremeneka8538d92009-02-13 01:45:31 +0000957 const GRState* state = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000958
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000959 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000960
961 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
962
Ted Kremeneka8538d92009-02-13 01:45:31 +0000963 SVal V = StateMgr.GetLValue(state, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000964
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000965 if (asLValue)
Ted Kremenek7090d542009-05-07 18:27:16 +0000966 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V),
967 ProgramPoint::PostLValueKind);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000968 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000969 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000970 return;
971
972 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
973 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
974
975 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000976 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Ted Kremeneka8538d92009-02-13 01:45:31 +0000977 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000978 return;
979
980 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000981 assert(asLValue);
Zhongxing Xu369f4472009-04-20 05:24:46 +0000982 SVal V = ValMgr.getFunctionPointer(FD);
Ted Kremenek7090d542009-05-07 18:27:16 +0000983 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V),
984 ProgramPoint::PostLValueKind);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000985 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000986 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000987
988 assert (false &&
989 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000990}
991
Ted Kremenek540cbe22008-04-22 04:56:29 +0000992/// VisitArraySubscriptExpr - Transfer function for array accesses
993void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000994 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000995
996 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000997 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000998 NodeSet Tmp;
Ted Kremenek265a3052009-02-24 02:23:11 +0000999
1000 if (Base->getType()->isVectorType()) {
1001 // For vector types get its lvalue.
1002 // FIXME: This may not be correct. Is the rvalue of a vector its location?
1003 // In fact, I think this is just a hack. We need to get the right
1004 // semantics.
1005 VisitLValue(Base, Pred, Tmp);
1006 }
1007 else
1008 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001009
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001010 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001011 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001012 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001013
1014 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001015 const GRState* state = GetState(*I2);
Ted Kremenekf936f452009-05-04 06:18:28 +00001016 SVal V = StateMgr.GetLValue(state, A->getType(),
1017 GetSVal(state, Base),
Ted Kremeneka8538d92009-02-13 01:45:31 +00001018 GetSVal(state, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001019
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001020 if (asLValue)
Ted Kremenek7090d542009-05-07 18:27:16 +00001021 MakeNode(Dst, A, *I2, BindExpr(state, A, V),
1022 ProgramPoint::PostLValueKind);
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001023 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001024 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001025 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001026 }
Ted Kremenek540cbe22008-04-22 04:56:29 +00001027}
1028
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001029/// VisitMemberExpr - Transfer function for member expressions.
1030void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001031 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001032
1033 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001034 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +00001035
1036 if (M->isArrow())
1037 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1038 else
1039 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
1040
Douglas Gregor86f19402008-12-20 23:49:58 +00001041 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1042 if (!Field) // FIXME: skipping member expressions for non-fields
1043 return;
1044
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001045 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001046 const GRState* state = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001047 // FIXME: Should we insert some assumption logic in here to determine
1048 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +00001049 // later when using FieldOffset lvals (which we no longer have).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001050 SVal L = StateMgr.GetLValue(state, GetSVal(state, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001051
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001052 if (asLValue)
Ted Kremenek7090d542009-05-07 18:27:16 +00001053 MakeNode(Dst, M, *I, BindExpr(state, M, L),
1054 ProgramPoint::PostLValueKind);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001055 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001056 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001057 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001058}
1059
Ted Kremeneka8538d92009-02-13 01:45:31 +00001060/// EvalBind - Handle the semantics of binding a value to a specific location.
1061/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
1062void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
1063 const GRState* state, SVal location, SVal Val) {
1064
Ted Kremenek41573eb2009-02-14 01:43:44 +00001065 const GRState* newState = 0;
1066
1067 if (location.isUnknown()) {
1068 // We know that the new state will be the same as the old state since
1069 // the location of the binding is "unknown". Consequently, there
1070 // is no reason to just create a new node.
1071 newState = state;
1072 }
1073 else {
1074 // We are binding to a value other than 'unknown'. Perform the binding
1075 // using the StoreManager.
1076 newState = StateMgr.BindLoc(state, cast<Loc>(location), Val);
1077 }
Ted Kremeneka8538d92009-02-13 01:45:31 +00001078
Ted Kremenek41573eb2009-02-14 01:43:44 +00001079 // The next thing to do is check if the GRTransferFuncs object wants to
1080 // update the state based on the new binding. If the GRTransferFunc object
1081 // doesn't do anything, just auto-propagate the current state.
1082 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1083 newState != state);
1084
1085 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001086}
1087
1088/// EvalStore - Handle the semantics of a store via an assignment.
1089/// @param Dst The node set to store generated state nodes
1090/// @param Ex The expression representing the location of the store
1091/// @param state The current simulation state
1092/// @param location The location to store the value
1093/// @param Val The value to be stored
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001094void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek1670e402009-04-11 00:11:10 +00001095 const GRState* state, SVal location, SVal Val,
1096 const void *tag) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001097
1098 assert (Builder && "GRStmtNodeBuilder must be defined.");
1099
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001100 // Evaluate the location (checks for bad dereferences).
Ted Kremenek1670e402009-04-11 00:11:10 +00001101 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001102
Ted Kremenek8c354752008-12-16 22:02:27 +00001103 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001104 return;
Ted Kremenekb0533962008-04-18 20:35:30 +00001105
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001106 assert (!location.isUndef());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001107 state = GetState(Pred);
1108
1109 // Proceed with the store.
1110 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek1670e402009-04-11 00:11:10 +00001111 SaveAndRestore<const void*> OldTag(Builder->Tag);
1112 Builder->PointKind = ProgramPoint::PostStoreKind;
1113 Builder->Tag = tag;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001114 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001115}
1116
1117void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek1670e402009-04-11 00:11:10 +00001118 const GRState* state, SVal location,
1119 const void *tag) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001120
Ted Kremenek8c354752008-12-16 22:02:27 +00001121 // Evaluate the location (checks for bad dereferences).
Ted Kremenek1670e402009-04-11 00:11:10 +00001122 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001123
Ted Kremenek8c354752008-12-16 22:02:27 +00001124 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001125 return;
1126
Ted Kremeneka8538d92009-02-13 01:45:31 +00001127 state = GetState(Pred);
Ted Kremenek8c354752008-12-16 22:02:27 +00001128
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001129 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +00001130 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001131
1132 // FIXME: Currently symbolic analysis "generates" new symbols
1133 // for the contents of values. We need a better approach.
1134
Ted Kremenek8c354752008-12-16 22:02:27 +00001135 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +00001136 // This is important. We must nuke the old binding.
Ted Kremenek1670e402009-04-11 00:11:10 +00001137 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K, tag);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001138 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001139 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001140 SVal V = GetSVal(state, cast<Loc>(location), Ex->getType());
Ted Kremenek1670e402009-04-11 00:11:10 +00001141 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K, tag);
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001142 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001143}
1144
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001145void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremenek1670e402009-04-11 00:11:10 +00001146 const GRState* state, SVal location, SVal Val,
1147 const void *tag) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001148
1149 NodeSet TmpDst;
Ted Kremenek1670e402009-04-11 00:11:10 +00001150 EvalStore(TmpDst, StoreE, Pred, state, location, Val, tag);
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001151
1152 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
Ted Kremenek1670e402009-04-11 00:11:10 +00001153 MakeNode(Dst, Ex, *I, (*I)->getState(), ProgramPoint::PostStmtKind, tag);
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001154}
1155
Ted Kremenek8c354752008-12-16 22:02:27 +00001156GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001157 const GRState* state,
Ted Kremenek1670e402009-04-11 00:11:10 +00001158 SVal location,
1159 const void *tag) {
1160
1161 SaveAndRestore<const void*> OldTag(Builder->Tag);
1162 Builder->Tag = tag;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001163
1164 // Check for loads/stores from/to undefined values.
1165 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001166 NodeTy* N =
Ted Kremeneka8538d92009-02-13 01:45:31 +00001167 Builder->generateNode(Ex, state, Pred,
Ted Kremenek8c354752008-12-16 22:02:27 +00001168 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001169
Ted Kremenek8c354752008-12-16 22:02:27 +00001170 if (N) {
1171 N->markAsSink();
1172 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001173 }
1174
Ted Kremenek8c354752008-12-16 22:02:27 +00001175 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001176 }
1177
1178 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1179 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001180 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001181
1182 // During a load, one of two possible situations arise:
1183 // (1) A crash, because the location (pointer) was NULL.
1184 // (2) The location (pointer) is not NULL, and the dereference works.
1185 //
1186 // We add these assumptions.
1187
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001188 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001189
1190 // "Assume" that the pointer is not NULL.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001191 bool isFeasibleNotNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001192 const GRState* StNotNull = Assume(state, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001193
1194 // "Assume" that the pointer is NULL.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001195 bool isFeasibleNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001196 GRStateRef StNull = GRStateRef(Assume(state, LV, false, isFeasibleNull),
Ted Kremenek7360fda2008-09-18 23:09:54 +00001197 getStateManager());
Zhongxing Xua1718c72009-04-03 07:33:13 +00001198
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001199 if (isFeasibleNull) {
1200
Ted Kremenek7360fda2008-09-18 23:09:54 +00001201 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001202 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001203 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1204
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001205 // We don't use "MakeNode" here because the node will be a sink
1206 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001207 NodeTy* NullNode =
1208 Builder->generateNode(Ex, StNull, Pred,
1209 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001210
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001211 if (NullNode) {
1212
1213 NullNode->markAsSink();
1214
1215 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1216 else ExplicitNullDeref.insert(NullNode);
1217 }
1218 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001219
1220 if (!isFeasibleNotNull)
1221 return 0;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001222
1223 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001224 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001225 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1226 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1227 // Get the index of the accessed element.
1228 SVal Idx = ER->getIndex();
1229 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001230 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1231 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001232
1233 bool isFeasibleInBound = false;
1234 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1235 true, isFeasibleInBound);
1236
1237 bool isFeasibleOutBound = false;
1238 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1239 false, isFeasibleOutBound);
1240
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001241 if (isFeasibleOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001242 // Report warning. Make sink node manually.
1243 NodeTy* OOBNode =
1244 Builder->generateNode(Ex, StOutBound, Pred,
1245 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001246
1247 if (OOBNode) {
1248 OOBNode->markAsSink();
1249
1250 if (isFeasibleInBound)
1251 ImplicitOOBMemAccesses.insert(OOBNode);
1252 else
1253 ExplicitOOBMemAccesses.insert(OOBNode);
1254 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001255 }
1256
Ted Kremenek8c354752008-12-16 22:02:27 +00001257 if (!isFeasibleInBound)
1258 return 0;
1259
1260 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001261 }
1262 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001263
Ted Kremenek8c354752008-12-16 22:02:27 +00001264 // Generate a new node indicating the checks succeed.
1265 return Builder->generateNode(Ex, StNotNull, Pred,
1266 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001267}
1268
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001269//===----------------------------------------------------------------------===//
Ted Kremenek1670e402009-04-11 00:11:10 +00001270// Transfer function: OSAtomics.
1271//
1272// FIXME: Eventually refactor into a more "plugin" infrastructure.
1273//===----------------------------------------------------------------------===//
1274
1275// Mac OS X:
1276// http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3
1277// atomic.3.html
1278//
1279static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet<GRState>& Dst,
1280 GRExprEngine& Engine,
1281 GRStmtNodeBuilder<GRState>& Builder,
1282 CallExpr* CE, SVal L,
1283 ExplodedNode<GRState>* Pred) {
1284
1285 // Not enough arguments to match OSAtomicCompareAndSwap?
1286 if (CE->getNumArgs() != 3)
1287 return false;
1288
1289 ASTContext &C = Engine.getContext();
1290 Expr *oldValueExpr = CE->getArg(0);
1291 QualType oldValueType = C.getCanonicalType(oldValueExpr->getType());
1292
1293 Expr *newValueExpr = CE->getArg(1);
1294 QualType newValueType = C.getCanonicalType(newValueExpr->getType());
1295
1296 // Do the types of 'oldValue' and 'newValue' match?
1297 if (oldValueType != newValueType)
1298 return false;
1299
1300 Expr *theValueExpr = CE->getArg(2);
1301 const PointerType *theValueType = theValueExpr->getType()->getAsPointerType();
1302
1303 // theValueType not a pointer?
1304 if (!theValueType)
1305 return false;
1306
1307 QualType theValueTypePointee =
1308 C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
1309
1310 // The pointee must match newValueType and oldValueType.
1311 if (theValueTypePointee != newValueType)
1312 return false;
1313
1314 static unsigned magic_load = 0;
1315 static unsigned magic_store = 0;
1316
1317 const void *OSAtomicLoadTag = &magic_load;
1318 const void *OSAtomicStoreTag = &magic_store;
1319
1320 // Load 'theValue'.
1321 GRStateManager &StateMgr = Engine.getStateManager();
1322 const GRState *state = Pred->getState();
1323 ExplodedNodeSet<GRState> Tmp;
1324 SVal location = StateMgr.GetSVal(state, theValueExpr);
1325 Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag);
1326
1327 for (ExplodedNodeSet<GRState>::iterator I = Tmp.begin(), E = Tmp.end();
1328 I != E; ++I) {
1329
1330 ExplodedNode<GRState> *N = *I;
1331 const GRState *stateLoad = N->getState();
1332 SVal theValueVal = StateMgr.GetSVal(stateLoad, theValueExpr);
1333 SVal oldValueVal = StateMgr.GetSVal(stateLoad, oldValueExpr);
1334
1335 // Perform the comparison.
1336 SVal Cmp = Engine.EvalBinOp(BinaryOperator::EQ, theValueVal, oldValueVal,
1337 Engine.getContext().IntTy);
1338 bool isFeasible = false;
1339 const GRState *stateEqual = StateMgr.Assume(stateLoad, Cmp, true,
1340 isFeasible);
1341
1342 // Were they equal?
1343 if (isFeasible) {
1344 // Perform the store.
1345 ExplodedNodeSet<GRState> TmpStore;
1346 Engine.EvalStore(TmpStore, theValueExpr, N, stateEqual, location,
1347 StateMgr.GetSVal(stateEqual, newValueExpr),
1348 OSAtomicStoreTag);
1349
1350 // Now bind the result of the comparison.
1351 for (ExplodedNodeSet<GRState>::iterator I2 = TmpStore.begin(),
1352 E2 = TmpStore.end(); I2 != E2; ++I2) {
1353 ExplodedNode<GRState> *predNew = *I2;
1354 const GRState *stateNew = predNew->getState();
1355 SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType());
1356 Engine.MakeNode(Dst, CE, predNew, Engine.BindExpr(stateNew, CE, Res));
1357 }
1358 }
1359
1360 // Were they not equal?
1361 isFeasible = false;
1362 const GRState *stateNotEqual = StateMgr.Assume(stateLoad, Cmp, false,
1363 isFeasible);
1364
1365 if (isFeasible) {
1366 SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType());
1367 Engine.MakeNode(Dst, CE, N, Engine.BindExpr(stateNotEqual, CE, Res));
1368 }
1369 }
1370
1371 return true;
1372}
1373
1374static bool EvalOSAtomic(ExplodedNodeSet<GRState>& Dst,
1375 GRExprEngine& Engine,
1376 GRStmtNodeBuilder<GRState>& Builder,
1377 CallExpr* CE, SVal L,
1378 ExplodedNode<GRState>* Pred) {
Zhongxing Xu369f4472009-04-20 05:24:46 +00001379 const FunctionDecl* FD = L.getAsFunctionDecl();
1380 if (!FD)
Ted Kremenek1670e402009-04-11 00:11:10 +00001381 return false;
Zhongxing Xu369f4472009-04-20 05:24:46 +00001382
Ted Kremenek1670e402009-04-11 00:11:10 +00001383 const char *FName = FD->getNameAsCString();
1384
1385 // Check for compare and swap.
Ted Kremenekb3bf76f2009-04-11 00:54:13 +00001386 if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0 ||
1387 strncmp(FName, "objc_atomicCompareAndSwap", 25) == 0)
Ted Kremenek1670e402009-04-11 00:11:10 +00001388 return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred);
Ted Kremenekb3bf76f2009-04-11 00:54:13 +00001389
Ted Kremenek1670e402009-04-11 00:11:10 +00001390 // FIXME: Other atomics.
1391 return false;
1392}
1393
1394//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001395// Transfer function: Function calls.
1396//===----------------------------------------------------------------------===//
Ted Kremenek1670e402009-04-11 00:11:10 +00001397
1398void GRExprEngine::EvalCall(NodeSet& Dst, CallExpr* CE, SVal L, NodeTy* Pred) {
1399 assert (Builder && "GRStmtNodeBuilder must be defined.");
1400
1401 // FIXME: Allow us to chain together transfer functions.
1402 if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred))
1403 return;
1404
1405 getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred);
1406}
1407
Ted Kremenekde434242008-02-19 01:44:53 +00001408void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001409 CallExpr::arg_iterator AI,
1410 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001411 NodeSet& Dst)
1412{
1413 // Determine the type of function we're calling (if available).
Douglas Gregor72564e72009-02-26 23:50:07 +00001414 const FunctionProtoType *Proto = NULL;
Douglas Gregor9d293df2008-10-28 00:22:11 +00001415 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1416 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
Douglas Gregor72564e72009-02-26 23:50:07 +00001417 Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
Douglas Gregor9d293df2008-10-28 00:22:11 +00001418
1419 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1420}
1421
1422void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1423 CallExpr::arg_iterator AI,
1424 CallExpr::arg_iterator AE,
Douglas Gregor72564e72009-02-26 23:50:07 +00001425 NodeSet& Dst, const FunctionProtoType *Proto,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001426 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001427
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001428 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001429 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001430 // If the call argument is being bound to a reference parameter,
1431 // visit it as an lvalue, not an rvalue.
1432 bool VisitAsLvalue = false;
1433 if (Proto && ParamIdx < Proto->getNumArgs())
1434 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1435
1436 NodeSet DstTmp;
1437 if (VisitAsLvalue)
1438 VisitLValue(*AI, Pred, DstTmp);
1439 else
1440 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001441 ++AI;
1442
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001443 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001444 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001445
1446 return;
1447 }
1448
1449 // If we reach here we have processed all of the arguments. Evaluate
1450 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001451
Ted Kremenek994a09b2008-02-25 21:16:03 +00001452 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001453 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001454
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001455 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001456
Ted Kremenekde434242008-02-19 01:44:53 +00001457 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001458 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1459
Ted Kremeneka8538d92009-02-13 01:45:31 +00001460 const GRState* state = GetState(*DI);
1461 SVal L = GetSVal(state, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001462
Ted Kremeneka1354a52008-03-03 16:47:31 +00001463 // FIXME: Add support for symbolic function calls (calls involving
1464 // function pointer values that are symbolic).
1465
1466 // Check for undefined control-flow or calls to NULL.
1467
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001468 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001469 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001470
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001471 if (N) {
1472 N->markAsSink();
1473 BadCalls.insert(N);
1474 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001475
Ted Kremenekde434242008-02-19 01:44:53 +00001476 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001477 }
1478
1479 // Check for the "noreturn" attribute.
1480
1481 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Zhongxing Xu369f4472009-04-20 05:24:46 +00001482 const FunctionDecl* FD = L.getAsFunctionDecl();
1483 if (FD) {
Ted Kremenekb7252322009-04-10 00:01:14 +00001484 if (FD->getAttr<NoReturnAttr>() || FD->getAttr<AnalyzerNoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001485 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001486 else {
1487 // HACK: Some functions are not marked noreturn, and don't return.
1488 // Here are a few hardwired ones. If this takes too long, we can
1489 // potentially cache these results.
1490 const char* s = FD->getIdentifier()->getName();
1491 unsigned n = strlen(s);
1492
1493 switch (n) {
1494 default:
1495 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001496
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001497 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001498 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1499 break;
1500
1501 case 5:
1502 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001503 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001504 if (CE->getNumArgs() > 0) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001505 SVal X = GetSVal(state, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001506 // FIXME: use Assume to inspect the possible symbolic value of
1507 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001508 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001509 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001510 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001511 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001512 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001513 break;
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001514
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001515 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001516 if (!memcmp(s, "Assert", 6)) {
1517 Builder->BuildSinks = true;
1518 break;
1519 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001520
1521 // FIXME: This is just a wrapper around throwing an exception.
1522 // Eventually inter-procedural analysis should handle this easily.
1523 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1524
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001525 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001526
1527 case 7:
1528 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1529 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001530
Ted Kremenekf47bb782008-04-30 17:54:04 +00001531 case 8:
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001532 if (!memcmp(s ,"db_error", 8) ||
1533 !memcmp(s, "__assert", 8))
1534 Builder->BuildSinks = true;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001535 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001536
1537 case 12:
1538 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1539 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001540
Ted Kremenekf9683082008-09-19 02:30:47 +00001541 case 13:
1542 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1543 break;
1544
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001545 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001546 if (!memcmp(s, "dtrace_assfail", 14) ||
1547 !memcmp(s, "yy_fatal_error", 14))
1548 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001549 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001550
1551 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001552 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek40bbff02009-02-17 23:27:17 +00001553 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1554 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001555 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001556
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001557 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001558 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001559
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001560 }
1561 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001562
1563 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001564
Zhongxing Xu369f4472009-04-20 05:24:46 +00001565 if (FD) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001566
Zhongxing Xu369f4472009-04-20 05:24:46 +00001567 if (unsigned id = FD->getBuiltinID(getContext()))
Ted Kremenek55aea312008-03-05 22:59:42 +00001568 switch (id) {
1569 case Builtin::BI__builtin_expect: {
1570 // For __builtin_expect, just return the value of the subexpression.
1571 assert (CE->arg_begin() != CE->arg_end());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001572 SVal X = GetSVal(state, *(CE->arg_begin()));
1573 MakeNode(Dst, CE, *DI, BindExpr(state, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001574 continue;
1575 }
1576
Ted Kremenekb3021332008-11-02 00:35:01 +00001577 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001578 // FIXME: Refactor into StoreManager itself?
1579 MemRegionManager& RM = getStateManager().getRegionManager();
1580 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001581 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001582
1583 // Set the extent of the region in bytes. This enables us to use the
1584 // SVal of the argument directly. If we save the extent in bits, we
1585 // cannot represent values like symbol*8.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001586 SVal Extent = GetSVal(state, *(CE->arg_begin()));
1587 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001588
Ted Kremeneka8538d92009-02-13 01:45:31 +00001589 MakeNode(Dst, CE, *DI, BindExpr(state, CE, loc::MemRegionVal(R)));
Ted Kremenekb3021332008-11-02 00:35:01 +00001590 continue;
1591 }
1592
Ted Kremenek55aea312008-03-05 22:59:42 +00001593 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001594 break;
1595 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001596 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001597
Ted Kremenek186350f2008-04-23 20:12:28 +00001598 // Check any arguments passed-by-value against being undefined.
1599
1600 bool badArg = false;
1601
1602 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1603 I != E; ++I) {
1604
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001605 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001606 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001607
Ted Kremenek186350f2008-04-23 20:12:28 +00001608 if (N) {
1609 N->markAsSink();
1610 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001611 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001612
Ted Kremenek186350f2008-04-23 20:12:28 +00001613 badArg = true;
1614 break;
1615 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001616 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001617
1618 if (badArg)
1619 continue;
1620
1621 // Dispatch to the plug-in transfer function.
1622
1623 unsigned size = Dst.size();
1624 SaveOr OldHasGen(Builder->HasGeneratedNode);
1625 EvalCall(Dst, CE, L, *DI);
1626
1627 // Handle the case where no nodes where generated. Auto-generate that
1628 // contains the updated state if we aren't generating sinks.
1629
1630 if (!Builder->BuildSinks && Dst.size() == size &&
1631 !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001632 MakeNode(Dst, CE, *DI, state);
Ted Kremenekde434242008-02-19 01:44:53 +00001633 }
1634}
1635
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001636//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001637// Transfer function: Objective-C ivar references.
1638//===----------------------------------------------------------------------===//
1639
Ted Kremenekf5cae632009-02-28 20:50:43 +00001640static std::pair<const void*,const void*> EagerlyAssumeTag
1641 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1642
Ted Kremenekb2939022009-02-25 23:32:10 +00001643void GRExprEngine::EvalEagerlyAssume(NodeSet &Dst, NodeSet &Src, Expr *Ex) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001644 for (NodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1645 NodeTy *Pred = *I;
Ted Kremenekb2939022009-02-25 23:32:10 +00001646
1647 // Test if the previous node was as the same expression. This can happen
1648 // when the expression fails to evaluate to anything meaningful and
1649 // (as an optimization) we don't generate a node.
1650 ProgramPoint P = Pred->getLocation();
1651 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
1652 Dst.Add(Pred);
1653 continue;
1654 }
1655
Ted Kremenek48af2a92009-02-25 22:32:02 +00001656 const GRState* state = Pred->getState();
Ted Kremenekb2939022009-02-25 23:32:10 +00001657 SVal V = GetSVal(state, Ex);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00001658 if (isa<nonloc::SymExprVal>(V)) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001659 // First assume that the condition is true.
1660 bool isFeasible = false;
1661 const GRState *stateTrue = Assume(state, V, true, isFeasible);
1662 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001663 stateTrue = BindExpr(stateTrue, Ex, MakeConstantVal(1U, Ex));
1664 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001665 stateTrue, Pred));
1666 }
1667
1668 // Next, assume that the condition is false.
1669 isFeasible = false;
1670 const GRState *stateFalse = Assume(state, V, false, isFeasible);
1671 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001672 stateFalse = BindExpr(stateFalse, Ex, MakeConstantVal(0U, Ex));
1673 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001674 stateFalse, Pred));
1675 }
1676 }
1677 else
1678 Dst.Add(Pred);
1679 }
1680}
1681
1682//===----------------------------------------------------------------------===//
1683// Transfer function: Objective-C ivar references.
1684//===----------------------------------------------------------------------===//
1685
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001686void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1687 NodeTy* Pred, NodeSet& Dst,
1688 bool asLValue) {
1689
1690 Expr* Base = cast<Expr>(Ex->getBase());
1691 NodeSet Tmp;
1692 Visit(Base, Pred, Tmp);
1693
1694 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001695 const GRState* state = GetState(*I);
1696 SVal BaseVal = GetSVal(state, Base);
1697 SVal location = StateMgr.GetLValue(state, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001698
1699 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001700 MakeNode(Dst, Ex, *I, BindExpr(state, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001701 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001702 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001703 }
1704}
1705
1706//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001707// Transfer function: Objective-C fast enumeration 'for' statements.
1708//===----------------------------------------------------------------------===//
1709
1710void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1711 NodeTy* Pred, NodeSet& Dst) {
1712
1713 // ObjCForCollectionStmts are processed in two places. This method
1714 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1715 // statements within a basic block. This transfer function does two things:
1716 //
1717 // (1) binds the next container value to 'element'. This creates a new
1718 // node in the ExplodedGraph.
1719 //
1720 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1721 // whether or not the container has any more elements. This value
1722 // will be tested in ProcessBranch. We need to explicitly bind
1723 // this value because a container can contain nil elements.
1724 //
1725 // FIXME: Eventually this logic should actually do dispatches to
1726 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1727 // This will require simulating a temporary NSFastEnumerationState, either
1728 // through an SVal or through the use of MemRegions. This value can
1729 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1730 // terminates we reclaim the temporary (it goes out of scope) and we
1731 // we can test if the SVal is 0 or if the MemRegion is null (depending
1732 // on what approach we take).
1733 //
1734 // For now: simulate (1) by assigning either a symbol or nil if the
1735 // container is empty. Thus this transfer function will by default
1736 // result in state splitting.
1737
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001738 Stmt* elem = S->getElement();
1739 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001740
1741 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner7e24e822009-03-28 06:33:19 +00001742 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001743 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001744 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1745 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1746 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001747 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001748
1749 NodeSet Tmp;
1750 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001751
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001752 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1753 const GRState* state = GetState(*I);
1754 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1755 }
1756}
1757
1758void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1759 NodeTy* Pred, NodeSet& Dst,
1760 SVal ElementV) {
1761
1762
Ted Kremenekaf337412008-11-12 19:24:17 +00001763
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001764 // Get the current state. Use 'EvalLocation' to determine if it is a null
1765 // pointer, etc.
1766 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001767
Ted Kremenek8c354752008-12-16 22:02:27 +00001768 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1769 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001770 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001771
1772 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001773
Ted Kremenekaf337412008-11-12 19:24:17 +00001774 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001775 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001776 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1777 GRStateRef hasElems = state.BindExpr(S, TrueV);
1778
Ted Kremenekaf337412008-11-12 19:24:17 +00001779 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001780 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1781 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001782
1783 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1784 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1785 // FIXME: The proper thing to do is to really iterate over the
1786 // container. We will do this with dispatch logic to the store.
1787 // For now, just 'conjure' up a symbolic value.
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001788 QualType T = R->getRValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001789 assert (Loc::IsLocType(T));
1790 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xuea7c5ce2009-04-09 06:49:52 +00001791 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
1792 SVal V = Loc::MakeVal(getStoreManager().getRegionManager().getSymbolicRegion(Sym));
1793 hasElems = hasElems.BindLoc(ElementV, V);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001794
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001795 // Bind the location to 'nil' on the false branch.
1796 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1797 noElems = noElems.BindLoc(ElementV, nilV);
1798 }
1799
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001800 // Create the new nodes.
1801 MakeNode(Dst, S, Pred, hasElems);
1802 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001803}
1804
1805//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001806// Transfer function: Objective-C message expressions.
1807//===----------------------------------------------------------------------===//
1808
1809void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1810 NodeSet& Dst){
1811
1812 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1813 Pred, Dst);
1814}
1815
1816void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001817 ObjCMessageExpr::arg_iterator AI,
1818 ObjCMessageExpr::arg_iterator AE,
1819 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001820 if (AI == AE) {
1821
1822 // Process the receiver.
1823
1824 if (Expr* Receiver = ME->getReceiver()) {
1825 NodeSet Tmp;
1826 Visit(Receiver, Pred, Tmp);
1827
1828 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1829 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1830
1831 return;
1832 }
1833
1834 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1835 return;
1836 }
1837
1838 NodeSet Tmp;
1839 Visit(*AI, Pred, Tmp);
1840
1841 ++AI;
1842
1843 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1844 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1845}
1846
1847void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1848 NodeTy* Pred,
1849 NodeSet& Dst) {
1850
1851 // FIXME: More logic for the processing the method call.
1852
Ted Kremeneka8538d92009-02-13 01:45:31 +00001853 const GRState* state = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001854 bool RaisesException = false;
1855
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001856
1857 if (Expr* Receiver = ME->getReceiver()) {
1858
Ted Kremeneka8538d92009-02-13 01:45:31 +00001859 SVal L = GetSVal(state, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001860
Ted Kremenek21fe8372009-02-19 04:06:22 +00001861 // Check for undefined control-flow.
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001862 if (L.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001863 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001864
1865 if (N) {
1866 N->markAsSink();
1867 UndefReceivers.insert(N);
1868 }
1869
1870 return;
1871 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001872
Ted Kremenek21fe8372009-02-19 04:06:22 +00001873 // "Assume" that the receiver is not NULL.
1874 bool isFeasibleNotNull = false;
Ted Kremenekda9ae602009-04-08 18:51:08 +00001875 const GRState *StNotNull = Assume(state, L, true, isFeasibleNotNull);
Ted Kremenek21fe8372009-02-19 04:06:22 +00001876
1877 // "Assume" that the receiver is NULL.
1878 bool isFeasibleNull = false;
1879 const GRState *StNull = Assume(state, L, false, isFeasibleNull);
1880
1881 if (isFeasibleNull) {
Ted Kremenekfe630b92009-04-09 05:45:56 +00001882 QualType RetTy = ME->getType();
1883
Ted Kremenek21fe8372009-02-19 04:06:22 +00001884 // Check if the receiver was nil and the return value a struct.
Ted Kremenekfe630b92009-04-09 05:45:56 +00001885 if(RetTy->isRecordType()) {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001886 if (BR.getParentMap().isConsumedExpr(ME)) {
Ted Kremenek899b3de2009-04-08 03:07:17 +00001887 // The [0 ...] expressions will return garbage. Flag either an
1888 // explicit or implicit error. Because of the structure of this
1889 // function we currently do not bifurfacte the state graph at
1890 // this point.
1891 // FIXME: We should bifurcate and fill the returned struct with
1892 // garbage.
1893 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1894 N->markAsSink();
1895 if (isFeasibleNotNull)
1896 NilReceiverStructRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001897 else
Ted Kremeneke6449392009-04-09 04:06:51 +00001898 NilReceiverStructRetExplicit.insert(N);
Ted Kremenek899b3de2009-04-08 03:07:17 +00001899 }
1900 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001901 }
Ted Kremenekfe630b92009-04-09 05:45:56 +00001902 else {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001903 ASTContext& Ctx = getContext();
Ted Kremenekfe630b92009-04-09 05:45:56 +00001904 if (RetTy != Ctx.VoidTy) {
1905 if (BR.getParentMap().isConsumedExpr(ME)) {
1906 // sizeof(void *)
1907 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
1908 // sizeof(return type)
1909 const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001910
Ted Kremenekfe630b92009-04-09 05:45:56 +00001911 if(voidPtrSize < returnTypeSize) {
1912 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1913 N->markAsSink();
1914 if(isFeasibleNotNull)
1915 NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001916 else
Ted Kremenekfe630b92009-04-09 05:45:56 +00001917 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
Ted Kremenekfe630b92009-04-09 05:45:56 +00001918 }
1919 }
1920 else if (!isFeasibleNotNull) {
1921 // Handle the safe cases where the return value is 0 if the
1922 // receiver is nil.
1923 //
1924 // FIXME: For now take the conservative approach that we only
1925 // return null values if we *know* that the receiver is nil.
1926 // This is because we can have surprises like:
1927 //
1928 // ... = [[NSScreens screens] objectAtIndex:0];
1929 //
1930 // What can happen is that [... screens] could return nil, but
1931 // it most likely isn't nil. We should assume the semantics
1932 // of this case unless we have *a lot* more knowledge.
1933 //
Ted Kremenek8e5fb282009-04-09 16:46:55 +00001934 SVal V = ValMgr.makeZeroVal(ME->getType());
Ted Kremenekfe630b92009-04-09 05:45:56 +00001935 MakeNode(Dst, ME, Pred, BindExpr(StNull, ME, V));
Ted Kremeneke6449392009-04-09 04:06:51 +00001936 return;
1937 }
Ted Kremenek899b3de2009-04-08 03:07:17 +00001938 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001939 }
Ted Kremenek21fe8372009-02-19 04:06:22 +00001940 }
Ted Kremenekda9ae602009-04-08 18:51:08 +00001941 // We have handled the cases where the receiver is nil. The remainder
Ted Kremenekf8769c82009-04-09 06:02:06 +00001942 // of this method should assume that the receiver is not nil.
1943 if (!StNotNull)
1944 return;
1945
Ted Kremenekda9ae602009-04-08 18:51:08 +00001946 state = StNotNull;
Ted Kremenek21fe8372009-02-19 04:06:22 +00001947 }
1948
Ted Kremeneke448ab42008-05-01 18:33:28 +00001949 // Check if the "raise" message was sent.
1950 if (ME->getSelector() == RaiseSel)
1951 RaisesException = true;
1952 }
1953 else {
1954
1955 IdentifierInfo* ClsName = ME->getClassName();
1956 Selector S = ME->getSelector();
1957
1958 // Check for special instance methods.
1959
1960 if (!NSExceptionII) {
1961 ASTContext& Ctx = getContext();
1962
1963 NSExceptionII = &Ctx.Idents.get("NSException");
1964 }
1965
1966 if (ClsName == NSExceptionII) {
1967
1968 enum { NUM_RAISE_SELECTORS = 2 };
1969
1970 // Lazily create a cache of the selectors.
1971
1972 if (!NSExceptionInstanceRaiseSelectors) {
1973
1974 ASTContext& Ctx = getContext();
1975
1976 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1977
1978 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1979 unsigned idx = 0;
1980
1981 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001982 II.push_back(&Ctx.Idents.get("raise"));
1983 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001984 NSExceptionInstanceRaiseSelectors[idx++] =
1985 Ctx.Selectors.getSelector(II.size(), &II[0]);
1986
1987 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001988 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001989 NSExceptionInstanceRaiseSelectors[idx++] =
1990 Ctx.Selectors.getSelector(II.size(), &II[0]);
1991 }
1992
1993 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1994 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1995 RaisesException = true; break;
1996 }
1997 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001998 }
1999
2000 // Check for any arguments that are uninitialized/undefined.
2001
2002 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
2003 I != E; ++I) {
2004
Ted Kremeneka8538d92009-02-13 01:45:31 +00002005 if (GetSVal(state, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002006
2007 // Generate an error node for passing an uninitialized/undefined value
2008 // as an argument to a message expression. This node is a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002009 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002010
2011 if (N) {
2012 N->markAsSink();
2013 MsgExprUndefArgs[N] = *I;
2014 }
2015
2016 return;
2017 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00002018 }
2019
2020 // Check if we raise an exception. For now treat these as sinks. Eventually
2021 // we will want to handle exceptions properly.
2022
2023 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2024
2025 if (RaisesException)
2026 Builder->BuildSinks = true;
2027
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002028 // Dispatch to plug-in transfer function.
2029
2030 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00002031 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002032
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002033 EvalObjCMessageExpr(Dst, ME, Pred);
2034
2035 // Handle the case where no nodes where generated. Auto-generate that
2036 // contains the updated state if we aren't generating sinks.
2037
Ted Kremenekb0533962008-04-18 20:35:30 +00002038 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002039 MakeNode(Dst, ME, Pred, state);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002040}
2041
2042//===----------------------------------------------------------------------===//
2043// Transfer functions: Miscellaneous statements.
2044//===----------------------------------------------------------------------===//
2045
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002046void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
2047 QualType PtrTy,
2048 Expr* CastE, NodeTy* Pred,
2049 NodeSet& Dst) {
2050 if (!V.isUnknownOrUndef()) {
2051 // FIXME: Determine if the number of bits of the target type is
2052 // equal or exceeds the number of bits to store the pointer value.
Ted Kremeneke121da42009-03-05 03:42:31 +00002053 // If not, flag an error.
Ted Kremenekac78d6b2009-03-05 03:44:53 +00002054 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, EvalCast(cast<Loc>(V),
2055 CastE->getType())));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002056 }
Ted Kremeneke121da42009-03-05 03:42:31 +00002057 else
2058 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002059}
2060
2061
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002062void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002063 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002064 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00002065 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00002066
Zhongxing Xud3118bd2008-10-31 07:26:14 +00002067 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00002068 T = ExCast->getTypeAsWritten();
2069
Zhongxing Xued340f72008-10-22 08:02:16 +00002070 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002071 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00002072 else
2073 Visit(Ex, Pred, S1);
2074
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002075 // Check for casting to "void".
Ted Kremenekdc402902009-03-04 00:14:35 +00002076 if (T->isVoidType()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002077 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002078 Dst.Add(*I1);
2079
Ted Kremenek874d63f2008-01-24 02:02:54 +00002080 return;
2081 }
2082
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002083 // FIXME: The rest of this should probably just go into EvalCall, and
2084 // let the transfer function object be responsible for constructing
2085 // nodes.
2086
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002087 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00002088 NodeTy* N = *I1;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002089 const GRState* state = GetState(N);
2090 SVal V = GetSVal(state, Ex);
Ted Kremenekc5302912009-03-05 20:22:13 +00002091 ASTContext& C = getContext();
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002092
2093 // Unknown?
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002094 if (V.isUnknown()) {
2095 Dst.Add(N);
2096 continue;
2097 }
2098
2099 // Undefined?
Ted Kremenekc5302912009-03-05 20:22:13 +00002100 if (V.isUndef())
2101 goto PassThrough;
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00002102
2103 // For const casts, just propagate the value.
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00002104 if (C.getCanonicalType(T).getUnqualifiedType() ==
Ted Kremenekc5302912009-03-05 20:22:13 +00002105 C.getCanonicalType(ExTy).getUnqualifiedType())
2106 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00002107
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002108 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002109 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002110 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002111 continue;
2112 }
2113
2114 // Check for casts from integers to pointers.
Ted Kremenek16aac322009-03-05 02:33:55 +00002115 if (Loc::IsLocType(T) && ExTy->isIntegerType()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002116 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002117 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002118 V = LV->getLoc();
Ted Kremeneka8538d92009-02-13 01:45:31 +00002119 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenekc5302912009-03-05 20:22:13 +00002120 continue;
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002121 }
Ted Kremeneke121da42009-03-05 03:42:31 +00002122
Ted Kremenekc5302912009-03-05 20:22:13 +00002123 goto DispatchCast;
Ted Kremenek16aac322009-03-05 02:33:55 +00002124 }
2125
2126 // Just pass through function and block pointers.
2127 if (ExTy->isBlockPointerType() || ExTy->isFunctionPointerType()) {
2128 assert(Loc::IsLocType(T));
Ted Kremenekc5302912009-03-05 20:22:13 +00002129 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00002130 }
2131
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002132 // Check for casts from array type to another type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00002133 if (ExTy->isArrayType()) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002134 // We will always decay to a pointer.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +00002135 V = StateMgr.ArrayToPointer(cast<Loc>(V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002136
2137 // Are we casting from an array to a pointer? If so just pass on
2138 // the decayed value.
Ted Kremenekc5302912009-03-05 20:22:13 +00002139 if (T->isPointerType())
2140 goto PassThrough;
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002141
2142 // Are we casting from an array to an integer? If so, cast the decayed
2143 // pointer value to an integer.
2144 assert(T->isIntegerType());
2145 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
2146 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002147 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xue1911af2008-10-23 03:10:39 +00002148 continue;
2149 }
2150
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002151 // Check for casts from a region to a specific type.
Ted Kremenek5c42f9b2009-03-05 22:47:06 +00002152 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
2153 // FIXME: For TypedViewRegions, we should handle the case where the
2154 // underlying symbolic pointer is a function pointer or
2155 // block pointer.
2156
2157 // FIXME: We should handle the case where we strip off view layers to get
2158 // to a desugared type.
2159
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002160 assert(Loc::IsLocType(T));
Zhongxing Xua1718c72009-04-03 07:33:13 +00002161 // We get a symbolic function pointer for a dereference of a function
2162 // pointer, but it is of function type. Example:
2163
2164 // struct FPRec {
2165 // void (*my_func)(int * x);
2166 // };
2167 //
2168 // int bar(int x);
2169 //
2170 // int f1_a(struct FPRec* foo) {
2171 // int x;
2172 // (*foo->my_func)(&x);
2173 // return bar(x)+1; // no-warning
2174 // }
2175
2176 assert(Loc::IsLocType(ExTy) || ExTy->isFunctionType());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002177
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002178 const MemRegion* R = RV->getRegion();
2179 StoreManager& StoreMgr = getStoreManager();
2180
2181 // Delegate to store manager to get the result of casting a region
2182 // to a different type.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002183 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002184
2185 // Inspect the result. If the MemRegion* returned is NULL, this
2186 // expression evaluates to UnknownVal.
2187 R = Res.getRegion();
2188 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
2189
2190 // Generate the new node in the ExplodedGraph.
2191 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00002192 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002193 }
Zhongxing Xu3330dcb2009-04-10 06:06:13 +00002194 // All other cases.
Ted Kremenekc5302912009-03-05 20:22:13 +00002195 DispatchCast: {
2196 MakeNode(Dst, CastE, N, BindExpr(state, CastE,
2197 EvalCast(V, CastE->getType())));
2198 continue;
2199 }
2200
2201 PassThrough: {
2202 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
2203 }
Ted Kremenek874d63f2008-01-24 02:02:54 +00002204 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002205}
2206
Ted Kremenek4f090272008-10-27 21:54:31 +00002207void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002208 NodeTy* Pred, NodeSet& Dst,
2209 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00002210 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
2211 NodeSet Tmp;
2212 Visit(ILE, Pred, Tmp);
2213
2214 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002215 const GRState* state = GetState(*I);
2216 SVal ILV = GetSVal(state, ILE);
2217 state = StateMgr.BindCompoundLiteral(state, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00002218
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002219 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002220 MakeNode(Dst, CL, *I, BindExpr(state, CL, StateMgr.GetLValue(state, CL)));
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002221 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002222 MakeNode(Dst, CL, *I, BindExpr(state, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00002223 }
2224}
2225
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002226void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002227
Ted Kremenek8369a8b2008-10-06 18:43:53 +00002228 // The CFG has one DeclStmt per Decl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002229 Decl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002230
2231 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002232 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00002233
Ted Kremenekefd59942008-12-08 22:47:34 +00002234 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00002235 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002236
2237 // FIXME: static variables may have an initializer, but the second
2238 // time a function is called those values may not be current.
2239 NodeSet Tmp;
2240
Ted Kremenekaf337412008-11-12 19:24:17 +00002241 if (InitEx)
2242 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002243
2244 if (Tmp.empty())
2245 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002246
2247 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002248 const GRState* state = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00002249 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002250
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002251 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2252 QualType T = getContext().getCanonicalType(VD->getType());
2253 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2254 // FIXME: Handle multi-dimensional VLAs.
2255
2256 Expr* SE = VLA->getSizeExpr();
2257 SVal Size = GetSVal(state, SE);
2258
2259 if (Size.isUndef()) {
2260 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
2261 N->markAsSink();
2262 ExplicitBadSizedVLA.insert(N);
2263 }
2264 continue;
2265 }
2266
2267 bool isFeasibleZero = false;
2268 const GRState* ZeroSt = Assume(state, Size, false, isFeasibleZero);
2269
2270 bool isFeasibleNotZero = false;
2271 state = Assume(state, Size, true, isFeasibleNotZero);
2272
2273 if (isFeasibleZero) {
2274 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
2275 N->markAsSink();
2276 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
2277 else ExplicitBadSizedVLA.insert(N);
2278 }
2279 }
2280
2281 if (!isFeasibleNotZero)
2282 continue;
2283 }
2284
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002285 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00002286 if (InitEx) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002287 SVal InitVal = GetSVal(state, InitEx);
Ted Kremenekaf337412008-11-12 19:24:17 +00002288 QualType T = VD->getType();
2289
2290 // Recover some path-sensitivity if a scalar value evaluated to
2291 // UnknownVal.
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002292 if (InitVal.isUnknown() ||
2293 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002294 InitVal = ValMgr.getConjuredSymbolVal(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00002295 }
2296
Ted Kremeneka8538d92009-02-13 01:45:31 +00002297 state = StateMgr.BindDecl(state, VD, InitVal);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002298
2299 // The next thing to do is check if the GRTransferFuncs object wants to
2300 // update the state based on the new binding. If the GRTransferFunc
2301 // object doesn't do anything, just auto-propagate the current state.
2302 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
2303 getTF().EvalBind(BuilderRef, loc::MemRegionVal(StateMgr.getRegion(VD)),
2304 InitVal);
2305 }
2306 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002307 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002308 MakeNode(Dst, DS, *I, state);
Ted Kremenekefd59942008-12-08 22:47:34 +00002309 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002310 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002311}
Ted Kremenek874d63f2008-01-24 02:02:54 +00002312
Ted Kremenekf75b1862008-10-30 17:47:32 +00002313namespace {
2314 // This class is used by VisitInitListExpr as an item in a worklist
2315 // for processing the values contained in an InitListExpr.
2316class VISIBILITY_HIDDEN InitListWLItem {
2317public:
2318 llvm::ImmutableList<SVal> Vals;
2319 GRExprEngine::NodeTy* N;
2320 InitListExpr::reverse_iterator Itr;
2321
2322 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
2323 InitListExpr::reverse_iterator itr)
2324 : Vals(vals), N(n), Itr(itr) {}
2325};
2326}
2327
2328
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002329void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
2330 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00002331
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002332 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00002333 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00002334 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002335
Zhongxing Xu05d1c572008-10-30 05:35:59 +00002336 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00002337
Ted Kremeneka49e3672008-10-30 23:14:36 +00002338 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00002339
Ted Kremeneka49e3672008-10-30 23:14:36 +00002340 // Handle base case where the initializer has no elements.
2341 // e.g: static int* myArray[] = {};
2342 if (NumInitElements == 0) {
2343 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
2344 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
2345 return;
2346 }
2347
2348 // Create a worklist to process the initializers.
2349 llvm::SmallVector<InitListWLItem, 10> WorkList;
2350 WorkList.reserve(NumInitElements);
2351 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002352 InitListExpr::reverse_iterator ItrEnd = E->rend();
2353
Ted Kremeneka49e3672008-10-30 23:14:36 +00002354 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002355 while (!WorkList.empty()) {
2356 InitListWLItem X = WorkList.back();
2357 WorkList.pop_back();
2358
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002359 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00002360 Visit(*X.Itr, X.N, Tmp);
2361
2362 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002363
Ted Kremenekf75b1862008-10-30 17:47:32 +00002364 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2365 // Get the last initializer value.
2366 state = GetState(*NI);
2367 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
2368
2369 // Construct the new list of values by prepending the new value to
2370 // the already constructed list.
2371 llvm::ImmutableList<SVal> NewVals =
2372 getBasicVals().consVals(InitV, X.Vals);
2373
2374 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00002375 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002376 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002377
Ted Kremenekf75b1862008-10-30 17:47:32 +00002378 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00002379 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002380 }
2381 else {
2382 // Still some initializer values to go. Push them onto the worklist.
2383 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2384 }
2385 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002386 }
Ted Kremenek87903072008-10-30 18:34:31 +00002387
2388 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002389 }
2390
Ted Kremenek062e2f92008-11-13 06:10:40 +00002391 if (T->isUnionType() || T->isVectorType()) {
2392 // FIXME: to be implemented.
2393 // Note: That vectors can return true for T->isIntegerType()
2394 MakeNode(Dst, E, Pred, state);
2395 return;
2396 }
2397
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002398 if (Loc::IsLocType(T) || T->isIntegerType()) {
2399 assert (E->getNumInits() == 1);
2400 NodeSet Tmp;
2401 Expr* Init = E->getInit(0);
2402 Visit(Init, Pred, Tmp);
2403 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
2404 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002405 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002406 }
2407 return;
2408 }
2409
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002410
2411 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2412 assert(0 && "unprocessed InitListExpr type");
2413}
Ted Kremenekf233d482008-02-05 00:26:40 +00002414
Sebastian Redl05189992008-11-11 17:56:53 +00002415/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2416void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2417 NodeTy* Pred,
2418 NodeSet& Dst) {
2419 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00002420 uint64_t amt;
2421
2422 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002423 if (T == getContext().VoidTy) {
2424 // sizeof(void) == 1 byte.
2425 amt = 1;
2426 }
2427 else if (!T.getTypePtr()->isConstantSizeType()) {
2428 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002429 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002430 }
2431 else if (T->isObjCInterfaceType()) {
2432 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2433 // the compiler has laid out its representation. Just report Unknown
2434 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002435 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002436 }
2437 else {
2438 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002439 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002440 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002441 }
2442 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002443 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002444
Ted Kremenek0e561a32008-03-21 21:30:14 +00002445 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002446 BindExpr(GetState(Pred), Ex,
2447 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002448}
2449
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002450
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002451void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002452 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002453
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002454 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002455
2456 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002457 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002458
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002459 case UnaryOperator::Deref: {
2460
2461 Expr* Ex = U->getSubExpr()->IgnoreParens();
2462 NodeSet Tmp;
2463 Visit(Ex, Pred, Tmp);
2464
2465 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002466
Ted Kremeneka8538d92009-02-13 01:45:31 +00002467 const GRState* state = GetState(*I);
2468 SVal location = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002469
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002470 if (asLValue)
Ted Kremenek7090d542009-05-07 18:27:16 +00002471 MakeNode(Dst, U, *I, BindExpr(state, U, location),
2472 ProgramPoint::PostLValueKind);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002473 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002474 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002475 }
2476
2477 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002478 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002479
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002480 case UnaryOperator::Real: {
2481
2482 Expr* Ex = U->getSubExpr()->IgnoreParens();
2483 NodeSet Tmp;
2484 Visit(Ex, Pred, Tmp);
2485
2486 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2487
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002488 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002489 if (Ex->getType()->isAnyComplexType()) {
2490 // Just report "Unknown."
2491 Dst.Add(*I);
2492 continue;
2493 }
2494
2495 // For all other types, UnaryOperator::Real is an identity operation.
2496 assert (U->getType() == Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002497 const GRState* state = GetState(*I);
2498 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002499 }
2500
2501 return;
2502 }
2503
2504 case UnaryOperator::Imag: {
2505
2506 Expr* Ex = U->getSubExpr()->IgnoreParens();
2507 NodeSet Tmp;
2508 Visit(Ex, Pred, Tmp);
2509
2510 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002511 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002512 if (Ex->getType()->isAnyComplexType()) {
2513 // Just report "Unknown."
2514 Dst.Add(*I);
2515 continue;
2516 }
2517
2518 // For all other types, UnaryOperator::Float returns 0.
2519 assert (Ex->getType()->isIntegerType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002520 const GRState* state = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002521 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002522 MakeNode(Dst, U, *I, BindExpr(state, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002523 }
2524
2525 return;
2526 }
2527
2528 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002529 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002530 Dst.Add(Pred);
2531 return;
2532
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002533 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002534 case UnaryOperator::Extension: {
2535
2536 // Unary "+" is a no-op, similar to a parentheses. We still have places
2537 // where it may be a block-level expression, so we need to
2538 // generate an extra node that just propagates the value of the
2539 // subexpression.
2540
2541 Expr* Ex = U->getSubExpr()->IgnoreParens();
2542 NodeSet Tmp;
2543 Visit(Ex, Pred, Tmp);
2544
2545 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002546 const GRState* state = GetState(*I);
2547 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002548 }
2549
2550 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002551 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002552
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002553 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002554
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002555 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002556 Expr* Ex = U->getSubExpr()->IgnoreParens();
2557 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002558 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002559
2560 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002561 const GRState* state = GetState(*I);
2562 SVal V = GetSVal(state, Ex);
2563 state = BindExpr(state, U, V);
2564 MakeNode(Dst, U, *I, state);
Ted Kremenek89063af2008-02-21 19:15:37 +00002565 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002566
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002567 return;
2568 }
2569
2570 case UnaryOperator::LNot:
2571 case UnaryOperator::Minus:
2572 case UnaryOperator::Not: {
2573
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002574 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002575 Expr* Ex = U->getSubExpr()->IgnoreParens();
2576 NodeSet Tmp;
2577 Visit(Ex, Pred, Tmp);
2578
2579 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002580 const GRState* state = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002581
2582 // Get the value of the subexpression.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002583 SVal V = GetSVal(state, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002584
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002585 if (V.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002586 MakeNode(Dst, U, *I, BindExpr(state, U, V));
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002587 continue;
2588 }
2589
Ted Kremenek60595da2008-11-15 04:01:56 +00002590// QualType DstT = getContext().getCanonicalType(U->getType());
2591// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2592//
2593// if (DstT != SrcT) // Perform promotions.
2594// V = EvalCast(V, DstT);
2595//
2596// if (V.isUnknownOrUndef()) {
2597// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2598// continue;
2599// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002600
2601 switch (U->getOpcode()) {
2602 default:
2603 assert(false && "Invalid Opcode.");
2604 break;
2605
2606 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002607 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002608 state = BindExpr(state, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002609 break;
2610
2611 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002612 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002613 state = BindExpr(state, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002614 break;
2615
2616 case UnaryOperator::LNot:
2617
2618 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2619 //
2620 // Note: technically we do "E == 0", but this is the same in the
2621 // transfer functions as "0 == E".
2622
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002623 if (isa<Loc>(V)) {
Ted Kremenekda9ae602009-04-08 18:51:08 +00002624 Loc X = Loc::MakeNull(getBasicVals());
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002625 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X,
2626 U->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002627 state = BindExpr(state, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002628 }
2629 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002630 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002631#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002632 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002633 state = SetSVal(state, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002634#else
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002635 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I,
2636 U->getType());
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002637 continue;
2638#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002639 }
2640
2641 break;
2642 }
2643
Ted Kremeneka8538d92009-02-13 01:45:31 +00002644 MakeNode(Dst, U, *I, state);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002645 }
2646
2647 return;
2648 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002649 }
2650
2651 // Handle ++ and -- (both pre- and post-increment).
2652
2653 assert (U->isIncrementDecrementOp());
2654 NodeSet Tmp;
2655 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002656 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002657
2658 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2659
Ted Kremeneka8538d92009-02-13 01:45:31 +00002660 const GRState* state = GetState(*I);
2661 SVal V1 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002662
2663 // Perform a load.
2664 NodeSet Tmp2;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002665 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002666
2667 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2668
Ted Kremeneka8538d92009-02-13 01:45:31 +00002669 state = GetState(*I2);
2670 SVal V2 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002671
2672 // Propagate unknown and undefined values.
2673 if (V2.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002674 MakeNode(Dst, U, *I2, BindExpr(state, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002675 continue;
2676 }
2677
Ted Kremenek21028dd2009-03-11 03:54:24 +00002678 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002679 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2680 : BinaryOperator::Sub;
Ted Kremenek21028dd2009-03-11 03:54:24 +00002681
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002682 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U), U->getType());
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002683
2684 // Conjure a new symbol if necessary to recover precision.
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002685 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002686 Result = ValMgr.getConjuredSymbolVal(Ex,
2687 Builder->getCurrentBlockCount());
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002688
2689 // If the value is a location, ++/-- should always preserve
2690 // non-nullness. Check if the original value was non-null, and if so propagate
2691 // that constraint.
2692 if (Loc::IsLocType(U->getType())) {
2693 SVal Constraint = EvalBinOp(BinaryOperator::EQ, V2,
2694 ValMgr.makeZeroVal(U->getType()),
2695 getContext().IntTy);
2696
2697 bool isFeasible = false;
2698 Assume(state, Constraint, true, isFeasible);
2699 if (!isFeasible) {
2700 // It isn't feasible for the original value to be null.
2701 // Propagate this constraint.
2702 Constraint = EvalBinOp(BinaryOperator::EQ, Result,
2703 ValMgr.makeZeroVal(U->getType()),
2704 getContext().IntTy);
2705
2706 bool isFeasible = false;
2707 state = Assume(state, Constraint, false, isFeasible);
2708 assert(isFeasible && state);
2709 }
2710 }
2711 }
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002712
Ted Kremeneka8538d92009-02-13 01:45:31 +00002713 state = BindExpr(state, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002714
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002715 // Perform the store.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002716 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002717 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002718 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002719}
2720
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002721void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2722 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2723}
2724
2725void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2726 AsmStmt::outputs_iterator I,
2727 AsmStmt::outputs_iterator E,
2728 NodeTy* Pred, NodeSet& Dst) {
2729 if (I == E) {
2730 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2731 return;
2732 }
2733
2734 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002735 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002736
2737 ++I;
2738
2739 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2740 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2741}
2742
2743void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2744 AsmStmt::inputs_iterator I,
2745 AsmStmt::inputs_iterator E,
2746 NodeTy* Pred, NodeSet& Dst) {
2747 if (I == E) {
2748
2749 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002750 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002751
2752 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2753 // which interprets the inline asm and stores proper results in the
2754 // outputs.
2755
Ted Kremeneka8538d92009-02-13 01:45:31 +00002756 const GRState* state = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002757
2758 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2759 OE = A->end_outputs(); OI != OE; ++OI) {
2760
Ted Kremeneka8538d92009-02-13 01:45:31 +00002761 SVal X = GetSVal(state, *OI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002762 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002763
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002764 if (isa<Loc>(X))
Ted Kremeneka8538d92009-02-13 01:45:31 +00002765 state = BindLoc(state, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002766 }
2767
Ted Kremeneka8538d92009-02-13 01:45:31 +00002768 MakeNode(Dst, A, Pred, state);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002769 return;
2770 }
2771
2772 NodeSet Tmp;
2773 Visit(*I, Pred, Tmp);
2774
2775 ++I;
2776
2777 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2778 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2779}
2780
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002781void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2782 assert (Builder && "GRStmtNodeBuilder must be defined.");
2783
2784 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002785
Ted Kremenek186350f2008-04-23 20:12:28 +00002786 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2787 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002788
Ted Kremenek729a9a22008-07-17 23:15:45 +00002789 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002790
Ted Kremenekb0533962008-04-18 20:35:30 +00002791 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002792
Ted Kremenekb0533962008-04-18 20:35:30 +00002793 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002794 MakeNode(Dst, S, Pred, GetState(Pred));
2795}
2796
Ted Kremenek02737ed2008-03-31 15:02:58 +00002797void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2798
2799 Expr* R = S->getRetValue();
2800
2801 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002802 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002803 return;
2804 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002805
Ted Kremenek5917d782008-11-21 00:27:44 +00002806 NodeSet Tmp;
2807 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002808
Ted Kremenek5917d782008-11-21 00:27:44 +00002809 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2810 SVal X = GetSVal((*I)->getState(), R);
2811
2812 // Check if we return the address of a stack variable.
2813 if (isa<loc::MemRegionVal>(X)) {
2814 // Determine if the value is on the stack.
2815 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002816
Ted Kremenek5917d782008-11-21 00:27:44 +00002817 if (R && getStateManager().hasStackStorage(R)) {
2818 // Create a special node representing the error.
2819 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2820 N->markAsSink();
2821 RetsStackAddr.insert(N);
2822 }
2823 continue;
2824 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002825 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002826 // Check if we return an undefined value.
2827 else if (X.isUndef()) {
2828 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2829 N->markAsSink();
2830 RetsUndef.insert(N);
2831 }
2832 continue;
2833 }
2834
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002835 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002836 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002837}
Ted Kremenek55deb972008-03-25 00:34:37 +00002838
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002839//===----------------------------------------------------------------------===//
2840// Transfer functions: Binary operators.
2841//===----------------------------------------------------------------------===//
2842
Ted Kremeneka8538d92009-02-13 01:45:31 +00002843const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002844 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002845
2846 // Divide by undefined? (potentially zero)
2847
2848 if (Denom.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002849 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002850
2851 if (DivUndef) {
2852 DivUndef->markAsSink();
2853 ExplicitBadDivides.insert(DivUndef);
2854 }
2855
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002856 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002857 }
2858
2859 // Check for divide/remainder-by-zero.
2860 // First, "assume" that the denominator is 0 or undefined.
2861
2862 bool isFeasibleZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002863 const GRState* ZeroSt = Assume(state, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002864
2865 // Second, "assume" that the denominator cannot be 0.
2866
2867 bool isFeasibleNotZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002868 state = Assume(state, Denom, true, isFeasibleNotZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002869
2870 // Create the node for the divide-by-zero (if it occurred).
2871
2872 if (isFeasibleZero)
2873 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2874 DivZeroNode->markAsSink();
2875
2876 if (isFeasibleNotZero)
2877 ImplicitBadDivides.insert(DivZeroNode);
2878 else
2879 ExplicitBadDivides.insert(DivZeroNode);
2880
2881 }
2882
Ted Kremeneka8538d92009-02-13 01:45:31 +00002883 return isFeasibleNotZero ? state : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002884}
2885
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002886void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002887 GRExprEngine::NodeTy* Pred,
2888 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002889
2890 NodeSet Tmp1;
2891 Expr* LHS = B->getLHS()->IgnoreParens();
2892 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002893
Ted Kremenek759623e2008-12-06 02:39:30 +00002894 // FIXME: Add proper support for ObjCKVCRefExpr.
2895 if (isa<ObjCKVCRefExpr>(LHS)) {
2896 Visit(RHS, Pred, Dst);
2897 return;
2898 }
2899
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002900 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002901 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002902 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002903 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002904
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002905 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002906
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002907 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002908
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002909 // Process the RHS.
2910
2911 NodeSet Tmp2;
2912 Visit(RHS, *I1, Tmp2);
2913
2914 // With both the LHS and RHS evaluated, process the operation itself.
2915
2916 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002917
Ted Kremeneka8538d92009-02-13 01:45:31 +00002918 const GRState* state = GetState(*I2);
2919 const GRState* OldSt = state;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002920
Ted Kremeneka8538d92009-02-13 01:45:31 +00002921 SVal RightV = GetSVal(state, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002922 BinaryOperator::Opcode Op = B->getOpcode();
2923
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002924 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002925
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002926 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002927
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002928 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002929 // FIXME: Handle structs.
2930 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002931
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002932 if ((RightV.isUnknown() ||
2933 !getConstraintManager().canReasonAbout(RightV))
2934 && (Loc::IsLocType(T) ||
2935 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002936 unsigned Count = Builder->getCurrentBlockCount();
2937 RightV = ValMgr.getConjuredSymbolVal(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002938 }
2939
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002940 // Simulate the effects of a "store": bind the value of the RHS
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002941 // to the L-Value represented by the LHS.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002942 EvalStore(Dst, B, LHS, *I2, BindExpr(state, B, RightV), LeftV,
2943 RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002944 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002945 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002946
2947 case BinaryOperator::Div:
2948 case BinaryOperator::Rem:
2949
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002950 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002951 if (RHS->getType()->isIntegerType() &&
2952 RHS->getType()->isScalarType()) {
2953
Ted Kremeneka8538d92009-02-13 01:45:31 +00002954 state = CheckDivideZero(B, state, *I2, RightV);
2955 if (!state) continue;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002956 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002957
2958 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002959
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002960 default: {
2961
2962 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002963 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002964
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002965 // Process non-assignements except commas or short-circuited
2966 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002967
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002968 SVal Result = EvalBinOp(Op, LeftV, RightV, B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002969
2970 if (Result.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002971 if (OldSt != state) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002972 // Generate a new node if we have already created a new state.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002973 MakeNode(Dst, B, *I2, state);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002974 }
2975 else
2976 Dst.Add(*I2);
2977
Ted Kremenek89063af2008-02-21 19:15:37 +00002978 continue;
2979 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002980
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002981 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002982
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002983 // The operands were *not* undefined, but the result is undefined.
2984 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002985
Ted Kremeneka8538d92009-02-13 01:45:31 +00002986 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002987 UndefNode->markAsSink();
2988 UndefResults.insert(UndefNode);
2989 }
2990
2991 continue;
2992 }
2993
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002994 // Otherwise, create a new node.
2995
Ted Kremeneka8538d92009-02-13 01:45:31 +00002996 MakeNode(Dst, B, *I2, BindExpr(state, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002997 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002998 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002999 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00003000
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003001 assert (B->isCompoundAssignmentOp());
3002
Ted Kremenekcad29962009-02-07 00:52:24 +00003003 switch (Op) {
3004 default:
3005 assert(0 && "Invalid opcode for compound assignment.");
3006 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
3007 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
3008 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
3009 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
3010 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
3011 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
3012 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
3013 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
3014 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
3015 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek934e3e92008-10-27 23:02:39 +00003016 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003017
3018 // Perform a load (the LHS). This performs the checks for
3019 // null dereferences, and so on.
3020 NodeSet Tmp3;
Ted Kremeneka8538d92009-02-13 01:45:31 +00003021 SVal location = GetSVal(state, LHS);
3022 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003023
3024 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
3025
Ted Kremeneka8538d92009-02-13 01:45:31 +00003026 state = GetState(*I3);
3027 SVal V = GetSVal(state, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003028
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003029 // Check for divide-by-zero.
3030 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00003031 && RHS->getType()->isIntegerType()
3032 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003033
3034 // CheckDivideZero returns a new state where the denominator
3035 // is assumed to be non-zero.
Ted Kremeneka8538d92009-02-13 01:45:31 +00003036 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003037
Ted Kremeneka8538d92009-02-13 01:45:31 +00003038 if (!state)
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003039 continue;
3040 }
3041
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003042 // Propagate undefined values (left-side).
3043 if (V.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00003044 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003045 continue;
3046 }
3047
3048 // Propagate unknown values (left and right-side).
3049 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00003050 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, UnknownVal()),
3051 location, UnknownVal());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003052 continue;
3053 }
3054
3055 // At this point:
3056 //
3057 // The LHS is not Undef/Unknown.
3058 // The RHS is not Unknown.
3059
3060 // Get the computation type.
Eli Friedmanab3a8522009-03-28 01:22:36 +00003061 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek60595da2008-11-15 04:01:56 +00003062 CTy = getContext().getCanonicalType(CTy);
Eli Friedmanab3a8522009-03-28 01:22:36 +00003063
3064 QualType CLHSTy = cast<CompoundAssignOperator>(B)->getComputationLHSType();
3065 CLHSTy = getContext().getCanonicalType(CTy);
3066
Ted Kremenek60595da2008-11-15 04:01:56 +00003067 QualType LTy = getContext().getCanonicalType(LHS->getType());
3068 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedmanab3a8522009-03-28 01:22:36 +00003069
3070 // Promote LHS.
3071 V = EvalCast(V, CLHSTy);
3072
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003073 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003074 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00003075 // Propagate undefined values (right-side).
Ted Kremeneke121da42009-03-05 03:42:31 +00003076 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, RightV), location,
Ted Kremeneka8538d92009-02-13 01:45:31 +00003077 RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003078 continue;
3079 }
3080
Ted Kremenek60595da2008-11-15 04:01:56 +00003081 // Compute the result of the operation.
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003082 SVal Result = EvalCast(EvalBinOp(Op, V, RightV, CTy), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003083
3084 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003085 // The operands were not undefined, but the result is undefined.
Ted Kremeneka8538d92009-02-13 01:45:31 +00003086 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003087 UndefNode->markAsSink();
3088 UndefResults.insert(UndefNode);
3089 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003090 continue;
3091 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003092
3093 // EXPERIMENTAL: "Conjured" symbols.
3094 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00003095
3096 SVal LHSVal;
3097
Ted Kremenek276c6ac2009-03-11 02:24:48 +00003098 if ((Result.isUnknown() ||
3099 !getConstraintManager().canReasonAbout(Result))
3100 && (Loc::IsLocType(CTy)
3101 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00003102
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003103 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003104
Ted Kremenek60595da2008-11-15 04:01:56 +00003105 // The symbolic value is actually for the type of the left-hand side
3106 // expression, not the computation type, as this is the value the
3107 // LValue on the LHS will bind to.
Ted Kremenek8d7f5482009-04-09 22:22:44 +00003108 LHSVal = ValMgr.getConjuredSymbolVal(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00003109
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00003110 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00003111 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003112 }
Ted Kremenek60595da2008-11-15 04:01:56 +00003113 else {
3114 // The left-hand side may bind to a different value then the
3115 // computation type.
3116 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
3117 }
3118
Ted Kremeneka8538d92009-02-13 01:45:31 +00003119 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, Result), location,
3120 LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003121 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00003122 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00003123 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00003124}
Ted Kremenekee985462008-01-16 18:18:48 +00003125
3126//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003127// Transfer-function Helpers.
3128//===----------------------------------------------------------------------===//
3129
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003130void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003131 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00003132 NonLoc L, NonLoc R,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003133 ExplodedNode<GRState>* Pred, QualType T) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003134
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003135 GRStateSet OStates;
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003136 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R, T);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003137
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003138 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003139 MakeNode(Dst, Ex, Pred, *I);
3140}
3141
Ted Kremeneka8538d92009-02-13 01:45:31 +00003142void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003143 Expr* Ex, BinaryOperator::Opcode Op,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003144 NonLoc L, NonLoc R, QualType T) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003145
Ted Kremeneka8538d92009-02-13 01:45:31 +00003146 GRStateSet::AutoPopulate AP(OStates, state);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003147 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R, T);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003148}
3149
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003150SVal GRExprEngine::EvalBinOp(BinaryOperator::Opcode Op, SVal L, SVal R,
3151 QualType T) {
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003152
3153 if (L.isUndef() || R.isUndef())
3154 return UndefinedVal();
3155
3156 if (L.isUnknown() || R.isUnknown())
3157 return UnknownVal();
3158
3159 if (isa<Loc>(L)) {
3160 if (isa<Loc>(R))
3161 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
3162 else
3163 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<NonLoc>(R));
3164 }
3165
3166 if (isa<Loc>(R)) {
3167 // Support pointer arithmetic where the increment/decrement operand
3168 // is on the left and the pointer on the right.
3169
3170 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
3171
3172 // Commute the operands.
3173 return getTF().EvalBinOp(*this, Op, cast<Loc>(R),
3174 cast<NonLoc>(L));
3175 }
3176 else
3177 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003178 cast<NonLoc>(R), T);
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003179}
3180
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003181//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00003182// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00003183//===----------------------------------------------------------------------===//
3184
Ted Kremenekaa66a322008-01-16 21:46:15 +00003185#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003186static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003187static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003188
Ted Kremenekaa66a322008-01-16 21:46:15 +00003189namespace llvm {
3190template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003191struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00003192 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00003193
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003194 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
3195
3196 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00003197 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003198 GraphPrintCheckerState->isUndefDeref(N) ||
3199 GraphPrintCheckerState->isUndefStore(N) ||
3200 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00003201 GraphPrintCheckerState->isExplicitBadDivide(N) ||
3202 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00003203 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00003204 GraphPrintCheckerState->isBadCall(N) ||
3205 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003206 return "color=\"red\",style=\"filled\"";
3207
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00003208 if (GraphPrintCheckerState->isNoReturnCall(N))
3209 return "color=\"blue\",style=\"filled\"";
3210
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003211 return "";
3212 }
Ted Kremeneked4de312008-02-06 03:56:15 +00003213
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003214 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00003215 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003216
3217 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00003218 ProgramPoint Loc = N->getLocation();
3219
3220 switch (Loc.getKind()) {
3221 case ProgramPoint::BlockEntranceKind:
3222 Out << "Block Entrance: B"
3223 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
3224 break;
3225
3226 case ProgramPoint::BlockExitKind:
3227 assert (false);
3228 break;
3229
Ted Kremenekaa66a322008-01-16 21:46:15 +00003230 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00003231 if (isa<PostStmt>(Loc)) {
3232 const PostStmt& L = cast<PostStmt>(Loc);
3233 Stmt* S = L.getStmt();
3234 SourceLocation SLoc = S->getLocStart();
3235
3236 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
3237 llvm::raw_os_ostream OutS(Out);
3238 S->printPretty(OutS);
3239 OutS.flush();
3240
3241 if (SLoc.isFileID()) {
3242 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003243 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3244 << " col="
3245 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3246 << "\\l";
Ted Kremenek8c354752008-12-16 22:02:27 +00003247 }
3248
Ted Kremenek7090d542009-05-07 18:27:16 +00003249 if (isa<PostLoad>(Loc))
3250 Out << "\\lPostLoad\\l;";
3251 else if (isa<PostStore>(Loc))
3252 Out << "\\lPostStore\\l";
3253 else if (isa<PostLValue>(Loc))
3254 Out << "\\lPostLValue\\l";
3255 else if (isa<PostLocationChecksSucceed>(Loc))
3256 Out << "\\lPostLocationChecksSucceed\\l";
3257 else if (isa<PostNullCheckFailed>(Loc))
3258 Out << "\\lPostNullCheckFailed\\l";
3259
Ted Kremenek8c354752008-12-16 22:02:27 +00003260 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3261 Out << "\\|Implicit-Null Dereference.\\l";
3262 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3263 Out << "\\|Explicit-Null Dereference.\\l";
3264 else if (GraphPrintCheckerState->isUndefDeref(N))
3265 Out << "\\|Dereference of undefialied value.\\l";
3266 else if (GraphPrintCheckerState->isUndefStore(N))
3267 Out << "\\|Store to Undefined Loc.";
3268 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
3269 Out << "\\|Explicit divide-by zero or undefined value.";
3270 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
3271 Out << "\\|Implicit divide-by zero or undefined value.";
3272 else if (GraphPrintCheckerState->isUndefResult(N))
3273 Out << "\\|Result of operation is undefined.";
3274 else if (GraphPrintCheckerState->isNoReturnCall(N))
3275 Out << "\\|Call to function marked \"noreturn\".";
3276 else if (GraphPrintCheckerState->isBadCall(N))
3277 Out << "\\|Call to NULL/Undefined.";
3278 else if (GraphPrintCheckerState->isUndefArg(N))
3279 Out << "\\|Argument in call is undefined";
3280
3281 break;
3282 }
3283
Ted Kremenekaa66a322008-01-16 21:46:15 +00003284 const BlockEdge& E = cast<BlockEdge>(Loc);
3285 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3286 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00003287
3288 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00003289
3290 SourceLocation SLoc = T->getLocStart();
3291
Ted Kremenekb38911f2008-01-30 23:03:39 +00003292 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00003293
Ted Kremeneka95d3752008-09-13 05:16:45 +00003294 llvm::raw_os_ostream OutS(Out);
3295 E.getSrc()->printTerminator(OutS);
3296 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00003297
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003298 if (SLoc.isFileID()) {
3299 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003300 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3301 << " col="
3302 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003303 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00003304
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003305 if (isa<SwitchStmt>(T)) {
3306 Stmt* Label = E.getDst()->getLabel();
3307
3308 if (Label) {
3309 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3310 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003311 llvm::raw_os_ostream OutS(Out);
3312 C->getLHS()->printPretty(OutS);
3313 OutS.flush();
3314
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003315 if (Stmt* RHS = C->getRHS()) {
3316 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003317 RHS->printPretty(OutS);
3318 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003319 }
3320
3321 Out << ":";
3322 }
3323 else {
3324 assert (isa<DefaultStmt>(Label));
3325 Out << "\\ldefault:";
3326 }
3327 }
3328 else
3329 Out << "\\l(implicit) default:";
3330 }
3331 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00003332 // FIXME
3333 }
3334 else {
3335 Out << "\\lCondition: ";
3336 if (*E.getSrc()->succ_begin() == E.getDst())
3337 Out << "true";
3338 else
3339 Out << "false";
3340 }
3341
3342 Out << "\\l";
3343 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003344
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003345 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3346 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003347 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00003348 }
3349 }
3350
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00003351 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00003352
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003353 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
3354 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003355
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003356 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00003357 return Out.str();
3358 }
3359};
3360} // end llvm namespace
3361#endif
3362
Ted Kremenekffe0f432008-03-07 22:58:01 +00003363#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003364template <typename ITERATOR>
3365GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
3366
3367template <>
3368GRExprEngine::NodeTy*
3369GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
3370 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
3371 return I->first;
3372}
Ted Kremenekffe0f432008-03-07 22:58:01 +00003373#endif
3374
3375void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00003376#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00003377 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00003378 std::vector<NodeTy*> Src;
Ted Kremenek940abcc2009-03-11 01:41:22 +00003379
3380 // Flush any outstanding reports to make sure we cover all the nodes.
3381 // This does not cause them to get displayed.
3382 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3383 const_cast<BugType*>(*I)->FlushReports(BR);
3384
3385 // Iterate through the reports and get their nodes.
3386 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
3387 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) {
3388 const BugReportEquivClass& EQ = *I2;
3389 const BugReport &R = **EQ.begin();
3390 NodeTy *N = const_cast<NodeTy*>(R.getEndNode());
3391 if (N) Src.push_back(N);
3392 }
3393 }
Ted Kremenekcb612922008-04-18 19:23:43 +00003394
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003395 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00003396 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00003397 else {
3398 GraphPrintCheckerState = this;
3399 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00003400
Ted Kremenekffe0f432008-03-07 22:58:01 +00003401 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00003402
3403 GraphPrintCheckerState = NULL;
3404 GraphPrintSourceManager = NULL;
3405 }
3406#endif
3407}
3408
3409void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
3410#ifndef NDEBUG
3411 GraphPrintCheckerState = this;
3412 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003413
Ted Kremenekcf118d42009-02-04 23:49:09 +00003414 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremenek493d7a22008-03-11 18:25:33 +00003415
Ted Kremenekcf118d42009-02-04 23:49:09 +00003416 if (!TrimmedG.get())
Ted Kremenek493d7a22008-03-11 18:25:33 +00003417 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekcf118d42009-02-04 23:49:09 +00003418 else
Ted Kremenek493d7a22008-03-11 18:25:33 +00003419 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenekffe0f432008-03-07 22:58:01 +00003420
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003421 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003422 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00003423#endif
Ted Kremenekee985462008-01-16 18:18:48 +00003424}