xref: /aosp_15_r20/external/perfetto/gn/standalone/sanitizers/BUILD.gn (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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    deps = [ ":ignorelist_copy" ]
23    if (is_android && sanitizer_lib != "" && !sanitizer_lib_dir_is_static) {
24      deps += [ ":copy_sanitizer_lib" ]
25    }
26  }
27}
28
29if (is_android && sanitizer_lib != "" && !sanitizer_lib_dir_is_static) {
30  copy("copy_sanitizer_lib") {
31    sources = [ "${sanitizer_lib_dir}/lib${sanitizer_lib}.so" ]
32    outputs = [ "${root_out_dir}/sanitizer_libs/lib${sanitizer_lib}.so" ]
33  }
34}
35
36# Add a dependency on the ignorelist.txt file to cause rebuilds when
37# the file changes.
38copy("ignorelist_copy") {
39  sources = [ "ignorelist.txt" ]
40  outputs = [ "${target_out_dir}/ignorelist.txt" ]
41}
42
43config("sanitizers_cflags") {
44  if (using_sanitizer) {
45    ignorelist_path_ = rebase_path("ignorelist.txt", root_build_dir)
46    cflags = [
47      "-fno-omit-frame-pointer",
48      "-fsanitize-ignorelist=$ignorelist_path_",
49    ]
50    defines = []
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}
104
105config("sanitizer_options_link_helper") {
106  if (is_mac) {
107    ldflags = [ "-Wl,-U,_sanitizer_options_link_helper" ]
108  }
109}
110
111config("sanitizers_ldflags") {
112  if (using_sanitizer) {
113    visibility = [ ":deps" ]
114    ldflags = []
115    if (is_asan) {
116      ldflags += [ "-fsanitize=address" ]
117    }
118    if (is_lsan) {
119      # This is not a copy/paste mistake. The LSan runtime library has
120      # moved into asan. So in order to make LSan work one has to build
121      # .cc files with -fsanitize=leak but link with -fsanitize=address.
122      ldflags += [ "-fsanitize=address" ]
123    }
124    if (is_tsan) {
125      ldflags += [ "-fsanitize=thread" ]
126    }
127    if (is_msan) {
128      ldflags += [ "-fsanitize=memory" ]
129    }
130    if (is_ubsan) {
131      ldflags += [ "-fsanitize=undefined" ]
132    }
133    configs = [ ":sanitizer_options_link_helper" ]
134  }
135}
136