# Differential fuzzing (/docs/operations/differential-fuzzing)

> shapekit fuzz feeds generated and mutated XDR values through two decoders and reports every input on which they disagree.

`shapekit fuzz` feeds generated and mutated XDR values through two decoders and reports every
input on which they disagree:

- **rust**: `stellar-xdr` 30.0.0, linked into `shapekit` and called in process the way core
  calls it, `T::from_xdr(bytes, limits)` then `to_xdr(limits)`.
- **go**: `go-stellar-sdk` at the `current` version of `[sdk.go]` in
  `conformance/versions.toml` (`--go-version` picks another), through its conformance testee
  and the `decode_encode` op of the [testee protocol](/docs/sdk/testee-protocol).

The nightly workflow runs 60,000,000 inputs with the UTC date as the seed (`--seconds 2400` is
only a safety stop), then replays every stored reproducer. It goes beyond what the RFP asks
for.

```sh
shapekit fuzz --seconds 1200 --seed 20261112 --jobs 4
shapekit fuzz --replay findings/fuzz          # run every stored reproducer again
```

## Why not cargo-fuzz [#why-not-cargo-fuzz]

cargo-fuzz 0.13.2 with libfuzzer-sys 0.4.13 (both listed in `pins.toml`) runs on the pinned
1.99.1 toolchain when the sanitizer is turned off (`cargo fuzz run -s none`), and
`shapekit native-fuzz --target rust` uses it that way on stellar-xdr alone. It is not the
engine of this differential run, for two reasons:

- libFuzzer's feedback is coverage of the process it runs in. Half of every comparison happens
  in the go testee, a separate process, so coverage would steer towards new paths in
  `stellar-xdr` only and say nothing about go. `shapekit guided-fuzz` (below) steers by the
  coverage of every testee process instead.
- Every input costs a pipe round trip to go either way, so the in-process speed libFuzzer is
  built for is not available.

So the fuzzer is an ordinary subcommand on the pinned toolchain. Values come from
`stellar-xdr`'s own `arbitrary` feature (arbitrary 1.5.0) over bytes from a seeded generator,
and from xdr-aware mutation of corpus entries. Every class records the seed, worker index and
iteration of its input. A whole run is reproducible from `--seed` for any `--jobs` when you
give it `--max-inputs` and it finishes before `--seconds` runs out: each worker then has a
fixed share of the inputs, and classes are merged in a fixed order. `report.json` says `reproducible: true` in that
case; timing goes to `timing.json` so it does not affect the report.

## Inputs [#inputs]

Types: every type both testees decode. The go testee's type table is generated from the corpus
manifest, so this is the manifest's xdr types: `TransactionEnvelope`, `LedgerCloseMeta`,
`TransactionResultMeta`, `TransactionResultMetaV1`, `LedgerEntry`, `ContractEvent`, `ScVal`,
`SorobanAuthorizationEntry`, `ScSpecFunctionV0`, `Asset`, `SignerKey`. `--types` narrows it.

Seeds: every corpus `kind = xdr` entry of those types up to `--max-seed-bytes` (64 KiB, which
leaves out only the largest files, such as the 100-operation envelope), plus the contract arguments and auth entries inside
envelopes and the key and value of contract data entries, so the small types get real seeds.

Each iteration picks a type, then one of:

| origin | what |
|---|---|
| `seed-mutation` | a corpus seed with one to four mutations |
| `arbitrary` | `T::arbitrary` over 1 to 4096 generated bytes, encoded without limits |
| `arbitrary-mutation` | the same, then mutated |
| `pool-mutation` | an input both sides accepted earlier in the run, mutated again |

The mutations work on 4-byte words because XDR lengths, counts and discriminants are
big-endian words: set a word to a boundary value (0, 1, 2, `0x7fffffff`, `0x80000000`,
`0xffffffff` and others), set a length word near the number of bytes after it, nudge a word by
a small delta, delete or repeat a run of words, insert zero or random words, truncate, splice
words from another input of the same type, flip a bit, set a byte to a boundary value, append
bytes.

## Limits [#limits]

