1# 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 5import("//build/config/coverage/coverage.gni") 6import("//build/toolchain/toolchain.gni") 7 8if (!build_xts && defined(ext_sanitizer_check_list_path)) { 9 import("${ext_sanitizer_check_list_path}") 10} 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 use_hwasan = false 18 19 # Customize asan detection. 20 asan_detector = false 21 22 # Compile for Leak Sanitizer to find leaks. 23 is_lsan = false 24 25 # Compile for Memory Sanitizer to find uninitialized reads. 26 is_msan = false 27 28 # Compile for Thread Sanitizer to find threading bugs. 29 is_tsan = false 30 31 # Compile for Undefined Behavior Sanitizer to find various types of 32 # undefined behavior (excludes vptr checks). 33 is_ubsan = false 34 35 # Halt the program if a problem is detected. 36 is_ubsan_no_recover = false 37 38 # Compile for Undefined Behavior Sanitizer's null pointer checks. 39 is_ubsan_null = false 40 41 # Compile for Undefined Behavior Sanitizer's vptr checks. 42 is_ubsan_vptr = false 43 44 # Compile with SafeStack shadow stack support. 45 is_safestack = false 46 47 # Track where uninitialized memory originates from. From fastest to slowest: 48 # 0 - no tracking, 1 - track only the initial allocation site, 2 - track the 49 # chain of stores leading from allocation site to use site. 50 msan_track_origins = 2 51 52 # Use dynamic libraries instrumented by one of the sanitizers instead of the 53 # standard system libraries. Set this flag to download prebuilt binaries from 54 # GCS. 55 use_prebuilt_instrumented_libraries = false 56 57 # Use dynamic libraries instrumented by one of the sanitizers instead of the 58 # standard system libraries. Set this flag to build the libraries from source. 59 use_locally_built_instrumented_libraries = false 60 61 # Compile with Control Flow Integrity to protect virtual calls and casts. 62 # See http://clang.llvm.org/docs/ControlFlowIntegrity.html 63 is_cfi = target_os == "linux" && !is_chromeos && target_cpu == "x64" && 64 is_official_build 65 66 # Enable checks for bad casts: derived cast and unrelated cast. 67 use_cfi_cast = false 68 69 # Enable checks for indirect function calls via a function pointer. 70 use_cfi_icall = target_os == "linux" && !is_chromeos && target_cpu == "x64" && 71 is_official_build 72 73 # Print detailed diagnostics when Control Flow Integrity detects a violation. 74 use_cfi_diag = false 75 76 # Let Control Flow Integrity continue execution instead of crashing when 77 # printing diagnostics (use_cfi_diag = true). 78 use_cfi_recover = false 79 80 # Compile for fuzzing with LLVM LibFuzzer. 81 # See http://www.chromium.org/developers/testing/libfuzzer 82 use_libfuzzer = false 83 84 # Compile for fuzzing with AFL. 85 use_afl = false 86 87 # Enables core ubsan security features. Will later be removed once it matches 88 # is_ubsan. 89 is_ubsan_security = false 90 91 # Compile for fuzzing with Dr. Fuzz 92 # See http://www.chromium.org/developers/testing/dr-fuzz 93 use_drfuzz = false 94 95 # Helper variable for testing builds with disabled libfuzzer. 96 # Not for client use. 97 disable_libfuzzer = false 98 99 # Optimize for coverage guided fuzzing (balance between speed and number of 100 # branches). Can be also used to remove non-determinism and other issues. 101 optimize_for_fuzzing = false 102 103 # Value for -fsanitize-coverage flag. Setting this causes 104 # use_sanitizer_coverage to be enabled. 105 # This flag is not used for libFuzzer (use_libfuzzer=true) unless we are on 106 # Mac. Instead, we use: 107 # -fsanitize=fuzzer-no-link 108 # Default value when unset and use_fuzzing_engine=true: 109 # trace-pc-guard 110 # Default value when unset and use_sanitizer_coverage=true: 111 # trace-pc-guard,indirect-calls 112 sanitizer_coverage_flags = "" 113 114 # The global switch of cfi. Disable it to improve compiling efficiency while 115 # being vulnerable to cfi attack. 116 use_cfi = true 117} 118 119is_v8_host_toolchain = 120 current_toolchain == "//build/toolchain/linux:clang_x64_v8_arm64" || 121 current_toolchain == "//build/toolchain/linux:clang_x86_v8_arm" 122 123# Disable sanitizers for non-default toolchains. 124if (current_toolchain == host_toolchain || is_v8_host_toolchain) { 125 is_asan = false 126 is_cfi = false 127 is_lsan = false 128 is_msan = false 129 is_tsan = false 130 is_ubsan = false 131 is_ubsan_null = false 132 is_ubsan_no_recover = false 133 is_ubsan_security = false 134 is_ubsan_vptr = false 135 msan_track_origins = 0 136 sanitizer_coverage_flags = "" 137 use_afl = false 138 use_cfi_diag = false 139 use_cfi_recover = false 140 use_drfuzz = false 141 use_libfuzzer = false 142 use_prebuilt_instrumented_libraries = false 143 use_locally_built_instrumented_libraries = false 144 use_sanitizer_coverage = false 145} 146 147# Whether we are doing a fuzzer build. Normally this should be checked instead 148# of checking "use_libfuzzer || use_afl" because often developers forget to 149# check for "use_afl". 150use_fuzzing_engine = use_libfuzzer || use_afl 151 152# Args that are in turn dependent on other args must be in a separate 153# declare_args block. User overrides are only applied at the end of a 154# declare_args block. 155declare_args() { 156 use_sanitizer_coverage = 157 !use_clang_coverage && 158 (use_fuzzing_engine || sanitizer_coverage_flags != "") 159 160 # Detect overflow/underflow for global objects. 161 # 162 # Mac: http://crbug.com/352073 163 asan_globals = !is_mac 164} 165 166if (use_fuzzing_engine && sanitizer_coverage_flags == "") { 167 sanitizer_coverage_flags = "trace-pc-guard" 168} else if (use_sanitizer_coverage && sanitizer_coverage_flags == "") { 169 sanitizer_coverage_flags = "trace-pc-guard,indirect-calls" 170} 171 172# Whether we are linking against a debugging sanitizer runtime library. Among 173# other things, this changes the default symbol level and other settings in 174# order to prepare to create stack traces "live" using the sanitizer runtime. 175using_sanitizer = 176 is_asan || is_lsan || is_tsan || is_msan || is_ubsan || is_ubsan_null || 177 is_ubsan_vptr || is_ubsan_security || use_sanitizer_coverage || use_cfi_diag 178 179if (!is_ohos) { 180 using_sanitizer = false 181} 182 183assert(!using_sanitizer || is_clang, 184 "Sanitizers (is_*san) require setting is_clang = true in 'gn args'") 185 186assert(!is_cfi || is_clang, 187 "is_cfi requires setting is_clang = true in 'gn args'") 188 189assert(!is_safestack || is_clang, 190 "is_safestack requires setting is_clang = true in 'gn args'") 191 192prebuilt_instrumented_libraries_available = 193 is_msan && (msan_track_origins == 0 || msan_track_origins == 2) 194 195if (use_libfuzzer && is_linux) { 196 if (is_asan) { 197 # We do leak checking with libFuzzer on Linux. Set is_lsan for code that 198 # relies on LEAK_SANITIZER define to avoid false positives. 199 is_lsan = true 200 } 201 if (is_msan) { 202 use_prebuilt_instrumented_libraries = true 203 } 204} 205 206# MSan only links Chrome properly in release builds (brettw -- 9/1/2015). The 207# same is possibly true for the other non-ASan sanitizers. But regardless of 208# whether it links, one would normally never run a sanitizer in debug mode. 209# Running in debug mode probably indicates you forgot to set the "is_debug = 210# false" flag in the build args. ASan seems to run fine in debug mode. 211# 212# If you find a use-case where you want to compile a sanitizer in debug mode 213# and have verified it works, ask brettw and we can consider removing it from 214# this condition. We may also be able to find another way to enable your case 215# without having people accidentally get broken builds by compiling an 216# unsupported or unadvisable configurations. 217# 218# For one-off testing, just comment this assertion out. 219assert(!is_debug || !(is_msan || is_ubsan || is_ubsan_null || is_ubsan_vptr), 220 "Sanitizers should generally be used in release (set is_debug=false).") 221 222assert(!is_msan || (is_linux && current_cpu == "x64"), 223 "MSan currently only works on 64-bit Linux and ChromeOS builds.") 224 225assert(!is_lsan || is_asan, "is_lsan = true requires is_asan = true also.") 226 227# ASAN build on Windows is not working in debug mode. Intercepting memory 228# allocation functions is hard on Windows and not yet implemented in LLVM. 229assert(!is_win || !is_debug || !is_asan, 230 "ASan on Windows doesn't work in debug (set is_debug=false).") 231 232# Make sure that if we recover on detection (i.e. not crash), diagnostics are 233# printed. 234assert(!use_cfi_recover || use_cfi_diag, 235 "Only use CFI recovery together with diagnostics.") 236 237assert( 238 !(use_sanitizer_coverage && is_mac && target_os == "ios"), 239 "crbug.com/753445: use_sanitizer_coverage=true is not supported by the " + 240 "Chromium mac_clang_x64 toolchain on iOS distribution. Please set " + 241 "the argument value to false.") 242 243# Use these lists of configs to disable instrumenting code that is part of a 244# fuzzer, but which isn't being targeted (such as libprotobuf-mutator, *.pb.cc 245# and libprotobuf when they are built as part of a proto fuzzer). Adding or 246# removing these lists does not have any effect if use_libfuzzer or use_afl are 247# not passed as arguments to gn. 248not_fuzzed_remove_configs = [] 249not_fuzzed_remove_nonasan_configs = [] 250 251if (use_fuzzing_engine) { 252 # Removing coverage should always just work. 253 not_fuzzed_remove_configs += [ "//build/config/coverage:default_coverage" ] 254 not_fuzzed_remove_nonasan_configs += 255 [ "//build/config/coverage:default_coverage" ] 256 257 if (!is_msan) { 258 # Allow sanitizer instrumentation to be removed if we are not using MSan 259 # since binaries cannot be partially instrumented with MSan. 260 not_fuzzed_remove_configs += 261 [ "//build/config/sanitizers:default_sanitizer_flags" ] 262 263 # Certain parts of binaries must be instrumented with ASan if the rest of 264 # the binary is. For these, only remove non-ASan sanitizer instrumentation. 265 if (!is_asan) { 266 not_fuzzed_remove_nonasan_configs += 267 [ "//build/config/sanitizers:default_sanitizer_flags" ] 268 assert(not_fuzzed_remove_nonasan_configs == not_fuzzed_remove_configs) 269 } 270 } 271} 272 273template("ohos_sanitizer_config") { 274 config(target_name) { 275 forward_variables_from(invoker, [ "sanitize" ]) 276 if (defined(sanitize)) { 277 configs = [ "//build/config/sanitizers:sanitizer_trap_all_flags" ] 278 _mode = "release" 279 _debug = (defined(sanitize.debug) && sanitize.debug) || is_asan 280 if (_debug) { 281 _mode = "debug" 282 } 283 _cfi = use_cfi && defined(sanitize.cfi) && sanitize.cfi && !is_asan 284 if (_cfi) { 285 if (defined(sanitize.cfi_cross_dso) && sanitize.cfi_cross_dso) { 286 configs += [ "//build/config/sanitizers:cfi_cross_dso_" + _mode ] 287 } else { 288 configs += [ "//build/config/sanitizers:cfi_config_" + _mode ] 289 } 290 } 291 _scudo = defined(sanitize.scudo) && sanitize.scudo && !is_asan && !is_tsan 292 if (_scudo) { 293 configs += [ "//build/config/sanitizers:scudo_config" ] 294 } 295 _ubsan = defined(sanitize.ubsan) && sanitize.ubsan && !is_asan 296 if (_ubsan) { 297 configs += 298 [ "//build/config/sanitizers:undefined_behavior_sanitize_config_" + 299 _mode ] 300 } 301 _all_ubsan = defined(sanitize.all_ubsan) && sanitize.all_ubsan && !is_asan 302 if (_all_ubsan) { 303 configs += [ 304 "//build/config/sanitizers:all_undefined_behavior_sanitize_config_" + 305 _mode, 306 ] 307 } 308 _scs = defined(sanitize.scs) && sanitize.scs 309 if (_scs) { 310 configs += [ "//build/config/sanitizers:shadow_call_stack_config" ] 311 } 312 _boundary_sanitize = defined(sanitize.boundary_sanitize) && 313 sanitize.boundary_sanitize && !is_asan 314 if (_boundary_sanitize) { 315 configs += 316 [ "//build/config/sanitizers:boundary_sanitize_config_" + _mode ] 317 } 318 319 _integer_overflow = defined(sanitize.integer_overflow) && 320 sanitize.integer_overflow && !is_asan 321 _unsigned_integer_overflow = 322 defined(sanitize.unsigned_integer_overflow) && 323 sanitize.unsigned_integer_overflow && !is_asan 324 _signed_integer_overflow = defined(sanitize.signed_integer_overflow) && 325 sanitize.signed_integer_overflow && !is_asan 326 if (_unsigned_integer_overflow || _integer_overflow) { 327 configs += 328 [ "//build/config/sanitizers:unsigned_integer_overflow_config" ] 329 } 330 if (_signed_integer_overflow || _integer_overflow) { 331 configs += 332 [ "//build/config/sanitizers:signed_integer_overflow_config" ] 333 } 334 if (_integer_overflow || _unsigned_integer_overflow || 335 _signed_integer_overflow) { 336 configs += 337 [ "//build/config/sanitizers:common_integer_overflow_config_" + 338 _mode ] 339 } 340 341 if (defined(sanitize.blocklist)) { 342 cflags = [ "-fsanitize-blacklist=" + 343 rebase_path(get_path_info(sanitize.blocklist, "abspath"), 344 root_build_dir) ] 345 } 346 configs += [ "//build/config/sanitizers:compiler_rt_" + _mode ] 347 } 348 } 349} 350 351template("ohos_sanitizer_check") { 352 config(target_name) { 353 forward_variables_from(invoker, 354 [ 355 "sanitize", 356 "build_name", 357 "part_name", 358 ]) 359 need_check_cfi = false 360 need_check_intsan = false 361 bypass_cfi_target_list = [] 362 foreach(component_name, enable_cfi_part_and_bypass_list) { 363 if (part_name == component_name[0]) { 364 need_check_cfi = true 365 bypass_cfi_target_list = component_name[1] 366 } 367 } 368 foreach(component_name, enable_intsan_part_list) { 369 if (part_name == component_name) { 370 need_check_intsan = true 371 } 372 } 373 foreach(cfi_target, bypass_cfi_target_list) { 374 if (build_name == cfi_target) { 375 need_check_cfi = false 376 } 377 } 378 foreach(intsan_target, bypass_intsan_target_list) { 379 if (build_name == intsan_target) { 380 need_check_intsan = false 381 } 382 } 383 384 if (need_check_cfi) { 385 assert( 386 defined(sanitize) && defined(sanitize.cfi) && sanitize.cfi == true && 387 defined(sanitize.cfi_cross_dso) && 388 sanitize.cfi_cross_dso == true && 389 ((defined(sanitize.debug) && sanitize.debug == false) || 390 !defined(sanitize.debug)) && !defined(sanitize.blocklist), 391 "Build targets in ${part_name} should fully enable cfi(cfi, cfi_cross_dso, !debug). For local debug, try to build with '--gn-args allow_sanitize_debug=true' to bypass sanitize check. If this target ${build_name} cannot enable cfi, add the target to ${ext_sanitizer_check_list_path} in bypass_cfi_target_list.") 392 } 393 if (need_check_intsan) { 394 assert( 395 defined(sanitize) && defined(sanitize.integer_overflow) && 396 sanitize.integer_overflow, 397 "Build targets in ${part_name} should enable intsan. For local debug, try to build with '--gn-args allow_sanitize_debug=true' to bypass sanitize check. If this target ${build_name} cannot enable intsan, add the target to ${ext_sanitizer_check_list_path} in bypass_intsan_target_list.") 398 } 399 not_needed([ 400 "sanitize", 401 "build_name", 402 "part_name", 403 ]) 404 } 405} 406