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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Defines fuzzer_test.
#
import("//build/config/features.gni")
import("//build/config/sanitizers/sanitizers.gni")
import("//testing/test.gni")
# fuzzer_test is used to define individual libfuzzer tests.
#
# Supported attributes:
# - (required) sources - fuzzer test source files
# - deps - test dependencies
# - libs - Additional libraries to link.
# - frameworks - Apple-only. Additional frameworks to link.
# - additional_configs - additional configs to be used for compilation
# - dict - a dictionary file for the fuzzer.
# - environment_variables - certain whitelisted environment variables for the
# fuzzer (AFL_DRIVER_DONT_DEFER is the only one allowed currently).
# - libfuzzer_options - options for the fuzzer (e.g. -close_fd_mask=N).
# - asan_options - AddressSanitizer options (e.g. allow_user_segv_handler=1).
# - msan_options - MemorySanitizer options.
# - ubsan_options - UndefinedBehaviorSanitizer options.
# - seed_corpus - a directory with seed corpus.
# - seed_corpus_deps - dependencies for generating the seed corpus.
# - grammar_options - defines a grammar used by a grammar based mutator.
#
# If use_libfuzzer gn flag is defined, then proper fuzzer would be build.
# Without use_libfuzzer or use_afl a unit-test style binary would be built on
# linux and the whole target is a no-op otherwise.
#
# The template wraps test() target with appropriate dependencies.
# If any test run-time options are present (dict or libfuzzer_options), then a
# config (.options file) file would be generated or modified in root output
# dir (next to test).
template("fuzzer_test") {
if (!disable_libfuzzer && use_fuzzing_engine) {
assert(defined(invoker.sources), "Need sources in $target_name.")
test_deps = [ "//testing/libfuzzer:libfuzzer_main" ]
test_data_deps = []
if (defined(invoker.deps)) {
test_deps += invoker.deps
}
if (defined(invoker.data_deps)) {
test_data_deps += invoker.data_deps
}
supporting_file_test_deps = []
supporting_file_test_data_deps = []
if (defined(invoker.seed_corpus) || defined(invoker.seed_corpuses)) {
assert(!(defined(invoker.seed_corpus) && defined(invoker.seed_corpuses)),
"Do not use both seed_corpus and seed_corpuses for $target_name.")
out = "$root_build_dir/$target_name" + "_seed_corpus.zip"
seed_corpus_deps = []
if (defined(invoker.seed_corpus_deps)) {
seed_corpus_deps += invoker.seed_corpus_deps
}
action(target_name + "_seed_corpus") {
script = "//testing/libfuzzer/archive_corpus.py"
testonly = true
args = [
"--output",
rebase_path(out, root_build_dir),
]
if (defined(invoker.seed_corpus)) {
args += [ rebase_path(invoker.seed_corpus, root_build_dir) ]
}
if (defined(invoker.seed_corpuses)) {
foreach(seed_corpus_path, invoker.seed_corpuses) {
args += [ rebase_path(seed_corpus_path, root_build_dir) ]
}
}
outputs = [ out ]
deps = [ "//testing/libfuzzer:seed_corpus" ] + seed_corpus_deps
}
if (archive_seed_corpus) {
supporting_file_test_deps += [ ":" + target_name + "_seed_corpus" ]
}
}
if (defined(invoker.dict) || defined(invoker.libfuzzer_options) ||
defined(invoker.asan_options) || defined(invoker.msan_options) ||
defined(invoker.ubsan_options) ||
defined(invoker.environment_variables) ||
defined(invoker.grammar_options)) {
if (defined(invoker.dict)) {
# Copy dictionary to output.
copy(target_name + "_dict_copy") {
sources = [ invoker.dict ]
outputs = [ "$root_build_dir/" + target_name + ".dict" ]
}
supporting_file_test_deps += [ ":" + target_name + "_dict_copy" ]
}
# Generate .options file.
config_file_name = target_name + ".options"
action(config_file_name) {
script = "//testing/libfuzzer/gen_fuzzer_config.py"
args = [
"--config",
rebase_path("$root_build_dir/" + config_file_name, root_build_dir),
]
if (defined(invoker.dict)) {
args += [
"--dict",
rebase_path("$root_build_dir/" + invoker.target_name + ".dict",
root_build_dir),
]
}
if (defined(invoker.libfuzzer_options)) {
args += [ "--libfuzzer_options" ]
args += invoker.libfuzzer_options
}
if (defined(invoker.asan_options)) {
args += [ "--asan_options" ]
args += invoker.asan_options
}
if (defined(invoker.msan_options)) {
args += [ "--msan_options" ]
args += invoker.msan_options
}
if (defined(invoker.ubsan_options)) {
args += [ "--ubsan_options" ]
args += invoker.ubsan_options
}
if (defined(invoker.environment_variables)) {
args += [ "--environment_variables" ]
args += invoker.environment_variables
}
if (defined(invoker.grammar_options)) {
args += [ "--grammar_options" ]
args += invoker.grammar_options
}
outputs = [ "$root_build_dir/$config_file_name" ]
}
supporting_file_test_deps += [ ":" + config_file_name ]
}
if (generate_fuzzer_owners) {
# Generating owners files is slow, only enable when fuzzing engine is
# used.
owners_file_name = target_name + ".owners"
action(owners_file_name) {
script = "//testing/libfuzzer/gen_fuzzer_owners.py"
pool = "//testing/libfuzzer:fuzzer_owners_pool"
args = [
"--owners",
rebase_path("$root_build_dir/" + owners_file_name, root_build_dir),
]
if (defined(invoker.sources) && invoker.sources != []) {
args += [ "--sources" ] + rebase_path(invoker.sources, "//")
} else if (defined(invoker.deps) && invoker.deps != []) {
_full_deps = []
foreach(_dep, invoker.deps) {
_full_deps += [ get_label_info(_dep, "dir") + ":" +
get_label_info(_dep, "name") ]
}
args += [
"--build-dir",
rebase_path("$root_build_dir/", root_build_dir),
"--deps",
] + _full_deps
}
outputs = [ "$root_build_dir/$owners_file_name" ]
}
supporting_file_test_data_deps += [ ":" + owners_file_name ]
}
# Copy to executable folder and codesign for catalyst
if (is_ios) {
# iOS only supports fuzzer in catalyst environment.
assert(target_environment == "catalyst")
# Loop over two kinds of deps to codesign these in two |action_foreach|s.
# Use the "test_deps", "test_data_deps" as identifiers in for loop.
foreach(_deps_type_label,
[
"test_deps",
"test_data_deps",
]) {
_dep_list = []
if (_deps_type_label == "test_deps") {
_dep_list = supporting_file_test_deps
} else {
_dep_list = supporting_file_test_data_deps
}
_files_to_sign = []
foreach(dep, _dep_list) {
_files_to_sign += get_target_outputs(dep)
}
if (_files_to_sign != []) {
_codesign_action_name =
target_name + "_codesign_supporting_files_" + _deps_type_label
action_foreach(_codesign_action_name) {
testonly = true
script = "//build/config/ios/codesign.py"
sources = _files_to_sign
_codesign_output_path =
"${root_build_dir}/codesign/{{source_file_part}}"
outputs = [ _codesign_output_path ]
args = [
"code-sign-file",
"--identity=" + ios_code_signing_identity,
"--output=" + rebase_path(_codesign_output_path, root_build_dir),
"{{source}}",
]
deps = _dep_list
}
_bundle_data_name = target_name + "_bundle_data_" + _deps_type_label
bundle_data(_bundle_data_name) {
testonly = true
sources = get_target_outputs(":${_codesign_action_name}")
outputs = [ "{{bundle_executable_dir}}/{{source_file_part}}" ]
public_deps = [ ":${_codesign_action_name}" ]
}
if (_deps_type_label == "test_deps") {
test_deps += [ ":${_bundle_data_name}" ]
} else {
test_data_deps += [ ":${_bundle_data_name}" ]
}
}
}
} else {
test_deps += supporting_file_test_deps
test_data_deps += supporting_file_test_data_deps
}
test(target_name) {
forward_variables_from(invoker,
[
"cflags",
"cflags_cc",
"check_includes",
"defines",
"include_dirs",
"sources",
"libs",
"frameworks",
])
deps = test_deps
data_deps = test_data_deps
if (defined(invoker.additional_configs)) {
configs += invoker.additional_configs
}
configs += [ "//testing/libfuzzer:fuzzer_test_config" ]
# Used by WebRTC to suppress some Clang warnings in their codebase.
if (defined(invoker.suppressed_configs)) {
configs -= invoker.suppressed_configs
}
if (defined(invoker.generated_sources)) {
sources += invoker.generated_sources
}
if (is_ios) {
info_plist =
"//testing/libfuzzer/fuzzer_support_ios/libfuzzer-Info.plist"
}
if (is_mac) {
sources += [ "//testing/libfuzzer/libfuzzer_exports.h" ]
}
# TODO(crbug.com/1292951): Remove this line.
configs -= [ "//build/config/compiler:prevent_unsafe_narrowing" ]
}
} else {
# noop on unsupported platforms.
# mark attributes as used.
not_needed(invoker, "*")
group(target_name) {
}
}
}
|