Debian Codegraph @

fuzz/build_corpus.sh

raw
1 #!/bin/sh
2 # Build a libFuzzer seed corpus from the existing tests/ data and
3 # the bundled hyph_en_US.dic. Each seed file has the format:
4 #
5 # [word bytes][NUL][dictionary bytes]
6 #
7 # matching what fuzz_hyphen.c expects.
8
9 set -eu
10
11 cd "$(dirname "$0")/.."
12
13 CORPUS_DIR=${1:-fuzz/corpus_seed}
14 mkdir -p "$CORPUS_DIR"
15
16 count=0
17
18 # tests/<name>.pat + tests/<name>.word -> one seed per word line
19 for pat in tests/*.pat; do
20 name=$(basename "$pat" .pat)
21 wordfile="tests/${name}.word"
22 [ -f "$wordfile" ] || continue
23
24 line_no=0
25 while IFS= read -r word || [ -n "$word" ]; do
26 line_no=$((line_no + 1))
27 # strip CR if present
28 word=$(printf '%s' "$word" | tr -d '\r')
29 out="$CORPUS_DIR/${name}_${line_no}"
30 # concat: word, NUL, dictionary
31 { printf '%s' "$word"; printf '\0'; cat "$pat"; } > "$out"
32 count=$((count + 1))
33 done < "$wordfile"
34 done
35
36 # A few generic seeds against hyph_en_US.dic (much larger dictionary,
37 # helps the FSM walk lots of states).
38 if [ -f hyph_en_US.dic ]; then
39 for w in hyphenation example creating absolutely beautifully \
40 antidisestablishmentarianism photograph supercalifragilistic \
41 "" "a" "ab" "abc" ".a" "a." ".." "a-b" "12345"; do
42 out="$CORPUS_DIR/en_$(printf '%s' "$w" | tr -c 'a-zA-Z0-9' '_')"
43 { printf '%s' "$w"; printf '\0'; cat hyph_en_US.dic; } > "$out"
44 count=$((count + 1))
45 done
46 fi
47
48 # A pure-parser seed: empty word, full hyph_en_US dictionary.
49 if [ -f hyph_en_US.dic ]; then
50 cat hyph_en_US.dic > "$CORPUS_DIR/parser_only_en"
51 count=$((count + 1))
52 fi
53
54 # Tiny edge cases for the parser path
55 { printf '%s' "x"; printf '\0'; printf 'UTF-8\n1.a\n'; } > "$CORPUS_DIR/tiny_boundary"
56 { printf '%s' "x"; printf '\0'; printf 'UTF-8\n'; } > "$CORPUS_DIR/tiny_empty_dict"
57 { printf '%s' "x"; printf '\0'; printf 'UTF-8\nNOHYPHEN \n1a1\n'; } > "$CORPUS_DIR/tiny_nohyphen_empty"
58 { printf '%s' "x"; printf '\0'; printf 'UTF-8\nNEXTLEVEL\nUTF-8\n1a1\n'; } > "$CORPUS_DIR/tiny_nextlevel"
59 count=$((count + 4))
60
61 echo "[corpus] wrote $count seed files to $CORPUS_DIR"
62 du -sh "$CORPUS_DIR"