fuzzing_options.cmake 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright 2018 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # Add fuzz testing options to the current build.
  15. option(FUZZING "Build for Fuzz Testing (local fuzzing and OSS Fuzz)" OFF)
  16. # Assume OSS Fuzz if LIB_FUZZING_ENGINE environment variable is set. OSS Fuzz
  17. # provides its required compiler-specific flags in CXXFLAGS, which are
  18. # automatically added to CMAKE_CXX_FLAGS. For local fuzzing, multiple compile
  19. # and linking flags must be set. These flags depend on the compiler version.
  20. if(FUZZING AND NOT DEFINED ENV{LIB_FUZZING_ENGINE})
  21. if(WIN32)
  22. # Currently, libFuzzer cannot be built on Windows.
  23. message(FATAL_ERROR "Fuzzing is currently not supported on Windows.")
  24. endif()
  25. # Address sanitizer must be enabled during fuzzing to detect memory errors.
  26. if(NOT WITH_ASAN)
  27. message(FATAL_ERROR "Fuzzing requires WITH_ASAN=ON to detect memory errors.")
  28. endif()
  29. # Set the flag to enable code coverage instrumentation. Fuzzing engines use
  30. # code coverage as a metric to guide the fuzzing. We use the basic code
  31. # coverage level (trace-pc). This flag has different values in Clang and GNU.
  32. # Other values, such as trace-cmp, can be used to trace data flow. See the
  33. # official documentation for the compiler flags.
  34. if(CXX_CLANG)
  35. # TODO(minafarid): Check the version of Clang. Clang versions >= 5.0 should
  36. # have libFuzzer by default.
  37. set(fuzzing_flags -fsanitize-coverage=trace-pc-guard)
  38. elseif(CXX_GNU)
  39. set(fuzzing_flags -fsanitize-coverage=trace-pc)
  40. else()
  41. message(FATAL_ERROR "Only Clang and GCC support fuzzing.")
  42. endif()
  43. foreach(flag ${fuzzing_flags})
  44. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
  45. endforeach()
  46. endif()