|  | # 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. | 
|  |  | 
|  | # gn-format disable | 
|  | import("//build_overrides/pigweed.gni") | 
|  |  | 
|  | import("$dir_pw_build/target_types.gni") | 
|  | import("$dir_pw_docgen/docs.gni") | 
|  | import("$dir_pw_fuzzer/fuzzer.gni") | 
|  | import("$dir_pw_fuzzer/oss_fuzz.gni") | 
|  | config("default_config") { | 
|  | include_dirs = [ "public" ] | 
|  | } | 
|  |  | 
|  | # This is added automatically by the `pw_fuzzer` template. | 
|  | config("fuzzing") { | 
|  | common_flags = [ "-fsanitize=fuzzer" ] | 
|  | cflags = common_flags | 
|  | ldflags = common_flags | 
|  | } | 
|  |  | 
|  | config("sanitize_address") { | 
|  | cflags = [ "-fsanitize=address" ] | 
|  | ldflags = cflags | 
|  | } | 
|  |  | 
|  | config("sanitize_memory") { | 
|  | cflags = [ "-fsanitize=memory" ] | 
|  | ldflags = cflags | 
|  | } | 
|  |  | 
|  | config("sanitize_undefined") { | 
|  | cflags = [ "-fsanitize=undefined" ] | 
|  | ldflags = cflags | 
|  | } | 
|  |  | 
|  | config("sanitize_coverage") { | 
|  | cflags = [ | 
|  | "-fprofile-instr-generate", | 
|  | "-fcoverage-mapping", | 
|  | ] | 
|  | ldflags = cflags | 
|  | } | 
|  |  | 
|  | # OSS-Fuzz needs to be able to specify its own compilers and add flags. | 
|  | config("oss_fuzz") { | 
|  | # OSS-Fuzz doesn't always link with -fsanitize=fuzzer, sometimes it uses | 
|  | #-fsanitize=fuzzer-no-link and provides the fuzzing engine explicitly to be | 
|  | # passed to the linker. | 
|  | ldflags = [ getenv("LIB_FUZZING_ENGINE") ] | 
|  | } | 
|  |  | 
|  | config("oss_fuzz_extra") { | 
|  | cflags_c = oss_fuzz_extra_cflags_c | 
|  | cflags_cc = oss_fuzz_extra_cflags_cc | 
|  | ldflags = oss_fuzz_extra_ldflags | 
|  | } | 
|  |  | 
|  | pw_source_set("pw_fuzzer") { | 
|  | public_configs = [ ":default_config" ] | 
|  | public = [ | 
|  | "public/pw_fuzzer/asan_interface.h", | 
|  | "public/pw_fuzzer/fuzzed_data_provider.h", | 
|  | ] | 
|  | sources = public | 
|  | public_deps = [ "$dir_pw_log" ] | 
|  | } | 
|  |  | 
|  | source_set("run_as_unit_test") { | 
|  | configs = [ ":default_config" ] | 
|  | sources = [ "pw_fuzzer_disabled.cc" ] | 
|  | deps = [ | 
|  | dir_pw_log, | 
|  | dir_pw_unit_test, | 
|  | ] | 
|  | } | 
|  |  | 
|  | # 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" ] | 
|  | } | 
|  |  | 
|  | config("fuzzing_verbose_logging") { | 
|  | defines = [ "FUZZING_VERBOSE_LOGGING" ] | 
|  | } | 
|  |  | 
|  | pw_doc_group("docs") { | 
|  | inputs = [ "doc_resources/pw_fuzzer_coverage_guided.png" ] | 
|  | sources = [ "docs.rst" ] | 
|  | } | 
|  |  | 
|  | # Sample fuzzer | 
|  | pw_fuzzer("toy_fuzzer") { | 
|  | sources = [ "examples/toy_fuzzer.cc" ] | 
|  | deps = [ "$dir_pw_string" ] | 
|  | } | 
|  |  | 
|  | pw_test_group("tests") { | 
|  | tests = [ ":toy_fuzzer" ] | 
|  | } |