1 //===-- asan_flags.cc -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // ASan flag parsing logic.
13 //===----------------------------------------------------------------------===//
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"
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 = 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 OverrideCommonFlags(cf);
71 FlagParser asan_parser;
72 RegisterAsanFlags(&asan_parser, f);
73 RegisterCommonFlags(&asan_parser);
75 // Set the default values and prepare for parsing LSan flags (which can also
76 // overwrite common flags).
77 #if CAN_SANITIZE_LEAKS
78 __lsan::Flags *lf = __lsan::flags();
81 FlagParser lsan_parser;
82 __lsan::RegisterLsanFlags(&lsan_parser, lf);
83 RegisterCommonFlags(&lsan_parser);
86 // Override from ASan compile definition.
87 const char *asan_compile_def = MaybeUseAsanDefaultOptionsCompileDefinition();
88 asan_parser.ParseString(asan_compile_def);
90 // Override from user-specified string.
91 const char *asan_default_options = MaybeCallAsanDefaultOptions();
92 asan_parser.ParseString(asan_default_options);
94 // Override from command line.
95 asan_parser.ParseString(GetEnv("ASAN_OPTIONS"));
96 #if CAN_SANITIZE_LEAKS
97 lsan_parser.ParseString(GetEnv("LSAN_OPTIONS"));
100 // Let activation flags override current settings. On Android they come
101 // from a system property. On other platforms this is no-op.
102 if (!flags()->start_deactivated) {
104 GetExtraActivationFlags(buf, sizeof(buf));
105 asan_parser.ParseString(buf);
108 SetVerbosity(common_flags()->verbosity);
110 // TODO(eugenis): dump all flags at verbosity>=2?
111 if (Verbosity()) ReportUnrecognizedFlags();
113 if (common_flags()->help) {
114 // TODO(samsonov): print all of the flags (ASan, LSan, common).
115 asan_parser.PrintFlagDescriptions();
119 if (!CAN_SANITIZE_LEAKS && common_flags()->detect_leaks) {
120 Report("%s: detect_leaks is not supported on this platform.\n",
124 // Make "strict_init_order" imply "check_initialization_order".
125 // TODO(samsonov): Use a single runtime flag for an init-order checker.
126 if (f->strict_init_order) {
127 f->check_initialization_order = true;
129 CHECK_LE((uptr)common_flags()->malloc_context_size, kStackTraceMax);
130 CHECK_LE(f->min_uar_stack_size_log, f->max_uar_stack_size_log);
131 CHECK_GE(f->redzone, 16);
132 CHECK_GE(f->max_redzone, f->redzone);
133 CHECK_LE(f->max_redzone, 2048);
134 CHECK(IsPowerOfTwo(f->redzone));
135 CHECK(IsPowerOfTwo(f->max_redzone));
137 // quarantine_size is deprecated but we still honor it.
138 // quarantine_size can not be used together with quarantine_size_mb.
139 if (f->quarantine_size >= 0 && f->quarantine_size_mb >= 0) {
140 Report("%s: please use either 'quarantine_size' (deprecated) or "
141 "quarantine_size_mb, but not both\n", SanitizerToolName);
144 if (f->quarantine_size >= 0)
145 f->quarantine_size_mb = f->quarantine_size >> 20;
146 if (f->quarantine_size_mb < 0) {
147 const int kDefaultQuarantineSizeMb =
148 (ASAN_LOW_MEMORY) ? 1UL << 6 : 1UL << 8;
149 f->quarantine_size_mb = kDefaultQuarantineSizeMb;
153 } // namespace __asan
155 #if !SANITIZER_SUPPORTS_WEAK_HOOKS
157 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
158 const char* __asan_default_options() { return ""; }