xref: /aosp_15_r20/external/compiler-rt/test/asan/TestCases/invalid-free.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -O0 %s -o %t
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 // XFAIL: arm-linux-gnueabi
8*7c3d14c8STreehugger Robot // XFAIL: armv7l-unknown-linux-gnueabihf
9*7c3d14c8STreehugger Robot 
10*7c3d14c8STreehugger Robot #include <stdlib.h>
11*7c3d14c8STreehugger Robot #include <string.h>
main(int argc,char ** argv)12*7c3d14c8STreehugger Robot int main(int argc, char **argv) {
13*7c3d14c8STreehugger Robot   char *x = (char*)malloc(10 * sizeof(char));
14*7c3d14c8STreehugger Robot   memset(x, 0, 10);
15*7c3d14c8STreehugger Robot   int res = x[argc];
16*7c3d14c8STreehugger Robot   free(x + 5);  // BOOM
17*7c3d14c8STreehugger Robot   // CHECK: AddressSanitizer: attempting free on address{{.*}}in thread T0
18*7c3d14c8STreehugger Robot   // CHECK: invalid-free.cc:[[@LINE-2]]
19*7c3d14c8STreehugger Robot   // CHECK: is located 5 bytes inside of 10-byte region
20*7c3d14c8STreehugger Robot   // CHECK: allocated by thread T0 here:
21*7c3d14c8STreehugger Robot   // MALLOC-CTX: invalid-free.cc:[[@LINE-8]]
22*7c3d14c8STreehugger Robot   return res;
23*7c3d14c8STreehugger Robot }
24