1 //===-- tsan_flags.cc -----------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
9 //
10 //===----------------------------------------------------------------------===//
11 
12 #include "sanitizer_common/sanitizer_flags.h"
13 #include "sanitizer_common/sanitizer_libc.h"
14 #include "tsan_flags.h"
15 #include "tsan_rtl.h"
16 #include "tsan_mman.h"
17 
18 namespace __tsan {
19 
flags()20 Flags *flags() {
21   return &CTX()->flags;
22 }
23 
24 // Can be overriden in frontend.
25 #ifdef TSAN_EXTERNAL_HOOKS
26 void OverrideFlags(Flags *f);
27 extern "C" const char* __tsan_default_options();
28 #else
OverrideFlags(Flags * f)29 void WEAK OverrideFlags(Flags *f) {
30   (void)f;
31 }
32 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
__tsan_default_options()33 const char *WEAK __tsan_default_options() {
34   return "";
35 }
36 #endif
37 
ParseFlags(Flags * f,const char * env)38 static void ParseFlags(Flags *f, const char *env) {
39   ParseFlag(env, &f->enable_annotations, "enable_annotations");
40   ParseFlag(env, &f->suppress_equal_stacks, "suppress_equal_stacks");
41   ParseFlag(env, &f->suppress_equal_addresses, "suppress_equal_addresses");
42   ParseFlag(env, &f->suppress_java, "suppress_java");
43   ParseFlag(env, &f->report_bugs, "report_bugs");
44   ParseFlag(env, &f->report_thread_leaks, "report_thread_leaks");
45   ParseFlag(env, &f->report_destroy_locked, "report_destroy_locked");
46   ParseFlag(env, &f->report_signal_unsafe, "report_signal_unsafe");
47   ParseFlag(env, &f->report_atomic_races, "report_atomic_races");
48   ParseFlag(env, &f->force_seq_cst_atomics, "force_seq_cst_atomics");
49   ParseFlag(env, &f->suppressions, "suppressions");
50   ParseFlag(env, &f->print_suppressions, "print_suppressions");
51   ParseFlag(env, &f->print_benign, "print_benign");
52   ParseFlag(env, &f->exitcode, "exitcode");
53   ParseFlag(env, &f->halt_on_error, "halt_on_error");
54   ParseFlag(env, &f->atexit_sleep_ms, "atexit_sleep_ms");
55   ParseFlag(env, &f->profile_memory, "profile_memory");
56   ParseFlag(env, &f->flush_memory_ms, "flush_memory_ms");
57   ParseFlag(env, &f->flush_symbolizer_ms, "flush_symbolizer_ms");
58   ParseFlag(env, &f->memory_limit_mb, "memory_limit_mb");
59   ParseFlag(env, &f->stop_on_start, "stop_on_start");
60   ParseFlag(env, &f->running_on_valgrind, "running_on_valgrind");
61   ParseFlag(env, &f->history_size, "history_size");
62   ParseFlag(env, &f->io_sync, "io_sync");
63 }
64 
InitializeFlags(Flags * f,const char * env)65 void InitializeFlags(Flags *f, const char *env) {
66   internal_memset(f, 0, sizeof(*f));
67 
68   // Default values.
69   f->enable_annotations = true;
70   f->suppress_equal_stacks = true;
71   f->suppress_equal_addresses = true;
72   f->suppress_java = false;
73   f->report_bugs = true;
74   f->report_thread_leaks = true;
75   f->report_destroy_locked = true;
76   f->report_signal_unsafe = true;
77   f->report_atomic_races = true;
78   f->force_seq_cst_atomics = false;
79   f->suppressions = "";
80   f->print_suppressions = false;
81   f->print_benign = false;
82   f->exitcode = 66;
83   f->halt_on_error = false;
84   f->atexit_sleep_ms = 1000;
85   f->profile_memory = "";
86   f->flush_memory_ms = 0;
87   f->flush_symbolizer_ms = 5000;
88   f->memory_limit_mb = 0;
89   f->stop_on_start = false;
90   f->running_on_valgrind = false;
91   f->history_size = kGoMode ? 1 : 2;  // There are a lot of goroutines in Go.
92   f->io_sync = 1;
93 
94   SetCommonFlagsDefaults(f);
95 
96   // Let a frontend override.
97   OverrideFlags(f);
98   ParseFlags(f, __tsan_default_options());
99   ParseCommonFlagsFromString(f, __tsan_default_options());
100   // Override from command line.
101   ParseFlags(f, env);
102   ParseCommonFlagsFromString(f, env);
103 
104   // Copy back to common flags.
105   *common_flags() = *f;
106 
107   // Sanity check.
108   if (!f->report_bugs) {
109     f->report_thread_leaks = false;
110     f->report_destroy_locked = false;
111     f->report_signal_unsafe = false;
112   }
113 
114   if (f->history_size < 0 || f->history_size > 7) {
115     Printf("ThreadSanitizer: incorrect value for history_size"
116            " (must be [0..7])\n");
117     Die();
118   }
119 
120   if (f->io_sync < 0 || f->io_sync > 2) {
121     Printf("ThreadSanitizer: incorrect value for io_sync"
122            " (must be [0..2])\n");
123     Die();
124   }
125 }
126 
127 }  // namespace __tsan
128