[ASan] Update sanitizer_common and asan test_util headers to support building on...
[blocksruntime.git] / lib / sanitizer_common / tests / sanitizer_posix_test.cc
blob035899c830220eb908fd2f108d84db58589d824b
1 //===-- sanitizer_posix_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 // Tests for POSIX-specific code.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common/sanitizer_platform.h"
15 #if SANITIZER_POSIX
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "gtest/gtest.h"
20 #include <pthread.h>
22 namespace __sanitizer {
24 static pthread_key_t key;
25 static bool destructor_executed;
27 extern "C"
28 void destructor(void *arg) {
29 uptr iter = reinterpret_cast<uptr>(arg);
30 if (iter > 1) {
31 ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void *>(iter - 1)));
32 return;
34 destructor_executed = true;
37 extern "C"
38 void *thread_func(void *arg) {
39 return reinterpret_cast<void*>(pthread_setspecific(key, arg));
42 static void SpawnThread(uptr iteration) {
43 destructor_executed = false;
44 pthread_t tid;
45 ASSERT_EQ(0, pthread_create(&tid, 0, &thread_func,
46 reinterpret_cast<void *>(iteration)));
47 void *retval;
48 ASSERT_EQ(0, pthread_join(tid, &retval));
49 ASSERT_EQ(0, retval);
52 TEST(SanitizerCommon, PthreadDestructorIterations) {
53 ASSERT_EQ(0, pthread_key_create(&key, &destructor));
54 SpawnThread(kPthreadDestructorIterations);
55 EXPECT_TRUE(destructor_executed);
56 SpawnThread(kPthreadDestructorIterations + 1);
57 EXPECT_FALSE(destructor_executed);
60 } // namespace __sanitizer
62 #endif // SANITIZER_POSIX