@@ -9,23 +9,20 @@ set -euo pipefail
99#  Prerequisites #
1010# ################
1111
12- for  cmd  in  python3 git wget rsync ;  do 
12+ for  cmd  in  python3 git wget zip ;  do 
1313  command  -v " $cmd " > /dev/null 2>&1  ||  {
1414    printf  ' [%s] Required command %s not found, exiting.\n' " $( date ' +%Y-%m-%d %H:%M:%S' ) " " $cmd " >&2 
1515    exit  1
1616  }
1717done 
1818
19- SEED_DATA_DIR=" $SRC /seed_data" 
20- mkdir -p " $SEED_DATA_DIR " 
21- 
2219# ############
2320#  Functions #
2421# ############
2522
2623download_and_concatenate_common_dictionaries () {
2724  #  Assign the first argument as the target file where all contents will be concatenated
28-   target_file=" $1 " 
25+   local   target_file=" $1 " 
2926
3027  #  Shift the arguments so the first argument (target_file path) is removed
3128  #  and only URLs are left for the loop below.
@@ -38,22 +35,61 @@ download_and_concatenate_common_dictionaries() {
3835  done 
3936}
4037
41- fetch_seed_corpora () {
42-   #  Seed corpus zip files are hosted in a separate repository to avoid additional bloat in this repo.
43-   git clone --depth 1 https://github.com/gitpython-developers/qa-assets.git qa-assets && 
44-     rsync -avc qa-assets/gitpython/corpra/ " $SEED_DATA_DIR /" && 
45-     rm -rf qa-assets #  Clean up the cloned repo to keep the Docker image as slim as possible.
38+ create_seed_corpora_zips () {
39+   local  seed_corpora_dir=" $1 " 
40+   local  output_zip
41+   for  dir  in  " $seed_corpora_dir " * ;  do 
42+     if  [ -d  " $dir " &&  [ -n  " $dir " ;  then 
43+       output_zip=" $SRC /$( basename " $dir " ) " 
44+       printf  ' [%s] Zipping the contents of %s into %s\n' " $( date ' +%Y-%m-%d %H:%M:%S' ) " " $dir " " $output_zip " 
45+       zip -jur " $output_zip " " $dir " * 
46+     fi 
47+   done 
48+ }
49+ 
50+ prepare_dictionaries_for_fuzz_targets () {
51+   local  dictionaries_dir=" $1 " 
52+   local  fuzz_targets_dir=" $2 " 
53+   local  common_base_dictionary_filename=" $WORK /__base.dict" 
54+ 
55+   printf  ' [%s] Copying .dict files from %s to %s\n' " $( date ' +%Y-%m-%d %H:%M:%S' ) " " $dictionaries_dir " " $SRC /" 
56+   cp -v " $dictionaries_dir " * .dict " $SRC /" 
57+ 
58+   download_and_concatenate_common_dictionaries " $common_base_dictionary_filename " 
59+     " https://raw.githubusercontent.com/google/fuzzing/master/dictionaries/utf8.dict" 
60+     " https://raw.githubusercontent.com/google/fuzzing/master/dictionaries/url.dict" 
61+ 
62+   find " $fuzz_targets_dir " ' fuzz_*.py' |  while  IFS= read  -r -d ' ' ;  do 
63+     if  [[ -r  " $common_base_dictionary_filename " ;  then 
64+       #  Strip the `.py` extension from the filename and replace it with `.dict`.
65+       fuzz_harness_dictionary_filename=" $( basename " $fuzz_harness " ) " 
66+       local  output_file=" $SRC /$fuzz_harness_dictionary_filename " 
67+ 
68+       printf  ' [%s] Appending %s to %s\n' " $( date ' +%Y-%m-%d %H:%M:%S' ) " " $common_base_dictionary_filename " " $output_file " 
69+       if  [[ -s  " $output_file " ;  then 
70+         #  If a dictionary file for this fuzzer already exists and is not empty,
71+         #  we append a new line to the end of it before appending any new entries.
72+         # 
73+         #  LibFuzzer will happily ignore multiple empty lines in a dictionary but fail with an error
74+         #  if any single line has incorrect syntax (e.g., if we accidentally add two entries to the same line.)
75+         #  See docs for valid syntax: https://llvm.org/docs/LibFuzzer.html#id32
76+         echo  >> " $output_file " 
77+       fi 
78+       cat " $common_base_dictionary_filename " >> " $output_file " 
79+     fi 
80+   done 
4681}
4782
4883# #######################
4984#  Main execution logic #
5085# #######################
86+ #  Seed corpora and dictionaries are hosted in a separate repository to avoid additional bloat in this repo.
87+ #  We clone into the $WORK directory because OSS-Fuzz cleans it up after building the image, keeping the image small.
88+ git clone --depth 1 https://github.com/gitpython-developers/qa-assets.git " $WORK /qa-assets" 
5189
52- fetch_seed_corpora 
90+ create_seed_corpora_zips  " $WORK /qa-assets/gitpython/corpora " 
5391
54- download_and_concatenate_common_dictionaries " $SEED_DATA_DIR /__base.dict" 
55-   " https://raw.githubusercontent.com/google/fuzzing/master/dictionaries/utf8.dict" 
56-   " https://raw.githubusercontent.com/google/fuzzing/master/dictionaries/url.dict" 
92+ prepare_dictionaries_for_fuzz_targets " $WORK /qa-assets/gitpython/dictionaries" " $SRC /gitpython/fuzzing" 
5793
5894#  The OSS-Fuzz base image has outdated dependencies by default so we upgrade them below.
5995python3 -m pip install --upgrade pip
0 commit comments