]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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 | #include <stdio.h> | |
8 | #include <string.h> | |
9 | #include <stdlib.h> | |
10 | ||
11 | __attribute__((noinline)) | |
12 | char *pretend_to_do_something(char *x) { | |
13 | __asm__ __volatile__("" : : "r" (x) : "memory"); | |
14 | return x; | |
15 | } | |
16 | ||
17 | __attribute__((noinline)) | |
18 | char *LeakStack() { | |
19 | char x[1024]; | |
20 | memset(x, 0, sizeof(x)); | |
21 | return pretend_to_do_something(x); | |
22 | } | |
23 | ||
24 | template<size_t kFrameSize> | |
25 | __attribute__((noinline)) | |
26 | void RecuriveFunctionWithStackFrame(int depth) { | |
27 | if (depth <= 0) return; | |
28 | char x[kFrameSize]; | |
29 | x[0] = depth; | |
30 | pretend_to_do_something(x); | |
31 | RecuriveFunctionWithStackFrame<kFrameSize>(depth - 1); | |
32 | } | |
33 | ||
34 | int main(int argc, char **argv) { | |
35 | int n_iter = argc >= 2 ? atoi(argv[1]) : 1000; | |
36 | int depth = argc >= 3 ? atoi(argv[2]) : 500; | |
37 | for (int i = 0; i < n_iter; i++) { | |
38 | RecuriveFunctionWithStackFrame<10>(depth); | |
39 | RecuriveFunctionWithStackFrame<100>(depth); | |
40 | RecuriveFunctionWithStackFrame<500>(depth); | |
41 | RecuriveFunctionWithStackFrame<1024>(depth); | |
42 | RecuriveFunctionWithStackFrame<2000>(depth); | |
43 | RecuriveFunctionWithStackFrame<5000>(depth); | |
44 | RecuriveFunctionWithStackFrame<10000>(depth); | |
45 | } | |
46 | char *stale_stack = LeakStack(); | |
47 | RecuriveFunctionWithStackFrame<1024>(10); | |
48 | stale_stack[100]++; | |
49 | // CHECK: ERROR: AddressSanitizer: stack-use-after-return on address | |
50 | // CHECK: is located in stack of thread T0 at offset {{116|132}} in frame | |
51 | // CHECK: in LeakStack(){{.*}}heavy_uar_test.cc: | |
52 | // CHECK: [{{16|32}}, {{1040|1056}}) 'x' | |
53 | return 0; | |
54 | } |