blob: 0d144dde9bf55b84f1e905ae6803067f40d202bf [file] [log] [blame]
# Copied from chromium's build/config/sanitizers/BUILD.gn,
# and modified for goma.
#
# Copyright 2014 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.
import("//build/config/clang/clang.gni")
import("//build/toolchain/toolchain.gni")
declare_args() {
sanitizer_coverage_flags = ""
}
if (using_sanitizer) {
sanitizer_coverage_flags = "edge,indirect-calls,trace-pc-guard"
}
# Contains the dependencies needed for sanitizers to link into executables and
# shared_libraries. Unconditionally depend upon this target as it is empty if
# |is_asan|, |is_lsan|, |is_tsan|, |is_msan| and |use_custom_libcxx| are false.
group("deps") {
public_deps = [ ":deps_no_options" ]
if (using_sanitizer) {
public_configs = [
":sanitizer_options_link_helper",
# Even when a target removes default_sanitizer_flags, it may be depending
# on a library that did not remove default_sanitizer_flags. Thus, we need
# to add the ldflags here as well as in default_sanitizer_flags.
":default_sanitizer_ldflags",
]
deps = [ ":options_sources" ]
}
#if (use_afl) {
# deps += [ "//third_party/afl" ]
#}
}
group("deps_no_options") {
if (using_sanitizer) {
public_configs = [
# Even when a target removes default_sanitizer_flags, it may be depending
# on a library that did not remove default_sanitizer_flags. Thus, we need
# to add the ldflags here as well as in default_sanitizer_flags.
":default_sanitizer_ldflags",
]
deps = []
public_deps = []
data = [ "//tools/valgrind/asan/" ]
if (os == "win") {
exe = ".exe"
} else {
exe = ""
}
data += [ "$clang_base_path/bin/llvm-symbolizer${exe}" ]
if (os == "linux") {
# llvm-symbolizer needs this.
data += [ "$clang_base_path/lib/libstdc++.so.6" ]
}
#if (use_prebuilt_instrumented_libraries ||
# use_locally_built_instrumented_libraries) {
# deps += [ "//third_party/instrumented_libraries:deps" ]
#}
if (use_custom_libcxx) {
data += [ "$root_out_dir/libc++.so" ]
}
# ASAN is supported on iOS but the runtime library depends on the compiler
# used (Chromium version of clang versus Xcode version of clang). Only copy
# the ASAN runtime on iOS if building with Chromium clang.
if (os == "win" || os == "mac") {
data_deps = [ ":copy_asan_runtime" ]
}
if (os == "mac") {
public_deps += [ ":asan_runtime_bundle_data" ]
}
}
}
if ((os == "mac" || os == "win") && using_sanitizer) {
if (os == "mac") {
_clang_rt_dso_path = "darwin/libclang_rt.asan_osx_dynamic.dylib"
} else if (os == "win" && target_cpu == "x86") {
_clang_rt_dso_path = "windows/clang_rt.asan_dynamic-i386.dll"
} else if (os == "win" && target_cpu == "x64") {
_clang_rt_dso_path = "windows/clang_rt.asan_dynamic-x86_64.dll"
}
_clang_rt_dso_full_path =
"$clang_base_path/lib/clang/$clang_version/lib/$_clang_rt_dso_path"
copy("copy_asan_runtime") {
sources = [ _clang_rt_dso_full_path ]
outputs = [ "$root_out_dir/{{source_file_part}}" ]
}
if (os == "mac") {
bundle_data("asan_runtime_bundle_data") {
sources = get_target_outputs(":copy_asan_runtime")
outputs = [ "{{bundle_executable_dir}}/{{source_file_part}}" ]
public_deps = [ ":copy_asan_runtime" ]
}
}
}
config("sanitizer_options_link_helper") {
if (os == "mac") {
ldflags = [ "-Wl,-U,_sanitizer_options_link_helper" ]
} else if (os != "win") {
ldflags = [ "-Wl,-u_sanitizer_options_link_helper" ]
}
}
static_library("options_sources") {
# This is a static_library instead of a source_set, as it shouldn't be
# unconditionally linked into targets.
visibility = [
":deps",
"//:gn_visibility",
]
sources = [ "//build/sanitizers/sanitizer_options.cc" ]
# Don't compile this target with any sanitizer code. It can be called from
# the sanitizer runtimes, so instrumenting these functions could cause
# recursive calls into the runtime if there is an error.
configs -= [ "//build/config/sanitizers:default_sanitizer_flags" ]
if (is_asan) {
sources += [ "//build/sanitizers/asan_suppressions.cc" ]
}
#if (is_lsan) {
# sources += [ lsan_suppressions_file ]
#}
if (is_tsan) {
sources += [ "//build/sanitizers/tsan_suppressions.cc" ]
}
}
# Applies linker flags necessary when either :deps or :default_sanitizer_flags
# are used.
config("default_sanitizer_ldflags") {
visibility = [
":default_sanitizer_flags",
":deps",
]
if (is_posix) {
ldflags = []
if (is_asan) {
ldflags += [ "-fsanitize=address" ]
}
#if (is_lsan) {
# ldflags += [ "-fsanitize=leak" ]
#}
if (is_tsan) {
ldflags += [ "-fsanitize=thread" ]
}
if (is_msan) {
ldflags += [ "-fsanitize=memory" ]
}
#if (is_ubsan || is_ubsan_security) {
# ldflags += [ "-fsanitize=undefined" ]
#}
#if (is_ubsan_null) {
# ldflags += [ "-fsanitize=null" ]
#}
#if (is_ubsan_vptr) {
# ldflags += [ "-fsanitize=vptr" ]
#}
if (use_sanitizer_coverage) {
if (use_libfuzzer) {
ldflags += [ "-fsanitize=fuzzer-no-link" ]
if (is_mac) {
# TODO: on macOS, dead code stripping does not work
# well with `pc-table` instrumentation enabled by `fuzzer-no-link`.
ldflags += [ "-fno-sanitize-coverage=pc-table" ]
}
} else {
ldflags += [ "-fsanitize-coverage=$sanitizer_coverage_flags" ]
}
}
#if (is_cfi && !is_nacl) {
# ldflags += [ "-fsanitize=cfi-vcall" ]
# if (use_cfi_cast) {
# ldflags += [
# "-fsanitize=cfi-derived-cast",
# "-fsanitize=cfi-unrelated-cast",
# ]
# }
# if (use_cfi_diag) {
# ldflags += [
# "-fno-sanitize-trap=cfi",
# "-fsanitize-recover=cfi",
# ]
# }
#}
}
if (is_asan && os == "win") {
if (target_cpu == "x64") {
libs = [ "$clang_base_path/lib/clang/$clang_version/lib/windows/clang_rt.asan-x86_64.lib" ]
} else {
libs = [ "$clang_base_path/lib/clang/$clang_version/lib/windows/clang_rt.asan-i386.lib" ]
}
}
}
config("common_sanitizer_flags") {
cflags = []
cflags_cc = []
# Sanitizers need line table info for stack traces. They don't need type info
# or variable info, so we can leave that out to speed up the build.
if (using_sanitizer) {
assert(is_clang, "sanitizers only supported with clang")
cflags += [
"-gline-tables-only",
# Column info in debug data confuses Visual Studio's debugger, so don't
# use this by default. However, clusterfuzz needs it for good attribution
# of reports to CLs, so turn it on there.
"-gcolumn-info",
]
}
# Common options for AddressSanitizer, LeakSanitizer, ThreadSanitizer,
# MemorySanitizer and non-official CFI builds.
if (using_sanitizer) {
if (is_posix) {
cflags += [ "-fno-omit-frame-pointer" ]
} else {
cflags += [ "/Oy-" ]
}
}
if (use_custom_libcxx) {
prefix = "//buildtools/third_party"
include = "trunk/include"
defines = [
"_LIBCPP_ENABLE_NODISCARD",
# TODO: libc++ requires this macro.
"_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS",
]
cflags_cc += [
"-nostdinc++",
"-isystem" + rebase_path("$prefix/libc++/$include", root_build_dir),
"-isystem" + rebase_path("$prefix/libc++abi/$include", root_build_dir),
]
}
}
config("asan_flags") {
if (is_asan) {
cflags = [ "-fsanitize=address" ]
}
}
config("coverage_flags") {
cflags = []
if (use_sanitizer_coverage) {
# Used by sandboxing code to allow coverage dump to be written on the disk.
defines = [ "SANITIZER_COVERAGE" ]
if (use_libfuzzer) {
cflags += [ "-fsanitize=fuzzer-no-link" ]
if (is_mac) {
# TODO: on macOS, dead code stripping does not work
# well with `pc-table` instrumentation enabled by `fuzzer-no-link`.
cflags += [ "-fno-sanitize-coverage=pc-table" ]
}
} else {
cflags += [
"-fsanitize-coverage=$sanitizer_coverage_flags",
"-mllvm",
"-sanitizer-coverage-prune-blocks=1",
]
if (current_cpu == "arm") {
# http://crbug.com/517105
cflags += [
"-mllvm",
"-sanitizer-coverage-block-threshold=0",
]
}
}
}
}
config("msan_flags") {
if (is_msan) {
assert(os == "linux", "msan only supported on linux x86_64")
if (!defined(msan_denylist_path)) {
msan_denylist_path =
rebase_path("//build/sanitizers/msan_blacklist.txt", root_build_dir)
}
cflags = [
"-fsanitize=memory",
"-fsanitize-blacklist=$msan_denylist_path",
"-fsanitize-memory-track-origins=$msan_track_origins",
]
}
}
config("tsan_flags") {
if (is_tsan) {
assert(os == "linux", "tsan only supported on linux x86_64")
cflags = [ "-fsanitize=thread" ]
}
}
config("ubsan_vptr_flags") {
}
all_sanitizer_configs = [
":common_sanitizer_flags",
":coverage_flags",
":default_sanitizer_ldflags",
":asan_flags",
":tsan_flags",
":msan_flags",
#":cfi_flags",
#":lsan_flags",
#":ubsan_flags",
#":ubsan_no_recover",
#":ubsan_null_flags",
#":ubsan_security_flags",
":ubsan_vptr_flags",
]
# This config is applied by default to all targets. It sets the compiler flags
# for sanitizer usage, or, if no sanitizer is set, does nothing.
#
# This needs to be in a separate config so that targets can opt out of
# sanitizers (by removing the config) if they desire. Even if a target
# removes this config, executables & shared libraries should still depend on
# :deps if any of their dependencies have not opted out of sanitizers.
# Keep this list in sync with default_sanitizer_flags_but_ubsan_vptr.
config("default_sanitizer_flags") {
configs = all_sanitizer_configs
}
# This config is equivalent to default_sanitizer_flags, but excludes ubsan_vptr.
# This allows to selectively disable ubsan_vptr, when needed. In particular,
# if some third_party code is required to be compiled without rtti, which
# is a requirement for ubsan_vptr.
config("default_sanitizer_flags_but_ubsan_vptr") {
configs = all_sanitizer_configs - [ ":ubsan_vptr_flags" ]
}
# This config is equivalent to default_sanitizer_flags, but excludes ubsan_vptr.
# This allows to selectively disable ubsan_vptr, when needed. In particular,
# if some third_party code is required to be compiled without rtti, which
# is a requirement for ubsan_vptr.
#config("default_sanitizer_flags_but_ubsan_vptr") {
# configs = all_sanitizer_configs - [ ":ubsan_vptr_flags" ]
#}
config("default_sanitizer_flags_but_coverage") {
configs = all_sanitizer_configs - [ ":coverage_flags" ]
}