Path: blob/main/contrib/llvm-project/compiler-rt/lib/hwasan/hwasan_allocation_functions.cpp
35236 views
//===-- hwasan_allocation_functions.cpp -----------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of HWAddressSanitizer.9//10// Definitions for __sanitizer allocation functions.11//12//===----------------------------------------------------------------------===//1314#include "hwasan.h"15#include "interception/interception.h"16#include "sanitizer_common/sanitizer_allocator_dlsym.h"17#include "sanitizer_common/sanitizer_allocator_interface.h"18#include "sanitizer_common/sanitizer_mallinfo.h"19#include "sanitizer_common/sanitizer_tls_get_addr.h"2021using namespace __hwasan;2223struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {24static bool UseImpl() { return !hwasan_inited; }25static void OnAllocate(const void *ptr, uptr size) {26# if CAN_SANITIZE_LEAKS27// Suppress leaks from dlerror(). Previously dlsym hack on global array was28// used by leak sanitizer as a root region.29__lsan_register_root_region(ptr, size);30# endif31}32static void OnFree(const void *ptr, uptr size) {33# if CAN_SANITIZE_LEAKS34__lsan_unregister_root_region(ptr, size);35# endif36}37};3839extern "C" {4041SANITIZER_INTERFACE_ATTRIBUTE42int __sanitizer_posix_memalign(void **memptr, uptr alignment, uptr size) {43GET_MALLOC_STACK_TRACE;44CHECK_NE(memptr, 0);45int res = hwasan_posix_memalign(memptr, alignment, size, &stack);46return res;47}4849SANITIZER_INTERFACE_ATTRIBUTE50void *__sanitizer_memalign(uptr alignment, uptr size) {51GET_MALLOC_STACK_TRACE;52return hwasan_memalign(alignment, size, &stack);53}5455SANITIZER_INTERFACE_ATTRIBUTE56void *__sanitizer_aligned_alloc(uptr alignment, uptr size) {57GET_MALLOC_STACK_TRACE;58return hwasan_aligned_alloc(alignment, size, &stack);59}6061SANITIZER_INTERFACE_ATTRIBUTE62void *__sanitizer___libc_memalign(uptr alignment, uptr size) {63GET_MALLOC_STACK_TRACE;64void *ptr = hwasan_memalign(alignment, size, &stack);65if (ptr)66DTLS_on_libc_memalign(ptr, size);67return ptr;68}6970SANITIZER_INTERFACE_ATTRIBUTE71void *__sanitizer_valloc(uptr size) {72GET_MALLOC_STACK_TRACE;73return hwasan_valloc(size, &stack);74}7576SANITIZER_INTERFACE_ATTRIBUTE77void *__sanitizer_pvalloc(uptr size) {78GET_MALLOC_STACK_TRACE;79return hwasan_pvalloc(size, &stack);80}8182SANITIZER_INTERFACE_ATTRIBUTE83void __sanitizer_free(void *ptr) {84if (!ptr)85return;86if (DlsymAlloc::PointerIsMine(ptr))87return DlsymAlloc::Free(ptr);88GET_MALLOC_STACK_TRACE;89hwasan_free(ptr, &stack);90}9192SANITIZER_INTERFACE_ATTRIBUTE93void __sanitizer_cfree(void *ptr) {94if (!ptr)95return;96if (DlsymAlloc::PointerIsMine(ptr))97return DlsymAlloc::Free(ptr);98GET_MALLOC_STACK_TRACE;99hwasan_free(ptr, &stack);100}101102SANITIZER_INTERFACE_ATTRIBUTE103uptr __sanitizer_malloc_usable_size(const void *ptr) {104return __sanitizer_get_allocated_size(ptr);105}106107SANITIZER_INTERFACE_ATTRIBUTE108struct __sanitizer_struct_mallinfo __sanitizer_mallinfo() {109__sanitizer_struct_mallinfo sret;110internal_memset(&sret, 0, sizeof(sret));111return sret;112}113114SANITIZER_INTERFACE_ATTRIBUTE115int __sanitizer_mallopt(int cmd, int value) { return 0; }116117SANITIZER_INTERFACE_ATTRIBUTE118void __sanitizer_malloc_stats(void) {119// FIXME: implement, but don't call REAL(malloc_stats)!120}121122SANITIZER_INTERFACE_ATTRIBUTE123void *__sanitizer_calloc(uptr nmemb, uptr size) {124if (DlsymAlloc::Use())125return DlsymAlloc::Callocate(nmemb, size);126GET_MALLOC_STACK_TRACE;127return hwasan_calloc(nmemb, size, &stack);128}129130SANITIZER_INTERFACE_ATTRIBUTE131void *__sanitizer_realloc(void *ptr, uptr size) {132if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))133return DlsymAlloc::Realloc(ptr, size);134GET_MALLOC_STACK_TRACE;135return hwasan_realloc(ptr, size, &stack);136}137138SANITIZER_INTERFACE_ATTRIBUTE139void *__sanitizer_reallocarray(void *ptr, uptr nmemb, uptr size) {140GET_MALLOC_STACK_TRACE;141return hwasan_reallocarray(ptr, nmemb, size, &stack);142}143144SANITIZER_INTERFACE_ATTRIBUTE145void *__sanitizer_malloc(uptr size) {146if (UNLIKELY(!hwasan_init_is_running))147ENSURE_HWASAN_INITED();148if (DlsymAlloc::Use())149return DlsymAlloc::Allocate(size);150GET_MALLOC_STACK_TRACE;151return hwasan_malloc(size, &stack);152}153154} // extern "C"155156#if HWASAN_WITH_INTERCEPTORS || SANITIZER_FUCHSIA157#if SANITIZER_FUCHSIA158// Fuchsia does not use WRAP/wrappers used for the interceptor infrastructure.159# define INTERCEPTOR_ALIAS(RET, FN, ARGS...) \160extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE RET FN( \161ARGS) ALIAS(__sanitizer_##FN)162#else163# define INTERCEPTOR_ALIAS(RET, FN, ARGS...) \164extern "C" SANITIZER_INTERFACE_ATTRIBUTE RET WRAP(FN)(ARGS) \165ALIAS(__sanitizer_##FN); \166extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE RET FN( \167ARGS) ALIAS(__sanitizer_##FN)168#endif169170INTERCEPTOR_ALIAS(int, posix_memalign, void **memptr, SIZE_T alignment,171SIZE_T size);172INTERCEPTOR_ALIAS(void *, aligned_alloc, SIZE_T alignment, SIZE_T size);173INTERCEPTOR_ALIAS(void *, __libc_memalign, SIZE_T alignment, SIZE_T size);174INTERCEPTOR_ALIAS(void *, valloc, SIZE_T size);175INTERCEPTOR_ALIAS(void, free, void *ptr);176INTERCEPTOR_ALIAS(uptr, malloc_usable_size, const void *ptr);177INTERCEPTOR_ALIAS(void *, calloc, SIZE_T nmemb, SIZE_T size);178INTERCEPTOR_ALIAS(void *, realloc, void *ptr, SIZE_T size);179INTERCEPTOR_ALIAS(void *, reallocarray, void *ptr, SIZE_T nmemb, SIZE_T size);180INTERCEPTOR_ALIAS(void *, malloc, SIZE_T size);181182# if !SANITIZER_FREEBSD && !SANITIZER_NETBSD183INTERCEPTOR_ALIAS(void *, memalign, SIZE_T alignment, SIZE_T size);184INTERCEPTOR_ALIAS(void *, pvalloc, SIZE_T size);185INTERCEPTOR_ALIAS(void, cfree, void *ptr);186INTERCEPTOR_ALIAS(__sanitizer_struct_mallinfo, mallinfo,);187INTERCEPTOR_ALIAS(int, mallopt, int cmd, int value);188INTERCEPTOR_ALIAS(void, malloc_stats, void);189# endif190#endif // #if HWASAN_WITH_INTERCEPTORS191192193