use-after-free-right.cc revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
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
6// Test use-after-free report in the case when access is at the right border of
7// the allocation.
8
9#include <stdlib.h>
10int main() {
11  volatile char *x = (char*)malloc(sizeof(char));
12  free((void*)x);
13  *x = 42;
14  // CHECK: {{.*ERROR: AddressSanitizer: heap-use-after-free on address}}
15  // CHECK:   {{0x.* at pc 0x.* bp 0x.* sp 0x.*}}
16  // CHECK: {{WRITE of size 1 at 0x.* thread T0}}
17  // CHECK: {{    #0 0x.* in main .*use-after-free-right.cc:}}[[@LINE-4]]
18  // CHECK: {{0x.* is located 0 bytes inside of 1-byte region .0x.*,0x.*}}
19  // CHECK: {{freed by thread T0 here:}}
20
21  // CHECK-Linux: {{    #0 0x.* in .*free}}
22  // CHECK-Linux: {{    #1 0x.* in main .*use-after-free-right.cc:}}[[@LINE-10]]
23
24  // CHECK-Darwin: {{    #0 0x.* in wrap_free}}
25  // CHECK-Darwin: {{    #1 0x.* in main .*use-after-free-right.cc:}}[[@LINE-13]]
26
27  // CHECK: {{previously allocated by thread T0 here:}}
28
29  // CHECK-Linux: {{    #0 0x.* in .*malloc}}
30  // CHECK-Linux: {{    #1 0x.* in main .*use-after-free-right.cc:}}[[@LINE-19]]
31
32  // CHECK-Darwin: {{    #0 0x.* in wrap_malloc.*}}
33  // CHECK-Darwin: {{    #1 0x.* in main .*use-after-free-right.cc:}}[[@LINE-22]]
34}
35