• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- tsan_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_interface.h"
14 #include "tsan_test_util.h"
15 #include "gtest/gtest.h"
16 
foo()17 static void foo() {}
bar()18 static void bar() {}
19 
TEST(ThreadSanitizer,FuncCall)20 TEST(ThreadSanitizer, FuncCall) {
21   ScopedThread t1, t2;
22   MemLoc l;
23   t1.Write1(l);
24   t2.Call(foo);
25   t2.Call(bar);
26   t2.Write1(l, true);
27   t2.Return();
28   t2.Return();
29 }
30 
31 // We use this function instead of main, as ISO C++ forbids taking the address
32 // of main, which we need to pass inside __tsan_func_entry.
run_tests(int argc,char ** argv)33 int run_tests(int argc, char **argv) {
34   TestMutexBeforeInit();  // Mutexes must be usable before __tsan_init();
35   __tsan_init();
36   __tsan_func_entry(__builtin_return_address(0));
37   __tsan_func_entry((void*)((intptr_t)&run_tests + 1));
38 
39   testing::GTEST_FLAG(death_test_style) = "threadsafe";
40   testing::InitGoogleTest(&argc, argv);
41   int res = RUN_ALL_TESTS();
42 
43   __tsan_func_exit();
44   __tsan_func_exit();
45   return res;
46 }
47 
48 const char *argv0;
49 
50 #ifdef __APPLE__
51 // On Darwin, turns off symbolication and crash logs to make tests faster.
__tsan_default_options()52 extern "C" const char* __tsan_default_options() {
53   return "symbolize=false:abort_on_error=0";
54 }
55 #endif
56 
57 namespace __sanitizer {
ReexecDisabled()58 bool ReexecDisabled() {
59   return true;
60 }
61 }
62 
main(int argc,char ** argv)63 int main(int argc, char **argv) {
64   argv0 = argv[0];
65   return run_tests(argc, argv);
66 }
67