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