diff --git a/compiler-rt/lib/lsan/lsan_interceptors.cpp b/compiler-rt/lib/lsan/lsan_interceptors.cpp index b569c337e9764..efbf2fdfb0ab3 100644 --- a/compiler-rt/lib/lsan/lsan_interceptors.cpp +++ b/compiler-rt/lib/lsan/lsan_interceptors.cpp @@ -77,6 +77,8 @@ INTERCEPTOR(void*, malloc, uptr size) { } INTERCEPTOR(void, free, void *p) { + if (UNLIKELY(!p)) + return; if (DlsymAlloc::PointerIsMine(p)) return DlsymAlloc::Free(p); ENSURE_LSAN_INITED; diff --git a/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c b/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c new file mode 100644 index 0000000000000..7b5b9cf34a90f --- /dev/null +++ b/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c @@ -0,0 +1,60 @@ +// RUN: %clang -O0 %s -o %t && %run %t + +// FIXME: TSAN does not use DlsymAlloc. +// UNSUPPORTED: tsan +// FIXME: investigate why this fails on macos +// UNSUPPORTED: darwin + +#include + +const char *test() __attribute__((disable_sanitizer_instrumentation)) { + void *volatile p = malloc(3); + p = realloc(p, 7); + free(p); + + p = calloc(3, 7); + free(p); + + free(NULL); + + return ""; +} + +const char *__asan_default_options() + __attribute__((disable_sanitizer_instrumentation)) { + return test(); +} +const char *__hwasan_default_options() + __attribute__((disable_sanitizer_instrumentation)) { + return test(); +} +const char *__lsan_default_options() + __attribute__((disable_sanitizer_instrumentation)) { + return test(); +} +const char *__memprof_default_options() + __attribute__((disable_sanitizer_instrumentation)) { + return test(); +} +const char *__msan_default_options() + __attribute__((disable_sanitizer_instrumentation)) { + return test(); +} +const char *__nsan_default_options() + __attribute__((disable_sanitizer_instrumentation)) { + return test(); +} +const char *__rtsan_default_options() + __attribute__((disable_sanitizer_instrumentation)) { + return test(); +} +const char *__tsan_default_options() + __attribute__((disable_sanitizer_instrumentation)) { + return test(); +} +const char *__ubsan_default_options() + __attribute__((disable_sanitizer_instrumentation)) { + return test(); +} + +int main(int argc, char **argv) { return 0; }