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