16d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany// Check that we can store lots of stack frames if asked to.
26d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany
36efa4d6cf9bb214a5e8ddbb224a69b38c4ae6de6Alexey Samsonov// RUN: %clangxx_asan -O0 %s -o %t 2>&1
42d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// RUN: env ASAN_OPTIONS=malloc_context_size=120:redzone=512 not %run %t 2>&1 | FileCheck %s
55d71de26cedae3dafc17449fe0182045c0bd20e8Stephen Hines// XFAIL: arm-linux-gnueabi
66d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany#include <stdlib.h>
76d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany#include <stdio.h>
86d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany
96d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryanytemplate <int depth>
106d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryanystruct DeepFree {
116d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany  static void free(char *x) {
126d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany    DeepFree<depth - 1>::free(x);
136d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany  }
146d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany};
156d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany
166d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryanytemplate<>
176d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryanystruct DeepFree<0> {
186d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany  static void free(char *x) {
196d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany    ::free(x);
206d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany  }
216d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany};
226d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany
236d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryanyint main() {
24fe6d91684bcda766593800f6307233f1a33d31f6Kostya Serebryany  char *x = (char*)malloc(10);
256d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany  // deep_free(x);
266d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany  DeepFree<200>::free(x);
276d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany  return x[5];
2869d8ede30a0ef32c74af7e4e795eb4b4e7fb1d36Kostya Serebryany  // CHECK: {{.*ERROR: AddressSanitizer: heap-use-after-free on address}}
296d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany  // CHECK: DeepFree<36>
306d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany  // CHECK: DeepFree<98>
316d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany  // CHECK: DeepFree<115>
326d924facc5c979a0d25f484cffcdb51c766ed551Kostya Serebryany}
33