1// RUN: %clangxx_asan -O0 %s -o %t 2>&1
2// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=MALLOC-CTX
3
4// Also works if no malloc context is available.
5// RUN: env ASAN_OPTIONS=malloc_context_size=0:fast_unwind_on_malloc=0 not %run %t 2>&1 | FileCheck %s
6// RUN: env ASAN_OPTIONS=malloc_context_size=0:fast_unwind_on_malloc=1 not %run %t 2>&1 | FileCheck %s
7// XFAIL: arm-linux-gnueabi
8// XFAIL: armv7l-unknown-linux-gnueabihf
9
10#include <stdlib.h>
11#include <string.h>
12int main(int argc, char **argv) {
13  char *x = (char*)malloc(10 * sizeof(char));
14  memset(x, 0, 10);
15  int res = x[argc];
16  free(x);
17  free(x + argc - 1);  // BOOM
18  // CHECK: AddressSanitizer: attempting double-free{{.*}}in thread T0
19  // CHECK: #0 0x{{.*}} in {{.*}}free
20  // CHECK: #1 0x{{.*}} in main {{.*}}double-free.cc:[[@LINE-3]]
21  // CHECK: freed by thread T0 here:
22  // MALLOC-CTX: #0 0x{{.*}} in {{.*}}free
23  // MALLOC-CTX: #1 0x{{.*}} in main {{.*}}double-free.cc:[[@LINE-7]]
24  // CHECK: allocated by thread T0 here:
25  // MALLOC-CTX: double-free.cc:[[@LINE-12]]
26  return res;
27}
28