blob: 0fabd35b0b2d172ff136a503e997c676dfa5f949 [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 Kremeneka8538d92009-02-13 01:45:31 +0000966 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000967 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000968 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000969 return;
970
971 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
972 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
973
974 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000975 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Ted Kremeneka8538d92009-02-13 01:45:31 +0000976 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000977 return;
978
979 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000980 assert(asLValue);
Zhongxing Xu369f4472009-04-20 05:24:46 +0000981 SVal V = ValMgr.getFunctionPointer(FD);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000982 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000983 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000984 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000985
986 assert (false &&
987 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000988}
989
Ted Kremenek540cbe22008-04-22 04:56:29 +0000990/// VisitArraySubscriptExpr - Transfer function for array accesses
991void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000992 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000993
994 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000995 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000996 NodeSet Tmp;
Ted Kremenek265a3052009-02-24 02:23:11 +0000997
998 if (Base->getType()->isVectorType()) {
999 // For vector types get its lvalue.
1000 // FIXME: This may not be correct. Is the rvalue of a vector its location?
1001 // In fact, I think this is just a hack. We need to get the right
1002 // semantics.
1003 VisitLValue(Base, Pred, Tmp);
1004 }
1005 else
1006 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001007
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001008 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001009 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001010 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001011
1012 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001013 const GRState* state = GetState(*I2);
Ted Kremenekf936f452009-05-04 06:18:28 +00001014 SVal V = StateMgr.GetLValue(state, A->getType(),
1015 GetSVal(state, Base),
Ted Kremeneka8538d92009-02-13 01:45:31 +00001016 GetSVal(state, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001017
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001018 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001019 MakeNode(Dst, A, *I2, BindExpr(state, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001020 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001021 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001022 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001023 }
Ted Kremenek540cbe22008-04-22 04:56:29 +00001024}
1025
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001026/// VisitMemberExpr - Transfer function for member expressions.
1027void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001028 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001029
1030 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001031 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +00001032
1033 if (M->isArrow())
1034 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1035 else
1036 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
1037
Douglas Gregor86f19402008-12-20 23:49:58 +00001038 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1039 if (!Field) // FIXME: skipping member expressions for non-fields
1040 return;
1041
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001042 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001043 const GRState* state = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001044 // FIXME: Should we insert some assumption logic in here to determine
1045 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +00001046 // later when using FieldOffset lvals (which we no longer have).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001047 SVal L = StateMgr.GetLValue(state, GetSVal(state, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001048
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001049 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001050 MakeNode(Dst, M, *I, BindExpr(state, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001051 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001052 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001053 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001054}
1055
Ted Kremeneka8538d92009-02-13 01:45:31 +00001056/// EvalBind - Handle the semantics of binding a value to a specific location.
1057/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
1058void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
1059 const GRState* state, SVal location, SVal Val) {
1060
Ted Kremenek41573eb2009-02-14 01:43:44 +00001061 const GRState* newState = 0;
1062
1063 if (location.isUnknown()) {
1064 // We know that the new state will be the same as the old state since
1065 // the location of the binding is "unknown". Consequently, there
1066 // is no reason to just create a new node.
1067 newState = state;
1068 }
1069 else {
1070 // We are binding to a value other than 'unknown'. Perform the binding
1071 // using the StoreManager.
1072 newState = StateMgr.BindLoc(state, cast<Loc>(location), Val);
1073 }
Ted Kremeneka8538d92009-02-13 01:45:31 +00001074
Ted Kremenek41573eb2009-02-14 01:43:44 +00001075 // The next thing to do is check if the GRTransferFuncs object wants to
1076 // update the state based on the new binding. If the GRTransferFunc object
1077 // doesn't do anything, just auto-propagate the current state.
1078 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1079 newState != state);
1080
1081 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001082}
1083
1084/// EvalStore - Handle the semantics of a store via an assignment.
1085/// @param Dst The node set to store generated state nodes
1086/// @param Ex The expression representing the location of the store
1087/// @param state The current simulation state
1088/// @param location The location to store the value
1089/// @param Val The value to be stored
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001090void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek1670e402009-04-11 00:11:10 +00001091 const GRState* state, SVal location, SVal Val,
1092 const void *tag) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001093
1094 assert (Builder && "GRStmtNodeBuilder must be defined.");
1095
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001096 // Evaluate the location (checks for bad dereferences).
Ted Kremenek1670e402009-04-11 00:11:10 +00001097 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001098
Ted Kremenek8c354752008-12-16 22:02:27 +00001099 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001100 return;
Ted Kremenekb0533962008-04-18 20:35:30 +00001101
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001102 assert (!location.isUndef());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001103 state = GetState(Pred);
1104
1105 // Proceed with the store.
1106 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek1670e402009-04-11 00:11:10 +00001107 SaveAndRestore<const void*> OldTag(Builder->Tag);
1108 Builder->PointKind = ProgramPoint::PostStoreKind;
1109 Builder->Tag = tag;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001110 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001111}
1112
1113void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek1670e402009-04-11 00:11:10 +00001114 const GRState* state, SVal location,
1115 const void *tag) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001116
Ted Kremenek8c354752008-12-16 22:02:27 +00001117 // Evaluate the location (checks for bad dereferences).
Ted Kremenek1670e402009-04-11 00:11:10 +00001118 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001119
Ted Kremenek8c354752008-12-16 22:02:27 +00001120 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001121 return;
1122
Ted Kremeneka8538d92009-02-13 01:45:31 +00001123 state = GetState(Pred);
Ted Kremenek8c354752008-12-16 22:02:27 +00001124
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001125 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +00001126 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001127
1128 // FIXME: Currently symbolic analysis "generates" new symbols
1129 // for the contents of values. We need a better approach.
1130
Ted Kremenek8c354752008-12-16 22:02:27 +00001131 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +00001132 // This is important. We must nuke the old binding.
Ted Kremenek1670e402009-04-11 00:11:10 +00001133 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K, tag);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001134 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001135 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001136 SVal V = GetSVal(state, cast<Loc>(location), Ex->getType());
Ted Kremenek1670e402009-04-11 00:11:10 +00001137 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K, tag);
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001138 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001139}
1140
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001141void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremenek1670e402009-04-11 00:11:10 +00001142 const GRState* state, SVal location, SVal Val,
1143 const void *tag) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001144
1145 NodeSet TmpDst;
Ted Kremenek1670e402009-04-11 00:11:10 +00001146 EvalStore(TmpDst, StoreE, Pred, state, location, Val, tag);
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001147
1148 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
Ted Kremenek1670e402009-04-11 00:11:10 +00001149 MakeNode(Dst, Ex, *I, (*I)->getState(), ProgramPoint::PostStmtKind, tag);
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001150}
1151
Ted Kremenek8c354752008-12-16 22:02:27 +00001152GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001153 const GRState* state,
Ted Kremenek1670e402009-04-11 00:11:10 +00001154 SVal location,
1155 const void *tag) {
1156
1157 SaveAndRestore<const void*> OldTag(Builder->Tag);
1158 Builder->Tag = tag;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001159
1160 // Check for loads/stores from/to undefined values.
1161 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001162 NodeTy* N =
Ted Kremeneka8538d92009-02-13 01:45:31 +00001163 Builder->generateNode(Ex, state, Pred,
Ted Kremenek8c354752008-12-16 22:02:27 +00001164 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001165
Ted Kremenek8c354752008-12-16 22:02:27 +00001166 if (N) {
1167 N->markAsSink();
1168 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001169 }
1170
Ted Kremenek8c354752008-12-16 22:02:27 +00001171 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001172 }
1173
1174 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1175 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001176 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001177
1178 // During a load, one of two possible situations arise:
1179 // (1) A crash, because the location (pointer) was NULL.
1180 // (2) The location (pointer) is not NULL, and the dereference works.
1181 //
1182 // We add these assumptions.
1183
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001184 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001185
1186 // "Assume" that the pointer is not NULL.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001187 bool isFeasibleNotNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001188 const GRState* StNotNull = Assume(state, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001189
1190 // "Assume" that the pointer is NULL.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001191 bool isFeasibleNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001192 GRStateRef StNull = GRStateRef(Assume(state, LV, false, isFeasibleNull),
Ted Kremenek7360fda2008-09-18 23:09:54 +00001193 getStateManager());
Zhongxing Xua1718c72009-04-03 07:33:13 +00001194
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001195 if (isFeasibleNull) {
1196
Ted Kremenek7360fda2008-09-18 23:09:54 +00001197 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001198 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001199 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1200
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001201 // We don't use "MakeNode" here because the node will be a sink
1202 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001203 NodeTy* NullNode =
1204 Builder->generateNode(Ex, StNull, Pred,
1205 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001206
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001207 if (NullNode) {
1208
1209 NullNode->markAsSink();
1210
1211 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1212 else ExplicitNullDeref.insert(NullNode);
1213 }
1214 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001215
1216 if (!isFeasibleNotNull)
1217 return 0;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001218
1219 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001220 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001221 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1222 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1223 // Get the index of the accessed element.
1224 SVal Idx = ER->getIndex();
1225 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001226 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1227 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001228
1229 bool isFeasibleInBound = false;
1230 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1231 true, isFeasibleInBound);
1232
1233 bool isFeasibleOutBound = false;
1234 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1235 false, isFeasibleOutBound);
1236
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001237 if (isFeasibleOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001238 // Report warning. Make sink node manually.
1239 NodeTy* OOBNode =
1240 Builder->generateNode(Ex, StOutBound, Pred,
1241 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001242
1243 if (OOBNode) {
1244 OOBNode->markAsSink();
1245
1246 if (isFeasibleInBound)
1247 ImplicitOOBMemAccesses.insert(OOBNode);
1248 else
1249 ExplicitOOBMemAccesses.insert(OOBNode);
1250 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001251 }
1252
Ted Kremenek8c354752008-12-16 22:02:27 +00001253 if (!isFeasibleInBound)
1254 return 0;
1255
1256 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001257 }
1258 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001259
Ted Kremenek8c354752008-12-16 22:02:27 +00001260 // Generate a new node indicating the checks succeed.
1261 return Builder->generateNode(Ex, StNotNull, Pred,
1262 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001263}
1264
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001265//===----------------------------------------------------------------------===//
Ted Kremenek1670e402009-04-11 00:11:10 +00001266// Transfer function: OSAtomics.
1267//
1268// FIXME: Eventually refactor into a more "plugin" infrastructure.
1269//===----------------------------------------------------------------------===//
1270
1271// Mac OS X:
1272// http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3
1273// atomic.3.html
1274//
1275static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet<GRState>& Dst,
1276 GRExprEngine& Engine,
1277 GRStmtNodeBuilder<GRState>& Builder,
1278 CallExpr* CE, SVal L,
1279 ExplodedNode<GRState>* Pred) {
1280
1281 // Not enough arguments to match OSAtomicCompareAndSwap?
1282 if (CE->getNumArgs() != 3)
1283 return false;
1284
1285 ASTContext &C = Engine.getContext();
1286 Expr *oldValueExpr = CE->getArg(0);
1287 QualType oldValueType = C.getCanonicalType(oldValueExpr->getType());
1288
1289 Expr *newValueExpr = CE->getArg(1);
1290 QualType newValueType = C.getCanonicalType(newValueExpr->getType());
1291
1292 // Do the types of 'oldValue' and 'newValue' match?
1293 if (oldValueType != newValueType)
1294 return false;
1295
1296 Expr *theValueExpr = CE->getArg(2);
1297 const PointerType *theValueType = theValueExpr->getType()->getAsPointerType();
1298
1299 // theValueType not a pointer?
1300 if (!theValueType)
1301 return false;
1302
1303 QualType theValueTypePointee =
1304 C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
1305
1306 // The pointee must match newValueType and oldValueType.
1307 if (theValueTypePointee != newValueType)
1308 return false;
1309
1310 static unsigned magic_load = 0;
1311 static unsigned magic_store = 0;
1312
1313 const void *OSAtomicLoadTag = &magic_load;
1314 const void *OSAtomicStoreTag = &magic_store;
1315
1316 // Load 'theValue'.
1317 GRStateManager &StateMgr = Engine.getStateManager();
1318 const GRState *state = Pred->getState();
1319 ExplodedNodeSet<GRState> Tmp;
1320 SVal location = StateMgr.GetSVal(state, theValueExpr);
1321 Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag);
1322
1323 for (ExplodedNodeSet<GRState>::iterator I = Tmp.begin(), E = Tmp.end();
1324 I != E; ++I) {
1325
1326 ExplodedNode<GRState> *N = *I;
1327 const GRState *stateLoad = N->getState();
1328 SVal theValueVal = StateMgr.GetSVal(stateLoad, theValueExpr);
1329 SVal oldValueVal = StateMgr.GetSVal(stateLoad, oldValueExpr);
1330
1331 // Perform the comparison.
1332 SVal Cmp = Engine.EvalBinOp(BinaryOperator::EQ, theValueVal, oldValueVal,
1333 Engine.getContext().IntTy);
1334 bool isFeasible = false;
1335 const GRState *stateEqual = StateMgr.Assume(stateLoad, Cmp, true,
1336 isFeasible);
1337
1338 // Were they equal?
1339 if (isFeasible) {
1340 // Perform the store.
1341 ExplodedNodeSet<GRState> TmpStore;
1342 Engine.EvalStore(TmpStore, theValueExpr, N, stateEqual, location,
1343 StateMgr.GetSVal(stateEqual, newValueExpr),
1344 OSAtomicStoreTag);
1345
1346 // Now bind the result of the comparison.
1347 for (ExplodedNodeSet<GRState>::iterator I2 = TmpStore.begin(),
1348 E2 = TmpStore.end(); I2 != E2; ++I2) {
1349 ExplodedNode<GRState> *predNew = *I2;
1350 const GRState *stateNew = predNew->getState();
1351 SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType());
1352 Engine.MakeNode(Dst, CE, predNew, Engine.BindExpr(stateNew, CE, Res));
1353 }
1354 }
1355
1356 // Were they not equal?
1357 isFeasible = false;
1358 const GRState *stateNotEqual = StateMgr.Assume(stateLoad, Cmp, false,
1359 isFeasible);
1360
1361 if (isFeasible) {
1362 SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType());
1363 Engine.MakeNode(Dst, CE, N, Engine.BindExpr(stateNotEqual, CE, Res));
1364 }
1365 }
1366
1367 return true;
1368}
1369
1370static bool EvalOSAtomic(ExplodedNodeSet<GRState>& Dst,
1371 GRExprEngine& Engine,
1372 GRStmtNodeBuilder<GRState>& Builder,
1373 CallExpr* CE, SVal L,
1374 ExplodedNode<GRState>* Pred) {
Zhongxing Xu369f4472009-04-20 05:24:46 +00001375 const FunctionDecl* FD = L.getAsFunctionDecl();
1376 if (!FD)
Ted Kremenek1670e402009-04-11 00:11:10 +00001377 return false;
Zhongxing Xu369f4472009-04-20 05:24:46 +00001378
Ted Kremenek1670e402009-04-11 00:11:10 +00001379 const char *FName = FD->getNameAsCString();
1380
1381 // Check for compare and swap.
Ted Kremenekb3bf76f2009-04-11 00:54:13 +00001382 if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0 ||
1383 strncmp(FName, "objc_atomicCompareAndSwap", 25) == 0)
Ted Kremenek1670e402009-04-11 00:11:10 +00001384 return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred);
Ted Kremenekb3bf76f2009-04-11 00:54:13 +00001385
Ted Kremenek1670e402009-04-11 00:11:10 +00001386 // FIXME: Other atomics.
1387 return false;
1388}
1389
1390//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001391// Transfer function: Function calls.
1392//===----------------------------------------------------------------------===//
Ted Kremenek1670e402009-04-11 00:11:10 +00001393
1394void GRExprEngine::EvalCall(NodeSet& Dst, CallExpr* CE, SVal L, NodeTy* Pred) {
1395 assert (Builder && "GRStmtNodeBuilder must be defined.");
1396
1397 // FIXME: Allow us to chain together transfer functions.
1398 if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred))
1399 return;
1400
1401 getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred);
1402}
1403
Ted Kremenekde434242008-02-19 01:44:53 +00001404void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001405 CallExpr::arg_iterator AI,
1406 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001407 NodeSet& Dst)
1408{
1409 // Determine the type of function we're calling (if available).
Douglas Gregor72564e72009-02-26 23:50:07 +00001410 const FunctionProtoType *Proto = NULL;
Douglas Gregor9d293df2008-10-28 00:22:11 +00001411 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1412 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
Douglas Gregor72564e72009-02-26 23:50:07 +00001413 Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
Douglas Gregor9d293df2008-10-28 00:22:11 +00001414
1415 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1416}
1417
1418void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1419 CallExpr::arg_iterator AI,
1420 CallExpr::arg_iterator AE,
Douglas Gregor72564e72009-02-26 23:50:07 +00001421 NodeSet& Dst, const FunctionProtoType *Proto,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001422 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001423
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001424 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001425 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001426 // If the call argument is being bound to a reference parameter,
1427 // visit it as an lvalue, not an rvalue.
1428 bool VisitAsLvalue = false;
1429 if (Proto && ParamIdx < Proto->getNumArgs())
1430 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1431
1432 NodeSet DstTmp;
1433 if (VisitAsLvalue)
1434 VisitLValue(*AI, Pred, DstTmp);
1435 else
1436 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001437 ++AI;
1438
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001439 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001440 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001441
1442 return;
1443 }
1444
1445 // If we reach here we have processed all of the arguments. Evaluate
1446 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001447
Ted Kremenek994a09b2008-02-25 21:16:03 +00001448 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001449 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001450
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001451 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001452
Ted Kremenekde434242008-02-19 01:44:53 +00001453 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001454 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1455
Ted Kremeneka8538d92009-02-13 01:45:31 +00001456 const GRState* state = GetState(*DI);
1457 SVal L = GetSVal(state, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001458
Ted Kremeneka1354a52008-03-03 16:47:31 +00001459 // FIXME: Add support for symbolic function calls (calls involving
1460 // function pointer values that are symbolic).
1461
1462 // Check for undefined control-flow or calls to NULL.
1463
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001464 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001465 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001466
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001467 if (N) {
1468 N->markAsSink();
1469 BadCalls.insert(N);
1470 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001471
Ted Kremenekde434242008-02-19 01:44:53 +00001472 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001473 }
1474
1475 // Check for the "noreturn" attribute.
1476
1477 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Zhongxing Xu369f4472009-04-20 05:24:46 +00001478 const FunctionDecl* FD = L.getAsFunctionDecl();
1479 if (FD) {
Ted Kremenekb7252322009-04-10 00:01:14 +00001480 if (FD->getAttr<NoReturnAttr>() || FD->getAttr<AnalyzerNoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001481 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001482 else {
1483 // HACK: Some functions are not marked noreturn, and don't return.
1484 // Here are a few hardwired ones. If this takes too long, we can
1485 // potentially cache these results.
1486 const char* s = FD->getIdentifier()->getName();
1487 unsigned n = strlen(s);
1488
1489 switch (n) {
1490 default:
1491 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001492
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001493 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001494 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1495 break;
1496
1497 case 5:
1498 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001499 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001500 if (CE->getNumArgs() > 0) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001501 SVal X = GetSVal(state, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001502 // FIXME: use Assume to inspect the possible symbolic value of
1503 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001504 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001505 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001506 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001507 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001508 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001509 break;
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001510
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001511 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001512 if (!memcmp(s, "Assert", 6)) {
1513 Builder->BuildSinks = true;
1514 break;
1515 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001516
1517 // FIXME: This is just a wrapper around throwing an exception.
1518 // Eventually inter-procedural analysis should handle this easily.
1519 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1520
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001521 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001522
1523 case 7:
1524 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1525 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001526
Ted Kremenekf47bb782008-04-30 17:54:04 +00001527 case 8:
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001528 if (!memcmp(s ,"db_error", 8) ||
1529 !memcmp(s, "__assert", 8))
1530 Builder->BuildSinks = true;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001531 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001532
1533 case 12:
1534 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1535 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001536
Ted Kremenekf9683082008-09-19 02:30:47 +00001537 case 13:
1538 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1539 break;
1540
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001541 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001542 if (!memcmp(s, "dtrace_assfail", 14) ||
1543 !memcmp(s, "yy_fatal_error", 14))
1544 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001545 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001546
1547 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001548 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek40bbff02009-02-17 23:27:17 +00001549 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1550 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001551 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001552
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001553 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001554 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001555
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001556 }
1557 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001558
1559 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001560
Zhongxing Xu369f4472009-04-20 05:24:46 +00001561 if (FD) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001562
Zhongxing Xu369f4472009-04-20 05:24:46 +00001563 if (unsigned id = FD->getBuiltinID(getContext()))
Ted Kremenek55aea312008-03-05 22:59:42 +00001564 switch (id) {
1565 case Builtin::BI__builtin_expect: {
1566 // For __builtin_expect, just return the value of the subexpression.
1567 assert (CE->arg_begin() != CE->arg_end());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001568 SVal X = GetSVal(state, *(CE->arg_begin()));
1569 MakeNode(Dst, CE, *DI, BindExpr(state, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001570 continue;
1571 }
1572
Ted Kremenekb3021332008-11-02 00:35:01 +00001573 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001574 // FIXME: Refactor into StoreManager itself?
1575 MemRegionManager& RM = getStateManager().getRegionManager();
1576 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001577 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001578
1579 // Set the extent of the region in bytes. This enables us to use the
1580 // SVal of the argument directly. If we save the extent in bits, we
1581 // cannot represent values like symbol*8.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001582 SVal Extent = GetSVal(state, *(CE->arg_begin()));
1583 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001584
Ted Kremeneka8538d92009-02-13 01:45:31 +00001585 MakeNode(Dst, CE, *DI, BindExpr(state, CE, loc::MemRegionVal(R)));
Ted Kremenekb3021332008-11-02 00:35:01 +00001586 continue;
1587 }
1588
Ted Kremenek55aea312008-03-05 22:59:42 +00001589 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001590 break;
1591 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001592 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001593
Ted Kremenek186350f2008-04-23 20:12:28 +00001594 // Check any arguments passed-by-value against being undefined.
1595
1596 bool badArg = false;
1597
1598 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1599 I != E; ++I) {
1600
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001601 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001602 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001603
Ted Kremenek186350f2008-04-23 20:12:28 +00001604 if (N) {
1605 N->markAsSink();
1606 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001607 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001608
Ted Kremenek186350f2008-04-23 20:12:28 +00001609 badArg = true;
1610 break;
1611 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001612 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001613
1614 if (badArg)
1615 continue;
1616
1617 // Dispatch to the plug-in transfer function.
1618
1619 unsigned size = Dst.size();
1620 SaveOr OldHasGen(Builder->HasGeneratedNode);
1621 EvalCall(Dst, CE, L, *DI);
1622
1623 // Handle the case where no nodes where generated. Auto-generate that
1624 // contains the updated state if we aren't generating sinks.
1625
1626 if (!Builder->BuildSinks && Dst.size() == size &&
1627 !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001628 MakeNode(Dst, CE, *DI, state);
Ted Kremenekde434242008-02-19 01:44:53 +00001629 }
1630}
1631
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001632//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001633// Transfer function: Objective-C ivar references.
1634//===----------------------------------------------------------------------===//
1635
Ted Kremenekf5cae632009-02-28 20:50:43 +00001636static std::pair<const void*,const void*> EagerlyAssumeTag
1637 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1638
Ted Kremenekb2939022009-02-25 23:32:10 +00001639void GRExprEngine::EvalEagerlyAssume(NodeSet &Dst, NodeSet &Src, Expr *Ex) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001640 for (NodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1641 NodeTy *Pred = *I;
Ted Kremenekb2939022009-02-25 23:32:10 +00001642
1643 // Test if the previous node was as the same expression. This can happen
1644 // when the expression fails to evaluate to anything meaningful and
1645 // (as an optimization) we don't generate a node.
1646 ProgramPoint P = Pred->getLocation();
1647 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
1648 Dst.Add(Pred);
1649 continue;
1650 }
1651
Ted Kremenek48af2a92009-02-25 22:32:02 +00001652 const GRState* state = Pred->getState();
Ted Kremenekb2939022009-02-25 23:32:10 +00001653 SVal V = GetSVal(state, Ex);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00001654 if (isa<nonloc::SymExprVal>(V)) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001655 // First assume that the condition is true.
1656 bool isFeasible = false;
1657 const GRState *stateTrue = Assume(state, V, true, isFeasible);
1658 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001659 stateTrue = BindExpr(stateTrue, Ex, MakeConstantVal(1U, Ex));
1660 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001661 stateTrue, Pred));
1662 }
1663
1664 // Next, assume that the condition is false.
1665 isFeasible = false;
1666 const GRState *stateFalse = Assume(state, V, false, isFeasible);
1667 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001668 stateFalse = BindExpr(stateFalse, Ex, MakeConstantVal(0U, Ex));
1669 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001670 stateFalse, Pred));
1671 }
1672 }
1673 else
1674 Dst.Add(Pred);
1675 }
1676}
1677
1678//===----------------------------------------------------------------------===//
1679// Transfer function: Objective-C ivar references.
1680//===----------------------------------------------------------------------===//
1681
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001682void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1683 NodeTy* Pred, NodeSet& Dst,
1684 bool asLValue) {
1685
1686 Expr* Base = cast<Expr>(Ex->getBase());
1687 NodeSet Tmp;
1688 Visit(Base, Pred, Tmp);
1689
1690 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001691 const GRState* state = GetState(*I);
1692 SVal BaseVal = GetSVal(state, Base);
1693 SVal location = StateMgr.GetLValue(state, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001694
1695 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001696 MakeNode(Dst, Ex, *I, BindExpr(state, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001697 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001698 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001699 }
1700}
1701
1702//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001703// Transfer function: Objective-C fast enumeration 'for' statements.
1704//===----------------------------------------------------------------------===//
1705
1706void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1707 NodeTy* Pred, NodeSet& Dst) {
1708
1709 // ObjCForCollectionStmts are processed in two places. This method
1710 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1711 // statements within a basic block. This transfer function does two things:
1712 //
1713 // (1) binds the next container value to 'element'. This creates a new
1714 // node in the ExplodedGraph.
1715 //
1716 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1717 // whether or not the container has any more elements. This value
1718 // will be tested in ProcessBranch. We need to explicitly bind
1719 // this value because a container can contain nil elements.
1720 //
1721 // FIXME: Eventually this logic should actually do dispatches to
1722 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1723 // This will require simulating a temporary NSFastEnumerationState, either
1724 // through an SVal or through the use of MemRegions. This value can
1725 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1726 // terminates we reclaim the temporary (it goes out of scope) and we
1727 // we can test if the SVal is 0 or if the MemRegion is null (depending
1728 // on what approach we take).
1729 //
1730 // For now: simulate (1) by assigning either a symbol or nil if the
1731 // container is empty. Thus this transfer function will by default
1732 // result in state splitting.
1733
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001734 Stmt* elem = S->getElement();
1735 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001736
1737 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner7e24e822009-03-28 06:33:19 +00001738 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001739 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001740 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1741 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1742 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001743 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001744
1745 NodeSet Tmp;
1746 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001747
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001748 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1749 const GRState* state = GetState(*I);
1750 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1751 }
1752}
1753
1754void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1755 NodeTy* Pred, NodeSet& Dst,
1756 SVal ElementV) {
1757
1758
Ted Kremenekaf337412008-11-12 19:24:17 +00001759
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001760 // Get the current state. Use 'EvalLocation' to determine if it is a null
1761 // pointer, etc.
1762 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001763
Ted Kremenek8c354752008-12-16 22:02:27 +00001764 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1765 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001766 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001767
1768 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001769
Ted Kremenekaf337412008-11-12 19:24:17 +00001770 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001771 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001772 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1773 GRStateRef hasElems = state.BindExpr(S, TrueV);
1774
Ted Kremenekaf337412008-11-12 19:24:17 +00001775 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001776 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1777 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001778
1779 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1780 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1781 // FIXME: The proper thing to do is to really iterate over the
1782 // container. We will do this with dispatch logic to the store.
1783 // For now, just 'conjure' up a symbolic value.
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001784 QualType T = R->getRValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001785 assert (Loc::IsLocType(T));
1786 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xuea7c5ce2009-04-09 06:49:52 +00001787 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
1788 SVal V = Loc::MakeVal(getStoreManager().getRegionManager().getSymbolicRegion(Sym));
1789 hasElems = hasElems.BindLoc(ElementV, V);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001790
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001791 // Bind the location to 'nil' on the false branch.
1792 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1793 noElems = noElems.BindLoc(ElementV, nilV);
1794 }
1795
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001796 // Create the new nodes.
1797 MakeNode(Dst, S, Pred, hasElems);
1798 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001799}
1800
1801//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001802// Transfer function: Objective-C message expressions.
1803//===----------------------------------------------------------------------===//
1804
1805void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1806 NodeSet& Dst){
1807
1808 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1809 Pred, Dst);
1810}
1811
1812void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001813 ObjCMessageExpr::arg_iterator AI,
1814 ObjCMessageExpr::arg_iterator AE,
1815 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001816 if (AI == AE) {
1817
1818 // Process the receiver.
1819
1820 if (Expr* Receiver = ME->getReceiver()) {
1821 NodeSet Tmp;
1822 Visit(Receiver, Pred, Tmp);
1823
1824 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1825 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1826
1827 return;
1828 }
1829
1830 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1831 return;
1832 }
1833
1834 NodeSet Tmp;
1835 Visit(*AI, Pred, Tmp);
1836
1837 ++AI;
1838
1839 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1840 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1841}
1842
1843void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1844 NodeTy* Pred,
1845 NodeSet& Dst) {
1846
1847 // FIXME: More logic for the processing the method call.
1848
Ted Kremeneka8538d92009-02-13 01:45:31 +00001849 const GRState* state = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001850 bool RaisesException = false;
1851
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001852
1853 if (Expr* Receiver = ME->getReceiver()) {
1854
Ted Kremeneka8538d92009-02-13 01:45:31 +00001855 SVal L = GetSVal(state, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001856
Ted Kremenek21fe8372009-02-19 04:06:22 +00001857 // Check for undefined control-flow.
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001858 if (L.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001859 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001860
1861 if (N) {
1862 N->markAsSink();
1863 UndefReceivers.insert(N);
1864 }
1865
1866 return;
1867 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001868
Ted Kremenek21fe8372009-02-19 04:06:22 +00001869 // "Assume" that the receiver is not NULL.
1870 bool isFeasibleNotNull = false;
Ted Kremenekda9ae602009-04-08 18:51:08 +00001871 const GRState *StNotNull = Assume(state, L, true, isFeasibleNotNull);
Ted Kremenek21fe8372009-02-19 04:06:22 +00001872
1873 // "Assume" that the receiver is NULL.
1874 bool isFeasibleNull = false;
1875 const GRState *StNull = Assume(state, L, false, isFeasibleNull);
1876
1877 if (isFeasibleNull) {
Ted Kremenekfe630b92009-04-09 05:45:56 +00001878 QualType RetTy = ME->getType();
1879
Ted Kremenek21fe8372009-02-19 04:06:22 +00001880 // Check if the receiver was nil and the return value a struct.
Ted Kremenekfe630b92009-04-09 05:45:56 +00001881 if(RetTy->isRecordType()) {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001882 if (BR.getParentMap().isConsumedExpr(ME)) {
Ted Kremenek899b3de2009-04-08 03:07:17 +00001883 // The [0 ...] expressions will return garbage. Flag either an
1884 // explicit or implicit error. Because of the structure of this
1885 // function we currently do not bifurfacte the state graph at
1886 // this point.
1887 // FIXME: We should bifurcate and fill the returned struct with
1888 // garbage.
1889 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1890 N->markAsSink();
1891 if (isFeasibleNotNull)
1892 NilReceiverStructRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001893 else
Ted Kremeneke6449392009-04-09 04:06:51 +00001894 NilReceiverStructRetExplicit.insert(N);
Ted Kremenek899b3de2009-04-08 03:07:17 +00001895 }
1896 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001897 }
Ted Kremenekfe630b92009-04-09 05:45:56 +00001898 else {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001899 ASTContext& Ctx = getContext();
Ted Kremenekfe630b92009-04-09 05:45:56 +00001900 if (RetTy != Ctx.VoidTy) {
1901 if (BR.getParentMap().isConsumedExpr(ME)) {
1902 // sizeof(void *)
1903 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
1904 // sizeof(return type)
1905 const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001906
Ted Kremenekfe630b92009-04-09 05:45:56 +00001907 if(voidPtrSize < returnTypeSize) {
1908 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1909 N->markAsSink();
1910 if(isFeasibleNotNull)
1911 NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001912 else
Ted Kremenekfe630b92009-04-09 05:45:56 +00001913 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
Ted Kremenekfe630b92009-04-09 05:45:56 +00001914 }
1915 }
1916 else if (!isFeasibleNotNull) {
1917 // Handle the safe cases where the return value is 0 if the
1918 // receiver is nil.
1919 //
1920 // FIXME: For now take the conservative approach that we only
1921 // return null values if we *know* that the receiver is nil.
1922 // This is because we can have surprises like:
1923 //
1924 // ... = [[NSScreens screens] objectAtIndex:0];
1925 //
1926 // What can happen is that [... screens] could return nil, but
1927 // it most likely isn't nil. We should assume the semantics
1928 // of this case unless we have *a lot* more knowledge.
1929 //
Ted Kremenek8e5fb282009-04-09 16:46:55 +00001930 SVal V = ValMgr.makeZeroVal(ME->getType());
Ted Kremenekfe630b92009-04-09 05:45:56 +00001931 MakeNode(Dst, ME, Pred, BindExpr(StNull, ME, V));
Ted Kremeneke6449392009-04-09 04:06:51 +00001932 return;
1933 }
Ted Kremenek899b3de2009-04-08 03:07:17 +00001934 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001935 }
Ted Kremenek21fe8372009-02-19 04:06:22 +00001936 }
Ted Kremenekda9ae602009-04-08 18:51:08 +00001937 // We have handled the cases where the receiver is nil. The remainder
Ted Kremenekf8769c82009-04-09 06:02:06 +00001938 // of this method should assume that the receiver is not nil.
1939 if (!StNotNull)
1940 return;
1941
Ted Kremenekda9ae602009-04-08 18:51:08 +00001942 state = StNotNull;
Ted Kremenek21fe8372009-02-19 04:06:22 +00001943 }
1944
Ted Kremeneke448ab42008-05-01 18:33:28 +00001945 // Check if the "raise" message was sent.
1946 if (ME->getSelector() == RaiseSel)
1947 RaisesException = true;
1948 }
1949 else {
1950
1951 IdentifierInfo* ClsName = ME->getClassName();
1952 Selector S = ME->getSelector();
1953
1954 // Check for special instance methods.
1955
1956 if (!NSExceptionII) {
1957 ASTContext& Ctx = getContext();
1958
1959 NSExceptionII = &Ctx.Idents.get("NSException");
1960 }
1961
1962 if (ClsName == NSExceptionII) {
1963
1964 enum { NUM_RAISE_SELECTORS = 2 };
1965
1966 // Lazily create a cache of the selectors.
1967
1968 if (!NSExceptionInstanceRaiseSelectors) {
1969
1970 ASTContext& Ctx = getContext();
1971
1972 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1973
1974 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1975 unsigned idx = 0;
1976
1977 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001978 II.push_back(&Ctx.Idents.get("raise"));
1979 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001980 NSExceptionInstanceRaiseSelectors[idx++] =
1981 Ctx.Selectors.getSelector(II.size(), &II[0]);
1982
1983 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001984 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001985 NSExceptionInstanceRaiseSelectors[idx++] =
1986 Ctx.Selectors.getSelector(II.size(), &II[0]);
1987 }
1988
1989 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1990 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1991 RaisesException = true; break;
1992 }
1993 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001994 }
1995
1996 // Check for any arguments that are uninitialized/undefined.
1997
1998 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1999 I != E; ++I) {
2000
Ted Kremeneka8538d92009-02-13 01:45:31 +00002001 if (GetSVal(state, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002002
2003 // Generate an error node for passing an uninitialized/undefined value
2004 // as an argument to a message expression. This node is a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002005 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002006
2007 if (N) {
2008 N->markAsSink();
2009 MsgExprUndefArgs[N] = *I;
2010 }
2011
2012 return;
2013 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00002014 }
2015
2016 // Check if we raise an exception. For now treat these as sinks. Eventually
2017 // we will want to handle exceptions properly.
2018
2019 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2020
2021 if (RaisesException)
2022 Builder->BuildSinks = true;
2023
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002024 // Dispatch to plug-in transfer function.
2025
2026 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00002027 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002028
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002029 EvalObjCMessageExpr(Dst, ME, Pred);
2030
2031 // Handle the case where no nodes where generated. Auto-generate that
2032 // contains the updated state if we aren't generating sinks.
2033
Ted Kremenekb0533962008-04-18 20:35:30 +00002034 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002035 MakeNode(Dst, ME, Pred, state);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002036}
2037
2038//===----------------------------------------------------------------------===//
2039// Transfer functions: Miscellaneous statements.
2040//===----------------------------------------------------------------------===//
2041
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002042void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
2043 QualType PtrTy,
2044 Expr* CastE, NodeTy* Pred,
2045 NodeSet& Dst) {
2046 if (!V.isUnknownOrUndef()) {
2047 // FIXME: Determine if the number of bits of the target type is
2048 // equal or exceeds the number of bits to store the pointer value.
Ted Kremeneke121da42009-03-05 03:42:31 +00002049 // If not, flag an error.
Ted Kremenekac78d6b2009-03-05 03:44:53 +00002050 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, EvalCast(cast<Loc>(V),
2051 CastE->getType())));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002052 }
Ted Kremeneke121da42009-03-05 03:42:31 +00002053 else
2054 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002055}
2056
2057
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002058void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002059 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002060 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00002061 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00002062
Zhongxing Xud3118bd2008-10-31 07:26:14 +00002063 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00002064 T = ExCast->getTypeAsWritten();
2065
Zhongxing Xued340f72008-10-22 08:02:16 +00002066 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002067 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00002068 else
2069 Visit(Ex, Pred, S1);
2070
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002071 // Check for casting to "void".
Ted Kremenekdc402902009-03-04 00:14:35 +00002072 if (T->isVoidType()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002073 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002074 Dst.Add(*I1);
2075
Ted Kremenek874d63f2008-01-24 02:02:54 +00002076 return;
2077 }
2078
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002079 // FIXME: The rest of this should probably just go into EvalCall, and
2080 // let the transfer function object be responsible for constructing
2081 // nodes.
2082
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002083 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00002084 NodeTy* N = *I1;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002085 const GRState* state = GetState(N);
2086 SVal V = GetSVal(state, Ex);
Ted Kremenekc5302912009-03-05 20:22:13 +00002087 ASTContext& C = getContext();
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002088
2089 // Unknown?
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002090 if (V.isUnknown()) {
2091 Dst.Add(N);
2092 continue;
2093 }
2094
2095 // Undefined?
Ted Kremenekc5302912009-03-05 20:22:13 +00002096 if (V.isUndef())
2097 goto PassThrough;
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00002098
2099 // For const casts, just propagate the value.
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00002100 if (C.getCanonicalType(T).getUnqualifiedType() ==
Ted Kremenekc5302912009-03-05 20:22:13 +00002101 C.getCanonicalType(ExTy).getUnqualifiedType())
2102 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00002103
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002104 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002105 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002106 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002107 continue;
2108 }
2109
2110 // Check for casts from integers to pointers.
Ted Kremenek16aac322009-03-05 02:33:55 +00002111 if (Loc::IsLocType(T) && ExTy->isIntegerType()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002112 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002113 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002114 V = LV->getLoc();
Ted Kremeneka8538d92009-02-13 01:45:31 +00002115 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenekc5302912009-03-05 20:22:13 +00002116 continue;
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002117 }
Ted Kremeneke121da42009-03-05 03:42:31 +00002118
Ted Kremenekc5302912009-03-05 20:22:13 +00002119 goto DispatchCast;
Ted Kremenek16aac322009-03-05 02:33:55 +00002120 }
2121
2122 // Just pass through function and block pointers.
2123 if (ExTy->isBlockPointerType() || ExTy->isFunctionPointerType()) {
2124 assert(Loc::IsLocType(T));
Ted Kremenekc5302912009-03-05 20:22:13 +00002125 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00002126 }
2127
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002128 // Check for casts from array type to another type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00002129 if (ExTy->isArrayType()) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002130 // We will always decay to a pointer.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +00002131 V = StateMgr.ArrayToPointer(cast<Loc>(V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002132
2133 // Are we casting from an array to a pointer? If so just pass on
2134 // the decayed value.
Ted Kremenekc5302912009-03-05 20:22:13 +00002135 if (T->isPointerType())
2136 goto PassThrough;
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002137
2138 // Are we casting from an array to an integer? If so, cast the decayed
2139 // pointer value to an integer.
2140 assert(T->isIntegerType());
2141 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
2142 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002143 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xue1911af2008-10-23 03:10:39 +00002144 continue;
2145 }
2146
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002147 // Check for casts from a region to a specific type.
Ted Kremenek5c42f9b2009-03-05 22:47:06 +00002148 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
2149 // FIXME: For TypedViewRegions, we should handle the case where the
2150 // underlying symbolic pointer is a function pointer or
2151 // block pointer.
2152
2153 // FIXME: We should handle the case where we strip off view layers to get
2154 // to a desugared type.
2155
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002156 assert(Loc::IsLocType(T));
Zhongxing Xua1718c72009-04-03 07:33:13 +00002157 // We get a symbolic function pointer for a dereference of a function
2158 // pointer, but it is of function type. Example:
2159
2160 // struct FPRec {
2161 // void (*my_func)(int * x);
2162 // };
2163 //
2164 // int bar(int x);
2165 //
2166 // int f1_a(struct FPRec* foo) {
2167 // int x;
2168 // (*foo->my_func)(&x);
2169 // return bar(x)+1; // no-warning
2170 // }
2171
2172 assert(Loc::IsLocType(ExTy) || ExTy->isFunctionType());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002173
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002174 const MemRegion* R = RV->getRegion();
2175 StoreManager& StoreMgr = getStoreManager();
2176
2177 // Delegate to store manager to get the result of casting a region
2178 // to a different type.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002179 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002180
2181 // Inspect the result. If the MemRegion* returned is NULL, this
2182 // expression evaluates to UnknownVal.
2183 R = Res.getRegion();
2184 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
2185
2186 // Generate the new node in the ExplodedGraph.
2187 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00002188 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002189 }
Zhongxing Xu3330dcb2009-04-10 06:06:13 +00002190 // All other cases.
Ted Kremenekc5302912009-03-05 20:22:13 +00002191 DispatchCast: {
2192 MakeNode(Dst, CastE, N, BindExpr(state, CastE,
2193 EvalCast(V, CastE->getType())));
2194 continue;
2195 }
2196
2197 PassThrough: {
2198 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
2199 }
Ted Kremenek874d63f2008-01-24 02:02:54 +00002200 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002201}
2202
Ted Kremenek4f090272008-10-27 21:54:31 +00002203void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002204 NodeTy* Pred, NodeSet& Dst,
2205 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00002206 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
2207 NodeSet Tmp;
2208 Visit(ILE, Pred, Tmp);
2209
2210 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002211 const GRState* state = GetState(*I);
2212 SVal ILV = GetSVal(state, ILE);
2213 state = StateMgr.BindCompoundLiteral(state, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00002214
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002215 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002216 MakeNode(Dst, CL, *I, BindExpr(state, CL, StateMgr.GetLValue(state, CL)));
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002217 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002218 MakeNode(Dst, CL, *I, BindExpr(state, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00002219 }
2220}
2221
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002222void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002223
Ted Kremenek8369a8b2008-10-06 18:43:53 +00002224 // The CFG has one DeclStmt per Decl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002225 Decl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002226
2227 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002228 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00002229
Ted Kremenekefd59942008-12-08 22:47:34 +00002230 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00002231 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002232
2233 // FIXME: static variables may have an initializer, but the second
2234 // time a function is called those values may not be current.
2235 NodeSet Tmp;
2236
Ted Kremenekaf337412008-11-12 19:24:17 +00002237 if (InitEx)
2238 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002239
2240 if (Tmp.empty())
2241 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002242
2243 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002244 const GRState* state = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00002245 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002246
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002247 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2248 QualType T = getContext().getCanonicalType(VD->getType());
2249 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2250 // FIXME: Handle multi-dimensional VLAs.
2251
2252 Expr* SE = VLA->getSizeExpr();
2253 SVal Size = GetSVal(state, SE);
2254
2255 if (Size.isUndef()) {
2256 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
2257 N->markAsSink();
2258 ExplicitBadSizedVLA.insert(N);
2259 }
2260 continue;
2261 }
2262
2263 bool isFeasibleZero = false;
2264 const GRState* ZeroSt = Assume(state, Size, false, isFeasibleZero);
2265
2266 bool isFeasibleNotZero = false;
2267 state = Assume(state, Size, true, isFeasibleNotZero);
2268
2269 if (isFeasibleZero) {
2270 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
2271 N->markAsSink();
2272 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
2273 else ExplicitBadSizedVLA.insert(N);
2274 }
2275 }
2276
2277 if (!isFeasibleNotZero)
2278 continue;
2279 }
2280
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002281 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00002282 if (InitEx) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002283 SVal InitVal = GetSVal(state, InitEx);
Ted Kremenekaf337412008-11-12 19:24:17 +00002284 QualType T = VD->getType();
2285
2286 // Recover some path-sensitivity if a scalar value evaluated to
2287 // UnknownVal.
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002288 if (InitVal.isUnknown() ||
2289 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002290 InitVal = ValMgr.getConjuredSymbolVal(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00002291 }
2292
Ted Kremeneka8538d92009-02-13 01:45:31 +00002293 state = StateMgr.BindDecl(state, VD, InitVal);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002294
2295 // The next thing to do is check if the GRTransferFuncs object wants to
2296 // update the state based on the new binding. If the GRTransferFunc
2297 // object doesn't do anything, just auto-propagate the current state.
2298 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
2299 getTF().EvalBind(BuilderRef, loc::MemRegionVal(StateMgr.getRegion(VD)),
2300 InitVal);
2301 }
2302 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002303 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002304 MakeNode(Dst, DS, *I, state);
Ted Kremenekefd59942008-12-08 22:47:34 +00002305 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002306 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002307}
Ted Kremenek874d63f2008-01-24 02:02:54 +00002308
Ted Kremenekf75b1862008-10-30 17:47:32 +00002309namespace {
2310 // This class is used by VisitInitListExpr as an item in a worklist
2311 // for processing the values contained in an InitListExpr.
2312class VISIBILITY_HIDDEN InitListWLItem {
2313public:
2314 llvm::ImmutableList<SVal> Vals;
2315 GRExprEngine::NodeTy* N;
2316 InitListExpr::reverse_iterator Itr;
2317
2318 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
2319 InitListExpr::reverse_iterator itr)
2320 : Vals(vals), N(n), Itr(itr) {}
2321};
2322}
2323
2324
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002325void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
2326 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00002327
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002328 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00002329 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00002330 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002331
Zhongxing Xu05d1c572008-10-30 05:35:59 +00002332 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00002333
Ted Kremeneka49e3672008-10-30 23:14:36 +00002334 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00002335
Ted Kremeneka49e3672008-10-30 23:14:36 +00002336 // Handle base case where the initializer has no elements.
2337 // e.g: static int* myArray[] = {};
2338 if (NumInitElements == 0) {
2339 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
2340 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
2341 return;
2342 }
2343
2344 // Create a worklist to process the initializers.
2345 llvm::SmallVector<InitListWLItem, 10> WorkList;
2346 WorkList.reserve(NumInitElements);
2347 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002348 InitListExpr::reverse_iterator ItrEnd = E->rend();
2349
Ted Kremeneka49e3672008-10-30 23:14:36 +00002350 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002351 while (!WorkList.empty()) {
2352 InitListWLItem X = WorkList.back();
2353 WorkList.pop_back();
2354
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002355 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00002356 Visit(*X.Itr, X.N, Tmp);
2357
2358 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002359
Ted Kremenekf75b1862008-10-30 17:47:32 +00002360 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2361 // Get the last initializer value.
2362 state = GetState(*NI);
2363 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
2364
2365 // Construct the new list of values by prepending the new value to
2366 // the already constructed list.
2367 llvm::ImmutableList<SVal> NewVals =
2368 getBasicVals().consVals(InitV, X.Vals);
2369
2370 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00002371 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002372 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002373
Ted Kremenekf75b1862008-10-30 17:47:32 +00002374 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00002375 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002376 }
2377 else {
2378 // Still some initializer values to go. Push them onto the worklist.
2379 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2380 }
2381 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002382 }
Ted Kremenek87903072008-10-30 18:34:31 +00002383
2384 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002385 }
2386
Ted Kremenek062e2f92008-11-13 06:10:40 +00002387 if (T->isUnionType() || T->isVectorType()) {
2388 // FIXME: to be implemented.
2389 // Note: That vectors can return true for T->isIntegerType()
2390 MakeNode(Dst, E, Pred, state);
2391 return;
2392 }
2393
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002394 if (Loc::IsLocType(T) || T->isIntegerType()) {
2395 assert (E->getNumInits() == 1);
2396 NodeSet Tmp;
2397 Expr* Init = E->getInit(0);
2398 Visit(Init, Pred, Tmp);
2399 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
2400 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002401 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002402 }
2403 return;
2404 }
2405
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002406
2407 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2408 assert(0 && "unprocessed InitListExpr type");
2409}
Ted Kremenekf233d482008-02-05 00:26:40 +00002410
Sebastian Redl05189992008-11-11 17:56:53 +00002411/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2412void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2413 NodeTy* Pred,
2414 NodeSet& Dst) {
2415 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00002416 uint64_t amt;
2417
2418 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002419 if (T == getContext().VoidTy) {
2420 // sizeof(void) == 1 byte.
2421 amt = 1;
2422 }
2423 else if (!T.getTypePtr()->isConstantSizeType()) {
2424 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002425 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002426 }
2427 else if (T->isObjCInterfaceType()) {
2428 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2429 // the compiler has laid out its representation. Just report Unknown
2430 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002431 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002432 }
2433 else {
2434 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002435 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002436 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002437 }
2438 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002439 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002440
Ted Kremenek0e561a32008-03-21 21:30:14 +00002441 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002442 BindExpr(GetState(Pred), Ex,
2443 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002444}
2445
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002446
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002447void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002448 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002449
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002450 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002451
2452 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002453 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002454
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002455 case UnaryOperator::Deref: {
2456
2457 Expr* Ex = U->getSubExpr()->IgnoreParens();
2458 NodeSet Tmp;
2459 Visit(Ex, Pred, Tmp);
2460
2461 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002462
Ted Kremeneka8538d92009-02-13 01:45:31 +00002463 const GRState* state = GetState(*I);
2464 SVal location = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002465
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002466 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002467 MakeNode(Dst, U, *I, BindExpr(state, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002468 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002469 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002470 }
2471
2472 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002473 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002474
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002475 case UnaryOperator::Real: {
2476
2477 Expr* Ex = U->getSubExpr()->IgnoreParens();
2478 NodeSet Tmp;
2479 Visit(Ex, Pred, Tmp);
2480
2481 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2482
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002483 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002484 if (Ex->getType()->isAnyComplexType()) {
2485 // Just report "Unknown."
2486 Dst.Add(*I);
2487 continue;
2488 }
2489
2490 // For all other types, UnaryOperator::Real is an identity operation.
2491 assert (U->getType() == Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002492 const GRState* state = GetState(*I);
2493 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002494 }
2495
2496 return;
2497 }
2498
2499 case UnaryOperator::Imag: {
2500
2501 Expr* Ex = U->getSubExpr()->IgnoreParens();
2502 NodeSet Tmp;
2503 Visit(Ex, Pred, Tmp);
2504
2505 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002506 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002507 if (Ex->getType()->isAnyComplexType()) {
2508 // Just report "Unknown."
2509 Dst.Add(*I);
2510 continue;
2511 }
2512
2513 // For all other types, UnaryOperator::Float returns 0.
2514 assert (Ex->getType()->isIntegerType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002515 const GRState* state = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002516 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002517 MakeNode(Dst, U, *I, BindExpr(state, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002518 }
2519
2520 return;
2521 }
2522
2523 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002524 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002525 Dst.Add(Pred);
2526 return;
2527
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002528 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002529 case UnaryOperator::Extension: {
2530
2531 // Unary "+" is a no-op, similar to a parentheses. We still have places
2532 // where it may be a block-level expression, so we need to
2533 // generate an extra node that just propagates the value of the
2534 // subexpression.
2535
2536 Expr* Ex = U->getSubExpr()->IgnoreParens();
2537 NodeSet Tmp;
2538 Visit(Ex, Pred, Tmp);
2539
2540 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002541 const GRState* state = GetState(*I);
2542 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002543 }
2544
2545 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002546 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002547
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002548 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002549
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002550 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002551 Expr* Ex = U->getSubExpr()->IgnoreParens();
2552 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002553 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002554
2555 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002556 const GRState* state = GetState(*I);
2557 SVal V = GetSVal(state, Ex);
2558 state = BindExpr(state, U, V);
2559 MakeNode(Dst, U, *I, state);
Ted Kremenek89063af2008-02-21 19:15:37 +00002560 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002561
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002562 return;
2563 }
2564
2565 case UnaryOperator::LNot:
2566 case UnaryOperator::Minus:
2567 case UnaryOperator::Not: {
2568
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002569 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002570 Expr* Ex = U->getSubExpr()->IgnoreParens();
2571 NodeSet Tmp;
2572 Visit(Ex, Pred, Tmp);
2573
2574 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002575 const GRState* state = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002576
2577 // Get the value of the subexpression.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002578 SVal V = GetSVal(state, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002579
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002580 if (V.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002581 MakeNode(Dst, U, *I, BindExpr(state, U, V));
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002582 continue;
2583 }
2584
Ted Kremenek60595da2008-11-15 04:01:56 +00002585// QualType DstT = getContext().getCanonicalType(U->getType());
2586// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2587//
2588// if (DstT != SrcT) // Perform promotions.
2589// V = EvalCast(V, DstT);
2590//
2591// if (V.isUnknownOrUndef()) {
2592// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2593// continue;
2594// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002595
2596 switch (U->getOpcode()) {
2597 default:
2598 assert(false && "Invalid Opcode.");
2599 break;
2600
2601 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002602 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002603 state = BindExpr(state, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002604 break;
2605
2606 case UnaryOperator::Minus:
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, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002609 break;
2610
2611 case UnaryOperator::LNot:
2612
2613 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2614 //
2615 // Note: technically we do "E == 0", but this is the same in the
2616 // transfer functions as "0 == E".
2617
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002618 if (isa<Loc>(V)) {
Ted Kremenekda9ae602009-04-08 18:51:08 +00002619 Loc X = Loc::MakeNull(getBasicVals());
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002620 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X,
2621 U->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002622 state = BindExpr(state, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002623 }
2624 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002625 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002626#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002627 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002628 state = SetSVal(state, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002629#else
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002630 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I,
2631 U->getType());
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002632 continue;
2633#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002634 }
2635
2636 break;
2637 }
2638
Ted Kremeneka8538d92009-02-13 01:45:31 +00002639 MakeNode(Dst, U, *I, state);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002640 }
2641
2642 return;
2643 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002644 }
2645
2646 // Handle ++ and -- (both pre- and post-increment).
2647
2648 assert (U->isIncrementDecrementOp());
2649 NodeSet Tmp;
2650 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002651 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002652
2653 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2654
Ted Kremeneka8538d92009-02-13 01:45:31 +00002655 const GRState* state = GetState(*I);
2656 SVal V1 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002657
2658 // Perform a load.
2659 NodeSet Tmp2;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002660 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002661
2662 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2663
Ted Kremeneka8538d92009-02-13 01:45:31 +00002664 state = GetState(*I2);
2665 SVal V2 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002666
2667 // Propagate unknown and undefined values.
2668 if (V2.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002669 MakeNode(Dst, U, *I2, BindExpr(state, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002670 continue;
2671 }
2672
Ted Kremenek21028dd2009-03-11 03:54:24 +00002673 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002674 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2675 : BinaryOperator::Sub;
Ted Kremenek21028dd2009-03-11 03:54:24 +00002676
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002677 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U), U->getType());
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002678
2679 // Conjure a new symbol if necessary to recover precision.
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002680 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002681 Result = ValMgr.getConjuredSymbolVal(Ex,
2682 Builder->getCurrentBlockCount());
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002683
2684 // If the value is a location, ++/-- should always preserve
2685 // non-nullness. Check if the original value was non-null, and if so propagate
2686 // that constraint.
2687 if (Loc::IsLocType(U->getType())) {
2688 SVal Constraint = EvalBinOp(BinaryOperator::EQ, V2,
2689 ValMgr.makeZeroVal(U->getType()),
2690 getContext().IntTy);
2691
2692 bool isFeasible = false;
2693 Assume(state, Constraint, true, isFeasible);
2694 if (!isFeasible) {
2695 // It isn't feasible for the original value to be null.
2696 // Propagate this constraint.
2697 Constraint = EvalBinOp(BinaryOperator::EQ, Result,
2698 ValMgr.makeZeroVal(U->getType()),
2699 getContext().IntTy);
2700
2701 bool isFeasible = false;
2702 state = Assume(state, Constraint, false, isFeasible);
2703 assert(isFeasible && state);
2704 }
2705 }
2706 }
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002707
Ted Kremeneka8538d92009-02-13 01:45:31 +00002708 state = BindExpr(state, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002709
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002710 // Perform the store.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002711 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002712 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002713 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002714}
2715
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002716void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2717 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2718}
2719
2720void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2721 AsmStmt::outputs_iterator I,
2722 AsmStmt::outputs_iterator E,
2723 NodeTy* Pred, NodeSet& Dst) {
2724 if (I == E) {
2725 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2726 return;
2727 }
2728
2729 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002730 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002731
2732 ++I;
2733
2734 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2735 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2736}
2737
2738void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2739 AsmStmt::inputs_iterator I,
2740 AsmStmt::inputs_iterator E,
2741 NodeTy* Pred, NodeSet& Dst) {
2742 if (I == E) {
2743
2744 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002745 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002746
2747 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2748 // which interprets the inline asm and stores proper results in the
2749 // outputs.
2750
Ted Kremeneka8538d92009-02-13 01:45:31 +00002751 const GRState* state = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002752
2753 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2754 OE = A->end_outputs(); OI != OE; ++OI) {
2755
Ted Kremeneka8538d92009-02-13 01:45:31 +00002756 SVal X = GetSVal(state, *OI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002757 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002758
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002759 if (isa<Loc>(X))
Ted Kremeneka8538d92009-02-13 01:45:31 +00002760 state = BindLoc(state, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002761 }
2762
Ted Kremeneka8538d92009-02-13 01:45:31 +00002763 MakeNode(Dst, A, Pred, state);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002764 return;
2765 }
2766
2767 NodeSet Tmp;
2768 Visit(*I, Pred, Tmp);
2769
2770 ++I;
2771
2772 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2773 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2774}
2775
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002776void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2777 assert (Builder && "GRStmtNodeBuilder must be defined.");
2778
2779 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002780
Ted Kremenek186350f2008-04-23 20:12:28 +00002781 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2782 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002783
Ted Kremenek729a9a22008-07-17 23:15:45 +00002784 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002785
Ted Kremenekb0533962008-04-18 20:35:30 +00002786 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002787
Ted Kremenekb0533962008-04-18 20:35:30 +00002788 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002789 MakeNode(Dst, S, Pred, GetState(Pred));
2790}
2791
Ted Kremenek02737ed2008-03-31 15:02:58 +00002792void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2793
2794 Expr* R = S->getRetValue();
2795
2796 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002797 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002798 return;
2799 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002800
Ted Kremenek5917d782008-11-21 00:27:44 +00002801 NodeSet Tmp;
2802 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002803
Ted Kremenek5917d782008-11-21 00:27:44 +00002804 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2805 SVal X = GetSVal((*I)->getState(), R);
2806
2807 // Check if we return the address of a stack variable.
2808 if (isa<loc::MemRegionVal>(X)) {
2809 // Determine if the value is on the stack.
2810 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002811
Ted Kremenek5917d782008-11-21 00:27:44 +00002812 if (R && getStateManager().hasStackStorage(R)) {
2813 // Create a special node representing the error.
2814 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2815 N->markAsSink();
2816 RetsStackAddr.insert(N);
2817 }
2818 continue;
2819 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002820 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002821 // Check if we return an undefined value.
2822 else if (X.isUndef()) {
2823 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2824 N->markAsSink();
2825 RetsUndef.insert(N);
2826 }
2827 continue;
2828 }
2829
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002830 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002831 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002832}
Ted Kremenek55deb972008-03-25 00:34:37 +00002833
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002834//===----------------------------------------------------------------------===//
2835// Transfer functions: Binary operators.
2836//===----------------------------------------------------------------------===//
2837
Ted Kremeneka8538d92009-02-13 01:45:31 +00002838const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002839 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002840
2841 // Divide by undefined? (potentially zero)
2842
2843 if (Denom.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002844 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002845
2846 if (DivUndef) {
2847 DivUndef->markAsSink();
2848 ExplicitBadDivides.insert(DivUndef);
2849 }
2850
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002851 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002852 }
2853
2854 // Check for divide/remainder-by-zero.
2855 // First, "assume" that the denominator is 0 or undefined.
2856
2857 bool isFeasibleZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002858 const GRState* ZeroSt = Assume(state, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002859
2860 // Second, "assume" that the denominator cannot be 0.
2861
2862 bool isFeasibleNotZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002863 state = Assume(state, Denom, true, isFeasibleNotZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002864
2865 // Create the node for the divide-by-zero (if it occurred).
2866
2867 if (isFeasibleZero)
2868 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2869 DivZeroNode->markAsSink();
2870
2871 if (isFeasibleNotZero)
2872 ImplicitBadDivides.insert(DivZeroNode);
2873 else
2874 ExplicitBadDivides.insert(DivZeroNode);
2875
2876 }
2877
Ted Kremeneka8538d92009-02-13 01:45:31 +00002878 return isFeasibleNotZero ? state : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002879}
2880
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002881void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002882 GRExprEngine::NodeTy* Pred,
2883 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002884
2885 NodeSet Tmp1;
2886 Expr* LHS = B->getLHS()->IgnoreParens();
2887 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002888
Ted Kremenek759623e2008-12-06 02:39:30 +00002889 // FIXME: Add proper support for ObjCKVCRefExpr.
2890 if (isa<ObjCKVCRefExpr>(LHS)) {
2891 Visit(RHS, Pred, Dst);
2892 return;
2893 }
2894
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002895 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002896 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002897 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002898 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002899
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002900 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002901
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002902 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002903
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002904 // Process the RHS.
2905
2906 NodeSet Tmp2;
2907 Visit(RHS, *I1, Tmp2);
2908
2909 // With both the LHS and RHS evaluated, process the operation itself.
2910
2911 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002912
Ted Kremeneka8538d92009-02-13 01:45:31 +00002913 const GRState* state = GetState(*I2);
2914 const GRState* OldSt = state;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002915
Ted Kremeneka8538d92009-02-13 01:45:31 +00002916 SVal RightV = GetSVal(state, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002917 BinaryOperator::Opcode Op = B->getOpcode();
2918
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002919 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002920
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002921 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002922
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002923 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002924 // FIXME: Handle structs.
2925 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002926
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002927 if ((RightV.isUnknown() ||
2928 !getConstraintManager().canReasonAbout(RightV))
2929 && (Loc::IsLocType(T) ||
2930 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002931 unsigned Count = Builder->getCurrentBlockCount();
2932 RightV = ValMgr.getConjuredSymbolVal(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002933 }
2934
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002935 // Simulate the effects of a "store": bind the value of the RHS
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002936 // to the L-Value represented by the LHS.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002937 EvalStore(Dst, B, LHS, *I2, BindExpr(state, B, RightV), LeftV,
2938 RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002939 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002940 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002941
2942 case BinaryOperator::Div:
2943 case BinaryOperator::Rem:
2944
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002945 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002946 if (RHS->getType()->isIntegerType() &&
2947 RHS->getType()->isScalarType()) {
2948
Ted Kremeneka8538d92009-02-13 01:45:31 +00002949 state = CheckDivideZero(B, state, *I2, RightV);
2950 if (!state) continue;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002951 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002952
2953 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002954
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002955 default: {
2956
2957 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002958 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002959
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002960 // Process non-assignements except commas or short-circuited
2961 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002962
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002963 SVal Result = EvalBinOp(Op, LeftV, RightV, B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002964
2965 if (Result.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002966 if (OldSt != state) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002967 // Generate a new node if we have already created a new state.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002968 MakeNode(Dst, B, *I2, state);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002969 }
2970 else
2971 Dst.Add(*I2);
2972
Ted Kremenek89063af2008-02-21 19:15:37 +00002973 continue;
2974 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002975
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002976 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002977
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002978 // The operands were *not* undefined, but the result is undefined.
2979 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002980
Ted Kremeneka8538d92009-02-13 01:45:31 +00002981 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002982 UndefNode->markAsSink();
2983 UndefResults.insert(UndefNode);
2984 }
2985
2986 continue;
2987 }
2988
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002989 // Otherwise, create a new node.
2990
Ted Kremeneka8538d92009-02-13 01:45:31 +00002991 MakeNode(Dst, B, *I2, BindExpr(state, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002992 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002993 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002994 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002995
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002996 assert (B->isCompoundAssignmentOp());
2997
Ted Kremenekcad29962009-02-07 00:52:24 +00002998 switch (Op) {
2999 default:
3000 assert(0 && "Invalid opcode for compound assignment.");
3001 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
3002 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
3003 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
3004 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
3005 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
3006 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
3007 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
3008 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
3009 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
3010 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek934e3e92008-10-27 23:02:39 +00003011 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003012
3013 // Perform a load (the LHS). This performs the checks for
3014 // null dereferences, and so on.
3015 NodeSet Tmp3;
Ted Kremeneka8538d92009-02-13 01:45:31 +00003016 SVal location = GetSVal(state, LHS);
3017 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003018
3019 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
3020
Ted Kremeneka8538d92009-02-13 01:45:31 +00003021 state = GetState(*I3);
3022 SVal V = GetSVal(state, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003023
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003024 // Check for divide-by-zero.
3025 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00003026 && RHS->getType()->isIntegerType()
3027 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003028
3029 // CheckDivideZero returns a new state where the denominator
3030 // is assumed to be non-zero.
Ted Kremeneka8538d92009-02-13 01:45:31 +00003031 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003032
Ted Kremeneka8538d92009-02-13 01:45:31 +00003033 if (!state)
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003034 continue;
3035 }
3036
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003037 // Propagate undefined values (left-side).
3038 if (V.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00003039 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003040 continue;
3041 }
3042
3043 // Propagate unknown values (left and right-side).
3044 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00003045 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, UnknownVal()),
3046 location, UnknownVal());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003047 continue;
3048 }
3049
3050 // At this point:
3051 //
3052 // The LHS is not Undef/Unknown.
3053 // The RHS is not Unknown.
3054
3055 // Get the computation type.
Eli Friedmanab3a8522009-03-28 01:22:36 +00003056 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek60595da2008-11-15 04:01:56 +00003057 CTy = getContext().getCanonicalType(CTy);
Eli Friedmanab3a8522009-03-28 01:22:36 +00003058
3059 QualType CLHSTy = cast<CompoundAssignOperator>(B)->getComputationLHSType();
3060 CLHSTy = getContext().getCanonicalType(CTy);
3061
Ted Kremenek60595da2008-11-15 04:01:56 +00003062 QualType LTy = getContext().getCanonicalType(LHS->getType());
3063 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedmanab3a8522009-03-28 01:22:36 +00003064
3065 // Promote LHS.
3066 V = EvalCast(V, CLHSTy);
3067
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003068 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003069 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00003070 // Propagate undefined values (right-side).
Ted Kremeneke121da42009-03-05 03:42:31 +00003071 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, RightV), location,
Ted Kremeneka8538d92009-02-13 01:45:31 +00003072 RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003073 continue;
3074 }
3075
Ted Kremenek60595da2008-11-15 04:01:56 +00003076 // Compute the result of the operation.
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003077 SVal Result = EvalCast(EvalBinOp(Op, V, RightV, CTy), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003078
3079 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003080 // The operands were not undefined, but the result is undefined.
Ted Kremeneka8538d92009-02-13 01:45:31 +00003081 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003082 UndefNode->markAsSink();
3083 UndefResults.insert(UndefNode);
3084 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003085 continue;
3086 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003087
3088 // EXPERIMENTAL: "Conjured" symbols.
3089 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00003090
3091 SVal LHSVal;
3092
Ted Kremenek276c6ac2009-03-11 02:24:48 +00003093 if ((Result.isUnknown() ||
3094 !getConstraintManager().canReasonAbout(Result))
3095 && (Loc::IsLocType(CTy)
3096 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00003097
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003098 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003099
Ted Kremenek60595da2008-11-15 04:01:56 +00003100 // The symbolic value is actually for the type of the left-hand side
3101 // expression, not the computation type, as this is the value the
3102 // LValue on the LHS will bind to.
Ted Kremenek8d7f5482009-04-09 22:22:44 +00003103 LHSVal = ValMgr.getConjuredSymbolVal(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00003104
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00003105 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00003106 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003107 }
Ted Kremenek60595da2008-11-15 04:01:56 +00003108 else {
3109 // The left-hand side may bind to a different value then the
3110 // computation type.
3111 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
3112 }
3113
Ted Kremeneka8538d92009-02-13 01:45:31 +00003114 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, Result), location,
3115 LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003116 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00003117 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00003118 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00003119}
Ted Kremenekee985462008-01-16 18:18:48 +00003120
3121//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003122// Transfer-function Helpers.
3123//===----------------------------------------------------------------------===//
3124
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003125void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003126 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00003127 NonLoc L, NonLoc R,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003128 ExplodedNode<GRState>* Pred, QualType T) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003129
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003130 GRStateSet OStates;
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003131 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R, T);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003132
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003133 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003134 MakeNode(Dst, Ex, Pred, *I);
3135}
3136
Ted Kremeneka8538d92009-02-13 01:45:31 +00003137void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003138 Expr* Ex, BinaryOperator::Opcode Op,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003139 NonLoc L, NonLoc R, QualType T) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003140
Ted Kremeneka8538d92009-02-13 01:45:31 +00003141 GRStateSet::AutoPopulate AP(OStates, state);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003142 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R, T);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003143}
3144
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003145SVal GRExprEngine::EvalBinOp(BinaryOperator::Opcode Op, SVal L, SVal R,
3146 QualType T) {
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003147
3148 if (L.isUndef() || R.isUndef())
3149 return UndefinedVal();
3150
3151 if (L.isUnknown() || R.isUnknown())
3152 return UnknownVal();
3153
3154 if (isa<Loc>(L)) {
3155 if (isa<Loc>(R))
3156 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
3157 else
3158 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<NonLoc>(R));
3159 }
3160
3161 if (isa<Loc>(R)) {
3162 // Support pointer arithmetic where the increment/decrement operand
3163 // is on the left and the pointer on the right.
3164
3165 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
3166
3167 // Commute the operands.
3168 return getTF().EvalBinOp(*this, Op, cast<Loc>(R),
3169 cast<NonLoc>(L));
3170 }
3171 else
3172 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003173 cast<NonLoc>(R), T);
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003174}
3175
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003176//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00003177// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00003178//===----------------------------------------------------------------------===//
3179
Ted Kremenekaa66a322008-01-16 21:46:15 +00003180#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003181static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003182static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003183
Ted Kremenekaa66a322008-01-16 21:46:15 +00003184namespace llvm {
3185template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003186struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00003187 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00003188
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003189 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
3190
3191 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00003192 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003193 GraphPrintCheckerState->isUndefDeref(N) ||
3194 GraphPrintCheckerState->isUndefStore(N) ||
3195 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00003196 GraphPrintCheckerState->isExplicitBadDivide(N) ||
3197 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00003198 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00003199 GraphPrintCheckerState->isBadCall(N) ||
3200 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003201 return "color=\"red\",style=\"filled\"";
3202
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00003203 if (GraphPrintCheckerState->isNoReturnCall(N))
3204 return "color=\"blue\",style=\"filled\"";
3205
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003206 return "";
3207 }
Ted Kremeneked4de312008-02-06 03:56:15 +00003208
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003209 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00003210 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003211
3212 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00003213 ProgramPoint Loc = N->getLocation();
3214
3215 switch (Loc.getKind()) {
3216 case ProgramPoint::BlockEntranceKind:
3217 Out << "Block Entrance: B"
3218 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
3219 break;
3220
3221 case ProgramPoint::BlockExitKind:
3222 assert (false);
3223 break;
3224
Ted Kremenekaa66a322008-01-16 21:46:15 +00003225 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00003226 if (isa<PostStmt>(Loc)) {
3227 const PostStmt& L = cast<PostStmt>(Loc);
3228 Stmt* S = L.getStmt();
3229 SourceLocation SLoc = S->getLocStart();
3230
3231 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
3232 llvm::raw_os_ostream OutS(Out);
3233 S->printPretty(OutS);
3234 OutS.flush();
3235
3236 if (SLoc.isFileID()) {
3237 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003238 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3239 << " col="
3240 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3241 << "\\l";
Ted Kremenek8c354752008-12-16 22:02:27 +00003242 }
3243
3244 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3245 Out << "\\|Implicit-Null Dereference.\\l";
3246 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3247 Out << "\\|Explicit-Null Dereference.\\l";
3248 else if (GraphPrintCheckerState->isUndefDeref(N))
3249 Out << "\\|Dereference of undefialied value.\\l";
3250 else if (GraphPrintCheckerState->isUndefStore(N))
3251 Out << "\\|Store to Undefined Loc.";
3252 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
3253 Out << "\\|Explicit divide-by zero or undefined value.";
3254 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
3255 Out << "\\|Implicit divide-by zero or undefined value.";
3256 else if (GraphPrintCheckerState->isUndefResult(N))
3257 Out << "\\|Result of operation is undefined.";
3258 else if (GraphPrintCheckerState->isNoReturnCall(N))
3259 Out << "\\|Call to function marked \"noreturn\".";
3260 else if (GraphPrintCheckerState->isBadCall(N))
3261 Out << "\\|Call to NULL/Undefined.";
3262 else if (GraphPrintCheckerState->isUndefArg(N))
3263 Out << "\\|Argument in call is undefined";
3264
3265 break;
3266 }
3267
Ted Kremenekaa66a322008-01-16 21:46:15 +00003268 const BlockEdge& E = cast<BlockEdge>(Loc);
3269 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3270 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00003271
3272 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00003273
3274 SourceLocation SLoc = T->getLocStart();
3275
Ted Kremenekb38911f2008-01-30 23:03:39 +00003276 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00003277
Ted Kremeneka95d3752008-09-13 05:16:45 +00003278 llvm::raw_os_ostream OutS(Out);
3279 E.getSrc()->printTerminator(OutS);
3280 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00003281
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003282 if (SLoc.isFileID()) {
3283 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003284 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3285 << " col="
3286 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003287 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00003288
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003289 if (isa<SwitchStmt>(T)) {
3290 Stmt* Label = E.getDst()->getLabel();
3291
3292 if (Label) {
3293 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3294 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003295 llvm::raw_os_ostream OutS(Out);
3296 C->getLHS()->printPretty(OutS);
3297 OutS.flush();
3298
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003299 if (Stmt* RHS = C->getRHS()) {
3300 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003301 RHS->printPretty(OutS);
3302 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003303 }
3304
3305 Out << ":";
3306 }
3307 else {
3308 assert (isa<DefaultStmt>(Label));
3309 Out << "\\ldefault:";
3310 }
3311 }
3312 else
3313 Out << "\\l(implicit) default:";
3314 }
3315 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00003316 // FIXME
3317 }
3318 else {
3319 Out << "\\lCondition: ";
3320 if (*E.getSrc()->succ_begin() == E.getDst())
3321 Out << "true";
3322 else
3323 Out << "false";
3324 }
3325
3326 Out << "\\l";
3327 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003328
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003329 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3330 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003331 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00003332 }
3333 }
3334
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00003335 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00003336
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003337 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
3338 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003339
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003340 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00003341 return Out.str();
3342 }
3343};
3344} // end llvm namespace
3345#endif
3346
Ted Kremenekffe0f432008-03-07 22:58:01 +00003347#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003348template <typename ITERATOR>
3349GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
3350
3351template <>
3352GRExprEngine::NodeTy*
3353GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
3354 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
3355 return I->first;
3356}
Ted Kremenekffe0f432008-03-07 22:58:01 +00003357#endif
3358
3359void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00003360#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00003361 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00003362 std::vector<NodeTy*> Src;
Ted Kremenek940abcc2009-03-11 01:41:22 +00003363
3364 // Flush any outstanding reports to make sure we cover all the nodes.
3365 // This does not cause them to get displayed.
3366 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3367 const_cast<BugType*>(*I)->FlushReports(BR);
3368
3369 // Iterate through the reports and get their nodes.
3370 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
3371 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) {
3372 const BugReportEquivClass& EQ = *I2;
3373 const BugReport &R = **EQ.begin();
3374 NodeTy *N = const_cast<NodeTy*>(R.getEndNode());
3375 if (N) Src.push_back(N);
3376 }
3377 }
Ted Kremenekcb612922008-04-18 19:23:43 +00003378
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003379 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00003380 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00003381 else {
3382 GraphPrintCheckerState = this;
3383 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00003384
Ted Kremenekffe0f432008-03-07 22:58:01 +00003385 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00003386
3387 GraphPrintCheckerState = NULL;
3388 GraphPrintSourceManager = NULL;
3389 }
3390#endif
3391}
3392
3393void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
3394#ifndef NDEBUG
3395 GraphPrintCheckerState = this;
3396 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003397
Ted Kremenekcf118d42009-02-04 23:49:09 +00003398 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremenek493d7a22008-03-11 18:25:33 +00003399
Ted Kremenekcf118d42009-02-04 23:49:09 +00003400 if (!TrimmedG.get())
Ted Kremenek493d7a22008-03-11 18:25:33 +00003401 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekcf118d42009-02-04 23:49:09 +00003402 else
Ted Kremenek493d7a22008-03-11 18:25:33 +00003403 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenekffe0f432008-03-07 22:58:01 +00003404
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003405 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003406 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00003407#endif
Ted Kremenekee985462008-01-16 18:18:48 +00003408}