blob: 4a6fbf438deb4a681cbdb40e2e18c26a08f91c5f [file] [log] [blame]
Aaron Greenf3c3d2b2020-04-02 23:12:31 -07001# 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
Armando Montanezfb3d3fb2020-06-09 18:12:12 -070015import("//build_overrides/pigweed.gni")
16
Alexei Frolov844ff0f2020-05-06 12:15:29 -070017import("$dir_pw_toolchain/host_clang/toolchains.gni")
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070018import("$dir_pw_unit_test/test.gni")
19
20# Creates a libFuzzer-based fuzzer executable target.
21#
22# This will link `sources` and `deps` with the libFuzzer compiler runtime. The
23# `sources` and `deps` should include a definition of the standard LLVM fuzz
24# target function, `LLVMFuzzerTestOneInput`. For more details, see:
25# //pw_fuzzer/docs.rst
26# https://llvm.org/docs/LibFuzzer.html
27#
28template("pw_fuzzer") {
29 # This currently is ONLY supported on Linux using clang (debug).
30 # TODO(pwbug/179): Add Darwin, Windows here after testing.
31 fuzzing_platforms = [ "linux" ]
32 fuzzing_toolchains =
33 [ get_path_info("$dir_pw_toolchain:host_clang_og", "abspath") ]
34
35 # This is how GN says 'elem in list':
36 can_fuzz = fuzzing_platforms + [ host_os ] - [ host_os ] != fuzzing_platforms
37 can_fuzz = fuzzing_toolchains + [ current_toolchain ] -
38 [ current_toolchain ] != fuzzing_toolchains && can_fuzz
Aaron Greenb3ae1dc2020-04-13 11:37:05 -070039 if (can_fuzz && pw_sanitizer != "") {
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070040 # Build the actual fuzzer using the fuzzing config.
41 pw_executable(target_name) {
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070042 forward_variables_from(invoker, "*", [ "visibility" ])
43 forward_variables_from(invoker, [ "visibility" ])
Alexei Frolov844ff0f2020-05-06 12:15:29 -070044 if (!defined(configs)) {
45 configs = []
46 }
Aaron Green33c67822020-04-06 13:47:38 -070047 configs += [
48 "$dir_pw_fuzzer:default_config",
Aaron Greenb3ae1dc2020-04-13 11:37:05 -070049 "$dir_pw_fuzzer:sanitize_${pw_sanitizer}",
Aaron Green33c67822020-04-06 13:47:38 -070050 ]
Aaron Greenb3ae1dc2020-04-13 11:37:05 -070051 if (oss_fuzz_enabled) {
52 configs += [ "$dir_pw_fuzzer:oss_fuzz" ]
53 } else {
54 configs += [ "$dir_pw_fuzzer:fuzzing" ]
55 }
Aaron Green58bba012020-04-06 15:49:04 -070056
57 # Metadata for this fuzzer when used as part of a pw_test_group target.
58 metadata = {
59 tests = [
60 {
61 type = "fuzzer"
62 test_name = target_name
63 test_directory = rebase_path(target_out_dir, root_build_dir)
64 },
65 ]
66 }
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070067 }
68
69 # Dummy target to satisfy `pw_test_group`. It is empty as we don't want to
70 # automatically run fuzzers.
71 group(target_name + "_run") {
72 }
73 } else {
74 # Build a unit test that exercise the fuzz target function.
75 pw_test(target_name) {
Armando Montanez15b1daf2020-05-08 10:12:30 -070076 # TODO(pwbug/195): Re-enable when there's better configurability for
77 # on-device fuzz testing.
78 enable_if = false
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070079 sources = []
80 deps = []
81 forward_variables_from(invoker, "*", [ "visibility" ])
82 forward_variables_from(invoker, [ "visibility" ])
Aaron Green33c67822020-04-06 13:47:38 -070083 sources += [ "$dir_pw_fuzzer/pw_fuzzer_disabled.cc" ]
Wyatt Heplerc5e511e2020-06-12 16:56:22 -070084 deps += [ "$dir_pw_fuzzer:run_as_unit_test" ]
Aaron Greenf3c3d2b2020-04-02 23:12:31 -070085 }
86 }
87}