43 lines · cpp
1// If user provides his own libc functions, ASan doesn't2// intercept these functions.3 4// RUN: %clangxx_asan -O0 %s -o %t && %run %t 2>&1 | FileCheck %s5// RUN: %clangxx_asan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s6// RUN: %clangxx_asan -O2 %s -o %t && %run %t 2>&1 | FileCheck %s7// RUN: %clangxx_asan -O3 %s -o %t && %run %t 2>&1 | FileCheck %s8// XFAIL: target={{.*freebsd.*}}9 10// On Windows, the static runtime build _will_ intercept static copies of libc11// functions, making this test invalid.12// In addition, defining strtol in a static build used to result in linker13// errors with libucrt.lib, but this stopped happening somewhere between WinSDK14// 10.0.19041.0 and 10.0.22621.0 due to some changes in its implementation.15// UNSUPPORTED: win32-static-asan16 17// On NetBSD, defining strtol in a static build results in linker errors, but18// it works with the dynamic runtime.19// XFAIL: target={{.*netbsd.*}} && !asan-dynamic-runtime20 21#if defined(_MSC_VER) && !defined(__clang__)22# pragma warning(disable : 4273)23#endif24 25#include <stdlib.h>26#include <stdio.h>27#include <string.h>28 29extern "C" long strtol(const char *nptr, char **endptr, int base) {30 fprintf(stderr, "my_strtol_interceptor\n");31 if (endptr)32 *endptr = (char*)nptr + strlen(nptr);33 return 0;34}35 36int main() {37 char *x = (char*)malloc(10 * sizeof(char));38 free(x);39 return (int)strtol(x, 0, 10);40 // CHECK: my_strtol_interceptor41 // CHECK-NOT: heap-use-after-free42}43