ExprEngineCallAndReturn.cpp revision d563d3fb73879df7147b8a5302c3bf0e1402ba18
1//=-- 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
14#include "clang/Analysis/Analyses/LiveVariables.h"
15#include "clang/StaticAnalyzer/Core/CheckerManager.h"
16#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
18#include "clang/AST/DeclCXX.h"
19#include "llvm/ADT/SmallSet.h"
20#include "llvm/Support/SaveAndRestore.h"
21
22using namespace clang;
23using namespace ento;
24
25void ExprEngine::processCallEnter(CallEnter CE, ExplodedNode *Pred) {
26  // Get the entry block in the CFG of the callee.
27  const StackFrameContext *calleeCtx = CE.getCalleeContext();
28  const CFG *CalleeCFG = calleeCtx->getCFG();
29  const CFGBlock *Entry = &(CalleeCFG->getEntry());
30
31  // Validate the CFG.
32  assert(Entry->empty());
33  assert(Entry->succ_size() == 1);
34
35  // Get the solitary sucessor.
36  const CFGBlock *Succ = *(Entry->succ_begin());
37
38  // Construct an edge representing the starting location in the callee.
39  BlockEdge Loc(Entry, Succ, calleeCtx);
40
41  ProgramStateRef state = Pred->getState();
42
43  // Construct a new node and add it to the worklist.
44  bool isNew;
45  ExplodedNode *Node = G.getNode(Loc, state, false, &isNew);
46  Node->addPredecessor(Pred, G);
47  if (isNew)
48    Engine.getWorkList()->enqueue(Node);
49}
50
51// Find the last statement on the path to the exploded node and the
52// corresponding Block.
53static std::pair<const Stmt*,
54                 const CFGBlock*> getLastStmt(const ExplodedNode *Node) {
55  const Stmt *S = 0;
56  const StackFrameContext *SF =
57          Node->getLocation().getLocationContext()->getCurrentStackFrame();
58
59  // Back up through the ExplodedGraph until we reach a statement node.
60  while (Node) {
61    const ProgramPoint &PP = Node->getLocation();
62
63    if (const StmtPoint *SP = dyn_cast<StmtPoint>(&PP)) {
64      S = SP->getStmt();
65      break;
66    } else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&PP)) {
67      S = CEE->getCalleeContext()->getCallSite();
68      if (S)
69        break;
70      // If we have an implicit call, we'll probably end up with a
71      // StmtPoint inside the callee, which is acceptable.
72      // (It's possible a function ONLY contains implicit calls -- such as an
73      // implicitly-generated destructor -- so we shouldn't just skip back to
74      // the CallEnter node and keep going.)
75    } else if (const CallEnter *CE = dyn_cast<CallEnter>(&PP)) {
76      // If we reached the CallEnter for this function, it has no statements.
77      if (CE->getCalleeContext() == SF)
78        break;
79    }
80
81    Node = *Node->pred_begin();
82  }
83
84  const CFGBlock *Blk = 0;
85  if (S) {
86    // Now, get the enclosing basic block.
87    while (Node && Node->pred_size() >=1 ) {
88      const ProgramPoint &PP = Node->getLocation();
89      if (isa<BlockEdge>(PP) &&
90          (PP.getLocationContext()->getCurrentStackFrame() == SF)) {
91        BlockEdge &EPP = cast<BlockEdge>(PP);
92        Blk = EPP.getDst();
93        break;
94      }
95      Node = *Node->pred_begin();
96    }
97  }
98
99  return std::pair<const Stmt*, const CFGBlock*>(S, Blk);
100}
101
102/// The call exit is simulated with a sequence of nodes, which occur between
103/// CallExitBegin and CallExitEnd. The following operations occur between the
104/// two program points:
105/// 1. CallExitBegin (triggers the start of call exit sequence)
106/// 2. Bind the return value
107/// 3. Run Remove dead bindings to clean up the dead symbols from the callee.
108/// 4. CallExitEnd (switch to the caller context)
109/// 5. PostStmt<CallExpr>
110void ExprEngine::processCallExit(ExplodedNode *CEBNode) {
111  // Step 1 CEBNode was generated before the call.
112
113  const StackFrameContext *calleeCtx =
114      CEBNode->getLocationContext()->getCurrentStackFrame();
115
116  // The parent context might not be a stack frame, so make sure we
117  // look up the first enclosing stack frame.
118  const StackFrameContext *callerCtx =
119    calleeCtx->getParent()->getCurrentStackFrame();
120
121  const Stmt *CE = calleeCtx->getCallSite();
122  ProgramStateRef state = CEBNode->getState();
123  // Find the last statement in the function and the corresponding basic block.
124  const Stmt *LastSt = 0;
125  const CFGBlock *Blk = 0;
126  llvm::tie(LastSt, Blk) = getLastStmt(CEBNode);
127
128  // Step 2: generate node with bound return value: CEBNode -> BindedRetNode.
129
130  // If the callee returns an expression, bind its value to CallExpr.
131  if (CE) {
132    if (const ReturnStmt *RS = dyn_cast_or_null<ReturnStmt>(LastSt)) {
133      const LocationContext *LCtx = CEBNode->getLocationContext();
134      SVal V = state->getSVal(RS, LCtx);
135      state = state->BindExpr(CE, calleeCtx->getParent(), V);
136    }
137
138    // Bind the constructed object value to CXXConstructExpr.
139    if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(CE)) {
140      loc::MemRegionVal This =
141        svalBuilder.getCXXThis(CCE->getConstructor()->getParent(), calleeCtx);
142      SVal ThisV = state->getSVal(This);
143
144      // Always bind the region to the CXXConstructExpr.
145      state = state->BindExpr(CCE, calleeCtx->getParent(), ThisV);
146    }
147  }
148
149  // Step 3: BindedRetNode -> CleanedNodes
150  // If we can find a statement and a block in the inlined function, run remove
151  // dead bindings before returning from the call. This is important to ensure
152  // that we report the issues such as leaks in the stack contexts in which
153  // they occurred.
154  ExplodedNodeSet CleanedNodes;
155  if (LastSt && Blk) {
156    static SimpleProgramPointTag retValBind("ExprEngine : Bind Return Value");
157    PostStmt Loc(LastSt, calleeCtx, &retValBind);
158    bool isNew;
159    ExplodedNode *BindedRetNode = G.getNode(Loc, state, false, &isNew);
160    BindedRetNode->addPredecessor(CEBNode, G);
161    if (!isNew)
162      return;
163
164    NodeBuilderContext Ctx(getCoreEngine(), Blk, BindedRetNode);
165    currentBuilderContext = &Ctx;
166    // Here, we call the Symbol Reaper with 0 statement and caller location
167    // context, telling it to clean up everything in the callee's context
168    // (and it's children). We use LastStmt as a diagnostic statement, which
169    // which the PreStmtPurge Dead point will be associated.
170    removeDead(BindedRetNode, CleanedNodes, 0, callerCtx, LastSt,
171               ProgramPoint::PostStmtPurgeDeadSymbolsKind);
172    currentBuilderContext = 0;
173  } else {
174    CleanedNodes.Add(CEBNode);
175  }
176
177  for (ExplodedNodeSet::iterator I = CleanedNodes.begin(),
178                                 E = CleanedNodes.end(); I != E; ++I) {
179
180    // Step 4: Generate the CallExit and leave the callee's context.
181    // CleanedNodes -> CEENode
182    CallExitEnd Loc(calleeCtx, callerCtx);
183    bool isNew;
184    ProgramStateRef CEEState = (*I == CEBNode) ? state : (*I)->getState();
185    ExplodedNode *CEENode = G.getNode(Loc, CEEState, false, &isNew);
186    CEENode->addPredecessor(*I, G);
187    if (!isNew)
188      return;
189
190    // Step 5: Perform the post-condition check of the CallExpr and enqueue the
191    // result onto the work list.
192    // CEENode -> Dst -> WorkList
193    ExplodedNodeSet Dst;
194    NodeBuilderContext Ctx(Engine, calleeCtx->getCallSiteBlock(), CEENode);
195    SaveAndRestore<const NodeBuilderContext*> NBCSave(currentBuilderContext,
196        &Ctx);
197    SaveAndRestore<unsigned> CBISave(currentStmtIdx, calleeCtx->getIndex());
198
199    // FIXME: This needs to call PostCall.
200    // FIXME: If/when we inline Objective-C messages, this also needs to call
201    // PostObjCMessage.
202    if (CE)
203      getCheckerManager().runCheckersForPostStmt(Dst, CEENode, CE, *this, true);
204    else
205      Dst.Add(CEENode);
206
207    // Enqueue the next element in the block.
208    for (ExplodedNodeSet::iterator PSI = Dst.begin(), PSE = Dst.end();
209                                   PSI != PSE; ++PSI) {
210      Engine.getWorkList()->enqueue(*PSI, calleeCtx->getCallSiteBlock(),
211                                    calleeCtx->getIndex()+1);
212    }
213  }
214}
215
216static unsigned getNumberStackFrames(const LocationContext *LCtx) {
217  unsigned count = 0;
218  while (LCtx) {
219    if (isa<StackFrameContext>(LCtx))
220      ++count;
221    LCtx = LCtx->getParent();
222  }
223  return count;
224}
225
226// Determine if we should inline the call.
227bool ExprEngine::shouldInlineDecl(const Decl *D, ExplodedNode *Pred) {
228  AnalysisDeclContext *CalleeADC = AMgr.getAnalysisDeclContext(D);
229  const CFG *CalleeCFG = CalleeADC->getCFG();
230
231  // It is possible that the CFG cannot be constructed.
232  // Be safe, and check if the CalleeCFG is valid.
233  if (!CalleeCFG)
234    return false;
235
236  if (getNumberStackFrames(Pred->getLocationContext())
237        == AMgr.InlineMaxStackDepth)
238    return false;
239
240  if (Engine.FunctionSummaries->hasReachedMaxBlockCount(D))
241    return false;
242
243  if (CalleeCFG->getNumBlockIDs() > AMgr.InlineMaxFunctionSize)
244    return false;
245
246  // Do not inline variadic calls (for now).
247  if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
248    if (BD->isVariadic())
249      return false;
250  }
251  else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
252    if (FD->isVariadic())
253      return false;
254  }
255
256  // It is possible that the live variables analysis cannot be
257  // run.  If so, bail out.
258  if (!CalleeADC->getAnalysis<RelaxedLiveVariables>())
259    return false;
260
261  return true;
262}
263
264bool ExprEngine::inlineCall(const CallEvent &Call,
265                            ExplodedNode *Pred) {
266  if (!getAnalysisManager().shouldInlineCall())
267    return false;
268
269  const Decl *D = Call.getRuntimeDefinition();
270  if (!D)
271    return false;
272
273  const LocationContext *CurLC = Pred->getLocationContext();
274  const StackFrameContext *CallerSFC = CurLC->getCurrentStackFrame();
275  const LocationContext *ParentOfCallee = 0;
276
277  switch (Call.getKind()) {
278  case CE_Function:
279  case CE_CXXMember:
280  case CE_CXXMemberOperator:
281    // These are always at least possible to inline.
282    break;
283  case CE_CXXConstructor:
284  case CE_CXXDestructor: {
285    // Only inline constructors and destructors if we built the CFGs for them
286    // properly.
287    const AnalysisDeclContext *ADC = CallerSFC->getAnalysisDeclContext();
288    if (!ADC->getCFGBuildOptions().AddImplicitDtors ||
289        !ADC->getCFGBuildOptions().AddInitializers)
290      return false;
291
292    // FIXME: We don't handle constructors or destructors for arrays properly.
293    const MemRegion *Target = Call.getCXXThisVal().getAsRegion();
294    if (Target && isa<ElementRegion>(Target))
295      return false;
296
297    // FIXME: This is a hack. We don't handle temporary destructors
298    // right now, so we shouldn't inline their constructors.
299    if (const CXXConstructorCall *Ctor = dyn_cast<CXXConstructorCall>(&Call)) {
300      const CXXConstructExpr *CtorExpr = Ctor->getOriginExpr();
301      if (CtorExpr->getConstructionKind() == CXXConstructExpr::CK_Complete)
302        if (!Target || !isa<DeclRegion>(Target))
303          return false;
304    }
305    break;
306  }
307  case CE_CXXAllocator:
308    // Do not inline allocators until we model deallocators.
309    // This is unfortunate, but basically necessary for smart pointers and such.
310    return false;
311  case CE_Block: {
312    const BlockDataRegion *BR = cast<BlockCall>(Call).getBlockRegion();
313    assert(BR && "If we have the block definition we should have its region");
314    AnalysisDeclContext *BlockCtx = AMgr.getAnalysisDeclContext(D);
315    ParentOfCallee = BlockCtx->getBlockInvocationContext(CallerSFC,
316                                                         cast<BlockDecl>(D),
317                                                         BR);
318    break;
319  }
320  case CE_ObjCMessage:
321    break;
322  }
323
324  if (!shouldInlineDecl(D, Pred))
325    return false;
326
327  if (!ParentOfCallee)
328    ParentOfCallee = CallerSFC;
329
330  // This may be NULL, but that's fine.
331  const Expr *CallE = Call.getOriginExpr();
332
333  // Construct a new stack frame for the callee.
334  AnalysisDeclContext *CalleeADC = AMgr.getAnalysisDeclContext(D);
335  const StackFrameContext *CalleeSFC =
336    CalleeADC->getStackFrame(ParentOfCallee, CallE,
337                             currentBuilderContext->getBlock(),
338                             currentStmtIdx);
339
340  CallEnter Loc(CallE, CalleeSFC, CurLC);
341
342  // Construct a new state which contains the mapping from actual to
343  // formal arguments.
344  ProgramStateRef State = Pred->getState()->enterStackFrame(Call, CalleeSFC);
345
346  bool isNew;
347  if (ExplodedNode *N = G.getNode(Loc, State, false, &isNew)) {
348    N->addPredecessor(Pred, G);
349    if (isNew)
350      Engine.getWorkList()->enqueue(N);
351  }
352  return true;
353}
354
355static ProgramStateRef getInlineFailedState(ProgramStateRef State,
356                                            const Stmt *CallE) {
357  void *ReplayState = State->get<ReplayWithoutInlining>();
358  if (!ReplayState)
359    return 0;
360
361  assert(ReplayState == (const void*)CallE && "Backtracked to the wrong call.");
362  (void)CallE;
363
364  return State->remove<ReplayWithoutInlining>();
365}
366
367void ExprEngine::VisitCallExpr(const CallExpr *CE, ExplodedNode *Pred,
368                               ExplodedNodeSet &dst) {
369  // Perform the previsit of the CallExpr.
370  ExplodedNodeSet dstPreVisit;
371  getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, CE, *this);
372
373  // Get the call in its initial state. We use this as a template to perform
374  // all the checks.
375  CallEventManager &CEMgr = getStateManager().getCallEventManager();
376  CallEventRef<SimpleCall> CallTemplate
377    = CEMgr.getSimpleCall(CE, Pred->getState(), Pred->getLocationContext());
378
379  // Evaluate the function call.  We try each of the checkers
380  // to see if the can evaluate the function call.
381  ExplodedNodeSet dstCallEvaluated;
382  for (ExplodedNodeSet::iterator I = dstPreVisit.begin(), E = dstPreVisit.end();
383       I != E; ++I) {
384    evalCall(dstCallEvaluated, *I, *CallTemplate);
385  }
386
387  // Finally, perform the post-condition check of the CallExpr and store
388  // the created nodes in 'Dst'.
389  // Note that if the call was inlined, dstCallEvaluated will be empty.
390  // The post-CallExpr check will occur in processCallExit.
391  getCheckerManager().runCheckersForPostStmt(dst, dstCallEvaluated, CE,
392                                             *this);
393}
394
395void ExprEngine::evalCall(ExplodedNodeSet &Dst, ExplodedNode *Pred,
396                          const SimpleCall &Call) {
397  // WARNING: At this time, the state attached to 'Call' may be older than the
398  // state in 'Pred'. This is a minor optimization since CheckerManager will
399  // use an updated CallEvent instance when calling checkers, but if 'Call' is
400  // ever used directly in this function all callers should be updated to pass
401  // the most recent state. (It is probably not worth doing the work here since
402  // for some callers this will not be necessary.)
403
404  // Run any pre-call checks using the generic call interface.
405  ExplodedNodeSet dstPreVisit;
406  getCheckerManager().runCheckersForPreCall(dstPreVisit, Pred, Call, *this);
407
408  // Actually evaluate the function call.  We try each of the checkers
409  // to see if the can evaluate the function call, and get a callback at
410  // defaultEvalCall if all of them fail.
411  ExplodedNodeSet dstCallEvaluated;
412  getCheckerManager().runCheckersForEvalCall(dstCallEvaluated, dstPreVisit,
413                                             Call, *this);
414
415  // Finally, run any post-call checks.
416  getCheckerManager().runCheckersForPostCall(Dst, dstCallEvaluated,
417                                             Call, *this);
418}
419
420ProgramStateRef ExprEngine::bindReturnValue(const CallEvent &Call,
421                                            const LocationContext *LCtx,
422                                            ProgramStateRef State) {
423  const Expr *E = Call.getOriginExpr();
424  if (!E)
425    return State;
426
427  // Some method families have known return values.
428  if (const ObjCMethodCall *Msg = dyn_cast<ObjCMethodCall>(&Call)) {
429    switch (Msg->getMethodFamily()) {
430    default:
431      break;
432    case OMF_autorelease:
433    case OMF_retain:
434    case OMF_self: {
435      // These methods return their receivers.
436      return State->BindExpr(E, LCtx, Msg->getReceiverSVal());
437    }
438    }
439  } else if (const CXXConstructorCall *C = dyn_cast<CXXConstructorCall>(&Call)){
440    return State->BindExpr(E, LCtx, C->getCXXThisVal());
441  }
442
443  // Conjure a symbol if the return value is unknown.
444  QualType ResultTy = Call.getResultType();
445  SValBuilder &SVB = getSValBuilder();
446  unsigned Count = currentBuilderContext->getCurrentBlockCount();
447  SVal R = SVB.getConjuredSymbolVal(0, E, LCtx, ResultTy, Count);
448  return State->BindExpr(E, LCtx, R);
449}
450
451void ExprEngine::defaultEvalCall(NodeBuilder &Bldr, ExplodedNode *Pred,
452                                 const CallEvent &CallTemplate) {
453  // Make sure we have the most recent state attached to the call.
454  ProgramStateRef State = Pred->getState();
455  CallEventRef<> Call = CallTemplate.cloneWithState(State);
456
457  // Try to inline the call.
458  // The origin expression here is just used as a kind of checksum;
459  // this should still be safe even for CallEvents that don't come from exprs.
460  const Expr *E = Call->getOriginExpr();
461  ProgramStateRef InlinedFailedState = getInlineFailedState(State, E);
462
463  if (InlinedFailedState) {
464    // If we already tried once and failed, make sure we don't retry later.
465    State = InlinedFailedState;
466  } else if (inlineCall(*Call, Pred)) {
467    // If we decided to inline the call, the successor has been manually
468    // added onto the work list and we should not perform our generic
469    // call-handling steps.
470    Bldr.takeNodes(Pred);
471    return;
472  }
473
474  // If we can't inline it, handle the return value and invalidate the regions.
475  unsigned Count = currentBuilderContext->getCurrentBlockCount();
476  State = Call->invalidateRegions(Count, State);
477  State = bindReturnValue(*Call, Pred->getLocationContext(), State);
478
479  // And make the result node.
480  Bldr.generateNode(Call->getProgramPoint(), State, Pred);
481}
482
483void ExprEngine::VisitReturnStmt(const ReturnStmt *RS, ExplodedNode *Pred,
484                                 ExplodedNodeSet &Dst) {
485
486  ExplodedNodeSet dstPreVisit;
487  getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, RS, *this);
488
489  StmtNodeBuilder B(dstPreVisit, Dst, *currentBuilderContext);
490
491  if (RS->getRetValue()) {
492    for (ExplodedNodeSet::iterator it = dstPreVisit.begin(),
493                                  ei = dstPreVisit.end(); it != ei; ++it) {
494      B.generateNode(RS, *it, (*it)->getState());
495    }
496  }
497}
498