1 //===-- asan_flags.cc -------------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // ASan flag parsing logic.
11 //===----------------------------------------------------------------------===//
13 #include "asan_activation.h"
14 #include "asan_flags.h"
15 #include "asan_interface_internal.h"
16 #include "asan_stack.h"
17 #include "lsan/lsan_common.h"
18 #include "sanitizer_common/sanitizer_common.h"
19 #include "sanitizer_common/sanitizer_flags.h"
20 #include "sanitizer_common/sanitizer_flag_parser.h"
21 #include "ubsan/ubsan_flags.h"
22 #include "ubsan/ubsan_platform.h"
26 Flags asan_flags_dont_use_directly
; // use via flags().
28 static const char *MaybeCallAsanDefaultOptions() {
29 return (&__asan_default_options
) ? __asan_default_options() : "";
32 static const char *MaybeUseAsanDefaultOptionsCompileDefinition() {
33 #ifdef ASAN_DEFAULT_OPTIONS
34 // Stringize the macro value.
35 # define ASAN_STRINGIZE(x) #x
36 # define ASAN_STRINGIZE_OPTIONS(options) ASAN_STRINGIZE(options)
37 return ASAN_STRINGIZE_OPTIONS(ASAN_DEFAULT_OPTIONS
);
43 void Flags::SetDefaults() {
44 #define ASAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
45 #include "asan_flags.inc"
49 static void RegisterAsanFlags(FlagParser
*parser
, Flags
*f
) {
50 #define ASAN_FLAG(Type, Name, DefaultValue, Description) \
51 RegisterFlag(parser, #Name, Description, &f->Name);
52 #include "asan_flags.inc"
56 void InitializeFlags() {
57 // Set the default values and prepare for parsing ASan and common flags.
58 SetCommonFlagsDefaults();
61 cf
.CopyFrom(*common_flags());
62 cf
.detect_leaks
= cf
.detect_leaks
&& CAN_SANITIZE_LEAKS
;
63 cf
.external_symbolizer_path
= GetEnv("ASAN_SYMBOLIZER_PATH");
64 cf
.malloc_context_size
= kDefaultMallocContextSize
;
65 cf
.intercept_tls_get_addr
= true;
67 OverrideCommonFlags(cf
);
72 FlagParser asan_parser
;
73 RegisterAsanFlags(&asan_parser
, f
);
74 RegisterCommonFlags(&asan_parser
);
76 // Set the default values and prepare for parsing LSan and UBSan flags
77 // (which can also overwrite common flags).
78 #if CAN_SANITIZE_LEAKS
79 __lsan::Flags
*lf
= __lsan::flags();
82 FlagParser lsan_parser
;
83 __lsan::RegisterLsanFlags(&lsan_parser
, lf
);
84 RegisterCommonFlags(&lsan_parser
);
88 __ubsan::Flags
*uf
= __ubsan::flags();
91 FlagParser ubsan_parser
;
92 __ubsan::RegisterUbsanFlags(&ubsan_parser
, uf
);
93 RegisterCommonFlags(&ubsan_parser
);
97 // Support macOS MallocScribble and MallocPreScribble:
98 // <https://developer.apple.com/library/content/documentation/Performance/
99 // Conceptual/ManagingMemory/Articles/MallocDebug.html>
100 if (GetEnv("MallocScribble")) {
101 f
->max_free_fill_size
= 0x1000;
103 if (GetEnv("MallocPreScribble")) {
104 f
->malloc_fill_byte
= 0xaa;
108 // Override from ASan compile definition.
109 const char *asan_compile_def
= MaybeUseAsanDefaultOptionsCompileDefinition();
110 asan_parser
.ParseString(asan_compile_def
);
112 // Override from user-specified string.
113 const char *asan_default_options
= MaybeCallAsanDefaultOptions();
114 asan_parser
.ParseString(asan_default_options
);
116 const char *ubsan_default_options
= __ubsan::MaybeCallUbsanDefaultOptions();
117 ubsan_parser
.ParseString(ubsan_default_options
);
119 #if CAN_SANITIZE_LEAKS
120 const char *lsan_default_options
= __lsan::MaybeCallLsanDefaultOptions();
121 lsan_parser
.ParseString(lsan_default_options
);
124 // Override from command line.
125 asan_parser
.ParseString(GetEnv("ASAN_OPTIONS"));
126 #if CAN_SANITIZE_LEAKS
127 lsan_parser
.ParseString(GetEnv("LSAN_OPTIONS"));
130 ubsan_parser
.ParseString(GetEnv("UBSAN_OPTIONS"));
133 InitializeCommonFlags();
135 // TODO(eugenis): dump all flags at verbosity>=2?
136 if (Verbosity()) ReportUnrecognizedFlags();
138 if (common_flags()->help
) {
139 // TODO(samsonov): print all of the flags (ASan, LSan, common).
140 asan_parser
.PrintFlagDescriptions();
144 if (!CAN_SANITIZE_LEAKS
&& common_flags()->detect_leaks
) {
145 Report("%s: detect_leaks is not supported on this platform.\n",
149 // Make "strict_init_order" imply "check_initialization_order".
150 // TODO(samsonov): Use a single runtime flag for an init-order checker.
151 if (f
->strict_init_order
) {
152 f
->check_initialization_order
= true;
154 CHECK_LE((uptr
)common_flags()->malloc_context_size
, kStackTraceMax
);
155 CHECK_LE(f
->min_uar_stack_size_log
, f
->max_uar_stack_size_log
);
156 CHECK_GE(f
->redzone
, 16);
157 CHECK_GE(f
->max_redzone
, f
->redzone
);
158 CHECK_LE(f
->max_redzone
, 2048);
159 CHECK(IsPowerOfTwo(f
->redzone
));
160 CHECK(IsPowerOfTwo(f
->max_redzone
));
162 // quarantine_size is deprecated but we still honor it.
163 // quarantine_size can not be used together with quarantine_size_mb.
164 if (f
->quarantine_size
>= 0 && f
->quarantine_size_mb
>= 0) {
165 Report("%s: please use either 'quarantine_size' (deprecated) or "
166 "quarantine_size_mb, but not both\n", SanitizerToolName
);
169 if (f
->quarantine_size
>= 0)
170 f
->quarantine_size_mb
= f
->quarantine_size
>> 20;
171 if (f
->quarantine_size_mb
< 0) {
172 const int kDefaultQuarantineSizeMb
=
173 (ASAN_LOW_MEMORY
) ? 1UL << 4 : 1UL << 8;
174 f
->quarantine_size_mb
= kDefaultQuarantineSizeMb
;
176 if (f
->thread_local_quarantine_size_kb
< 0) {
177 const u32 kDefaultThreadLocalQuarantineSizeKb
=
178 // It is not advised to go lower than 64Kb, otherwise quarantine batches
179 // pushed from thread local quarantine to global one will create too
180 // much overhead. One quarantine batch size is 8Kb and it holds up to
181 // 1021 chunk, which amounts to 1/8 memory overhead per batch when
182 // thread local quarantine is set to 64Kb.
183 (ASAN_LOW_MEMORY
) ? 1 << 6 : FIRST_32_SECOND_64(1 << 8, 1 << 10);
184 f
->thread_local_quarantine_size_kb
= kDefaultThreadLocalQuarantineSizeKb
;
186 if (f
->thread_local_quarantine_size_kb
== 0 && f
->quarantine_size_mb
> 0) {
187 Report("%s: thread_local_quarantine_size_kb can be set to 0 only when "
188 "quarantine_size_mb is set to 0\n", SanitizerToolName
);
191 if (!f
->replace_str
&& common_flags()->intercept_strlen
) {
192 Report("WARNING: strlen interceptor is enabled even though replace_str=0. "
193 "Use intercept_strlen=0 to disable it.");
195 if (!f
->replace_str
&& common_flags()->intercept_strchr
) {
196 Report("WARNING: strchr* interceptors are enabled even though "
197 "replace_str=0. Use intercept_strchr=0 to disable them.");
199 if (!f
->replace_str
&& common_flags()->intercept_strndup
) {
200 Report("WARNING: strndup* interceptors are enabled even though "
201 "replace_str=0. Use intercept_strndup=0 to disable them.");
205 } // namespace __asan
207 SANITIZER_INTERFACE_WEAK_DEF(const char*, __asan_default_options
, void) {