| # Copyright 2019 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| import("//build/fuzzing/fuzzer.gni") |
| import("//build/go/go_build.gni") |
| import("//build/go/go_library.gni") |
| |
| go_fuzzer_wrapper_template = |
| read_file("//build/go/go_fuzzer_wrapper.go", "string") |
| |
| # Defines a Go fuzz target. |
| # |
| # gopackage (required) |
| # The Go package exporting the target "Fuzz" function. |
| # |
| # deps (required) |
| # List of labels representing go_library targets this target depends on. |
| # |
| # tags (optional) |
| # List of additional Go build tags to specify for the build. |
| # |
| # Additionally, the `cmx`, `dictionary`, and `options` parameters |
| # defined for `fuzzer` are supported. |
| |
| template("go_fuzzer") { |
| assert(defined(invoker.gopackage), |
| "gopackage must be defined for $target_name") |
| assert(defined(invoker.deps), "deps must be defined for $target_name") |
| |
| fuzzer_name = target_name |
| |
| # Building a Go fuzzer involves a few steps: |
| # - Generate the wrapper source file. |
| # - Define a go_library rule containing that source file. |
| # - Build the wrapper package as a C archive with Go's libfuzzer instrumentation. |
| # - Link the resulting C archive as though it was a normal C++ fuzzer. |
| |
| write_file("${target_gen_dir}/${fuzzer_name}_wrapper.go", |
| "// Code generated by //build/go/go_fuzzer.gni; DO NOT EDIT.\n\n" + |
| string_replace(go_fuzzer_wrapper_template, |
| "GO_FUZZER_PKG", |
| invoker.gopackage)) |
| |
| go_library("${fuzzer_name}_wrapper_library") { |
| name = "${fuzzer_name}_wrapper" |
| source_dir = "${target_gen_dir}" |
| sources = [ |
| "${fuzzer_name}_main.go", |
| ] |
| |
| forward_variables_from(invoker, [ "deps" ]) |
| } |
| |
| go_build("${fuzzer_name}_wrapper") { |
| gopackages = [ "${fuzzer_name}_wrapper" ] |
| fuzzer = true |
| forward_variables_from(invoker, [ "tags" ]) |
| if (!defined(tags)) { |
| tags = [] |
| } |
| tags += [ "libfuzzer" ] |
| deps = [ |
| ":${fuzzer_name}_wrapper_library", |
| ] |
| } |
| |
| fuzzer("${fuzzer_name}") { |
| libs = [ "${root_out_dir}/${fuzzer_name}_wrapper.a" ] |
| deps = [ |
| ":${fuzzer_name}_wrapper", |
| ] |
| forward_variables_from(invoker, |
| [ |
| "cmx", |
| "dictionary", |
| "options", |
| ]) |
| } |
| } |