[mlir] More fixes for 9fddaf6b14102963f12dbb9730f101fc52e662c1
[llvm-project.git] / compiler-rt / test / lsan / TestCases / swapcontext.cpp
blob567cde74499f10b0fb77ef21defaafe162d5ca68
1 // We can't unwind stack if we're running coroutines on heap-allocated
2 // memory. Make sure we don't report these leaks.
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
10 #include "sanitizer_common/sanitizer_ucontext.h"
11 #include <stdio.h>
12 #include <unistd.h>
14 const int kStackSize = 1 << 20;
16 void Child() {
17 int child_stack;
18 printf("Child: %p\n", &child_stack);
19 int *leaked = new int[666];
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;
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;
40 delete[] heap_memory;
41 return 0;
44 // CHECK: SUMMARY: {{.*}}Sanitizer: 2664 byte(s) leaked in 1 allocation(s)