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