Debian Codegraph @

ossfuzz/ossfuzz.sh

raw
1 #!/bin/bash -eu
2 # Copyright 2023 Google LLC
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 ################################################################################
17
18 # This script is meant to be run by
19 # https://github.com/google/oss-fuzz/tree/master/projects/compress
20
21 # In one of the Zstd fuzzers, the "dict" variable is created by reading a series of files.
22 # These files are not available at runtime in the OSS-FUzz environment,
23 # so we add the contents of these files to a variable and create a new file
24 # that is included when we build the fuzzers in OSS-Fuzz.
25 mkdir $SRC/setupdicts
26 cp $SRC/compress/ossfuzz/cmd/setup_dicts.go $SRC/setupdicts/main.go
27 cd $SRC/setupdicts
28 go mod init setupdicts
29 go mod tidy
30 go run main.go --dict-path=$SRC/compress/zstd/testdata/dict-tests-small.zip --output-file=$SRC/compress/zstd/fuzzDicts.go
31 cp $SRC/compress/zstd/fuzzDicts.go $OUT/
32 # Done creating "dicts" variable.
33
34 cd $SRC/compress
35
36 # Modify some files. This would be better done upstream.
37 printf "package compress\nimport _ \"github.com/AdamKorcz/go-118-fuzz-build/testing\"\n" > registerfuzzdependency.go
38 sed -i 's/zr := testCreateZipReader/\/\/zr := testCreateZipReader/g' "${SRC}"/compress/zstd/fuzz_test.go
39 sed -i 's/dicts = readDicts(f, zr)/dicts = fuzzDicts/g' "${SRC}"/compress/zstd/fuzz_test.go
40
41 if [ "$SANITIZER" != "coverage" ]; then
42 sed -i 's/\"testing\"/\"github.com\/AdamKorcz\/go-118-fuzz-build\/testing\"/g' "${SRC}"/compress/internal/fuzz/helpers.go
43 printf "\n\nreplace github.com/AdamKorcz/go-118-fuzz-build => github.com/klauspost/go-118-fuzz-build d5f2eff5a9ec105b249e0bb1a24c1725330ed424\n" >> go.mod
44 fi
45
46 # OSS-Fuzz uses 'go build' to build the fuzzers, so we move the tests
47 # we need into scope.
48 mv $SRC/compress/zstd/decoder_test.go $SRC/compress/zstd/decoder_test_fuzz.go
49 mv $SRC/compress/zstd/zstd_test.go $SRC/compress/zstd/zstd_test_fuzz.go
50 mv $SRC/compress/zstd/seqdec_test.go $SRC/compress/zstd/seqdec_test_fuzz.go
51 mv $SRC/compress/zstd/dict_test.go $SRC/compress/zstd/dict_test_fuzz.go
52 mv $SRC/compress/s2/s2_test.go $SRC/compress/s2/s2_test_fuzz.go
53 go mod tidy
54
55 # Build fuzzers
56 compile_native_go_fuzzer $SRC/compress/flate FuzzEncoding FuzzFlateEncoding
57 compile_native_go_fuzzer $SRC/compress/zstd FuzzDecodeAll FuzzDecodeAll
58 compile_native_go_fuzzer $SRC/compress/zstd FuzzDecoder FuzzDecoder
59 compile_native_go_fuzzer $SRC/compress/zstd FuzzEncoding FuzzZstdEncoding
60 compile_native_go_fuzzer $SRC/compress/zstd FuzzDecAllNoBMI2 FuzzDecAllNoBMI2
61 compile_native_go_fuzzer $SRC/compress/zstd FuzzNoBMI2Dec FuzzNoBMI2Dec
62 compile_native_go_fuzzer $SRC/compress/s2 FuzzDictBlocks FuzzDictBlocks
63 compile_native_go_fuzzer $SRC/compress/s2 FuzzEncodingBlocks FuzzEncodingBlocks
64 compile_native_go_fuzzer $SRC/compress/zip FuzzReader FuzzReader
65 compile_native_go_fuzzer $SRC/compress/snappy/xerial FuzzDecode FuzzDecode
66 compile_native_go_fuzzer $SRC/compress/fse FuzzCompress FuzzFSECompress
67 compile_native_go_fuzzer $SRC/compress/fse FuzzDecompress FuzzFSEDecompress
68 compile_native_go_fuzzer $SRC/compress/huff0 FuzzCompress FuzzHuff0Compress
69 compile_native_go_fuzzer $SRC/compress/huff0 FuzzDecompress1x FuzzHuff0Decompress1x
70 compile_native_go_fuzzer $SRC/compress/snappy/xerial FuzzEncode FuzzEncode
71
72 #Add corpora from compress-fuzz dir
73 cp $SRC/compress-fuzz/zstd/compress/fuzz/encode-corpus-raw.zip $OUT/FuzzZstdEncoding_seed_corpus.zip
74 cp $SRC/compress-fuzz/zstd/decompress/fuzz/decode-corpus-raw.zip $OUT/FuzzDecodeAll_seed_corpus.zip
75 cp $SRC/compress-fuzz/zstd/decompress/fuzz/decode-corpus-raw.zip $OUT/FuzzDecoder_seed_corpus.zip
76 cp $SRC/compress-fuzz/zip/fuzz/FuzzReader-raw.zip $OUT/FuzzReader_seed_corpus.zip
77 cp $SRC/compress-fuzz/fse/compress/fuzz/fse_compress.zip $OUT/FuzzFSECompress_seed_corpus.zip
78 cp $SRC/compress-fuzz/fse/decompress/fuzz/fse_decompress.zip $OUT/FuzzFSEDecompress_seed_corpus.zip
79 cp $SRC/compress-fuzz/huff0/compress/fuzz/huff0_compress.zip $OUT/FuzzHuff0Compress_seed_corpus.zip
80 cp $SRC/compress-fuzz/huff0/decompress/fuzz/huff0_decompress1x.zip $OUT/FuzzHuff0Decompress1x_seed_corpus.zip
81 cp $SRC/compress-fuzz/flate/flate/fuzz/encode-raw-corpus.zip $OUT/FuzzFlateEncoding_seed_corpus.zip
82 cp $SRC/compress-fuzz/s2/compress/fuzz/block-corpus-raw.zip $OUT/FuzzDictBlocks_seed_corpus.zip
83 cp $SRC/compress-fuzz/s2/compress/fuzz/block-corpus-raw.zip $OUT/FuzzEncodingBlocks_seed_corpus.zip
84 cp $SRC/compress-fuzz/snappy/fuzz/FuzzDecode_raw.zip $OUT/FuzzDecode_seed_corpus.zip
85 cp $SRC/compress-fuzz/snappy/fuzz/block-corpus-raw.zip $OUT/FuzzEncode_seed_corpus.zip
86
87 # Add missing test files to avoid errors and have test files for code coverage
88 cp -r $SRC/compress/zstd/testdata $OUT/
89 cp -r $SRC/compress/zip/testdata $OUT/
90 cp -r $SRC/compress/flate/testdata $OUT/
91 cp -r $SRC/compress/s2/testdata $OUT/
92 cp -r $SRC/compress/fse/testdata $OUT/
93 cp -r $SRC/compress/snappy/xerial/testdata $OUT/
94 cp -r $SRC/compress/huff0/testdata $OUT/
95 cp -r $SRC/compress/testdata $OUT