]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/asan/asan_flags.cc
MFV r285970:
[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
24 namespace __asan {
25
26 Flags asan_flags_dont_use_directly;  // use via flags().
27
28 static const char *MaybeCallAsanDefaultOptions() {
29   return (&__asan_default_options) ? __asan_default_options() : "";
30 }
31
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);
38 #else
39   return "";
40 #endif
41 }
42
43 void Flags::SetDefaults() {
44 #define ASAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
45 #include "asan_flags.inc"
46 #undef ASAN_FLAG
47 }
48
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"
53 #undef ASAN_FLAG
54 }
55
56 void InitializeFlags() {
57   // Set the default values and prepare for parsing ASan and common flags.
58   SetCommonFlagsDefaults();
59   {
60     CommonFlags cf;
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);
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 flags (which can also
76   // 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   // Override from ASan compile definition.
87   const char *asan_compile_def = MaybeUseAsanDefaultOptionsCompileDefinition();
88   asan_parser.ParseString(asan_compile_def);
89
90   // Override from user-specified string.
91   const char *asan_default_options = MaybeCallAsanDefaultOptions();
92   asan_parser.ParseString(asan_default_options);
93
94   // Override from command line.
95   asan_parser.ParseString(GetEnv("ASAN_OPTIONS"));
96 #if CAN_SANITIZE_LEAKS
97   lsan_parser.ParseString(GetEnv("LSAN_OPTIONS"));
98 #endif
99
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) {
103     char buf[100];
104     GetExtraActivationFlags(buf, sizeof(buf));
105     asan_parser.ParseString(buf);
106   }
107
108   SetVerbosity(common_flags()->verbosity);
109
110   // TODO(eugenis): dump all flags at verbosity>=2?
111   if (Verbosity()) ReportUnrecognizedFlags();
112
113   if (common_flags()->help) {
114     // TODO(samsonov): print all of the flags (ASan, LSan, common).
115     asan_parser.PrintFlagDescriptions();
116   }
117
118   // Flag validation:
119   if (!CAN_SANITIZE_LEAKS && common_flags()->detect_leaks) {
120     Report("%s: detect_leaks is not supported on this platform.\n",
121            SanitizerToolName);
122     Die();
123   }
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;
128   }
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));
136
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);
142     Die();
143   }
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;
150   }
151 }
152
153 }  // namespace __asan
154
155 #if !SANITIZER_SUPPORTS_WEAK_HOOKS
156 extern "C" {
157 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
158 const char* __asan_default_options() { return ""; }
159 }  // extern "C"
160 #endif