xref: /aosp_15_r20/external/cronet/build/sanitizers/sanitizer_options.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // This file contains the default options for various compiler-based dynamic
6 // tools.
7 
8 #include "build/build_config.h"
9 
10 #if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) ||  \
11     defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \
12     defined(UNDEFINED_SANITIZER)
13 // The callbacks we define here will be called from the sanitizer runtime, but
14 // aren't referenced from the Chrome executable. We must ensure that those
15 // callbacks are not sanitizer-instrumented, and that they aren't stripped by
16 // the linker.
17 #define SANITIZER_HOOK_ATTRIBUTE                                           \
18   extern "C"                                                               \
19   __attribute__((no_sanitize("address", "memory", "thread", "undefined"))) \
20   __attribute__((visibility("default")))                                   \
21   __attribute__((used))
22 
23 // Functions returning default options are declared weak in the tools' runtime
24 // libraries. To make the linker pick the strong replacements for those
25 // functions from this module, we explicitly force its inclusion by passing
26 // -Wl,-u_sanitizer_options_link_helper
27 // SANITIZER_HOOK_ATTRIBUTE instead of just `extern "C"` solely to make the
28 // symbol externally visible, for ToolsSanityTest.LinksSanitizerOptions.
_sanitizer_options_link_helper()29 SANITIZER_HOOK_ATTRIBUTE void _sanitizer_options_link_helper() {}
30 #endif
31 
32 #if defined(ADDRESS_SANITIZER)
33 // Default options for AddressSanitizer in various configurations:
34 //   strip_path_prefix=/../../ - prefixes up to and including this
35 //     substring will be stripped from source file paths in symbolized reports
36 //   fast_unwind_on_fatal=1 - use the fast (frame-pointer-based) stack unwinder
37 //     to print error reports. V8 doesn't generate debug info for the JIT code,
38 //     so the slow unwinder may not work properly.
39 //   detect_stack_use_after_return=1 - use fake stack to delay the reuse of
40 //     stack allocations and detect stack-use-after-return errors.
41 //   symbolize=1 - enable in-process symbolization.
42 //   external_symbolizer_path=... - provides the path to llvm-symbolizer
43 //     relative to the main executable
44 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) | BUILDFLAG(IS_APPLE)
45 const char kAsanDefaultOptions[] =
46     "strip_path_prefix=/../../ fast_unwind_on_fatal=1 "
47     "detect_stack_use_after_return=1 symbolize=1 detect_leaks=0 "
48     "external_symbolizer_path=%d/../../third_party/llvm-build/Release+Asserts/"
49     "bin/llvm-symbolizer";
50 #elif BUILDFLAG(IS_WIN)
51 const char* kAsanDefaultOptions =
52     "strip_path_prefix=\\..\\..\\ fast_unwind_on_fatal=1 "
53     "detect_stack_use_after_return=1 symbolize=1 "
54     "external_symbolizer_path=%d/../../third_party/"
55     "llvm-build/Release+Asserts/bin/llvm-symbolizer.exe";
56 #endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_APPLE)
57 
58 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_APPLE) || \
59     BUILDFLAG(IS_WIN)
60 // Allow NaCl to override the default asan options.
61 extern const char* kAsanDefaultOptionsNaCl;
62 __attribute__((weak)) const char* kAsanDefaultOptionsNaCl = nullptr;
63 
__asan_default_options()64 SANITIZER_HOOK_ATTRIBUTE const char *__asan_default_options() {
65   if (kAsanDefaultOptionsNaCl)
66     return kAsanDefaultOptionsNaCl;
67   return kAsanDefaultOptions;
68 }
69 
70 extern char kASanDefaultSuppressions[];
71 
__asan_default_suppressions()72 SANITIZER_HOOK_ATTRIBUTE const char *__asan_default_suppressions() {
73   return kASanDefaultSuppressions;
74 }
75 #endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_APPLE)
76         // || BUILDFLAG(IS_WIN)
77 #endif  // ADDRESS_SANITIZER
78 
79 #if defined(THREAD_SANITIZER) && (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS))
80 // Default options for ThreadSanitizer in various configurations:
81 //   second_deadlock_stack=1 - more verbose deadlock reports.
82 //   report_signal_unsafe=0 - do not report async-signal-unsafe functions
83 //     called from signal handlers.
84 //   report_thread_leaks=0 - do not report unjoined threads at the end of
85 //     the program execution.
86 //   print_suppressions=1 - print the list of matched suppressions.
87 //   history_size=7 - make the history buffer proportional to 2^7 (the maximum
88 //     value) to keep more stack traces.
89 //   strip_path_prefix=/../../ - prefixes up to and including this
90 //     substring will be stripped from source file paths in symbolized reports.
91 //   external_symbolizer_path=... - provides the path to llvm-symbolizer
92 //     relative to the main executable
93 const char kTsanDefaultOptions[] =
94     "second_deadlock_stack=1 report_signal_unsafe=0 "
95     "report_thread_leaks=0 print_suppressions=1 history_size=7 "
96     "strip_path_prefix=/../../ external_symbolizer_path=%d/../../third_party/"
97     "llvm-build/Release+Asserts/bin/llvm-symbolizer";
98 
__tsan_default_options()99 SANITIZER_HOOK_ATTRIBUTE const char *__tsan_default_options() {
100   return kTsanDefaultOptions;
101 }
102 
103 extern char kTSanDefaultSuppressions[];
104 
__tsan_default_suppressions()105 SANITIZER_HOOK_ATTRIBUTE const char *__tsan_default_suppressions() {
106   return kTSanDefaultSuppressions;
107 }
108 
109 #endif  // defined(THREAD_SANITIZER) && (BUILDFLAG(IS_LINUX) ||
110         // BUILDFLAG(IS_CHROMEOS))
111 
112 #if defined(MEMORY_SANITIZER)
113 // Default options for MemorySanitizer:
114 //   strip_path_prefix=/../../ - prefixes up to and including this
115 //     substring will be stripped from source file paths in symbolized reports.
116 //   external_symbolizer_path=... - provides the path to llvm-symbolizer
117 //     relative to the main executable
118 const char kMsanDefaultOptions[] =
119     "strip_path_prefix=/../../ "
120     "external_symbolizer_path=%d/../../third_party/llvm-build/Release+Asserts/"
121     "bin/llvm-symbolizer";
122 
__msan_default_options()123 SANITIZER_HOOK_ATTRIBUTE const char *__msan_default_options() {
124   return kMsanDefaultOptions;
125 }
126 
127 #endif  // MEMORY_SANITIZER
128 
129 #if defined(LEAK_SANITIZER)
130 // Default options for LeakSanitizer:
131 //   strip_path_prefix=/../../ - prefixes up to and including this
132 //     substring will be stripped from source file paths in symbolized reports.
133 //   external_symbolizer_path=... - provides the path to llvm-symbolizer
134 //     relative to the main executable
135 //   use_poisoned=1 - Scan poisoned memory. This is useful for Oilpan (C++
136 //     garbage collection) which wants to exclude its managed memory from being
137 //     reported as leaks (through root regions) and also temporarily poisons
138 //     memory regions before calling destructors of objects to avoid destructors
139 //     cross-referencing memory in other objects. Main thread termination in
140 //     Blink is not graceful and leak checks may be emitted at any time, which
141 //     means that the garbage collector may be in a state with poisoned memory,
142 //     leading to false-positive reports.
143 const char kLsanDefaultOptions[] =
144     "strip_path_prefix=/../../ use_poisoned=1 "
145 
146 #if !BUILDFLAG(IS_FUCHSIA)
147     "external_symbolizer_path=%d/../../third_party/llvm-build/Release+Asserts/"
148     "bin/llvm-symbolizer "
149 #endif
150 
151 #if defined(ARCH_CPU_64_BITS)
152     // When pointer compression in V8 is enabled the external pointers in the
153     // heap are guaranteed to be only 4 bytes aligned. So we need this option
154     // in order to ensure that LSAN will find all the external pointers.
155     // TODO(crbug.com/328552): see updates from 2019.
156     "use_unaligned=1 "
157 #endif  // ARCH_CPU_64_BITS
158     ;
159 
__lsan_default_options()160 SANITIZER_HOOK_ATTRIBUTE const char *__lsan_default_options() {
161   return kLsanDefaultOptions;
162 }
163 
164 // TODO(https://fxbug.dev/102967): Remove when Fuchsia supports
165 // module-name-based and function-name-based suppression.
166 #if !BUILDFLAG(IS_FUCHSIA)
167 
168 extern char kLSanDefaultSuppressions[];
169 
__lsan_default_suppressions()170 SANITIZER_HOOK_ATTRIBUTE const char *__lsan_default_suppressions() {
171   return kLSanDefaultSuppressions;
172 }
173 
174 #endif  // !BUILDFLAG(IS_FUCHSIA)
175 #endif  // LEAK_SANITIZER
176 
177 #if defined(UNDEFINED_SANITIZER)
178 // Default options for UndefinedBehaviorSanitizer:
179 //   print_stacktrace=1 - print the stacktrace when UBSan reports an error.
180 const char kUbsanDefaultOptions[] =
181     "print_stacktrace=1 strip_path_prefix=/../../ "
182     "external_symbolizer_path=%d/../../third_party/llvm-build/Release+Asserts/"
183     "bin/llvm-symbolizer";
184 
__ubsan_default_options()185 SANITIZER_HOOK_ATTRIBUTE const char* __ubsan_default_options() {
186   return kUbsanDefaultOptions;
187 }
188 
189 #endif  // UNDEFINED_SANITIZER
190