Skip to content

test: add parser fuzzing and discriminator injectivity checks - #324

Merged
senzenn merged 1 commit into
mainfrom
test/parser-robustness
Sep 16, 2026
Merged

senzenn merged 1 commit into
mainfrom
test/parser-robustness

Conversation

@senzenn

@senzenn senzenn commented Sep 13, 2026

Copy link
Copy Markdown
Collaborator

Two properties the generated parsers already have, with nothing asserting them. No
production code changes.

parser_robustness.rs drives every parse entry point with buffers the IDL never described:
every prefix of a valid one including empty, and valid ones with bytes appended. The
contract is return Ok or Err, never panic, which in Rust also rules out an over-read since
a slice past the end aborts. Truncation is the shape that slips through, because a guard
that checks data.len() == N and then slices data[..M] only fails between M and N, which a
happy-path test misses. Random buffers come from a seeded xorshift64* so a CI failure
replays from the seed, and drive returns a tally of outcomes, since "never panicked" is
also true of a corpus that bounced off the first length check every time.

discriminator_injectivity_tests proves no two differently-keyed instructions can both match
one buffer. Equal keys are already handled: the renderer groups them and disambiguates on
account count. Unequal keys get one arm each, tried in order, so an overlap there is decided
by emission order rather than by anything the IDL states. jointly_satisfiable compares two
keys byte-wise over the intersection of their windows, and treats non-overlapping windows as
satisfiable, which is the case that matters. The check runs over the whole corpus rather than
up to a bound.

joint_satisfiability_detects_real_overlap pins the helper on hand-built keys (same window
different bytes, disjoint windows, overlapping and agreeing, overlapping and disagreeing, a
size discriminator against a window past its end), since a helper that always answered no
would make the corpus check pass on any input.
same_key_collision_groups_are_accounted_for pins the four IDLs that do rely on account-count
disambiguation:

colliding_envelope
collision_safe_cpi_event_envelope
macro_arg_envelope_collision
raydium_amm_v4_with_swapv2

so a fifth arrives as a deliberate change.

Neither check depends on the codama bump; both hold on 0.9.1. It sits on #323 to keep the
stack linear.

Tested: 378 passed, 0 failed. clippy -Dwarnings and fmt --check clean.

@senzenn
senzenn force-pushed the test/parser-robustness branch from e8b2d4c to 6ec3380 Compare September 13, 2026 11:46
@senzenn
senzenn marked this pull request as ready for review September 13, 2026 12:08
@senzenn
senzenn force-pushed the ci/pin-codama-version branch from 3a41ab1 to 86f44ab Compare September 14, 2026 11:22
@senzenn
senzenn force-pushed the test/parser-robustness branch from 6ec3380 to 562a47f Compare September 14, 2026 11:22
@senzenn
senzenn force-pushed the ci/pin-codama-version branch from 86f44ab to d6670fa Compare September 14, 2026 11:34
@senzenn
senzenn force-pushed the test/parser-robustness branch from 562a47f to cb10ae7 Compare September 14, 2026 11:34
@senzenn
senzenn force-pushed the ci/pin-codama-version branch from d6670fa to 908e025 Compare September 14, 2026 16:49
@senzenn
senzenn force-pushed the test/parser-robustness branch 2 times, most recently from a4bf29b to cb2052e Compare September 14, 2026 16:55
@senzenn
senzenn force-pushed the ci/pin-codama-version branch from 908e025 to e2ef2e4 Compare September 14, 2026 18:04
@senzenn
senzenn force-pushed the test/parser-robustness branch from cb2052e to 02abaca Compare September 14, 2026 18:04
@senzenn
senzenn changed the base branch from ci/pin-codama-version to main September 16, 2026 16:03
Two properties the generated parsers already have, with nothing asserting them. No
production code changes.

parser_robustness.rs drives every parse entry point with buffers the IDL never described:
every prefix of a valid one including empty, and valid ones with bytes appended. The
contract is return Ok or Err, never panic, which in Rust also rules out an over-read since
a slice past the end aborts. Truncation is the shape that slips through, because a guard
that checks data.len() == N and then slices data[..M] only fails between M and N.

It drives two IDLs. discriminator_guards alone is not enough: its zero-width account arm
matches data.get(0..0) for every buffer and returns before any later arm is tried, so the
one arm behind it that slices a real window is unreachable and the account side is never
truncated at all. truncation_guards has no zero-width arm, and its windows, 8 bytes at
offset 0 and 4 at offset 8, are what exercise that path. Reverting the account renderer's
data.get(offset..end) to an unchecked index fails only the test that drives the second
fixture; the other five pass, which is the reason it exists.

Random buffers come from a seeded xorshift64* so a CI failure replays from the seed. Half
the random corpus is primed with a valid discriminator, because uniformly random bytes
satisfy a 4-byte one with probability 2^-32, so an unprimed corpus never reaches the body
decoder and the tally proves only that the first compare rejected everything. The tally
records which account arms were reached rather than counting successes, since a count
cannot separate real coverage from every buffer tripping one unconditional arm.

pump_fun.rs gets the same loop against a real Anchor IDL. That binary already expands
pump_fun.json, so it adds no compile time, and it reaches what the synthetic fixtures
cannot: six accounts with a real 8-byte discriminator and no zero-width or size-only arm,
and instructions declaring real account lists, so a short account vector reaches a
generated accessor.

discriminator_injectivity_tests proves no two differently-keyed instructions can both match
one buffer. Equal keys are already handled: the renderer groups them and disambiguates on
account count. Unequal keys get one arm each, tried in order, so an overlap there is decided
by emission order rather than by anything the IDL states. The check runs over the whole
corpus. joint_satisfiability_detects_real_overlap pins the helper on hand-built keys, since
one that always answered no would make the corpus check pass on any input, and
same_key_collision_groups_are_accounted_for pins the four IDLs that do rely on account-count
disambiguation so a fifth arrives as a deliberate change.

Both corpus walks now report a fixture that stops loading instead of skipping it. They
read every IDL in tests/idls, so a silent skip would let most of the corpus fall out
while the tests stayed green. They also share one helper: the directory walk, the
extension filter, the load and the key extraction were duplicated verbatim between
them, and only what each does with the keys differs.

Neither check depends on the codama bump; both hold on 0.9.1.
@senzenn
senzenn force-pushed the test/parser-robustness branch from 02abaca to c485108 Compare September 16, 2026 16:51
@senzenn
senzenn merged commit 5766c10 into main Sep 16, 2026
8 checks passed
@senzenn
senzenn deleted the test/parser-robustness branch September 16, 2026 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants