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#include <ucontext.h>
10#include <unistd.h>
11
12const int kStackSize = 1 << 20;
13
14void Child() {
15  int child_stack;
16  printf("Child: %p\n", &child_stack);
17  int *leaked = new int[666];
18}
19
20int main(int argc, char *argv[]) {
21  char stack_memory[kStackSize + 1];
22  char *heap_memory = new char[kStackSize + 1];
23  char *child_stack = (argc > 1) ? stack_memory : heap_memory;
24
25  printf("Child stack: %p\n", child_stack);
26  ucontext_t orig_context;
27  ucontext_t child_context;
28  getcontext(&child_context);
29  child_context.uc_stack.ss_sp = child_stack;
30  child_context.uc_stack.ss_size = kStackSize / 2;
31  child_context.uc_link = &orig_context;
32  makecontext(&child_context, Child, 0);
33  if (swapcontext(&orig_context, &child_context) < 0) {
34    perror("swapcontext");
35    return 1;
36  }
37
38  delete[] heap_memory;
39  return 0;
40}
41
42// CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer: 2664 byte(s) leaked in 1 allocation(s)
43