11
11
// ASan flag parsing logic.
12
12
// ===----------------------------------------------------------------------===//
13
13
14
- #include " asan_activation.h"
15
14
#include " asan_flags.h"
15
+
16
+ #include " asan_activation.h"
16
17
#include " asan_interface_internal.h"
17
18
#include " asan_stack.h"
18
19
#include " lsan/lsan_common.h"
19
20
#include " sanitizer_common/sanitizer_common.h"
20
- #include " sanitizer_common/sanitizer_flags.h"
21
21
#include " sanitizer_common/sanitizer_flag_parser.h"
22
+ #include " sanitizer_common/sanitizer_flags.h"
23
+ #include " sanitizer_common/sanitizer_win_interception.h"
22
24
#include " ubsan/ubsan_flags.h"
23
25
#include " ubsan/ubsan_platform.h"
24
26
@@ -47,7 +49,21 @@ static void RegisterAsanFlags(FlagParser *parser, Flags *f) {
47
49
#undef ASAN_FLAG
48
50
}
49
51
50
- void InitializeFlags () {
52
+ static void DisplayHelpMessages (FlagParser *parser) {
53
+ // TODO(eugenis): dump all flags at verbosity>=2?
54
+ if (Verbosity ()) {
55
+ ReportUnrecognizedFlags ();
56
+ }
57
+
58
+ if (common_flags ()->help ) {
59
+ parser->PrintFlagDescriptions ();
60
+ }
61
+ }
62
+
63
+ static void InitializeDefaultFlags () {
64
+ Flags *f = flags ();
65
+ FlagParser asan_parser;
66
+
51
67
// Set the default values and prepare for parsing ASan and common flags.
52
68
SetCommonFlagsDefaults ();
53
69
{
@@ -60,10 +76,8 @@ void InitializeFlags() {
60
76
cf.exitcode = 1 ;
61
77
OverrideCommonFlags (cf);
62
78
}
63
- Flags *f = flags ();
64
79
f->SetDefaults ();
65
80
66
- FlagParser asan_parser;
67
81
RegisterAsanFlags (&asan_parser, f);
68
82
RegisterCommonFlags (&asan_parser);
69
83
@@ -126,13 +140,12 @@ void InitializeFlags() {
126
140
127
141
InitializeCommonFlags ();
128
142
129
- // TODO(eugenis): dump all flags at verbosity>=2?
130
- if (Verbosity ()) ReportUnrecognizedFlags ();
143
+ // TODO(samsonov): print all of the flags (ASan, LSan, common).
144
+ DisplayHelpMessages (&asan_parser);
145
+ }
131
146
132
- if (common_flags ()->help ) {
133
- // TODO(samsonov): print all of the flags (ASan, LSan, common).
134
- asan_parser.PrintFlagDescriptions ();
135
- }
147
+ static void ProcessFlags () {
148
+ Flags *f = flags ();
136
149
137
150
// Flag validation:
138
151
if (!CAN_SANITIZE_LEAKS && common_flags ()->detect_leaks ) {
@@ -199,6 +212,67 @@ void InitializeFlags() {
199
212
}
200
213
}
201
214
215
+ void InitializeFlags () {
216
+ InitializeDefaultFlags ();
217
+ ProcessFlags ();
218
+
219
+ #if SANITIZER_WINDOWS
220
+ // On Windows, weak symbols are emulated by having the user program
221
+ // register which weak functions are defined.
222
+ // The ASAN DLL will initialize flags prior to user module initialization,
223
+ // so __asan_default_options will not point to the user definition yet.
224
+ // We still want to ensure we capture when options are passed via
225
+ // __asan_default_options, so we add a callback to be run
226
+ // when it is registered with the runtime.
227
+
228
+ // There is theoretically time between the initial ProcessFlags and
229
+ // registering the weak callback where a weak function could be added and we
230
+ // would miss it, but in practice, InitializeFlags will always happen under
231
+ // the loader lock (if built as a DLL) and so will any calls to
232
+ // __sanitizer_register_weak_function.
233
+ AddRegisterWeakFunctionCallback (
234
+ reinterpret_cast <uptr>(__asan_default_options), []() {
235
+ FlagParser asan_parser;
236
+
237
+ RegisterAsanFlags (&asan_parser, flags ());
238
+ RegisterCommonFlags (&asan_parser);
239
+ asan_parser.ParseString (__asan_default_options ());
240
+
241
+ DisplayHelpMessages (&asan_parser);
242
+ ProcessFlags ();
243
+ });
244
+
245
+ # if CAN_SANITIZE_UB
246
+ AddRegisterWeakFunctionCallback (
247
+ reinterpret_cast <uptr>(__ubsan_default_options), []() {
248
+ FlagParser ubsan_parser;
249
+
250
+ __ubsan::RegisterUbsanFlags (&ubsan_parser, __ubsan::flags ());
251
+ RegisterCommonFlags (&ubsan_parser);
252
+ ubsan_parser.ParseString (__ubsan_default_options ());
253
+
254
+ // To match normal behavior, do not print UBSan help.
255
+ ProcessFlags ();
256
+ });
257
+ # endif
258
+
259
+ # if CAN_SANITIZE_LEAKS
260
+ AddRegisterWeakFunctionCallback (
261
+ reinterpret_cast <uptr>(__lsan_default_options), []() {
262
+ FlagParser lsan_parser;
263
+
264
+ __lsan::RegisterLsanFlags (&lsan_parser, __lsan::flags ());
265
+ RegisterCommonFlags (&lsan_parser);
266
+ lsan_parser.ParseString (__lsan_default_options ());
267
+
268
+ // To match normal behavior, do not print LSan help.
269
+ ProcessFlags ();
270
+ });
271
+ # endif
272
+
273
+ #endif
274
+ }
275
+
202
276
} // namespace __asan
203
277
204
278
SANITIZER_INTERFACE_WEAK_DEF (const char *, __asan_default_options, void ) {
0 commit comments