deep_stack_uaf.cc revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
1// Check that we can store lots of stack frames if asked to.
2
3// RUN: %clangxx_asan -O0 %s -o %t 2>&1
4// RUN: env ASAN_OPTIONS=malloc_context_size=120:redzone=512 not %run %t 2>&1 | FileCheck %s
5#include <stdlib.h>
6#include <stdio.h>
7
8template <int depth>
9struct DeepFree {
10  static void free(char *x) {
11    DeepFree<depth - 1>::free(x);
12  }
13};
14
15template<>
16struct DeepFree<0> {
17  static void free(char *x) {
18    ::free(x);
19  }
20};
21
22int main() {
23  char *x = (char*)malloc(10);
24  // deep_free(x);
25  DeepFree<200>::free(x);
26  return x[5];
27  // CHECK: {{.*ERROR: AddressSanitizer: heap-use-after-free on address}}
28  // CHECK: DeepFree<36>
29  // CHECK: DeepFree<98>
30  // CHECK: DeepFree<115>
31}
32