1# Copyright (C) 2017 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import("//gn/standalone/sanitizers/sanitizers.gni") 16 17# Link dependencies for sanitizers for executables. 18group("deps") { 19 visibility = [ "*" ] 20 if (using_sanitizer) { 21 public_configs = [ ":sanitizers_ldflags" ] 22 if (is_android && sanitizer_lib != "" && !sanitizer_lib_dir_is_static) { 23 deps = [ 24 ":copy_sanitizer_lib", 25 ] 26 } 27 } 28} 29 30if (is_android && sanitizer_lib != "" && !sanitizer_lib_dir_is_static) { 31 copy("copy_sanitizer_lib") { 32 sources = [ 33 "${sanitizer_lib_dir}/lib${sanitizer_lib}.so", 34 ] 35 outputs = [ 36 "${root_out_dir}/sanitizer_libs/lib${sanitizer_lib}.so", 37 ] 38 } 39} 40 41config("sanitizers_cflags") { 42 cflags = [] 43 defines = [] 44 if (using_sanitizer) { 45 blacklist_path_ = rebase_path("blacklist.txt", root_build_dir) 46 cflags += [ 47 "-fno-omit-frame-pointer", 48 "-fsanitize-blacklist=$blacklist_path_", 49 ] 50 } 51 52 if (is_asan) { 53 cflags += [ "-fsanitize=address" ] 54 defines += [ "ADDRESS_SANITIZER" ] 55 } 56 if (is_lsan) { 57 cflags += [ "-fsanitize=leak" ] 58 defines += [ "LEAK_SANITIZER" ] 59 } 60 if (is_tsan) { 61 cflags += [ "-fsanitize=thread" ] 62 defines += [ 63 "THREAD_SANITIZER", 64 "DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL=1", 65 ] 66 } 67 if (is_msan) { 68 cflags += [ 69 "-fsanitize=memory", 70 "-fsanitize-memory-track-origins=2", 71 ] 72 defines += [ "MEMORY_SANITIZER" ] 73 } 74 if (is_ubsan) { 75 cflags += [ 76 "-fsanitize=bounds", 77 "-fsanitize=float-divide-by-zero", 78 "-fsanitize=integer-divide-by-zero", 79 "-fsanitize=null", 80 "-fsanitize=object-size", 81 "-fsanitize=return", 82 "-fsanitize=returns-nonnull-attribute", 83 "-fsanitize=shift-exponent", 84 "-fsanitize=signed-integer-overflow", 85 "-fsanitize=unreachable", 86 "-fsanitize=vla-bound", 87 ] 88 defines += [ "UNDEFINED_SANITIZER" ] 89 } 90 if (is_fuzzer) { 91 # FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION is also defined by oss-fuzz, 92 # so using the same name. 93 defines += [ "FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION" ] 94 cflags += [ "-fsanitize=fuzzer-no-link" ] 95 if (is_asan) { 96 cflags += [ 97 "-mllvm", 98 "-asan-use-private-alias", 99 ] 100 } 101 } 102} 103 104config("sanitizer_options_link_helper") { 105 if (is_mac) { 106 ldflags = [ "-Wl,-U,_sanitizer_options_link_helper" ] 107 } 108} 109 110config("sanitizers_ldflags") { 111 visibility = [ ":deps" ] 112 ldflags = [] 113 if (is_asan) { 114 ldflags += [ "-fsanitize=address" ] 115 } 116 if (is_lsan) { 117 # This is not a copy/paste mistake. The LSan runtime library has 118 # moved into asan. So in order to make LSan work one has to build 119 # .cc files with -fsanitize=leak but link with -fsanitize=address. 120 ldflags += [ "-fsanitize=address" ] 121 } 122 if (is_tsan) { 123 ldflags += [ "-fsanitize=thread" ] 124 } 125 if (is_msan) { 126 ldflags += [ "-fsanitize=memory" ] 127 } 128 if (is_ubsan) { 129 ldflags += [ "-fsanitize=undefined" ] 130 } 131 configs = [ ":sanitizer_options_link_helper" ] 132} 133