Skip to content

Commit

Permalink
[asan] in asan tests, check all return values of pthread_create/pthre…
Browse files Browse the repository at this point in the history
…ad_join. Also add the ASAN_AVOID_EXPENSIVE_TESTS macro to guard the test that creates too many threads

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@169118 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
kcc committed Dec 3, 2012
1 parent a82a5d3 commit 2697687
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 44 deletions.
12 changes: 6 additions & 6 deletions lib/asan/tests/asan_noinst_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ TEST(AddressSanitizer, ThreadedQuarantineTest) {
size_t mmaped1 = __asan_get_heap_size();
for (int i = 0; i < n_threads; i++) {
pthread_t t;
pthread_create(&t, NULL, ThreadedQuarantineTestWorker, 0);
pthread_join(t, 0);
PTHREAD_CREATE(&t, NULL, ThreadedQuarantineTestWorker, 0);
PTHREAD_JOIN(t, 0);
size_t mmaped2 = __asan_get_heap_size();
EXPECT_LT(mmaped2 - mmaped1, 320U * (1 << 20));
}
Expand Down Expand Up @@ -325,10 +325,10 @@ TEST(AddressSanitizer, ThreadedOneSizeMallocStressTest) {
const int kNumThreads = 4;
pthread_t t[kNumThreads];
for (int i = 0; i < kNumThreads; i++) {
pthread_create(&t[i], 0, ThreadedOneSizeMallocStress, 0);
PTHREAD_CREATE(&t[i], 0, ThreadedOneSizeMallocStress, 0);
}
for (int i = 0; i < kNumThreads; i++) {
pthread_join(t[i], 0);
PTHREAD_JOIN(t[i], 0);
}
}

Expand Down Expand Up @@ -504,11 +504,11 @@ TEST(AddressSanitizerInterface, ManyThreadsWithStatsStressTest) {
pthread_t threads[kManyThreadsNumThreads];
before_test = __asan_get_current_allocated_bytes();
for (i = 0; i < kManyThreadsNumThreads; i++) {
pthread_create(&threads[i], 0,
PTHREAD_CREATE(&threads[i], 0,
(void* (*)(void *x))ManyThreadsWithStatsWorker, (void*)i);
}
for (i = 0; i < kManyThreadsNumThreads; i++) {
pthread_join(threads[i], 0);
PTHREAD_JOIN(threads[i], 0);
}
after_test = __asan_get_current_allocated_bytes();
// ASan stats also reflect memory usage of internal ASan RTL structs,
Expand Down
75 changes: 38 additions & 37 deletions lib/asan/tests/asan_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ void *TSDWorker(void *test_key) {
void TSDDestructor(void *tsd) {
// Spawning a thread will check that the current thread id is not -1.
pthread_t th;
pthread_create(&th, NULL, TSDWorker, NULL);
pthread_join(th, NULL);
PTHREAD_CREATE(&th, NULL, TSDWorker, NULL);
PTHREAD_JOIN(th, NULL);
}

// This tests triggers the thread-specific data destruction fiasco which occurs
Expand All @@ -225,8 +225,8 @@ TEST(AddressSanitizer, DISABLED_TSDTest) {
pthread_t th;
pthread_key_t test_key;
pthread_key_create(&test_key, TSDDestructor);
pthread_create(&th, NULL, TSDWorker, &test_key);
pthread_join(th, NULL);
PTHREAD_CREATE(&th, NULL, TSDWorker, &test_key);
PTHREAD_JOIN(th, NULL);
pthread_key_delete(test_key);
}

Expand Down Expand Up @@ -463,11 +463,11 @@ TEST(AddressSanitizer, ThreadedMallocStressTest) {
const int kNumIterations = (ASAN_LOW_MEMORY) ? 10000 : 100000;
pthread_t t[kNumThreads];
for (int i = 0; i < kNumThreads; i++) {
pthread_create(&t[i], 0, (void* (*)(void *x))MallocStress,
PTHREAD_CREATE(&t[i], 0, (void* (*)(void *x))MallocStress,
(void*)kNumIterations);
}
for (int i = 0; i < kNumThreads; i++) {
pthread_join(t[i], 0);
PTHREAD_JOIN(t[i], 0);
}
}

Expand All @@ -481,13 +481,14 @@ void *ManyThreadsWorker(void *a) {
}

TEST(AddressSanitizer, ManyThreadsTest) {
const size_t kNumThreads = SANITIZER_WORDSIZE == 32 ? 30 : 1000;
const size_t kNumThreads =
(SANITIZER_WORDSIZE == 32 || ASAN_AVOID_EXPENSIVE_TESTS) ? 30 : 1000;
pthread_t t[kNumThreads];
for (size_t i = 0; i < kNumThreads; i++) {
pthread_create(&t[i], 0, (void* (*)(void *x))ManyThreadsWorker, (void*)i);
PTHREAD_CREATE(&t[i], 0, ManyThreadsWorker, (void*)i);
}
for (size_t i = 0; i < kNumThreads; i++) {
pthread_join(t[i], 0);
PTHREAD_JOIN(t[i], 0);
}
}

Expand Down Expand Up @@ -761,10 +762,10 @@ void *ThreadStackReuseFunc2(void *unused) {

TEST(AddressSanitizer, ThreadStackReuseTest) {
pthread_t t;
pthread_create(&t, 0, ThreadStackReuseFunc1, 0);
pthread_join(t, 0);
pthread_create(&t, 0, ThreadStackReuseFunc2, 0);
pthread_join(t, 0);
PTHREAD_CREATE(&t, 0, ThreadStackReuseFunc1, 0);
PTHREAD_JOIN(t, 0);
PTHREAD_CREATE(&t, 0, ThreadStackReuseFunc2, 0);
PTHREAD_JOIN(t, 0);
}

#if defined(__i386__) || defined(__x86_64__)
Expand Down Expand Up @@ -1639,12 +1640,12 @@ void *ThreadedTestUse(void *a) {
void ThreadedTestSpawn() {
pthread_t t;
int *x;
pthread_create(&t, 0, ThreadedTestAlloc, &x);
pthread_join(t, 0);
pthread_create(&t, 0, ThreadedTestFree, &x);
pthread_join(t, 0);
pthread_create(&t, 0, ThreadedTestUse, &x);
pthread_join(t, 0);
PTHREAD_CREATE(&t, 0, ThreadedTestAlloc, &x);
PTHREAD_JOIN(t, 0);
PTHREAD_CREATE(&t, 0, ThreadedTestFree, &x);
PTHREAD_JOIN(t, 0);
PTHREAD_CREATE(&t, 0, ThreadedTestUse, &x);
PTHREAD_JOIN(t, 0);
}

TEST(AddressSanitizer, ThreadedTest) {
Expand Down Expand Up @@ -1802,10 +1803,10 @@ TEST(AddressSanitizer, ThreadedStressStackReuseTest) {
const int kNumThreads = 20;
pthread_t t[kNumThreads];
for (int i = 0; i < kNumThreads; i++) {
pthread_create(&t[i], 0, (void* (*)(void *x))LotsOfStackReuse, 0);
PTHREAD_CREATE(&t[i], 0, (void* (*)(void *x))LotsOfStackReuse, 0);
}
for (int i = 0; i < kNumThreads; i++) {
pthread_join(t[i], 0);
PTHREAD_JOIN(t[i], 0);
}
}

Expand All @@ -1817,8 +1818,8 @@ static void *PthreadExit(void *a) {
TEST(AddressSanitizer, PthreadExitTest) {
pthread_t t;
for (int i = 0; i < 1000; i++) {
pthread_create(&t, 0, PthreadExit, 0);
pthread_join(t, 0);
PTHREAD_CREATE(&t, 0, PthreadExit, 0);
PTHREAD_JOIN(t, 0);
}
}

Expand Down Expand Up @@ -1887,8 +1888,8 @@ TEST(AddressSanitizer, DISABLED_DemoStackTest) {

TEST(AddressSanitizer, DISABLED_DemoThreadStackTest) {
pthread_t t;
pthread_create(&t, 0, SimpleBugOnSTack, 0);
pthread_join(t, 0);
PTHREAD_CREATE(&t, 0, SimpleBugOnSTack, 0);
PTHREAD_JOIN(t, 0);
}

TEST(AddressSanitizer, DISABLED_DemoUAFLowIn) {
Expand Down Expand Up @@ -1978,8 +1979,8 @@ TEST(AddressSanitizerMac, CFAllocatorDefaultDoubleFree) {

void CFAllocator_DoubleFreeOnPthread() {
pthread_t child;
pthread_create(&child, NULL, CFAllocatorDefaultDoubleFree, NULL);
pthread_join(child, NULL); // Shouldn't be reached.
PTHREAD_CREATE(&child, NULL, CFAllocatorDefaultDoubleFree, NULL);
PTHREAD_JOIN(child, NULL); // Shouldn't be reached.
}

TEST(AddressSanitizerMac, CFAllocatorDefaultDoubleFree_ChildPhread) {
Expand All @@ -2004,10 +2005,10 @@ void *CFAllocatorDeallocateFromGlob(void *unused) {

void CFAllocator_PassMemoryToAnotherThread() {
pthread_t th1, th2;
pthread_create(&th1, NULL, CFAllocatorAllocateToGlob, NULL);
pthread_join(th1, NULL);
pthread_create(&th2, NULL, CFAllocatorDeallocateFromGlob, NULL);
pthread_join(th2, NULL);
PTHREAD_CREATE(&th1, NULL, CFAllocatorAllocateToGlob, NULL);
PTHREAD_JOIN(th1, NULL);
PTHREAD_CREATE(&th2, NULL, CFAllocatorDeallocateFromGlob, NULL);
PTHREAD_JOIN(th2, NULL);
}

TEST(AddressSanitizerMac, CFAllocator_PassMemoryToAnotherThread) {
Expand Down Expand Up @@ -2123,13 +2124,13 @@ TEST(AddressSanitizerMac, MallocIntrospectionLock) {
for (iter = 0; iter < kNumIterations; iter++) {
pthread_t workers[kNumWorkers], forker;
for (i = 0; i < kNumWorkers; i++) {
pthread_create(&workers[i], 0, MallocIntrospectionLockWorker, 0);
PTHREAD_CREATE(&workers[i], 0, MallocIntrospectionLockWorker, 0);
}
pthread_create(&forker, 0, MallocIntrospectionLockForker, 0);
PTHREAD_CREATE(&forker, 0, MallocIntrospectionLockForker, 0);
for (i = 0; i < kNumWorkers; i++) {
pthread_join(workers[i], 0);
PTHREAD_JOIN(workers[i], 0);
}
pthread_join(forker, 0);
PTHREAD_JOIN(forker, 0);
}
}

Expand All @@ -2145,8 +2146,8 @@ TEST(AddressSanitizerMac, DISABLED_TSDWorkqueueTest) {
pthread_t th;
pthread_key_t test_key;
pthread_key_create(&test_key, CallFreeOnWorkqueue);
pthread_create(&th, NULL, TSDAllocWorker, &test_key);
pthread_join(th, NULL);
PTHREAD_CREATE(&th, NULL, TSDAllocWorker, &test_key);
PTHREAD_JOIN(th, NULL);
pthread_key_delete(test_key);
}

Expand Down
6 changes: 5 additions & 1 deletion lib/asan/tests/asan_test_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ using std::map;
#endif

#ifndef ASAN_LOW_MEMORY
#define ASAN_LOW_MEMORY 0
# define ASAN_LOW_MEMORY 0
#endif

#ifndef ASAN_AVOID_EXPENSIVE_TESTS
# define ASAN_AVOID_EXPENSIVE_TESTS 0
#endif

#define ASAN_PCRE_DOTALL ""
Expand Down
4 changes: 4 additions & 0 deletions lib/asan/tests/asan_test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@ static T Ident(T t) {
return ret;
}

// Check that pthread_create/pthread_join return success.
#define PTHREAD_CREATE(a, b, c, d) EXPECT_EQ(0, pthread_create(a, b, c, d))
#define PTHREAD_JOIN(a, b) EXPECT_EQ(0, pthread_join(a, b))

#endif // ASAN_TEST_UTILS_H

0 comments on commit 2697687

Please sign in to comment.