1 // RUN: %clangxx_asan -O0 %s -pthread -o %t -mllvm -asan-detect-invalid-pointer-pair
2 
3 // RUN: %env_asan_opts=detect_invalid_pointer_pairs=2 %run %t a 2>&1 | FileCheck %s -check-prefix=OK -allow-empty
4 // RUN: %env_asan_opts=detect_invalid_pointer_pairs=2 not %run %t b 2>&1 | FileCheck %s -check-prefix=B
5 
6 // pthread barriers are not available on OS X
7 // UNSUPPORTED: darwin
8 
9 #include <assert.h>
10 #include <pthread.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 
14 char *pointers[2];
15 pthread_barrier_t bar;
16 
thread_main(void * n)17 void *thread_main(void *n) {
18   char local;
19 
20   unsigned long id = (unsigned long)n;
21   pointers[id] = &local;
22   pthread_barrier_wait(&bar);
23   pthread_barrier_wait(&bar);
24 
25   return NULL;
26 }
27 
main(int argc,char ** argv)28 int main(int argc, char **argv) {
29   assert(argc >= 2);
30 
31   char t = argv[1][0];
32 
33   pthread_t threads[2];
34   pthread_barrier_init(&bar, NULL, 3);
35   pthread_create(&threads[0], 0, thread_main, (void *)0);
36   pthread_create(&threads[1], 0, thread_main, (void *)1);
37   pthread_barrier_wait(&bar);
38 
39   if (t == 'a') {
40     // OK-NOT: not handled yet
41     unsigned r = pointers[0] - pointers[1];
42   } else {
43     char local;
44     char *parent_pointer = &local;
45 
46     // B: ERROR: AddressSanitizer: invalid-pointer-pair
47     // B: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-threads.cpp:[[@LINE+1]]
48     unsigned r = parent_pointer - pointers[0];
49   }
50 
51   pthread_barrier_wait(&bar);
52   pthread_join(threads[0], 0);
53   pthread_join(threads[1], 0);
54   pthread_barrier_destroy(&bar);
55 
56   return 0;
57 }
58