15f9996aaSopenharmony_ci# Copyright 2015 The Chromium Authors. All rights reserved.
25f9996aaSopenharmony_ci# Use of this source code is governed by a BSD-style license that can be
35f9996aaSopenharmony_ci# found in the LICENSE file.
45f9996aaSopenharmony_ci
55f9996aaSopenharmony_ciimport("//build/config/coverage/coverage.gni")
65f9996aaSopenharmony_ciimport("//build/toolchain/toolchain.gni")
75f9996aaSopenharmony_ci
85f9996aaSopenharmony_ciif (!build_xts && defined(ext_sanitizer_check_list_path)) {
95f9996aaSopenharmony_ci  import("${ext_sanitizer_check_list_path}")
105f9996aaSopenharmony_ci}
115f9996aaSopenharmony_ci
125f9996aaSopenharmony_cideclare_args() {
135f9996aaSopenharmony_ci  # Compile for Address Sanitizer to find memory bugs.
145f9996aaSopenharmony_ci  is_asan = false
155f9996aaSopenharmony_ci
165f9996aaSopenharmony_ci  # Compile for Hardware-Assisted Address Sanitizer to find memory bugs.
175f9996aaSopenharmony_ci  use_hwasan = false
185f9996aaSopenharmony_ci
195f9996aaSopenharmony_ci  # Customize asan detection.
205f9996aaSopenharmony_ci  asan_detector = false
215f9996aaSopenharmony_ci
225f9996aaSopenharmony_ci  # Compile for Leak Sanitizer to find leaks.
235f9996aaSopenharmony_ci  is_lsan = false
245f9996aaSopenharmony_ci
255f9996aaSopenharmony_ci  # Compile for Memory Sanitizer to find uninitialized reads.
265f9996aaSopenharmony_ci  is_msan = false
275f9996aaSopenharmony_ci
285f9996aaSopenharmony_ci  # Compile for Thread Sanitizer to find threading bugs.
295f9996aaSopenharmony_ci  is_tsan = false
305f9996aaSopenharmony_ci
315f9996aaSopenharmony_ci  # Compile for Undefined Behavior Sanitizer to find various types of
325f9996aaSopenharmony_ci  # undefined behavior (excludes vptr checks).
335f9996aaSopenharmony_ci  is_ubsan = false
345f9996aaSopenharmony_ci
355f9996aaSopenharmony_ci  # Halt the program if a problem is detected.
365f9996aaSopenharmony_ci  is_ubsan_no_recover = false
375f9996aaSopenharmony_ci
385f9996aaSopenharmony_ci  # Compile for Undefined Behavior Sanitizer's null pointer checks.
395f9996aaSopenharmony_ci  is_ubsan_null = false
405f9996aaSopenharmony_ci
415f9996aaSopenharmony_ci  # Compile for Undefined Behavior Sanitizer's vptr checks.
425f9996aaSopenharmony_ci  is_ubsan_vptr = false
435f9996aaSopenharmony_ci
445f9996aaSopenharmony_ci  # Compile with SafeStack shadow stack support.
455f9996aaSopenharmony_ci  is_safestack = false
465f9996aaSopenharmony_ci
475f9996aaSopenharmony_ci  # Track where uninitialized memory originates from. From fastest to slowest:
485f9996aaSopenharmony_ci  # 0 - no tracking, 1 - track only the initial allocation site, 2 - track the
495f9996aaSopenharmony_ci  # chain of stores leading from allocation site to use site.
505f9996aaSopenharmony_ci  msan_track_origins = 2
515f9996aaSopenharmony_ci
525f9996aaSopenharmony_ci  # Use dynamic libraries instrumented by one of the sanitizers instead of the
535f9996aaSopenharmony_ci  # standard system libraries. Set this flag to download prebuilt binaries from
545f9996aaSopenharmony_ci  # GCS.
555f9996aaSopenharmony_ci  use_prebuilt_instrumented_libraries = false
565f9996aaSopenharmony_ci
575f9996aaSopenharmony_ci  # Use dynamic libraries instrumented by one of the sanitizers instead of the
585f9996aaSopenharmony_ci  # standard system libraries. Set this flag to build the libraries from source.
595f9996aaSopenharmony_ci  use_locally_built_instrumented_libraries = false
605f9996aaSopenharmony_ci
615f9996aaSopenharmony_ci  # Compile with Control Flow Integrity to protect virtual calls and casts.
625f9996aaSopenharmony_ci  # See http://clang.llvm.org/docs/ControlFlowIntegrity.html
635f9996aaSopenharmony_ci  is_cfi = target_os == "linux" && !is_chromeos && target_cpu == "x64" &&
645f9996aaSopenharmony_ci           is_official_build
655f9996aaSopenharmony_ci
665f9996aaSopenharmony_ci  # Enable checks for bad casts: derived cast and unrelated cast.
675f9996aaSopenharmony_ci  use_cfi_cast = false
685f9996aaSopenharmony_ci
695f9996aaSopenharmony_ci  # Enable checks for indirect function calls via a function pointer.
705f9996aaSopenharmony_ci  use_cfi_icall = target_os == "linux" && !is_chromeos && target_cpu == "x64" &&
715f9996aaSopenharmony_ci                  is_official_build
725f9996aaSopenharmony_ci
735f9996aaSopenharmony_ci  # Print detailed diagnostics when Control Flow Integrity detects a violation.
745f9996aaSopenharmony_ci  use_cfi_diag = false
755f9996aaSopenharmony_ci
765f9996aaSopenharmony_ci  # Let Control Flow Integrity continue execution instead of crashing when
775f9996aaSopenharmony_ci  # printing diagnostics (use_cfi_diag = true).
785f9996aaSopenharmony_ci  use_cfi_recover = false
795f9996aaSopenharmony_ci
805f9996aaSopenharmony_ci  # Compile for fuzzing with LLVM LibFuzzer.
815f9996aaSopenharmony_ci  # See http://www.chromium.org/developers/testing/libfuzzer
825f9996aaSopenharmony_ci  use_libfuzzer = false
835f9996aaSopenharmony_ci
845f9996aaSopenharmony_ci  # Compile for fuzzing with AFL.
855f9996aaSopenharmony_ci  use_afl = false
865f9996aaSopenharmony_ci
875f9996aaSopenharmony_ci  # Enables core ubsan security features. Will later be removed once it matches
885f9996aaSopenharmony_ci  # is_ubsan.
895f9996aaSopenharmony_ci  is_ubsan_security = false
905f9996aaSopenharmony_ci
915f9996aaSopenharmony_ci  # Compile for fuzzing with Dr. Fuzz
925f9996aaSopenharmony_ci  # See http://www.chromium.org/developers/testing/dr-fuzz
935f9996aaSopenharmony_ci  use_drfuzz = false
945f9996aaSopenharmony_ci
955f9996aaSopenharmony_ci  # Helper variable for testing builds with disabled libfuzzer.
965f9996aaSopenharmony_ci  # Not for client use.
975f9996aaSopenharmony_ci  disable_libfuzzer = false
985f9996aaSopenharmony_ci
995f9996aaSopenharmony_ci  # Optimize for coverage guided fuzzing (balance between speed and number of
1005f9996aaSopenharmony_ci  # branches). Can be also used to remove non-determinism and other issues.
1015f9996aaSopenharmony_ci  optimize_for_fuzzing = false
1025f9996aaSopenharmony_ci
1035f9996aaSopenharmony_ci  # Value for -fsanitize-coverage flag. Setting this causes
1045f9996aaSopenharmony_ci  # use_sanitizer_coverage to be enabled.
1055f9996aaSopenharmony_ci  # This flag is not used for libFuzzer (use_libfuzzer=true) unless we are on
1065f9996aaSopenharmony_ci  # Mac. Instead, we use:
1075f9996aaSopenharmony_ci  #     -fsanitize=fuzzer-no-link
1085f9996aaSopenharmony_ci  # Default value when unset and use_fuzzing_engine=true:
1095f9996aaSopenharmony_ci  #     trace-pc-guard
1105f9996aaSopenharmony_ci  # Default value when unset and use_sanitizer_coverage=true:
1115f9996aaSopenharmony_ci  #     trace-pc-guard,indirect-calls
1125f9996aaSopenharmony_ci  sanitizer_coverage_flags = ""
1135f9996aaSopenharmony_ci
1145f9996aaSopenharmony_ci  # The global switch of cfi. Disable it to improve compiling efficiency while
1155f9996aaSopenharmony_ci  # being vulnerable to cfi attack.
1165f9996aaSopenharmony_ci  use_cfi = true
1175f9996aaSopenharmony_ci
1185f9996aaSopenharmony_ci  # The global switch of cfi debug mode.
1195f9996aaSopenharmony_ci  cfi_debug = false
1205f9996aaSopenharmony_ci}
1215f9996aaSopenharmony_ci
1225f9996aaSopenharmony_ciis_v8_host_toolchain =
1235f9996aaSopenharmony_ci    current_toolchain == "//build/toolchain/linux:clang_x64_v8_arm64" ||
1245f9996aaSopenharmony_ci    current_toolchain == "//build/toolchain/linux:clang_x86_v8_arm"
1255f9996aaSopenharmony_ci
1265f9996aaSopenharmony_ci# Disable sanitizers for non-default toolchains.
1275f9996aaSopenharmony_ciif (current_toolchain == host_toolchain || is_v8_host_toolchain) {
1285f9996aaSopenharmony_ci  is_asan = false
1295f9996aaSopenharmony_ci  is_cfi = false
1305f9996aaSopenharmony_ci  is_lsan = false
1315f9996aaSopenharmony_ci  is_msan = false
1325f9996aaSopenharmony_ci  is_tsan = false
1335f9996aaSopenharmony_ci  is_ubsan = false
1345f9996aaSopenharmony_ci  is_ubsan_null = false
1355f9996aaSopenharmony_ci  is_ubsan_no_recover = false
1365f9996aaSopenharmony_ci  is_ubsan_security = false
1375f9996aaSopenharmony_ci  is_ubsan_vptr = false
1385f9996aaSopenharmony_ci  msan_track_origins = 0
1395f9996aaSopenharmony_ci  sanitizer_coverage_flags = ""
1405f9996aaSopenharmony_ci  use_afl = false
1415f9996aaSopenharmony_ci  use_cfi_diag = false
1425f9996aaSopenharmony_ci  use_cfi_recover = false
1435f9996aaSopenharmony_ci  use_drfuzz = false
1445f9996aaSopenharmony_ci  use_libfuzzer = false
1455f9996aaSopenharmony_ci  use_prebuilt_instrumented_libraries = false
1465f9996aaSopenharmony_ci  use_locally_built_instrumented_libraries = false
1475f9996aaSopenharmony_ci  use_sanitizer_coverage = false
1485f9996aaSopenharmony_ci}
1495f9996aaSopenharmony_ci
1505f9996aaSopenharmony_ci# Whether we are doing a fuzzer build. Normally this should be checked instead
1515f9996aaSopenharmony_ci# of checking "use_libfuzzer || use_afl" because often developers forget to
1525f9996aaSopenharmony_ci# check for "use_afl".
1535f9996aaSopenharmony_ciuse_fuzzing_engine = use_libfuzzer || use_afl
1545f9996aaSopenharmony_ci
1555f9996aaSopenharmony_ci# Args that are in turn dependent on other args must be in a separate
1565f9996aaSopenharmony_ci# declare_args block. User overrides are only applied at the end of a
1575f9996aaSopenharmony_ci# declare_args block.
1585f9996aaSopenharmony_cideclare_args() {
1595f9996aaSopenharmony_ci  use_sanitizer_coverage =
1605f9996aaSopenharmony_ci      !use_clang_coverage &&
1615f9996aaSopenharmony_ci      (use_fuzzing_engine || sanitizer_coverage_flags != "")
1625f9996aaSopenharmony_ci
1635f9996aaSopenharmony_ci  # Detect overflow/underflow for global objects.
1645f9996aaSopenharmony_ci  #
1655f9996aaSopenharmony_ci  # Mac: http://crbug.com/352073
1665f9996aaSopenharmony_ci  asan_globals = !is_mac
1675f9996aaSopenharmony_ci}
1685f9996aaSopenharmony_ci
1695f9996aaSopenharmony_ciif (use_fuzzing_engine && sanitizer_coverage_flags == "") {
1705f9996aaSopenharmony_ci  sanitizer_coverage_flags = "trace-pc-guard"
1715f9996aaSopenharmony_ci} else if (use_sanitizer_coverage && sanitizer_coverage_flags == "") {
1725f9996aaSopenharmony_ci  sanitizer_coverage_flags = "trace-pc-guard,indirect-calls"
1735f9996aaSopenharmony_ci}
1745f9996aaSopenharmony_ci
1755f9996aaSopenharmony_ci# Whether we are linking against a debugging sanitizer runtime library. Among
1765f9996aaSopenharmony_ci# other things, this changes the default symbol level and other settings in
1775f9996aaSopenharmony_ci# order to prepare to create stack traces "live" using the sanitizer runtime.
1785f9996aaSopenharmony_ciusing_sanitizer =
1795f9996aaSopenharmony_ci    is_asan || is_lsan || is_tsan || is_msan || is_ubsan || is_ubsan_null ||
1805f9996aaSopenharmony_ci    is_ubsan_vptr || is_ubsan_security || use_sanitizer_coverage || use_cfi_diag
1815f9996aaSopenharmony_ci
1825f9996aaSopenharmony_ciif (!is_ohos) {
1835f9996aaSopenharmony_ci  using_sanitizer = false
1845f9996aaSopenharmony_ci}
1855f9996aaSopenharmony_ci
1865f9996aaSopenharmony_ciassert(!using_sanitizer || is_clang,
1875f9996aaSopenharmony_ci       "Sanitizers (is_*san) require setting is_clang = true in 'gn args'")
1885f9996aaSopenharmony_ci
1895f9996aaSopenharmony_ciassert(!is_cfi || is_clang,
1905f9996aaSopenharmony_ci       "is_cfi requires setting is_clang = true in 'gn args'")
1915f9996aaSopenharmony_ci
1925f9996aaSopenharmony_ciassert(!is_safestack || is_clang,
1935f9996aaSopenharmony_ci       "is_safestack requires setting is_clang = true in 'gn args'")
1945f9996aaSopenharmony_ci
1955f9996aaSopenharmony_ciprebuilt_instrumented_libraries_available =
1965f9996aaSopenharmony_ci    is_msan && (msan_track_origins == 0 || msan_track_origins == 2)
1975f9996aaSopenharmony_ci
1985f9996aaSopenharmony_ciif (use_libfuzzer && is_linux) {
1995f9996aaSopenharmony_ci  if (is_asan) {
2005f9996aaSopenharmony_ci    # We do leak checking with libFuzzer on Linux. Set is_lsan for code that
2015f9996aaSopenharmony_ci    # relies on LEAK_SANITIZER define to avoid false positives.
2025f9996aaSopenharmony_ci    is_lsan = true
2035f9996aaSopenharmony_ci  }
2045f9996aaSopenharmony_ci  if (is_msan) {
2055f9996aaSopenharmony_ci    use_prebuilt_instrumented_libraries = true
2065f9996aaSopenharmony_ci  }
2075f9996aaSopenharmony_ci}
2085f9996aaSopenharmony_ci
2095f9996aaSopenharmony_ci# MSan only links Chrome properly in release builds (brettw -- 9/1/2015). The
2105f9996aaSopenharmony_ci# same is possibly true for the other non-ASan sanitizers. But regardless of
2115f9996aaSopenharmony_ci# whether it links, one would normally never run a sanitizer in debug mode.
2125f9996aaSopenharmony_ci# Running in debug mode probably indicates you forgot to set the "is_debug =
2135f9996aaSopenharmony_ci# false" flag in the build args. ASan seems to run fine in debug mode.
2145f9996aaSopenharmony_ci#
2155f9996aaSopenharmony_ci# If you find a use-case where you want to compile a sanitizer in debug mode
2165f9996aaSopenharmony_ci# and have verified it works, ask brettw and we can consider removing it from
2175f9996aaSopenharmony_ci# this condition. We may also be able to find another way to enable your case
2185f9996aaSopenharmony_ci# without having people accidentally get broken builds by compiling an
2195f9996aaSopenharmony_ci# unsupported or unadvisable configurations.
2205f9996aaSopenharmony_ci#
2215f9996aaSopenharmony_ci# For one-off testing, just comment this assertion out.
2225f9996aaSopenharmony_ciassert(!is_debug || !(is_msan || is_ubsan || is_ubsan_null || is_ubsan_vptr),
2235f9996aaSopenharmony_ci       "Sanitizers should generally be used in release (set is_debug=false).")
2245f9996aaSopenharmony_ci
2255f9996aaSopenharmony_ciassert(!is_msan || (is_linux && current_cpu == "x64"),
2265f9996aaSopenharmony_ci       "MSan currently only works on 64-bit Linux and ChromeOS builds.")
2275f9996aaSopenharmony_ci
2285f9996aaSopenharmony_ciassert(!is_lsan || is_asan, "is_lsan = true requires is_asan = true also.")
2295f9996aaSopenharmony_ci
2305f9996aaSopenharmony_ci# ASAN build on Windows is not working in debug mode. Intercepting memory
2315f9996aaSopenharmony_ci# allocation functions is hard on Windows and not yet implemented in LLVM.
2325f9996aaSopenharmony_ciassert(!is_win || !is_debug || !is_asan,
2335f9996aaSopenharmony_ci       "ASan on Windows doesn't work in debug (set is_debug=false).")
2345f9996aaSopenharmony_ci
2355f9996aaSopenharmony_ci# Make sure that if we recover on detection (i.e. not crash), diagnostics are
2365f9996aaSopenharmony_ci# printed.
2375f9996aaSopenharmony_ciassert(!use_cfi_recover || use_cfi_diag,
2385f9996aaSopenharmony_ci       "Only use CFI recovery together with diagnostics.")
2395f9996aaSopenharmony_ci
2405f9996aaSopenharmony_ciassert(
2415f9996aaSopenharmony_ci    !(use_sanitizer_coverage && is_mac && target_os == "ios"),
2425f9996aaSopenharmony_ci    "crbug.com/753445: use_sanitizer_coverage=true is not supported by the " +
2435f9996aaSopenharmony_ci        "Chromium mac_clang_x64 toolchain on iOS distribution. Please set " +
2445f9996aaSopenharmony_ci        "the argument value to false.")
2455f9996aaSopenharmony_ci
2465f9996aaSopenharmony_ci# Use these lists of configs to disable instrumenting code that is part of a
2475f9996aaSopenharmony_ci# fuzzer, but which isn't being targeted (such as libprotobuf-mutator, *.pb.cc
2485f9996aaSopenharmony_ci# and libprotobuf when they are built as part of a proto fuzzer). Adding or
2495f9996aaSopenharmony_ci# removing these lists does not have any effect if use_libfuzzer or use_afl are
2505f9996aaSopenharmony_ci# not passed as arguments to gn.
2515f9996aaSopenharmony_cinot_fuzzed_remove_configs = []
2525f9996aaSopenharmony_cinot_fuzzed_remove_nonasan_configs = []
2535f9996aaSopenharmony_ci
2545f9996aaSopenharmony_ciif (use_fuzzing_engine) {
2555f9996aaSopenharmony_ci  # Removing coverage should always just work.
2565f9996aaSopenharmony_ci  not_fuzzed_remove_configs += [ "//build/config/coverage:default_coverage" ]
2575f9996aaSopenharmony_ci  not_fuzzed_remove_nonasan_configs +=
2585f9996aaSopenharmony_ci      [ "//build/config/coverage:default_coverage" ]
2595f9996aaSopenharmony_ci
2605f9996aaSopenharmony_ci  if (!is_msan) {
2615f9996aaSopenharmony_ci    # Allow sanitizer instrumentation to be removed if we are not using MSan
2625f9996aaSopenharmony_ci    # since binaries cannot be partially instrumented with MSan.
2635f9996aaSopenharmony_ci    not_fuzzed_remove_configs +=
2645f9996aaSopenharmony_ci        [ "//build/config/sanitizers:default_sanitizer_flags" ]
2655f9996aaSopenharmony_ci
2665f9996aaSopenharmony_ci    # Certain parts of binaries must be instrumented with ASan if the rest of
2675f9996aaSopenharmony_ci    # the binary is. For these, only remove non-ASan sanitizer instrumentation.
2685f9996aaSopenharmony_ci    if (!is_asan && !is_tsan) {
2695f9996aaSopenharmony_ci      not_fuzzed_remove_nonasan_configs +=
2705f9996aaSopenharmony_ci          [ "//build/config/sanitizers:default_sanitizer_flags" ]
2715f9996aaSopenharmony_ci      assert(not_fuzzed_remove_nonasan_configs == not_fuzzed_remove_configs)
2725f9996aaSopenharmony_ci    }
2735f9996aaSopenharmony_ci  }
2745f9996aaSopenharmony_ci}
2755f9996aaSopenharmony_ci
2765f9996aaSopenharmony_citemplate("ohos_sanitizer_config") {
2775f9996aaSopenharmony_ci  config(target_name) {
2785f9996aaSopenharmony_ci    forward_variables_from(invoker, [ "sanitize" ])
2795f9996aaSopenharmony_ci    if (defined(sanitize)) {
2805f9996aaSopenharmony_ci      configs = [ "//build/config/sanitizers:sanitizer_trap_all_flags" ]
2815f9996aaSopenharmony_ci      _mode = "release"
2825f9996aaSopenharmony_ci      _debug = (defined(sanitize.debug) && sanitize.debug) || is_asan || is_tsan
2835f9996aaSopenharmony_ci      if (_debug) {
2845f9996aaSopenharmony_ci        _mode = "debug"
2855f9996aaSopenharmony_ci      }
2865f9996aaSopenharmony_ci      _scudo = defined(sanitize.scudo) && sanitize.scudo && !is_asan && !is_tsan
2875f9996aaSopenharmony_ci      if (_scudo) {
2885f9996aaSopenharmony_ci        configs += [ "//build/config/sanitizers:scudo_config" ]
2895f9996aaSopenharmony_ci      }
2905f9996aaSopenharmony_ci      _ubsan = defined(sanitize.ubsan) && sanitize.ubsan && !is_asan && !is_tsan
2915f9996aaSopenharmony_ci      if (_ubsan) {
2925f9996aaSopenharmony_ci        configs +=
2935f9996aaSopenharmony_ci            [ "//build/config/sanitizers:undefined_behavior_sanitize_config_" +
2945f9996aaSopenharmony_ci              _mode ]
2955f9996aaSopenharmony_ci      }
2965f9996aaSopenharmony_ci      _all_ubsan = defined(sanitize.all_ubsan) && sanitize.all_ubsan &&
2975f9996aaSopenharmony_ci                   !is_asan && !is_tsan
2985f9996aaSopenharmony_ci      if (_all_ubsan) {
2995f9996aaSopenharmony_ci        configs += [
3005f9996aaSopenharmony_ci          "//build/config/sanitizers:all_undefined_behavior_sanitize_config_" +
3015f9996aaSopenharmony_ci              _mode,
3025f9996aaSopenharmony_ci        ]
3035f9996aaSopenharmony_ci      }
3045f9996aaSopenharmony_ci      _scs = defined(sanitize.scs) && sanitize.scs
3055f9996aaSopenharmony_ci      if (_scs) {
3065f9996aaSopenharmony_ci        configs += [ "//build/config/sanitizers:shadow_call_stack_config" ]
3075f9996aaSopenharmony_ci      }
3085f9996aaSopenharmony_ci      _boundary_sanitize = defined(sanitize.boundary_sanitize) &&
3095f9996aaSopenharmony_ci                           sanitize.boundary_sanitize && !is_asan && !is_tsan
3105f9996aaSopenharmony_ci      if (_boundary_sanitize) {
3115f9996aaSopenharmony_ci        configs +=
3125f9996aaSopenharmony_ci            [ "//build/config/sanitizers:boundary_sanitize_config_" + _mode ]
3135f9996aaSopenharmony_ci      }
3145f9996aaSopenharmony_ci
3155f9996aaSopenharmony_ci      _integer_overflow = defined(sanitize.integer_overflow) &&
3165f9996aaSopenharmony_ci                          sanitize.integer_overflow && !is_asan && !is_tsan
3175f9996aaSopenharmony_ci      _unsigned_integer_overflow =
3185f9996aaSopenharmony_ci          defined(sanitize.unsigned_integer_overflow) &&
3195f9996aaSopenharmony_ci          sanitize.unsigned_integer_overflow && !is_asan && !is_tsan
3205f9996aaSopenharmony_ci      _signed_integer_overflow =
3215f9996aaSopenharmony_ci          defined(sanitize.signed_integer_overflow) &&
3225f9996aaSopenharmony_ci          sanitize.signed_integer_overflow && !is_asan && !is_tsan
3235f9996aaSopenharmony_ci      if (_unsigned_integer_overflow || _integer_overflow) {
3245f9996aaSopenharmony_ci        configs +=
3255f9996aaSopenharmony_ci            [ "//build/config/sanitizers:unsigned_integer_overflow_config" ]
3265f9996aaSopenharmony_ci      }
3275f9996aaSopenharmony_ci      if (_signed_integer_overflow || _integer_overflow) {
3285f9996aaSopenharmony_ci        configs +=
3295f9996aaSopenharmony_ci            [ "//build/config/sanitizers:signed_integer_overflow_config" ]
3305f9996aaSopenharmony_ci      }
3315f9996aaSopenharmony_ci      if (_integer_overflow || _unsigned_integer_overflow ||
3325f9996aaSopenharmony_ci          _signed_integer_overflow) {
3335f9996aaSopenharmony_ci        configs +=
3345f9996aaSopenharmony_ci            [ "//build/config/sanitizers:common_integer_overflow_config_" +
3355f9996aaSopenharmony_ci              _mode ]
3365f9996aaSopenharmony_ci      }
3375f9996aaSopenharmony_ci
3385f9996aaSopenharmony_ci      if (defined(sanitize.blocklist)) {
3395f9996aaSopenharmony_ci        cflags = [ "-fsanitize-blacklist=" +
3405f9996aaSopenharmony_ci                   rebase_path(get_path_info(sanitize.blocklist, "abspath"),
3415f9996aaSopenharmony_ci                               root_build_dir) ]
3425f9996aaSopenharmony_ci      }
3435f9996aaSopenharmony_ci      if (defined(sanitize.cfi) && sanitize.cfi && cfi_debug) {
3445f9996aaSopenharmony_ci        _mode = "debug"
3455f9996aaSopenharmony_ci      }
3465f9996aaSopenharmony_ci      _cfi = use_cfi && defined(sanitize.cfi) && sanitize.cfi && !is_asan &&
3475f9996aaSopenharmony_ci             !is_tsan
3485f9996aaSopenharmony_ci      if (_cfi) {
3495f9996aaSopenharmony_ci        if (defined(sanitize.cfi_cross_dso) && sanitize.cfi_cross_dso) {
3505f9996aaSopenharmony_ci          configs += [ "//build/config/sanitizers:cfi_cross_dso_" + _mode ]
3515f9996aaSopenharmony_ci        } else {
3525f9996aaSopenharmony_ci          configs += [ "//build/config/sanitizers:cfi_config_" + _mode ]
3535f9996aaSopenharmony_ci        }
3545f9996aaSopenharmony_ci        if (!defined(sanitize.cfi_check_std) || !sanitize.cfi_check_std) {
3555f9996aaSopenharmony_ci          configs += [ "//build/config/sanitizers:cfi_config_skip_std" ]
3565f9996aaSopenharmony_ci        }
3575f9996aaSopenharmony_ci        if (defined(sanitize.cfi_no_nvcall) && sanitize.cfi_no_nvcall) {
3585f9996aaSopenharmony_ci          configs += [ "//build/config/sanitizers:cfi_no_nvcall" ]
3595f9996aaSopenharmony_ci        }
3605f9996aaSopenharmony_ci        if (defined(sanitize.cfi_vcall_icall_only) &&
3615f9996aaSopenharmony_ci            sanitize.cfi_vcall_icall_only) {
3625f9996aaSopenharmony_ci          configs += [ "//build/config/sanitizers:cfi_no_all" ]
3635f9996aaSopenharmony_ci          configs += [ "//build/config/sanitizers:cfi_vcall" ]
3645f9996aaSopenharmony_ci          configs += [ "//build/config/sanitizers:cfi_icall" ]
3655f9996aaSopenharmony_ci        }
3665f9996aaSopenharmony_ci        configs += [ "//build/config/sanitizers:cfi_trap_function_flags" ]
3675f9996aaSopenharmony_ci      }
3685f9996aaSopenharmony_ci      configs += [ "//build/config/sanitizers:compiler_rt_" + _mode ]
3695f9996aaSopenharmony_ci    }
3705f9996aaSopenharmony_ci  }
3715f9996aaSopenharmony_ci}
3725f9996aaSopenharmony_ci
3735f9996aaSopenharmony_citemplate("ohos_sanitizer_check") {
3745f9996aaSopenharmony_ci  config(target_name) {
3755f9996aaSopenharmony_ci    forward_variables_from(invoker,
3765f9996aaSopenharmony_ci                           [
3775f9996aaSopenharmony_ci                             "sanitize",
3785f9996aaSopenharmony_ci                             "build_name",
3795f9996aaSopenharmony_ci                             "part_name",
3805f9996aaSopenharmony_ci                           ])
3815f9996aaSopenharmony_ci    need_check_cfi = false
3825f9996aaSopenharmony_ci    need_check_intsan = false
3835f9996aaSopenharmony_ci    need_check_debug = true
3845f9996aaSopenharmony_ci    bypass_cfi_target_list = []
3855f9996aaSopenharmony_ci
3865f9996aaSopenharmony_ci    foreach(config_name, bypass_config_debug_list) {
3875f9996aaSopenharmony_ci      if (part_name == config_name) {
3885f9996aaSopenharmony_ci        need_check_debug = false
3895f9996aaSopenharmony_ci      }
3905f9996aaSopenharmony_ci    }
3915f9996aaSopenharmony_ci
3925f9996aaSopenharmony_ci    if (need_check_debug && defined(sanitize) && defined(sanitize.debug)) {
3935f9996aaSopenharmony_ci      assert(
3945f9996aaSopenharmony_ci          sanitize.debug == false,
3955f9996aaSopenharmony_ci          "Enable CFI can't configure 'debug=true'. Please check this target ${build_name} in ${part_name}. For local debug, try to build with '--gn-args allow_sanitize_debug=true' to bypass sanitize check.")
3965f9996aaSopenharmony_ci    }
3975f9996aaSopenharmony_ci
3985f9996aaSopenharmony_ci    foreach(component_name, enable_cfi_part_and_bypass_list) {
3995f9996aaSopenharmony_ci      if (part_name == component_name[0]) {
4005f9996aaSopenharmony_ci        need_check_cfi = true
4015f9996aaSopenharmony_ci        bypass_cfi_target_list = component_name[1]
4025f9996aaSopenharmony_ci      }
4035f9996aaSopenharmony_ci    }
4045f9996aaSopenharmony_ci    foreach(component_name, enable_intsan_part_list) {
4055f9996aaSopenharmony_ci      if (part_name == component_name) {
4065f9996aaSopenharmony_ci        need_check_intsan = true
4075f9996aaSopenharmony_ci      }
4085f9996aaSopenharmony_ci    }
4095f9996aaSopenharmony_ci    foreach(cfi_target, bypass_cfi_target_list) {
4105f9996aaSopenharmony_ci      if (build_name == cfi_target) {
4115f9996aaSopenharmony_ci        need_check_cfi = false
4125f9996aaSopenharmony_ci      }
4135f9996aaSopenharmony_ci    }
4145f9996aaSopenharmony_ci    foreach(intsan_target, bypass_intsan_target_list) {
4155f9996aaSopenharmony_ci      if (build_name == intsan_target) {
4165f9996aaSopenharmony_ci        need_check_intsan = false
4175f9996aaSopenharmony_ci      }
4185f9996aaSopenharmony_ci    }
4195f9996aaSopenharmony_ci
4205f9996aaSopenharmony_ci    if (need_check_cfi) {
4215f9996aaSopenharmony_ci      assert(
4225f9996aaSopenharmony_ci          defined(sanitize) && defined(sanitize.cfi) && sanitize.cfi == true &&
4235f9996aaSopenharmony_ci              defined(sanitize.cfi_cross_dso) &&
4245f9996aaSopenharmony_ci              sanitize.cfi_cross_dso == true &&
4255f9996aaSopenharmony_ci              ((defined(sanitize.debug) && sanitize.debug == false) ||
4265f9996aaSopenharmony_ci                   !defined(sanitize.debug)) && !defined(sanitize.blocklist),
4275f9996aaSopenharmony_ci          "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_${build_name}.")
4285f9996aaSopenharmony_ci    }
4295f9996aaSopenharmony_ci    if (need_check_intsan) {
4305f9996aaSopenharmony_ci      assert(
4315f9996aaSopenharmony_ci          defined(sanitize) && defined(sanitize.integer_overflow) &&
4325f9996aaSopenharmony_ci              sanitize.integer_overflow,
4335f9996aaSopenharmony_ci          "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.")
4345f9996aaSopenharmony_ci    }
4355f9996aaSopenharmony_ci    not_needed([
4365f9996aaSopenharmony_ci                 "sanitize",
4375f9996aaSopenharmony_ci                 "build_name",
4385f9996aaSopenharmony_ci                 "part_name",
4395f9996aaSopenharmony_ci               ])
4405f9996aaSopenharmony_ci  }
4415f9996aaSopenharmony_ci}
442