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: %clangxx_lsan %s -o %t
5// RUN: %env_lsan_opts=use_stacks=0:use_registers=0 not %run %t 2>&1 | FileCheck --check-prefix=CHECK-strict %s
6// RUN: %env_lsan_opts=use_stacks=0:use_registers=0 not %run %t foo 2>&1 | FileCheck --check-prefix=CHECK-normal %s
7
8// Investigate why LeakyGlobal leak does show
9// UNSUPPORTED: arm-linux || armhf-linux
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <sanitizer/lsan_interface.h>
14
15struct LeakyGlobal {
16 LeakyGlobal() {
17 p = malloc(size: 1337);
18 }
19 ~LeakyGlobal() {
20 p = 0;
21 }
22 void *p;
23};
24
25LeakyGlobal leaky_global;
26
27int main(int argc, char *argv[]) {
28 // Register leak check to run before global destructors.
29 if (argc > 1)
30 atexit(func: &__lsan_do_leak_check);
31 void *p = malloc(size: 666);
32 printf(format: "Test alloc: %p\n", p);
33 printf(format: "Test alloc in leaky global: %p\n", leaky_global.p);
34 return 0;
35}
36
37// CHECK-strict: SUMMARY: {{.*}}Sanitizer: 2003 byte(s) leaked in 2 allocation(s)
38// CHECK-normal: SUMMARY: {{.*}}Sanitizer: 666 byte(s) leaked in 1 allocation(s)
39

source code of compiler-rt/test/lsan/TestCases/do_leak_check_override.cpp