forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

seed.sh67 lines, 2.7 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# seed.sh: fill fuzz/corpus/<target>/ from the repo's FLAC fixtures (root fixtures/).
3#
4# decode_file and parse_metadata get every .flac as-is: the root fixtures/audio set plus the
5# xiph FLAC testbench (subset, uncommon, faulty).
6# decode_frame gets each file's frames only, behind a selector byte of 0: the metadata blocks
7# are stripped so the fuzzer starts from bare frames, as a container would hand them out.
8#
9# Corpus lives under fuzz/corpus, which is git-ignored.
10set -euo pipefail
11
12FUZZ_DIR=$(cd -- "$(dirname -- "$0")" && pwd)
13ROOT=$(cd -- "$FUZZ_DIR/../../.." && pwd)
14FIXTURES="$ROOT/fixtures"
15[ -d "$FIXTURES/audio" ] || { echo "seed.sh: $FIXTURES/audio missing; run scripts/link-fixtures.sh" >&2; exit 2; }
16XIPH="$FIXTURES/vectors/flac-xiph/flac-test-files-main"
17[ -d "$XIPH" ] || { echo "seed.sh: $XIPH missing; run scripts/fetch-vectors.sh" >&2; exit 2; }
18
19FILE_DIR="$FUZZ_DIR/corpus/decode_file"
20META_DIR="$FUZZ_DIR/corpus/parse_metadata"
21FRAME_DIR="$FUZZ_DIR/corpus/decode_frame"
22mkdir -p "$FILE_DIR" "$META_DIR" "$FRAME_DIR"
23
24python3 -I - "$FIXTURES/audio" "$XIPH" "$FILE_DIR" "$META_DIR" "$FRAME_DIR" <<'PY'
25import glob, os, re, sys
26
27audio, xiph, file_dir, meta_dir, frame_dir = sys.argv[1:6]
28
29paths = sorted(glob.glob(os.path.join(audio, "flac-*.flac")))
30for sub in ("subset", "uncommon", "faulty"):
31 paths += sorted(glob.glob(os.path.join(xiph, sub, "*.flac")))
32# Harness cap: seeds above 1 MiB are skipped. The xiph testbench has 87 MB and 42 MB files;
33# one such input runs at a few exec/s and would dominate the RSS budget of a fuzz run.
34MAX_SEED = 1 << 20
35paths = [p for p in paths if os.path.getsize(p) <= MAX_SEED]
36if not paths:
37 sys.exit("seed.sh: no FLAC fixtures found")
38
39def frames_after_metadata(data):
40 """Bytes from the first frame on, or None when the file has no fLaC magic."""
41 if data[:4] != b"fLaC":
42 return None
43 i = 4
44 while i + 4 <= len(data):
45 last = data[i] & 0x80
46 n = int.from_bytes(data[i + 1:i + 4], "big")
47 i += 4 + n
48 if last:
49 return data[i:]
50 return None
51
52def stem(path):
53 rel = os.path.relpath(path, xiph) if path.startswith(xiph) else os.path.basename(path)
54 return re.sub(r"[^A-Za-z0-9._-]+", "_", os.path.splitext(rel)[0]).strip("_")
55
56count = 0
57for path in paths:
58 data = open(path, "rb").read()
59 name = stem(path)
60 open(os.path.join(file_dir, name), "wb").write(data)
61 open(os.path.join(meta_dir, name), "wb").write(data)
62 body = frames_after_metadata(data)
63 if body is not None:
64 open(os.path.join(frame_dir, name), "wb").write(b"\x00" + body)
65 count += 1
66print(f"seeded {len(paths)} decode_file and parse_metadata seeds, {count} decode_frame seeds")
67PY