xref: /freebsd/contrib/llvm-project/compiler-rt/lib/asan/asan_flags.cpp (revision 5b27928474e6a4103d65b347544705c40c9618fd)
1 //===-- asan_flags.cpp ------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of AddressSanitizer, an address sanity checker.
10 //
11 // ASan flag parsing logic.
12 //===----------------------------------------------------------------------===//
13 
14 #include "asan_activation.h"
15 #include "asan_flags.h"
16 #include "asan_interface_internal.h"
17 #include "asan_stack.h"
18 #include "lsan/lsan_common.h"
19 #include "sanitizer_common/sanitizer_common.h"
20 #include "sanitizer_common/sanitizer_flags.h"
21 #include "sanitizer_common/sanitizer_flag_parser.h"
22 #include "ubsan/ubsan_flags.h"
23 #include "ubsan/ubsan_platform.h"
24 
25 namespace __asan {
26 
27 Flags asan_flags_dont_use_directly;  // use via flags().
28 
29 static const char *MaybeCallAsanDefaultOptions() {
30   return (&__asan_default_options) ? __asan_default_options() : "";
31 }
32 
33 static const char *MaybeUseAsanDefaultOptionsCompileDefinition() {
34 #ifdef ASAN_DEFAULT_OPTIONS
35   return SANITIZER_STRINGIFY(ASAN_DEFAULT_OPTIONS);
36 #else
37   return "";
38 #endif
39 }
40 
41 void Flags::SetDefaults() {
42 #define ASAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
43 #include "asan_flags.inc"
44 #undef ASAN_FLAG
45 }
46 
47 static void RegisterAsanFlags(FlagParser *parser, Flags *f) {
48 #define ASAN_FLAG(Type, Name, DefaultValue, Description) \
49   RegisterFlag(parser, #Name, Description, &f->Name);
50 #include "asan_flags.inc"
51 #undef ASAN_FLAG
52 }
53 
54 void InitializeFlags() {
55   // Set the default values and prepare for parsing ASan and common flags.
56   SetCommonFlagsDefaults();
57   {
58     CommonFlags cf;
59     cf.CopyFrom(*common_flags());
60     cf.detect_leaks = cf.detect_leaks && CAN_SANITIZE_LEAKS;
61     cf.external_symbolizer_path = GetEnv("ASAN_SYMBOLIZER_PATH");
62     cf.malloc_context_size = kDefaultMallocContextSize;
63     cf.intercept_tls_get_addr = true;
64     cf.exitcode = 1;
65     OverrideCommonFlags(cf);
66   }
67   Flags *f = flags();
68   f->SetDefaults();
69 
70   FlagParser asan_parser;
71   RegisterAsanFlags(&asan_parser, f);
72   RegisterCommonFlags(&asan_parser);
73 
74   // Set the default values and prepare for parsing LSan and UBSan flags
75   // (which can also overwrite common flags).
76 #if CAN_SANITIZE_LEAKS
77   __lsan::Flags *lf = __lsan::flags();
78   lf->SetDefaults();
79 
80   FlagParser lsan_parser;
81   __lsan::RegisterLsanFlags(&lsan_parser, lf);
82   RegisterCommonFlags(&lsan_parser);
83 #endif
84 
85 #if CAN_SANITIZE_UB
86   __ubsan::Flags *uf = __ubsan::flags();
87   uf->SetDefaults();
88 
89   FlagParser ubsan_parser;
90   __ubsan::RegisterUbsanFlags(&ubsan_parser, uf);
91   RegisterCommonFlags(&ubsan_parser);
92 #endif
93 
94   if (SANITIZER_MAC) {
95     // Support macOS MallocScribble and MallocPreScribble:
96     // <https://developer.apple.com/library/content/documentation/Performance/
97     // Conceptual/ManagingMemory/Articles/MallocDebug.html>
98     if (GetEnv("MallocScribble")) {
99       f->max_free_fill_size = 0x1000;
100     }
101     if (GetEnv("MallocPreScribble")) {
102       f->malloc_fill_byte = 0xaa;
103     }
104   }
105 
106   // Override from ASan compile definition.
107   const char *asan_compile_def = MaybeUseAsanDefaultOptionsCompileDefinition();
108   asan_parser.ParseString(asan_compile_def);
109 
110   // Override from user-specified string.
111   const char *asan_default_options = MaybeCallAsanDefaultOptions();
112   asan_parser.ParseString(asan_default_options);
113 #if CAN_SANITIZE_UB
114   const char *ubsan_default_options = __ubsan::MaybeCallUbsanDefaultOptions();
115   ubsan_parser.ParseString(ubsan_default_options);
116 #endif
117 #if CAN_SANITIZE_LEAKS
118   const char *lsan_default_options = __lsan::MaybeCallLsanDefaultOptions();
119   lsan_parser.ParseString(lsan_default_options);
120 #endif
121 
122   // Override from command line.
123   asan_parser.ParseStringFromEnv("ASAN_OPTIONS");
124 #if CAN_SANITIZE_LEAKS
125   lsan_parser.ParseStringFromEnv("LSAN_OPTIONS");
126 #endif
127 #if CAN_SANITIZE_UB
128   ubsan_parser.ParseStringFromEnv("UBSAN_OPTIONS");
129 #endif
130 
131   InitializeCommonFlags();
132 
133   // TODO(eugenis): dump all flags at verbosity>=2?
134   if (Verbosity()) ReportUnrecognizedFlags();
135 
136   if (common_flags()->help) {
137     // TODO(samsonov): print all of the flags (ASan, LSan, common).
138     asan_parser.PrintFlagDescriptions();
139   }
140 
141   // Flag validation:
142   if (!CAN_SANITIZE_LEAKS && common_flags()->detect_leaks) {
143     Report("%s: detect_leaks is not supported on this platform.\n",
144            SanitizerToolName);
145     Die();
146   }
147   // Ensure that redzone is at least SHADOW_GRANULARITY.
148   if (f->redzone < (int)SHADOW_GRANULARITY)
149     f->redzone = SHADOW_GRANULARITY;
150   // Make "strict_init_order" imply "check_initialization_order".
151   // TODO(samsonov): Use a single runtime flag for an init-order checker.
152   if (f->strict_init_order) {
153     f->check_initialization_order = true;
154   }
155   CHECK_LE((uptr)common_flags()->malloc_context_size, kStackTraceMax);
156   CHECK_LE(f->min_uar_stack_size_log, f->max_uar_stack_size_log);
157   CHECK_GE(f->redzone, 16);
158   CHECK_GE(f->max_redzone, f->redzone);
159   CHECK_LE(f->max_redzone, 2048);
160   CHECK(IsPowerOfTwo(f->redzone));
161   CHECK(IsPowerOfTwo(f->max_redzone));
162   if (SANITIZER_RTEMS) {
163     CHECK(!f->unmap_shadow_on_exit);
164     CHECK(!f->protect_shadow_gap);
165   }
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 =
178         (ASAN_LOW_MEMORY) ? 1UL << 4 : 1UL << 8;
179     f->quarantine_size_mb = kDefaultQuarantineSizeMb;
180   }
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   }
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   }
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   }
208 }
209 
210 }  // namespace __asan
211 
212 SANITIZER_INTERFACE_WEAK_DEF(const char*, __asan_default_options, void) {
213   return "";
214 }
215