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

The entry point for analysis. More...

#include <analysis/analysis_context.hpp>

Public Member Functions

std::vector< Class > classes () const
 All classes across every loaded DEX file, in file / definition order.
 
std::vector< std::string_view > strings () const
 Every string in every loaded DEX's string pool, in (dex, string-index) order — including strings no bytecode loads (type/member names, debug data, etc.).
 
std::optional< Class > find_class (std::string_view descriptor) const
 Locate a class by its 'L...;' descriptor; std::nullopt if not present.
 
const CallGraph & call_graph () const
 Whole-program call graph for every defined method across all loaded DEX files.
 
const ClassHierarchy & class_hierarchy () const
 Whole-program class hierarchy: inheritance and interface-implementation relationships across all loaded DEX files, plus descriptor-only nodes for every external supertype referenced.
 
const Xrefs & xrefs () const
 Whole-program cross-reference index: call sites by target method, field reads/writes, const-string loads, and type references.
 

Static Public Member Functions

static std::expected< AnalysisContext, Error > from_dex (const std::string &path)
 Parse a single DEX file from disk.
 
static std::expected< AnalysisContext, Error > from_dex_files (const std::vector< std::string > &paths)
 Parse multiple DEX files (e.g. multi-dex splits) in one context.
 
static std::expected< AnalysisContext, Error > from_apk (const std::string &path, const ApkLoadOptions &options={})
 Open an APK (ZIP) and load every classes*.dex inside it, in multidex order (classes.dex, classes2.dex, classes3.dex, ...).
 
static std::expected< AnalysisContext, Error > from_apk_buffer (std::span< const uint8_t > apk_bytes, const std::string &label="<apk>", const ApkLoadOptions &options={})
 Build a context from an in-memory APK (ZIP) image.
 

Detailed Description

The entry point for analysis.

AnalysisContext owns one or more parsed DEX files and exposes a high-level API for querying classes, methods, and fields. It is the root object that all Class / Method / Field / ClassRef handles point back to.

Internally the context uses a shared_ptr to a private Impl, making copy cheap (shared ownership) and the object safe to return from factory functions by value. Handles obtained from this context keep their own shared_ptr to the Impl, so they remain valid even after every AnalysisContext wrapper has been destroyed.

Member Function Documentation

◆ from_dex()

static std::expected< AnalysisContext, Error > dex::AnalysisContext::from_dex ( const std::string &  path)
static

Parse a single DEX file from disk.

Returns
AnalysisContext on success. On failure, an Error with code FileNotFound if the path does not exist, CannotRead if it cannot be read (a directory, no permission), or InvalidDex if the file is not a DEX file it can parse.

◆ from_dex_files()

static std::expected< AnalysisContext, Error > dex::AnalysisContext::from_dex_files ( const std::vector< std::string > &  paths)
static

Parse multiple DEX files (e.g. multi-dex splits) in one context.

Returns on the first failure, with the same error codes as from_dex().

◆ from_apk()

static std::expected< AnalysisContext, Error > dex::AnalysisContext::from_apk ( const std::string &  path,
const ApkLoadOptions &  options = {} 
)
static

Open an APK (ZIP) and load every classes*.dex inside it, in multidex order (classes.dex, classes2.dex, classes3.dex, ...).

If a name appears more than once in the archive, only its first entry is loaded.

Returns
AnalysisContext on success. On failure, an Error with code FileNotFound if the path does not exist, CannotRead if it cannot be read, InvalidApk if it is not a ZIP, an entry is damaged or there is no classes.dex, and InvalidDex if an embedded DEX is malformed. With several bad entries, the error is the one for the first in multidex order, whatever options.threads is.

◆ from_apk_buffer()

static std::expected< AnalysisContext, Error > dex::AnalysisContext::from_apk_buffer ( std::span< const uint8_t >  apk_bytes,
const std::string &  label = "<apk>",
const ApkLoadOptions &  options = {} 
)
static

Build a context from an in-memory APK (ZIP) image.

Used by the apk layer (dex::apk::Apk) so an already-loaded archive is not re-read from disk. label only prefixes error messages. The error codes are those of from_apk(), without the file ones.

◆ classes()

std::vector< Class > dex::AnalysisContext::classes ( ) const

All classes across every loaded DEX file, in file / definition order.

A class defined in more than one DEX file is listed once per definition; find_class() says which definition the analysis uses.

◆ strings()

std::vector< std::string_view > dex::AnalysisContext::strings ( ) const

Every string in every loaded DEX's string pool, in (dex, string-index) order — including strings no bytecode loads (type/member names, debug data, etc.).

This is the complete pool; Xrefs::referenced_strings() is the subset reachable from const-string instructions.

Decodes and caches the whole pool on demand (an explicitly expensive call); the returned views are backed by per-DEX caches and stay valid for the lifetime of the AnalysisContext. A string present in more than one DEX appears once per DEX.

◆ find_class()

std::optional< Class > dex::AnalysisContext::find_class ( std::string_view  descriptor) const

Locate a class by its 'L...;' descriptor; std::nullopt if not present.

When several loaded DEX files define the class, the first definition wins: the one in the earliest DEX file, in load order (multidex order for an APK: classes.dex, classes2.dex, ...). Android does the same: an app's class loader searches its DEX files in that order and stops at the first one that defines the class, so a later definition is never loaded. The call graph, the class hierarchy and the cross-references use this same definition and ignore the others.

◆ call_graph()

const CallGraph & dex::AnalysisContext::call_graph ( ) const

Whole-program call graph for every defined method across all loaded DEX files.

Built lazily on first access and cached; repeated calls return the same reference. With several DEX files, part of the build runs on one thread per DEX file (see ApkLoadOptions::threads; contexts loaded from DEX files use the automatic setting). The result does not depend on the number of threads.

◆ class_hierarchy()

const ClassHierarchy & dex::AnalysisContext::class_hierarchy ( ) const

Whole-program class hierarchy: inheritance and interface-implementation relationships across all loaded DEX files, plus descriptor-only nodes for every external supertype referenced.

Built lazily and cached.

◆ xrefs()

const Xrefs & dex::AnalysisContext::xrefs ( ) const

Whole-program cross-reference index: call sites by target method, field reads/writes, const-string loads, and type references.

Built lazily on first access and cached; repeated calls return the same reference.


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