Home | History | Annotate | Download | only in tests
      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.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "sanitizer_common/sanitizer_platform.h"
     15 #if SANITIZER_POSIX
     16 
     17 #include "sanitizer_common/sanitizer_common.h"
     18 #include "gtest/gtest.h"
     19 
     20 #include <pthread.h>
     21 
     22 namespace __sanitizer {
     23 
     24 static pthread_key_t key;
     25 static bool destructor_executed;
     26 
     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;
     33   }
     34   destructor_executed = true;
     35 }
     36 
     37 extern "C"
     38 void *thread_func(void *arg) {
     39   return reinterpret_cast<void*>(pthread_setspecific(key, arg));
     40 }
     41 
     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);
     50 }
     51 
     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);
     58 }
     59 
     60 }  // namespace __sanitizer
     61 
     62 #endif  // SANITIZER_POSIX
     63