> Documentation version: 0.8.0. [HTML page](https://lukewilliamboswell.github.io/roc-fuzz/internals.html)

<a id="understand-roc-fuzz-internals-and-advanced-features"></a>

# Understand roc-fuzz internals

This chapter explains how the runner executes a target, how Roc resolves
` generator_for `, the binary input format behind saved witnesses, and the
low-level libFuzzer escape hatch. Application authors do not need this material
for the normal target workflow.

<a id="understand-the-in-process-model"></a>

## Understand the in-process model

The embedded libFuzzer calls a target thousands of times in the same
process. It does not start a clean executable for every generated input.
This is what makes it fast. Roc’s purity means target calls cannot leak
application state or background work into later calls, and determinism
makes saved inputs reliable with the same target and generator.

A reliable target still needs all of the following properties:

- Invalid or unusual inputs return ordinary values such as ` Err `, rather
  than triggering an intentional assertion or process exit.

- One input normally finishes in less than 10 milliseconds.


- Inputs and allocations have sensible upper bounds.


- Every input terminates.


The runner is a poor fit when one test takes a significant fraction of a
second, can grow without a practical memory bound, or is expected to
terminate the process for invalid input. If a crash, timeout, or memory
blow-up would be a bug, it remains a useful fuzzing failure.

The runner evaluates the application’s ` target ` once in a process, before any
input, and holds the result until the process exits. Work done in the target
expression, outside the property, is done once in a process; work done in the
property is done for every input. What that expression allocates is outside
every [allocation measurement](observations.md#measure-allocations-and-validate-traces).

A saved entropy input produces the same typed value and result when the
target and generator are unchanged. Encoded examples use the binary parser and
the same property instead. Keep the executable alongside
important failure artifacts when exact historical reproduction matters.

<a id="understand-generator-dispatch"></a>

## Understand generator dispatch

` generator_for ` uses Roc’s ordinary static dispatch. It is a method
associated with the target’s input type, not a runtime interface,
registry, or callback table. Package APIs can require their own methods
with ` where ` clauses, so the method does not need to be a
compiler-defined name.

The pattern deliberately mirrors ` Json.parser_for `:

- ` Json.parse ` requires its result type to have a compatible
  ` parser_for ` method and asks that type for a JSON parser.

- ` Fuzz.target ` requires its input type to have a compatible
  ` generator_for ` method and asks that type for a fuzz generator.

- The checker resolves both to a specific method implementation at
  compile time. The compiled call has no dynamic-dispatch overhead.

Conceptually, ` Fuzz.target ` has this requirement:

``` roc
target : { name : Str, examples : List(Example(a)), test : a -> Outcome, show : a -> Str } -> Target
    where [a.generator_for : FuzzEncoding -> Generator(a)]
```

` FuzzEncoding ` currently contains the ` Default ` policy. Passing a policy
value follows the same shape as ` JsonEncoding ` and leaves room for
additional generation policies without changing the method pattern.

Use ` Fuzz.target ` when generation is part of a reusable named input
type. Use ` Fuzz.target_with ` with an explicit ` Generator(a) ` for a local
structural input or when choosing between generators for the same type
in different targets.

<a id="binary-inputs-and-witness-identity"></a>

## Binary inputs and witness identity

Structural values use the platform-owned binary format through Roc’s existing
` encoder_for ` and ` parser_for ` derivation. Nominal inputs opt in with both methods.
` Fuzz.encode(value) ` creates a versioned encoded-value input; ` Fuzz.entropy(bytes) `
frames generator entropy. Source examples are encoded into actual mutable seeds.
Host preparation copies bytes and releases temporary Roc values before running
an input. Encoding does not invert arbitrary ` Fuzz.map ` functions.

The frame is ` RFZ `, version byte ` 1 `, form byte (` 0 ` entropy, ` 1 ` value), then
payload. Bare historical bytes without the reserved ` RFZ ` prefix remain entropy.
Artifacts whose interpretation changes across platform versions need their
original executable; cross-version corpus migration is outside this API.
Unsupported versions, malformed values and exceeded decoding budgets reject.
The encoded-value parser allows at most 1 MiB, 4096 value nodes and 128 total
containers, validating counts before allocation.

Generation chooses bias. Preconditions or validated types define validity for
both forms; a range enforced only by a generator cannot protect encoded values.
Logical encoding is not a storage snapshot. Preserve original entropy when
aliasing, capacity or slices are part of a witness. ` retain ` copies original
bytes, then prints a saved declaration and a decoded representative.

Float encoding follows Roc’s floating-point value semantics. Do not rely on
NaN payloads surviving compiler operations; use integer bit inputs for
payload-sensitive properties. With the October 27 compiler, dictionary keys
that are structural records or tuples can crash compiler specialization during
codec derivation. ` --specialize=no ` avoids that compiler limitation; ordinary
scalar and tag keys work with the default backend.

<a id="use-the-libfuzzer-interface"></a>

## Use the libFuzzer interface

` TARGET raw [LIBFUZZER_ARG...] ` passes arguments directly to the
embedded libFuzzer CLI. Run this for its complete option list:

``` sh
TARGET raw -help=1
```

Prefer ` run `, ` show `, ` replay `, ` minimize `, and ` reduce-corpus ` for the
normal workflow. Low-level flags such as ` -max_total_time ` and
` -max_len ` belong behind ` raw `; ` run ` accepts the stable
[friendly options](campaigns.md#command-reference).
` raw ` is an escape hatch for libFuzzer features the friendly command layer does
not yet expose directly.

<a id="further-reading"></a>

## Further reading

- [LLVM libFuzzer documentation](https://llvm.org/docs/LibFuzzer.html)
  covers the in-process execution model, options, corpus behavior, output,
  and FAQ.

