xref: /freebsd/contrib/llvm-project/compiler-rt/lib/tsan/rtl/tsan_flags.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1 //===-- tsan_flags.cpp ----------------------------------------------------===//
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 ThreadSanitizer (TSan), a race detector.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "tsan_flags.h"
14 
15 #include "sanitizer_common/sanitizer_flag_parser.h"
16 #include "sanitizer_common/sanitizer_flags.h"
17 #include "sanitizer_common/sanitizer_libc.h"
18 #include "tsan_interface.h"
19 #include "tsan_mman.h"
20 #include "tsan_rtl.h"
21 #include "ubsan/ubsan_flags.h"
22 
23 namespace __tsan {
24 
25 // Can be overriden in frontend.
26 #ifdef TSAN_EXTERNAL_HOOKS
27 extern "C" const char *__tsan_default_options();
28 #else
29 SANITIZER_WEAK_DEFAULT_IMPL
30 const char *__tsan_default_options() {
31   return "";
32 }
33 #endif
34 
SetDefaults()35 void Flags::SetDefaults() {
36 #define TSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
37 #include "tsan_flags.inc"
38 #undef TSAN_FLAG
39   // DDFlags
40   second_deadlock_stack = false;
41 }
42 
RegisterTsanFlags(FlagParser * parser,Flags * f)43 void RegisterTsanFlags(FlagParser *parser, Flags *f) {
44 #define TSAN_FLAG(Type, Name, DefaultValue, Description) \
45   RegisterFlag(parser, #Name, Description, &f->Name);
46 #include "tsan_flags.inc"
47 #undef TSAN_FLAG
48   // DDFlags
49   RegisterFlag(parser, "second_deadlock_stack",
50       "Report where each mutex is locked in deadlock reports",
51       &f->second_deadlock_stack);
52 }
53 
InitializeFlags(Flags * f,const char * env,const char * env_option_name)54 void InitializeFlags(Flags *f, const char *env, const char *env_option_name) {
55   SetCommonFlagsDefaults();
56   {
57     // Override some common flags defaults.
58     CommonFlags cf;
59     cf.CopyFrom(*common_flags());
60     cf.external_symbolizer_path = GetEnv("TSAN_SYMBOLIZER_PATH");
61     cf.allow_addr2line = true;
62     if (SANITIZER_GO) {
63       // Does not work as expected for Go: runtime handles SIGABRT and crashes.
64       cf.abort_on_error = false;
65       // Go does not have mutexes.
66       cf.detect_deadlocks = false;
67     }
68     cf.print_suppressions = false;
69     cf.stack_trace_format = "    #%n %f %S %M";
70     cf.exitcode = 66;
71     cf.intercept_tls_get_addr = true;
72     OverrideCommonFlags(cf);
73   }
74 
75   f->SetDefaults();
76 
77   FlagParser parser;
78   RegisterTsanFlags(&parser, f);
79   RegisterCommonFlags(&parser);
80 
81 #if TSAN_CONTAINS_UBSAN
82   __ubsan::Flags *uf = __ubsan::flags();
83   uf->SetDefaults();
84 
85   FlagParser ubsan_parser;
86   __ubsan::RegisterUbsanFlags(&ubsan_parser, uf);
87   RegisterCommonFlags(&ubsan_parser);
88 #endif
89 
90   // Let a frontend override.
91   parser.ParseString(__tsan_default_options());
92 #if TSAN_CONTAINS_UBSAN
93   const char *ubsan_default_options = __ubsan_default_options();
94   ubsan_parser.ParseString(ubsan_default_options);
95 #endif
96   // Override from command line.
97   parser.ParseString(env, env_option_name);
98 #if TSAN_CONTAINS_UBSAN
99   ubsan_parser.ParseStringFromEnv("UBSAN_OPTIONS");
100 #endif
101 
102   // Check flags.
103   if (!f->report_bugs) {
104     f->report_thread_leaks = false;
105     f->report_destroy_locked = false;
106     f->report_signal_unsafe = false;
107   }
108 
109   InitializeCommonFlags();
110 
111   if (Verbosity()) ReportUnrecognizedFlags();
112 
113   if (common_flags()->help) parser.PrintFlagDescriptions();
114 
115   if (f->io_sync < 0 || f->io_sync > 2) {
116     Printf("ThreadSanitizer: incorrect value for io_sync"
117            " (must be [0..2])\n");
118     Die();
119   }
120 }
121 
122 }  // namespace __tsan
123