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