Aaron Green | f3c3d2b | 2020-04-02 23:12:31 -0700 | [diff] [blame] | 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 | |
Armando Montanez | fb3d3fb | 2020-06-09 18:12:12 -0700 | [diff] [blame] | 15 | import("//build_overrides/pigweed.gni") |
| 16 | |
Alexei Frolov | 844ff0f | 2020-05-06 12:15:29 -0700 | [diff] [blame] | 17 | import("$dir_pw_toolchain/host_clang/toolchains.gni") |
Aaron Green | f3c3d2b | 2020-04-02 23:12:31 -0700 | [diff] [blame] | 18 | import("$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 | # |
| 28 | template("pw_fuzzer") { |
Keir Mierle | 6a10636 | 2021-01-14 16:27:44 -0800 | [diff] [blame] | 29 | # This currently is ONLY supported on Linux and Mac using clang (debug). |
| 30 | # TODO(pwbug/179): Add Windows here after testing. |
| 31 | fuzzing_platforms = [ |
| 32 | "linux", |
| 33 | "mac", |
| 34 | ] |
Ali Zhang | 5876558 | 2021-01-29 12:04:06 -0800 | [diff] [blame] | 35 | |
| 36 | fuzzing_toolchains = [ "//targets/host:host_clang_fuzz" ] |
Aaron Green | f3c3d2b | 2020-04-02 23:12:31 -0700 | [diff] [blame] | 37 | |
| 38 | # This is how GN says 'elem in list': |
| 39 | can_fuzz = fuzzing_platforms + [ host_os ] - [ host_os ] != fuzzing_platforms |
Keir Mierle | 6a10636 | 2021-01-14 16:27:44 -0800 | [diff] [blame] | 40 | |
Aaron Green | f3c3d2b | 2020-04-02 23:12:31 -0700 | [diff] [blame] | 41 | can_fuzz = fuzzing_toolchains + [ current_toolchain ] - |
| 42 | [ current_toolchain ] != fuzzing_toolchains && can_fuzz |
Keir Mierle | 6a10636 | 2021-01-14 16:27:44 -0800 | [diff] [blame] | 43 | |
Ali Zhang | 5876558 | 2021-01-29 12:04:06 -0800 | [diff] [blame] | 44 | if (can_fuzz && pw_toolchain_SANITIZERS != []) { |
Aaron Green | f3c3d2b | 2020-04-02 23:12:31 -0700 | [diff] [blame] | 45 | # Build the actual fuzzer using the fuzzing config. |
| 46 | pw_executable(target_name) { |
Aaron Green | f3c3d2b | 2020-04-02 23:12:31 -0700 | [diff] [blame] | 47 | forward_variables_from(invoker, "*", [ "visibility" ]) |
| 48 | forward_variables_from(invoker, [ "visibility" ]) |
Alexei Frolov | 844ff0f | 2020-05-06 12:15:29 -0700 | [diff] [blame] | 49 | if (!defined(configs)) { |
| 50 | configs = [] |
| 51 | } |
Ali Zhang | dc4510a | 2021-01-26 15:01:20 -0800 | [diff] [blame] | 52 | configs += [ "$dir_pw_fuzzer:default_config" ] |
Keir Mierle | 6a10636 | 2021-01-14 16:27:44 -0800 | [diff] [blame] | 53 | if (pw_toolchain_OSS_FUZZ_ENABLED) { |
Aaron Green | b3ae1dc | 2020-04-13 11:37:05 -0700 | [diff] [blame] | 54 | configs += [ "$dir_pw_fuzzer:oss_fuzz" ] |
| 55 | } else { |
| 56 | configs += [ "$dir_pw_fuzzer:fuzzing" ] |
| 57 | } |
Aaron Green | 58bba01 | 2020-04-06 15:49:04 -0700 | [diff] [blame] | 58 | |
Ali Zhang | 5876558 | 2021-01-29 12:04:06 -0800 | [diff] [blame] | 59 | _fuzzer_output_dir = "${target_out_dir}/bin" |
| 60 | if (defined(invoker.output_dir)) { |
| 61 | _fuzzer_output_dir = invoker.output_dir |
| 62 | } |
| 63 | output_dir = _fuzzer_output_dir |
| 64 | |
Aaron Green | 58bba01 | 2020-04-06 15:49:04 -0700 | [diff] [blame] | 65 | # Metadata for this fuzzer when used as part of a pw_test_group target. |
| 66 | metadata = { |
| 67 | tests = [ |
| 68 | { |
| 69 | type = "fuzzer" |
| 70 | test_name = target_name |
Ali Zhang | 5876558 | 2021-01-29 12:04:06 -0800 | [diff] [blame] | 71 | test_directory = rebase_path(output_dir, root_build_dir) |
Aaron Green | 58bba01 | 2020-04-06 15:49:04 -0700 | [diff] [blame] | 72 | }, |
| 73 | ] |
| 74 | } |
Aaron Green | f3c3d2b | 2020-04-02 23:12:31 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | # Dummy target to satisfy `pw_test_group`. It is empty as we don't want to |
| 78 | # automatically run fuzzers. |
Alexei Frolov | 69dccfd | 2020-11-13 12:10:24 -0800 | [diff] [blame] | 79 | group(target_name + ".run") { |
Aaron Green | f3c3d2b | 2020-04-02 23:12:31 -0700 | [diff] [blame] | 80 | } |
Ali Zhang | 5876558 | 2021-01-29 12:04:06 -0800 | [diff] [blame] | 81 | |
| 82 | # Dummy target to satisfy `pw_test`. It is empty as we don't need a separate |
| 83 | # lib target. |
| 84 | group(target_name + ".lib") { |
| 85 | } |
Aaron Green | f3c3d2b | 2020-04-02 23:12:31 -0700 | [diff] [blame] | 86 | } else { |
| 87 | # Build a unit test that exercise the fuzz target function. |
| 88 | pw_test(target_name) { |
Armando Montanez | 15b1daf | 2020-05-08 10:12:30 -0700 | [diff] [blame] | 89 | # TODO(pwbug/195): Re-enable when there's better configurability for |
| 90 | # on-device fuzz testing. |
| 91 | enable_if = false |
Aaron Green | f3c3d2b | 2020-04-02 23:12:31 -0700 | [diff] [blame] | 92 | sources = [] |
| 93 | deps = [] |
| 94 | forward_variables_from(invoker, "*", [ "visibility" ]) |
| 95 | forward_variables_from(invoker, [ "visibility" ]) |
Aaron Green | 33c6782 | 2020-04-06 13:47:38 -0700 | [diff] [blame] | 96 | sources += [ "$dir_pw_fuzzer/pw_fuzzer_disabled.cc" ] |
Wyatt Hepler | c5e511e | 2020-06-12 16:56:22 -0700 | [diff] [blame] | 97 | deps += [ "$dir_pw_fuzzer:run_as_unit_test" ] |
Aaron Green | f3c3d2b | 2020-04-02 23:12:31 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } |