-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbigheap.hh
139 lines (118 loc) · 4.04 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
/*
* 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 * start;
size_t size;
size_t pageUpSize;
#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);
}
// For big objects, we don't have the quarantine list.
// Actually, the size information will be kept until new allocation is
void * allocateAtBigHeap(size_t size) {
assert(IF_CANARY_CONDITION);
size_t pageUpSize = alignup(size, PageSize);
size_t diff = pageUpSize - size;
bigObjectStatus * objStatus = (bigObjectStatus *)HeapAllocator::allocate(sizeof(bigObjectStatus));
void * ptr = MM::mmapAllocatePrivate(pageUpSize, NULL);
void * objStartPtr = (void *)((char *)ptr + diff);
acquireGlobalLock();
_xmap.insert(objStartPtr, sizeof(void *), objStatus);
releaseGlobalLock();
objStatus->start = ptr;
objStatus->pageUpSize = pageUpSize;
objStatus->size = size;
//PRDBG("BigHeap returning %p (begins @ %p), size %zu (actual %zu)", objStartPtr, ptr, size, pageUpSize);
return objStartPtr;
}
size_t getObjectSize(void * addr) {
bigObjectStatus * objStatus;
if(!_xmap.find(addr, sizeof(void *), &objStatus)) {
return -1;
}
return objStatus->size;
}
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->start, 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__