blob: 565858b33275e9765539cbb41f16cea22fc1f708 [file] [log] [blame]
# Copyright 2020 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/rust/rustc_staticlib.gni")
# Defines a Rust fuzzer
#
# The template creates a Rust static library with fuzz target functions enabled
# and provides it to the usual `fuzzer` template. This can create three
# different outputs based on the toolchain variant applied to the target:
#
# - If the toolchain variant is "rust-*san-fuzzer", this will build a fuzzer
# which instruments the Rust code of the target and its dependencies.
#
# - If the toolchain variant is "*san-fuzzer", this will build a fuzzer which
# instruments the C/C++ code of the target and its dependencies.
#
# - Otherwise, it will build an uninstrumented unit test that ensures the
# fuzzer code can build and link.
#
# Parameters:
#
# rustfunction (optional)
# Name of the fuzz target function. Defaults to the GN target name.
# See also https://llvm.org/docs/LibFuzzer.html#fuzz-target.
#
# version
# edition
# configs
# data_deps
# deps
# source_root
# enforce_source_listing
# sources
# inputs
# features
# rustenv
# disable_rbe
# Same meaning as for rustc_staticlib.
#
# cmx
# dictionary
# options
# executable parameters, except deps
# Same meaning as for fuzzer.
#
# Example:
#
# In src/lib.rs:
# fuzz!("my_fuzzer", (input: ArbitraryType), { ... });
#
# In BUILD.gn:
# rust_fuzzer("my_fuzzer") {}
#
template("rustc_fuzzer") {
fuzzer_name = target_name
fuzzer_lib = "${fuzzer_name}_lib"
fuzzer_cfg = "${fuzzer_name}_cfg"
rustfunction = target_name
if (defined(invoker.rustfunction)) {
rustfunction = invoker.rustfunction
}
if (defined(invoker.original_target_name)) {
_original_target_name = invoker.original_target_name
} else {
_original_target_name = target_name
}
config(fuzzer_cfg) {
visibility = [ ":*" ]
rustflags = [
"--cfg=fuzz",
"--cfg=fuzz_target=\"$rustfunction\"",
]
# fxbug.dev/94239: rustc multiple codegen units seems incompatible
# with fuzzing.
configs = [ "//build/config/rust:one_codegen_unit" ]
}
rustc_staticlib(fuzzer_lib) {
forward_variables_from(invoker,
[
"deps",
"version",
"edition",
"source_root",
"enforce_source_listing",
"sources",
"inputs",
"features",
"rustenv",
])
visibility = [ ":*" ]
testonly = true
configs = []
configs = [ ":$fuzzer_cfg" ] + invoker.configs
original_target_name = _original_target_name
}
fuzzer(fuzzer_name) {
forward_variables_from(invoker,
"*",
[
"version",
"edition",
"deps",
"source_root",
"sources",
"inputs",
"features",
"configs",
])
deps = [ ":$fuzzer_lib" ]
# fxbug.dev/94239: rustc multiple codegen units seems incompatible
# with fuzzing.
configs += [ "//build/config/rust:one_codegen_unit" ]
}
}
set_defaults("rustc_fuzzer") {
configs = default_common_binary_configs + default_rust_configs
}