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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/*
* Mlock Allocator
* (C) 2012,2014 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/locking_allocator.h>
#include <botan/mem_ops.h>
#include <algorithm>
#include <string>
#include <sys/mman.h>
#include <sys/resource.h>
namespace Botan {
namespace {
/**
* Requests for objects of sizeof(T) will be aligned at
* sizeof(T)*ALIGNMENT_MULTIPLE bytes.
*/
const size_t ALIGNMENT_MULTIPLE = 2;
size_t mlock_limit()
{
/*
* Linux defaults to only 64 KiB of mlockable memory per process
* (too small) but BSDs offer a small fraction of total RAM (more
* than we need). Bound the total mlock size to 512 KiB which is
* enough to run the entire test suite without spilling to non-mlock
* memory (and thus presumably also enough for many useful
* programs), but small enough that we should not cause problems
* even if many processes are mlocking on the same machine.
*/
const size_t MLOCK_UPPER_BOUND = 512*1024;
struct rlimit limits;
::getrlimit(RLIMIT_MEMLOCK, &limits);
if(limits.rlim_cur < limits.rlim_max)
{
limits.rlim_cur = limits.rlim_max;
::setrlimit(RLIMIT_MEMLOCK, &limits);
::getrlimit(RLIMIT_MEMLOCK, &limits);
}
return std::min<size_t>(limits.rlim_cur, MLOCK_UPPER_BOUND);
}
bool ptr_in_pool(const void* pool_ptr, size_t poolsize,
const void* buf_ptr, size_t bufsize)
{
const uintptr_t pool = reinterpret_cast<uintptr_t>(pool_ptr);
const uintptr_t buf = reinterpret_cast<uintptr_t>(buf_ptr);
if(buf < pool || buf >= pool + poolsize)
return false;
BOTAN_ASSERT(buf + bufsize <= pool + poolsize,
"Pointer does not partially overlap pool");
return true;
}
size_t padding_for_alignment(size_t offset, size_t desired_alignment)
{
size_t mod = offset % desired_alignment;
if(mod == 0)
return 0; // already right on
return desired_alignment - mod;
}
}
void* mlock_allocator::allocate(size_t num_elems, size_t elem_size)
{
if(!m_pool)
return nullptr;
const size_t n = num_elems * elem_size;
const size_t alignment = ALIGNMENT_MULTIPLE * elem_size;
if(n / elem_size != num_elems)
return nullptr; // overflow!
if(n > m_poolsize || n > BOTAN_MLOCK_ALLOCATOR_MAX_ALLOCATION)
return nullptr;
std::lock_guard<std::mutex> lock(m_mutex);
auto best_fit = m_freelist.end();
for(auto i = m_freelist.begin(); i != m_freelist.end(); ++i)
{
// If we have a perfect fit, use it immediately
if(i->second == n && (i->first % alignment) == 0)
{
const size_t offset = i->first;
m_freelist.erase(i);
clear_mem(m_pool + offset, n);
BOTAN_ASSERT((reinterpret_cast<size_t>(m_pool) + offset) % alignment == 0,
"Returning correctly aligned pointer");
return m_pool + offset;
}
if((i->second >= (n + padding_for_alignment(i->first, alignment)) &&
((best_fit == m_freelist.end()) || (best_fit->second > i->second))))
{
best_fit = i;
}
}
if(best_fit != m_freelist.end())
{
const size_t offset = best_fit->first;
const size_t alignment_padding = padding_for_alignment(offset, alignment);
best_fit->first += n + alignment_padding;
best_fit->second -= n + alignment_padding;
// Need to realign, split the block
if(alignment_padding)
{
/*
If we used the entire block except for small piece used for
alignment at the beginning, so just update the entry already
in place (as it is in the correct location), rather than
deleting the empty range and inserting the new one in the
same location.
*/
if(best_fit->second == 0)
{
best_fit->first = offset;
best_fit->second = alignment_padding;
}
else
m_freelist.insert(best_fit, std::make_pair(offset, alignment_padding));
}
clear_mem(m_pool + offset + alignment_padding, n);
BOTAN_ASSERT((reinterpret_cast<size_t>(m_pool) + offset + alignment_padding) % alignment == 0,
"Returning correctly aligned pointer");
return m_pool + offset + alignment_padding;
}
return nullptr;
}
bool mlock_allocator::deallocate(void* p, size_t num_elems, size_t elem_size)
{
if(!m_pool)
return false;
/*
We do not have to zero the memory here, as
secure_allocator::deallocate does that for all arguments before
invoking the deallocator (us or delete[])
*/
size_t n = num_elems * elem_size;
/*
We return nullptr in allocate if there was an overflow, so we
should never ever see an overflow in a deallocation.
*/
BOTAN_ASSERT(n / elem_size == num_elems,
"No overflow in deallocation");
if(!ptr_in_pool(m_pool, m_poolsize, p, n))
return false;
std::lock_guard<std::mutex> lock(m_mutex);
const size_t start = static_cast<byte*>(p) - m_pool;
auto comp = [](std::pair<size_t, size_t> x, std::pair<size_t, size_t> y){ return x.first < y.first; };
auto i = std::lower_bound(m_freelist.begin(), m_freelist.end(),
std::make_pair(start, 0), comp);
// try to merge with later block
if(i != m_freelist.end() && start + n == i->first)
{
i->first = start;
i->second += n;
n = 0;
}
// try to merge with previous block
if(i != m_freelist.begin())
{
auto prev = std::prev(i);
if(prev->first + prev->second == start)
{
if(n)
{
prev->second += n;
n = 0;
}
else
{
// merge adjoining
prev->second += i->second;
m_freelist.erase(i);
}
}
}
if(n != 0) // no merge possible?
m_freelist.insert(i, std::make_pair(start, n));
return true;
}
mlock_allocator::mlock_allocator() :
m_poolsize(mlock_limit()),
m_pool(nullptr)
{
#if !defined(MAP_NOCORE)
#define MAP_NOCORE 0
#endif
#if !defined(MAP_ANONYMOUS)
#define MAP_ANONYMOUS MAP_ANON
#endif
if(m_poolsize)
{
m_pool = static_cast<byte*>(
::mmap(
nullptr, m_poolsize,
PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_SHARED | MAP_NOCORE,
-1, 0));
if(m_pool == static_cast<byte*>(MAP_FAILED))
{
m_pool = nullptr;
throw std::runtime_error("Failed to mmap locking_allocator pool");
}
clear_mem(m_pool, m_poolsize);
if(::mlock(m_pool, m_poolsize) != 0)
{
::munmap(m_pool, m_poolsize);
m_pool = nullptr;
throw std::runtime_error("Could not mlock " + std::to_string(m_poolsize) + " bytes");
}
#if defined(MADV_DONTDUMP)
::madvise(m_pool, m_poolsize, MADV_DONTDUMP);
#endif
m_freelist.push_back(std::make_pair(0, m_poolsize));
}
}
mlock_allocator::~mlock_allocator()
{
if(m_pool)
{
clear_mem(m_pool, m_poolsize);
::munlock(m_pool, m_poolsize);
::munmap(m_pool, m_poolsize);
m_pool = nullptr;
}
}
mlock_allocator& mlock_allocator::instance()
{
static mlock_allocator mlock;
return mlock;
}
}
|