1// RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK
2// RUN: %clangxx_asan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK
3// RUN: %clangxx_asan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK
4// RUN: %clangxx_asan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK
5// XFAIL: arm-linux-gnueabi
6// XFAIL: armv7l-unknown-linux-gnueabihf
7
8// Test use-after-free report in the case when access is at the right border of
9// the allocation.
10
11#include <stdlib.h>
12int main() {
13  volatile char *x = (char*)malloc(sizeof(char));
14  free((void*)x);
15  *x = 42;
16  // CHECK: {{.*ERROR: AddressSanitizer: heap-use-after-free on address}}
17  // CHECK:   {{0x.* at pc 0x.* bp 0x.* sp 0x.*}}
18  // CHECK: {{WRITE of size 1 at 0x.* thread T0}}
19  // CHECK: {{    #0 0x.* in main .*use-after-free-right.cc:}}[[@LINE-4]]
20  // CHECK: {{0x.* is located 0 bytes inside of 1-byte region .0x.*,0x.*}}
21  // CHECK: {{freed by thread T0 here:}}
22
23  // CHECK-Linux: {{    #0 0x.* in .*free}}
24  // CHECK-Linux: {{    #1 0x.* in main .*use-after-free-right.cc:}}[[@LINE-10]]
25
26  // CHECK-Darwin: {{    #0 0x.* in wrap_free}}
27  // CHECK-Darwin: {{    #1 0x.* in main .*use-after-free-right.cc:}}[[@LINE-13]]
28
29  // CHECK: {{previously allocated by thread T0 here:}}
30
31  // CHECK-Linux: {{    #0 0x.* in .*malloc}}
32  // CHECK-Linux: {{    #1 0x.* in main .*use-after-free-right.cc:}}[[@LINE-19]]
33
34  // CHECK-Darwin: {{    #0 0x.* in wrap_malloc.*}}
35  // CHECK-Darwin: {{    #1 0x.* in main .*use-after-free-right.cc:}}[[@LINE-22]]
36}
37