1 // RUN: %clang_safestack %s -pthread -o %t
3 // RUN: not --crash %run %t 1
5 // Test unsafe stack deallocation. Unsafe stacks are not deallocated immediately
6 // at thread exit. They are deallocated by following exiting threads.
12 enum { kBufferSize = (1 << 15) };
14 void *start(void *ptr)
16 char buffer[kBufferSize];
20 int main(int argc, char **argv)
22 int arg = atoi(argv[1]);
25 char *t1_buffer = NULL;
27 if (pthread_create(&t1, NULL, start, NULL))
29 if (pthread_join(t1, &t1_buffer))
32 // Stack has not yet been deallocated
33 memset(t1_buffer, 0, kBufferSize);
38 for (int i = 0; i < 3; i++) {
39 if (pthread_create(&t2, NULL, start, NULL))
41 // Second thread destructor cleans up the first thread's stack.
42 if (pthread_join(t2, NULL))
45 // Should segfault here
46 memset(t1_buffer, 0, kBufferSize);
48 // PR39001: Re-try in the rare case that pthread_join() returns before the
49 // thread finishes exiting in the kernel--hence the tgkill() check for t1
50 // returns that it's alive despite pthread_join() returning.