1 // Test that out-of-scope local variables are ignored by LSan. 2 // RUN: LSAN_BASE="report_objects=1:use_registers=0:use_stacks=1" 3 // RUN: %clangxx_lsan %s -o %t 4 // RUN: LSAN_OPTIONS=$LSAN_BASE not %run %t 2>&1 | FileCheck %s 5 // RUN: LSAN_OPTIONS=$LSAN_BASE":exitcode=0" %run %t 2>&1 | FileCheck --check-prefix=CHECK-sanity %s 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 10 void **pp; 11 12 // Put pointer far enough on the stack that LSan has space to run in without 13 // overwriting it. 14 // Hopefully the argument p will be passed on a register, saving us from false 15 // negatives. 16 __attribute__((noinline)) 17 void *PutPointerOnStaleStack(void *p) { 18 void *locals[2048]; 19 locals[0] = p; 20 pp = &locals[0]; 21 fprintf(stderr, "Test alloc: %p.\n", locals[0]); 22 return 0; 23 } 24 25 int main() { 26 PutPointerOnStaleStack(malloc(1337)); 27 return 0; 28 } 29 30 // This must run after LSan, to ensure LSan didn't overwrite the pointer before 31 // it had a chance to see it. If LSan is invoked with atexit(), this works. 32 // Otherwise, we need a different method. 33 __attribute__((destructor)) 34 void ConfirmPointerHasSurvived() { 35 fprintf(stderr, "Value after LSan: %p.\n", *pp); 36 } 37 // CHECK: Test alloc: [[ADDR:.*]]. 38 // CHECK-sanity: Test alloc: [[ADDR:.*]]. 39 // CHECK: LeakSanitizer: detected memory leaks 40 // CHECK: [[ADDR]] (1337 bytes) 41 // CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer: 42 // CHECK-sanity: Value after LSan: [[ADDR]]. 43