Artifactspublic-v0

ply

main
ply4 git calls347 ms42 KiB2 cachedprotocol v2
134 lines11 KiBRaw
1// High-volume differential fuzz of the pure codecs. No git server; just node:zlib / node:crypto as oracles for
2// inflate and SHA-1, and git's own patch_delta (via `git index-pack --fix-thin` on a one-delta pack) for deltas.
3// node test/fuzz/codec.mjs [--n 200000] [--seed 1]
4import { execFileSync } from 'node:child_process';
5import { closeSync, mkdtempSync, openSync, rmSync, writeFileSync } from 'node:fs';
6import { tmpdir } from 'node:os';
7import { join } from 'node:path';
8import zlib from 'node:zlib';
9import crypto from 'node:crypto';
10import { Rng, splitRandom } from './prng.js';
11import { inflate } from '../../src/inflate.js';
12import { Sha1 } from '../../src/sha1.js';
13import { applyDelta, spliceDelta, makeDelta, buildPack, parsePack, resolvePack } from '../../src/pack.js';
14import { pktInit, pktStep, pkt } from '../../src/pktline.js';
15import { concat } from '../../src/bytes.js';
16import { zlibBound } from '../../src/format.js';
17import { unwrap } from '../unwrap.js';
19const args = Object.fromEntries(process.argv.slice(2).map((a, i, arr) => a.startsWith('--') ? [a.slice(2), arr[i + 1] ?? true] : []).filter(x => x.length));
20const N = parseInt(args.n ?? '200000', 10), SEED = parseInt(args.seed ?? String(Date.now() % 1e9), 10);
21// --oracle-timeout-s is a whole number of seconds, 1 or more. To execFileSync a timeout of 0 means none, and a
22// negative or non-numeric one is a RangeError on every oracle call: each is a usage error here, before anything runs.
23const ORACLE_TIMEOUT_S = String(args['oracle-timeout-s'] ?? '120');
24if (!/^\d+$/.test(ORACLE_TIMEOUT_S) || Number(ORACLE_TIMEOUT_S) < 1 || !Number.isSafeInteger(Number(ORACLE_TIMEOUT_S) * 1000)) {
25 console.error(`usage: --oracle-timeout-s takes a whole number of seconds, 1 or more; got ${JSON.stringify(args['oracle-timeout-s'])}`);
26 process.exit(2);
28// The seed is printed on start and in every FAIL line, with the command that replays it: with no --seed it comes
29// from the clock, and a failure nobody can replay is lost. One stream is drawn from the seed, so case #i is the
30// (i+1)th case of `--n <i+1> --seed <seed>`.
31const REPLAY = 'node --import ./test/ts-resolve.mjs test/fuzz/codec.mjs';
32console.log(`codec: seed ${SEED}, n ${N} replay: ${REPLAY} --n ${N} --seed ${SEED}`);
33const rng = new Rng(SEED);
34const fails = [], counts = {};
35const tick = k => counts[k] = (counts[k] ?? 0) + 1;
36const t0 = Date.now();
38// data generators biased toward what deflate cares about: runs, repeats at every distance class, random, structured
39function gen() {
40 const kind = rng.weighted([['random', 3], ['run', 2], ['repeat', 3], ['text', 2], ['mixed', 2], ['empty', 1], ['one', 1]]);
41 tick(`data.${kind}`);
42 const n = rng.weighted([[rng.int(0, 64), 3], [rng.int(64, 4096), 3], [rng.int(4096, 70000), 2], [rng.int(70000, 300000), 1]]);
43 switch (kind) {
44 case 'empty': return new Uint8Array(0);
45 case 'one': return Uint8Array.of(rng.int(0, 255));
46 case 'random': return rng.bytes(n);
47 case 'run': return new Uint8Array(n).fill(rng.int(0, 255));
48 case 'repeat': { const d = rng.pick([1, 2, 3, 4, 5, 7, 8, 9, 13, 31, 32, 33, 257, 258, 259, 1024, 4096, 32767, 32768, 32769, rng.int(1, 32768)]); const pat = rng.bytes(Math.min(d, n || 1)); const out = new Uint8Array(n); for (let i = 0; i < n; i++) out[i] = pat[i % pat.length]; return out; }
49 case 'text': { const w = ['the', 'inflate', 'huffman', 'delta', 'pack', '\n', ' ', 'git', 'object', 'stream']; let s = ''; while (s.length < n) s += rng.pick(w); return new TextEncoder().encode(s.slice(0, n)); }
50 case 'mixed': { const parts = []; let len = 0; while (len < n) { const p = rng.bool() ? rng.bytes(rng.int(1, 2000)) : new Uint8Array(rng.int(1, 2000)).fill(rng.int(0, 255)); parts.push(p); len += p.length; } return concat(parts).subarray(0, n); }
51 }
54// a git repo for delta judging: one-delta thin packs against a stored base
55const dir = mkdtempSync(join(tmpdir(), 'gw-codec-'));
56execFileSync('git', ['-C', dir, 'init', '-q']);
57// The git oracle. Its stdin is a regular file, never a spawnSync `input` pipe: on node v24.18.0 / macOS under load,
58// execFileSync('git', ['hash-object', '-w', '--stdin'], { input }) with a 250 kB input sometimes never returns (node
59// waits in kevent inside SyncProcessRunner, git waits in read(0)). That happens with no ply code loaded (5 of about
60// 27 000 calls, all five at 250 kB), and it hung this fuzzer without judging anything. Every oracle call also has a
61// timeout: a git that does not exit is killed and the case fails saying so, instead of hanging the run.
62const ORACLE_TIMEOUT_MS = Number(ORACLE_TIMEOUT_S) * 1000;
63const feed = `${dir}.stdin`;
64class OracleHang extends Error {}
65function git(args, stdin = null) {
66 let fd = null;
67 if (stdin !== null) { writeFileSync(feed, stdin); fd = openSync(feed, 'r'); }
68 try { return execFileSync('git', ['-C', dir, ...args], { stdio: [fd ?? 'ignore', 'pipe', 'pipe'], timeout: ORACLE_TIMEOUT_MS, killSignal: 'SIGKILL' }); }
69 catch (e) { if (e.code === 'ETIMEDOUT') throw new OracleHang(`oracle did not answer: git ${args.join(' ')} had not exited after ${ORACLE_TIMEOUT_MS / 1000} s and was killed (nothing was judged)`); throw e; }
70 finally { if (fd !== null) closeSync(fd); }
72const gitHash = (data) => git(['hash-object', '-w', '--stdin'], data).toString().trim();
74for (let i = 0; i < N; i++) {
75 const which = rng.weighted([['inflate', 6], ['sha1', 1], ['delta', 2], ['delta-git', 0.3], ['pkt', 1]]);
76 try {
77 if (which === 'inflate') {
78 const d = gen();
79 const level = rng.int(-1, 9), strategy = rng.pick([0, 0, 0, 1, 2, 3, 4]), memLevel = rng.int(1, 9), windowBits = rng.pick([9, 10, 12, 15, 15, 15]);
80 const z = zlib.deflateSync(d, { level, strategy, memLevel, windowBits });
81 tick(`inflate.level.${level}`); tick(`inflate.strategy.${strategy}`); tick(`inflate.wbits.${windowBits}`);
82 const pre = rng.bytes(rng.int(0, 3)), post = rng.bytes(rng.int(0, 3));
83 const buf = concat([pre, z, post]);
84 if (z.length > zlibBound(d.length)) throw new Error(`zlib emitted ${z.length} B for ${d.length} B, over zlibBound ${zlibBound(d.length)} (level ${level}, strategy ${strategy}, memLevel ${memLevel}, windowBits ${windowBits})`);
85 tick('inflate.under_zlib_bound');
86 const r = unwrap(inflate(buf, pre.length, rng.bool() ? { expectedSize: d.length } : {}));
87 if (r.consumed !== z.length) throw new Error(`consumed ${r.consumed} != ${z.length}`);
88 if (r.out.length !== d.length || !Buffer.from(r.out).equals(Buffer.from(d))) throw new Error('content mismatch');
89 // truncation at a random point must throw (never return success, never hang)
90 if (z.length > 2 && rng.bool(0.3)) { const cut = rng.int(1, z.length - 1); let ok = false; try { unwrap(inflate(z.subarray(0, cut))); ok = true; } catch { } if (ok) throw new Error(`truncated at ${cut}/${z.length} returned success`); tick('inflate.truncated'); }
91 // single bit flip in the body must throw or produce a different output (adler catches) — never silently equal output with wrong consumed
92 if (z.length > 8 && rng.bool(0.2)) { const m = z.slice(); const at = rng.int(2, z.length - 5); m[at] ^= 1 << rng.int(0, 7); let r2 = null; try { r2 = unwrap(inflate(m)); } catch { tick('inflate.flip.rejected'); } if (r2 && Buffer.from(r2.out).equals(Buffer.from(d)) && r2.consumed === z.length) tick('inflate.flip.benign'); else if (r2) throw new Error('bit flip accepted with different output (adler should catch)'); }
93 } else if (which === 'sha1') {
94 const d = gen(); const h = new Sha1(); for (const c of splitRandom(rng, d, { maxChunks: 50 })) h.update(c);
95 if (h.hex() !== crypto.createHash('sha1').update(d).digest('hex')) throw new Error('sha1 mismatch');
96 } else if (which === 'delta' || which === 'delta-git') {
97 const base = gen(); const off = rng.int(0, base.length), oldLen = rng.int(0, base.length - off); const ins = rng.bool(0.3) ? new Uint8Array(0) : gen().subarray(0, rng.int(0, 70000));
98 const target = concat([base.subarray(0, off), ins, base.subarray(off + oldLen)]);
99 // git refuses a delta whose result IS its base ("REF_DELTA already resolved (duplicate base)"): identical content must
100 // never be sent as a delta. editFile returns noop before building a pack; the generator just skips it here.
101 if (Buffer.from(target).equals(Buffer.from(base))) { tick('delta.identity-skipped'); continue; }
102 const d1 = spliceDelta({ baseLen: base.length, off, oldLen, insert: ins }).delta, d2 = makeDelta(base, target).delta;
103 for (const [name, delta] of [['splice', d1], ['make', d2]]) {
104 if (delta.length < 4) { tick('delta.min-size'); continue; }
105 const r = unwrap(applyDelta(base, delta)); if (!Buffer.from(r.out).equals(Buffer.from(target))) throw new Error(`${name} apply mismatch`);
106 if (which === 'delta-git' && base.length > 0) {
107 const baseOid = gitHash(base);
108 const { pack } = await buildPack([{ type: 3, data: delta, base: { tag: 'ref', oid: baseOid } }]);
109 try { git(['index-pack', '--fix-thin', '--strict', '--stdin'], pack); }
110 catch (e) { if (e instanceof OracleHang) throw e; throw new Error(`${name}: git rejected delta (base ${base.length}B off=${off} old=${oldLen} ins=${ins.length} delta=${delta.length}B): ${(e.stderr || '').toString().trim().split('\n').pop()}`); }
111 const targetOid = crypto.createHash('sha1').update(`blob ${target.length}\0`).update(target).digest('hex');
112 const got = git(['cat-file', 'blob', targetOid]);
113 if (!got.equals(Buffer.from(target))) throw new Error(`${name}: git patch_delta produced different bytes`);
114 tick('delta.git-judged');
115 }
116 }
117 tick(`delta.copyops.${Math.min(5, Math.ceil(off / 65536) + Math.ceil((base.length - off - oldLen) / 65536))}`);
118 } else if (which === 'pkt') {
119 const frames = Array.from({ length: rng.int(0, 40) }, () => rng.weighted([[{ kind: 'data', payload: rng.bytes(rng.pick([0, 1, 100, 65516, rng.int(0, 65516)])) }, 6], [{ kind: 'flush' }, 2], [{ kind: 'delim' }, 1], [{ kind: 'end' }, 1]]));
120 const enc = concat(frames.map(f => f.kind === 'data' ? unwrap(pkt(f.payload)) : new TextEncoder().encode(f.kind === 'flush' ? '0000' : f.kind === 'delim' ? '0001' : '0002')));
121 let st = pktInit(); const got = [];
122 for (const c of splitRandom(rng, enc, { maxChunks: 64 })) { const r = unwrap(pktStep(st, c)); st = r.state; got.push(...r.frames); }
123 if (got.length !== frames.length || got.some((g, k) => g.kind !== frames[k].kind || (g.kind === 'data' && !Buffer.from(g.payload).equals(Buffer.from(frames[k].payload))))) throw new Error('pkt mismatch');
124 }
125 } catch (e) {
126 fails.push({ i, which, seed: SEED, msg: e.message });
127 console.log(`FAIL #${i} ${which}: ${e.message} seed=${SEED} replay: ${REPLAY} --n ${i + 1} --seed ${SEED}`);
128 if (fails.length > 20) break;
129 }
130 if (i % 20000 === 0 && i) process.stdout.write(` ${i}/${N} ${((Date.now() - t0) / 1000) | 0}s\n`);
132rmSync(dir, { recursive: true, force: true }); rmSync(feed, { force: true });
133console.log(`\n${N} cases in ${((Date.now() - t0) / 1000) | 0}s, ${fails.length} failures\n` + Object.entries(counts).sort().map(([k, v]) => `${String(v).padStart(7)} ${k}`).join('\n'));
134process.exit(fails.length ? 1 : 0);
Diagnostics4 git calls · 347 ms
CallStatusSizeTime0347 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200145 B · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=145
git.response.cache=HIT
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=145
git.pkt.payload.max=76
git.ls_refs.count=2
git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020038 KiB · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=340
git.response.bytes=38434
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=6
git.pkt.data=4
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=38434
git.pkt.payload.max=38339
git.sideband.band1.bytes=38338
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=63
git.pack.bytes=38338
git.pack.ofs_delta=0
git.pack.ref_delta=8
git.pack.chain.max=8
git.pack.trailer=0bc36ab7adf82d3109177f11f1ea93f1ce071f77
git.pack.full=55
Open pack map
fetch2004.5 KiB340 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=4650
git.response.cache=MISS
git.response.duration_ms=340
git.fetch.wants=807912c2b8585895b1304ba0f9fd5dc88d408304
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=4650
git.pkt.payload.max=4629
git.sideband.band1.bytes=4628
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply.git
Ref
refs/heads/main
Commit
19912563f540228593df0c489082e5e2508e21e4
cf-ray
a5e2937b8b00224d
Request
4c41f10f…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format