| Peter Collingbourne | b64d0b1 | 2015-06-15 21:08:47 +0000 | [diff] [blame] | 1 | // RUN: %clang_safestack %s -pthread -o %t |
| Vlad Tsyrklevich | a24ecc3 | 2018-08-09 22:56:41 +0000 | [diff] [blame^] | 2 | // RUN: %run %t 0 |
| 3 | // RUN: not --crash %run %t 1 |
| Peter Collingbourne | b64d0b1 | 2015-06-15 21:08:47 +0000 | [diff] [blame] | 4 | |
| Vlad Tsyrklevich | a24ecc3 | 2018-08-09 22:56:41 +0000 | [diff] [blame^] | 5 | // Test unsafe stack deallocation. Unsafe stacks are not deallocated immediately |
| 6 | // at thread exit. They are deallocated by following exiting threads. |
| Peter Collingbourne | b64d0b1 | 2015-06-15 21:08:47 +0000 | [diff] [blame] | 7 | |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <pthread.h> |
| 11 | |
| 12 | enum { kBufferSize = (1 << 15) }; |
| 13 | |
| Vlad Tsyrklevich | a24ecc3 | 2018-08-09 22:56:41 +0000 | [diff] [blame^] | 14 | void *start(void *ptr) |
| Peter Collingbourne | b64d0b1 | 2015-06-15 21:08:47 +0000 | [diff] [blame] | 15 | { |
| 16 | char buffer[kBufferSize]; |
| 17 | return buffer; |
| 18 | } |
| 19 | |
| 20 | int main(int argc, char **argv) |
| 21 | { |
| Vlad Tsyrklevich | a24ecc3 | 2018-08-09 22:56:41 +0000 | [diff] [blame^] | 22 | int arg = atoi(argv[1]); |
| Peter Collingbourne | b64d0b1 | 2015-06-15 21:08:47 +0000 | [diff] [blame] | 23 | |
| Vlad Tsyrklevich | a24ecc3 | 2018-08-09 22:56:41 +0000 | [diff] [blame^] | 24 | pthread_t t1, t2; |
| 25 | char *t1_buffer = NULL; |
| 26 | |
| 27 | if (pthread_create(&t1, NULL, start, NULL)) |
| Peter Collingbourne | b64d0b1 | 2015-06-15 21:08:47 +0000 | [diff] [blame] | 28 | abort(); |
| Vlad Tsyrklevich | a24ecc3 | 2018-08-09 22:56:41 +0000 | [diff] [blame^] | 29 | if (pthread_join(t1, &t1_buffer)) |
| 30 | abort(); |
| 31 | |
| 32 | memset(t1_buffer, 0, kBufferSize); |
| 33 | |
| 34 | if (arg == 0) |
| 35 | return 0; |
| 36 | |
| 37 | if (pthread_create(&t2, NULL, start, NULL)) |
| 38 | abort(); |
| 39 | // Second thread destructor cleans up the first thread's stack. |
| 40 | if (pthread_join(t2, NULL)) |
| Peter Collingbourne | b64d0b1 | 2015-06-15 21:08:47 +0000 | [diff] [blame] | 41 | abort(); |
| 42 | |
| 43 | // should segfault here |
| Vlad Tsyrklevich | a24ecc3 | 2018-08-09 22:56:41 +0000 | [diff] [blame^] | 44 | memset(t1_buffer, 0, kBufferSize); |
| Peter Collingbourne | b64d0b1 | 2015-06-15 21:08:47 +0000 | [diff] [blame] | 45 | return 0; |
| 46 | } |