1 //===-- asan_flags.cc -------------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // ASan flag parsing logic.
11 //===----------------------------------------------------------------------===//
12
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"
23
24 namespace __asan {
25
26 Flags asan_flags_dont_use_directly; // use via flags().
27
MaybeCallAsanDefaultOptions()28 static const char *MaybeCallAsanDefaultOptions() {
29 return (&__asan_default_options) ? __asan_default_options() : "";
30 }
31
MaybeUseAsanDefaultOptionsCompileDefinition()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);
38 #else
39 return "";
40 #endif
41 }
42
SetDefaults()43 void Flags::SetDefaults() {
44 #define ASAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
45 #include "asan_flags.inc"
46 #undef ASAN_FLAG
47 }
48
RegisterAsanFlags(FlagParser * parser,Flags * f)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"
53 #undef ASAN_FLAG
54 }
55
InitializeFlags()56 void InitializeFlags() {
57 // Set the default values and prepare for parsing ASan and common flags.
58 SetCommonFlagsDefaults();
59 {
60 CommonFlags cf;
61 cf.CopyFrom(*common_flags());
62 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;
66 cf.exitcode = 1;
67 OverrideCommonFlags(cf);
68 }
69 Flags *f = flags();
70 f->SetDefaults();
71
72 FlagParser asan_parser;
73 RegisterAsanFlags(&asan_parser, f);
74 RegisterCommonFlags(&asan_parser);
75
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();
80 lf->SetDefaults();
81
82 FlagParser lsan_parser;
83 __lsan::RegisterLsanFlags(&lsan_parser, lf);
84 RegisterCommonFlags(&lsan_parser);
85 #endif
86
87 #if CAN_SANITIZE_UB
88 __ubsan::Flags *uf = __ubsan::flags();
89 uf->SetDefaults();
90
91 FlagParser ubsan_parser;
92 __ubsan::RegisterUbsanFlags(&ubsan_parser, uf);
93 RegisterCommonFlags(&ubsan_parser);
94 #endif
95
96 // Override from ASan compile definition.
97 const char *asan_compile_def = MaybeUseAsanDefaultOptionsCompileDefinition();
98 asan_parser.ParseString(asan_compile_def);
99
100 // Override from user-specified string.
101 const char *asan_default_options = MaybeCallAsanDefaultOptions();
102 asan_parser.ParseString(asan_default_options);
103 #if CAN_SANITIZE_UB
104 const char *ubsan_default_options = __ubsan::MaybeCallUbsanDefaultOptions();
105 ubsan_parser.ParseString(ubsan_default_options);
106 #endif
107
108 // Override from command line.
109 asan_parser.ParseString(GetEnv("ASAN_OPTIONS"));
110 #if CAN_SANITIZE_LEAKS
111 lsan_parser.ParseString(GetEnv("LSAN_OPTIONS"));
112 #endif
113 #if CAN_SANITIZE_UB
114 ubsan_parser.ParseString(GetEnv("UBSAN_OPTIONS"));
115 #endif
116
117 // Let activation flags override current settings. On Android they come
118 // from a system property. On other platforms this is no-op.
119 if (!flags()->start_deactivated) {
120 char buf[100];
121 GetExtraActivationFlags(buf, sizeof(buf));
122 asan_parser.ParseString(buf);
123 }
124
125 SetVerbosity(common_flags()->verbosity);
126
127 // TODO(eugenis): dump all flags at verbosity>=2?
128 if (Verbosity()) ReportUnrecognizedFlags();
129
130 if (common_flags()->help) {
131 // TODO(samsonov): print all of the flags (ASan, LSan, common).
132 asan_parser.PrintFlagDescriptions();
133 }
134
135 // Flag validation:
136 if (!CAN_SANITIZE_LEAKS && common_flags()->detect_leaks) {
137 Report("%s: detect_leaks is not supported on this platform.\n",
138 SanitizerToolName);
139 Die();
140 }
141 // Make "strict_init_order" imply "check_initialization_order".
142 // TODO(samsonov): Use a single runtime flag for an init-order checker.
143 if (f->strict_init_order) {
144 f->check_initialization_order = true;
145 }
146 CHECK_LE((uptr)common_flags()->malloc_context_size, kStackTraceMax);
147 CHECK_LE(f->min_uar_stack_size_log, f->max_uar_stack_size_log);
148 CHECK_GE(f->redzone, 16);
149 CHECK_GE(f->max_redzone, f->redzone);
150 CHECK_LE(f->max_redzone, 2048);
151 CHECK(IsPowerOfTwo(f->redzone));
152 CHECK(IsPowerOfTwo(f->max_redzone));
153
154 // quarantine_size is deprecated but we still honor it.
155 // quarantine_size can not be used together with quarantine_size_mb.
156 if (f->quarantine_size >= 0 && f->quarantine_size_mb >= 0) {
157 Report("%s: please use either 'quarantine_size' (deprecated) or "
158 "quarantine_size_mb, but not both\n", SanitizerToolName);
159 Die();
160 }
161 if (f->quarantine_size >= 0)
162 f->quarantine_size_mb = f->quarantine_size >> 20;
163 if (f->quarantine_size_mb < 0) {
164 const int kDefaultQuarantineSizeMb =
165 (ASAN_LOW_MEMORY) ? 1UL << 6 : 1UL << 8;
166 f->quarantine_size_mb = kDefaultQuarantineSizeMb;
167 }
168 }
169
170 } // namespace __asan
171
172 #if !SANITIZER_SUPPORTS_WEAK_HOOKS
173 extern "C" {
174 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
__asan_default_options()175 const char* __asan_default_options() { return ""; }
176 } // extern "C"
177 #endif
178