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 15 import("//gn/standalone/sanitizers/sanitizers.gni") 16 17 # Link dependencies for sanitizers for executables. 18 group("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 30 if (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 41 config("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 (use_libfuzzer) { 91 cflags += [ "-fsanitize=fuzzer" ] 92 if (is_asan) { 93 cflags += [ 94 "-mllvm", 95 "-asan-use-private-alias", 96 ] 97 } 98 } 99 } 100 101 config("sanitizer_options_link_helper") { 102 if (is_mac) { 103 ldflags = [ "-Wl,-U,_sanitizer_options_link_helper" ] 104 } 105 } 106 107 config("sanitizers_ldflags") { 108 visibility = [ ":deps" ] 109 ldflags = [] 110 if (is_asan) { 111 ldflags += [ "-fsanitize=address" ] 112 } 113 if (is_lsan) { 114 # This is not a copy/paste mistake. The LSan runtime library has 115 # moved into asan. So in order to make LSan work one has to build 116 # .cc files with -fsanitize=leak but link with -fsanitize=address. 117 ldflags += [ "-fsanitize=address" ] 118 } 119 if (is_tsan) { 120 ldflags += [ "-fsanitize=thread" ] 121 } 122 if (is_msan) { 123 ldflags += [ "-fsanitize=memory" ] 124 } 125 if (is_ubsan) { 126 ldflags += [ "-fsanitize=undefined" ] 127 } 128 configs = [ ":sanitizer_options_link_helper" ] 129 } 130