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