1*67e74705SXin LiInlining 2*67e74705SXin Li======== 3*67e74705SXin Li 4*67e74705SXin LiThere are several options that control which calls the analyzer will consider for 5*67e74705SXin Liinlining. The major one is -analyzer-config ipa: 6*67e74705SXin Li 7*67e74705SXin Li -analyzer-config ipa=none - All inlining is disabled. This is the only mode 8*67e74705SXin Li available in LLVM 3.1 and earlier and in Xcode 4.3 and earlier. 9*67e74705SXin Li 10*67e74705SXin Li -analyzer-config ipa=basic-inlining - Turns on inlining for C functions, C++ 11*67e74705SXin Li static member functions, and blocks -- essentially, the calls that behave 12*67e74705SXin Li like simple C function calls. This is essentially the mode used in 13*67e74705SXin Li Xcode 4.4. 14*67e74705SXin Li 15*67e74705SXin Li -analyzer-config ipa=inlining - Turns on inlining when we can confidently find 16*67e74705SXin Li the function/method body corresponding to the call. (C functions, static 17*67e74705SXin Li functions, devirtualized C++ methods, Objective-C class methods, Objective-C 18*67e74705SXin Li instance methods when ExprEngine is confident about the dynamic type of the 19*67e74705SXin Li instance). 20*67e74705SXin Li 21*67e74705SXin Li -analyzer-config ipa=dynamic - Inline instance methods for which the type is 22*67e74705SXin Li determined at runtime and we are not 100% sure that our type info is 23*67e74705SXin Li correct. For virtual calls, inline the most plausible definition. 24*67e74705SXin Li 25*67e74705SXin Li -analyzer-config ipa=dynamic-bifurcate - Same as -analyzer-config ipa=dynamic, 26*67e74705SXin Li but the path is split. We inline on one branch and do not inline on the 27*67e74705SXin Li other. This mode does not drop the coverage in cases when the parent class 28*67e74705SXin Li has code that is only exercised when some of its methods are overridden. 29*67e74705SXin Li 30*67e74705SXin LiCurrently, -analyzer-config ipa=dynamic-bifurcate is the default mode. 31*67e74705SXin Li 32*67e74705SXin LiWhile -analyzer-config ipa determines in general how aggressively the analyzer 33*67e74705SXin Liwill try to inline functions, several additional options control which types of 34*67e74705SXin Lifunctions can inlined, in an all-or-nothing way. These options use the 35*67e74705SXin Lianalyzer's configuration table, so they are all specified as follows: 36*67e74705SXin Li 37*67e74705SXin Li -analyzer-config OPTION=VALUE 38*67e74705SXin Li 39*67e74705SXin Li### c++-inlining ### 40*67e74705SXin Li 41*67e74705SXin LiThis option controls which C++ member functions may be inlined. 42*67e74705SXin Li 43*67e74705SXin Li -analyzer-config c++-inlining=[none | methods | constructors | destructors] 44*67e74705SXin Li 45*67e74705SXin LiEach of these modes implies that all the previous member function kinds will be 46*67e74705SXin Liinlined as well; it doesn't make sense to inline destructors without inlining 47*67e74705SXin Liconstructors, for example. 48*67e74705SXin Li 49*67e74705SXin LiThe default c++-inlining mode is 'destructors', meaning that all member 50*67e74705SXin Lifunctions with visible definitions will be considered for inlining. In some 51*67e74705SXin Licases the analyzer may still choose not to inline the function. 52*67e74705SXin Li 53*67e74705SXin LiNote that under 'constructors', constructors for types with non-trivial 54*67e74705SXin Lidestructors will not be inlined. Additionally, no C++ member functions will be 55*67e74705SXin Liinlined under -analyzer-config ipa=none or -analyzer-config ipa=basic-inlining, 56*67e74705SXin Liregardless of the setting of the c++-inlining mode. 57*67e74705SXin Li 58*67e74705SXin Li### c++-template-inlining ### 59*67e74705SXin Li 60*67e74705SXin LiThis option controls whether C++ templated functions may be inlined. 61*67e74705SXin Li 62*67e74705SXin Li -analyzer-config c++-template-inlining=[true | false] 63*67e74705SXin Li 64*67e74705SXin LiCurrently, template functions are considered for inlining by default. 65*67e74705SXin Li 66*67e74705SXin LiThe motivation behind this option is that very generic code can be a source 67*67e74705SXin Liof false positives, either by considering paths that the caller considers 68*67e74705SXin Liimpossible (by some unstated precondition), or by inlining some but not all 69*67e74705SXin Liof a deep implementation of a function. 70*67e74705SXin Li 71*67e74705SXin Li### c++-stdlib-inlining ### 72*67e74705SXin Li 73*67e74705SXin LiThis option controls whether functions from the C++ standard library, including 74*67e74705SXin Limethods of the container classes in the Standard Template Library, should be 75*67e74705SXin Liconsidered for inlining. 76*67e74705SXin Li 77*67e74705SXin Li -analyzer-config c++-stdlib-inlining=[true | false] 78*67e74705SXin Li 79*67e74705SXin LiCurrently, C++ standard library functions are considered for inlining by 80*67e74705SXin Lidefault. 81*67e74705SXin Li 82*67e74705SXin LiThe standard library functions and the STL in particular are used ubiquitously 83*67e74705SXin Lienough that our tolerance for false positives is even lower here. A false 84*67e74705SXin Lipositive due to poor modeling of the STL leads to a poor user experience, since 85*67e74705SXin Limost users would not be comfortable adding assertions to system headers in order 86*67e74705SXin Lito silence analyzer warnings. 87*67e74705SXin Li 88*67e74705SXin Li### c++-container-inlining ### 89*67e74705SXin Li 90*67e74705SXin LiThis option controls whether constructors and destructors of "container" types 91*67e74705SXin Lishould be considered for inlining. 92*67e74705SXin Li 93*67e74705SXin Li -analyzer-config c++-container-inlining=[true | false] 94*67e74705SXin Li 95*67e74705SXin LiCurrently, these constructors and destructors are NOT considered for inlining 96*67e74705SXin Liby default. 97*67e74705SXin Li 98*67e74705SXin LiThe current implementation of this setting checks whether a type has a member 99*67e74705SXin Linamed 'iterator' or a member named 'begin'; these names are idiomatic in C++, 100*67e74705SXin Liwith the latter specified in the C++11 standard. The analyzer currently does a 101*67e74705SXin Lifairly poor job of modeling certain data structure invariants of container-like 102*67e74705SXin Liobjects. For example, these three expressions should be equivalent: 103*67e74705SXin Li 104*67e74705SXin Li std::distance(c.begin(), c.end()) == 0 105*67e74705SXin Li c.begin() == c.end() 106*67e74705SXin Li c.empty()) 107*67e74705SXin Li 108*67e74705SXin LiMany of these issues are avoided if containers always have unknown, symbolic 109*67e74705SXin Listate, which is what happens when their constructors are treated as opaque. 110*67e74705SXin LiIn the future, we may decide specific containers are "safe" to model through 111*67e74705SXin Liinlining, or choose to model them directly using checkers instead. 112*67e74705SXin Li 113*67e74705SXin Li 114*67e74705SXin LiBasics of Implementation 115*67e74705SXin Li----------------------- 116*67e74705SXin Li 117*67e74705SXin LiThe low-level mechanism of inlining a function is handled in 118*67e74705SXin LiExprEngine::inlineCall and ExprEngine::processCallExit. 119*67e74705SXin Li 120*67e74705SXin LiIf the conditions are right for inlining, a CallEnter node is created and added 121*67e74705SXin Lito the analysis work list. The CallEnter node marks the change to a new 122*67e74705SXin LiLocationContext representing the called function, and its state includes the 123*67e74705SXin Licontents of the new stack frame. When the CallEnter node is actually processed, 124*67e74705SXin Liits single successor will be a edge to the first CFG block in the function. 125*67e74705SXin Li 126*67e74705SXin LiExiting an inlined function is a bit more work, fortunately broken up into 127*67e74705SXin Lireasonable steps: 128*67e74705SXin Li 129*67e74705SXin Li1. The CoreEngine realizes we're at the end of an inlined call and generates a 130*67e74705SXin Li CallExitBegin node. 131*67e74705SXin Li 132*67e74705SXin Li2. ExprEngine takes over (in processCallExit) and finds the return value of the 133*67e74705SXin Li function, if it has one. This is bound to the expression that triggered the 134*67e74705SXin Li call. (In the case of calls without origin expressions, such as destructors, 135*67e74705SXin Li this step is skipped.) 136*67e74705SXin Li 137*67e74705SXin Li3. Dead symbols and bindings are cleaned out from the state, including any local 138*67e74705SXin Li bindings. 139*67e74705SXin Li 140*67e74705SXin Li4. A CallExitEnd node is generated, which marks the transition back to the 141*67e74705SXin Li caller's LocationContext. 142*67e74705SXin Li 143*67e74705SXin Li5. Custom post-call checks are processed and the final nodes are pushed back 144*67e74705SXin Li onto the work list, so that evaluation of the caller can continue. 145*67e74705SXin Li 146*67e74705SXin LiRetry Without Inlining 147*67e74705SXin Li---------------------- 148*67e74705SXin Li 149*67e74705SXin LiIn some cases, we would like to retry analysis without inlining a particular 150*67e74705SXin Licall. 151*67e74705SXin Li 152*67e74705SXin LiCurrently, we use this technique to recover coverage in case we stop 153*67e74705SXin Lianalyzing a path due to exceeding the maximum block count inside an inlined 154*67e74705SXin Lifunction. 155*67e74705SXin Li 156*67e74705SXin LiWhen this situation is detected, we walk up the path to find the first node 157*67e74705SXin Libefore inlining was started and enqueue it on the WorkList with a special 158*67e74705SXin LiReplayWithoutInlining bit added to it (ExprEngine::replayWithoutInlining). The 159*67e74705SXin Lipath is then re-analyzed from that point without inlining that particular call. 160*67e74705SXin Li 161*67e74705SXin LiDeciding When to Inline 162*67e74705SXin Li----------------------- 163*67e74705SXin Li 164*67e74705SXin LiIn general, the analyzer attempts to inline as much as possible, since it 165*67e74705SXin Liprovides a better summary of what actually happens in the program. There are 166*67e74705SXin Lisome cases, however, where the analyzer chooses not to inline: 167*67e74705SXin Li 168*67e74705SXin Li- If there is no definition available for the called function or method. In 169*67e74705SXin Li this case, there is no opportunity to inline. 170*67e74705SXin Li 171*67e74705SXin Li- If the CFG cannot be constructed for a called function, or the liveness 172*67e74705SXin Li cannot be computed. These are prerequisites for analyzing a function body, 173*67e74705SXin Li with or without inlining. 174*67e74705SXin Li 175*67e74705SXin Li- If the LocationContext chain for a given ExplodedNode reaches a maximum cutoff 176*67e74705SXin Li depth. This prevents unbounded analysis due to infinite recursion, but also 177*67e74705SXin Li serves as a useful cutoff for performance reasons. 178*67e74705SXin Li 179*67e74705SXin Li- If the function is variadic. This is not a hard limitation, but an engineering 180*67e74705SXin Li limitation. 181*67e74705SXin Li 182*67e74705SXin Li Tracked by: <rdar://problem/12147064> Support inlining of variadic functions 183*67e74705SXin Li 184*67e74705SXin Li- In C++, constructors are not inlined unless the destructor call will be 185*67e74705SXin Li processed by the ExprEngine. Thus, if the CFG was built without nodes for 186*67e74705SXin Li implicit destructors, or if the destructors for the given object are not 187*67e74705SXin Li represented in the CFG, the constructor will not be inlined. (As an exception, 188*67e74705SXin Li constructors for objects with trivial constructors can still be inlined.) 189*67e74705SXin Li See "C++ Caveats" below. 190*67e74705SXin Li 191*67e74705SXin Li- In C++, ExprEngine does not inline custom implementations of operator 'new' 192*67e74705SXin Li or operator 'delete', nor does it inline the constructors and destructors 193*67e74705SXin Li associated with these. See "C++ Caveats" below. 194*67e74705SXin Li 195*67e74705SXin Li- Calls resulting in "dynamic dispatch" are specially handled. See more below. 196*67e74705SXin Li 197*67e74705SXin Li- The FunctionSummaries map stores additional information about declarations, 198*67e74705SXin Li some of which is collected at runtime based on previous analyses. 199*67e74705SXin Li We do not inline functions which were not profitable to inline in a different 200*67e74705SXin Li context (for example, if the maximum block count was exceeded; see 201*67e74705SXin Li "Retry Without Inlining"). 202*67e74705SXin Li 203*67e74705SXin Li 204*67e74705SXin LiDynamic Calls and Devirtualization 205*67e74705SXin Li---------------------------------- 206*67e74705SXin Li 207*67e74705SXin Li"Dynamic" calls are those that are resolved at runtime, such as C++ virtual 208*67e74705SXin Limethod calls and Objective-C message sends. Due to the path-sensitive nature of 209*67e74705SXin Lithe analysis, the analyzer may be able to reason about the dynamic type of the 210*67e74705SXin Liobject whose method is being called and thus "devirtualize" the call. 211*67e74705SXin Li 212*67e74705SXin LiThis path-sensitive devirtualization occurs when the analyzer can determine what 213*67e74705SXin Limethod would actually be called at runtime. This is possible when the type 214*67e74705SXin Liinformation is constrained enough for a simulated C++/Objective-C object that 215*67e74705SXin Lithe analyzer can make such a decision. 216*67e74705SXin Li 217*67e74705SXin Li == DynamicTypeInfo == 218*67e74705SXin Li 219*67e74705SXin LiAs the analyzer analyzes a path, it may accrue information to refine the 220*67e74705SXin Liknowledge about the type of an object. This can then be used to make better 221*67e74705SXin Lidecisions about the target method of a call. 222*67e74705SXin Li 223*67e74705SXin LiSuch type information is tracked as DynamicTypeInfo. This is path-sensitive 224*67e74705SXin Lidata that is stored in ProgramState, which defines a mapping from MemRegions to 225*67e74705SXin Lian (optional) DynamicTypeInfo. 226*67e74705SXin Li 227*67e74705SXin LiIf no DynamicTypeInfo has been explicitly set for a MemRegion, it will be lazily 228*67e74705SXin Liinferred from the region's type or associated symbol. Information from symbolic 229*67e74705SXin Liregions is weaker than from true typed regions. 230*67e74705SXin Li 231*67e74705SXin Li EXAMPLE: A C++ object declared "A obj" is known to have the class 'A', but a 232*67e74705SXin Li reference "A &ref" may dynamically be a subclass of 'A'. 233*67e74705SXin Li 234*67e74705SXin LiThe DynamicTypePropagation checker gathers and propagates DynamicTypeInfo, 235*67e74705SXin Liupdating it as information is observed along a path that can refine that type 236*67e74705SXin Liinformation for a region. 237*67e74705SXin Li 238*67e74705SXin Li WARNING: Not all of the existing analyzer code has been retrofitted to use 239*67e74705SXin Li DynamicTypeInfo, nor is it universally appropriate. In particular, 240*67e74705SXin Li DynamicTypeInfo always applies to a region with all casts stripped 241*67e74705SXin Li off, but sometimes the information provided by casts can be useful. 242*67e74705SXin Li 243*67e74705SXin Li 244*67e74705SXin Li == RuntimeDefinition == 245*67e74705SXin Li 246*67e74705SXin LiThe basis of devirtualization is CallEvent's getRuntimeDefinition() method, 247*67e74705SXin Liwhich returns a RuntimeDefinition object. When asked to provide a definition, 248*67e74705SXin Lithe CallEvents for dynamic calls will use the DynamicTypeInfo in their 249*67e74705SXin LiProgramState to attempt to devirtualize the call. In the case of no dynamic 250*67e74705SXin Lidispatch, or perfectly constrained devirtualization, the resulting 251*67e74705SXin LiRuntimeDefinition contains a Decl corresponding to the definition of the called 252*67e74705SXin Lifunction, and RuntimeDefinition::mayHaveOtherDefinitions will return FALSE. 253*67e74705SXin Li 254*67e74705SXin LiIn the case of dynamic dispatch where our information is not perfect, CallEvent 255*67e74705SXin Lican make a guess, but RuntimeDefinition::mayHaveOtherDefinitions will return 256*67e74705SXin LiTRUE. The RuntimeDefinition object will then also include a MemRegion 257*67e74705SXin Licorresponding to the object being called (i.e., the "receiver" in Objective-C 258*67e74705SXin Liparlance), which ExprEngine uses to decide whether or not the call should be 259*67e74705SXin Liinlined. 260*67e74705SXin Li 261*67e74705SXin Li == Inlining Dynamic Calls == 262*67e74705SXin Li 263*67e74705SXin LiThe -analyzer-config ipa option has five different modes: none, basic-inlining, 264*67e74705SXin Liinlining, dynamic, and dynamic-bifurcate. Under -analyzer-config ipa=dynamic, 265*67e74705SXin Liall dynamic calls are inlined, whether we are certain or not that this will 266*67e74705SXin Liactually be the definition used at runtime. Under -analyzer-config ipa=inlining, 267*67e74705SXin Lionly "near-perfect" devirtualized calls are inlined*, and other dynamic calls 268*67e74705SXin Liare evaluated conservatively (as if no definition were available). 269*67e74705SXin Li 270*67e74705SXin Li* Currently, no Objective-C messages are not inlined under 271*67e74705SXin Li -analyzer-config ipa=inlining, even if we are reasonably confident of the type 272*67e74705SXin Li of the receiver. We plan to enable this once we have tested our heuristics 273*67e74705SXin Li more thoroughly. 274*67e74705SXin Li 275*67e74705SXin LiThe last option, -analyzer-config ipa=dynamic-bifurcate, behaves similarly to 276*67e74705SXin Li"dynamic", but performs a conservative invalidation in the general virtual case 277*67e74705SXin Liin *addition* to inlining. The details of this are discussed below. 278*67e74705SXin Li 279*67e74705SXin LiAs stated above, -analyzer-config ipa=basic-inlining does not inline any C++ 280*67e74705SXin Limember functions or Objective-C method calls, even if they are non-virtual or 281*67e74705SXin Lican be safely devirtualized. 282*67e74705SXin Li 283*67e74705SXin Li 284*67e74705SXin LiBifurcation 285*67e74705SXin Li----------- 286*67e74705SXin Li 287*67e74705SXin LiExprEngine::BifurcateCall implements the -analyzer-config ipa=dynamic-bifurcate 288*67e74705SXin Limode. 289*67e74705SXin Li 290*67e74705SXin LiWhen a call is made on an object with imprecise dynamic type information 291*67e74705SXin Li(RuntimeDefinition::mayHaveOtherDefinitions() evaluates to TRUE), ExprEngine 292*67e74705SXin Libifurcates the path and marks the object's region (retrieved from the 293*67e74705SXin LiRuntimeDefinition object) with a path-sensitive "mode" in the ProgramState. 294*67e74705SXin Li 295*67e74705SXin LiCurrently, there are 2 modes: 296*67e74705SXin Li 297*67e74705SXin Li DynamicDispatchModeInlined - Models the case where the dynamic type information 298*67e74705SXin Li of the receiver (MemoryRegion) is assumed to be perfectly constrained so 299*67e74705SXin Li that a given definition of a method is expected to be the code actually 300*67e74705SXin Li called. When this mode is set, ExprEngine uses the Decl from 301*67e74705SXin Li RuntimeDefinition to inline any dynamically dispatched call sent to this 302*67e74705SXin Li receiver because the function definition is considered to be fully resolved. 303*67e74705SXin Li 304*67e74705SXin Li DynamicDispatchModeConservative - Models the case where the dynamic type 305*67e74705SXin Li information is assumed to be incorrect, for example, implies that the method 306*67e74705SXin Li definition is overriden in a subclass. In such cases, ExprEngine does not 307*67e74705SXin Li inline the methods sent to the receiver (MemoryRegion), even if a candidate 308*67e74705SXin Li definition is available. This mode is conservative about simulating the 309*67e74705SXin Li effects of a call. 310*67e74705SXin Li 311*67e74705SXin LiGoing forward along the symbolic execution path, ExprEngine consults the mode 312*67e74705SXin Liof the receiver's MemRegion to make decisions on whether the calls should be 313*67e74705SXin Liinlined or not, which ensures that there is at most one split per region. 314*67e74705SXin Li 315*67e74705SXin LiAt a high level, "bifurcation mode" allows for increased semantic coverage in 316*67e74705SXin Licases where the parent method contains code which is only executed when the 317*67e74705SXin Liclass is subclassed. The disadvantages of this mode are a (considerable?) 318*67e74705SXin Liperformance hit and the possibility of false positives on the path where the 319*67e74705SXin Liconservative mode is used. 320*67e74705SXin Li 321*67e74705SXin LiObjective-C Message Heuristics 322*67e74705SXin Li------------------------------ 323*67e74705SXin Li 324*67e74705SXin LiExprEngine relies on a set of heuristics to partition the set of Objective-C 325*67e74705SXin Limethod calls into those that require bifurcation and those that do not. Below 326*67e74705SXin Liare the cases when the DynamicTypeInfo of the object is considered precise 327*67e74705SXin Li(cannot be a subclass): 328*67e74705SXin Li 329*67e74705SXin Li - If the object was created with +alloc or +new and initialized with an -init 330*67e74705SXin Li method. 331*67e74705SXin Li 332*67e74705SXin Li - If the calls are property accesses using dot syntax. This is based on the 333*67e74705SXin Li assumption that children rarely override properties, or do so in an 334*67e74705SXin Li essentially compatible way. 335*67e74705SXin Li 336*67e74705SXin Li - If the class interface is declared inside the main source file. In this case 337*67e74705SXin Li it is unlikely that it will be subclassed. 338*67e74705SXin Li 339*67e74705SXin Li - If the method is not declared outside of main source file, either by the 340*67e74705SXin Li receiver's class or by any superclasses. 341*67e74705SXin Li 342*67e74705SXin LiC++ Caveats 343*67e74705SXin Li-------------------- 344*67e74705SXin Li 345*67e74705SXin LiC++11 [class.cdtor]p4 describes how the vtable of an object is modified as it is 346*67e74705SXin Libeing constructed or destructed; that is, the type of the object depends on 347*67e74705SXin Liwhich base constructors have been completed. This is tracked using 348*67e74705SXin LiDynamicTypeInfo in the DynamicTypePropagation checker. 349*67e74705SXin Li 350*67e74705SXin LiThere are several limitations in the current implementation: 351*67e74705SXin Li 352*67e74705SXin Li- Temporaries are poorly modeled right now because we're not confident in the 353*67e74705SXin Li placement of their destructors in the CFG. We currently won't inline their 354*67e74705SXin Li constructors unless the destructor is trivial, and don't process their 355*67e74705SXin Li destructors at all, not even to invalidate the region. 356*67e74705SXin Li 357*67e74705SXin Li- 'new' is poorly modeled due to some nasty CFG/design issues. This is tracked 358*67e74705SXin Li in PR12014. 'delete' is not modeled at all. 359*67e74705SXin Li 360*67e74705SXin Li- Arrays of objects are modeled very poorly right now. ExprEngine currently 361*67e74705SXin Li only simulates the first constructor and first destructor. Because of this, 362*67e74705SXin Li ExprEngine does not inline any constructors or destructors for arrays. 363*67e74705SXin Li 364*67e74705SXin Li 365*67e74705SXin LiCallEvent 366*67e74705SXin Li========= 367*67e74705SXin Li 368*67e74705SXin LiA CallEvent represents a specific call to a function, method, or other body of 369*67e74705SXin Licode. It is path-sensitive, containing both the current state (ProgramStateRef) 370*67e74705SXin Liand stack space (LocationContext), and provides uniform access to the argument 371*67e74705SXin Livalues and return type of a call, no matter how the call is written in the 372*67e74705SXin Lisource or what sort of code body is being invoked. 373*67e74705SXin Li 374*67e74705SXin Li NOTE: For those familiar with Cocoa, CallEvent is roughly equivalent to 375*67e74705SXin Li NSInvocation. 376*67e74705SXin Li 377*67e74705SXin LiCallEvent should be used whenever there is logic dealing with function calls 378*67e74705SXin Lithat does not care how the call occurred. 379*67e74705SXin Li 380*67e74705SXin LiExamples include checking that arguments satisfy preconditions (such as 381*67e74705SXin Li__attribute__((nonnull))), and attempting to inline a call. 382*67e74705SXin Li 383*67e74705SXin LiCallEvents are reference-counted objects managed by a CallEventManager. While 384*67e74705SXin Lithere is no inherent issue with persisting them (say, in a ProgramState's GDM), 385*67e74705SXin Lithey are intended for short-lived use, and can be recreated from CFGElements or 386*67e74705SXin Linon-top-level StackFrameContexts fairly easily. 387