asan_flags.cpp 7.1 KB

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