blob: 93109442b364d344572a8fc6acac1f72156c6dd2 [file] [log] [blame]
aizatskycb1a43932015-12-14 20:59:141# Copyright 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
aizatsky7dfb57342015-12-21 04:02:525# Defines fuzzer_test.
6#
Max Moroz915eb5a2018-04-24 16:10:357import("//build/config/coverage/coverage.gni")
aizatsky43d459a52016-02-13 01:22:188import("//build/config/features.gni")
9import("//build/config/sanitizers/sanitizers.gni")
aizatskycb1a43932015-12-14 20:59:1410import("//testing/test.gni")
11
12# fuzzer_test is used to define individual libfuzzer tests.
13#
14# Supported attributes:
15# - (required) sources - fuzzer test source files
aizatskycb1a43932015-12-14 20:59:1416# - deps - test dependencies
17# - additional_configs - additional configs to be used for compilation
18# - dict - a dictionary file for the fuzzer.
mmoroz344a75ae2016-03-02 16:57:1719# - libfuzzer_options - options for the fuzzer (e.g. -max_len or -timeout).
aizatsky198e3022016-03-08 21:33:4620# - seed_corpus - a directory with seed corpus.
Calder Kitagawa90e9fb12018-05-07 16:08:5721# - seed_corpus_deps - dependencies for generating the seed corpus.
Abhishek Aryac73294d2018-04-26 22:12:4322# - skip_owners - if true, skips writing the owners file.
aizatskycb1a43932015-12-14 20:59:1423#
aizatsky43d459a52016-02-13 01:22:1824# If use_libfuzzer gn flag is defined, then proper fuzzer would be build.
metzman8c9ed0e2016-06-21 20:37:2025# Without use_libfuzzer or use_afl a unit-test style binary would be built on
26# linux and the whole target is a no-op otherwise.
aizatsky43d459a52016-02-13 01:22:1827#
aizatskycb1a43932015-12-14 20:59:1428# The template wraps test() target with appropriate dependencies.
mmoroz344a75ae2016-03-02 16:57:1729# If any test run-time options are present (dict or libfuzzer_options), then a
30# config (.options file) file would be generated or modified in root output
aizatskycb1a43932015-12-14 20:59:1431# dir (next to test).
32template("fuzzer_test") {
Max Moroz915eb5a2018-04-24 16:10:3533 assert(!(is_mac && use_clang_coverage && use_fuzzing_engine),
34 "Code Coverage for fuzz targets does not work on Mac until " +
35 "crbug.com/790747 is resolved.")
36
Jonathan Metzmand901d1b2017-10-09 23:07:2837 if (!disable_libfuzzer && (use_fuzzing_engine || use_drfuzz || is_linux)) {
aizatsky43d459a52016-02-13 01:22:1838 assert(defined(invoker.sources), "Need sources in $target_name.")
aizatskycb1a43932015-12-14 20:59:1439
aizatsky43d459a52016-02-13 01:22:1840 test_deps = [ "//testing/libfuzzer:libfuzzer_main" ]
aizatskycb1a43932015-12-14 20:59:1441
aizatsky43d459a52016-02-13 01:22:1842 if (defined(invoker.deps)) {
43 test_deps += invoker.deps
44 }
aizatskycb1a43932015-12-14 20:59:1445
mmoroz902ef432017-02-07 17:03:3746 if (defined(invoker.seed_corpus) || defined(invoker.seed_corpuses)) {
47 assert(!(defined(invoker.seed_corpus) && defined(invoker.seed_corpuses)),
48 "Do not use both seed_corpus and seed_corpuses for $target_name.")
49
aizatsky198e3022016-03-08 21:33:4650 out = "$root_build_dir/$target_name" + "_seed_corpus.zip"
51
Calder Kitagawa90e9fb12018-05-07 16:08:5752 seed_corpus_deps = []
53
54 if (defined(invoker.seed_corpus_deps)) {
55 seed_corpus_deps += invoker.seed_corpus_deps
56 }
57
aizatsky198e3022016-03-08 21:33:4658 action(target_name + "_seed_corpus") {
59 script = "//testing/libfuzzer/archive_corpus.py"
mmoroz902ef432017-02-07 17:03:3760
aizatsky198e3022016-03-08 21:33:4661 args = [
aizatsky198e3022016-03-08 21:33:4662 "--output",
wychen5e1f7252017-05-30 08:05:1063 rebase_path(out, root_build_dir),
aizatsky198e3022016-03-08 21:33:4664 ]
65
mmoroz902ef432017-02-07 17:03:3766 if (defined(invoker.seed_corpus)) {
wychen5e1f7252017-05-30 08:05:1067 args += [ rebase_path(invoker.seed_corpus, root_build_dir) ]
mmoroz902ef432017-02-07 17:03:3768 }
69
70 if (defined(invoker.seed_corpuses)) {
71 foreach(seed_corpus_path, invoker.seed_corpuses) {
wychen5e1f7252017-05-30 08:05:1072 args += [ rebase_path(seed_corpus_path, root_build_dir) ]
mmoroz902ef432017-02-07 17:03:3773 }
74 }
75
aizatsky198e3022016-03-08 21:33:4676 outputs = [
77 out,
78 ]
mmoroz902ef432017-02-07 17:03:3779
Calder Kitagawa90e9fb12018-05-07 16:08:5780 deps = [ "//testing/libfuzzer:seed_corpus" ] + seed_corpus_deps
aizatsky198e3022016-03-08 21:33:4681 }
82
83 test_deps += [ ":" + target_name + "_seed_corpus" ]
84 }
85
mmoroz062a4a62016-04-12 09:02:3386 if (defined(invoker.dict) || defined(invoker.libfuzzer_options)) {
87 if (defined(invoker.dict)) {
88 # Copy dictionary to output.
89 copy(target_name + "_dict_copy") {
90 sources = [
91 invoker.dict,
92 ]
93 outputs = [
94 "$root_build_dir/" + target_name + ".dict",
95 ]
96 }
97 test_deps += [ ":" + target_name + "_dict_copy" ]
aizatsky43d459a52016-02-13 01:22:1898 }
99
mmoroz062a4a62016-04-12 09:02:33100 # Generate .options file.
Abhishek Arya31f18f52018-04-27 19:27:34101 config_file_name = target_name + ".options"
102 action(config_file_name) {
mmoroz344a75ae2016-03-02 16:57:17103 script = "//testing/libfuzzer/gen_fuzzer_config.py"
104 args = [
105 "--config",
Abhishek Arya31f18f52018-04-27 19:27:34106 rebase_path("$root_build_dir/" + config_file_name, root_build_dir),
mmoroz344a75ae2016-03-02 16:57:17107 ]
108
mmoroz062a4a62016-04-12 09:02:33109 if (defined(invoker.dict)) {
mmoroz344a75ae2016-03-02 16:57:17110 args += [
mmoroz062a4a62016-04-12 09:02:33111 "--dict",
wychen5e1f7252017-05-30 08:05:10112 rebase_path("$root_build_dir/" + invoker.target_name + ".dict",
113 root_build_dir),
mmoroz344a75ae2016-03-02 16:57:17114 ]
115 }
mmoroz062a4a62016-04-12 09:02:33116
117 if (defined(invoker.libfuzzer_options)) {
118 args += [ "--libfuzzer_options" ]
119 args += invoker.libfuzzer_options
120 }
121
mmoroz344a75ae2016-03-02 16:57:17122 outputs = [
Abhishek Arya31f18f52018-04-27 19:27:34123 "$root_build_dir/$config_file_name",
mmoroz344a75ae2016-03-02 16:57:17124 ]
125 }
Abhishek Arya31f18f52018-04-27 19:27:34126 test_deps += [ ":" + config_file_name ]
aizatskycb1a43932015-12-14 20:59:14127 }
128
Abhishek Aryac73294d2018-04-26 22:12:43129 # Generate .owners file (if skip_owners is not true).
Abhishek Arya31f18f52018-04-27 19:27:34130 owners_file_name = target_name + ".owners"
131 action(owners_file_name) {
132 script = "//testing/libfuzzer/gen_fuzzer_owners.py"
133 args = [
134 "--owners",
135 rebase_path("$root_build_dir/" + owners_file_name, root_build_dir),
136 ]
Abhishek Aryae1488c922018-04-26 03:25:58137
Abhishek Arya31f18f52018-04-27 19:27:34138 if (defined(invoker.sources) && invoker.sources != []) {
139 args += [ "--sources" ] + rebase_path(invoker.sources, root_build_dir)
140 } else if (defined(invoker.deps) && invoker.deps != []) {
141 args += [
142 "--build-dir",
143 rebase_path("$root_build_dir/", root_build_dir),
144 "--deps",
145 ] + invoker.deps
Abhishek Aryae1488c922018-04-26 03:25:58146 }
Abhishek Arya31f18f52018-04-27 19:27:34147
148 outputs = [
149 "$root_build_dir/$owners_file_name",
150 ]
Abhishek Aryae1488c922018-04-26 03:25:58151 }
Abhishek Arya31f18f52018-04-27 19:27:34152 test_deps += [ ":" + owners_file_name ]
Abhishek Aryae1488c922018-04-26 03:25:58153
aizatsky43d459a52016-02-13 01:22:18154 test(target_name) {
155 forward_variables_from(invoker,
156 [
[email protected]419d0f92017-09-14 12:07:47157 "cflags",
158 "cflags_cc",
brettw6b0712a2016-07-15 17:48:20159 "check_includes",
wfhdee978f2016-09-01 22:18:18160 "defines",
aizatsky43d459a52016-02-13 01:22:18161 "include_dirs",
brettw6b0712a2016-07-15 17:48:20162 "sources",
aizatsky43d459a52016-02-13 01:22:18163 ])
164 deps = test_deps
aizatsky43d459a52016-02-13 01:22:18165
166 if (defined(invoker.additional_configs)) {
167 configs += invoker.additional_configs
168 }
aizatsky345732a2016-04-14 22:30:44169 configs += [ "//testing/libfuzzer:fuzzer_test_config" ]
katrielc2bfe70482016-06-16 13:43:00170
171 # Used by WebRTC to suppress some Clang warnings in their codebase.
172 if (defined(invoker.suppressed_configs)) {
173 configs -= invoker.suppressed_configs
174 }
Robert Sesekd88ebf82017-10-17 21:54:41175
176 if (is_mac) {
Max Morozae3aae52017-10-18 01:50:19177 sources += [ "//testing/libfuzzer/libfuzzer_exports.h" ]
Robert Sesekd88ebf82017-10-17 21:54:41178 }
aizatskycb1a43932015-12-14 20:59:14179 }
aizatsky43d459a52016-02-13 01:22:18180 } else {
181 # noop on unsupported platforms.
182 # mark attributes as used.
Tom Andersonc94b10c2018-05-18 16:12:51183 not_needed(invoker, "*")
aizatsky43d459a52016-02-13 01:22:18184
185 group(target_name) {
aizatskycb1a43932015-12-14 20:59:14186 }
187 }
188}