xref: /llvm-project/compiler-rt/test/lsan/TestCases/swapcontext.cpp (revision b2aa0a465013aca2fc43ca729fdb714eb52150b3)
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: %env_lsan_opts= %run %t 2>&1
6 // RUN: %env_lsan_opts= not %run %t foo 2>&1 | FileCheck %s
7 // Missing 'getcontext' and 'makecontext' on Android.
8 // UNSUPPORTED: target={{(arm|aarch64|loongarch64|powerpc64).*}},android
9 
10 #include "sanitizer_common/sanitizer_ucontext.h"
11 #include <stdio.h>
12 #include <unistd.h>
13 
14 const int kStackSize = 1 << 20;
15 
Child()16 void Child() {
17   int child_stack;
18   printf("Child: %p\n", &child_stack);
19   int *leaked = new int[666];
20 }
21 
main(int argc,char * argv[])22 int main(int argc, char *argv[]) {
23   char stack_memory[kStackSize + 1] __attribute__((aligned(16)));
24   char *heap_memory = new char[kStackSize + 1];
25   char *child_stack = (argc > 1) ? stack_memory : heap_memory;
26 
27   printf("Child stack: %p\n", child_stack);
28   ucontext_t orig_context;
29   ucontext_t child_context;
30   getcontext(&child_context);
31   child_context.uc_stack.ss_sp = child_stack;
32   child_context.uc_stack.ss_size = kStackSize / 2;
33   child_context.uc_link = &orig_context;
34   makecontext(&child_context, Child, 0);
35   if (swapcontext(&orig_context, &child_context) < 0) {
36     perror("swapcontext");
37     return 1;
38   }
39 
40   delete[] heap_memory;
41   return 0;
42 }
43 
44 // CHECK: SUMMARY: {{.*}}Sanitizer: 2664 byte(s) leaked in 1 allocation(s)
45