39 lines · cpp
1// Test for __lsan_do_leak_check(). We test it by making the leak check run2// before global destructors, which also tests compatibility with HeapChecker's3// "normal" mode (LSan runs in "strict" mode by default).4// RUN: %clangxx_lsan %s -o %t5// RUN: %env_lsan_opts=use_stacks=0:use_registers=0 not %run %t 2>&1 | FileCheck --check-prefix=CHECK-strict %s6// RUN: %env_lsan_opts=use_stacks=0:use_registers=0 not %run %t foo 2>&1 | FileCheck --check-prefix=CHECK-normal %s7 8// Investigate why LeakyGlobal leak does show9// UNSUPPORTED: arm-linux || armhf-linux10 11#include <stdio.h>12#include <stdlib.h>13#include <sanitizer/lsan_interface.h>14 15struct LeakyGlobal {16 LeakyGlobal() {17 p = malloc(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(&__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