blob: 4e4f103ea3467cfcca71bc78787d8e9c28f33c6f [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"
Jordan Rose6fe4dfb2012-08-27 18:39:22 +000021#include "clang/AST/ParentMap.h"
Benjamin Kramer4a5f7242012-04-01 19:30:51 +000022#include "llvm/ADT/SmallSet.h"
Anna Zakse90d3f82012-08-09 00:21:33 +000023#include "llvm/ADT/Statistic.h"
Benjamin Kramer4a5f7242012-04-01 19:30:51 +000024#include "llvm/Support/SaveAndRestore.h"
Ted Kremenek294fd0a2011-08-20 06:00:03 +000025
26using namespace clang;
27using namespace ento;
28
Anna Zakse90d3f82012-08-09 00:21:33 +000029STATISTIC(NumOfDynamicDispatchPathSplits,
30 "The # of times we split the path due to imprecise dynamic dispatch info");
31
Anna Zaks210f5a22012-08-27 18:38:32 +000032STATISTIC(NumInlinedCalls,
33 "The # of times we inlined a call");
34
Ted Kremenek3070e132012-01-07 01:03:17 +000035void ExprEngine::processCallEnter(CallEnter CE, ExplodedNode *Pred) {
36 // Get the entry block in the CFG of the callee.
Ted Kremenek0849ade2012-01-12 19:25:46 +000037 const StackFrameContext *calleeCtx = CE.getCalleeContext();
38 const CFG *CalleeCFG = calleeCtx->getCFG();
Ted Kremenek3070e132012-01-07 01:03:17 +000039 const CFGBlock *Entry = &(CalleeCFG->getEntry());
40
41 // Validate the CFG.
42 assert(Entry->empty());
43 assert(Entry->succ_size() == 1);
44
45 // Get the solitary sucessor.
46 const CFGBlock *Succ = *(Entry->succ_begin());
47
48 // Construct an edge representing the starting location in the callee.
Ted Kremenek0849ade2012-01-12 19:25:46 +000049 BlockEdge Loc(Entry, Succ, calleeCtx);
Ted Kremenek3070e132012-01-07 01:03:17 +000050
Jordan Rosee54cfc72012-07-10 22:07:57 +000051 ProgramStateRef state = Pred->getState();
Ted Kremenek3070e132012-01-07 01:03:17 +000052
53 // Construct a new node and add it to the worklist.
54 bool isNew;
55 ExplodedNode *Node = G.getNode(Loc, state, false, &isNew);
56 Node->addPredecessor(Pred, G);
57 if (isNew)
58 Engine.getWorkList()->enqueue(Node);
Ted Kremenek294fd0a2011-08-20 06:00:03 +000059}
60
Anna Zaks0b3ade82012-04-20 21:59:08 +000061// Find the last statement on the path to the exploded node and the
62// corresponding Block.
63static std::pair<const Stmt*,
64 const CFGBlock*> getLastStmt(const ExplodedNode *Node) {
65 const Stmt *S = 0;
Anna Zaks0b3ade82012-04-20 21:59:08 +000066 const StackFrameContext *SF =
67 Node->getLocation().getLocationContext()->getCurrentStackFrame();
Jordan Rose888c90a2012-07-26 20:04:13 +000068
69 // Back up through the ExplodedGraph until we reach a statement node.
Ted Kremenek256ef642012-01-11 01:06:27 +000070 while (Node) {
71 const ProgramPoint &PP = Node->getLocation();
Jordan Rose888c90a2012-07-26 20:04:13 +000072
Ted Kremenek256ef642012-01-11 01:06:27 +000073 if (const StmtPoint *SP = dyn_cast<StmtPoint>(&PP)) {
Anna Zaks0b3ade82012-04-20 21:59:08 +000074 S = SP->getStmt();
Anna Zaks0b3ade82012-04-20 21:59:08 +000075 break;
Jordan Rose888c90a2012-07-26 20:04:13 +000076 } else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&PP)) {
77 S = CEE->getCalleeContext()->getCallSite();
78 if (S)
79 break;
80 // If we have an implicit call, we'll probably end up with a
81 // StmtPoint inside the callee, which is acceptable.
82 // (It's possible a function ONLY contains implicit calls -- such as an
83 // implicitly-generated destructor -- so we shouldn't just skip back to
84 // the CallEnter node and keep going.)
85 } else if (const CallEnter *CE = dyn_cast<CallEnter>(&PP)) {
86 // If we reached the CallEnter for this function, it has no statements.
87 if (CE->getCalleeContext() == SF)
88 break;
Ted Kremenek256ef642012-01-11 01:06:27 +000089 }
Jordan Rose888c90a2012-07-26 20:04:13 +000090
91 Node = *Node->pred_begin();
Ted Kremenek256ef642012-01-11 01:06:27 +000092 }
Jordan Rose888c90a2012-07-26 20:04:13 +000093
94 const CFGBlock *Blk = 0;
95 if (S) {
96 // Now, get the enclosing basic block.
97 while (Node && Node->pred_size() >=1 ) {
98 const ProgramPoint &PP = Node->getLocation();
99 if (isa<BlockEdge>(PP) &&
100 (PP.getLocationContext()->getCurrentStackFrame() == SF)) {
101 BlockEdge &EPP = cast<BlockEdge>(PP);
102 Blk = EPP.getDst();
103 break;
104 }
105 Node = *Node->pred_begin();
106 }
107 }
108
Anna Zaks0b3ade82012-04-20 21:59:08 +0000109 return std::pair<const Stmt*, const CFGBlock*>(S, Blk);
Ted Kremenek256ef642012-01-11 01:06:27 +0000110}
111
Anna Zaks0b3ade82012-04-20 21:59:08 +0000112/// The call exit is simulated with a sequence of nodes, which occur between
113/// CallExitBegin and CallExitEnd. The following operations occur between the
114/// two program points:
115/// 1. CallExitBegin (triggers the start of call exit sequence)
116/// 2. Bind the return value
117/// 3. Run Remove dead bindings to clean up the dead symbols from the callee.
118/// 4. CallExitEnd (switch to the caller context)
119/// 5. PostStmt<CallExpr>
120void ExprEngine::processCallExit(ExplodedNode *CEBNode) {
121 // Step 1 CEBNode was generated before the call.
122
123 const StackFrameContext *calleeCtx =
124 CEBNode->getLocationContext()->getCurrentStackFrame();
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000125
126 // The parent context might not be a stack frame, so make sure we
127 // look up the first enclosing stack frame.
128 const StackFrameContext *callerCtx =
129 calleeCtx->getParent()->getCurrentStackFrame();
130
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000131 const Stmt *CE = calleeCtx->getCallSite();
Anna Zaks0b3ade82012-04-20 21:59:08 +0000132 ProgramStateRef state = CEBNode->getState();
133 // Find the last statement in the function and the corresponding basic block.
134 const Stmt *LastSt = 0;
135 const CFGBlock *Blk = 0;
136 llvm::tie(LastSt, Blk) = getLastStmt(CEBNode);
137
Jordan Rose852aa0d2012-07-10 22:07:52 +0000138 // Step 2: generate node with bound return value: CEBNode -> BindedRetNode.
Anna Zaks0b3ade82012-04-20 21:59:08 +0000139
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000140 // If the callee returns an expression, bind its value to CallExpr.
Jordan Rose852aa0d2012-07-10 22:07:52 +0000141 if (CE) {
142 if (const ReturnStmt *RS = dyn_cast_or_null<ReturnStmt>(LastSt)) {
143 const LocationContext *LCtx = CEBNode->getLocationContext();
144 SVal V = state->getSVal(RS, LCtx);
Jordan Rose57c03362012-07-30 23:39:47 +0000145 state = state->BindExpr(CE, callerCtx, V);
Jordan Rose852aa0d2012-07-10 22:07:52 +0000146 }
Anna Zaks0b3ade82012-04-20 21:59:08 +0000147
Jordan Rose852aa0d2012-07-10 22:07:52 +0000148 // Bind the constructed object value to CXXConstructExpr.
149 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(CE)) {
150 loc::MemRegionVal This =
151 svalBuilder.getCXXThis(CCE->getConstructor()->getParent(), calleeCtx);
152 SVal ThisV = state->getSVal(This);
Anna Zaks0b3ade82012-04-20 21:59:08 +0000153
Jordan Rose852aa0d2012-07-10 22:07:52 +0000154 // Always bind the region to the CXXConstructExpr.
Jordan Rose57c03362012-07-30 23:39:47 +0000155 state = state->BindExpr(CCE, callerCtx, ThisV);
Jordan Rose852aa0d2012-07-10 22:07:52 +0000156 }
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000157 }
Anna Zaks0b3ade82012-04-20 21:59:08 +0000158
Jordan Rose4e79fdf2012-08-15 20:07:17 +0000159 // Generate a CallEvent /before/ cleaning the state, so that we can get the
160 // correct value for 'this' (if necessary).
161 CallEventManager &CEMgr = getStateManager().getCallEventManager();
162 CallEventRef<> Call = CEMgr.getCaller(calleeCtx, state);
163
Anna Zaks0b3ade82012-04-20 21:59:08 +0000164 // Step 3: BindedRetNode -> CleanedNodes
165 // If we can find a statement and a block in the inlined function, run remove
166 // dead bindings before returning from the call. This is important to ensure
167 // that we report the issues such as leaks in the stack contexts in which
168 // they occurred.
169 ExplodedNodeSet CleanedNodes;
170 if (LastSt && Blk) {
Jordan Rose48b62472012-07-10 22:08:01 +0000171 static SimpleProgramPointTag retValBind("ExprEngine : Bind Return Value");
172 PostStmt Loc(LastSt, calleeCtx, &retValBind);
173 bool isNew;
174 ExplodedNode *BindedRetNode = G.getNode(Loc, state, false, &isNew);
175 BindedRetNode->addPredecessor(CEBNode, G);
176 if (!isNew)
177 return;
178
Anna Zaks0b3ade82012-04-20 21:59:08 +0000179 NodeBuilderContext Ctx(getCoreEngine(), Blk, BindedRetNode);
Ted Kremenek66c486f2012-08-22 06:26:15 +0000180 currBldrCtx = &Ctx;
Anna Zaks0b3ade82012-04-20 21:59:08 +0000181 // Here, we call the Symbol Reaper with 0 statement and caller location
182 // context, telling it to clean up everything in the callee's context
183 // (and it's children). We use LastStmt as a diagnostic statement, which
184 // which the PreStmtPurge Dead point will be associated.
185 removeDead(BindedRetNode, CleanedNodes, 0, callerCtx, LastSt,
186 ProgramPoint::PostStmtPurgeDeadSymbolsKind);
Ted Kremenek66c486f2012-08-22 06:26:15 +0000187 currBldrCtx = 0;
Anna Zaks144e52b2012-06-01 23:48:40 +0000188 } else {
189 CleanedNodes.Add(CEBNode);
Anna Zaks0b3ade82012-04-20 21:59:08 +0000190 }
191
192 for (ExplodedNodeSet::iterator I = CleanedNodes.begin(),
193 E = CleanedNodes.end(); I != E; ++I) {
194
195 // Step 4: Generate the CallExit and leave the callee's context.
196 // CleanedNodes -> CEENode
Jordan Rose852aa0d2012-07-10 22:07:52 +0000197 CallExitEnd Loc(calleeCtx, callerCtx);
Anna Zaks0b3ade82012-04-20 21:59:08 +0000198 bool isNew;
Jordan Rose48b62472012-07-10 22:08:01 +0000199 ProgramStateRef CEEState = (*I == CEBNode) ? state : (*I)->getState();
200 ExplodedNode *CEENode = G.getNode(Loc, CEEState, false, &isNew);
Anna Zaks0b3ade82012-04-20 21:59:08 +0000201 CEENode->addPredecessor(*I, G);
202 if (!isNew)
203 return;
204
205 // Step 5: Perform the post-condition check of the CallExpr and enqueue the
206 // result onto the work list.
207 // CEENode -> Dst -> WorkList
Anna Zaks0b3ade82012-04-20 21:59:08 +0000208 NodeBuilderContext Ctx(Engine, calleeCtx->getCallSiteBlock(), CEENode);
Ted Kremenek66c486f2012-08-22 06:26:15 +0000209 SaveAndRestore<const NodeBuilderContext*> NBCSave(currBldrCtx,
Anna Zaks0b3ade82012-04-20 21:59:08 +0000210 &Ctx);
Ted Kremenek66c486f2012-08-22 06:26:15 +0000211 SaveAndRestore<unsigned> CBISave(currStmtIdx, calleeCtx->getIndex());
Anna Zaks0b3ade82012-04-20 21:59:08 +0000212
Jordan Rose4e79fdf2012-08-15 20:07:17 +0000213 CallEventRef<> UpdatedCall = Call.cloneWithState(CEEState);
Jordan Rose57c03362012-07-30 23:39:47 +0000214
215 ExplodedNodeSet DstPostCall;
Jordan Rose4e79fdf2012-08-15 20:07:17 +0000216 getCheckerManager().runCheckersForPostCall(DstPostCall, CEENode,
217 *UpdatedCall, *this,
218 /*WasInlined=*/true);
Jordan Rose57c03362012-07-30 23:39:47 +0000219
220 ExplodedNodeSet Dst;
Jordan Rose4e79fdf2012-08-15 20:07:17 +0000221 if (const ObjCMethodCall *Msg = dyn_cast<ObjCMethodCall>(Call)) {
222 getCheckerManager().runCheckersForPostObjCMessage(Dst, DstPostCall, *Msg,
223 *this,
224 /*WasInlined=*/true);
Jordan Rose57c03362012-07-30 23:39:47 +0000225 } else if (CE) {
226 getCheckerManager().runCheckersForPostStmt(Dst, DstPostCall, CE,
Jordan Rose4e79fdf2012-08-15 20:07:17 +0000227 *this, /*WasInlined=*/true);
Jordan Rose57c03362012-07-30 23:39:47 +0000228 } else {
229 Dst.insert(DstPostCall);
230 }
Anna Zaks0b3ade82012-04-20 21:59:08 +0000231
232 // Enqueue the next element in the block.
233 for (ExplodedNodeSet::iterator PSI = Dst.begin(), PSE = Dst.end();
234 PSI != PSE; ++PSI) {
235 Engine.getWorkList()->enqueue(*PSI, calleeCtx->getCallSiteBlock(),
236 calleeCtx->getIndex()+1);
237 }
Ted Kremenek242384d2012-01-07 00:10:49 +0000238 }
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000239}
240
Ted Kremenek0849ade2012-01-12 19:25:46 +0000241static unsigned getNumberStackFrames(const LocationContext *LCtx) {
242 unsigned count = 0;
243 while (LCtx) {
244 if (isa<StackFrameContext>(LCtx))
245 ++count;
246 LCtx = LCtx->getParent();
247 }
248 return count;
249}
250
Anna Zaks6cc09692012-03-13 22:15:58 +0000251// Determine if we should inline the call.
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000252bool ExprEngine::shouldInlineDecl(const Decl *D, ExplodedNode *Pred) {
253 AnalysisDeclContext *CalleeADC = AMgr.getAnalysisDeclContext(D);
Anna Zaks6cc09692012-03-13 22:15:58 +0000254 const CFG *CalleeCFG = CalleeADC->getCFG();
255
Ted Kremenek01561d12012-04-17 01:36:03 +0000256 // It is possible that the CFG cannot be constructed.
257 // Be safe, and check if the CalleeCFG is valid.
258 if (!CalleeCFG)
259 return false;
260
Anna Zaks6cc09692012-03-13 22:15:58 +0000261 if (getNumberStackFrames(Pred->getLocationContext())
262 == AMgr.InlineMaxStackDepth)
263 return false;
264
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000265 if (Engine.FunctionSummaries->hasReachedMaxBlockCount(D))
Anna Zaks3bbd8cd2012-03-30 05:48:10 +0000266 return false;
267
Anna Zaks6cc09692012-03-13 22:15:58 +0000268 if (CalleeCFG->getNumBlockIDs() > AMgr.InlineMaxFunctionSize)
269 return false;
270
Ted Kremenek10f77ad2012-06-22 23:55:50 +0000271 // Do not inline variadic calls (for now).
272 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
273 if (BD->isVariadic())
274 return false;
Anna Zaks5903a372012-03-27 20:02:53 +0000275 }
Ted Kremenek10f77ad2012-06-22 23:55:50 +0000276 else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
277 if (FD->isVariadic())
278 return false;
279 }
Anna Zaks5903a372012-03-27 20:02:53 +0000280
Ted Kremenekd4aeb802012-07-02 20:21:52 +0000281 // It is possible that the live variables analysis cannot be
282 // run. If so, bail out.
283 if (!CalleeADC->getAnalysis<RelaxedLiveVariables>())
284 return false;
285
Ted Kremenek10f77ad2012-06-22 23:55:50 +0000286 return true;
Anna Zaks5903a372012-03-27 20:02:53 +0000287}
288
Anna Zakse90d3f82012-08-09 00:21:33 +0000289/// The GDM component containing the dynamic dispatch bifurcation info. When
290/// the exact type of the receiver is not known, we want to explore both paths -
291/// one on which we do inline it and the other one on which we don't. This is
292/// done to ensure we do not drop coverage.
293/// This is the map from the receiver region to a bool, specifying either we
294/// consider this region's information precise or not along the given path.
295namespace clang {
296namespace ento {
Anna Zaks6960f6e2012-08-09 21:02:41 +0000297enum DynamicDispatchMode { DynamicDispatchModeInlined = 1,
298 DynamicDispatchModeConservative };
299
Anna Zakse90d3f82012-08-09 00:21:33 +0000300struct DynamicDispatchBifurcationMap {};
301typedef llvm::ImmutableMap<const MemRegion*,
Anna Zaks6960f6e2012-08-09 21:02:41 +0000302 unsigned int> DynamicDispatchBifur;
Anna Zakse90d3f82012-08-09 00:21:33 +0000303template<> struct ProgramStateTrait<DynamicDispatchBifurcationMap>
304 : public ProgramStatePartialTrait<DynamicDispatchBifur> {
305 static void *GDMIndex() { static int index; return &index; }
306};
Anna Zaks6960f6e2012-08-09 21:02:41 +0000307
Anna Zakse90d3f82012-08-09 00:21:33 +0000308}}
Anna Zaks5903a372012-03-27 20:02:53 +0000309
Jordan Rosec568e2f2012-08-21 21:44:21 +0000310static bool shouldInlineCXX(AnalysisManager &AMgr) {
311 switch (AMgr.IPAMode) {
312 case None:
313 case BasicInlining:
314 return false;
315 case Inlining:
316 case DynamicDispatch:
317 case DynamicDispatchBifurcate:
318 return true;
319 case NumIPAModes:
320 llvm_unreachable("not actually a valid option");
321 }
Matt Beaumont-Gay12e2fb02012-08-21 22:27:18 +0000322 llvm_unreachable("bogus IPAMode");
Jordan Rosec568e2f2012-08-21 21:44:21 +0000323}
324
Anna Zakse90d3f82012-08-09 00:21:33 +0000325bool ExprEngine::inlineCall(const CallEvent &Call, const Decl *D,
326 NodeBuilder &Bldr, ExplodedNode *Pred,
327 ProgramStateRef State) {
328 assert(D);
Jordan Roseee158bc2012-07-09 16:54:49 +0000329
Jordan Rosec36b30c2012-07-12 00:16:25 +0000330 const LocationContext *CurLC = Pred->getLocationContext();
331 const StackFrameContext *CallerSFC = CurLC->getCurrentStackFrame();
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000332 const LocationContext *ParentOfCallee = 0;
Jordan Rose69f87c92012-07-02 19:28:09 +0000333
Jordan Roseef158312012-07-31 01:07:55 +0000334 // FIXME: Refactor this check into a hypothetical CallEvent::canInline.
Jordan Rose69f87c92012-07-02 19:28:09 +0000335 switch (Call.getKind()) {
336 case CE_Function:
Jordan Rose2f9c40a2012-07-31 18:22:40 +0000337 break;
Jordan Rose69f87c92012-07-02 19:28:09 +0000338 case CE_CXXMember:
Jordan Rosee54cfc72012-07-10 22:07:57 +0000339 case CE_CXXMemberOperator:
Jordan Rosec568e2f2012-08-21 21:44:21 +0000340 if (!shouldInlineCXX(getAnalysisManager()))
Jordan Rose2f9c40a2012-07-31 18:22:40 +0000341 return false;
Jordan Rose69f87c92012-07-02 19:28:09 +0000342 break;
Jordan Roseef158312012-07-31 01:07:55 +0000343 case CE_CXXConstructor: {
Jordan Rosec568e2f2012-08-21 21:44:21 +0000344 if (!shouldInlineCXX(getAnalysisManager()))
Jordan Rose2f9c40a2012-07-31 18:22:40 +0000345 return false;
346
Jordan Roseef158312012-07-31 01:07:55 +0000347 const CXXConstructorCall &Ctor = cast<CXXConstructorCall>(Call);
348
349 // FIXME: We don't handle constructors or destructors for arrays properly.
350 const MemRegion *Target = Ctor.getCXXThisVal().getAsRegion();
351 if (Target && isa<ElementRegion>(Target))
352 return false;
353
Jordan Rose6fe4dfb2012-08-27 18:39:22 +0000354 // FIXME: This is a hack. We don't use the correct region for a new
355 // expression, so if we inline the constructor its result will just be
356 // thrown away. This short-term hack is tracked in <rdar://problem/12180598>
357 // and the longer-term possible fix is discussed in PR12014.
358 const CXXConstructExpr *CtorExpr = Ctor.getOriginExpr();
359 if (const Stmt *Parent = CurLC->getParentMap().getParent(CtorExpr))
360 if (isa<CXXNewExpr>(Parent))
361 return false;
362
Jordan Rosec210cb72012-08-27 17:50:07 +0000363 // If the destructor is trivial, it's always safe to inline the constructor.
364 if (Ctor.getDecl()->getParent()->hasTrivialDestructor())
365 break;
366
367 // For other types, only inline constructors if we built the CFGs for the
368 // destructor properly.
369 const AnalysisDeclContext *ADC = CallerSFC->getAnalysisDeclContext();
370 assert(ADC->getCFGBuildOptions().AddInitializers && "No CFG initializers");
371 if (!ADC->getCFGBuildOptions().AddImplicitDtors)
372 return false;
373
Jordan Roseef158312012-07-31 01:07:55 +0000374 // FIXME: This is a hack. We don't handle temporary destructors
375 // right now, so we shouldn't inline their constructors.
Jordan Roseef158312012-07-31 01:07:55 +0000376 if (CtorExpr->getConstructionKind() == CXXConstructExpr::CK_Complete)
377 if (!Target || !isa<DeclRegion>(Target))
378 return false;
379
380 break;
381 }
Jordan Roseda5fc532012-07-26 20:04:00 +0000382 case CE_CXXDestructor: {
Jordan Rosec568e2f2012-08-21 21:44:21 +0000383 if (!shouldInlineCXX(getAnalysisManager()))
Jordan Rose2f9c40a2012-07-31 18:22:40 +0000384 return false;
385
Jordan Roseda5fc532012-07-26 20:04:00 +0000386 // Only inline constructors and destructors if we built the CFGs for them
387 // properly.
388 const AnalysisDeclContext *ADC = CallerSFC->getAnalysisDeclContext();
Jordan Rosec210cb72012-08-27 17:50:07 +0000389 if (!ADC->getCFGBuildOptions().AddImplicitDtors)
Jordan Roseda5fc532012-07-26 20:04:00 +0000390 return false;
Jordan Rose3a0a9e32012-07-26 20:04:21 +0000391
Jordan Roseef158312012-07-31 01:07:55 +0000392 const CXXDestructorCall &Dtor = cast<CXXDestructorCall>(Call);
393
Jordan Rosee460c462012-07-26 20:04:25 +0000394 // FIXME: We don't handle constructors or destructors for arrays properly.
Jordan Roseef158312012-07-31 01:07:55 +0000395 const MemRegion *Target = Dtor.getCXXThisVal().getAsRegion();
Jordan Rosee460c462012-07-26 20:04:25 +0000396 if (Target && isa<ElementRegion>(Target))
397 return false;
398
Jordan Roseda5fc532012-07-26 20:04:00 +0000399 break;
400 }
Jordan Rose70cbf3c2012-07-02 22:21:47 +0000401 case CE_CXXAllocator:
Jordan Rosec568e2f2012-08-21 21:44:21 +0000402 if (!shouldInlineCXX(getAnalysisManager()))
Jordan Rose2f9c40a2012-07-31 18:22:40 +0000403 return false;
404
Jordan Rose70cbf3c2012-07-02 22:21:47 +0000405 // Do not inline allocators until we model deallocators.
406 // This is unfortunate, but basically necessary for smart pointers and such.
407 return false;
Jordan Rose69f87c92012-07-02 19:28:09 +0000408 case CE_Block: {
409 const BlockDataRegion *BR = cast<BlockCall>(Call).getBlockRegion();
Jordan Roseee158bc2012-07-09 16:54:49 +0000410 assert(BR && "If we have the block definition we should have its region");
Jordan Rose69f87c92012-07-02 19:28:09 +0000411 AnalysisDeclContext *BlockCtx = AMgr.getAnalysisDeclContext(D);
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000412 ParentOfCallee = BlockCtx->getBlockInvocationContext(CallerSFC,
Jordan Rose69f87c92012-07-02 19:28:09 +0000413 cast<BlockDecl>(D),
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000414 BR);
Jordan Rose69f87c92012-07-02 19:28:09 +0000415 break;
416 }
417 case CE_ObjCMessage:
Anna Zakse90d3f82012-08-09 00:21:33 +0000418 if (!(getAnalysisManager().IPAMode == DynamicDispatch ||
419 getAnalysisManager().IPAMode == DynamicDispatchBifurcate))
Anna Zakse13056a2012-07-30 20:31:18 +0000420 return false;
Anna Zaks9dc51672012-07-26 00:27:51 +0000421 break;
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000422 }
Jordan Roseee158bc2012-07-09 16:54:49 +0000423
424 if (!shouldInlineDecl(D, Pred))
Ted Kremenek256ef642012-01-11 01:06:27 +0000425 return false;
426
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000427 if (!ParentOfCallee)
428 ParentOfCallee = CallerSFC;
Anna Zaks8235f9c2012-03-02 19:05:03 +0000429
Jordan Rose852aa0d2012-07-10 22:07:52 +0000430 // This may be NULL, but that's fine.
Jordan Rose69f87c92012-07-02 19:28:09 +0000431 const Expr *CallE = Call.getOriginExpr();
Jordan Rose69f87c92012-07-02 19:28:09 +0000432
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000433 // Construct a new stack frame for the callee.
434 AnalysisDeclContext *CalleeADC = AMgr.getAnalysisDeclContext(D);
435 const StackFrameContext *CalleeSFC =
Jordan Rose69f87c92012-07-02 19:28:09 +0000436 CalleeADC->getStackFrame(ParentOfCallee, CallE,
Ted Kremenek66c486f2012-08-22 06:26:15 +0000437 currBldrCtx->getBlock(),
438 currStmtIdx);
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000439
Jordan Rosec36b30c2012-07-12 00:16:25 +0000440 CallEnter Loc(CallE, CalleeSFC, CurLC);
Jordan Rosee54cfc72012-07-10 22:07:57 +0000441
442 // Construct a new state which contains the mapping from actual to
443 // formal arguments.
Anna Zakse90d3f82012-08-09 00:21:33 +0000444 State = State->enterStackFrame(Call, CalleeSFC);
Jordan Rosee54cfc72012-07-10 22:07:57 +0000445
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000446 bool isNew;
Jordan Rosee54cfc72012-07-10 22:07:57 +0000447 if (ExplodedNode *N = G.getNode(Loc, State, false, &isNew)) {
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000448 N->addPredecessor(Pred, G);
449 if (isNew)
450 Engine.getWorkList()->enqueue(N);
Ted Kremenek256ef642012-01-11 01:06:27 +0000451 }
Anna Zakse90d3f82012-08-09 00:21:33 +0000452
453 // If we decided to inline the call, the successor has been manually
454 // added onto the work list so remove it from the node builder.
455 Bldr.takeNodes(Pred);
456
Anna Zaks210f5a22012-08-27 18:38:32 +0000457 NumInlinedCalls++;
458
Ted Kremenek7fa9b4f2012-06-01 20:04:04 +0000459 return true;
Ted Kremenek256ef642012-01-11 01:06:27 +0000460}
461
Anna Zakse81ce252012-07-19 23:38:13 +0000462static ProgramStateRef getInlineFailedState(ProgramStateRef State,
Jordan Rose69f87c92012-07-02 19:28:09 +0000463 const Stmt *CallE) {
Anna Zakse81ce252012-07-19 23:38:13 +0000464 void *ReplayState = State->get<ReplayWithoutInlining>();
Anna Zaks5903a372012-03-27 20:02:53 +0000465 if (!ReplayState)
466 return 0;
Jordan Rose28038f32012-07-10 22:07:42 +0000467
468 assert(ReplayState == (const void*)CallE && "Backtracked to the wrong call.");
469 (void)CallE;
470
Anna Zakse81ce252012-07-19 23:38:13 +0000471 return State->remove<ReplayWithoutInlining>();
Ted Kremenek10520d72012-02-09 21:59:52 +0000472}
473
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000474void ExprEngine::VisitCallExpr(const CallExpr *CE, ExplodedNode *Pred,
475 ExplodedNodeSet &dst) {
476 // Perform the previsit of the CallExpr.
477 ExplodedNodeSet dstPreVisit;
478 getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, CE, *this);
Anna Zaks5903a372012-03-27 20:02:53 +0000479
Jordan Rosed563d3f2012-07-30 20:22:09 +0000480 // Get the call in its initial state. We use this as a template to perform
481 // all the checks.
482 CallEventManager &CEMgr = getStateManager().getCallEventManager();
Jordan Rose645baee2012-08-13 23:46:05 +0000483 CallEventRef<> CallTemplate
Jordan Rosed563d3f2012-07-30 20:22:09 +0000484 = CEMgr.getSimpleCall(CE, Pred->getState(), Pred->getLocationContext());
Anna Zaks5903a372012-03-27 20:02:53 +0000485
Jordan Rose69f87c92012-07-02 19:28:09 +0000486 // Evaluate the function call. We try each of the checkers
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000487 // to see if the can evaluate the function call.
488 ExplodedNodeSet dstCallEvaluated;
Jordan Rose69f87c92012-07-02 19:28:09 +0000489 for (ExplodedNodeSet::iterator I = dstPreVisit.begin(), E = dstPreVisit.end();
490 I != E; ++I) {
Jordan Rosed563d3f2012-07-30 20:22:09 +0000491 evalCall(dstCallEvaluated, *I, *CallTemplate);
Jordan Rose69f87c92012-07-02 19:28:09 +0000492 }
493
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000494 // Finally, perform the post-condition check of the CallExpr and store
495 // the created nodes in 'Dst'.
Jordan Rose69f87c92012-07-02 19:28:09 +0000496 // Note that if the call was inlined, dstCallEvaluated will be empty.
497 // The post-CallExpr check will occur in processCallExit.
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000498 getCheckerManager().runCheckersForPostStmt(dst, dstCallEvaluated, CE,
499 *this);
500}
501
Jordan Rose69f87c92012-07-02 19:28:09 +0000502void ExprEngine::evalCall(ExplodedNodeSet &Dst, ExplodedNode *Pred,
Jordan Rose645baee2012-08-13 23:46:05 +0000503 const CallEvent &Call) {
Jordan Rosed563d3f2012-07-30 20:22:09 +0000504 // WARNING: At this time, the state attached to 'Call' may be older than the
505 // state in 'Pred'. This is a minor optimization since CheckerManager will
506 // use an updated CallEvent instance when calling checkers, but if 'Call' is
507 // ever used directly in this function all callers should be updated to pass
508 // the most recent state. (It is probably not worth doing the work here since
509 // for some callers this will not be necessary.)
510
Jordan Rose96479da2012-07-02 19:28:16 +0000511 // Run any pre-call checks using the generic call interface.
512 ExplodedNodeSet dstPreVisit;
513 getCheckerManager().runCheckersForPreCall(dstPreVisit, Pred, Call, *this);
514
515 // Actually evaluate the function call. We try each of the checkers
516 // to see if the can evaluate the function call, and get a callback at
517 // defaultEvalCall if all of them fail.
518 ExplodedNodeSet dstCallEvaluated;
519 getCheckerManager().runCheckersForEvalCall(dstCallEvaluated, dstPreVisit,
520 Call, *this);
521
522 // Finally, run any post-call checks.
523 getCheckerManager().runCheckersForPostCall(Dst, dstCallEvaluated,
524 Call, *this);
Jordan Rose69f87c92012-07-02 19:28:09 +0000525}
526
Anna Zakse81ce252012-07-19 23:38:13 +0000527ProgramStateRef ExprEngine::bindReturnValue(const CallEvent &Call,
528 const LocationContext *LCtx,
529 ProgramStateRef State) {
530 const Expr *E = Call.getOriginExpr();
531 if (!E)
532 return State;
533
534 // Some method families have known return values.
535 if (const ObjCMethodCall *Msg = dyn_cast<ObjCMethodCall>(&Call)) {
536 switch (Msg->getMethodFamily()) {
537 default:
538 break;
539 case OMF_autorelease:
540 case OMF_retain:
541 case OMF_self: {
542 // These methods return their receivers.
543 return State->BindExpr(E, LCtx, Msg->getReceiverSVal());
Anna Zakse81ce252012-07-19 23:38:13 +0000544 }
545 }
Jordan Rosee460c462012-07-26 20:04:25 +0000546 } else if (const CXXConstructorCall *C = dyn_cast<CXXConstructorCall>(&Call)){
547 return State->BindExpr(E, LCtx, C->getCXXThisVal());
Anna Zakse81ce252012-07-19 23:38:13 +0000548 }
549
550 // Conjure a symbol if the return value is unknown.
551 QualType ResultTy = Call.getResultType();
552 SValBuilder &SVB = getSValBuilder();
Ted Kremenek66c486f2012-08-22 06:26:15 +0000553 unsigned Count = currBldrCtx->blockCount();
Ted Kremenek3b1df8b2012-08-22 06:26:06 +0000554 SVal R = SVB.conjureSymbolVal(0, E, LCtx, ResultTy, Count);
Anna Zakse81ce252012-07-19 23:38:13 +0000555 return State->BindExpr(E, LCtx, R);
556}
557
Anna Zakse90d3f82012-08-09 00:21:33 +0000558// Conservatively evaluate call by invalidating regions and binding
559// a conjured return value.
560void ExprEngine::conservativeEvalCall(const CallEvent &Call, NodeBuilder &Bldr,
561 ExplodedNode *Pred, ProgramStateRef State) {
Ted Kremenek66c486f2012-08-22 06:26:15 +0000562 State = Call.invalidateRegions(currBldrCtx->blockCount(), State);
Anna Zakse90d3f82012-08-09 00:21:33 +0000563 State = bindReturnValue(Call, Pred->getLocationContext(), State);
564
565 // And make the result node.
566 Bldr.generateNode(Call.getProgramPoint(), State, Pred);
567}
568
Anna Zakse81ce252012-07-19 23:38:13 +0000569void ExprEngine::defaultEvalCall(NodeBuilder &Bldr, ExplodedNode *Pred,
Jordan Rosed563d3f2012-07-30 20:22:09 +0000570 const CallEvent &CallTemplate) {
571 // Make sure we have the most recent state attached to the call.
572 ProgramStateRef State = Pred->getState();
573 CallEventRef<> Call = CallTemplate.cloneWithState(State);
Anna Zakse81ce252012-07-19 23:38:13 +0000574
Anna Zaks5960f4a2012-08-09 18:43:00 +0000575 if (!getAnalysisManager().shouldInlineCall()) {
576 conservativeEvalCall(*Call, Bldr, Pred, State);
577 return;
578 }
Jordan Rose69f87c92012-07-02 19:28:09 +0000579 // Try to inline the call.
Jordan Rose28038f32012-07-10 22:07:42 +0000580 // The origin expression here is just used as a kind of checksum;
Jordan Rosed563d3f2012-07-30 20:22:09 +0000581 // this should still be safe even for CallEvents that don't come from exprs.
582 const Expr *E = Call->getOriginExpr();
583 ProgramStateRef InlinedFailedState = getInlineFailedState(State, E);
584
585 if (InlinedFailedState) {
586 // If we already tried once and failed, make sure we don't retry later.
587 State = InlinedFailedState;
Anna Zaks5960f4a2012-08-09 18:43:00 +0000588 } else {
Anna Zakse90d3f82012-08-09 00:21:33 +0000589 RuntimeDefinition RD = Call->getRuntimeDefinition();
Anna Zaksfc05dec2012-08-09 02:57:02 +0000590 const Decl *D = RD.getDecl();
Anna Zakse90d3f82012-08-09 00:21:33 +0000591 if (D) {
Jordan Roseb763ede2012-08-15 00:52:00 +0000592 if (RD.mayHaveOtherDefinitions()) {
593 // Explore with and without inlining the call.
594 if (getAnalysisManager().IPAMode == DynamicDispatchBifurcate) {
595 BifurcateCall(RD.getDispatchRegion(), *Call, D, Bldr, Pred);
596 return;
597 }
598
599 // Don't inline if we're not in any dynamic dispatch mode.
Jordan Roseda29ac52012-08-15 21:05:15 +0000600 if (getAnalysisManager().IPAMode != DynamicDispatch) {
601 conservativeEvalCall(*Call, Bldr, Pred, State);
Jordan Roseb763ede2012-08-15 00:52:00 +0000602 return;
Jordan Roseda29ac52012-08-15 21:05:15 +0000603 }
Anna Zakse90d3f82012-08-09 00:21:33 +0000604 }
Jordan Roseb763ede2012-08-15 00:52:00 +0000605
Anna Zaks5960f4a2012-08-09 18:43:00 +0000606 // We are not bifurcating and we do have a Decl, so just inline.
607 if (inlineCall(*Call, D, Bldr, Pred, State))
608 return;
Anna Zakse90d3f82012-08-09 00:21:33 +0000609 }
Anna Zakse81ce252012-07-19 23:38:13 +0000610 }
Jordan Rose69f87c92012-07-02 19:28:09 +0000611
612 // If we can't inline it, handle the return value and invalidate the regions.
Anna Zakse90d3f82012-08-09 00:21:33 +0000613 conservativeEvalCall(*Call, Bldr, Pred, State);
Jordan Rose69f87c92012-07-02 19:28:09 +0000614}
615
Anna Zakse90d3f82012-08-09 00:21:33 +0000616void ExprEngine::BifurcateCall(const MemRegion *BifurReg,
617 const CallEvent &Call, const Decl *D,
618 NodeBuilder &Bldr, ExplodedNode *Pred) {
619 assert(BifurReg);
Jordan Roseb763ede2012-08-15 00:52:00 +0000620 BifurReg = BifurReg->StripCasts();
Anna Zakse90d3f82012-08-09 00:21:33 +0000621
622 // Check if we've performed the split already - note, we only want
623 // to split the path once per memory region.
624 ProgramStateRef State = Pred->getState();
Anna Zaks6960f6e2012-08-09 21:02:41 +0000625 const unsigned int *BState =
626 State->get<DynamicDispatchBifurcationMap>(BifurReg);
Anna Zaks5960f4a2012-08-09 18:43:00 +0000627 if (BState) {
628 // If we are on "inline path", keep inlining if possible.
Anna Zaks6960f6e2012-08-09 21:02:41 +0000629 if (*BState == DynamicDispatchModeInlined)
Anna Zaks5960f4a2012-08-09 18:43:00 +0000630 if (inlineCall(Call, D, Bldr, Pred, State))
631 return;
632 // If inline failed, or we are on the path where we assume we
633 // don't have enough info about the receiver to inline, conjure the
634 // return value and invalidate the regions.
635 conservativeEvalCall(Call, Bldr, Pred, State);
636 return;
Anna Zakse90d3f82012-08-09 00:21:33 +0000637 }
638
639 // If we got here, this is the first time we process a message to this
640 // region, so split the path.
641 ProgramStateRef IState =
Anna Zaks6960f6e2012-08-09 21:02:41 +0000642 State->set<DynamicDispatchBifurcationMap>(BifurReg,
643 DynamicDispatchModeInlined);
Anna Zakse90d3f82012-08-09 00:21:33 +0000644 inlineCall(Call, D, Bldr, Pred, IState);
645
646 ProgramStateRef NoIState =
Anna Zaks6960f6e2012-08-09 21:02:41 +0000647 State->set<DynamicDispatchBifurcationMap>(BifurReg,
648 DynamicDispatchModeConservative);
Anna Zakse90d3f82012-08-09 00:21:33 +0000649 conservativeEvalCall(Call, Bldr, Pred, NoIState);
650
651 NumOfDynamicDispatchPathSplits++;
652 return;
653}
654
655
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000656void ExprEngine::VisitReturnStmt(const ReturnStmt *RS, ExplodedNode *Pred,
657 ExplodedNodeSet &Dst) {
Ted Kremenek256ef642012-01-11 01:06:27 +0000658
659 ExplodedNodeSet dstPreVisit;
660 getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, RS, *this);
661
Ted Kremenek66c486f2012-08-22 06:26:15 +0000662 StmtNodeBuilder B(dstPreVisit, Dst, *currBldrCtx);
Ted Kremenek256ef642012-01-11 01:06:27 +0000663
664 if (RS->getRetValue()) {
665 for (ExplodedNodeSet::iterator it = dstPreVisit.begin(),
666 ei = dstPreVisit.end(); it != ei; ++it) {
667 B.generateNode(RS, *it, (*it)->getState());
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000668 }
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000669 }
Ted Kremenek294fd0a2011-08-20 06:00:03 +0000670}