| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | set -euo pipefail |
| 11 | |
| 12 | FUZZ_DIR=$(cd -- "$(dirname -- "$0")" && pwd) |
| 13 | ROOT=$(cd -- "$FUZZ_DIR/../../.." && pwd) |
| 14 | FIXTURES="$ROOT/fixtures" |
| 15 | [ -d "$FIXTURES/audio" ] || { echo "seed.sh: $FIXTURES/audio missing; run scripts/link-fixtures.sh" >&2; exit 2; } |
| 16 | XIPH="$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 | |
| 19 | FILE_DIR="$FUZZ_DIR/corpus/decode_file" |
| 20 | META_DIR="$FUZZ_DIR/corpus/parse_metadata" |
| 21 | FRAME_DIR="$FUZZ_DIR/corpus/decode_frame" |
| 22 | mkdir -p "$FILE_DIR" "$META_DIR" "$FRAME_DIR" |
| 23 | |
| 24 | python3 -I - "$FIXTURES/audio" "$XIPH" "$FILE_DIR" "$META_DIR" "$FRAME_DIR" <<'PY' |
| 25 | import glob, os, re, sys |
| 26 | |
| 27 | audio, xiph, file_dir, meta_dir, frame_dir = sys.argv[1:6] |
| 28 | |
| 29 | paths = sorted(glob.glob(os.path.join(audio, "flac-*.flac"))) |
| 30 | for 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. |
| 34 | MAX_SEED = 1 << 20 |
| 35 | paths = [p for p in paths if os.path.getsize(p) <= MAX_SEED] |
| 36 | if not paths: |
| 37 | sys.exit("seed.sh: no FLAC fixtures found") |
| 38 | |
| 39 | def 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 | |
| 52 | def 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 | |
| 56 | count = 0 |
| 57 | for 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 |
| 66 | print(f"seeded {len(paths)} decode_file and parse_metadata seeds, {count} decode_frame seeds") |
| 67 | PY |