dex++ 0.3.1
C++ and C API reference
Loading...
Searching...
No Matches
dex::CallGraph Class Reference

Whole-program call graph for an AnalysisContext. More...

#include <analysis/call_graph.hpp>

Public Member Functions

 CallGraph ()=default
 
 CallGraph (const CallGraph &)=delete
 
CallGraph & operator= (const CallGraph &)=delete
 
 CallGraph (CallGraph &&)=default
 
CallGraph & operator= (CallGraph &&)=default
 
std::span< const CallNode > nodes () const
 
std::span< const CallEdge > edges () const
 
const CallNode * find (const MethodId &id) const
 O(1) lookup of a node by its canonical MethodId.
 
std::optional< Method > method_for (const CallNode &node) const
 For a Defined node, returns a Method handle for the defined method.
 
std::span< const uint32_t > dispatch_targets (uint32_t node) const
 Class-Hierarchy Analysis targets of a virtual / interface call declared against node index node: the definitions such a call may dispatch to at runtime besides node itself and its own upward resolution.
 
std::span< const uint32_t > dispatch_targets (const CallEdge &edge) const
 The dispatch targets that apply at one call site: dispatch_targets(edge.callee_node) for a Declared invoke-virtual / invoke-interface edge, and empty for every other edge — super, direct, static, polymorphic, and custom invokes never dispatch, and an InheritedResolution edge's site is covered by its Declared edge.
 
std::span< const uint32_t > dispatch_sources (uint32_t node) const
 The reverse of dispatch_targets(): the nodes whose dispatch targets include node index node — the declared targets through which a virtual / interface call can dispatch to it.
 
std::vector< uint32_t > callees (uint32_t node) const
 Every node that a call site in node index node may reach: each outgoing edge's callee plus the dispatch targets of each virtual / interface site.
 
std::vector< uint32_t > callers (uint32_t node) const
 Every node with a call site that may reach node index node: the callers on its incoming edges, plus the callers of every virtual / interface site declared against one of its dispatch_sources().
 
bool empty () const
 

Detailed Description

Whole-program call graph for an AnalysisContext.

Built lazily on first AnalysisContext::call_graph() access and cached on the underlying impl. Edges are stored in a single flat vector. Each invoke instruction yields exactly one declared-target edge (origin == Declared), uniquely traceable via its code_offset, plus at most one InheritedResolution edge sharing that offset and kind (see below).

Virtual dispatch uses Class-Hierarchy Analysis: an invoke-virtual / invoke-interface call site may also reach every distinct dispatch target among the declared class's loaded descendants — a descendant's own override, or, for descendants that don't override, the definition they inherit (which may live outside the declared class's subtree, e.g. class C extends Base implements I where Base provides I's method). Static / private / constructor name collisions are never dispatch targets and are resolved past. These targets depend only on the declared node, never on the call site, so they are not stored as edges: dispatch_targets() holds them once per declared node and every call site shares that list. (Stored per call site, a popular target such as Object.toString() would repeat its whole override list at every call — on large apps that is hundreds of millions of edges.) callees() / callers() give a node's neighbours with dispatch included.

When the declared class itself defines no matching method, the call site additionally gets one InheritedResolution edge to the nearest loaded definition, mirroring Dalvik resolution: the superclass chain first (nearest-first; an abstract redeclaration shadows concrete ancestors), then the transitive superinterfaces of every chain class, preferring default methods over abstract declarations. Applies to virtual, interface, super, and static invokes (static resolves through the superclass chain only — static interface methods are not inherited). If no loaded ancestor defines the method (e.g. the chain leaves the loaded set at a framework class), the declared node simply stays External and no edge is added.

Known divergences from runtime resolution, chosen for analysis usefulness: flag-mismatched shadows (e.g. a static hiding an instance method) are skipped rather than resolved-then-ICCE; conflicting maximally-specific default methods pick the BFS-nearest where the runtime throws; an abstract redeclaration that would raise AbstractMethodError still resolves to the nearest default/abstract definition; invoke-super resolves from the method_id's class (identical to the caller's superclass in compiler output); Object methods called through an interface, and array-receiver descriptors, stay External (Object is never loaded).

