CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
267 lines
8.4 KiB
C++
267 lines
8.4 KiB
C++
//===-- asan_malloc_linux.cpp ---------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
//
|
|
// Linux-specific (and Unix/Unix-like) malloc interception.
|
|
// We simply define functions like malloc, free, realloc, etc.
|
|
// They will replace the corresponding libc functions automagically.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "sanitizer_common/sanitizer_platform.h"
|
|
#if SANITIZER_FREEBSD || SANITIZER_FUCHSIA || SANITIZER_LINUX || \
|
|
SANITIZER_NETBSD || SANITIZER_SOLARIS || SANITIZER_HAIKU || SANITIZER_AIX
|
|
|
|
# include "asan_allocator.h"
|
|
# include "asan_interceptors.h"
|
|
# include "asan_internal.h"
|
|
# include "asan_stack.h"
|
|
# include "lsan/lsan_common.h"
|
|
# include "sanitizer_common/sanitizer_allocator_checks.h"
|
|
# include "sanitizer_common/sanitizer_allocator_dlsym.h"
|
|
# include "sanitizer_common/sanitizer_errno.h"
|
|
|
|
// ---------------------- Replacement functions ---------------- {{{1
|
|
using namespace __asan;
|
|
|
|
struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
|
|
static bool UseImpl() { return !TryAsanInitFromRtl(); }
|
|
static void OnAllocate(const void* ptr, uptr size) {
|
|
# if CAN_SANITIZE_LEAKS
|
|
// Suppress leaks from dlerror(). Previously dlsym hack on global array was
|
|
// used by leak sanitizer as a root region.
|
|
__lsan_register_root_region(ptr, size);
|
|
# endif
|
|
}
|
|
static void OnFree(const void* ptr, uptr size) {
|
|
# if CAN_SANITIZE_LEAKS
|
|
__lsan_unregister_root_region(ptr, size);
|
|
# endif
|
|
}
|
|
};
|
|
|
|
INTERCEPTOR(void, free, void* ptr) {
|
|
if (DlsymAlloc::PointerIsMine(ptr))
|
|
return DlsymAlloc::Free(ptr);
|
|
GET_STACK_TRACE_FREE;
|
|
asan_free(ptr, &stack);
|
|
}
|
|
|
|
# if SANITIZER_INTERCEPT_CFREE
|
|
INTERCEPTOR(void, cfree, void* ptr) {
|
|
if (DlsymAlloc::PointerIsMine(ptr))
|
|
return DlsymAlloc::Free(ptr);
|
|
GET_STACK_TRACE_FREE;
|
|
asan_free(ptr, &stack);
|
|
}
|
|
# endif // SANITIZER_INTERCEPT_CFREE
|
|
|
|
# if SANITIZER_INTERCEPT_FREE_SIZED
|
|
INTERCEPTOR(void, free_sized, void* ptr, uptr size) {
|
|
if (UNLIKELY(!ptr))
|
|
return;
|
|
if (DlsymAlloc::PointerIsMine(ptr))
|
|
return DlsymAlloc::Free(ptr);
|
|
GET_STACK_TRACE_FREE;
|
|
asan_free_sized(ptr, size, &stack);
|
|
}
|
|
# endif // SANITIZER_INTERCEPT_FREE_SIZED
|
|
|
|
# if SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED
|
|
INTERCEPTOR(void, free_aligned_sized, void* ptr, uptr alignment, uptr size) {
|
|
if (UNLIKELY(!ptr))
|
|
return;
|
|
if (DlsymAlloc::PointerIsMine(ptr))
|
|
return DlsymAlloc::Free(ptr);
|
|
GET_STACK_TRACE_FREE;
|
|
asan_free_aligned_sized(ptr, alignment, size, &stack);
|
|
}
|
|
# endif // SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED
|
|
|
|
# if SANITIZER_AIX
|
|
// Unlike malloc, vec_malloc must return memory aligned to 16 bytes.
|
|
INTERCEPTOR(void*, vec_malloc, uptr size) {
|
|
if (DlsymAlloc::Use())
|
|
return DlsymAlloc::Allocate(size, 16);
|
|
GET_STACK_TRACE_MALLOC;
|
|
return asan_vec_malloc(size, &stack);
|
|
}
|
|
|
|
// Unlike calloc, vec_calloc must return memory aligned to 16 bytes.
|
|
INTERCEPTOR(void*, vec_calloc, uptr nmemb, uptr size) {
|
|
if (DlsymAlloc::Use())
|
|
return DlsymAlloc::Callocate(nmemb, size, 16);
|
|
GET_STACK_TRACE_MALLOC;
|
|
return asan_vec_calloc(nmemb, size, &stack);
|
|
}
|
|
# endif
|
|
|
|
// TODO: Fix malloc/calloc interceptors to return 16-byte alignment with AIX on
|
|
// PASE.
|
|
INTERCEPTOR(void*, malloc, uptr size) {
|
|
if (DlsymAlloc::Use())
|
|
return DlsymAlloc::Allocate(size);
|
|
GET_STACK_TRACE_MALLOC;
|
|
return asan_malloc(size, &stack);
|
|
}
|
|
|
|
INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
|
|
if (DlsymAlloc::Use())
|
|
return DlsymAlloc::Callocate(nmemb, size);
|
|
GET_STACK_TRACE_MALLOC;
|
|
return asan_calloc(nmemb, size, &stack);
|
|
}
|
|
|
|
// TODO: AIX needs a method to ensure 16-byte alignment if the incoming
|
|
// pointer was allocated with a 16-byte alignment requirement (or perhaps
|
|
// merely if it happens to have 16-byte alignment).
|
|
INTERCEPTOR(void*, realloc, void* ptr, uptr size) {
|
|
if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
|
|
return DlsymAlloc::Realloc(ptr, size);
|
|
GET_STACK_TRACE_MALLOC;
|
|
return asan_realloc(ptr, size, &stack);
|
|
}
|
|
|
|
# if SANITIZER_INTERCEPT_REALLOCARRAY
|
|
INTERCEPTOR(void*, reallocarray, void* ptr, uptr nmemb, uptr size) {
|
|
AsanInitFromRtl();
|
|
GET_STACK_TRACE_MALLOC;
|
|
return asan_reallocarray(ptr, nmemb, size, &stack);
|
|
}
|
|
# endif // SANITIZER_INTERCEPT_REALLOCARRAY
|
|
|
|
# if SANITIZER_INTERCEPT_MEMALIGN
|
|
INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
|
|
GET_STACK_TRACE_MALLOC;
|
|
return asan_memalign(boundary, size, &stack);
|
|
}
|
|
|
|
INTERCEPTOR(void*, __libc_memalign, uptr boundary, uptr size) {
|
|
GET_STACK_TRACE_MALLOC;
|
|
return asan_memalign(boundary, size, &stack);
|
|
}
|
|
# endif // SANITIZER_INTERCEPT_MEMALIGN
|
|
|
|
# if SANITIZER_INTERCEPT_ALIGNED_ALLOC
|
|
INTERCEPTOR(void*, aligned_alloc, uptr boundary, uptr size) {
|
|
GET_STACK_TRACE_MALLOC;
|
|
return asan_aligned_alloc(boundary, size, &stack);
|
|
}
|
|
# endif // SANITIZER_INTERCEPT_ALIGNED_ALLOC
|
|
|
|
INTERCEPTOR(uptr, malloc_usable_size, void* ptr) {
|
|
GET_CURRENT_PC_BP_SP;
|
|
(void)sp;
|
|
return asan_malloc_usable_size(ptr, pc, bp);
|
|
}
|
|
|
|
# if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
|
|
// We avoid including malloc.h for portability reasons.
|
|
// man mallinfo says the fields are "long", but the implementation uses int.
|
|
// It doesn't matter much -- we just need to make sure that the libc's mallinfo
|
|
// is not called.
|
|
struct fake_mallinfo {
|
|
int x[10];
|
|
};
|
|
|
|
INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
|
|
struct fake_mallinfo res;
|
|
REAL(memset)(&res, 0, sizeof(res));
|
|
return res;
|
|
}
|
|
|
|
INTERCEPTOR(int, mallopt, int cmd, int value) { return 0; }
|
|
# endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
|
|
|
|
INTERCEPTOR(int, posix_memalign, void** memptr, uptr alignment, uptr size) {
|
|
GET_STACK_TRACE_MALLOC;
|
|
return asan_posix_memalign(memptr, alignment, size, &stack);
|
|
}
|
|
|
|
INTERCEPTOR(void*, valloc, uptr size) {
|
|
GET_STACK_TRACE_MALLOC;
|
|
return asan_valloc(size, &stack);
|
|
}
|
|
|
|
# if SANITIZER_INTERCEPT_PVALLOC
|
|
INTERCEPTOR(void*, pvalloc, uptr size) {
|
|
GET_STACK_TRACE_MALLOC;
|
|
return asan_pvalloc(size, &stack);
|
|
}
|
|
# endif // SANITIZER_INTERCEPT_PVALLOC
|
|
|
|
INTERCEPTOR(void, malloc_stats, void) { __asan_print_accumulated_stats(); }
|
|
|
|
# if SANITIZER_ANDROID
|
|
// Format of __libc_malloc_dispatch has changed in Android L.
|
|
// While we are moving towards a solution that does not depend on bionic
|
|
// internals, here is something to support both K* and L releases.
|
|
struct MallocDebugK {
|
|
void* (*malloc)(uptr bytes);
|
|
void (*free)(void* mem);
|
|
void* (*calloc)(uptr n_elements, uptr elem_size);
|
|
void* (*realloc)(void* oldMem, uptr bytes);
|
|
void* (*memalign)(uptr alignment, uptr bytes);
|
|
uptr (*malloc_usable_size)(void* mem);
|
|
};
|
|
|
|
struct MallocDebugL {
|
|
void* (*calloc)(uptr n_elements, uptr elem_size);
|
|
void (*free)(void* mem);
|
|
fake_mallinfo (*mallinfo)(void);
|
|
void* (*malloc)(uptr bytes);
|
|
uptr (*malloc_usable_size)(void* mem);
|
|
void* (*memalign)(uptr alignment, uptr bytes);
|
|
int (*posix_memalign)(void** memptr, uptr alignment, uptr size);
|
|
void* (*pvalloc)(uptr size);
|
|
void* (*realloc)(void* oldMem, uptr bytes);
|
|
void* (*valloc)(uptr size);
|
|
};
|
|
|
|
alignas(32) const MallocDebugK asan_malloc_dispatch_k = {
|
|
WRAP(malloc), WRAP(free), WRAP(calloc),
|
|
WRAP(realloc), WRAP(memalign), WRAP(malloc_usable_size)};
|
|
|
|
alignas(32) const MallocDebugL asan_malloc_dispatch_l = {
|
|
WRAP(calloc),
|
|
WRAP(free),
|
|
WRAP(mallinfo),
|
|
WRAP(malloc),
|
|
WRAP(malloc_usable_size),
|
|
WRAP(memalign),
|
|
WRAP(posix_memalign),
|
|
WRAP(pvalloc),
|
|
WRAP(realloc),
|
|
WRAP(valloc)};
|
|
|
|
namespace __asan {
|
|
void ReplaceSystemMalloc() {
|
|
void** __libc_malloc_dispatch_p =
|
|
(void**)AsanDlSymNext("__libc_malloc_dispatch");
|
|
if (__libc_malloc_dispatch_p) {
|
|
// Decide on K vs L dispatch format by the presence of
|
|
// __libc_malloc_default_dispatch export in libc.
|
|
void* default_dispatch_p = AsanDlSymNext("__libc_malloc_default_dispatch");
|
|
if (default_dispatch_p)
|
|
*__libc_malloc_dispatch_p = (void*)&asan_malloc_dispatch_k;
|
|
else
|
|
*__libc_malloc_dispatch_p = (void*)&asan_malloc_dispatch_l;
|
|
}
|
|
}
|
|
} // namespace __asan
|
|
|
|
# else // SANITIZER_ANDROID
|
|
|
|
namespace __asan {
|
|
void ReplaceSystemMalloc() {}
|
|
} // namespace __asan
|
|
# endif // SANITIZER_ANDROID
|
|
|
|
#endif // SANITIZER_FREEBSD || SANITIZER_FUCHSIA || SANITIZER_LINUX ||
|
|
// SANITIZER_NETBSD || SANITIZER_SOLARIS || SANITIZER_HAIKU
|