Both sides get the corpus decode limits from `crates/shapekit-manifest/src/depth.rs`: depth
1000 (`xdr.envelope.depth`), 1012 for `LedgerCloseMeta`, and the input length as the length
limit. The go testee honours both (`decode_limits: "honoured"`).

## What counts as a disagreement [#what-counts-as-a-disagreement]

| class kind | meaning |
|---|---|
| `rust-accepts-go-rejects` | rust decodes the whole input, go refuses it |
| `go-accepts-rust-rejects` | the other way round |
| `reencode-differs` | both decode, the re-encoded bytes differ |
| `rust-panic`, `go-panic` | a decoder crashed instead of refusing |
| `go-failure` | the go testee timed out, died or broke the protocol |

Both refusing, for whatever reasons, is agreement. Both accepting with the same bytes is
agreement even when those bytes differ from the input.

Disagreements are grouped into classes by a signature: the type, the kind, rust's error
variant without its payload, and go's innermost `decoding <Type>: ...` message with numbers
and quoted values blanked. go wraps its error once per enclosing type, so without the trim one
refusal reached through two paths would be two classes. The class id is
`fuzz.<type>-<kind>-<first 8 hex of sha256(signature)>`.

## The depth-accounting filter [#the-depth-accounting-filter]

go-stellar-sdk spends `MaxDepth` once per generated `DecodeFrom` call; rs-stellar-xdr spends
its depth once per `read_xdr` of every type, including the wrappers and boxes go does not
count. At the same number the two refuse at different nesting, and go reads deeper: at depth
1000, go decodes a bare `ScVal` holding 499 `ScVec` levels. This is already known and recorded (`conformance/known-failures/go.txt`, the `depth` entries,
filed as [go-stellar-sdk#6047](https://github.com/stellar/go-stellar-sdk/issues/6047)), so it is
filtered rather than reported again.

A disagreement is filtered when, and only when, lifting the refusing side's depth limit, and
nothing else, makes that side accept with exactly the bytes the other side produced:

- `depth-accounting-go-deeper`: rust refused with `DepthLimitExceeded`, go accepted, and rust
  at depth `u32::MAX` with the same length limit accepts and re-encodes to go's bytes.
- `depth-accounting-go-shallower`: go refused with `maximum decoding depth reached`, rust
  accepted, and go at depth `u32::MAX` accepts and re-encodes to rust's bytes.

Anything else about the input, including a depth refusal combined with a second difference,
stays a disagreement. The report counts filtered inputs per type.

## Reproducers [#reproducers]

At the end of the run every class is shrunk while it stays in the same class: delta debugging
over aligned word runs, then trailing bytes, then each remaining word set to 0 or 1. Each class
gets a directory in the corpus vector layout:

- `entry.json`: a manifest entry record (`kind: xdr`, `synthetic: true`, family `fuzz`),
  checked against the manifest's own record type before it is written.
- `any.xdr`: the minimised bytes.
- `class.json`: the signature, both verdicts, the run seed and where the class was first seen.

Every class goes to `<out>/reproducers/`; classes not in `findings/fuzz/known-classes.txt` are
also written under `findings/fuzz/`, which is where triaged reproducers are kept.

The expected verdict in `entry.json` follows core where core fixes it:

| rust on the reproducer | type | expected |
|---|---|---|
| accepts, re-encodes other bytes | any | implementation-defined, not-emittable: not canonical, no conforming encoder writes it |
| accepts and round-trips | `TransactionEnvelope` | must-accept: core's decode is stellar-xdr at depth 1000 |
| accepts and round-trips | other | implementation-defined: core produces these types and never decodes untrusted bytes of them |
| rejects | `TransactionEnvelope` | must-reject, invalid-by-core: from protocol 28 core refuses what its rust parser cannot read (`can_parse_transaction`) |
| rejects | other | implementation-defined |

core decodes an envelope with its own decoder, so stellar-xdr's verdict on bytes it does not
round-trip is not core's verdict, and such inputs are left implementation-defined.

A reproducer is a finding, not a corpus entry. When a class is real and new, add a synthetic
entry for it to `crates/shapekit-gen/src/synth_xdr.rs` (where the build recomputes the
expectation with stellar-xdr), draft the issue under `findings/issues/`, and list the class in
`findings/fuzz/known-classes.txt` with the entry and draft.

## Known classes and the exit status [#known-classes-and-the-exit-status]

`findings/fuzz/known-classes.txt` lists triaged classes, one `<class-id> <issue or reason>` per
line. A run exits non-zero when it sees a class that is not listed; listed classes are counted
and reported. `--replay` exits non-zero when a stored reproducer no longer lands in its class,
which is how a fix upstream shows up.

## Output [#output]

`<out>/report.json` and `<out>/FUZZ.md` (posted to the nightly job summary): inputs and rate,
per type the number of seeds, inputs, both-accept, both-reject, filtered and disagreeing
inputs, the inputs per origin, and every class with its count, reproducer and both verdicts.

## First run (2026-11-12) [#first-run-2026-11-12]

The first run used seed 20261112 against go-stellar-sdk v0.8.0. Apart from the inputs filtered
as depth accounting, every disagreement (113,063 inputs) was one root cause in eight classes,
one per type that carries a bool: stellar-xdr reads a bool word other than 0 or 1 (as false), go-stellar-sdk
refuses it. Core's own decoder, xdrpp, reads the same word as true and re-encodes it as 1, and
core hashes and checks signatures against that re-encoding, never the submitted bytes. The
minimal reproducer is the `ScVal` `00000000 00000002`. The corpus already holds it as
`soroban.scval-shapes.synth-bool-value-2`, and in an envelope as
`soroban.invoke-basics.synth-invoke-arg-bool-2`; it is filed as
[rs-stellar-xdr#629](https://github.com/stellar/rs-stellar-xdr/issues/629) and
[stellar-core#5509](https://github.com/stellar/stellar-core/issues/5509). The eight classes, each with its
reproducer under `findings/fuzz/`, are the classes `findings/fuzz/known-classes.txt` lists.

## Coverage-guided search across the three sdks [#coverage-guided-search-across-the-three-sdks]

`shapekit guided-fuzz` runs the coverage builds of the rust, go and javascript testees
(`target/testees-cov/`, the builds `conformance run --coverage` uses) side by side, with
stellar-xdr in process as the oracle, and sends every input to all three. After each input
every testee answers `coverage_new` from its live counters (see the
[testee protocol](/docs/sdk/testee-protocol#coverage_new)), so the search knows at once whether the
input reached code no input before it reached, in any of the three. Such an input joins one
pool the three share: an input that opened a new path in go is mutated further and sent to
rust and javascript too.

```sh
shapekit guided-fuzz --seed 1 --max-inputs 100000 --compare
```

The corpus seeds and the pool form one list per type. With the feedback an entry is picked
with a weight that grows with the inputs mutated from it that grew coverage and shrinks with
the times it was picked, so new entries are tried soon and worn-out ones rarely. A generated
input keeps the raw bytes `arbitrary` built it from, and mutating those yields another
well-formed value close to it. Without the feedback, seeds are picked uniformly and nothing
joins the pool; a quarter of the inputs is generated in both runs.

`--compare` runs the same seed and input budget once with and once without the feedback, and
`<out>/REPORT.md` counts per testee the ranges of `coverage_snapshot` each run reached, the
inputs per source and how many of them grew coverage, and every disagreement class with
stellar-xdr. The depth-accounting filter above applies to all three testees.

### Measured (2026-11-17) [#measured-2026-11-17]

Against stellar-xdr 30.0.0, go-stellar-sdk v0.9.0 and @stellar/stellar-sdk 17.4.0, 100,000
inputs per run (about 17 minutes each). The full reports are
[conformance/guided-fuzz/seed-1.md](https://github.com/ErenTopaal/shapekit/blob/main/conformance/guided-fuzz/seed-1.md) and
[seed-2.md](https://github.com/ErenTopaal/shapekit/blob/main/conformance/guided-fuzz/seed-2.md).

| seed | testee | ranges with feedback | ranges without |
|---|---|---|---|
| 1 | rust | 16,128 | 16,010 |
| 1 | go | 6,529 | 6,446 |
| 1 | javascript | 4,106 | 4,086 |
| 2 | rust | 16,100 | 16,009 |
| 2 | go | 6,532 | 6,444 |
| 2 | javascript | 4,086 | 4,081 |

With the feedback every testee reached more ranges in both seeds; go and rust gained the most.
The inputs follow from the seed: both seeds, run once before and once after the go testee's
type table was regenerated, gave the same pools (1,190 and 1,196 entries) and the same rust
and javascript counts; go's count rose by the 3 ranges the new go testee code reaches at
startup.
The gain needs a budget well past the number of seeds: at 10,000 inputs the two seeds tried
split (seed 1 gained 68 rust and 29 javascript ranges and tied in go, seed 2 lost 76, 12 and
17), because uniform seed mutation still finds new code on almost every seed at that point.
Every disagreement class in all four runs is the bool word class above (rs-stellar-xdr#629),
seen from go and from javascript; a class signature cuts a long javascript path, so two of
them look different in the table and are the same refusal of a bool word other than 0 or 1.

## Each SDK's own fuzzer [#each-sdks-own-fuzzer]

`shapekit native-fuzz` runs each SDK's own coverage-guided fuzzer on its decoder, with the
encoding laws of the law library as the check:

| `--target` | SDK | Fuzzer | Target |
|---|---|---|---|
| `rust` | stellar-xdr 30.0.0 | cargo-fuzz 0.13.2 (libFuzzer), stable toolchain, `-s none` | `testees/rust/fuzz` |
| `go` | go-stellar-sdk v0.9.0 | `go test -fuzz` | `testees/go/fuzz_test.go` |
| `js` | @stellar/stellar-sdk 17.4.0 | jazzer.js 4.0.0 | `testees/js/fuzz/xdr-laws.cjs` |

```sh
shapekit native-fuzz --target rust --seconds 600 --out target/native-fuzz-out/rust
```

The three targets read one input format: a type selector byte, then the XDR. They name a
broken law the same way, as a class:

- `<type>:roundtrip:<in>-><out>`: decoding and encoding again changes the bytes;
- `<type>:noncanonical:longer` or `<type>:noncanonical:shorter`: the decoder accepts bytes that
  are not the canonical encoding;
- `<type>:panic`: the decoder crashes.

The corpus files up to `--max-seed-bytes` (64 KiB) are the seeds. A fuzzer stops at the first
failing input. The runner records the input with its class, adds the class to the target's skip
list and starts the fuzzer again on the time left, so one run finds several classes. Each class
found is minimised by the fuzzer's own tool and checked once more on its own. stellar-xdr then
judges it under the corpus limits, and it is written to the candidate lane (`--candidate`,
default `candidate/`) as a synthetic XDR entry.

Classes listed in
[conformance/native-fuzz/known-classes.txt](https://github.com/ErenTopaal/shapekit/blob/main/conformance/native-fuzz/known-classes.txt) are
skipped from the start of a run, so a run looks for others. The list holds the eight rust
classes of the bool word root cause (rs-stellar-xdr#629), one per fuzzed type that carries a
bool. `<out>/report.json` gives the run's seconds, seeds, restarts, the skipped classes and
every class found.

The `native-fuzz.yml` workflow runs the three targets for 600 seconds each, daily at 04:30 UTC.
A new class does not fail the run: it is listed in the job summary and uploaded with the
candidate lane, as a finding to triage.

### Measured (2026-11-17) [#measured-2026-11-17-1]

600 seconds per target, one target at a time, on a shared 10-core machine. The load average
column is the 1-minute load sampled every 30 seconds during the run.

| Target | Inputs run | Seeds | Restarts | New classes | Load average (min, mean, max) |
|---|---|---|---|---|---|
| rust | 36,116,732 | 1,107 | 1 | 0 | 10.0, 16.3, 21.5 |
| go | 5,827,586 | 1,107 | 1 | 0 | 22.7, 36.3, 44.1 |
| js | 4,591,179 | 1,107 | 1 | 0 | 34.2, 39.4, 53.0 |

No run found a class outside the known list.