Path: blob/main/contrib/llvm-project/compiler-rt/lib/nsan/nsan_malloc_linux.cpp
35233 views
//===- nsan_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// Interceptors for memory allocation functions on ELF OSes.9//10//===----------------------------------------------------------------------===//1112#include "interception/interception.h"13#include "nsan/nsan.h"14#include "sanitizer_common/sanitizer_allocator_dlsym.h"15#include "sanitizer_common/sanitizer_common.h"16#include "sanitizer_common/sanitizer_platform.h"17#include "sanitizer_common/sanitizer_platform_interceptors.h"1819#if !SANITIZER_APPLE && !SANITIZER_WINDOWS20using namespace __sanitizer;21using __nsan::nsan_initialized;2223namespace {24struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {25static bool UseImpl() { return !nsan_initialized; }26};27} // namespace2829INTERCEPTOR(void *, aligned_alloc, uptr align, uptr size) {30void *res = REAL(aligned_alloc)(align, size);31if (res)32__nsan_set_value_unknown(static_cast<u8 *>(res), size);33return res;34}3536INTERCEPTOR(void *, calloc, uptr nmemb, uptr size) {37if (DlsymAlloc::Use())38return DlsymAlloc::Callocate(nmemb, size);3940void *res = REAL(calloc)(nmemb, size);41if (res)42__nsan_set_value_unknown(static_cast<u8 *>(res), nmemb * size);43return res;44}4546INTERCEPTOR(void, free, void *ptr) {47if (DlsymAlloc::PointerIsMine(ptr))48return DlsymAlloc::Free(ptr);49REAL(free)(ptr);50}5152INTERCEPTOR(void *, malloc, uptr size) {53if (DlsymAlloc::Use())54return DlsymAlloc::Allocate(size);55void *res = REAL(malloc)(size);56if (res)57__nsan_set_value_unknown(static_cast<u8 *>(res), size);58return res;59}6061INTERCEPTOR(void *, realloc, void *ptr, uptr size) {62if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))63return DlsymAlloc::Realloc(ptr, size);64void *res = REAL(realloc)(ptr, size);65// TODO: We might want to copy the types from the original allocation66// (although that would require that we know its size).67if (res)68__nsan_set_value_unknown(static_cast<u8 *>(res), size);69return res;70}7172#if SANITIZER_INTERCEPT_REALLOCARRAY73INTERCEPTOR(void *, reallocarray, void *ptr, uptr nmemb, uptr size) {74void *res = REAL(reallocarray)(ptr, nmemb, size);75if (res)76__nsan_set_value_unknown(static_cast<u8 *>(res), nmemb * size);77return res;78}79#endif // SANITIZER_INTERCEPT_REALLOCARRAY8081INTERCEPTOR(int, posix_memalign, void **memptr, uptr align, uptr size) {82int res = REAL(posix_memalign)(memptr, align, size);83if (res == 0 && *memptr)84__nsan_set_value_unknown(static_cast<u8 *>(*memptr), size);85return res;86}8788// Deprecated allocation functions (memalign, etc).89#if SANITIZER_INTERCEPT_MEMALIGN90INTERCEPTOR(void *, memalign, uptr align, uptr size) {91void *const res = REAL(memalign)(align, size);92if (res)93__nsan_set_value_unknown(static_cast<u8 *>(res), size);94return res;95}9697INTERCEPTOR(void *, __libc_memalign, uptr align, uptr size) {98void *const res = REAL(__libc_memalign)(align, size);99if (res)100__nsan_set_value_unknown(static_cast<u8 *>(res), size);101return res;102}103#endif104105void __nsan::InitializeMallocInterceptors() {106INTERCEPT_FUNCTION(aligned_alloc);107INTERCEPT_FUNCTION(calloc);108INTERCEPT_FUNCTION(free);109INTERCEPT_FUNCTION(malloc);110INTERCEPT_FUNCTION(posix_memalign);111INTERCEPT_FUNCTION(realloc);112#if SANITIZER_INTERCEPT_REALLOCARRAY113INTERCEPT_FUNCTION(reallocarray);114#endif115116#if SANITIZER_INTERCEPT_MEMALIGN117INTERCEPT_FUNCTION(memalign);118INTERCEPT_FUNCTION(__libc_memalign);119#endif120}121122#endif123124125