1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
//===-- sanitizer_allocator_testlib.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
//
//===----------------------------------------------------------------------===//
// Malloc replacement library based on CombinedAllocator.
// The primary purpose of this file is an end-to-end integration test
// for CombinedAllocator.
//===----------------------------------------------------------------------===//
/* Usage:
clang++ -std=c++11 -fno-exceptions -g -fPIC -I. -I../include -Isanitizer \
sanitizer_common/tests/sanitizer_allocator_testlib.cpp \
$(\ls sanitizer_common/sanitizer_*.cpp | grep -v sanitizer_common_nolibc.cpp) \
sanitizer_common/sanitizer_linux_x86_64.S \
-shared -lpthread -o testmalloc.so
LD_PRELOAD=`pwd`/testmalloc.so /your/app
*/
#include "sanitizer_common/sanitizer_allocator.h"
#include "sanitizer_common/sanitizer_common.h"
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#ifndef SANITIZER_MALLOC_HOOK
# define SANITIZER_MALLOC_HOOK(p, s)
#endif
#ifndef SANITIZER_FREE_HOOK
# define SANITIZER_FREE_HOOK(p)
#endif
static const uptr kAllocatorSpace = 0x600000000000ULL;
static const uptr kAllocatorSize = 0x10000000000ULL; // 1T.
struct __AP64 {
static const uptr kSpaceBeg = ~(uptr)0;
static const uptr kSpaceSize = kAllocatorSize;
static const uptr kMetadataSize = 0;
typedef CompactSizeClassMap SizeClassMap;
typedef NoOpMapUnmapCallback MapUnmapCallback;
static const uptr kFlags =
SizeClassAllocator64FlagMasks::kRandomShuffleChunks;
};
namespace {
typedef SizeClassAllocator64<__AP64> PrimaryAllocator;
typedef CombinedAllocator<PrimaryAllocator> Allocator;
typedef Allocator::AllocatorCache AllocatorCache;
static Allocator allocator;
static bool global_inited;
static THREADLOCAL AllocatorCache cache;
static THREADLOCAL bool thread_inited;
static pthread_key_t pkey;
static void thread_dtor(void *v) {
if ((uptr)v != 3) {
pthread_setspecific(pkey, (void*)((uptr)v + 1));
return;
}
allocator.SwallowCache(&cache);
}
static size_t GetRss() {
if (FILE *f = fopen("/proc/self/statm", "r")) {
size_t size = 0, rss = 0;
fscanf(f, "%zd %zd", &size, &rss);
fclose(f);
return rss << 12; // rss is in pages.
}
return 0;
}
struct AtExit {
~AtExit() {
allocator.PrintStats();
Printf("RSS: %zdM\n", GetRss() >> 20);
}
};
static AtExit at_exit;
static void NOINLINE thread_init() {
if (!global_inited) {
global_inited = true;
allocator.Init(false /*may_return_null*/);
pthread_key_create(&pkey, thread_dtor);
}
thread_inited = true;
pthread_setspecific(pkey, (void*)1);
cache.Init(nullptr);
}
} // namespace
extern "C" {
void *malloc(size_t size) {
if (UNLIKELY(!thread_inited))
thread_init();
void *p = allocator.Allocate(&cache, size, 8);
SANITIZER_MALLOC_HOOK(p, size);
return p;
}
void free(void *p) {
if (UNLIKELY(!thread_inited))
thread_init();
SANITIZER_FREE_HOOK(p);
allocator.Deallocate(&cache, p);
}
void *calloc(size_t nmemb, size_t size) {
if (UNLIKELY(!thread_inited))
thread_init();
size *= nmemb;
void *p = allocator.Allocate(&cache, size, 8, false);
memset(p, 0, size);
SANITIZER_MALLOC_HOOK(p, size);
return p;
}
void *realloc(void *p, size_t size) {
if (UNLIKELY(!thread_inited))
thread_init();
if (p) {
SANITIZER_FREE_HOOK(p);
}
p = allocator.Reallocate(&cache, p, size, 8);
if (p) {
SANITIZER_MALLOC_HOOK(p, size);
}
return p;
}
#if SANITIZER_INTERCEPT_MEMALIGN
void *memalign(size_t alignment, size_t size) {
if (UNLIKELY(!thread_inited))
thread_init();
void *p = allocator.Allocate(&cache, size, alignment);
SANITIZER_MALLOC_HOOK(p, size);
return p;
}
#endif // SANITIZER_INTERCEPT_MEMALIGN
int posix_memalign(void **memptr, size_t alignment, size_t size) {
if (UNLIKELY(!thread_inited))
thread_init();
*memptr = allocator.Allocate(&cache, size, alignment);
SANITIZER_MALLOC_HOOK(*memptr, size);
return 0;
}
void *valloc(size_t size) {
if (UNLIKELY(!thread_inited))
thread_init();
if (size == 0)
size = GetPageSizeCached();
void *p = allocator.Allocate(&cache, size, GetPageSizeCached());
SANITIZER_MALLOC_HOOK(p, size);
return p;
}
#if SANITIZER_INTERCEPT_CFREE
void cfree(void *p) ALIAS("free");
#endif // SANITIZER_INTERCEPT_CFREE
#if SANITIZER_INTERCEPT_PVALLOC
void *pvalloc(size_t size) ALIAS("valloc");
#endif // SANITIZER_INTERCEPT_PVALLOC
#if SANITIZER_INTERCEPT_MEMALIGN
void *__libc_memalign(size_t alignment, size_t size) ALIAS("memalign");
#endif // SANITIZER_INTERCEPT_MEMALIGN
void malloc_usable_size() {
}
#if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
void mallinfo() {
}
void mallopt() {
}
#endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
} // extern "C"
namespace std {
struct nothrow_t;
}
void *operator new(size_t size) ALIAS("malloc");
void *operator new[](size_t size) ALIAS("malloc");
void *operator new(size_t size, std::nothrow_t const&) ALIAS("malloc");
void *operator new[](size_t size, std::nothrow_t const&) ALIAS("malloc");
void operator delete(void *ptr) throw() ALIAS("free");
void operator delete[](void *ptr) throw() ALIAS("free");
void operator delete(void *ptr, std::nothrow_t const&) ALIAS("free");
void operator delete[](void *ptr, std::nothrow_t const&) ALIAS("free");
|