angr.analyses.decompiler.known_patterns.generator

PatternGenerator: build a KnownPattern from a decompilation text selection.

This is the inverse of KnownPatternFinder. Given a range of the generated C source (Decompiler.codegen) and a target call, it resolves the selection back to AIL statements/expressions and emits the corresponding DSL pattern:

  • an expression selection (a sub-expression of one statement) becomes a PatternExpr tree;

  • a straight-line statement selection (statements of one basic block) becomes a PStmtSeq;

  • a selection spanning several basic blocks becomes a PGraphPat (requires the finder’s ail_graph for the block/edge structure).

Text offsets are resolved through the codegen position maps (map_pos_to_node / ailexpr2cnode / cnode2ailexpr); the rendered AIL objects are .likes()-equal to the finder’s graph objects, so a generated pattern re-matches via KnownPatternFinder on the same ail_graph (the round-trip is the acceptance test).

angr.analyses.decompiler.known_patterns.generator.stmt_ins_addrs(stmt)

The instruction addresses a statement and its subexpressions carry.

Return type:

set[int]

Parameters:

stmt (Statement)

exception angr.analyses.decompiler.known_patterns.generator.PatternGenerationError

Bases: Exception

Raised when a selection cannot be turned into a KnownPattern (ambiguous, unsupported, or inconsistent with the given arguments).

class angr.analyses.decompiler.known_patterns.generator.PatternGenerator

Bases: object

Generate a KnownPattern from a text selection in codegen.text.

Parameters:
  • codegen – the Decompiler.codegen (a CStructuredCodeGenerator).

  • ail_graph (DiGraph | None) – the graph the generated pattern will be matched against (Decompiler.ail_graph). Required for multi-block (control-flow-spanning) selections; optional otherwise.

__init__(codegen, ail_graph=None)
Parameters:

ail_graph (networkx.DiGraph | None)

vvar_id_at(offset)

The AIL vvar id of the variable rendered at offset, or None.

Return type:

int | None

Parameters:

offset (int)

offset_of(needle, start=0)

Convenience: the offset of needle in the rendered text.

Return type:

int

Parameters:
generate(start_offset, end_offset, call_name, arg_offsets, *, name=None, display_name=None, returnty=None, param_types=None, const_arg_offsets=(), arches=None, platforms=None, binary_guard=None, default_enabled=False)
Return type:

KnownPattern

Parameters:
  • start_offset (int)

  • end_offset (int)

  • call_name (str)

  • arg_offsets (Sequence[int])

  • name (str | None)

  • display_name (str | None)

  • returnty (TypeRef | None)

  • param_types (Sequence[TypeRef | None] | None)

  • const_arg_offsets (Sequence[int])

  • arches (tuple[str, ...] | None)

  • platforms (tuple[str, ...] | None)

  • default_enabled (bool)

generate_pattern(start_offset, end_offset, call_name, *, name=None, display_name=None, returnty=None, param_types=None)

A pattern for the fuzzy matcher from the statements the selection covers.

Unlike generate(), this asks nothing of the user beyond the span: every whole statement inside it becomes a leaf, a statement the DSL cannot express becomes a PAnyStmt rather than an error, and the call’s parameters are the variables the statements read without defining, in the order they are first read. Constants stay exact; the editor is where they are loosened.

Return type:

KnownPattern

Parameters:
  • start_offset (int)

  • end_offset (int)

  • call_name (str)

  • name (str | None)

  • display_name (str | None)

  • returnty (TypeRef | None)

  • param_types (Sequence[TypeRef | None] | None)

generate_pattern_from_statements(stmts, call_name, *, name=None, display_name=None, returnty=None, param_types=None)

generate_pattern() for a caller that already holds the statements, in order.

Return type:

KnownPattern

Parameters:
  • stmts (Sequence[Statement])

  • call_name (str)

  • name (str | None)

  • display_name (str | None)

  • returnty (TypeRef | None)

  • param_types (Sequence[TypeRef | None] | None)