xref: /llvm-project/compiler-rt/test/lsan/TestCases/do_leak_check_override.cpp (revision b2aa0a465013aca2fc43ca729fdb714eb52150b3)
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 
15 struct LeakyGlobal {
LeakyGlobalLeakyGlobal16   LeakyGlobal() {
17     p = malloc(1337);
18   }
~LeakyGlobalLeakyGlobal19   ~LeakyGlobal() {
20     p = 0;
21   }
22   void *p;
23 };
24 
25 LeakyGlobal leaky_global;
26 
main(int argc,char * argv[])27 int main(int argc, char *argv[]) {
28   // Register leak check to run before global destructors.
29   if (argc > 1)
30     atexit(&__lsan_do_leak_check);
31   void *p = malloc(666);
32   printf("Test alloc: %p\n", p);
33   printf("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