15de29a4bSNico Weber //===-- tsan_test.cpp -----------------------------------------------------===//
25de29a4bSNico Weber //
35de29a4bSNico Weber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45de29a4bSNico Weber // See https://llvm.org/LICENSE.txt for license information.
55de29a4bSNico Weber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65de29a4bSNico Weber //
75de29a4bSNico Weber //===----------------------------------------------------------------------===//
85de29a4bSNico Weber //
95de29a4bSNico Weber // This file is a part of ThreadSanitizer (TSan), a race detector.
105de29a4bSNico Weber //
115de29a4bSNico Weber //===----------------------------------------------------------------------===//
125de29a4bSNico Weber #include "tsan_interface.h"
135de29a4bSNico Weber #include "tsan_test_util.h"
145de29a4bSNico Weber #include "gtest/gtest.h"
155de29a4bSNico Weber 
foo()165de29a4bSNico Weber static void foo() {}
bar()175de29a4bSNico Weber static void bar() {}
185de29a4bSNico Weber 
TEST_F(ThreadSanitizer,FuncCall)19*a61c8e1eSYuanfang Chen TEST_F(ThreadSanitizer, FuncCall) {
205de29a4bSNico Weber   ScopedThread t1, t2;
215de29a4bSNico Weber   MemLoc l;
225de29a4bSNico Weber   t1.Write1(l);
235de29a4bSNico Weber   t2.Call(foo);
245de29a4bSNico Weber   t2.Call(bar);
255de29a4bSNico Weber   t2.Write1(l, true);
265de29a4bSNico Weber   t2.Return();
275de29a4bSNico Weber   t2.Return();
285de29a4bSNico Weber }
295de29a4bSNico Weber 
305de29a4bSNico Weber // We use this function instead of main, as ISO C++ forbids taking the address
315de29a4bSNico Weber // of main, which we need to pass inside __tsan_func_entry.
run_tests(int argc,char ** argv)325de29a4bSNico Weber int run_tests(int argc, char **argv) {
335de29a4bSNico Weber   TestMutexBeforeInit();  // Mutexes must be usable before __tsan_init();
345de29a4bSNico Weber   __tsan_init();
355de29a4bSNico Weber   __tsan_func_entry(__builtin_return_address(0));
365de29a4bSNico Weber   __tsan_func_entry((void*)((intptr_t)&run_tests + 1));
375de29a4bSNico Weber 
385de29a4bSNico Weber   testing::GTEST_FLAG(death_test_style) = "threadsafe";
395de29a4bSNico Weber   testing::InitGoogleTest(&argc, argv);
405de29a4bSNico Weber   int res = RUN_ALL_TESTS();
415de29a4bSNico Weber 
425de29a4bSNico Weber   __tsan_func_exit();
435de29a4bSNico Weber   __tsan_func_exit();
445de29a4bSNico Weber   return res;
455de29a4bSNico Weber }
465de29a4bSNico Weber 
475de29a4bSNico Weber const char *argv0;
485de29a4bSNico Weber 
495de29a4bSNico Weber #ifdef __APPLE__
505de29a4bSNico Weber // On Darwin, turns off symbolication and crash logs to make tests faster.
__tsan_default_options()515de29a4bSNico Weber extern "C" const char* __tsan_default_options() {
525de29a4bSNico Weber   return "symbolize=false:abort_on_error=0";
535de29a4bSNico Weber }
545de29a4bSNico Weber #endif
555de29a4bSNico Weber 
main(int argc,char ** argv)565de29a4bSNico Weber int main(int argc, char **argv) {
575de29a4bSNico Weber   argv0 = argv[0];
585de29a4bSNico Weber   return run_tests(argc, argv);
595de29a4bSNico Weber }
60