xref: /llvm-project/compiler-rt/test/asan/TestCases/interception_failure_test.cpp (revision 3237f568403b80a6df47dd88dcfb03cb3a99b633)
1 // If user provides his own libc functions, ASan doesn't
2 // intercept these functions.
3 
4 // RUN: %clangxx_asan -O0 %s -o %t && %run %t 2>&1 | FileCheck %s
5 // RUN: %clangxx_asan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
6 // RUN: %clangxx_asan -O2 %s -o %t && %run %t 2>&1 | FileCheck %s
7 // RUN: %clangxx_asan -O3 %s -o %t && %run %t 2>&1 | FileCheck %s
8 // XFAIL: target={{.*freebsd.*}}
9 
10 // On Windows, the static runtime build _will_ intercept static copies of libc
11 // functions, making this test invalid.
12 // In addition, defining strtol in a static build used to result in linker
13 // errors with libucrt.lib, but this stopped happening somewhere between WinSDK
14 // 10.0.19041.0 and 10.0.22621.0 due to some changes in its implementation.
15 // UNSUPPORTED: win32-static-asan
16 
17 // On NetBSD, defining strtol in a static build results in linker errors, but
18 // it works with the dynamic runtime.
19 // XFAIL: target={{.*netbsd.*}} && !asan-dynamic-runtime
20 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 
strtol(const char * nptr,char ** endptr,int base)25 extern "C" long strtol(const char *nptr, char **endptr, int base) {
26   fprintf(stderr, "my_strtol_interceptor\n");
27   if (endptr)
28     *endptr = (char*)nptr + strlen(nptr);
29   return 0;
30 }
31 
main()32 int main() {
33   char *x = (char*)malloc(10 * sizeof(char));
34   free(x);
35   return (int)strtol(x, 0, 10);
36   // CHECK: my_strtol_interceptor
37   // CHECK-NOT: heap-use-after-free
38 }
39