invoke-super never dispatches. invoke-polymorphic records the declared MethodHandle target only — distinct call-site shapes through the same MethodHandle.invoke collapse into one node. An invoke-custom site points at a CallSite node, one for each call site of each DEX file.

A class defined in more than one loaded DEX file (or twice in one) follows Android's rule: the first definition wins, in load order (multidex order for an APK: classes.dex, classes2.dex, ...). Only that definition's methods are Defined nodes, method_for() returns that definition's method, and only its code adds edges; the methods and code of a later definition are left out, as Android never loads them. This is the definition AnalysisContext::find_class() returns, and the class hierarchy and the cross-references follow the same rule.

Move-only: copying a graph would deep-copy all node/edge vectors.

Constructor & Destructor Documentation

◆ CallGraph() [1/3]

dex::CallGraph::CallGraph ( )
default

◆ CallGraph() [2/3]

dex::CallGraph::CallGraph ( const CallGraph &  )
delete

◆ CallGraph() [3/3]

dex::CallGraph::CallGraph ( CallGraph &&  )
default

Member Function Documentation

◆ operator=() [1/2]

CallGraph & dex::CallGraph::operator= ( const CallGraph &  )
delete

◆ operator=() [2/2]

CallGraph & dex::CallGraph::operator= ( CallGraph &&  )
default

◆ nodes()

std::span< const CallNode > dex::CallGraph::nodes ( ) const
inline

◆ edges()

std::span< const CallEdge > dex::CallGraph::edges ( ) const
inline

◆ find()

const CallNode * dex::CallGraph::find ( const MethodId &  id) const

O(1) lookup of a node by its canonical MethodId.

Returns nullptr if no node carries that id.

◆ method_for()

std::optional< Method > dex::CallGraph::method_for ( const CallNode &  node) const

For a Defined node, returns a Method handle for the defined method.

Returns nullopt for External and CallSite nodes. Precondition: node must be a member of this graph's nodes() span.

◆ dispatch_targets() [1/2]

std::span< const uint32_t > dex::CallGraph::dispatch_targets ( uint32_t  node) const

Class-Hierarchy Analysis targets of a virtual / interface call declared against node index node: the definitions such a call may dispatch to at runtime besides node itself and its own upward resolution.

Every target is a Defined node; sorted by node index. Empty when no invoke-virtual / invoke-interface site declares node, when no loaded descendant changes the dispatch, or when node is out of range.

◆ dispatch_targets() [2/2]

std::span< const uint32_t > dex::CallGraph::dispatch_targets ( const CallEdge &  edge) const

The dispatch targets that apply at one call site: dispatch_targets(edge.callee_node) for a Declared invoke-virtual / invoke-interface edge, and empty for every other edge — super, direct, static, polymorphic, and custom invokes never dispatch, and an InheritedResolution edge's site is covered by its Declared edge.

◆ dispatch_sources()

std::span< const uint32_t > dex::CallGraph::dispatch_sources ( uint32_t  node) const

The reverse of dispatch_targets(): the nodes whose dispatch targets include node index node — the declared targets through which a virtual / interface call can dispatch to it.

Sorted by node index; empty when node is out of range.

◆ callees()

std::vector< uint32_t > dex::CallGraph::callees ( uint32_t  node) const

Every node that a call site in node index node may reach: each outgoing edge's callee plus the dispatch targets of each virtual / interface site.

Deduplicated and sorted by node index; empty when node is out of range.

◆ callers()

std::vector< uint32_t > dex::CallGraph::callers ( uint32_t  node) const

Every node with a call site that may reach node index node: the callers on its incoming edges, plus the callers of every virtual / interface site declared against one of its dispatch_sources().

Deduplicated and sorted by node index; empty when node is out of range.

◆ empty()

bool dex::CallGraph::empty ( ) const
inline

The documentation for this class was generated from the following file: