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