xref: /aosp_15_r20/external/compiler-rt/test/asan/TestCases/double-free.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -O0 %s -o %t 2>&1
2*7c3d14c8STreehugger Robot // RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=MALLOC-CTX
3*7c3d14c8STreehugger Robot 
4*7c3d14c8STreehugger Robot // Also works if no malloc context is available.
5*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=malloc_context_size=0:fast_unwind_on_malloc=0 not %run %t 2>&1 | FileCheck %s
6*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=malloc_context_size=0:fast_unwind_on_malloc=1 not %run %t 2>&1 | FileCheck %s
7*7c3d14c8STreehugger Robot 
8*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -O0 -fsanitize-recover=address %s -o %t 2>&1
9*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=halt_on_error=false %run %t 2>&1 | FileCheck %s --check-prefix CHECK-RECOVER
10*7c3d14c8STreehugger Robot 
11*7c3d14c8STreehugger Robot // XFAIL: arm-linux-gnueabi
12*7c3d14c8STreehugger Robot // XFAIL: armv7l-unknown-linux-gnueabihf
13*7c3d14c8STreehugger Robot 
14*7c3d14c8STreehugger Robot #include <stdlib.h>
15*7c3d14c8STreehugger Robot #include <string.h>
main(int argc,char ** argv)16*7c3d14c8STreehugger Robot int main(int argc, char **argv) {
17*7c3d14c8STreehugger Robot   char *x = (char*)malloc(10 * sizeof(char));
18*7c3d14c8STreehugger Robot   memset(x, 0, 10);
19*7c3d14c8STreehugger Robot   int res = x[argc];
20*7c3d14c8STreehugger Robot   free(x);
21*7c3d14c8STreehugger Robot   free(x + argc - 1);  // BOOM
22*7c3d14c8STreehugger Robot   // CHECK: AddressSanitizer: attempting double-free{{.*}}in thread T0
23*7c3d14c8STreehugger Robot   // CHECK: #0 0x{{.*}} in {{.*}}free
24*7c3d14c8STreehugger Robot   // CHECK: #1 0x{{.*}} in main {{.*}}double-free.cc:[[@LINE-3]]
25*7c3d14c8STreehugger Robot   // CHECK: freed by thread T0 here:
26*7c3d14c8STreehugger Robot   // MALLOC-CTX: #0 0x{{.*}} in {{.*}}free
27*7c3d14c8STreehugger Robot   // MALLOC-CTX: #1 0x{{.*}} in main {{.*}}double-free.cc:[[@LINE-7]]
28*7c3d14c8STreehugger Robot   // CHECK: allocated by thread T0 here:
29*7c3d14c8STreehugger Robot   // MALLOC-CTX: double-free.cc:[[@LINE-12]]
30*7c3d14c8STreehugger Robot   // CHECK-RECOVER: AddressSanitizer: attempting double-free{{.*}}in thread T0
31*7c3d14c8STreehugger Robot   // CHECK-RECOVER-NOT: AddressSanitizer CHECK failed:
32*7c3d14c8STreehugger Robot   return res;
33*7c3d14c8STreehugger Robot }
34