1*61c4878aSAndroid Build Coastguard Worker# Copyright 2020 The Pigweed Authors 2*61c4878aSAndroid Build Coastguard Worker# 3*61c4878aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4*61c4878aSAndroid Build Coastguard Worker# use this file except in compliance with the License. You may obtain a copy of 5*61c4878aSAndroid Build Coastguard Worker# the License at 6*61c4878aSAndroid Build Coastguard Worker# 7*61c4878aSAndroid Build Coastguard Worker# https://www.apache.org/licenses/LICENSE-2.0 8*61c4878aSAndroid Build Coastguard Worker# 9*61c4878aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*61c4878aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11*61c4878aSAndroid Build Coastguard Worker# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12*61c4878aSAndroid Build Coastguard Worker# License for the specific language governing permissions and limitations under 13*61c4878aSAndroid Build Coastguard Worker# the License. 14*61c4878aSAndroid Build Coastguard Worker 15*61c4878aSAndroid Build Coastguard Workerimport("//build_overrides/pigweed.gni") 16*61c4878aSAndroid Build Coastguard Worker 17*61c4878aSAndroid Build Coastguard Workerimport("$dir_pw_build/error.gni") 18*61c4878aSAndroid Build Coastguard Workerimport("$dir_pw_build/target_types.gni") 19*61c4878aSAndroid Build Coastguard Workerimport("$dir_pw_docgen/docs.gni") 20*61c4878aSAndroid Build Coastguard Workerimport("$dir_pw_third_party/fuzztest/fuzztest.gni") 21*61c4878aSAndroid Build Coastguard Workerimport("$dir_pw_unit_test/test.gni") 22*61c4878aSAndroid Build Coastguard Worker 23*61c4878aSAndroid Build Coastguard Workerconfig("public_include_path") { 24*61c4878aSAndroid Build Coastguard Worker include_dirs = [ "public" ] 25*61c4878aSAndroid Build Coastguard Worker visibility = [ ":*" ] 26*61c4878aSAndroid Build Coastguard Worker} 27*61c4878aSAndroid Build Coastguard Worker 28*61c4878aSAndroid Build Coastguard Workerconfig("private_include_path") { 29*61c4878aSAndroid Build Coastguard Worker include_dirs = [ "private" ] 30*61c4878aSAndroid Build Coastguard Worker visibility = [ ":*" ] 31*61c4878aSAndroid Build Coastguard Worker} 32*61c4878aSAndroid Build Coastguard Worker 33*61c4878aSAndroid Build Coastguard Workerconfig("overrides_include_path") { 34*61c4878aSAndroid Build Coastguard Worker include_dirs = [ 35*61c4878aSAndroid Build Coastguard Worker "public_overrides", 36*61c4878aSAndroid Build Coastguard Worker "private_overrides", 37*61c4878aSAndroid Build Coastguard Worker ] 38*61c4878aSAndroid Build Coastguard Worker visibility = [ ":*" ] 39*61c4878aSAndroid Build Coastguard Worker} 40*61c4878aSAndroid Build Coastguard Worker 41*61c4878aSAndroid Build Coastguard Worker# See https://llvm.org/docs/LibFuzzer.html#fuzzer-friendly-build-mode 42*61c4878aSAndroid Build Coastguard Workerconfig("fuzzing_build_mode_unsafe_for_production") { 43*61c4878aSAndroid Build Coastguard Worker defines = [ "FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION" ] 44*61c4878aSAndroid Build Coastguard Worker} 45*61c4878aSAndroid Build Coastguard Worker 46*61c4878aSAndroid Build Coastguard Workerpw_doc_group("docs") { 47*61c4878aSAndroid Build Coastguard Worker sources = [ 48*61c4878aSAndroid Build Coastguard Worker "concepts.rst", 49*61c4878aSAndroid Build Coastguard Worker "docs.rst", 50*61c4878aSAndroid Build Coastguard Worker "guides/fuzztest.rst", 51*61c4878aSAndroid Build Coastguard Worker "guides/index.rst", 52*61c4878aSAndroid Build Coastguard Worker "guides/libfuzzer.rst", 53*61c4878aSAndroid Build Coastguard Worker "guides/reproducing_oss_fuzz_bugs.rst", 54*61c4878aSAndroid Build Coastguard Worker ] 55*61c4878aSAndroid Build Coastguard Worker inputs = [ 56*61c4878aSAndroid Build Coastguard Worker "doc_resources/pw_fuzzer_coverage_guided.png", 57*61c4878aSAndroid Build Coastguard Worker "examples/fuzztest/BUILD.gn", 58*61c4878aSAndroid Build Coastguard Worker "examples/fuzztest/BUILD.bazel", 59*61c4878aSAndroid Build Coastguard Worker "examples/fuzztest/CMakeLists.txt", 60*61c4878aSAndroid Build Coastguard Worker "examples/fuzztest/metrics.h", 61*61c4878aSAndroid Build Coastguard Worker "examples/fuzztest/metrics_unittest.cc", 62*61c4878aSAndroid Build Coastguard Worker "examples/fuzztest/metrics_fuzztest.cc", 63*61c4878aSAndroid Build Coastguard Worker ] 64*61c4878aSAndroid Build Coastguard Worker} 65*61c4878aSAndroid Build Coastguard Worker 66*61c4878aSAndroid Build Coastguard Workerpw_test_group("tests") { 67*61c4878aSAndroid Build Coastguard Worker group_deps = [ 68*61c4878aSAndroid Build Coastguard Worker ":fuzztest_tests", 69*61c4878aSAndroid Build Coastguard Worker "examples/fuzztest:tests", 70*61c4878aSAndroid Build Coastguard Worker "examples/libfuzzer:tests", 71*61c4878aSAndroid Build Coastguard Worker ] 72*61c4878aSAndroid Build Coastguard Worker} 73*61c4878aSAndroid Build Coastguard Worker 74*61c4878aSAndroid Build Coastguard Workerpw_source_set("asan_default_options") { 75*61c4878aSAndroid Build Coastguard Worker sources = [ "asan_default_options.c" ] 76*61c4878aSAndroid Build Coastguard Worker} 77*61c4878aSAndroid Build Coastguard Worker 78*61c4878aSAndroid Build Coastguard Worker################################################################################ 79*61c4878aSAndroid Build Coastguard Worker# FuzzTest support 80*61c4878aSAndroid Build Coastguard Worker# 81*61c4878aSAndroid Build Coastguard Worker# Create FuzzTest-style fuzzers by adding a dep on dir_pw_fuzzer:fuzztest 82*61c4878aSAndroid Build Coastguard Worker 83*61c4878aSAndroid Build Coastguard Workergroup("fuzztest") { 84*61c4878aSAndroid Build Coastguard Worker if (dir_pw_third_party_fuzztest != "" && pw_toolchain_FUZZING_ENABLED) { 85*61c4878aSAndroid Build Coastguard Worker public_deps = [ ":fuzztest.enabled" ] 86*61c4878aSAndroid Build Coastguard Worker } else { 87*61c4878aSAndroid Build Coastguard Worker public_deps = [ ":fuzztest.disabled" ] 88*61c4878aSAndroid Build Coastguard Worker } 89*61c4878aSAndroid Build Coastguard Worker} 90*61c4878aSAndroid Build Coastguard Worker 91*61c4878aSAndroid Build Coastguard Worker# Used by fuzzable unit tests when fuzzing is enabled. Includes headers and deps 92*61c4878aSAndroid Build Coastguard Worker# that provide a Pigweed-compatible subset of FuzzTest, as well as extensions to 93*61c4878aSAndroid Build Coastguard Worker# support common Pigweed types. 94*61c4878aSAndroid Build Coastguard Workerif (dir_pw_third_party_fuzztest != "" && pw_toolchain_FUZZING_ENABLED) { 95*61c4878aSAndroid Build Coastguard Worker pw_source_set("fuzztest.enabled") { 96*61c4878aSAndroid Build Coastguard Worker public = [ 97*61c4878aSAndroid Build Coastguard Worker "private/pw_fuzzer/internal/fuzztest.h", 98*61c4878aSAndroid Build Coastguard Worker "public/pw_fuzzer/fuzztest.h", 99*61c4878aSAndroid Build Coastguard Worker ] 100*61c4878aSAndroid Build Coastguard Worker public_configs = [ 101*61c4878aSAndroid Build Coastguard Worker ":public_include_path", 102*61c4878aSAndroid Build Coastguard Worker ":private_include_path", 103*61c4878aSAndroid Build Coastguard Worker ] 104*61c4878aSAndroid Build Coastguard Worker public_deps = [ 105*61c4878aSAndroid Build Coastguard Worker "$dir_pw_third_party/fuzztest/fuzztest:fuzztest_core", 106*61c4878aSAndroid Build Coastguard Worker dir_pw_containers, 107*61c4878aSAndroid Build Coastguard Worker dir_pw_result, 108*61c4878aSAndroid Build Coastguard Worker dir_pw_status, 109*61c4878aSAndroid Build Coastguard Worker dir_pw_string, 110*61c4878aSAndroid Build Coastguard Worker ] 111*61c4878aSAndroid Build Coastguard Worker } 112*61c4878aSAndroid Build Coastguard Worker} 113*61c4878aSAndroid Build Coastguard Worker 114*61c4878aSAndroid Build Coastguard Worker# Used by fuzzable unit tests when fuzzing is disabled. Includes stubs of the 115*61c4878aSAndroid Build Coastguard Worker# Pigweed-compatible subset of FuzzTest's interface, as well as extensions to 116*61c4878aSAndroid Build Coastguard Worker# support common Pigweed types. 117*61c4878aSAndroid Build Coastguard Workerpw_source_set("fuzztest.disabled") { 118*61c4878aSAndroid Build Coastguard Worker public = [ 119*61c4878aSAndroid Build Coastguard Worker "private_overrides/pw_fuzzer/internal/fuzztest.h", 120*61c4878aSAndroid Build Coastguard Worker "public/pw_fuzzer/fuzztest.h", 121*61c4878aSAndroid Build Coastguard Worker "public_overrides/fuzztest/fuzztest.h", 122*61c4878aSAndroid Build Coastguard Worker ] 123*61c4878aSAndroid Build Coastguard Worker public_configs = [ 124*61c4878aSAndroid Build Coastguard Worker ":public_include_path", 125*61c4878aSAndroid Build Coastguard Worker ":overrides_include_path", 126*61c4878aSAndroid Build Coastguard Worker ] 127*61c4878aSAndroid Build Coastguard Worker public_deps = [ 128*61c4878aSAndroid Build Coastguard Worker dir_pw_containers, 129*61c4878aSAndroid Build Coastguard Worker dir_pw_result, 130*61c4878aSAndroid Build Coastguard Worker dir_pw_status, 131*61c4878aSAndroid Build Coastguard Worker dir_pw_string, 132*61c4878aSAndroid Build Coastguard Worker ] 133*61c4878aSAndroid Build Coastguard Worker} 134*61c4878aSAndroid Build Coastguard Worker 135*61c4878aSAndroid Build Coastguard Workerpw_test("fuzztest_tests") { 136*61c4878aSAndroid Build Coastguard Worker sources = [ "domain_test.cc" ] 137*61c4878aSAndroid Build Coastguard Worker deps = [ ":fuzztest" ] 138*61c4878aSAndroid Build Coastguard Worker} 139*61c4878aSAndroid Build Coastguard Worker 140*61c4878aSAndroid Build Coastguard Worker# This target should only be used when defining a fuzzing toolchain, e.g. to set 141*61c4878aSAndroid Build Coastguard Worker# `pw_unit_test_BACKEND = "$dir_pw_fuzzer:gtest" 142*61c4878aSAndroid Build Coastguard Worker# TODO: b/295961502 - Support running FuzzTest-based fuzzers on OSS-Fuzz. 143*61c4878aSAndroid Build Coastguard Workerif (dir_pw_third_party_googletest == "") { 144*61c4878aSAndroid Build Coastguard Worker pw_error("gtest") { 145*61c4878aSAndroid Build Coastguard Worker message_lines = [ 146*61c4878aSAndroid Build Coastguard Worker "pw_unit_test_BACKEND is set to dir_pw_fuzzer:gtest, ", 147*61c4878aSAndroid Build Coastguard Worker "but dir_pw_third_party_googletest is not set.", 148*61c4878aSAndroid Build Coastguard Worker ] 149*61c4878aSAndroid Build Coastguard Worker } 150*61c4878aSAndroid Build Coastguard Worker} else if (!pw_toolchain_FUZZING_ENABLED) { 151*61c4878aSAndroid Build Coastguard Worker pw_error("gtest") { 152*61c4878aSAndroid Build Coastguard Worker message_lines = [ 153*61c4878aSAndroid Build Coastguard Worker "pw_unit_test_BACKEND is set to dir_pw_fuzzer:gtest, ", 154*61c4878aSAndroid Build Coastguard Worker "but $current_toolchain does not support fuzzing.", 155*61c4878aSAndroid Build Coastguard Worker ] 156*61c4878aSAndroid Build Coastguard Worker } 157*61c4878aSAndroid Build Coastguard Worker} else { 158*61c4878aSAndroid Build Coastguard Worker group("gtest") { 159*61c4878aSAndroid Build Coastguard Worker if (pw_toolchain_OSS_FUZZ_ENABLED) { 160*61c4878aSAndroid Build Coastguard Worker public_deps = [ "$dir_pw_unit_test:light" ] 161*61c4878aSAndroid Build Coastguard Worker } else { 162*61c4878aSAndroid Build Coastguard Worker public_deps = [ "$dir_pw_unit_test:googletest" ] 163*61c4878aSAndroid Build Coastguard Worker } 164*61c4878aSAndroid Build Coastguard Worker } 165*61c4878aSAndroid Build Coastguard Worker} 166*61c4878aSAndroid Build Coastguard Worker 167*61c4878aSAndroid Build Coastguard Worker# This target should only be used when defining a fuzzing toolchain, e.g. to set 168*61c4878aSAndroid Build Coastguard Worker# `pw_unit_test_MAIN = "$dir_pw_fuzzer:fuzztest_main" 169*61c4878aSAndroid Build Coastguard Worker# TODO: b/295961502 - Support running FuzzTest-based fuzzers on OSS-Fuzz. 170*61c4878aSAndroid Build Coastguard Workerif (dir_pw_third_party_fuzztest == "") { 171*61c4878aSAndroid Build Coastguard Worker pw_error("fuzztest_main") { 172*61c4878aSAndroid Build Coastguard Worker message_lines = [ 173*61c4878aSAndroid Build Coastguard Worker "pw_unit_test_MAIN is set to dir_pw_fuzzer:fuzztest_main, ", 174*61c4878aSAndroid Build Coastguard Worker "but dir_pw_third_party_fuzztest is not set.", 175*61c4878aSAndroid Build Coastguard Worker ] 176*61c4878aSAndroid Build Coastguard Worker } 177*61c4878aSAndroid Build Coastguard Worker} else if (!pw_toolchain_FUZZING_ENABLED) { 178*61c4878aSAndroid Build Coastguard Worker pw_error("fuzztest_main") { 179*61c4878aSAndroid Build Coastguard Worker message_lines = [ 180*61c4878aSAndroid Build Coastguard Worker "pw_unit_test_MAIN is set to dir_pw_fuzzer:fuzztest_main, ", 181*61c4878aSAndroid Build Coastguard Worker "but $current_toolchain does not support fuzzing.", 182*61c4878aSAndroid Build Coastguard Worker ] 183*61c4878aSAndroid Build Coastguard Worker } 184*61c4878aSAndroid Build Coastguard Worker} else { 185*61c4878aSAndroid Build Coastguard Worker group("fuzztest_main") { 186*61c4878aSAndroid Build Coastguard Worker if (pw_toolchain_OSS_FUZZ_ENABLED) { 187*61c4878aSAndroid Build Coastguard Worker deps = [ "$dir_pw_unit_test:simple_printing_main" ] 188*61c4878aSAndroid Build Coastguard Worker } else { 189*61c4878aSAndroid Build Coastguard Worker deps = [ 190*61c4878aSAndroid Build Coastguard Worker ":asan_default_options", 191*61c4878aSAndroid Build Coastguard Worker "$dir_pw_third_party/fuzztest/fuzztest:fuzztest_gtest_main", 192*61c4878aSAndroid Build Coastguard Worker ] 193*61c4878aSAndroid Build Coastguard Worker } 194*61c4878aSAndroid Build Coastguard Worker } 195*61c4878aSAndroid Build Coastguard Worker} 196*61c4878aSAndroid Build Coastguard Worker 197*61c4878aSAndroid Build Coastguard Worker################################################################################ 198*61c4878aSAndroid Build Coastguard Worker# libFuzzer support 199*61c4878aSAndroid Build Coastguard Worker# 200*61c4878aSAndroid Build Coastguard Worker# Create libFuzzer-style fuzzers by using the `pw_fuzzer` template from 201*61c4878aSAndroid Build Coastguard Worker# fuzzer.gni. 202*61c4878aSAndroid Build Coastguard Worker 203*61c4878aSAndroid Build Coastguard Worker# Add flags for linking against compiler-rt's libFuzzer. This is added 204*61c4878aSAndroid Build Coastguard Worker# automatically by `pw_fuzzer`. 205*61c4878aSAndroid Build Coastguard Workerconfig("libfuzzer_config") { 206*61c4878aSAndroid Build Coastguard Worker ldflags = [ "-fsanitize=fuzzer" ] 207*61c4878aSAndroid Build Coastguard Worker} 208*61c4878aSAndroid Build Coastguard Worker 209*61c4878aSAndroid Build Coastguard Worker# Includes wrapper's for LLVM's libFuzzer compiler runtime library. 210*61c4878aSAndroid Build Coastguard Workerpw_source_set("libfuzzer") { 211*61c4878aSAndroid Build Coastguard Worker public = [ 212*61c4878aSAndroid Build Coastguard Worker "public/pw_fuzzer/asan_interface.h", 213*61c4878aSAndroid Build Coastguard Worker "public/pw_fuzzer/fuzzed_data_provider.h", 214*61c4878aSAndroid Build Coastguard Worker ] 215*61c4878aSAndroid Build Coastguard Worker public_configs = [ ":public_include_path" ] 216*61c4878aSAndroid Build Coastguard Worker public_deps = [ dir_pw_log ] 217*61c4878aSAndroid Build Coastguard Worker} 218*61c4878aSAndroid Build Coastguard Worker 219*61c4878aSAndroid Build Coastguard Worker# This can be linked against fuzz target functions to create unit tests for 220*61c4878aSAndroid Build Coastguard Worker# them. 221*61c4878aSAndroid Build Coastguard Workerpw_source_set("libfuzzer_test") { 222*61c4878aSAndroid Build Coastguard Worker testonly = pw_unit_test_TESTONLY 223*61c4878aSAndroid Build Coastguard Worker sources = [ "pw_fuzzer_disabled.cc" ] 224*61c4878aSAndroid Build Coastguard Worker public_deps = [ ":libfuzzer" ] 225*61c4878aSAndroid Build Coastguard Worker deps = [ dir_pw_unit_test ] 226*61c4878aSAndroid Build Coastguard Worker} 227*61c4878aSAndroid Build Coastguard Worker 228*61c4878aSAndroid Build Coastguard Worker# libFuzzer-based fuzzers have a distinct dep graph. 229*61c4878aSAndroid Build Coastguard Workergroup("fuzzers") { 230*61c4878aSAndroid Build Coastguard Worker deps = [ "examples/libfuzzer:fuzzers" ] 231*61c4878aSAndroid Build Coastguard Worker} 232*61c4878aSAndroid Build Coastguard Worker 233*61c4878aSAndroid Build Coastguard Worker################################################################################ 234*61c4878aSAndroid Build Coastguard Worker# Local fuzzing support 235*61c4878aSAndroid Build Coastguard Worker 236*61c4878aSAndroid Build Coastguard Worker# Add flags for adding LLVM sanitizer coverage for fuzzing. This is added by 237*61c4878aSAndroid Build Coastguard Worker# the host_clang_fuzz toolchains. 238*61c4878aSAndroid Build Coastguard Workerconfig("instrumentation") { 239*61c4878aSAndroid Build Coastguard Worker cflags = [ "-fsanitize=fuzzer-no-link" ] 240*61c4878aSAndroid Build Coastguard Worker} 241*61c4878aSAndroid Build Coastguard Worker 242*61c4878aSAndroid Build Coastguard Worker################################################################################ 243*61c4878aSAndroid Build Coastguard Worker# OSS-Fuzz support 244*61c4878aSAndroid Build Coastguard Worker# 245*61c4878aSAndroid Build Coastguard Worker# OSS-Fuzz manipulates compiler and linker flags directly. See 246*61c4878aSAndroid Build Coastguard Worker# google.github.io/oss-fuzz/getting-started/new-project-guide/#Requirements. 247*61c4878aSAndroid Build Coastguard Worker# 248*61c4878aSAndroid Build Coastguard Worker# WARNING: This is not hermetic by design. It never can be, and never will be. 249*61c4878aSAndroid Build Coastguard Worker 250*61c4878aSAndroid Build Coastguard Workerconfig("oss_fuzz_instrumentation") { 251*61c4878aSAndroid Build Coastguard Worker cflags_c = string_split(getenv("CFLAGS")) 252*61c4878aSAndroid Build Coastguard Worker cflags_cc = string_split(getenv("CXXFLAGS")) 253*61c4878aSAndroid Build Coastguard Worker 254*61c4878aSAndroid Build Coastguard Worker # OSS-Fuzz sets "-stdlib=libc++", which conflicts with the "-nostdinc++" set 255*61c4878aSAndroid Build Coastguard Worker # by `pw_minimal_cpp_stdlib`. 256*61c4878aSAndroid Build Coastguard Worker if (cflags_cc + [ "-stdlib=libc++" ] - [ "-stdlib=libc++" ] != cflags_cc) { 257*61c4878aSAndroid Build Coastguard Worker cflags_cc += [ "-Wno-unused-command-line-argument" ] 258*61c4878aSAndroid Build Coastguard Worker } 259*61c4878aSAndroid Build Coastguard Worker 260*61c4878aSAndroid Build Coastguard Worker # Disable UBSan vptr when the target is built with -fno-rtti. 261*61c4878aSAndroid Build Coastguard Worker if (cflags_cc + [ "-fno-rtti" ] - [ "-fno-rtti" ] != cflags_cc) { 262*61c4878aSAndroid Build Coastguard Worker cflags_cc += [ " -fno-sanitize=vptr" ] 263*61c4878aSAndroid Build Coastguard Worker } 264*61c4878aSAndroid Build Coastguard Worker cflags_cc += [ "-fcoverage-compilation-dir=" + getenv("PW_ROOT") ] 265*61c4878aSAndroid Build Coastguard Worker 266*61c4878aSAndroid Build Coastguard Worker ldflags = cflags_cc + [ "-fuse-ld=lld" ] 267*61c4878aSAndroid Build Coastguard Worker} 268*61c4878aSAndroid Build Coastguard Worker 269*61c4878aSAndroid Build Coastguard Workerconfig("libfuzzer_oss_fuzz_config") { 270*61c4878aSAndroid Build Coastguard Worker engine = getenv("LIB_FUZZING_ENGINE") 271*61c4878aSAndroid Build Coastguard Worker if (engine == "") { 272*61c4878aSAndroid Build Coastguard Worker engine = "-fsanitize=fuzzer" 273*61c4878aSAndroid Build Coastguard Worker } 274*61c4878aSAndroid Build Coastguard Worker ldflags = [ engine ] 275*61c4878aSAndroid Build Coastguard Worker} 276