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