blob: 8ee6723057a6153af0bb04be27eca77ae10f81dd [file] [log] [blame]
Ted Kremenek294fd0a2011-08-20 06:00:03 +00001//=-- ExprEngineCallAndReturn.cpp - Support for call/return -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines ExprEngine's support for calls and returns.
11//
12//===----------------------------------------------------------------------===//
13
Anna Zakse90d3f82012-08-09 00:21:33 +000014#define DEBUG_TYPE "ExprEngine"
15
Ted Kremenekd4aeb802012-07-02 20:21:52 +000016#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek294fd0a2011-08-20 06:00:03 +000017#include "clang/StaticAnalyzer/Core/CheckerManager.h"
18#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Jordan Rosef540c542012-07-26 21:39:41 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
Ted Kremenek294fd0a2011-08-20 06:00:03 +000020#include "clang/AST/DeclCXX.h"
Benjamin Kramer4a5f7242012-04-01 19:30:51 +000021#include "llvm/ADT/SmallSet.h"
Anna Zakse90d3f82012-08-09 00:21:33 +000022#include "llvm/ADT/Statistic.h"
Benjamin Kramer4a5f7242012-04-01 19:30:51 +000023#include "llvm/Support/SaveAndRestore.h"
Ted Kremenek294fd0a2011-08-20 06:00:03 +000024
Jordan Rose2f9c40a2012-07-31 18:22:40 +000025#define CXX_INLINING_ENABLED 1
26
Ted Kremenek294fd0a2011-08-20 06:00:03 +000027using namespace clang;
28using namespace ento;
29
Anna Zakse90d3f82012-08-09 00:21:33 +000030STATISTIC(NumOfDynamicDispatchPathSplits,
31 "The # of times we split the path due to imprecise dynamic dispatch info");
32
Ted Kremenek3070e132012-01-07 01:03:17 +000033void ExprEngine::processCallEnter(CallEnter CE, ExplodedNode *Pred) {
34 // Get the entry block in the CFG of the callee.
Ted Kremenek0849ade2012-01-12 19:25:46 +000035 const StackFrameContext *calleeCtx = CE.getCalleeContext();
36 const CFG *CalleeCFG = calleeCtx->getCFG();
Ted Kremenek3070e132012-01-07 01:03:17 +000037 const CFGBlock *Entry = &(CalleeCFG->getEntry());
38
39 // Validate the CFG.
40 assert(Entry->empty());
41 assert(Entry->succ_size() == 1);
42
43 // Get the solitary sucessor.
44 const CFGBlock *Succ = *(Entry->succ_begin());
45
46 // Construct an edge representing the starting location in the callee.
Ted Kremenek0849ade2012-01-12 19:25:46 +000047 BlockEdge Loc(Entry, Succ, calleeCtx);
Ted Kremenek3070e132012-01-07 01:03:17 +000048
Jordan Rosee54cfc72012-07-10 22:07:57 +000049 ProgramStateRef state = Pred->getState();
Ted Kremenek3070e132012-01-07 01:03:17 +000050
51 // Construct a new node and add it to the worklist.
52 bool isNew;
53 ExplodedNode *Node = G.getNode(Loc, state, false, &isNew);
54 Node->addPredecessor(Pred, G);
55 if (isNew)
56 Engine.getWorkList()->enqueue(Node);
Ted Kremenek294fd0a2011-08-20 06:00:03 +000057}
58
Anna Zaks0b3ade82012-04-20 21:59:08 +000059// Find the last statement on the path to the exploded node and the
60// corresponding Block.
61static std::pair<const Stmt*,
62 const CFGBlock*> getLastStmt(const ExplodedNode *Node) {
63 const Stmt *S = 0;
Anna Zaks0b3ade82012-04-20 21:59:08 +000064 const StackFrameContext *SF =
65 Node->getLocation().getLocationContext()->getCurrentStackFrame();
Jordan Rose888c90a2012-07-26 20:04:13 +000066
67 // Back up through the ExplodedGraph until we reach a statement node.
Ted Kremenek256ef642012-01-11 01:06:27 +000068 while (Node) {
69 const ProgramPoint &PP = Node->getLocation();
Jordan Rose888c90a2012-07-26 20:04:13 +000070
Ted Kremenek256ef642012-01-11 01:06:27 +000071 if (const StmtPoint *SP = dyn_cast<StmtPoint>(&PP)) {
Anna Zaks0b3ade82012-04-20 21:59:08 +000072 S = SP->getStmt();
Anna Zaks0b3ade82012-04-20 21:59:08 +000073 break;
Jordan Rose888c90a2012-07-26 20:04:13 +000074 } else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&PP)) {
75 S = CEE->getCalleeContext()->getCallSite();
76 if (S)
77 break;
78 // If we have an implicit call, we'll probably end up with a
79 // StmtPoint inside the callee, which is acceptable.
80 // (It's possible a function ONLY contains implicit calls -- such as an
81 // implicitly-generated destructor -- so we shouldn't just skip back to
82 // the CallEnter node and keep going.)
83 } else if (const CallEnter *CE = dyn_cast<CallEnter>(&PP)) {
84 // If we reached the CallEnter for this function, it has no statements.
85 if (CE->getCalleeContext() == SF)
86 break;
Ted Kremenek256ef642012-01-11 01:06:27 +000087 }
Jordan Rose888c90a2012-07-26 20:04:13 +000088
89 Node = *Node->pred_begin();
Ted Kremenek256ef642012-01-11 01:06:27 +000090 }
Jordan Rose888c90a2012-07-26 20:04:13 +000091
92 const CFGBlock *Blk = 0;
93 if (S) {
94 // Now, get the enclosing basic block.
95 while (Node && Node->pred_size() >=1 ) {
96 const ProgramPoint &PP = Node->getLocation();
97 if (isa<BlockEdge>(PP) &&
98 (PP.getLocationContext()->getCurrentStackFrame() == SF)) {
99 BlockEdge &EPP = cast<BlockEdge>(PP);
100 Blk = EPP.getDst();
101 break;
102 }
103 Node = *Node->pred_begin();
104 }
105 }
106
Anna Zaks0b3ade82012-04-20 21:59:08 +0000107 return std::pair<const Stmt*, const CFGBlock*>(S, Blk);
Ted Kremenek256ef642012-01-11 01:06:27 +0000108}
109
Anna Zaks0b3ade82012-04-20 21:59:08 +0000110/// The call exit is simulated with a sequence of nodes, which occur between
111/// CallExitBegin and CallExitEnd. The following operations occur between the
112/// two program points:
113/// 1. CallExitBegin (triggers the start of call exit sequence)
114/// 2. Bind the return value
115/// 3. Run Remove dead bindings to clean up the dead symbols from the callee.
116/// 4. CallExitEnd (switch to the caller context)
117/// 5. PostStmt<CallExpr>
118void ExprEngine::processCallExit(ExplodedNode *CEBNode) {
119 // Step 1 CEBNode was generated before the call.
120
121 const StackFrameContext *calleeCtx =
122 CEBNode->getLocationContext()->getCurrentStackFrame();
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000123
124 // The parent context might not be a stack frame, so make sure we
125 // look up the first enclosing stack frame.
126 const StackFrameContext *callerCtx =
127 calleeCtx->getParent()->getCurrentStackFrame();
128
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000129 const Stmt *CE = calleeCtx->getCallSite();
Anna Zaks0b3ade82012-04-20 21:59:08 +0000130 ProgramStateRef state = CEBNode->getState();
131 // Find the last statement in the function and the corresponding basic block.
132 const Stmt *LastSt = 0;
133 const CFGBlock *Blk = 0;
134 llvm::tie(LastSt, Blk) = getLastStmt(CEBNode);
135
Jordan Rose852aa0d2012-07-10 22:07:52 +0000136 // Step 2: generate node with bound return value: CEBNode -> BindedRetNode.
Anna Zaks0b3ade82012-04-20 21:59:08 +0000137
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000138 // If the callee returns an expression, bind its value to CallExpr.
Jordan Rose852aa0d2012-07-10 22:07:52 +0000139 if (CE) {
140 if (const ReturnStmt *RS = dyn_cast_or_null<ReturnStmt>(LastSt)) {
141 const LocationContext *LCtx = CEBNode->getLocationContext();
142 SVal V = state->getSVal(RS, LCtx);
Jordan Rose57c03362012-07-30 23:39:47 +0000143 state = state->BindExpr(CE, callerCtx, V);
Jordan Rose852aa0d2012-07-10 22:07:52 +0000144 }
Anna Zaks0b3ade82012-04-20 21:59:08 +0000145
Jordan Rose852aa0d2012-07-10 22:07:52 +0000146 // Bind the constructed object value to CXXConstructExpr.
147 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(CE)) {
148 loc::MemRegionVal This =
149 svalBuilder.getCXXThis(CCE->getConstructor()->getParent(), calleeCtx);
150 SVal ThisV = state->getSVal(This);
Anna Zaks0b3ade82012-04-20 21:59:08 +0000151
Jordan Rose852aa0d2012-07-10 22:07:52 +0000152 // Always bind the region to the CXXConstructExpr.
Jordan Rose57c03362012-07-30 23:39:47 +0000153 state = state->BindExpr(CCE, callerCtx, ThisV);
Jordan Rose852aa0d2012-07-10 22:07:52 +0000154 }
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000155 }
Anna Zaks0b3ade82012-04-20 21:59:08 +0000156
Anna Zaks0b3ade82012-04-20 21:59:08 +0000157 // Step 3: BindedRetNode -> CleanedNodes
158 // If we can find a statement and a block in the inlined function, run remove
159 // dead bindings before returning from the call. This is important to ensure
160 // that we report the issues such as leaks in the stack contexts in which
161 // they occurred.
162 ExplodedNodeSet CleanedNodes;
163 if (LastSt && Blk) {
Jordan Rose48b62472012-07-10 22:08:01 +0000164 static SimpleProgramPointTag retValBind("ExprEngine : Bind Return Value");
165 PostStmt Loc(LastSt, calleeCtx, &retValBind);
166 bool isNew;
167 ExplodedNode *BindedRetNode = G.getNode(Loc, state, false, &isNew);
168 BindedRetNode->addPredecessor(CEBNode, G);
169 if (!isNew)
170 return;
171
Anna Zaks0b3ade82012-04-20 21:59:08 +0000172 NodeBuilderContext Ctx(getCoreEngine(), Blk, BindedRetNode);
173 currentBuilderContext = &Ctx;
174 // Here, we call the Symbol Reaper with 0 statement and caller location
175 // context, telling it to clean up everything in the callee's context
176 // (and it's children). We use LastStmt as a diagnostic statement, which
177 // which the PreStmtPurge Dead point will be associated.
178 removeDead(BindedRetNode, CleanedNodes, 0, callerCtx, LastSt,
179 ProgramPoint::PostStmtPurgeDeadSymbolsKind);
180 currentBuilderContext = 0;
Anna Zaks144e52b2012-06-01 23:48:40 +0000181 } else {
182 CleanedNodes.Add(CEBNode);
Anna Zaks0b3ade82012-04-20 21:59:08 +0000183 }
184
185 for (ExplodedNodeSet::iterator I = CleanedNodes.begin(),
186 E = CleanedNodes.end(); I != E; ++I) {
187
188 // Step 4: Generate the CallExit and leave the callee's context.
189 // CleanedNodes -> CEENode
Jordan Rose852aa0d2012-07-10 22:07:52 +0000190 CallExitEnd Loc(calleeCtx, callerCtx);
Anna Zaks0b3ade82012-04-20 21:59:08 +0000191 bool isNew;
Jordan Rose48b62472012-07-10 22:08:01 +0000192 ProgramStateRef CEEState = (*I == CEBNode) ? state : (*I)->getState();
193 ExplodedNode *CEENode = G.getNode(Loc, CEEState, false, &isNew);
Anna Zaks0b3ade82012-04-20 21:59:08 +0000194 CEENode->addPredecessor(*I, G);
195 if (!isNew)
196 return;
197
198 // Step 5: Perform the post-condition check of the CallExpr and enqueue the
199 // result onto the work list.
200 // CEENode -> Dst -> WorkList
Anna Zaks0b3ade82012-04-20 21:59:08 +0000201 NodeBuilderContext Ctx(Engine, calleeCtx->getCallSiteBlock(), CEENode);
202 SaveAndRestore<const NodeBuilderContext*> NBCSave(currentBuilderContext,
203 &Ctx);
204 SaveAndRestore<unsigned> CBISave(currentStmtIdx, calleeCtx->getIndex());
205
Jordan Rose57c03362012-07-30 23:39:47 +0000206 CallEventManager &CEMgr = getStateManager().getCallEventManager();
207 CallEventRef<> Call = CEMgr.getCaller(calleeCtx, CEEState);
208
209 ExplodedNodeSet DstPostCall;
210 getCheckerManager().runCheckersForPostCall(DstPostCall, CEENode, *Call,
211 *this, true);
212
213 ExplodedNodeSet Dst;
214 if (isa<ObjCMethodCall>(Call)) {
215 getCheckerManager().runCheckersForPostObjCMessage(Dst, DstPostCall,
216 cast<ObjCMethodCall>(*Call),
217 *this, true);
218 } else if (CE) {
219 getCheckerManager().runCheckersForPostStmt(Dst, DstPostCall, CE,
220 *this, true);
221 } else {
222 Dst.insert(DstPostCall);
223 }
Anna Zaks0b3ade82012-04-20 21:59:08 +0000224
225 // Enqueue the next element in the block.
226 for (ExplodedNodeSet::iterator PSI = Dst.begin(), PSE = Dst.end();
227 PSI != PSE; ++PSI) {
228 Engine.getWorkList()->enqueue(*PSI, calleeCtx->getCallSiteBlock(),
229 calleeCtx->getIndex()+1);
230 }
Ted Kremenek242384d2012-01-07 00:10:49 +0000231 }
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000232}
233
Ted Kremenek0849ade2012-01-12 19:25:46 +0000234static unsigned getNumberStackFrames(const LocationContext *LCtx) {
235 unsigned count = 0;
236 while (LCtx) {
237 if (isa<StackFrameContext>(LCtx))
238 ++count;
239 LCtx = LCtx->getParent();
240 }
241 return count;
242}
243
Anna Zaks6cc09692012-03-13 22:15:58 +0000244// Determine if we should inline the call.
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000245bool ExprEngine::shouldInlineDecl(const Decl *D, ExplodedNode *Pred) {
246 AnalysisDeclContext *CalleeADC = AMgr.getAnalysisDeclContext(D);
Anna Zaks6cc09692012-03-13 22:15:58 +0000247 const CFG *CalleeCFG = CalleeADC->getCFG();
248
Ted Kremenek01561d12012-04-17 01:36:03 +0000249 // It is possible that the CFG cannot be constructed.
250 // Be safe, and check if the CalleeCFG is valid.
251 if (!CalleeCFG)
252 return false;
253
Anna Zaks6cc09692012-03-13 22:15:58 +0000254 if (getNumberStackFrames(Pred->getLocationContext())
255 == AMgr.InlineMaxStackDepth)
256 return false;
257
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000258 if (Engine.FunctionSummaries->hasReachedMaxBlockCount(D))
Anna Zaks3bbd8cd2012-03-30 05:48:10 +0000259 return false;
260
Anna Zaks6cc09692012-03-13 22:15:58 +0000261 if (CalleeCFG->getNumBlockIDs() > AMgr.InlineMaxFunctionSize)
262 return false;
263
Ted Kremenek10f77ad2012-06-22 23:55:50 +0000264 // Do not inline variadic calls (for now).
265 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
266 if (BD->isVariadic())
267 return false;
Anna Zaks5903a372012-03-27 20:02:53 +0000268 }
Ted Kremenek10f77ad2012-06-22 23:55:50 +0000269 else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
270 if (FD->isVariadic())
271 return false;
272 }
Anna Zaks5903a372012-03-27 20:02:53 +0000273
Ted Kremenekd4aeb802012-07-02 20:21:52 +0000274 // It is possible that the live variables analysis cannot be
275 // run. If so, bail out.
276 if (!CalleeADC->getAnalysis<RelaxedLiveVariables>())
277 return false;
278
Ted Kremenek10f77ad2012-06-22 23:55:50 +0000279 return true;
Anna Zaks5903a372012-03-27 20:02:53 +0000280}
281
Anna Zakse90d3f82012-08-09 00:21:33 +0000282/// The GDM component containing the dynamic dispatch bifurcation info. When
283/// the exact type of the receiver is not known, we want to explore both paths -
284/// one on which we do inline it and the other one on which we don't. This is
285/// done to ensure we do not drop coverage.
286/// This is the map from the receiver region to a bool, specifying either we
287/// consider this region's information precise or not along the given path.
288namespace clang {
289namespace ento {
Anna Zaks6960f6e2012-08-09 21:02:41 +0000290enum DynamicDispatchMode { DynamicDispatchModeInlined = 1,
291 DynamicDispatchModeConservative };
292
Anna Zakse90d3f82012-08-09 00:21:33 +0000293struct DynamicDispatchBifurcationMap {};
294typedef llvm::ImmutableMap<const MemRegion*,
Anna Zaks6960f6e2012-08-09 21:02:41 +0000295 unsigned int> DynamicDispatchBifur;
Anna Zakse90d3f82012-08-09 00:21:33 +0000296template<> struct ProgramStateTrait<DynamicDispatchBifurcationMap>
297 : public ProgramStatePartialTrait<DynamicDispatchBifur> {
298 static void *GDMIndex() { static int index; return &index; }
299};
Anna Zaks6960f6e2012-08-09 21:02:41 +0000300
Anna Zakse90d3f82012-08-09 00:21:33 +0000301}}
Anna Zaks5903a372012-03-27 20:02:53 +0000302
Anna Zakse90d3f82012-08-09 00:21:33 +0000303bool ExprEngine::inlineCall(const CallEvent &Call, const Decl *D,
304 NodeBuilder &Bldr, ExplodedNode *Pred,
305 ProgramStateRef State) {
306 assert(D);
Jordan Roseee158bc2012-07-09 16:54:49 +0000307
Jordan Rosec36b30c2012-07-12 00:16:25 +0000308 const LocationContext *CurLC = Pred->getLocationContext();
309 const StackFrameContext *CallerSFC = CurLC->getCurrentStackFrame();
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000310 const LocationContext *ParentOfCallee = 0;
Jordan Rose69f87c92012-07-02 19:28:09 +0000311
Jordan Roseef158312012-07-31 01:07:55 +0000312 // FIXME: Refactor this check into a hypothetical CallEvent::canInline.
Jordan Rose69f87c92012-07-02 19:28:09 +0000313 switch (Call.getKind()) {
314 case CE_Function:
Jordan Rose2f9c40a2012-07-31 18:22:40 +0000315 break;
Jordan Rose69f87c92012-07-02 19:28:09 +0000316 case CE_CXXMember:
Jordan Rosee54cfc72012-07-10 22:07:57 +0000317 case CE_CXXMemberOperator:
Jordan Rose2f9c40a2012-07-31 18:22:40 +0000318 if (!CXX_INLINING_ENABLED)
319 return false;
Jordan Rose69f87c92012-07-02 19:28:09 +0000320 break;
Jordan Roseef158312012-07-31 01:07:55 +0000321 case CE_CXXConstructor: {
Jordan Rose2f9c40a2012-07-31 18:22:40 +0000322 if (!CXX_INLINING_ENABLED)
323 return false;
324
Jordan Roseef158312012-07-31 01:07:55 +0000325 // Only inline constructors and destructors if we built the CFGs for them
326 // properly.
327 const AnalysisDeclContext *ADC = CallerSFC->getAnalysisDeclContext();
328 if (!ADC->getCFGBuildOptions().AddImplicitDtors ||
329 !ADC->getCFGBuildOptions().AddInitializers)
330 return false;
331
332 const CXXConstructorCall &Ctor = cast<CXXConstructorCall>(Call);
333
334 // FIXME: We don't handle constructors or destructors for arrays properly.
335 const MemRegion *Target = Ctor.getCXXThisVal().getAsRegion();
336 if (Target && isa<ElementRegion>(Target))
337 return false;
338
339 // FIXME: This is a hack. We don't handle temporary destructors
340 // right now, so we shouldn't inline their constructors.
341 const CXXConstructExpr *CtorExpr = Ctor.getOriginExpr();
342 if (CtorExpr->getConstructionKind() == CXXConstructExpr::CK_Complete)
343 if (!Target || !isa<DeclRegion>(Target))
344 return false;
345
346 break;
347 }
Jordan Roseda5fc532012-07-26 20:04:00 +0000348 case CE_CXXDestructor: {
Jordan Rose2f9c40a2012-07-31 18:22:40 +0000349 if (!CXX_INLINING_ENABLED)
350 return false;
351
Jordan Roseda5fc532012-07-26 20:04:00 +0000352 // Only inline constructors and destructors if we built the CFGs for them
353 // properly.
354 const AnalysisDeclContext *ADC = CallerSFC->getAnalysisDeclContext();
355 if (!ADC->getCFGBuildOptions().AddImplicitDtors ||
356 !ADC->getCFGBuildOptions().AddInitializers)
357 return false;
Jordan Rose3a0a9e32012-07-26 20:04:21 +0000358
Jordan Roseef158312012-07-31 01:07:55 +0000359 const CXXDestructorCall &Dtor = cast<CXXDestructorCall>(Call);
360
Jordan Rosee460c462012-07-26 20:04:25 +0000361 // FIXME: We don't handle constructors or destructors for arrays properly.
Jordan Roseef158312012-07-31 01:07:55 +0000362 const MemRegion *Target = Dtor.getCXXThisVal().getAsRegion();
Jordan Rosee460c462012-07-26 20:04:25 +0000363 if (Target && isa<ElementRegion>(Target))
364 return false;
365
Jordan Roseda5fc532012-07-26 20:04:00 +0000366 break;
367 }
Jordan Rose70cbf3c2012-07-02 22:21:47 +0000368 case CE_CXXAllocator:
Jordan Rose2f9c40a2012-07-31 18:22:40 +0000369 if (!CXX_INLINING_ENABLED)
370 return false;
371
Jordan Rose70cbf3c2012-07-02 22:21:47 +0000372 // Do not inline allocators until we model deallocators.
373 // This is unfortunate, but basically necessary for smart pointers and such.
374 return false;
Jordan Rose69f87c92012-07-02 19:28:09 +0000375 case CE_Block: {
376 const BlockDataRegion *BR = cast<BlockCall>(Call).getBlockRegion();
Jordan Roseee158bc2012-07-09 16:54:49 +0000377 assert(BR && "If we have the block definition we should have its region");
Jordan Rose69f87c92012-07-02 19:28:09 +0000378 AnalysisDeclContext *BlockCtx = AMgr.getAnalysisDeclContext(D);
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000379 ParentOfCallee = BlockCtx->getBlockInvocationContext(CallerSFC,
Jordan Rose69f87c92012-07-02 19:28:09 +0000380 cast<BlockDecl>(D),
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000381 BR);
Jordan Rose69f87c92012-07-02 19:28:09 +0000382 break;
383 }
384 case CE_ObjCMessage:
Anna Zakse90d3f82012-08-09 00:21:33 +0000385 if (!(getAnalysisManager().IPAMode == DynamicDispatch ||
386 getAnalysisManager().IPAMode == DynamicDispatchBifurcate))
Anna Zakse13056a2012-07-30 20:31:18 +0000387 return false;
Anna Zaks9dc51672012-07-26 00:27:51 +0000388 break;
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000389 }
Jordan Roseee158bc2012-07-09 16:54:49 +0000390
391 if (!shouldInlineDecl(D, Pred))
Ted Kremenek256ef642012-01-11 01:06:27 +0000392 return false;
393
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000394 if (!ParentOfCallee)
395 ParentOfCallee = CallerSFC;
Anna Zaks8235f9c2012-03-02 19:05:03 +0000396
Jordan Rose852aa0d2012-07-10 22:07:52 +0000397 // This may be NULL, but that's fine.
Jordan Rose69f87c92012-07-02 19:28:09 +0000398 const Expr *CallE = Call.getOriginExpr();
Jordan Rose69f87c92012-07-02 19:28:09 +0000399
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000400 // Construct a new stack frame for the callee.
401 AnalysisDeclContext *CalleeADC = AMgr.getAnalysisDeclContext(D);
402 const StackFrameContext *CalleeSFC =
Jordan Rose69f87c92012-07-02 19:28:09 +0000403 CalleeADC->getStackFrame(ParentOfCallee, CallE,
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000404 currentBuilderContext->getBlock(),
405 currentStmtIdx);
406
Jordan Rosec36b30c2012-07-12 00:16:25 +0000407 CallEnter Loc(CallE, CalleeSFC, CurLC);
Jordan Rosee54cfc72012-07-10 22:07:57 +0000408
409 // Construct a new state which contains the mapping from actual to
410 // formal arguments.
Anna Zakse90d3f82012-08-09 00:21:33 +0000411 State = State->enterStackFrame(Call, CalleeSFC);
Jordan Rosee54cfc72012-07-10 22:07:57 +0000412
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000413 bool isNew;
Jordan Rosee54cfc72012-07-10 22:07:57 +0000414 if (ExplodedNode *N = G.getNode(Loc, State, false, &isNew)) {
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000415 N->addPredecessor(Pred, G);
416 if (isNew)
417 Engine.getWorkList()->enqueue(N);
Ted Kremenek256ef642012-01-11 01:06:27 +0000418 }
Anna Zakse90d3f82012-08-09 00:21:33 +0000419
420 // If we decided to inline the call, the successor has been manually
421 // added onto the work list so remove it from the node builder.
422 Bldr.takeNodes(Pred);
423
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000424 return true;
Ted Kremenek256ef642012-01-11 01:06:27 +0000425}
426
Anna Zakse81ce252012-07-19 23:38:13 +0000427static ProgramStateRef getInlineFailedState(ProgramStateRef State,
Jordan Rose69f87c92012-07-02 19:28:09 +0000428 const Stmt *CallE) {
Anna Zakse81ce252012-07-19 23:38:13 +0000429 void *ReplayState = State->get<ReplayWithoutInlining>();
Anna Zaks5903a372012-03-27 20:02:53 +0000430 if (!ReplayState)
431 return 0;
Jordan Rose28038f32012-07-10 22:07:42 +0000432
433 assert(ReplayState == (const void*)CallE && "Backtracked to the wrong call.");
434 (void)CallE;
435
Anna Zakse81ce252012-07-19 23:38:13 +0000436 return State->remove<ReplayWithoutInlining>();
Ted Kremenek10520d72012-02-09 21:59:52 +0000437}
438
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000439void ExprEngine::VisitCallExpr(const CallExpr *CE, ExplodedNode *Pred,
440 ExplodedNodeSet &dst) {
441 // Perform the previsit of the CallExpr.
442 ExplodedNodeSet dstPreVisit;
443 getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, CE, *this);
Anna Zaks5903a372012-03-27 20:02:53 +0000444
Jordan Rosed563d3f2012-07-30 20:22:09 +0000445 // Get the call in its initial state. We use this as a template to perform
446 // all the checks.
447 CallEventManager &CEMgr = getStateManager().getCallEventManager();
Jordan Rose645baee2012-08-13 23:46:05 +0000448 CallEventRef<> CallTemplate
Jordan Rosed563d3f2012-07-30 20:22:09 +0000449 = CEMgr.getSimpleCall(CE, Pred->getState(), Pred->getLocationContext());
Anna Zaks5903a372012-03-27 20:02:53 +0000450
Jordan Rose69f87c92012-07-02 19:28:09 +0000451 // Evaluate the function call. We try each of the checkers
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000452 // to see if the can evaluate the function call.
453 ExplodedNodeSet dstCallEvaluated;
Jordan Rose69f87c92012-07-02 19:28:09 +0000454 for (ExplodedNodeSet::iterator I = dstPreVisit.begin(), E = dstPreVisit.end();
455 I != E; ++I) {
Jordan Rosed563d3f2012-07-30 20:22:09 +0000456 evalCall(dstCallEvaluated, *I, *CallTemplate);
Jordan Rose69f87c92012-07-02 19:28:09 +0000457 }
458
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000459 // Finally, perform the post-condition check of the CallExpr and store
460 // the created nodes in 'Dst'.
Jordan Rose69f87c92012-07-02 19:28:09 +0000461 // Note that if the call was inlined, dstCallEvaluated will be empty.
462 // The post-CallExpr check will occur in processCallExit.
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000463 getCheckerManager().runCheckersForPostStmt(dst, dstCallEvaluated, CE,
464 *this);
465}
466
Jordan Rose69f87c92012-07-02 19:28:09 +0000467void ExprEngine::evalCall(ExplodedNodeSet &Dst, ExplodedNode *Pred,
Jordan Rose645baee2012-08-13 23:46:05 +0000468 const CallEvent &Call) {
Jordan Rosed563d3f2012-07-30 20:22:09 +0000469 // WARNING: At this time, the state attached to 'Call' may be older than the
470 // state in 'Pred'. This is a minor optimization since CheckerManager will
471 // use an updated CallEvent instance when calling checkers, but if 'Call' is
472 // ever used directly in this function all callers should be updated to pass
473 // the most recent state. (It is probably not worth doing the work here since
474 // for some callers this will not be necessary.)
475
Jordan Rose96479da2012-07-02 19:28:16 +0000476 // Run any pre-call checks using the generic call interface.
477 ExplodedNodeSet dstPreVisit;
478 getCheckerManager().runCheckersForPreCall(dstPreVisit, Pred, Call, *this);
479
480 // Actually evaluate the function call. We try each of the checkers
481 // to see if the can evaluate the function call, and get a callback at
482 // defaultEvalCall if all of them fail.
483 ExplodedNodeSet dstCallEvaluated;
484 getCheckerManager().runCheckersForEvalCall(dstCallEvaluated, dstPreVisit,
485 Call, *this);
486
487 // Finally, run any post-call checks.
488 getCheckerManager().runCheckersForPostCall(Dst, dstCallEvaluated,
489 Call, *this);
Jordan Rose69f87c92012-07-02 19:28:09 +0000490}
491
Anna Zakse81ce252012-07-19 23:38:13 +0000492ProgramStateRef ExprEngine::bindReturnValue(const CallEvent &Call,
493 const LocationContext *LCtx,
494 ProgramStateRef State) {
495 const Expr *E = Call.getOriginExpr();
496 if (!E)
497 return State;
498
499 // Some method families have known return values.
500 if (const ObjCMethodCall *Msg = dyn_cast<ObjCMethodCall>(&Call)) {
501 switch (Msg->getMethodFamily()) {
502 default:
503 break;
504 case OMF_autorelease:
505 case OMF_retain:
506 case OMF_self: {
507 // These methods return their receivers.
508 return State->BindExpr(E, LCtx, Msg->getReceiverSVal());
Anna Zakse81ce252012-07-19 23:38:13 +0000509 }
510 }
Jordan Rosee460c462012-07-26 20:04:25 +0000511 } else if (const CXXConstructorCall *C = dyn_cast<CXXConstructorCall>(&Call)){
512 return State->BindExpr(E, LCtx, C->getCXXThisVal());
Anna Zakse81ce252012-07-19 23:38:13 +0000513 }
514
515 // Conjure a symbol if the return value is unknown.
516 QualType ResultTy = Call.getResultType();
517 SValBuilder &SVB = getSValBuilder();
518 unsigned Count = currentBuilderContext->getCurrentBlockCount();
519 SVal R = SVB.getConjuredSymbolVal(0, E, LCtx, ResultTy, Count);
520 return State->BindExpr(E, LCtx, R);
521}
522
Anna Zakse90d3f82012-08-09 00:21:33 +0000523// Conservatively evaluate call by invalidating regions and binding
524// a conjured return value.
525void ExprEngine::conservativeEvalCall(const CallEvent &Call, NodeBuilder &Bldr,
526 ExplodedNode *Pred, ProgramStateRef State) {
527 unsigned Count = currentBuilderContext->getCurrentBlockCount();
528 State = Call.invalidateRegions(Count, State);
529 State = bindReturnValue(Call, Pred->getLocationContext(), State);
530
531 // And make the result node.
532 Bldr.generateNode(Call.getProgramPoint(), State, Pred);
533}
534
Anna Zakse81ce252012-07-19 23:38:13 +0000535void ExprEngine::defaultEvalCall(NodeBuilder &Bldr, ExplodedNode *Pred,
Jordan Rosed563d3f2012-07-30 20:22:09 +0000536 const CallEvent &CallTemplate) {
537 // Make sure we have the most recent state attached to the call.
538 ProgramStateRef State = Pred->getState();
539 CallEventRef<> Call = CallTemplate.cloneWithState(State);
Anna Zakse81ce252012-07-19 23:38:13 +0000540
Anna Zaks5960f4a2012-08-09 18:43:00 +0000541 if (!getAnalysisManager().shouldInlineCall()) {
542 conservativeEvalCall(*Call, Bldr, Pred, State);
543 return;
544 }
Jordan Rose69f87c92012-07-02 19:28:09 +0000545 // Try to inline the call.
Jordan Rose28038f32012-07-10 22:07:42 +0000546 // The origin expression here is just used as a kind of checksum;
Jordan Rosed563d3f2012-07-30 20:22:09 +0000547 // this should still be safe even for CallEvents that don't come from exprs.
548 const Expr *E = Call->getOriginExpr();
549 ProgramStateRef InlinedFailedState = getInlineFailedState(State, E);
550
551 if (InlinedFailedState) {
552 // If we already tried once and failed, make sure we don't retry later.
553 State = InlinedFailedState;
Anna Zaks5960f4a2012-08-09 18:43:00 +0000554 } else {
Anna Zakse90d3f82012-08-09 00:21:33 +0000555 RuntimeDefinition RD = Call->getRuntimeDefinition();
Anna Zaksfc05dec2012-08-09 02:57:02 +0000556 const Decl *D = RD.getDecl();
Anna Zakse90d3f82012-08-09 00:21:33 +0000557 if (D) {
558 // Explore with and without inlining the call.
Anna Zaks5960f4a2012-08-09 18:43:00 +0000559 if (RD.mayHaveOtherDefinitions() &&
Anna Zakse90d3f82012-08-09 00:21:33 +0000560 getAnalysisManager().IPAMode == DynamicDispatchBifurcate) {
Anna Zaks5960f4a2012-08-09 18:43:00 +0000561 BifurcateCall(RD.getDispatchRegion(), *Call, D, Bldr, Pred);
Anna Zakse90d3f82012-08-09 00:21:33 +0000562 return;
Anna Zakse90d3f82012-08-09 00:21:33 +0000563 }
Anna Zaks5960f4a2012-08-09 18:43:00 +0000564 // We are not bifurcating and we do have a Decl, so just inline.
565 if (inlineCall(*Call, D, Bldr, Pred, State))
566 return;
Anna Zakse90d3f82012-08-09 00:21:33 +0000567 }
Anna Zakse81ce252012-07-19 23:38:13 +0000568 }
Jordan Rose69f87c92012-07-02 19:28:09 +0000569
570 // If we can't inline it, handle the return value and invalidate the regions.
Anna Zakse90d3f82012-08-09 00:21:33 +0000571 conservativeEvalCall(*Call, Bldr, Pred, State);
Jordan Rose69f87c92012-07-02 19:28:09 +0000572}
573
Anna Zakse90d3f82012-08-09 00:21:33 +0000574void ExprEngine::BifurcateCall(const MemRegion *BifurReg,
575 const CallEvent &Call, const Decl *D,
576 NodeBuilder &Bldr, ExplodedNode *Pred) {
577 assert(BifurReg);
578
579 // Check if we've performed the split already - note, we only want
580 // to split the path once per memory region.
581 ProgramStateRef State = Pred->getState();
Anna Zaks6960f6e2012-08-09 21:02:41 +0000582 const unsigned int *BState =
583 State->get<DynamicDispatchBifurcationMap>(BifurReg);
Anna Zaks5960f4a2012-08-09 18:43:00 +0000584 if (BState) {
585 // If we are on "inline path", keep inlining if possible.
Anna Zaks6960f6e2012-08-09 21:02:41 +0000586 if (*BState == DynamicDispatchModeInlined)
Anna Zaks5960f4a2012-08-09 18:43:00 +0000587 if (inlineCall(Call, D, Bldr, Pred, State))
588 return;
589 // If inline failed, or we are on the path where we assume we
590 // don't have enough info about the receiver to inline, conjure the
591 // return value and invalidate the regions.
592 conservativeEvalCall(Call, Bldr, Pred, State);
593 return;
Anna Zakse90d3f82012-08-09 00:21:33 +0000594 }
595
596 // If we got here, this is the first time we process a message to this
597 // region, so split the path.
598 ProgramStateRef IState =
Anna Zaks6960f6e2012-08-09 21:02:41 +0000599 State->set<DynamicDispatchBifurcationMap>(BifurReg,
600 DynamicDispatchModeInlined);
Anna Zakse90d3f82012-08-09 00:21:33 +0000601 inlineCall(Call, D, Bldr, Pred, IState);
602
603 ProgramStateRef NoIState =
Anna Zaks6960f6e2012-08-09 21:02:41 +0000604 State->set<DynamicDispatchBifurcationMap>(BifurReg,
605 DynamicDispatchModeConservative);
Anna Zakse90d3f82012-08-09 00:21:33 +0000606 conservativeEvalCall(Call, Bldr, Pred, NoIState);
607
608 NumOfDynamicDispatchPathSplits++;
609 return;
610}
611
612
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000613void ExprEngine::VisitReturnStmt(const ReturnStmt *RS, ExplodedNode *Pred,
614 ExplodedNodeSet &Dst) {
Ted Kremenek256ef642012-01-11 01:06:27 +0000615
616 ExplodedNodeSet dstPreVisit;
617 getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, RS, *this);
618
619 StmtNodeBuilder B(dstPreVisit, Dst, *currentBuilderContext);
620
621 if (RS->getRetValue()) {
622 for (ExplodedNodeSet::iterator it = dstPreVisit.begin(),
623 ei = dstPreVisit.end(); it != ei; ++it) {
624 B.generateNode(RS, *it, (*it)->getState());
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000625 }
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000626 }
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000627}