1 | // RUN: %clangxx_asan %s -pthread -o %t |
2 | // RUN: %env_asan_opts=detect_stack_use_after_return=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK1 |
3 | // RUN: %env_asan_opts=detect_stack_use_after_return=0 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK0 |
4 | // RUN: %clangxx_asan -O3 %s -pthread -o %t |
5 | // RUN: %env_asan_opts=detect_stack_use_after_return=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK1 |
6 | // RUN: %env_asan_opts=detect_stack_use_after_return=0 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK0 |
7 | // REQUIRES: stable-runtime |
8 | |
9 | #include <assert.h> |
10 | #include <stdio.h> |
11 | #include <pthread.h> |
12 | #include <sanitizer/asan_interface.h> |
13 | |
14 | static const int kNumThreads = 2; |
15 | static const int kLeftRedzoneSize = sizeof(void *) * 4; |
16 | |
17 | void *Thread(void *unused) { |
18 | void *fake_stack = __asan_get_current_fake_stack(); |
19 | char var[15]; |
20 | if (fake_stack) { |
21 | fprintf(stderr, format: "fake stack found: %p; var: %p\n" , fake_stack, var); |
22 | // CHECK1: fake stack found |
23 | // CHECK1: fake stack found |
24 | void *beg, *end; |
25 | void *real_stack = |
26 | __asan_addr_is_in_fake_stack(fake_stack, addr: &var[0], beg: &beg, end: &end); |
27 | assert(real_stack); |
28 | assert((char*)beg <= (char*)&var[0]); |
29 | assert((char*)end > (char*)&var[0]); |
30 | for (int i = -kLeftRedzoneSize; i < 15; i++) { |
31 | void *beg1, *end1; |
32 | char *ptr = &var[0] + i; |
33 | void *real_stack1 = |
34 | __asan_addr_is_in_fake_stack(fake_stack, addr: ptr, beg: &beg1, end: &end1); |
35 | assert(real_stack == real_stack1); |
36 | assert(beg == beg1); |
37 | assert(end == end1); |
38 | } |
39 | } else { |
40 | fprintf(stderr, format: "no fake stack\n" ); |
41 | // CHECK0: no fake stack |
42 | // CHECK0: no fake stack |
43 | } |
44 | return NULL; |
45 | } |
46 | |
47 | int main(int argc, char **argv) { |
48 | pthread_t t[kNumThreads]; |
49 | for (int i = 0; i < kNumThreads; i++) |
50 | pthread_create(newthread: &t[i], attr: 0, start_routine: Thread, arg: 0); |
51 | for (int i = 0; i < kNumThreads; i++) |
52 | pthread_join(th: t[i], thread_return: 0); |
53 | return 0; |
54 | } |
55 | |