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