| # Copyright 2020 The Pigweed Authors |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| # use this file except in compliance with the License. You may obtain a copy of |
| # the License at |
| # |
| # https://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| # License for the specific language governing permissions and limitations under |
| # the License. |
| |
| import("//build_overrides/pigweed.gni") |
| |
| import("$dir_pw_build/error.gni") |
| import("$dir_pw_build/target_types.gni") |
| import("$dir_pw_docgen/docs.gni") |
| import("$dir_pw_third_party/fuzztest/fuzztest.gni") |
| import("$dir_pw_unit_test/test.gni") |
| |
| config("public_include_path") { |
| include_dirs = [ "public" ] |
| visibility = [ ":*" ] |
| } |
| |
| config("private_include_path") { |
| include_dirs = [ "private" ] |
| visibility = [ ":*" ] |
| } |
| |
| config("overrides_include_path") { |
| include_dirs = [ |
| "public_overrides", |
| "private_overrides", |
| ] |
| visibility = [ ":*" ] |
| } |
| |
| # See https://llvm.org/docs/LibFuzzer.html#fuzzer-friendly-build-mode |
| config("fuzzing_build_mode_unsafe_for_production") { |
| defines = [ "FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION" ] |
| } |
| |
| pw_doc_group("docs") { |
| sources = [ |
| "concepts.rst", |
| "docs.rst", |
| "guides/fuzztest.rst", |
| "guides/index.rst", |
| "guides/libfuzzer.rst", |
| "guides/reproducing_oss_fuzz_bugs.rst", |
| ] |
| inputs = [ |
| "doc_resources/pw_fuzzer_coverage_guided.png", |
| "examples/fuzztest/BUILD.gn", |
| "examples/fuzztest/BUILD.bazel", |
| "examples/fuzztest/CMakeLists.txt", |
| "examples/fuzztest/metrics.h", |
| "examples/fuzztest/metrics_unittest.cc", |
| "examples/fuzztest/metrics_fuzztest.cc", |
| ] |
| } |
| |
| pw_test_group("tests") { |
| group_deps = [ |
| ":fuzztest_tests", |
| "examples/fuzztest:tests", |
| "examples/libfuzzer:tests", |
| ] |
| } |
| |
| ################################################################################ |
| # FuzzTest support |
| # |
| # Create FuzzTest-style fuzzers by adding a dep on dir_pw_fuzzer:fuzztest |
| |
| # Unit tests with a dep on this target can include FuzzTest-style fuzzers. |
| pw_source_set("fuzztest") { |
| # Include headers that extend FuzzTest's interface with support for common |
| # Pigweed types. |
| public = [ "public/pw_fuzzer/fuzztest.h" ] |
| public_configs = [ ":public_include_path" ] |
| public_deps = [ |
| dir_pw_containers, |
| dir_pw_result, |
| dir_pw_status, |
| dir_pw_string, |
| ] |
| if (dir_pw_third_party_fuzztest != "" && pw_toolchain_FUZZING_ENABLED) { |
| # Include headers and deps that provide a Pigweed-compatible subset of |
| # FuzzTest. |
| public += [ "private/pw_fuzzer/internal/fuzztest.h" ] |
| public_configs += [ |
| "$dir_pw_third_party/abseil-cpp/configs:disabled_warnings", |
| "$dir_pw_third_party/re2/configs:disabled_warnings", |
| "$dir_pw_third_party/fuzztest/configs:disabled_warnings", |
| ":private_include_path", |
| ] |
| public_deps += [ "$dir_pw_third_party/fuzztest/fuzztest" ] |
| } else { |
| # Include headers that provide stubs of the Pigweed-compatible subset of |
| # FuzzTest's interface. |
| public += [ |
| "private_overrides/pw_fuzzer/internal/fuzztest.h", |
| "public_overrides/fuzztest/fuzztest.h", |
| ] |
| public_configs += [ ":overrides_include_path" ] |
| } |
| } |
| |
| pw_test("fuzztest_tests") { |
| sources = [ "domain_test.cc" ] |
| deps = [ ":fuzztest" ] |
| } |
| |
| # This target should only be used when defining a fuzzing toolchain, e.g. to set |
| # `pw_unit_test_GOOGLETEST_BACKEND = "$dir_pw_fuzzer:gtest" |
| # TODO: b/295961502 - Support running FuzzTest-based fuzzers on OSS-Fuzz. |
| if (pw_toolchain_OSS_FUZZ_ENABLED) { |
| group("gtest") { |
| public_deps = [ "$dir_pw_unit_test:light" ] |
| } |
| } else if (dir_pw_third_party_googletest != "") { |
| group("gtest") { |
| public_deps = [ "$dir_pw_third_party/googletest" ] |
| } |
| } else { |
| pw_error("gtest") { |
| message_lines = [ |
| "pw_unit_test_GOOGLETEST_BACKEND is set to dir_pw_fuzzer:gtest, ", |
| "but dir_pw_third_party_googletest is not set.", |
| ] |
| } |
| } |
| |
| # This target should only be used when defining a fuzzing toolchain, e.g. to set |
| # `pw_unit_test_MAIN = "$dir_pw_fuzzer:fuzztest_main" |
| # TODO: b/295961502 - Support running FuzzTest-based fuzzers on OSS-Fuzz. |
| if (pw_toolchain_OSS_FUZZ_ENABLED) { |
| group("fuzztest_main") { |
| deps = [ "$dir_pw_unit_test:simple_printing_main" ] |
| } |
| } else if (dir_pw_third_party_fuzztest != "") { |
| group("fuzztest_main") { |
| deps = [ "$dir_pw_third_party/fuzztest/fuzztest:fuzztest_gtest_main" ] |
| } |
| } else { |
| pw_error("fuzztest_main") { |
| message_lines = [ |
| "pw_unit_test_MAIN is set to dir_pw_fuzzer:fuzztest_main, ", |
| "but dir_pw_third_party_fuzztest is not set.", |
| ] |
| } |
| } |
| |
| ################################################################################ |
| # libFuzzer support |
| # |
| # Create libFuzzer-style fuzzers by using the `pw_fuzzer` template from |
| # fuzzer.gni. |
| |
| # Add flags for adding LLVM sanitizer coverage for fuzzing. This is added by |
| # the host_clang_fuzz toolchains. |
| config("instrumentation") { |
| if (pw_toolchain_OSS_FUZZ_ENABLED) { |
| # OSS-Fuzz manipulates compiler flags directly. See |
| # google.github.io/oss-fuzz/getting-started/new-project-guide/#Requirements. |
| cflags_c = string_split(getenv("CFLAGS")) |
| cflags_cc = string_split(getenv("CXXFLAGS")) |
| |
| # OSS-Fuzz sets "-stdlib=libc++", which conflicts with the "-nostdinc++" set |
| # by `pw_minimal_cpp_stdlib`. |
| cflags_cc += [ "-Wno-unused-command-line-argument" ] |
| } else { |
| cflags = [ "-fsanitize=fuzzer-no-link" ] |
| } |
| } |
| |
| # Add flags for linking against compiler-rt's libFuzzer. This is added |
| # automatically by `pw_fuzzer`. |
| config("libfuzzer_config") { |
| if (pw_toolchain_OSS_FUZZ_ENABLED) { |
| # OSS-Fuzz manipulates linker flags directly. See |
| # google.github.io/oss-fuzz/getting-started/new-project-guide/#Requirements. |
| ldflags = string_split(getenv("LDFLAGS")) + [ getenv("LIB_FUZZING_ENGINE") ] |
| } else { |
| ldflags = [ "-fsanitize=fuzzer" ] |
| } |
| } |
| |
| # Includes wrapper's for LLVM's libFuzzer compiler runtime library. |
| pw_source_set("libfuzzer") { |
| public = [ |
| "public/pw_fuzzer/asan_interface.h", |
| "public/pw_fuzzer/fuzzed_data_provider.h", |
| ] |
| public_configs = [ ":public_include_path" ] |
| public_deps = [ dir_pw_log ] |
| } |
| |
| # This can be linked against fuzz target functions to create unit tests for |
| # them. |
| pw_source_set("libfuzzer_test") { |
| sources = [ "pw_fuzzer_disabled.cc" ] |
| public_deps = [ ":libfuzzer" ] |
| deps = [ dir_pw_unit_test ] |
| } |
| |
| # libFuzzer-based fuzzers have a distinct dep graph. |
| group("fuzzers") { |
| deps = [ "examples/libfuzzer:fuzzers" ] |
| } |