File: in_process_fuzzer.gni

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (285 lines) | stat: -rw-r--r-- 9,203 bytes parent folder | download | duplicates (5)
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
# Copyright 2023 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//mojo/public/tools/fuzzers/mojolpm.gni")
import("//testing/libfuzzer/fuzzer_test.gni")
import("//third_party/protobuf/proto_library.gni")

# This template allows creation of a fuzzer which has access to all the
# functionality of a browser_test, including a full GUI instance of Chromium.
# See in_process_fuzzer.h.
#
# It accepts all the same arguments as //testing/libfuzzer/fuzzer_test.gni's
# fuzzer_test template.
#
# This template also accepts the argument `target_type`. This defaults to
# `fuzzer_test`.
template("in_process_fuzzer") {
  if (fuzzing_engine_supports_custom_main) {
    if (defined(invoker.target_type)) {
      target_type = invoker.target_type
    } else {
      target_type = "fuzzer_test"
    }
    target(target_type, target_name) {
      # Those tests are big, they need a lot of resources.
      high_end_job_required = true
      deps = [ "//chrome/test/fuzzing:in_process_fuzzer_runner" ]
      if (defined(invoker.deps)) {
        deps += invoker.deps
      }
      forward_variables_from(invoker,
                             "*",
                             [
                               "deps",
                               "exclude_main",
                               "asan_options",
                               "libfuzzer_options",
                               "centipede_options",
                             ])

      # As these are full browser tests, we have crashpad running,
      # and we can't intercept signals that crashpad needs.
      sanitizer_options = [
        "allow_user_segv_handler=1",
        "handle_sigtrap=1",
        "handle_abort=1",
        "handle_segv=1",
        "handle_sigbus=1",
        "handle_sigfpe=1",
        "handle_sigill=1",
        "handle_sigtrap=1",
      ]

      asan_options = [
        # Browser tests store arbitrary data in globals; leak
        # detection will come up with lots of false positives.
        "detect_leaks=0",
      ]

      asan_options += sanitizer_options

      ubsan_options = sanitizer_options

      msan_options = sanitizer_options

      # Again, these are full browser tests, so it needs a fairly high memory
      # limit so that it runs correctly.
      libfuzzer_options = [
        "rss_limit_mb=8192",

        # We can't allow libfuzzer to fork because these fuzzers may have
        # pre-existing threads. See crbug.com/325928325
        "fork=0",
      ]

      centipede_options = [
        # As for now, CF machines only have 8GB. Centipede allocates two
        # buffers of `shmem_size_mb`, one for the input test cases, one for the
        # output (features, cmps, stats). Because those fuzzers are big, this
        # means we are very likely to fill-up the output buffers, and thus
        # running out of memory if `shmem_size_mb` value is too big.
        # Note that 1024 is currently the default value in Centipede.
        "shmem_size_mb=1024",

        # Since `shmem_size_mb` is fairly low, we want not to lose features
        # that would overflow Centipede's output buffer. For this reason, we
        # are lowering `batch_size`. This number has been chosen after some
        # experiments. This might not be 100% accurate.
        "batch_size=400",

        # Browser tests are big
        "address_space_limit_mb=0",

        # For testing. Those fuzzers are extremely slow on ClusterFuzz, and
        # using this option doesn't help.
        "fork_server=0",

        "rss_limit_mb=8192",

        # This value is the one used by CF for Centipede fuzzers.
        # Default computed timeout per batch is ridiculously low (1127, which
        # means ~1.1 sec per input average) for those fuzzers.
        "timeout_per_batch=8400",

        # Those big fuzzers are likely to be flaky on test case sequence
        # execution, but unlikely to crash while executing inputs one-by-one.
        # Since those are super slow, let's not waste time re-running the whole
        # batches.
        "batch_triage_suspect_only=1",
      ]

      if (defined(invoker.asan_options)) {
        asan_options += invoker.asan_options
      }
      if (defined(invoker.libfuzzer_options)) {
        libfuzzer_options += invoker.libfuzzer_options
      }
      if (defined(invoker.centipede_options)) {
        centipede_options += invoker.centipede_options
      }
      exclude_main = true
    }
  } else {
    # noop if the fuzzer harness always provides its own main
    not_needed(invoker, "*")

    group(target_name) {
    }
  }
}

