]> git.proxmox.com Git - rustc.git/blame - src/libcompiler_builtins/compiler-rt/lib/asan/asan_flags.cc
New upstream version 1.25.0+dfsg1
[rustc.git] / src / libcompiler_builtins / compiler-rt / lib / asan / asan_flags.cc
CommitLineData
92a42be0
SL
1//===-- asan_flags.cc -------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// ASan flag parsing logic.
13//===----------------------------------------------------------------------===//
14
15#include "asan_activation.h"
16#include "asan_flags.h"
17#include "asan_interface_internal.h"
18#include "asan_stack.h"
19#include "lsan/lsan_common.h"
20#include "sanitizer_common/sanitizer_common.h"
21#include "sanitizer_common/sanitizer_flags.h"
22#include "sanitizer_common/sanitizer_flag_parser.h"
23#include "ubsan/ubsan_flags.h"
24#include "ubsan/ubsan_platform.h"
25
26namespace __asan {
27
28Flags asan_flags_dont_use_directly; // use via flags().
29
30static const char *MaybeCallAsanDefaultOptions() {
31 return (&__asan_default_options) ? __asan_default_options() : "";
32}
33
34static const char *MaybeUseAsanDefaultOptionsCompileDefinition() {
35#ifdef ASAN_DEFAULT_OPTIONS
36// Stringize the macro value.
37# define ASAN_STRINGIZE(x) #x
38# define ASAN_STRINGIZE_OPTIONS(options) ASAN_STRINGIZE(options)
39 return ASAN_STRINGIZE_OPTIONS(ASAN_DEFAULT_OPTIONS);
40#else
41 return "";
42#endif
43}
44
45void Flags::SetDefaults() {
46#define ASAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
47#include "asan_flags.inc"
48#undef ASAN_FLAG
49}
50
51static void RegisterAsanFlags(FlagParser *parser, Flags *f) {
52#define ASAN_FLAG(Type, Name, DefaultValue, Description) \
53 RegisterFlag(parser, #Name, Description, &f->Name);
54#include "asan_flags.inc"
55#undef ASAN_FLAG
56}
57
58void InitializeFlags() {
59 // Set the default values and prepare for parsing ASan and common flags.
60 SetCommonFlagsDefaults();
61 {
62 CommonFlags cf;
63 cf.CopyFrom(*common_flags());
2c00a5a8 64 cf.detect_leaks = cf.detect_leaks && CAN_SANITIZE_LEAKS;
92a42be0
SL
65 cf.external_symbolizer_path = GetEnv("ASAN_SYMBOLIZER_PATH");
66 cf.malloc_context_size = kDefaultMallocContextSize;
67 cf.intercept_tls_get_addr = true;
68 cf.exitcode = 1;
69 OverrideCommonFlags(cf);
70 }
71 Flags *f = flags();
72 f->SetDefaults();
73
74 FlagParser asan_parser;
75 RegisterAsanFlags(&asan_parser, f);
76 RegisterCommonFlags(&asan_parser);
77
78 // Set the default values and prepare for parsing LSan and UBSan flags
79 // (which can also overwrite common flags).
80#if CAN_SANITIZE_LEAKS
81 __lsan::Flags *lf = __lsan::flags();
82 lf->SetDefaults();
83
84 FlagParser lsan_parser;
85 __lsan::RegisterLsanFlags(&lsan_parser, lf);
86 RegisterCommonFlags(&lsan_parser);
87#endif
88
89#if CAN_SANITIZE_UB
90 __ubsan::Flags *uf = __ubsan::flags();
91 uf->SetDefaults();
92
93 FlagParser ubsan_parser;
94 __ubsan::RegisterUbsanFlags(&ubsan_parser, uf);
95 RegisterCommonFlags(&ubsan_parser);
96#endif
97
2c00a5a8
XL
98 if (SANITIZER_MAC) {
99 // Support macOS MallocScribble and MallocPreScribble:
100 // <https://developer.apple.com/library/content/documentation/Performance/
101 // Conceptual/ManagingMemory/Articles/MallocDebug.html>
102 if (GetEnv("MallocScribble")) {
103 f->max_free_fill_size = 0x1000;
104 }
105 if (GetEnv("MallocPreScribble")) {
106 f->malloc_fill_byte = 0xaa;
107 }
108 }
109
92a42be0
SL
110 // Override from ASan compile definition.
111 const char *asan_compile_def = MaybeUseAsanDefaultOptionsCompileDefinition();
112 asan_parser.ParseString(asan_compile_def);
113
114 // Override from user-specified string.
115 const char *asan_default_options = MaybeCallAsanDefaultOptions();
116 asan_parser.ParseString(asan_default_options);
117#if CAN_SANITIZE_UB
118 const char *ubsan_default_options = __ubsan::MaybeCallUbsanDefaultOptions();
119 ubsan_parser.ParseString(ubsan_default_options);
120#endif
2c00a5a8
XL
121#if CAN_SANITIZE_LEAKS
122 const char *lsan_default_options = __lsan::MaybeCallLsanDefaultOptions();
123 lsan_parser.ParseString(lsan_default_options);
124#endif
92a42be0
SL
125
126 // Override from command line.
127 asan_parser.ParseString(GetEnv("ASAN_OPTIONS"));
128#if CAN_SANITIZE_LEAKS
129 lsan_parser.ParseString(GetEnv("LSAN_OPTIONS"));
130#endif
131#if CAN_SANITIZE_UB
132 ubsan_parser.ParseString(GetEnv("UBSAN_OPTIONS"));
133#endif
134
5bcae85e 135 InitializeCommonFlags();
92a42be0
SL
136
137 // TODO(eugenis): dump all flags at verbosity>=2?
138 if (Verbosity()) ReportUnrecognizedFlags();
139
140 if (common_flags()->help) {
141 // TODO(samsonov): print all of the flags (ASan, LSan, common).
142 asan_parser.PrintFlagDescriptions();
143 }
144
145 // Flag validation:
146 if (!CAN_SANITIZE_LEAKS && common_flags()->detect_leaks) {
147 Report("%s: detect_leaks is not supported on this platform.\n",
148 SanitizerToolName);
149 Die();
150 }
2c00a5a8
XL
151 // Ensure that redzone is at least SHADOW_GRANULARITY.
152 if (f->redzone < (int)SHADOW_GRANULARITY)
153 f->redzone = SHADOW_GRANULARITY;
92a42be0
SL
154 // Make "strict_init_order" imply "check_initialization_order".
155 // TODO(samsonov): Use a single runtime flag for an init-order checker.
156 if (f->strict_init_order) {
157 f->check_initialization_order = true;
158 }
159 CHECK_LE((uptr)common_flags()->malloc_context_size, kStackTraceMax);
160 CHECK_LE(f->min_uar_stack_size_log, f->max_uar_stack_size_log);
161 CHECK_GE(f->redzone, 16);
162 CHECK_GE(f->max_redzone, f->redzone);
163 CHECK_LE(f->max_redzone, 2048);
164 CHECK(IsPowerOfTwo(f->redzone));
165 CHECK(IsPowerOfTwo(f->max_redzone));
166
167 // quarantine_size is deprecated but we still honor it.
168 // quarantine_size can not be used together with quarantine_size_mb.
169 if (f->quarantine_size >= 0 && f->quarantine_size_mb >= 0) {
170 Report("%s: please use either 'quarantine_size' (deprecated) or "
171 "quarantine_size_mb, but not both\n", SanitizerToolName);
172 Die();
173 }
174 if (f->quarantine_size >= 0)
175 f->quarantine_size_mb = f->quarantine_size >> 20;
176 if (f->quarantine_size_mb < 0) {
177 const int kDefaultQuarantineSizeMb =
7cac9316 178 (ASAN_LOW_MEMORY) ? 1UL << 4 : 1UL << 8;
92a42be0
SL
179 f->quarantine_size_mb = kDefaultQuarantineSizeMb;
180 }
7cac9316
XL
181 if (f->thread_local_quarantine_size_kb < 0) {
182 const u32 kDefaultThreadLocalQuarantineSizeKb =
183 // It is not advised to go lower than 64Kb, otherwise quarantine batches
184 // pushed from thread local quarantine to global one will create too
185 // much overhead. One quarantine batch size is 8Kb and it holds up to
186 // 1021 chunk, which amounts to 1/8 memory overhead per batch when
187 // thread local quarantine is set to 64Kb.
188 (ASAN_LOW_MEMORY) ? 1 << 6 : FIRST_32_SECOND_64(1 << 8, 1 << 10);
189 f->thread_local_quarantine_size_kb = kDefaultThreadLocalQuarantineSizeKb;
190 }
191 if (f->thread_local_quarantine_size_kb == 0 && f->quarantine_size_mb > 0) {
192 Report("%s: thread_local_quarantine_size_kb can be set to 0 only when "
193 "quarantine_size_mb is set to 0\n", SanitizerToolName);
194 Die();
195 }
5bcae85e
SL
196 if (!f->replace_str && common_flags()->intercept_strlen) {
197 Report("WARNING: strlen interceptor is enabled even though replace_str=0. "
198 "Use intercept_strlen=0 to disable it.");
199 }
200 if (!f->replace_str && common_flags()->intercept_strchr) {
201 Report("WARNING: strchr* interceptors are enabled even though "
202 "replace_str=0. Use intercept_strchr=0 to disable them.");
203 }
2c00a5a8
XL
204 if (!f->replace_str && common_flags()->intercept_strndup) {
205 Report("WARNING: strndup* interceptors are enabled even though "
206 "replace_str=0. Use intercept_strndup=0 to disable them.");
207 }
92a42be0
SL
208}
209
210} // namespace __asan
211
2c00a5a8
XL
212SANITIZER_INTERFACE_WEAK_DEF(const char*, __asan_default_options, void) {
213 return "";
214}