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