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 4 // LSan-in-ASan fails at -O0 on aarch64, because the stack use-after-return 5 // instrumentation stashes the argument to `PutPointerOnStaleStack` on the stack 6 // in order to conditionally call __asan_stack_malloc. This subverts our 7 // expectations for this test, where we assume the pointer is never stashed 8 // except at the bottom of the dead frame. Building at -O1 or greater solves 9 // this problem, because the compiler is smart enough to stash the argument in a 10 // callee-saved register for rematerialization instead. 11 // RUN: %clangxx_lsan -O1 %s -o %t 12 13 // RUN: %env_lsan_opts=$LSAN_BASE not %run %t 2>&1 | FileCheck %s 14 // RUN: %env_lsan_opts=$LSAN_BASE":exitcode=0" %run %t 2>&1 | FileCheck --check-prefix=CHECK-sanity %s 15 // 16 // x86 passes parameters through stack that may lead to false negatives 17 // The same applies to s390x register save areas. 18 // UNSUPPORTED: x86,i686,powerpc64,arm,s390x 19 20 #include <stdio.h> 21 #include <stdlib.h> 22 #include "sanitizer_common/print_address.h" 23 24 void **pp; 25 26 // Put pointer far enough on the stack that LSan has space to run in without 27 // overwriting it. 28 // Hopefully the argument p will be passed on a register, saving us from false 29 // negatives. 30 __attribute__((noinline)) 31 void *PutPointerOnStaleStack(void *p) { 32 void *locals[2048]; 33 locals[0] = p; 34 pp = &locals[0]; 35 print_address("Test alloc: ", 1, locals[0]); 36 return 0; 37 } 38 39 int main() { 40 PutPointerOnStaleStack(malloc(1337)); 41 return 0; 42 } 43 44 // This must run after LSan, to ensure LSan didn't overwrite the pointer before 45 // it had a chance to see it. If LSan is invoked with atexit(), this works. 46 // Otherwise, we need a different method. 47 __attribute__((destructor)) 48 __attribute__((no_sanitize_address)) 49 void ConfirmPointerHasSurvived() { 50 print_address("Value after LSan: ", 1, *pp); 51 } 52 // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]] 53 // CHECK-sanity: Test alloc: [[ADDR:0x[0-9,a-f]+]] 54 // CHECK: LeakSanitizer: detected memory leaks 55 // CHECK: [[ADDR]] (1337 bytes) 56 // CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer: 57 // CHECK-sanity: Value after LSan: [[ADDR]] 58