# `MaeBPF.IR`
[🔗](https://github.com/cocoa-xu/MaeBPF/blob/v0.1.0/maebpf/lib/maebpf/ir.ex#L1)

Typed intermediate representation (IR) and static safety checker for `MaeBPF`.

A `%MaeBPF.IR.Module{}` describes a single eBPF ELF object containing one or
more programs (`:xdp`, `{:tcx, :ingress | :egress}`,
`{:tracepoint, "category/name"}`) sharing module-level BPF maps.

Before lowering to eBPF ISA v3, `normalize/1` and `validate/1`:
  * Auto-register a hidden `PERCPU_ARRAY` drop counter
    (`"__maebpf_rb_drops_<stream>"`, `key_size: 4`, `value_size: 8`,
    `max_entries: 1`) for every declared `:ringbuf` stream.
  * Enforce the 512-byte kernel eBPF stack frame limit per program.
  * Verify map references, key/value sizes, `LPM_TRIE` `BPF_F_NO_PREALLOC`,
    port ranges, CIDR prefixes, and instruction/branch budgets.
  * Compute minimal shared packet header parsing requirements (`:eth`,
    `:ipv4`, `:udp`, `:tcp`) for shared bounds-check fusion.

# `allowlist_map`

Constructs a `%MaeBPF.IR.MapDef{}` for an IPv4 or reversed-FQDN domain
allowlist (symmetric to `blocklist_map/2`, tagged with `meta.role = :allowlist`).

# `analyze_headers`

Analyzes a `%MaeBPF.IR.Program{}` to determine which packet header parsing
checks (`:eth`, `:ipv4`, `:udp`, `:tcp`) are required so the compiler can
emit shared bounds checks once at the prologue.

# `blocklist_map`

Constructs a `%MaeBPF.IR.MapDef{}` for an IPv4 (`:lru_hash`, `:hash`,
`:lpm_ipv4`) or reversed-FQDN domain (`:lpm_domain`) blocklist.

# `bpf_f_no_prealloc`

Returns the `BPF_F_NO_PREALLOC` flag constant (`1`).

# `counter_map`

Constructs a `%MaeBPF.IR.MapDef{}` for a counter map (`:percpu_array` by
default).

# `debug_drop_counter_name`

Returns the canonical hidden debug RingBuf drop counter map name.

# `debug_ringbuf_name`

Returns the canonical hidden debug RingBuf map name (`"__maebpf_debug_rb"`).

# `default_c_name`

Derives the default ELF function symbol name for a program hook and name.

# `drop_counter_id`

Returns the hidden drop counter map ID atom for a `:ringbuf` stream.

# `drop_counter_name`

Returns the canonical hidden `PERCPU_ARRAY` drop counter map name for a
`:ringbuf` stream name.

# `estimate_budget`

Estimates instruction and branch counts for static budget validation.

# `flatten_ops`

Flattens a list of IR ops including nested `:scope` inner ops.

# `format_error`

Formats a single IR validation error term into a human-readable message.

# `map_type_id`

Returns the kernel `bpf_map_type` integer for a supported IR map kind.

# `max_stack_bytes`

Returns the maximum eBPF stack frame size in bytes (`512`).

# `normalize`

Normalizes a `%MaeBPF.IR.Module{}`:
  * Fills missing `name`, `section`, `c_name`, and `LPM_TRIE` `BPF_F_NO_PREALLOC`.
  * Assigns stable 1-based `plug_id` indices per program when unset.
  * Auto-injects hidden `"__maebpf_rb_drops_<stream>"` `PERCPU_ARRAY` maps
    for every declared `:ringbuf` stream.
  * Auto-injects hidden `"__maebpf_sample_cnt"` `PERCPU_ARRAY` when any op
    uses sampling (`every > 1`) or `debug_trace` uses `sample > 1`.

# `pid_filter_enabled?`

Returns `true` when `program` has tracepoint PID filtering enabled.

# `pid_filter_map`

Constructs the hidden `HASH` map definition (`4B u32 PID -> 4B u32`) used to
filter tracepoint invocations to selected OS PIDs.

# `pid_filter_map_name`

Returns the canonical hidden tracepoint OS PID filter `HASH` map name.

# `sample_counter_name`

Returns the canonical hidden per-CPU sampling sequence counter map name.

# `section_for_hook`

Derives the canonical libbpf ELF section name for a program hook.

# `sni_flows_map`

Constructs the hidden 5-tuple `LRU_HASH` flow verdict cache (`key_size: 12`,
`value_size: 4`) used by `MaeBPF.Plugs.TLSSNI`.

# `sni_flows_map_name`

Returns the canonical hidden `LRU_HASH` 5-tuple flow cache map name used by `:tls_sni`.

# `sni_scratch_map`

Constructs the hidden `PERCPU_ARRAY` scratch buffer map (`value_size: 2304`,
`max_entries: 1`) used by `MaeBPF.Plugs.TLSSNI`.

# `sni_scratch_map_name`

Returns the canonical hidden `PERCPU_ARRAY` scratch map name used by `:tls_sni`.

# `stack_bytes`

Computes exact kernel stack bytes required by `program` within `ir_module`.

# `stream_map`

Constructs a `%MaeBPF.IR.MapDef{}` for a RingBuf event stream.

# `validate`

Validates a `%MaeBPF.IR.Module{}` statically.

Options:
  * `:max_stack_bytes` - default `512`
  * `:max_instructions` - default `4096`
  * `:max_branches` - default `512`
  * `:debug_trace` - validate including debug-trace overhead

Returns `{:ok, normalized_ir}` or `{:error, [reason, ...]}`.

# `validate!`

Like `validate/2`, returning the normalized `%MaeBPF.IR.Module{}` or raising
`ArgumentError` with a detailed validation report.

# `with_debug_trace`

Returns a copy of `ir` configured for live zero-downtime debug tracing.

Injects the hidden `"__maebpf_debug_rb"` `:ringbuf` stream, its hidden
`"__maebpf_rb_drops___maebpf_debug_rb"` drop counter, and sets
`meta[:debug_trace]` and `meta[:debug_sample]`.

# `with_pid_filter`

Returns a copy of `ir` with tracepoint PID filtering enabled on all
tracepoint programs, injecting the hidden `"__maebpf_pid_filter"` `HASH` map.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
