1 // We can't unwind stack if we're running coroutines on heap-allocated
2 // memory. Make sure we don't report these leaks.
3 
4 // RUN: %clangxx_lsan %s -o %t
5 // RUN: %run %t 2>&1
6 // RUN: not %run %t foo 2>&1 | FileCheck %s
7 
8 #include <stdio.h>
9 #if defined(__APPLE__)
10 // Note: ucontext.h is deprecated on OSX, so this test may stop working
11 // someday. We define _XOPEN_SOURCE to keep using ucontext.h for now.
12 #define _XOPEN_SOURCE 1
13 #endif
14 #include <ucontext.h>
15 #include <unistd.h>
16 
17 const int kStackSize = 1 << 20;
18 
Child()19 void Child() {
20   int child_stack;
21   printf("Child: %p\n", &child_stack);
22   int *leaked = new int[666];
23 }
24 
main(int argc,char * argv[])25 int main(int argc, char *argv[]) {
26   char stack_memory[kStackSize + 1];
27   char *heap_memory = new char[kStackSize + 1];
28   char *child_stack = (argc > 1) ? stack_memory : heap_memory;
29 
30   printf("Child stack: %p\n", child_stack);
31   ucontext_t orig_context;
32   ucontext_t child_context;
33   getcontext(&child_context);
34   child_context.uc_stack.ss_sp = child_stack;
35   child_context.uc_stack.ss_size = kStackSize / 2;
36   child_context.uc_link = &orig_context;
37   makecontext(&child_context, Child, 0);
38   if (swapcontext(&orig_context, &child_context) < 0) {
39     perror("swapcontext");
40     return 1;
41   }
42 
43   delete[] heap_memory;
44   return 0;
45 }
46 
47 // CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer: 2664 byte(s) leaked in 1 allocation(s)
48