angr.analyses.decompiler.known_patterns.finder

KnownPatternFinder: find occurrences of KnownPatterns in a Clinic AIL graph and outline them into calls via the Outliner analysis.

exception angr.analyses.decompiler.known_patterns.finder.UnsupportedOutlineError

Bases: Exception

Raised when a match cannot be safely outlined (e.g. side-effecting statements interleave with the matched span, or the recovered callee interface does not agree with the pattern’s declared parameters).

class angr.analyses.decompiler.known_patterns.finder.KnownPatternMatch

Bases: object

One occurrence of a KnownPattern in an AIL graph.

For expression-level matches, the boundary is consumed_stmt_idxs (statements wholly owned by the match, e.g. chased definitions) plus expr_path (the location of the matched sub-expression inside the anchor statement). For statement-sequence matches, stmt_span holds the ordered matched statement indices (the anchor is the first) and matched_expr is None.

duplicated_stmt_idxs holds chased definitions that are copied into the outlined region instead of moved: the originals stay where they are (other code still uses them) and the copies make the pattern’s captured values the region’s live-ins. It is always disjoint from consumed_stmt_idxs.

recomputed_defs does the same for definitions that live in another block (a CSE’d value, possibly reaching the anchor through a phi): the expression is pure, so the region recomputes it from the captured operands.

pattern: KnownPattern
block_loc: tuple[int, int]
anchor_stmt_idx: int
expr_path: tuple[tuple[str, int | None], ...]
consumed_stmt_idxs: frozenset[int]
captures: dict[str, Expression]
matched_expr: Expression | None
duplicated_stmt_idxs: frozenset[int] = frozenset({})
recomputed_defs: tuple[tuple[int, Expression], ...] = ()
base_aliases: tuple[tuple[int, int, int], ...] = ()
stmt_span: tuple[int, ...] | None = None
block_map: dict[str, tuple[int, int]] | None = None
consumed_by_block: dict[tuple[int, int], frozenset[int]] | None = None
frontier_locs: frozenset[tuple[int, int]] | None = None
__init__(pattern, block_loc, anchor_stmt_idx, expr_path, consumed_stmt_idxs, captures, matched_expr, duplicated_stmt_idxs=frozenset({}), recomputed_defs=(), base_aliases=(), stmt_span=None, block_map=None, consumed_by_block=None, frontier_locs=None)
Parameters:
Return type:

None

class angr.analyses.decompiler.known_patterns.finder.OutlineResult

Bases: object

The outcome of outlining one KnownPatternMatch.

graph: DiGraph
match: KnownPatternMatch
call_stmt: Statement
child_func: Function | None
child_graph: DiGraph | None
child_funcargs: list[VirtualVariable]
__init__(graph, match, call_stmt, child_func, child_graph, child_funcargs)
Parameters:
Return type:

None

class angr.analyses.decompiler.known_patterns.finder.KnownPatternFinder

Bases: Analysis

Finds occurrences of KnownPatterns in a Clinic AIL graph.

The graph to match on is the final (SSA-form) Clinic graph, i.e. decompiler.ail_graph / clinic.cc_graph. Matches are reported in self.matches; outline() turns a match into a synthesized call by invoking the Outliner analysis on a copy of the graph.

MAX_REMOTE_CHASE_DEPTH = 6
__init__(func, ail_graph, patterns=None, chase_defs=True, chase_remote_defs=True, skip_conversions=True, vvar_id_start=48879, block_addr_start=2864381952, ail_manager=None, force_patterns=None)

patterns may be pattern templates (KnownPatternTemplate) and/or already-concrete KnownPatterns; templates are instantiated for this binary’s PatternContext. When None, the registry is used, filtered down to the templates that are enabled for this target: default-on ones, ones named by force_patterns, and ones whose gate opens (see gating).

force_patterns is the user’s force-enable selection, "all" or an iterable of template names / call names, and is only consulted when patterns is None (an explicit pattern list is already a selection).

Parameters:
matches: list[KnownPatternMatch]
call_target_names(call)

Public face of _resolve_call_target(), for the outliner’s clean-up of calls a pattern declared pure.

Return type:

frozenset[str]

Parameters:

call (Call)

apply_matches(matches, ail_graph)

Apply as many of matches as possible, batching where it matters.

Applying one match at a time is fine until several of them interleave in one block, which is exactly what consecutive field swaps do:

t1 = x->b; t2 = y->b; y->b = t1; x->b = t2;

^ the next field’s load sits in this one’s gap

Rewrite the first and the second’s statements have moved; rewrite it anyway and the third now has an opaque call sitting in its gap, which no disjointness test will let it move across. So the void statement-span matches of a block, which the finder has already checked do not conflict, are spliced in together, against the statement indices they were found at. Everything else keeps the one-at-a-time path.

Returns the new graph and the ids of the matches that were applied.

Return type:

tuple[DiGraph, set[int]]

Parameters:
outline(match, ail_graph=None)

Outline one match: split the anchor block so the matched span forms its own block, invoke the Outliner analysis on it, and rewrite the synthesized callsite into the pattern’s call. Operates on (and returns) a copy; neither self._graph nor ail_graph is mutated.

Return type:

OutlineResult

Parameters:
MAX_LIFTED_EXPR_NODES = 8

Node budget for a lifted parameter expression; see _is_liftable.

outline_at(location, pattern, ail_graph)

Find pattern at location ((block_addr, block_idx) or (block_addr, block_idx, stmt_idx)) in ail_graph and outline it.

Return type:

OutlineResult

Parameters: