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