xref: /llvm-project/compiler-rt/test/asan/TestCases/Linux/interception_malloc_test.cpp (revision 673dc3d4a0b0fbb3b9b34ae2ecbfa522627fe582)
1*673dc3d4SNico Weber // ASan interceptor can be accessed with __interceptor_ prefix.
2*673dc3d4SNico Weber 
3*673dc3d4SNico Weber // RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
4*673dc3d4SNico Weber // RUN: %clangxx_asan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s
5*673dc3d4SNico Weber // RUN: %clangxx_asan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
6*673dc3d4SNico Weber // RUN: %clangxx_asan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s
7*673dc3d4SNico Weber #include <stdlib.h>
8*673dc3d4SNico Weber #include <stdio.h>
9*673dc3d4SNico Weber #include <unistd.h>
10*673dc3d4SNico Weber 
11*673dc3d4SNico Weber extern "C" void *__interceptor_malloc(size_t size);
malloc(size_t size)12*673dc3d4SNico Weber extern "C" void *malloc(size_t size) {
13*673dc3d4SNico Weber   write(2, "malloc call\n", sizeof("malloc call\n") - 1);
14*673dc3d4SNico Weber   return __interceptor_malloc(size);
15*673dc3d4SNico Weber }
16*673dc3d4SNico Weber 
main()17*673dc3d4SNico Weber int main() {
18*673dc3d4SNico Weber   char *x = (char*)malloc(10 * sizeof(char));
19*673dc3d4SNico Weber   free(x);
20*673dc3d4SNico Weber   return (int)strtol(x, 0, 10);
21*673dc3d4SNico Weber   // CHECK: malloc call
22*673dc3d4SNico Weber   // CHECK: heap-use-after-free
23*673dc3d4SNico Weber }
24