• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15import("//build_overrides/pigweed.gni")
16
17import("$dir_pw_build/target_types.gni")
18import("$dir_pw_docgen/docs.gni")
19import("$dir_pw_fuzzer/fuzzer.gni")
20
21config("public_include_path") {
22  include_dirs = [ "public" ]
23  visibility = [ ":*" ]
24}
25
26# Add flags for adding LLVM sanitizer coverage for fuzzing. This is added by
27# the host_clang_fuzz toolchains.
28config("instrumentation") {
29  if (pw_toolchain_OSS_FUZZ_ENABLED) {
30    # OSS-Fuzz manipulates compiler flags directly. See
31    # google.github.io/oss-fuzz/getting-started/new-project-guide/#Requirements.
32    cflags_c = string_split(getenv("CFLAGS"))
33    cflags_cc = string_split(getenv("CXXFLAGS"))
34
35    # OSS-Fuzz sets "-stdlib=libc++", which conflicts with the "-nostdinc++" set
36    # by `pw_minimal_cpp_stdlib`.
37    cflags_cc += [ "-Wno-unused-command-line-argument" ]
38  } else {
39    cflags = [ "-fsanitize=fuzzer-no-link" ]
40  }
41}
42
43# Add flags for linking against compiler-rt's libFuzzer. This is added
44# automatically by `pw_fuzzer`.
45config("engine") {
46  if (pw_toolchain_OSS_FUZZ_ENABLED) {
47    # OSS-Fuzz manipulates linker flags directly. See
48    # google.github.io/oss-fuzz/getting-started/new-project-guide/#Requirements.
49    ldflags = string_split(getenv("LDFLAGS")) + [ getenv("LIB_FUZZING_ENGINE") ]
50  } else {
51    ldflags = [ "-fsanitize=fuzzer" ]
52  }
53}
54
55pw_source_set("pw_fuzzer") {
56  public_configs = [ ":public_include_path" ]
57  public = [
58    "public/pw_fuzzer/asan_interface.h",
59    "public/pw_fuzzer/fuzzed_data_provider.h",
60  ]
61  public_deps = [ "$dir_pw_log" ]
62}
63
64pw_source_set("run_as_unit_test") {
65  configs = [ ":public_include_path" ]
66  sources = [ "pw_fuzzer_disabled.cc" ]
67  deps = [
68    dir_pw_log,
69    dir_pw_unit_test,
70  ]
71}
72
73# See https://llvm.org/docs/LibFuzzer.html#fuzzer-friendly-build-mode
74config("fuzzing_build_mode_unsafe_for_production") {
75  defines = [ "FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION" ]
76}
77
78config("fuzzing_verbose_logging") {
79  defines = [ "FUZZING_VERBOSE_LOGGING" ]
80}
81
82pw_doc_group("docs") {
83  inputs = [ "doc_resources/pw_fuzzer_coverage_guided.png" ]
84  sources = [ "docs.rst" ]
85}
86
87# Sample fuzzer
88pw_fuzzer("toy_fuzzer") {
89  sources = [ "examples/toy_fuzzer.cc" ]
90  deps = [
91    ":pw_fuzzer",
92    dir_pw_status,
93  ]
94}
95
96pw_test_group("tests") {
97  tests = [ ":toy_fuzzer_test" ]
98}
99
100group("fuzzers") {
101  deps = [ ":toy_fuzzer" ]
102}
103