# This template is a proto-based version of the in_process_fuzzer template.
#
# This rule copies the proto-relevant bits from the mojolpm_fuzzer_test
# template (//mojo/public/tools/fuzzers/mojolpm.gni)
#
# Parameters:
#   sources
#       List of source .cc files to compile.
#
#   deps
#       List of dependencies to compile this target.
#
#   proto_source
#       Single source .proto file defining the structure of a testcase.
#
#   proto_deps
#       List of additional dependencies for compiling proto_source.
#
#   testcase_proto_kind (optional, required if seed_corpus_sources provided)
#       Name of proto message type representing a testcase.
#
#   seed_corpus_sources (optional)
#       List of source .textproto files used to build a seed corpus.

template("in_process_proto_fuzzer") {
  if (fuzzing_engine_supports_custom_main) {
    assert(defined(invoker.sources) && defined(invoker.proto_source),
           "\"sources\" and \"proto_source\" must be defined for $target_name")

    assert(
        !defined(invoker.seed_corpus_sources) ||
            defined(invoker.testcase_proto_kind),
        "\"testcase_proto_kind\" must be defined for $target_name since \"seed_corpus_sources\" is defined.")

    proto_target_name = "${target_name}_proto"

    proto_library(proto_target_name) {
      # Work relative to src (//) instead of (by default) the BUILD file.
      proto_in_dir = "//"

      sources = [ invoker.proto_source ]
      generate_python = false

      proto_deps = []

      import_dirs = [ root_gen_dir ]

      link_deps = []

      if (defined(invoker.proto_deps)) {
        proto_deps += invoker.proto_deps
        link_deps += invoker.proto_deps
      }

      testonly = true
    }

    if (defined(invoker.seed_corpus_sources)) {
      protoc_convert_target_name = "${target_name}_protoc_convert"
      seed_corpus_path = "${target_gen_dir}/${target_name}_seed_corpus"

      protoc_convert(protoc_convert_target_name) {
        sources = invoker.seed_corpus_sources

        inputs = [ invoker.proto_source ]

        output_pattern = "${seed_corpus_path}/{{source_name_part}}.binarypb"

        args = [
          "--encode=${invoker.testcase_proto_kind}",
          "-I",
          rebase_path(root_gen_dir, root_build_dir),
          "-I",
          rebase_path("//", root_build_dir),
          rebase_path(inputs[0], root_build_dir),
        ]

        deps = []

        if (defined(invoker.proto_deps)) {
          deps += invoker.proto_deps
        }

        testonly = true
      }
    }

    in_process_fuzzer(target_name) {
      sources = invoker.sources
      deps = [
        ":${proto_target_name}",
        "//chrome/test/fuzzing:in_process_proto_fuzzer_runner",
        "//third_party/libprotobuf-mutator",
      ]

      if (defined(invoker.deps)) {
        deps += invoker.deps
      }

      if (defined(invoker.seed_corpus_sources)) {
        seed_corpus = seed_corpus_path
        seed_corpus_deps = [ ":${protoc_convert_target_name}" ]
      }

      forward_variables_from(invoker,
                             [
                               "libfuzzer_options",
                               "centipede_options",
                             ])
    }
  } else {
    # noop if the fuzzer harness always provides its own main
    not_needed(invoker, "*")

    group(target_name) {
    }
  }
}

# This template aims at mirroring what `mojolpm_fuzzer_test` does, except that
# it embeds InProcessFuzzer internals in order to function correctly.
#
# Parameters:
#  see parameters for //mojo/public/tools/fuzzers/mojolpm.gni:mojolpm_fuzzer_test
template("in_process_mojolpm_fuzzer") {
  in_process_fuzzer(target_name) {
    target_type = "mojolpm_fuzzer_test"
    forward_variables_from(invoker,
                           "*",
                           [
                             "testonly",
                             "visibility",
                           ])
  }
}

# This template aims at mirroring what `mojolpm_generated_fuzzer` does, except
# that it embeds InProcessFuzzer internals in order to function correctly.
#
# Parameters:
#  see parameters for //mojo/public/tools/fuzzers/mojolpm.gni:mojolpm_generated_fuzzer
template("in_process_mojolpm_generated_fuzzer") {
  in_process_fuzzer(target_name) {
    target_type = "mojolpm_generated_fuzzer"
    ensure_remote = true
    forward_variables_from(invoker,
                           "*",
                           [
                             "testonly",
                             "visibility",
                           ])
  }
}