1 //===-- sanitizer_allocator64_testlib.cc ----------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // Malloc replacement library based on CombinedAllocator.
10 // The primary purpose of this file is an end-to-end integration test
11 // for CombinedAllocator.
12 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_allocator64.h"
14 #include <stddef.h>
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <assert.h>
18
19 namespace {
20 static const uptr kAllocatorSpace = 0x600000000000ULL;
21 static const uptr kAllocatorSize = 0x10000000000; // 1T.
22
23 typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, 16,
24 DefaultSizeClassMap> PrimaryAllocator;
25 typedef SizeClassAllocatorLocalCache<PrimaryAllocator::kNumClasses,
26 PrimaryAllocator> AllocatorCache;
27 typedef LargeMmapAllocator SecondaryAllocator;
28 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
29 SecondaryAllocator> Allocator;
30
31 static THREADLOCAL AllocatorCache cache;
32 static Allocator allocator;
33
34 static int inited = 0;
35
36 __attribute__((constructor))
Init()37 void Init() {
38 if (inited) return;
39 inited = true; // this must happen before any threads are created.
40 allocator.Init();
41 }
42
43 } // namespace
44
45 namespace __sanitizer {
Die()46 void NORETURN Die() {
47 _exit(77);
48 }
CheckFailed(const char * file,int line,const char * cond,u64 v1,u64 v2)49 void NORETURN CheckFailed(const char *file, int line, const char *cond,
50 u64 v1, u64 v2) {
51 fprintf(stderr, "CheckFailed: %s:%d %s (%lld %lld)\n",
52 file, line, cond, v1, v2);
53 Die();
54 }
55 }
56
57 #if 1
58 extern "C" {
malloc(size_t size)59 void *malloc(size_t size) {
60 Init();
61 assert(inited);
62 return allocator.Allocate(&cache, size, 8);
63 }
64
free(void * p)65 void free(void *p) {
66 assert(inited);
67 allocator.Deallocate(&cache, p);
68 }
69
calloc(size_t nmemb,size_t size)70 void *calloc(size_t nmemb, size_t size) {
71 assert(inited);
72 return allocator.Allocate(&cache, nmemb * size, 8, /*cleared=*/true);
73 }
74
realloc(void * p,size_t new_size)75 void *realloc(void *p, size_t new_size) {
76 assert(inited);
77 return allocator.Reallocate(&cache, p, new_size, 8);
78 }
79
memalign()80 void *memalign() { assert(0); }
81
posix_memalign(void ** memptr,size_t alignment,size_t size)82 int posix_memalign(void **memptr, size_t alignment, size_t size) {
83 *memptr = allocator.Allocate(&cache, size, alignment);
84 CHECK_EQ(((uptr)*memptr & (alignment - 1)), 0);
85 return 0;
86 }
87
valloc(size_t size)88 void *valloc(size_t size) {
89 assert(inited);
90 return allocator.Allocate(&cache, size, kPageSize);
91 }
92
pvalloc(size_t size)93 void *pvalloc(size_t size) {
94 assert(inited);
95 if (size == 0) size = kPageSize;
96 return allocator.Allocate(&cache, size, kPageSize);
97 }
98 }
99 #endif
100