1 // RUN: export ASAN_OPTIONS=detect_stack_use_after_return=1 2 // RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s 3 // RUN: %clangxx_asan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s 4 // XFAIL: arm-linux-gnueabi 5 6 // FIXME: Fix this test under GCC. 7 // REQUIRES: Clang 8 9 #include <stdio.h> 10 #include <string.h> 11 #include <stdlib.h> 12 13 __attribute__((noinline)) 14 char *pretend_to_do_something(char *x) { 15 __asm__ __volatile__("" : : "r" (x) : "memory"); 16 return x; 17 } 18 19 __attribute__((noinline)) 20 char *LeakStack() { 21 char x[1024]; 22 memset(x, 0, sizeof(x)); 23 return pretend_to_do_something(x); 24 } 25 26 template<size_t kFrameSize> 27 __attribute__((noinline)) 28 void RecursiveFunctionWithStackFrame(int depth) { 29 if (depth <= 0) return; 30 char x[kFrameSize]; 31 x[0] = depth; 32 pretend_to_do_something(x); 33 RecursiveFunctionWithStackFrame<kFrameSize>(depth - 1); 34 } 35 36 int main(int argc, char **argv) { 37 int n_iter = argc >= 2 ? atoi(argv[1]) : 1000; 38 int depth = argc >= 3 ? atoi(argv[2]) : 500; 39 for (int i = 0; i < n_iter; i++) { 40 RecursiveFunctionWithStackFrame<10>(depth); 41 RecursiveFunctionWithStackFrame<100>(depth); 42 RecursiveFunctionWithStackFrame<500>(depth); 43 RecursiveFunctionWithStackFrame<1024>(depth); 44 RecursiveFunctionWithStackFrame<2000>(depth); 45 // The stack size is tight for the main thread in multithread 46 // environment on FreeBSD. 47 #if !defined(__FreeBSD__) 48 RecursiveFunctionWithStackFrame<5000>(depth); 49 RecursiveFunctionWithStackFrame<10000>(depth); 50 #endif 51 } 52 char *stale_stack = LeakStack(); 53 RecursiveFunctionWithStackFrame<1024>(10); 54 stale_stack[100]++; 55 // CHECK: ERROR: AddressSanitizer: stack-use-after-return on address 56 // CHECK: is located in stack of thread T0 at offset {{116|132}} in frame 57 // CHECK: in LeakStack{{.*}}heavy_uar_test.cc: 58 // CHECK: [{{16|32}}, {{1040|1056}}) 'x' 59 return 0; 60 } 61