|
| 1 | +// Case generators for the parser fuzz lane (#1414). |
| 2 | +// |
| 3 | +// fast-check rather than a bespoke PRNG, for two reasons the lane depends on: a reported |
| 4 | +// counterexample is the SHRUNK input (a hand-rolled mutator reports the 20k-character random one), |
| 5 | +// and the hazard vocabulary is the one #1437's property suite already curates — a hazard added |
| 6 | +// there for a round-trip property reaches the fuzzer without being retyped here. |
| 7 | +// |
| 8 | +// Cases stay near the grammar's edge on purpose: a well-formed base with hostile chunks spliced in |
| 9 | +// reaches deep parser branches that uniformly random noise never gets past the first token of. |
| 10 | + |
| 11 | +import fc from 'fast-check'; |
| 12 | +import { |
| 13 | + replayScriptArb, |
| 14 | + SELECTOR_VALUE_HAZARDS, |
| 15 | + selectorChainArb, |
| 16 | +} from '../../src/__tests__/test-utils/property-arbitraries.ts'; |
| 17 | +import type { FuzzTarget, FuzzTargetName } from './target-types.ts'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Hazards a valid-input property cannot use — they exist to be *rejected*: structural JSON/YAML |
| 21 | + * punctuation, delimiter lookalikes, prototype-pollution keys, numeric edges, bidi/zero-width |
| 22 | + * controls. The shared list above carries the ones valid inputs must also survive. |
| 23 | + */ |
| 24 | +const REJECTION_HAZARDS = [ |
| 25 | + '`', |
| 26 | + '==', |
| 27 | + '&&', |
| 28 | + '--', |
| 29 | + '---', |
| 30 | + '#', |
| 31 | + ':', |
| 32 | + ',', |
| 33 | + '{', |
| 34 | + '}', |
| 35 | + '[', |
| 36 | + ']', |
| 37 | + '(', |
| 38 | + ')', |
| 39 | + '${', |
| 40 | + '${}', |
| 41 | + '@', |
| 42 | + '~=', |
| 43 | + '*', |
| 44 | + '\r\n', |
| 45 | + '\u0000', |
| 46 | + '\u200b', |
| 47 | + '\u202e', |
| 48 | + '\ufeff', |
| 49 | + '-0', |
| 50 | + 'NaN', |
| 51 | + 'Infinity', |
| 52 | + '1e999', |
| 53 | + '9007199254740993', |
| 54 | + 'null', |
| 55 | + 'undefined', |
| 56 | + '__proto__', |
| 57 | + 'constructor', |
| 58 | +] as const; |
| 59 | + |
| 60 | +const hazardArb: fc.Arbitrary<string> = fc.constantFrom( |
| 61 | + ...SELECTOR_VALUE_HAZARDS, |
| 62 | + ...REJECTION_HAZARDS, |
| 63 | +); |
| 64 | + |
| 65 | +/** A base string with 1–4 hazards spliced in at shrinkable positions. */ |
| 66 | +function corrupted(base: fc.Arbitrary<string>): fc.Arbitrary<string> { |
| 67 | + return fc |
| 68 | + .tuple( |
| 69 | + base, |
| 70 | + fc.array(fc.tuple(hazardArb, fc.nat({ max: 4096 })), { minLength: 1, maxLength: 4 }), |
| 71 | + ) |
| 72 | + .map(([text, edits]) => |
| 73 | + edits.reduce((current, [chunk, at]) => { |
| 74 | + const index = at % (current.length + 1); |
| 75 | + return current.slice(0, index) + chunk + current.slice(index); |
| 76 | + }, text), |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +/** A long run of one hazard — regex-backtracking and quadratic-scan bait. */ |
| 81 | +const repeatedHazardArb: fc.Arbitrary<string> = fc |
| 82 | + .tuple(hazardArb, fc.integer({ min: 50, max: 400 })) |
| 83 | + .map(([chunk, times]) => chunk.repeat(times)); |
| 84 | + |
| 85 | +/** Input that is not trying to look like the grammar at all. */ |
| 86 | +const noiseArb: fc.Arbitrary<string> = fc.oneof( |
| 87 | + fc.string({ maxLength: 40 }), |
| 88 | + fc.string({ unit: 'binary', maxLength: 40 }), |
| 89 | + fc.array(hazardArb, { maxLength: 8 }).map((parts) => parts.join('')), |
| 90 | + repeatedHazardArb, |
| 91 | +); |
| 92 | + |
| 93 | +/** |
| 94 | + * Bases borrowed from the property suite, which generates *valid* inputs: corrupting a |
| 95 | + * grammar-correct script or selector chain is how the fuzzer reaches branches past the first |
| 96 | + * rejection. Targets without one generate from their own seed list. |
| 97 | + */ |
| 98 | +const STRUCTURED_BASES: Partial<Record<FuzzTargetName, fc.Arbitrary<string>>> = { |
| 99 | + selector: selectorChainArb.map((chain) => chain.expression), |
| 100 | + 'replay-script': replayScriptArb, |
| 101 | + 'batch-steps': fc.json({ maxDepth: 3 }), |
| 102 | +}; |
| 103 | + |
| 104 | +/** The case distribution for one target: mostly near-miss, some valid, some pure noise. */ |
| 105 | +export function arbitraryForTarget(target: FuzzTarget): fc.Arbitrary<string> { |
| 106 | + const seeded = fc.constantFrom(...target.seeds); |
| 107 | + const structured = STRUCTURED_BASES[target.name]; |
| 108 | + const base = structured === undefined ? seeded : fc.oneof(seeded, structured); |
| 109 | + return fc.oneof( |
| 110 | + { weight: 6, arbitrary: corrupted(base) }, |
| 111 | + { weight: 2, arbitrary: base }, |
| 112 | + { weight: 2, arbitrary: noiseArb }, |
| 113 | + ); |
| 114 | +} |
0 commit comments