oss-fuzz.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #/bin/bash -eu
  2. # Copyright 2020 Google Inc.
  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. # This file is for integration with Google OSS-Fuzz.
  18. # The following ENV variables are available when executing on OSS-fuzz:
  19. #
  20. # /out/ $OUT Directory to store build artifacts (fuzz targets, dictionaries, options files, seed corpus archives).
  21. # /src/ $SRC Directory to checkout source files.
  22. # /work/ $WORK Directory to store intermediate files.
  23. #
  24. # $CC, $CXX, $CCC The C and C++ compiler binaries.
  25. # $CFLAGS, $CXXFLAGS C and C++ compiler flags.
  26. # $LIB_FUZZING_ENGINE C++ compiler argument to link fuzz target against the prebuilt engine library (e.g. libFuzzer).
  27. # This sets the -coverpgk for the coverage report when the corpus is executed through go test
  28. coverpkg="github.com/ethereum/go-ethereum/..."
  29. function coverbuild {
  30. path=$1
  31. function=$2
  32. fuzzer=$3
  33. tags=""
  34. if [[ $# -eq 4 ]]; then
  35. tags="-tags $4"
  36. fi
  37. cd $path
  38. fuzzed_package=`pwd | rev | cut -d'/' -f 1 | rev`
  39. cp $GOPATH/ossfuzz_coverage_runner.go ./"${function,,}"_test.go
  40. sed -i -e 's/FuzzFunction/'$function'/' ./"${function,,}"_test.go
  41. sed -i -e 's/mypackagebeingfuzzed/'$fuzzed_package'/' ./"${function,,}"_test.go
  42. sed -i -e 's/TestFuzzCorpus/Test'$function'Corpus/' ./"${function,,}"_test.go
  43. cat << DOG > $OUT/$fuzzer
  44. #/bin/sh
  45. cd $OUT/$path
  46. go test -run Test${function}Corpus -v $tags -coverprofile \$1 -coverpkg $coverpkg
  47. DOG
  48. chmod +x $OUT/$fuzzer
  49. #echo "Built script $OUT/$fuzzer"
  50. #cat $OUT/$fuzzer
  51. cd -
  52. }
  53. function compile_fuzzer {
  54. # Inputs:
  55. # $1: The package to fuzz, within go-ethereum
  56. # $2: The name of the fuzzing function
  57. # $3: The name to give to the final fuzzing-binary
  58. path=$GOPATH/src/github.com/ethereum/go-ethereum/$1
  59. func=$2
  60. fuzzer=$3
  61. echo "Building $fuzzer"
  62. # Do a coverage-build or a regular build
  63. if [[ $SANITIZER = *coverage* ]]; then
  64. coverbuild $path $func $fuzzer $coverpkg
  65. else
  66. (cd $path && \
  67. go-fuzz -func $func -o $WORK/$fuzzer.a . && \
  68. $CXX $CXXFLAGS $LIB_FUZZING_ENGINE $WORK/$fuzzer.a -o $OUT/$fuzzer)
  69. fi
  70. ## Check if there exists a seed corpus file
  71. corpusfile="${path}/testdata/${fuzzer}_seed_corpus.zip"
  72. if [ -f $corpusfile ]
  73. then
  74. cp $corpusfile $OUT/
  75. echo "Found seed corpus: $corpusfile"
  76. fi
  77. }
  78. compile_fuzzer tests/fuzzers/bitutil Fuzz fuzzBitutilCompress
  79. compile_fuzzer tests/fuzzers/bn256 FuzzAdd fuzzBn256Add
  80. compile_fuzzer tests/fuzzers/bn256 FuzzMul fuzzBn256Mul
  81. compile_fuzzer tests/fuzzers/bn256 FuzzPair fuzzBn256Pair
  82. compile_fuzzer tests/fuzzers/runtime Fuzz fuzzVmRuntime
  83. compile_fuzzer tests/fuzzers/keystore Fuzz fuzzKeystore
  84. compile_fuzzer tests/fuzzers/txfetcher Fuzz fuzzTxfetcher
  85. compile_fuzzer tests/fuzzers/rlp Fuzz fuzzRlp
  86. compile_fuzzer tests/fuzzers/trie Fuzz fuzzTrie
  87. compile_fuzzer tests/fuzzers/stacktrie Fuzz fuzzStackTrie
  88. compile_fuzzer tests/fuzzers/difficulty Fuzz fuzzDifficulty
  89. compile_fuzzer tests/fuzzers/abi Fuzz fuzzAbi
  90. compile_fuzzer tests/fuzzers/les Fuzz fuzzLes
  91. compile_fuzzer tests/fuzzers/vflux FuzzClientPool fuzzClientPool
  92. compile_fuzzer tests/fuzzers/bls12381 FuzzG1Add fuzz_g1_add
  93. compile_fuzzer tests/fuzzers/bls12381 FuzzG1Mul fuzz_g1_mul
  94. compile_fuzzer tests/fuzzers/bls12381 FuzzG1MultiExp fuzz_g1_multiexp
  95. compile_fuzzer tests/fuzzers/bls12381 FuzzG2Add fuzz_g2_add
  96. compile_fuzzer tests/fuzzers/bls12381 FuzzG2Mul fuzz_g2_mul
  97. compile_fuzzer tests/fuzzers/bls12381 FuzzG2MultiExp fuzz_g2_multiexp
  98. compile_fuzzer tests/fuzzers/bls12381 FuzzPairing fuzz_pairing
  99. compile_fuzzer tests/fuzzers/bls12381 FuzzMapG1 fuzz_map_g1
  100. compile_fuzzer tests/fuzzers/bls12381 FuzzMapG2 fuzz_map_g2
  101. compile_fuzzer tests/fuzzers/bls12381 FuzzCrossG1Add fuzz_cross_g1_add
  102. compile_fuzzer tests/fuzzers/bls12381 FuzzCrossG1MultiExp fuzz_cross_g1_multiexp
  103. compile_fuzzer tests/fuzzers/bls12381 FuzzCrossG2Add fuzz_cross_g2_add
  104. compile_fuzzer tests/fuzzers/bls12381 FuzzCrossPairing fuzz_cross_pairing
  105. #TODO: move this to tests/fuzzers, if possible
  106. compile_fuzzer crypto/blake2b Fuzz fuzzBlake2b