• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- tsan_flags_test.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 #include "tsan_flags.h"
14 #include "tsan_rtl.h"
15 #include "gtest/gtest.h"
16 #include <string>
17 
18 namespace __tsan {
19 
TEST(Flags,Basic)20 TEST(Flags, Basic) {
21   // At least should not crash.
22   Flags f;
23   InitializeFlags(&f, 0);
24   InitializeFlags(&f, "");
25 }
26 
TEST(Flags,DefaultValues)27 TEST(Flags, DefaultValues) {
28   Flags f;
29 
30   f.enable_annotations = false;
31   InitializeFlags(&f, "");
32   EXPECT_EQ(true, f.enable_annotations);
33 }
34 
35 static const char *options1 =
36   " enable_annotations=0"
37   " suppress_equal_stacks=0"
38   " suppress_equal_addresses=0"
39   " report_bugs=0"
40   " report_thread_leaks=0"
41   " report_destroy_locked=0"
42   " report_mutex_bugs=0"
43   " report_signal_unsafe=0"
44   " report_atomic_races=0"
45   " force_seq_cst_atomics=0"
46   " print_benign=0"
47   " halt_on_error=0"
48   " atexit_sleep_ms=222"
49   " profile_memory=qqq"
50   " flush_memory_ms=444"
51   " flush_symbolizer_ms=555"
52   " memory_limit_mb=666"
53   " stop_on_start=0"
54   " running_on_valgrind=0"
55   " history_size=5"
56   " io_sync=1"
57   " die_after_fork=true"
58   "";
59 
60 static const char *options2 =
61   " enable_annotations=true"
62   " suppress_equal_stacks=true"
63   " suppress_equal_addresses=true"
64   " report_bugs=true"
65   " report_thread_leaks=true"
66   " report_destroy_locked=true"
67   " report_mutex_bugs=true"
68   " report_signal_unsafe=true"
69   " report_atomic_races=true"
70   " force_seq_cst_atomics=true"
71   " print_benign=true"
72   " halt_on_error=true"
73   " atexit_sleep_ms=123"
74   " profile_memory=bbbbb"
75   " flush_memory_ms=234"
76   " flush_symbolizer_ms=345"
77   " memory_limit_mb=456"
78   " stop_on_start=true"
79   " running_on_valgrind=true"
80   " history_size=6"
81   " io_sync=2"
82   " die_after_fork=false"
83   "";
84 
VerifyOptions1(Flags * f)85 void VerifyOptions1(Flags *f) {
86   EXPECT_EQ(f->enable_annotations, 0);
87   EXPECT_EQ(f->suppress_equal_stacks, 0);
88   EXPECT_EQ(f->suppress_equal_addresses, 0);
89   EXPECT_EQ(f->report_bugs, 0);
90   EXPECT_EQ(f->report_thread_leaks, 0);
91   EXPECT_EQ(f->report_destroy_locked, 0);
92   EXPECT_EQ(f->report_mutex_bugs, 0);
93   EXPECT_EQ(f->report_signal_unsafe, 0);
94   EXPECT_EQ(f->report_atomic_races, 0);
95   EXPECT_EQ(f->force_seq_cst_atomics, 0);
96   EXPECT_EQ(f->print_benign, 0);
97   EXPECT_EQ(f->halt_on_error, 0);
98   EXPECT_EQ(f->atexit_sleep_ms, 222);
99   EXPECT_EQ(f->profile_memory, std::string("qqq"));
100   EXPECT_EQ(f->flush_memory_ms, 444);
101   EXPECT_EQ(f->flush_symbolizer_ms, 555);
102   EXPECT_EQ(f->memory_limit_mb, 666);
103   EXPECT_EQ(f->stop_on_start, 0);
104   EXPECT_EQ(f->running_on_valgrind, 0);
105   EXPECT_EQ(f->history_size, 5);
106   EXPECT_EQ(f->io_sync, 1);
107   EXPECT_EQ(f->die_after_fork, true);
108 }
109 
VerifyOptions2(Flags * f)110 void VerifyOptions2(Flags *f) {
111   EXPECT_EQ(f->enable_annotations, true);
112   EXPECT_EQ(f->suppress_equal_stacks, true);
113   EXPECT_EQ(f->suppress_equal_addresses, true);
114   EXPECT_EQ(f->report_bugs, true);
115   EXPECT_EQ(f->report_thread_leaks, true);
116   EXPECT_EQ(f->report_destroy_locked, true);
117   EXPECT_EQ(f->report_mutex_bugs, true);
118   EXPECT_EQ(f->report_signal_unsafe, true);
119   EXPECT_EQ(f->report_atomic_races, true);
120   EXPECT_EQ(f->force_seq_cst_atomics, true);
121   EXPECT_EQ(f->print_benign, true);
122   EXPECT_EQ(f->halt_on_error, true);
123   EXPECT_EQ(f->atexit_sleep_ms, 123);
124   EXPECT_EQ(f->profile_memory, std::string("bbbbb"));
125   EXPECT_EQ(f->flush_memory_ms, 234);
126   EXPECT_EQ(f->flush_symbolizer_ms, 345);
127   EXPECT_EQ(f->memory_limit_mb, 456);
128   EXPECT_EQ(f->stop_on_start, true);
129   EXPECT_EQ(f->running_on_valgrind, true);
130   EXPECT_EQ(f->history_size, 6);
131   EXPECT_EQ(f->io_sync, 2);
132   EXPECT_EQ(f->die_after_fork, false);
133 }
134 
135 static const char *test_default_options;
__tsan_default_options()136 extern "C" const char *__tsan_default_options() {
137   return test_default_options;
138 }
139 
TEST(Flags,ParseDefaultOptions)140 TEST(Flags, ParseDefaultOptions) {
141   Flags f;
142 
143   test_default_options = options1;
144   InitializeFlags(&f, "");
145   VerifyOptions1(&f);
146 
147   test_default_options = options2;
148   InitializeFlags(&f, "");
149   VerifyOptions2(&f);
150 }
151 
TEST(Flags,ParseEnvOptions)152 TEST(Flags, ParseEnvOptions) {
153   Flags f;
154 
155   InitializeFlags(&f, options1);
156   VerifyOptions1(&f);
157 
158   InitializeFlags(&f, options2);
159   VerifyOptions2(&f);
160 }
161 
TEST(Flags,ParsePriority)162 TEST(Flags, ParsePriority) {
163   Flags f;
164 
165   test_default_options = options2;
166   InitializeFlags(&f, options1);
167   VerifyOptions1(&f);
168 
169   test_default_options = options1;
170   InitializeFlags(&f, options2);
171   VerifyOptions2(&f);
172 }
173 
174 }  // namespace __tsan
175