• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import("//build/config/cast.gni")
6import("//build/config/chrome_build.gni")
7import("//build/config/chromeos/args.gni")
8import("//build/config/chromeos/ui_mode.gni")
9import("//build/config/profiling/profiling.gni")
10import("//build/toolchain/toolchain.gni")
11
12declare_args() {
13  # Compile for Address Sanitizer to find memory bugs.
14  is_asan = false
15
16  # Compile for Hardware-Assisted Address Sanitizer to find memory bugs
17  # (android/arm64 only).
18  # See http://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html
19  is_hwasan = false
20
21  # Compile for Leak Sanitizer to find leaks.
22  is_lsan = false
23
24  # Compile for Memory Sanitizer to find uninitialized reads.
25  is_msan = false
26
27  # Compile for Thread Sanitizer to find threading bugs.
28  is_tsan = false
29
30  # Compile for Undefined Behaviour Sanitizer to find various types of
31  # undefined behaviour (excludes vptr checks).
32  is_ubsan = false
33
34  # Halt the program if a problem is detected.
35  is_ubsan_no_recover = false
36
37  # Track where uninitialized memory originates from. From fastest to slowest:
38  # 0 - no tracking, 1 - track only the initial allocation site, 2 - track the
39  # chain of stores leading from allocation site to use site.
40  msan_track_origins = 2
41
42  # Use dynamic libraries instrumented by one of the sanitizers instead of the
43  # standard system libraries. Set this flag to build the libraries from source.
44  use_locally_built_instrumented_libraries = false
45
46  # Compile with Control Flow Integrity to protect virtual calls and casts.
47  # See http://clang.llvm.org/docs/ControlFlowIntegrity.html
48  #
49  # TODO(pcc): Remove this flag if/when CFI is enabled in all official builds.
50  is_cfi = is_official_build && is_clang &&
51           ((target_os == "linux" && target_cpu == "x64") ||
52            (is_chromeos && is_chromeos_device))
53
54  # Enable checks for indirect function calls via a function pointer.
55  # TODO(pcc): remove this when we're ready to add these checks by default.
56  # https://crbug.com/701919
57  use_cfi_icall =
58      target_os == "linux" && target_cpu == "x64" && is_official_build
59
60  # Print detailed diagnostics when Control Flow Integrity detects a violation.
61  use_cfi_diag = false
62
63  # Let Control Flow Integrity continue execution instead of crashing when
64  # printing diagnostics (use_cfi_diag = true).
65  use_cfi_recover = false
66
67  # Compile for fuzzing with LLVM LibFuzzer.
68  # See http://www.chromium.org/developers/testing/libfuzzer
69  use_libfuzzer = false
70
71  # Compile for fuzzing with centipede.
72  # See https://github.com/google/centipede
73  use_centipede = false
74
75  # Compile for fuzzing with AFL.
76  use_afl = false
77
78  # Compile for fuzzing with an external engine (e.g., Grammarinator).
79  use_external_fuzzing_engine = false
80
81  # Enables core ubsan security features. Will later be removed once it matches
82  # is_ubsan.
83  is_ubsan_security = false
84
85  # Helper variable for testing builds with disabled libfuzzer.
86  # Not for client use.
87  disable_libfuzzer = false
88
89  # Value for -fsanitize-coverage flag. Setting this causes
90  # use_sanitizer_coverage to be enabled.
91  # This flag is not used for libFuzzer (use_libfuzzer=true). Instead, we use:
92  #     -fsanitize=fuzzer-no-link
93  # Default value when unset and use_fuzzing_engine=true:
94  #     trace-pc-guard
95  # Default value when unset and use_sanitizer_coverage=true:
96  #     trace-pc-guard,indirect-calls
97  sanitizer_coverage_flags = ""
98
99  # When enabled, only relevant sanitizer defines are set, but compilation
100  # happens with no extra flags. This is useful when in component build
101  # enabling sanitizers only in some of the components.
102  use_sanitizer_configs_without_instrumentation = false
103
104  # When true, seed corpora archives are built.
105  archive_seed_corpus = true
106
107  # When true, sanitizer warnings will cause test case failures.
108  fail_on_san_warnings = false
109
110  # The fuzztest library builds only on some platforms, so for now,
111  # all targets depending on fuzztest need to be configured according
112  # to this.
113  # TODO(crbug.com/1494445): remove this when all build permutations
114  # work
115  fuzztest_supported = !(is_win && is_component_build)
116}
117
118declare_args() {
119  # Enable checks for bad casts: derived cast and unrelated cast.
120  # TODO(krasin): remove this, when we're ready to add these checks by default.
121  # https://crbug.com/626794
122  use_cfi_cast = is_cfi && is_chromeos
123
124  # Compile for Undefined Behaviour Sanitizer's vptr checks.
125  is_ubsan_vptr = is_ubsan_security
126}
127
128declare_args() {
129  # Builds fuzztest test executables such that they support the
130  # --fuzz= argument, which requires some sanitizer coverage.
131  # We want to enable this only when we're *NOT* using a fuzzing
132  # engine such as libfuzzer or centipede. It's generally a
133  # useful option, but it requires sanitizer coverage, and that
134  # could conceivably disrupt normal unit testing workflows, so we'll
135  # enable it by default only in sanitizer builds.
136  # Also be sure not to enable this on non-Chromium builds where
137  # the required //third_party/fuzztest dependency may be absent.
138  # TODO(crbug.com/1495713): enable on component builds
139  enable_fuzztest_fuzz =
140      !(use_libfuzzer || use_afl || use_centipede ||
141        use_external_fuzzing_engine) &&
142      (is_asan || is_hwasan || is_lsan || is_tsan || is_msan || is_ubsan ||
143       is_ubsan_vptr || is_ubsan_security) && !is_component_build && is_linux &&
144      build_with_chromium
145}
146
147assert(!is_hwasan || (target_os == "android" && target_cpu == "arm64"),
148       "HWASan only supported on Android ARM64 builds.")
149
150assert(
151    !(enable_fuzztest_fuzz && use_libfuzzer),
152    "Can't specify enable_fuzztest_fuzz and use_libfuzzer. When libfuzzer is enabled, fuzztest executables automatically support --fuzz but provide output that's libfuzzer compatible.")
153
154assert(
155    !(enable_fuzztest_fuzz && use_centipede),
156    "Can't specify enable_fuzztest_fuzz and use_centipede. The same binaries are built in a different mode to add centipede support.")
157
158assert(
159    !(enable_fuzztest_fuzz && is_component_build),
160    "Can't specify enable_fuzztest_fuzz in component builds; fuzztest doesn't yet support it. Consider using use_libfuzzer=true instead which provides superficially similar functionality.")
161
162# Disable sanitizers for non-target toolchains, and for the toolchain using
163# the prebuilt Rust stdlib which has no sanitizer support with it.
164if (!is_a_target_toolchain || toolchain_for_rust_host_build_tools) {
165  is_asan = false
166  is_cfi = false
167  is_hwasan = false
168  is_lsan = false
169  is_msan = false
170  is_tsan = false
171  is_ubsan = false
172  is_ubsan_no_recover = false
173  is_ubsan_security = false
174  is_ubsan_vptr = false
175  fail_on_san_warnings = false
176  msan_track_origins = 0
177  sanitizer_coverage_flags = ""
178  use_afl = false
179  use_centipede = false
180  use_cfi_diag = false
181  use_cfi_recover = false
182  use_libfuzzer = false
183  use_locally_built_instrumented_libraries = false
184  use_sanitizer_coverage = false
185  enable_fuzztest_fuzz = false
186} else if (current_cpu != "arm64") {
187  is_hwasan = false
188}
189
190# Use dynamic libraries instrumented by one of the sanitizers instead of the
191# standard system libraries. We have instrumented system libraries for msan,
192# which requires them to prevent false positives.
193# TODO(thakis): Maybe remove this variable.
194use_prebuilt_instrumented_libraries = is_msan
195
196# Whether we are doing a fuzzer build. Normally this should be checked instead
197# of checking "use_libfuzzer || use_afl" because often developers forget to
198# check for "use_afl", and "use_centipede" is new.
199use_fuzzing_engine =
200    use_libfuzzer || use_afl || use_centipede || use_external_fuzzing_engine
201
202# Whether the current fuzzing engine supports libprotobuf_mutator. Right now
203# this is just libfuzzer, but others are likely to support this in future,
204# so it's preferable to check this.
205use_fuzzing_engine_with_lpm = use_libfuzzer || use_centipede
206
207# Whether the fuzzing engine supports fuzzers which supply their own
208# "main" function.
209fuzzing_engine_supports_custom_main = use_libfuzzer || use_centipede
210
211# Args that are in turn dependent on other args must be in a separate
212# declare_args block. User overrides are only applied at the end of a
213# declare_args block.
214declare_args() {
215  # Generates an owners file for each fuzzer test.
216  # TODO(crbug.com/1194183): Remove this arg when finding OWNERS is faster.
217  generate_fuzzer_owners = use_fuzzing_engine
218
219  use_sanitizer_coverage =
220      !use_clang_coverage && (use_fuzzing_engine || enable_fuzztest_fuzz ||
221                              sanitizer_coverage_flags != "")
222
223  # https://crbug.com/1002058: Code coverage works inside the sandbox via the
224  # help of several helper IPCs. Unfortunately, the sandbox-only path does not
225  # work well for fuzzing builds. Since fuzzing builds already disable the
226  # sandbox when dumping coverage, limit the sandbox-only path to non-fuzzing
227  # builds.
228  # Everything is IPC on Fuchsia, so this workaround for code coverage inside
229  # the sandbox does not apply.
230  use_clang_profiling_inside_sandbox =
231      use_clang_profiling && !use_fuzzing_engine && !is_fuchsia
232}
233
234if (sanitizer_coverage_flags == "") {
235  if (enable_fuzztest_fuzz) {
236    # ./fuzztest_executable --fuzz=
237    # requires only this single type of coverage
238    sanitizer_coverage_flags = "inline-8bit-counters"
239  } else if (use_fuzzing_engine) {
240    sanitizer_coverage_flags = "trace-pc-guard"
241    if (use_centipede) {
242      # Centipede's minimal flags are listed in //third_party/fuzztest/src/centipede/clang-flags.txt.
243      # But, for users like Chromium using an up-to-date clang, we can also
244      # enable extra optional types of coverage which may make Centipede more
245      # effective. This list is not currently documented and has been derived
246      # from discussion with centipede creators (though one is warned about at
247      # https://github.com/google/centipede/blob/main/centipede_callbacks.cc#L68)
248      sanitizer_coverage_flags = sanitizer_coverage_flags +
249                                 ",pc-table,trace-cmp,control-flow,trace-loads"
250    }
251  } else if (use_sanitizer_coverage) {
252    sanitizer_coverage_flags = "trace-pc-guard,indirect-calls"
253  }
254}
255
256# Whether we are linking against a sanitizer runtime library. Among other
257# things, this changes the default symbol level and other settings in order to
258# prepare to create stack traces "live" using the sanitizer runtime.
259using_sanitizer =
260    is_asan || is_hwasan || is_lsan || is_tsan || is_msan || is_ubsan ||
261    is_ubsan_vptr || is_ubsan_security || use_sanitizer_coverage || use_cfi_diag
262
263assert(!using_sanitizer || is_clang,
264       "Sanitizers (is_*san) require setting is_clang = true in 'gn args'")
265
266assert(!is_cfi || is_clang,
267       "is_cfi requires setting is_clang = true in 'gn args'")
268
269prebuilt_instrumented_libraries_available =
270    is_msan && (msan_track_origins == 0 || msan_track_origins == 2)
271
272if (use_libfuzzer && (is_linux || is_chromeos)) {
273  if (is_asan) {
274    # We do leak checking with libFuzzer on Linux. Set is_lsan for code that
275    # relies on LEAK_SANITIZER define to avoid false positives.
276    is_lsan = true
277  }
278}
279
280# MSan only links Chrome properly in release builds (brettw -- 9/1/2015). The
281# same is possibly true for the other non-ASan sanitizers. But regardless of
282# whether it links, one would normally never run a sanitizer in debug mode.
283# Running in debug mode probably indicates you forgot to set the "is_debug =
284# false" flag in the build args. ASan seems to run fine in debug mode.
285#
286# If you find a use-case where you want to compile a sanitizer in debug mode
287# and have verified it works, ask brettw and we can consider removing it from
288# this condition. We may also be able to find another way to enable your case
289# without having people accidentally get broken builds by compiling an
290# unsupported or unadvisable configurations.
291#
292# For one-off testing, just comment this assertion out.
293assert(!is_debug || !(is_msan || is_ubsan || is_ubsan_vptr),
294       "Sanitizers should generally be used in release (set is_debug=false).")
295
296assert(!is_msan || ((is_linux || is_chromeos) && current_cpu == "x64"),
297       "MSan currently only works on 64-bit Linux and ChromeOS builds.")
298
299assert(!is_lsan || is_asan, "is_lsan = true requires is_asan = true also.")
300
301# ASAN build on Windows is not working in debug mode. Intercepting memory
302# allocation functions is hard on Windows and not yet implemented in LLVM.
303assert(!is_win || !is_debug || !is_asan,
304       "ASan on Windows doesn't work in debug (set is_debug=false).")
305
306# libFuzzer targets can fail to build or behave incorrectly when built without
307# ASAN on Windows.
308assert(!is_win || !use_libfuzzer || is_asan,
309       "use_libfuzzer on Windows requires setting is_asan = true")
310
311# Make sure that if we recover on detection (i.e. not crash), diagnostics are
312# printed.
313assert(!use_cfi_recover || use_cfi_diag,
314       "Only use CFI recovery together with diagnostics.")
315
316# TODO(crbug.com/753445): the use_sanitizer_coverage arg is currently
317# not supported by the Chromium mac_clang_x64 toolchain on iOS distribution.
318# The coverage works with iOS toolchain but it is broken when the mac
319# toolchain is used as a secondary one on iOS distribution. E.g., it should be
320# possible to build the "net" target for iOS with the sanitizer coverage
321# enabled.
322assert(
323    !(use_sanitizer_coverage && is_mac && target_os == "ios"),
324    "crbug.com/753445: use_sanitizer_coverage=true is not supported by the " +
325        "Chromium mac_clang_x64 toolchain on iOS distribution. Please set " +
326        "the argument value to false.")
327
328# Use these lists of configs to disable instrumenting code that is part of a
329# fuzzer, but which isn't being targeted (such as libprotobuf-mutator, *.pb.cc
330# and libprotobuf when they are built as part of a proto fuzzer). Adding or
331# removing these lists does not have any effect if use_libfuzzer or use_afl are
332# not passed as arguments to gn.
333not_fuzzed_remove_configs = []
334not_fuzzed_remove_nonasan_configs = []
335
336if (use_fuzzing_engine) {
337  # Removing coverage should always just work.
338  not_fuzzed_remove_configs += [ "//build/config/coverage:default_coverage" ]
339  not_fuzzed_remove_nonasan_configs +=
340      [ "//build/config/coverage:default_coverage" ]
341
342  if (!is_msan) {
343    # Allow sanitizer instrumentation to be removed if we are not using MSan
344    # since binaries cannot be partially instrumented with MSan.
345    not_fuzzed_remove_configs +=
346        [ "//build/config/sanitizers:default_sanitizer_flags" ]
347
348    # Certain parts of binaries must be instrumented with ASan if the rest of
349    # the binary is. For these, only remove non-ASan sanitizer instrumentation.
350    if (!is_asan) {
351      not_fuzzed_remove_nonasan_configs +=
352          [ "//build/config/sanitizers:default_sanitizer_flags" ]
353
354      assert(not_fuzzed_remove_nonasan_configs == not_fuzzed_remove_configs)
355    }
356  }
357}
358
359# Options common to different fuzzer engines.
360# Engine should be compiled without coverage (infinite loop in trace_cmp).
361fuzzing_engine_remove_configs = [
362  "//build/config/coverage:default_coverage",
363  "//build/config/sanitizers:default_sanitizer_flags",
364]
365
366# Add any sanitizer flags back. In MSAN builds, instrumenting libfuzzer with
367# MSAN is necessary since all parts of the binary need to be instrumented for it
368# to work. ASAN builds are more subtle: libfuzzer depends on features from the
369# C++ STL. If it were not instrumented, templates would be insantiated without
370# ASAN from libfuzzer and with ASAN in other TUs. The linker might merge
371# instrumented template instantiations with non-instrumented ones (which could
372# have a different ABI) in the final binary, which is problematic for TUs
373# expecting one particular ABI (https://crbug.com/915422). The other sanitizers
374# are added back for the same reason.
375fuzzing_engine_add_configs =
376    [ "//build/config/sanitizers:default_sanitizer_flags_but_coverage" ]
377