1 //===-- sanitizer_posix_test.cc -------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Tests for POSIX-specific code.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common/sanitizer_platform.h"
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "gtest/gtest.h"
23 namespace __sanitizer
{
25 static pthread_key_t key
;
26 static bool destructor_executed
;
29 void destructor(void *arg
) {
30 uptr iter
= reinterpret_cast<uptr
>(arg
);
32 ASSERT_EQ(0, pthread_setspecific(key
, reinterpret_cast<void *>(iter
- 1)));
35 destructor_executed
= true;
39 void *thread_func(void *arg
) {
40 return reinterpret_cast<void*>(pthread_setspecific(key
, arg
));
43 static void SpawnThread(uptr iteration
) {
44 destructor_executed
= false;
46 ASSERT_EQ(0, pthread_create(&tid
, 0, &thread_func
,
47 reinterpret_cast<void *>(iteration
)));
49 ASSERT_EQ(0, pthread_join(tid
, &retval
));
53 TEST(SanitizerCommon
, PthreadDestructorIterations
) {
54 ASSERT_EQ(0, pthread_key_create(&key
, &destructor
));
55 SpawnThread(GetPthreadDestructorIterations());
56 EXPECT_TRUE(destructor_executed
);
57 SpawnThread(GetPthreadDestructorIterations() + 1);
58 EXPECT_FALSE(destructor_executed
);
59 ASSERT_EQ(0, pthread_key_delete(key
));
62 TEST(SanitizerCommon
, IsAccessibleMemoryRange
) {
63 const int page_size
= GetPageSize();
64 uptr mem
= (uptr
)mmap(0, 3 * page_size
, PROT_READ
| PROT_WRITE
,
65 MAP_PRIVATE
| MAP_ANON
, -1, 0);
66 // Protect the middle page.
67 mprotect((void *)(mem
+ page_size
), page_size
, PROT_NONE
);
68 EXPECT_TRUE(IsAccessibleMemoryRange(mem
, page_size
- 1));
69 EXPECT_TRUE(IsAccessibleMemoryRange(mem
, page_size
));
70 EXPECT_FALSE(IsAccessibleMemoryRange(mem
, page_size
+ 1));
71 EXPECT_TRUE(IsAccessibleMemoryRange(mem
+ page_size
- 1, 1));
72 EXPECT_FALSE(IsAccessibleMemoryRange(mem
+ page_size
- 1, 2));
73 EXPECT_FALSE(IsAccessibleMemoryRange(mem
+ 2 * page_size
- 1, 1));
74 EXPECT_TRUE(IsAccessibleMemoryRange(mem
+ 2 * page_size
, page_size
));
75 EXPECT_FALSE(IsAccessibleMemoryRange(mem
, 3 * page_size
));
76 EXPECT_FALSE(IsAccessibleMemoryRange(0x0, 2));
79 } // namespace __sanitizer
81 #endif // SANITIZER_POSIX