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 extern unsigned sleep(unsigned seconds
);
22 int main(int argc
, char **argv
)
24 int arg
= atoi(argv
[1]);
27 char *t1_buffer
= NULL
;
29 if (pthread_create(&t1
, NULL
, start
, NULL
))
31 if (pthread_join(t1
, &t1_buffer
))
34 // Stack has not yet been deallocated
35 memset(t1_buffer
, 0, kBufferSize
);
40 for (int i
= 0; i
< 3; i
++) {
41 if (pthread_create(&t2
, NULL
, start
, NULL
))
43 // Second thread destructor cleans up the first thread's stack.
44 if (pthread_join(t2
, NULL
))
47 // Should segfault here
48 memset(t1_buffer
, 0, kBufferSize
);
50 // PR39001: Re-try in the rare case that pthread_join() returns before the
51 // thread finishes exiting in the kernel--hence the tgkill() check for t1
52 // returns that it's alive despite pthread_join() returning.