1// Test for __lsan_do_leak_check(). We test it by making the leak check run
2// before global destructors, which also tests compatibility with HeapChecker's
3// "normal" mode (LSan runs in "strict" mode by default).
4// RUN: LSAN_BASE="use_stacks=0:use_registers=0"
5// RUN: %clangxx_lsan %s -o %t
6// RUN: LSAN_OPTIONS=$LSAN_BASE not %run %t 2>&1 | FileCheck --check-prefix=CHECK-strict %s
7// RUN: LSAN_OPTIONS=$LSAN_BASE not %run %t foo 2>&1 | FileCheck --check-prefix=CHECK-normal %s
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <sanitizer/lsan_interface.h>
12
13struct LeakyGlobal {
14  LeakyGlobal() {
15    p = malloc(1337);
16  }
17  ~LeakyGlobal() {
18    p = 0;
19  }
20  void *p;
21};
22
23LeakyGlobal leaky_global;
24
25int main(int argc, char *argv[]) {
26  // Register leak check to run before global destructors.
27  if (argc > 1)
28    atexit(&__lsan_do_leak_check);
29  void *p = malloc(666);
30  printf("Test alloc: %p\n", p);
31  printf("Test alloc in leaky global: %p\n", leaky_global.p);
32  return 0;
33}
34
35// CHECK-strict: SUMMARY: {{(Leak|Address)}}Sanitizer: 2003 byte(s) leaked in 2 allocation(s)
36// CHECK-normal: SUMMARY: {{(Leak|Address)}}Sanitizer: 666 byte(s) leaked in 1 allocation(s)
37