Path: blob/main/contrib/llvm-project/compiler-rt/lib/memprof/memprof_malloc_linux.cpp
35236 views
//===-- memprof_malloc_linux.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 MemProfiler, a memory profiler.9//10// Linux-specific malloc interception.11// We simply define functions like malloc, free, realloc, etc.12// They will replace the corresponding libc functions automagically.13//===----------------------------------------------------------------------===//1415#include "sanitizer_common/sanitizer_platform.h"16#if !SANITIZER_LINUX17#error Unsupported OS18#endif1920#include "memprof_allocator.h"21#include "memprof_interceptors.h"22#include "memprof_internal.h"23#include "memprof_stack.h"24#include "sanitizer_common/sanitizer_allocator_checks.h"25#include "sanitizer_common/sanitizer_allocator_dlsym.h"26#include "sanitizer_common/sanitizer_errno.h"27#include "sanitizer_common/sanitizer_tls_get_addr.h"2829// ---------------------- Replacement functions ---------------- {{{130using namespace __memprof;3132struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {33static bool UseImpl() { return memprof_init_is_running; }34};3536INTERCEPTOR(void, free, void *ptr) {37if (DlsymAlloc::PointerIsMine(ptr))38return DlsymAlloc::Free(ptr);39GET_STACK_TRACE_FREE;40memprof_free(ptr, &stack, FROM_MALLOC);41}4243#if SANITIZER_INTERCEPT_CFREE44INTERCEPTOR(void, cfree, void *ptr) {45if (DlsymAlloc::PointerIsMine(ptr))46return DlsymAlloc::Free(ptr);47GET_STACK_TRACE_FREE;48memprof_free(ptr, &stack, FROM_MALLOC);49}50#endif // SANITIZER_INTERCEPT_CFREE5152INTERCEPTOR(void *, malloc, uptr size) {53if (DlsymAlloc::Use())54return DlsymAlloc::Allocate(size);55ENSURE_MEMPROF_INITED();56GET_STACK_TRACE_MALLOC;57return memprof_malloc(size, &stack);58}5960INTERCEPTOR(void *, calloc, uptr nmemb, uptr size) {61if (DlsymAlloc::Use())62return DlsymAlloc::Callocate(nmemb, size);63ENSURE_MEMPROF_INITED();64GET_STACK_TRACE_MALLOC;65return memprof_calloc(nmemb, size, &stack);66}6768INTERCEPTOR(void *, realloc, void *ptr, uptr size) {69if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))70return DlsymAlloc::Realloc(ptr, size);71ENSURE_MEMPROF_INITED();72GET_STACK_TRACE_MALLOC;73return memprof_realloc(ptr, size, &stack);74}7576#if SANITIZER_INTERCEPT_REALLOCARRAY77INTERCEPTOR(void *, reallocarray, void *ptr, uptr nmemb, uptr size) {78ENSURE_MEMPROF_INITED();79GET_STACK_TRACE_MALLOC;80return memprof_reallocarray(ptr, nmemb, size, &stack);81}82#endif // SANITIZER_INTERCEPT_REALLOCARRAY8384#if SANITIZER_INTERCEPT_MEMALIGN85INTERCEPTOR(void *, memalign, uptr boundary, uptr size) {86GET_STACK_TRACE_MALLOC;87return memprof_memalign(boundary, size, &stack, FROM_MALLOC);88}8990INTERCEPTOR(void *, __libc_memalign, uptr boundary, uptr size) {91GET_STACK_TRACE_MALLOC;92void *res = memprof_memalign(boundary, size, &stack, FROM_MALLOC);93DTLS_on_libc_memalign(res, size);94return res;95}96#endif // SANITIZER_INTERCEPT_MEMALIGN9798#if SANITIZER_INTERCEPT_ALIGNED_ALLOC99INTERCEPTOR(void *, aligned_alloc, uptr boundary, uptr size) {100GET_STACK_TRACE_MALLOC;101return memprof_aligned_alloc(boundary, size, &stack);102}103#endif // SANITIZER_INTERCEPT_ALIGNED_ALLOC104105INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {106return memprof_malloc_usable_size(ptr);107}108109#if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO110// We avoid including malloc.h for portability reasons.111// man mallinfo says the fields are "long", but the implementation uses int.112// It doesn't matter much -- we just need to make sure that the libc's mallinfo113// is not called.114struct fake_mallinfo {115int x[10];116};117118INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {119struct fake_mallinfo res;120REAL(memset)(&res, 0, sizeof(res));121return res;122}123124INTERCEPTOR(int, mallopt, int cmd, int value) { return 0; }125#endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO126127INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {128GET_STACK_TRACE_MALLOC;129return memprof_posix_memalign(memptr, alignment, size, &stack);130}131132INTERCEPTOR(void *, valloc, uptr size) {133GET_STACK_TRACE_MALLOC;134return memprof_valloc(size, &stack);135}136137#if SANITIZER_INTERCEPT_PVALLOC138INTERCEPTOR(void *, pvalloc, uptr size) {139GET_STACK_TRACE_MALLOC;140return memprof_pvalloc(size, &stack);141}142#endif // SANITIZER_INTERCEPT_PVALLOC143144INTERCEPTOR(void, malloc_stats, void) { __memprof_print_accumulated_stats(); }145146namespace __memprof {147void ReplaceSystemMalloc() {}148} // namespace __memprof149150151