-
Notifications
You must be signed in to change notification settings - Fork 4
/
bigheap.hh
161 lines (135 loc) · 4.77 KB
/
bigheap.hh
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
/*
* FreeGuard: A Faster Secure Heap Allocator
* Copyright (C) 2017 Sam Silvestro, Hongyu Liu, Corey Crosser,
* Zhiqiang Lin, and Tongping Liu
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @file bigheap.hh: manages the mapping/unmapping of large objects
* @author Tongping Liu <http://www.cs.utsa.edu/~tongpingliu/>
* @author Sam Silvestro <sam.silvestro@utsa.edu>
*/
#ifndef __BIGHEAP_HH__
#define __BIGHEAP_HH__
#include "real.hh"
#include "xthread.hh"
#include "mm.hh"
#include "xdefines.hh"
#include "errmsg.hh"
class BigHeap {
private:
class bigObjectStatus {
public:
void * actualStart;
void * userStart;
// pageUpSize and usableSize are usually the same thing,
// unless an aligned allocation function was used (e.g.,
// memalign or posix_memalign). In this instance, all
// three size values could be different.
size_t size;
size_t pageUpSize;
size_t usableSize;
#ifdef DETECT_UAF
unsigned long long freedtime;
#endif
};
public:
static BigHeap &getInstance() {
static char buf[sizeof(BigHeap)];
static BigHeap* theOneTrueObject = new (buf) BigHeap();
return *theOneTrueObject;
}
// Initialization of the Big Heap
void initBigHeap(void) {
// Initialize the spin_lock
pthread_spin_init(&_spin_lock, PTHREAD_PROCESS_PRIVATE);
// Initialize the hash map
_xmap.initialize(HashFuncs::hashAddr, HashFuncs::compareAddr, THREAD_MAP_SIZE);
}
size_t getUsableSize(void * ptr) {
bigObjectStatus * objStatus;
if(!_xmap.find(ptr, sizeof(void *), &objStatus)) {
return 0;
}
return objStatus->usableSize;
}
inline void * allocateAtBigHeap(size_t size) {
return allocateAlignedAtBigHeap(16, size);
}
// For big objects, we don't have the quarantine list.
// Actually, the size information will be kept until new allocation is
void * allocateAlignedAtBigHeap(size_t alignment, size_t size) {
assert(IF_CANARY_CONDITION);
size_t pageUpSize = alignup(alignment + size, PageSize);
size_t slackSize = pageUpSize - size;
bigObjectStatus * objStatus = (bigObjectStatus *)HeapAllocator::allocate(sizeof(bigObjectStatus));
void * ptr = MM::mmapAllocatePrivate(pageUpSize, NULL);
uintptr_t userStartPtr = (uintptr_t)ptr + slackSize;
size_t residualBytes = userStartPtr % alignment;
userStartPtr -= residualBytes;
acquireGlobalLock();
_xmap.insert((void *)userStartPtr, sizeof(void *), objStatus);
releaseGlobalLock();
objStatus->actualStart = ptr;
objStatus->userStart = (void *)userStartPtr;
objStatus->pageUpSize = pageUpSize;
objStatus->usableSize = size + residualBytes;
objStatus->size = size;
PRDBG("BigHeap returning 0x%lx (begins @ %p), size %zu (actual %zu, usable %zu)",
userStartPtr, ptr, size, pageUpSize, objStatus->usableSize);
return (void *)userStartPtr;
}
size_t getObjectSize(void * addr) {
return getUsableSize(addr);
}
bool isLargeObject(void * addr) {
bigObjectStatus * objStatus;
return(_xmap.find(addr, sizeof(void *), &objStatus));
}
void deallocateToBigHeap(void * ptr) {
bigObjectStatus * objStatus;
if(!_xmap.find(ptr, sizeof(void *), &objStatus)) {
PRINT("invalid or double free on address %p", ptr);
printCallStack();
exit(-1);
}
//PRDBG("BigHeap freed %p (begins @ %p), size %zu (actual %zu), objStatus @ %p",
// ptr, objStatus->start, objStatus->size, objStatus->pageUpSize, objStatus);
acquireGlobalLock();
_xmap.erase(ptr, sizeof(void *));
releaseGlobalLock();
MM::mmapDeallocate(objStatus->actualStart, objStatus->pageUpSize);
HeapAllocator::deallocate(objStatus);
}
void acquireGlobalLock() {
spin_lock();
}
void releaseGlobalLock() {
spin_unlock();
}
private:
size_t _bigObjectStatusSize = sizeof(bigObjectStatus);
unsigned _bigObjectStatusSizeShiftBits = LOG2(_bigObjectStatusSize);
pthread_spinlock_t _spin_lock;
typedef HashMap<void *, bigObjectStatus *, HeapAllocator> objectHashMap;
objectHashMap _xmap;
inline void spin_lock() {
pthread_spin_lock(&_spin_lock);
}
inline void spin_unlock() {
pthread_spin_unlock(&_spin_lock);
}
};
#endif // __BIGHEAP_HH__