From 59039fcb0170a3203234ff734c0a82649fe77661 Mon Sep 17 00:00:00 2001 From: sky Date: Tue, 28 Oct 2014 17:52:55 -0700 Subject: [PATCH] Adds ability to build for asan with gn BUG=none TEST=none R=brettw@chromium.org Review URL: https://codereview.chromium.org/682843002 Cr-Original-Commit-Position: refs/heads/master@{#301751} Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src Cr-Mirrored-Commit: fec68221196ae74b73f6c7a4c574bd33666bb7ed --- config/BUILD.gn | 7 +++++++ config/BUILDCONFIG.gn | 16 ++++++++++++++-- config/allocator.gni | 2 +- config/compiler/BUILD.gn | 29 +++++++++++++++++++++++++---- config/sanitizers/BUILD.gn | 26 ++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 config/sanitizers/BUILD.gn diff --git a/config/BUILD.gn b/config/BUILD.gn index d4629ea3a..7fbbd5d70 100644 --- a/config/BUILD.gn +++ b/config/BUILD.gn @@ -121,6 +121,13 @@ config("feature_flags") { if (use_allocator != "tcmalloc") { defines += [ "NO_TCMALLOC" ] } + if (is_asan) { + defines += [ + "ADDRESS_SANITIZER", + "MEMORY_TOOL_REPLACES_ALLOCATOR", + "MEMORY_SANITIZER_INITIAL_SIZE", + ] + } if (enable_webrtc) { defines += [ "ENABLE_WEBRTC=1" ] } diff --git a/config/BUILDCONFIG.gn b/config/BUILDCONFIG.gn index 890d33e06..151b8e962 100644 --- a/config/BUILDCONFIG.gn +++ b/config/BUILDCONFIG.gn @@ -568,7 +568,13 @@ template("component") { if (defined(invoker.data)) { data = invoker.data } if (defined(invoker.datadeps)) { datadeps = invoker.datadeps } if (defined(invoker.defines)) { defines = invoker.defines } - if (defined(invoker.deps)) { deps = invoker.deps } + # All shared libraries must have the sanitizer deps to properly link in + # asan mode (this target will be empty in other cases). + if (defined(invoker.deps)) { + deps = invoker.deps + [ "//build/config/sanitizers:deps" ] + } else { + deps = [ "//build/config/sanitizers:deps" ] + } if (defined(invoker.direct_dependent_configs)) { direct_dependent_configs = invoker.direct_dependent_configs } if (defined(invoker.forward_dependent_configs_from)) { forward_dependent_configs_from = invoker.forward_dependent_configs_from } if (defined(invoker.include_dirs)) { include_dirs = invoker.include_dirs } @@ -691,7 +697,13 @@ template("test") { if (defined(invoker.data)) { data = invoker.data } if (defined(invoker.datadeps)) { datadeps = invoker.datadeps } if (defined(invoker.defines)) { defines = invoker.defines } - if (defined(invoker.deps)) { deps = invoker.deps } + # All shared libraries must have the sanitizer deps to properly link in + # asan mode (this target will be empty in other cases). + if (defined(invoker.deps)) { + deps = invoker.deps + [ "//build/config/sanitizers:deps" ] + } else { + deps = [ "//build/config/sanitizers:deps" ] + } if (defined(invoker.direct_dependent_configs)) { direct_dependent_configs = invoker.direct_dependent_configs } if (defined(invoker.forward_dependent_configs_from)) { forward_dependent_configs_from = invoker.forward_dependent_configs_from } if (defined(invoker.include_dirs)) { include_dirs = invoker.include_dirs } diff --git a/config/allocator.gni b/config/allocator.gni index 3c4fe90bc..acc21d11a 100644 --- a/config/allocator.gni +++ b/config/allocator.gni @@ -2,7 +2,7 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -if (is_android || cpu_arch == "mipsel" || is_mac) { +if (is_android || cpu_arch == "mipsel" || is_mac || is_asan) { _default_allocator = "none" } else { _default_allocator = "tcmalloc" diff --git a/config/compiler/BUILD.gn b/config/compiler/BUILD.gn index ba44d1a3f..b80fa16a6 100644 --- a/config/compiler/BUILD.gn +++ b/config/compiler/BUILD.gn @@ -45,6 +45,9 @@ config("default_include_dirs") { ] } +# TODO(GYP): is_ubsan, is_ubsan_vptr +using_sanitizer = is_asan || is_lsan || is_tsan || is_msan + # compiler --------------------------------------------------------------------- # # Base compiler configuration. @@ -103,6 +106,22 @@ config("compiler") { # TODO(jochen): Enable this on chromeos on arm. http://crbug.com/356580 ldflags += [ "-Wl,--fatal-warnings" ] } + + # Common options for AddressSanitizer, LeakSanitizer, ThreadSanitizer and + # MemorySanitizer + if (using_sanitizer) { + cflags += [ + "-fno-omit-frame-pointer", + "-gline-tables-only", + ] + } + if (is_asan) { + cflags += [ "-fsanitize=address" ] + if (is_mac) { + cflags += [ "-mllvm -asan-globals=0" ] # http://crbug.com/352073 + # TODO(GYP): deal with mac_bundles. + } + } } if (is_clang && is_debug) { @@ -814,9 +833,9 @@ if (is_win) { common_optimize_on_ldflags = [] if (is_android) { - common_optimize_on_cflags += [ - "-fomit-frame-pointer", - ] + if (!using_sanitizer) { + common_optimize_on_cflags += [ "-fomit-frame-pointer" ] + } common_optimize_on_ldflags += [ # Warn in case of text relocations. "-Wl,--warn-shared-textrel", @@ -874,10 +893,12 @@ config("no_optimize") { # much even when optimization is disabled to get the binary size down. cflags = [ "-Os", - "-fomit-frame-pointer", "-fdata-sections", "-ffunction-sections", ] + if (!using_sanitizer) { + cflags += [ "-fomit-frame-pointer" ] + } ldflags = common_optimize_on_ldflags } else { cflags = [ "-O0" ] diff --git a/config/sanitizers/BUILD.gn b/config/sanitizers/BUILD.gn new file mode 100644 index 000000000..6012b48e3 --- /dev/null +++ b/config/sanitizers/BUILD.gn @@ -0,0 +1,26 @@ +# 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. + +# Contains the dependencies needed for asan to link into executables and +# shared_libraries. Unconditionally depend upon this target as it is empty if +# |is_asan| is false. +group("deps") { + if (is_asan) { + public_configs = [ ":sanitizer_options_link_helper" ] + deps = [ ":options_sources" ] + } +} + +config("sanitizer_options_link_helper") { + ldflags = [ "-Wl,-u_sanitizer_options_link_helper", "-fsanitize=address" ] +} + +source_set("options_sources") { + visibility = [ ":deps" ] + sources = [ "//build/sanitizers/sanitizer_options.cc" ] + + if (is_tsan) { + sources += [ "//build/sanitizers/tsan_suppressions.cc" ] + } +}