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 | |
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(barrier: &bar); |
23 | pthread_barrier_wait(barrier: &bar); |
24 | |
25 | return NULL; |
26 | } |
27 | |
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(barrier: &bar, NULL, count: 3); |
35 | pthread_create(newthread: &threads[0], attr: 0, start_routine: thread_main, arg: (void *)0); |
36 | pthread_create(newthread: &threads[1], attr: 0, start_routine: thread_main, arg: (void *)1); |
37 | pthread_barrier_wait(barrier: &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(barrier: &bar); |
52 | pthread_join(th: threads[0], thread_return: 0); |
53 | pthread_join(th: threads[1], thread_return: 0); |
54 | pthread_barrier_destroy(barrier: &bar); |
55 | |
56 | return 0; |
57 | } |
58 | |