fuzzing_ci.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env bash
  2. ##
  3. # Copyright 2018 Google
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. # Run fuzz testing using xcodebuild. Given a total fuzzing duration, select
  17. # a fuzzing target to run for half of the fuzzing duration. The remaining
  18. # duration is split equally over the rest of the fuzzing targets. Must be run
  19. # from the project root directory. The script is intended to execute on travis
  20. # cron jobs, but can run locally (on a macOS) from the project root.
  21. # Total time allowed for fuzzing in seconds (40 minutes for now).
  22. readonly ALLOWED_TIME=2400
  23. # An invalid target that is used to retrieve the list of available targets in
  24. # the returned error message.
  25. readonly INVALID_TARGET="INVALID_TARGET"
  26. # The NONE target that does not perform fuzzing. It is valid but useless.
  27. readonly NONE_TARGET="NONE"
  28. # Script exit codes.
  29. readonly EXIT_SUCCESS=0
  30. readonly EXIT_FAILURE=1
  31. # Get day of the year to use it when selecting a main target.
  32. readonly DOY="$(date +%j)"
  33. # Run fuzz testing only if executing on xcode >= 9 because fuzzing requires
  34. # Clang that supports -fsanitize-coverage=trace-pc-guard.
  35. xcode_version="$(xcodebuild -version | head -n 1)"
  36. xcode_version="${xcode_version/Xcode /}"
  37. xcode_major="${xcode_version/.*/}"
  38. if [[ "${xcode_major}" -lt 9 ]]; then
  39. echo "Unsupported version of xcode."
  40. exit ${EXIT_SUCCESS}
  41. fi
  42. # Helper to run fuzz tests using xcodebuild. Takes the fuzzing target as the
  43. # first argument and the fuzzing duration as the second argument.
  44. function run_xcode_fuzzing() {
  45. xcodebuild \
  46. -workspace 'Firestore/Example/Firestore.xcworkspace' \
  47. -scheme 'Firestore_FuzzTests_iOS' \
  48. -sdk 'iphonesimulator' \
  49. -destination 'platform=iOS Simulator,name=iPhone 7' \
  50. FUZZING_TARGET="$1" \
  51. FUZZING_DURATION="$2" \
  52. test
  53. }
  54. # Run fuzz testing with an INVALID_TARGET and grep the response message line
  55. # that contains the string 'Available targets: {'.
  56. fuzzing_targets_line="$(run_xcode_fuzzing ${INVALID_TARGET} "0" \
  57. | grep "Available targets: {")"
  58. # The fuzzing_targets_line string contains { TARGET1 TARGET2 ... TARGETN }.
  59. # The following command extracts the targets and splits them into an array
  60. # in the variable all_fuzzing_targets.
  61. read -r -a all_fuzzing_targets <<< "$(echo ${fuzzing_targets_line} \
  62. | cut -d "{" -f2 | cut -d "}" -f1)"
  63. # Remove the NONE target, if it exists.
  64. for i in "${!all_fuzzing_targets[@]}"; do
  65. if [[ "${all_fuzzing_targets[${i}]}" == ${NONE_TARGET} ]]; then
  66. unset all_fuzzing_targets[${i}]
  67. fi
  68. done
  69. # Count all targets.
  70. readonly fuzzing_targets_count="${#all_fuzzing_targets[@]}"
  71. # Select a primary target to fuzz test for longer. Changes daily. The variable
  72. # todays_primary_target contains the index of the target selected as primary.
  73. todays_primary_target="$(( ${DOY} % ${fuzzing_targets_count} ))"
  74. # Spend half of allowed time on the selected primary target and half on the
  75. # remaining targets.
  76. primary_target_time="$(( ${ALLOWED_TIME} / 2 ))"
  77. other_targets_time="$(( ${ALLOWED_TIME} / 2 / $(( ${fuzzing_targets_count} - 1)) ))"
  78. script_return=${EXIT_SUCCESS}
  79. for i in "${!all_fuzzing_targets[@]}"; do
  80. fuzzing_duration="${other_targets_time}"
  81. if [[ "${i}" -eq "${todays_primary_target}" ]]; then
  82. fuzzing_duration="${primary_target_time}"
  83. fi
  84. fuzzing_target="${all_fuzzing_targets[${i}]}"
  85. echo "Running fuzzing target ${fuzzing_target} for ${fuzzing_duration} seconds..."
  86. fuzzing_results="$(run_xcode_fuzzing "${fuzzing_target}" "${fuzzing_duration}")"
  87. # Get the last line of the fuzzing results.
  88. fuzzing_results_last_line="$(echo "${fuzzing_results}" | tail -n1)"
  89. # If fuzzing succeeded, the last line will start with "Done"; otherwise,
  90. # print all fuzzing results.
  91. if [[ "${fuzzing_results_last_line}" == Done* ]]; then
  92. echo "Fuzz testing for target ${fuzzing_target} succeeded. Ignore the ** TEST FAILED ** message."
  93. else
  94. echo "Error: Fuzz testing for target ${fuzzing_target} failed."
  95. script_return=${EXIT_FAILURE}
  96. echo "Fuzzing logs:"
  97. echo "${fuzzing_results}"
  98. fi
  99. done
  100. exit ${script_return}