Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / safestack / pthread-stack-size.c
blob2fe7c680ef6f09bb09be83f8e4871aa5eb4a5c53
1 // RUN: %clang_safestack %s -pthread -o %t
2 // RUN: %run %t
4 // Test unsafe stack deallocation with custom stack sizes, in particular ensure
5 // that we correctly deallocate small stacks and don't accidentally deallocate
6 // adjacent memory.
8 #include <pthread.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
13 volatile int step = 0;
15 void *wait_until(void *ptr) {
16 while ((int)ptr != step)
17 usleep(1000);
19 volatile char buf[64];
20 buf[0] = 0;
22 return NULL;
25 int main(int argc, char **argv) {
26 pthread_t t1, t2, t3;
28 pthread_attr_t small_stack_attr;
29 pthread_attr_init(&small_stack_attr);
30 pthread_attr_setstacksize(&small_stack_attr, 65536);
32 if (pthread_create(&t3, NULL, wait_until, (void *)3))
33 abort();
34 if (pthread_create(&t1, &small_stack_attr, wait_until, (void *)1))
35 abort();
36 if (pthread_create(&t2, NULL, wait_until, (void *)2))
37 abort();
39 step = 1;
40 if (pthread_join(t1, NULL))
41 abort();
43 step = 2;
44 if (pthread_join(t2, NULL))
45 abort();
47 step = 3;
48 if (pthread_join(t3, NULL))
49 abort();
51 pthread_attr_destroy(&small_stack_attr);
52 return 0;