aboutsummaryrefslogtreecommitdiff
path: root/pw_fuzzer/fuzzer.gni
blob: a43a2d9cb9d7bcee8a91944de3cb0724efb1cf1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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.

import("//build_overrides/pigweed.gni")

import("$dir_pw_build/error.gni")
import("$dir_pw_toolchain/host_clang/toolchains.gni")
import("$dir_pw_unit_test/test.gni")

# Creates a libFuzzer-based fuzzer executable target and unit test
#
# This will link `sources` and `deps` with the libFuzzer compiler runtime. The
# `sources` and `deps` should include a definition of the standard LLVM fuzz
# target function, `LLVMFuzzerTestOneInput`. For more details, see:
#   //pw_fuzzer/docs.rst
#   https://llvm.org/docs/LibFuzzer.html
#
# Additionally, this creates a unit test that does not generate fuzzer inputs
# and simply executes the fuzz target function with fixed inputs. This is useful
# for verifying the fuzz target function compiles, links, and runs even when not
# using a fuzzing-capable host or toolchain.
#
# Args:
#   - enable_test_if: (optional) Passed as `enable_if` to the unit test.
#   Remaining arguments are the same as `pw_executable`.
#
template("pw_fuzzer") {
  if (!pw_toolchain_FUZZING_ENABLED) {
    pw_error(target_name) {
      message_lines = [ "Toolchain does not enable fuzzing." ]
    }
    not_needed(invoker, "*")
  } else if (pw_toolchain_SANITIZERS == []) {
    pw_error(target_name) {
      message_lines = [ "No sanitizer runtime set." ]
    }
    not_needed(invoker, "*")
  } else {
    pw_executable(target_name) {
      configs = []
      deps = []
      forward_variables_from(invoker,
                             "*",
                             [
                               "enable_test_if",
                               "visibility",
                             ])
      forward_variables_from(invoker, [ "visibility" ])
      configs += [ "$dir_pw_fuzzer:engine" ]
      deps += [ dir_pw_fuzzer ]
    }
  }

  pw_test("${target_name}_test") {
    deps = []
    forward_variables_from(invoker, "*", [ "visibility" ])
    forward_variables_from(invoker, [ "visibility" ])
    deps += [ "$dir_pw_fuzzer:run_as_unit_test" ]
    enable_if = !defined(enable_test_if) || enable_test_if
  }
}