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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/*
* (C) 2018 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/internal/mem_pool.h>
#include <botan/mem_ops.h>
namespace Botan {
/*
* Memory pool theory of operation
*
* This allocator is not useful for general purpose but works well within the
* context of allocating cryptographic keys. It makes several assumptions which
* don't work for a malloc but simplify and speed up the implementation:
*
* - There is a single fixed block of memory, which cannot be expanded. This is
* the block that was allocated, mlocked and passed to the Memory_Pool
* constructor. It is assumed to be page-aligned.
*
* - The allocator is allowed to return null anytime it feels like not servicing
* a request, in which case the request will be sent to calloc instead. In
* particular values which are too small or too large are given to calloc.
*
* - Most allocations are powers of 2, the remainder are usually a multiple of 4
* or 8.
*
* - Free requests include the size of the allocation, so there is no need to
* track this within the pool.
*
* - Alignment is important to the caller. For this allocator, any allocation of
* size N is aligned evenly at N bytes.
*
* The block of memory is split up into pages. Initially each page is in the free
* page list. Each page is used for just one size of allocation, with requests
* bucketed into a small number of common sizes. If the allocation would be too
* big, too small, or with too much slack, it is rejected by the pool.
*
* The free list is maintained by a bitmap, one per page/Bucket. Since each
* Bucket only maintains objects of a single size, each bit set or clear
* indicates the status of one object.
*
* An allocation walks the list of buckets and asks each in turn if there is
* space. If a Bucket does not have any space, it sets a boolean flag m_is_full
* so that it does not need to rescan when asked again. The flag is cleared on
* first free from that bucket. If no bucket has space, but there are some free
* pages left, a free page is claimed as a new Bucket for that size. In this case
* it is pushed to the front of the list so it is first in line to service new
* requests.
*
* A deallocation also walks the list of buckets for the size and asks each
* Bucket in turn if it recognizes the pointer. When a Bucket becomes empty as a
* result of a deallocation, it is recycled back into the free pool. When this
* happens, the Buckets pointer goes to the end of the free list. This will delay
* slightly the reuse of this page, which may offer some slight help wrt use
* after free issues.
*
* It may be worthwhile to optimize deallocation by storing the Buckets in order
* (by pointer value) which would allow binary search to find the owning bucket.
*/
namespace {
size_t choose_bucket(size_t n)
{
const size_t MINIMUM_ALLOCATION = 16;
const size_t MAXIMUM_ALLOCATION = 512;
const size_t MAXIMUM_SLACK = 31;
if(n < MINIMUM_ALLOCATION|| n > MAXIMUM_ALLOCATION)
return 0;
// Need to tune these
const size_t buckets[] = {
16, 24, 32, 48, 64, 80, 96, 112, 128, 160, 192, 256, 320, 384, 448, 512, 0
};
for(size_t i = 0; buckets[i]; ++i)
{
if(n <= buckets[i])
{
const size_t slack = buckets[i] - n;
if(slack > MAXIMUM_SLACK)
return 0;
return buckets[i];
}
}
return 0;
}
inline 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);
return (buf >= pool) && (buf + bufsize <= pool + poolsize);
}
// return index of first set bit
template<typename T>
size_t find_set_bit(T b)
{
size_t s = 8*sizeof(T) / 2;
size_t bit = 0;
// In this context we don't need to be const-time
while(s > 0)
{
const T mask = (static_cast<T>(1) << s) - 1;
if((b & mask) == 0)
{
bit += s;
b >>= s;
}
s /= 2;
}
return bit;
}
class BitMap final
{
public:
BitMap(size_t bits) : m_len(bits)
{
m_bits.resize((bits + BITMASK_BITS - 1) / BITMASK_BITS);
m_main_mask = ~static_cast<bitmask_type>(0);
m_last_mask = m_main_mask;
if(bits % BITMASK_BITS != 0)
m_last_mask = (static_cast<bitmask_type>(1) << (bits % BITMASK_BITS)) - 1;
}
bool find_free(size_t* bit);
void free(size_t bit);
bool empty() const;
private:
#if defined(BOTAN_ENABLE_DEBUG_ASSERTS)
typedef uint8_t bitmask_type;
enum { BITMASK_BITS = 8 };
#else
typedef word bitmask_type;
enum { BITMASK_BITS = BOTAN_MP_WORD_BITS };
#endif
size_t m_len;
bitmask_type m_main_mask;
bitmask_type m_last_mask;
std::vector<bitmask_type> m_bits;
};
bool BitMap::find_free(size_t* bit)
{
for(size_t i = 0; i != m_bits.size(); ++i)
{
const bitmask_type mask = (i == m_bits.size() - 1) ? m_last_mask : m_main_mask;
if((m_bits[i] & mask) != mask)
{
size_t free_bit = find_set_bit(~m_bits[i]);
const size_t bmask = static_cast<bitmask_type>(1) << (free_bit % BITMASK_BITS);
BOTAN_ASSERT_NOMSG((m_bits[i] & bmask) == 0);
m_bits[i] |= bmask;
*bit = BITMASK_BITS*i + free_bit;
return true;
}
}
return false;
}
void BitMap::free(size_t bit)
{
BOTAN_ASSERT_NOMSG(bit <= m_len);
const size_t w = bit / BITMASK_BITS;
BOTAN_ASSERT_NOMSG(w < m_bits.size());
const size_t mask = static_cast<bitmask_type>(1) << (bit % BITMASK_BITS);
m_bits[w] = m_bits[w] & (~mask);
}
bool BitMap::empty() const
{
for(size_t i = 0; i != m_bits.size(); ++i)
{
if(m_bits[i] != 0)
{
return false;
}
}
return true;
}
}
class Bucket final
{
public:
Bucket(uint8_t* mem, size_t mem_size, size_t item_size) :
m_item_size(item_size),
m_page_size(mem_size),
m_range(mem),
m_bitmap(mem_size / item_size),
m_is_full(false)
{
}
uint8_t* alloc();
bool free(void* p);
bool in_this_bucket(void* p) const
{
return ptr_in_pool(m_range, m_page_size, p, m_item_size);
}
bool empty() const
{
return m_bitmap.empty();
}
uint8_t* ptr() const
{
return m_range;
}
private:
size_t m_item_size;
size_t m_page_size;
uint8_t* m_range;
BitMap m_bitmap;
bool m_is_full;
};
uint8_t* Bucket::alloc()
{
if(m_is_full)
{
// I know I am full
return nullptr;
}
size_t offset;
if(!m_bitmap.find_free(&offset))
{
// I just found out I am full
m_is_full = true;
return nullptr;
}
BOTAN_ASSERT(offset * m_item_size < m_page_size, "Offset is in range");
return m_range + m_item_size*offset;
}
bool Bucket::free(void* p)
{
if(!in_this_bucket(p))
return false;
const size_t offset = (reinterpret_cast<uintptr_t>(p) - reinterpret_cast<uintptr_t>(m_range)) / m_item_size;
m_bitmap.free(offset);
m_is_full = false;
return true;
}
Memory_Pool::Memory_Pool(uint8_t* pool, size_t num_pages, size_t page_size) :
m_page_size(page_size)
{
BOTAN_ARG_CHECK(pool != nullptr, "Memory_Pool pool was null");
// This is basically just to verify that the range is valid
clear_mem(pool, num_pages * page_size);
m_pool = pool;
m_pool_size = num_pages * page_size;
for(size_t i = 0; i != num_pages; ++i)
{
m_free_pages.push_back(pool + page_size*i);
}
}
Memory_Pool::~Memory_Pool()
{
}
void* Memory_Pool::allocate(size_t n)
{
if(n > m_page_size)
return nullptr;
const size_t n_bucket = choose_bucket(n);
if(n_bucket == 0)
return nullptr;
lock_guard_type<mutex_type> lock(m_mutex);
std::deque<Bucket>& buckets = m_buckets_for[n_bucket];
for(auto& bucket : buckets)
{
if(uint8_t* p = bucket.alloc())
return p;
// If the bucket is full, maybe move it to the end of the list?
// Otoh bucket search should be very fast
}
if(m_free_pages.size() > 0)
{
uint8_t* ptr = m_free_pages[0];
m_free_pages.pop_front();
buckets.push_front(Bucket(ptr, m_page_size, n_bucket));
void* p = buckets[0].alloc();
BOTAN_ASSERT_NOMSG(p != nullptr);
return p;
}
// out of room
return nullptr;
}
bool Memory_Pool::deallocate(void* p, size_t len) noexcept
{
if(!ptr_in_pool(m_pool, m_pool_size, p, len))
return false;
const size_t n_bucket = choose_bucket(len);
if(n_bucket != 0)
{
/*
Zero also any trailing bytes, which should not have been written to,
but maybe the user was bad and wrote past the end.
*/
std::memset(p, 0, n_bucket);
lock_guard_type<mutex_type> lock(m_mutex);
std::deque<Bucket>& buckets = m_buckets_for[n_bucket];
for(size_t i = 0; i != buckets.size(); ++i)
{
Bucket& bucket = buckets[i];
if(bucket.free(p) == false)
continue;
if(bucket.empty())
{
m_free_pages.push_back(bucket.ptr());
if(i != buckets.size() - 1)
std::swap(buckets.back(), buckets[i]);
buckets.pop_back();
}
return true;
}
}
/*
* If we reach this point, something bad has occurred. We know the pointer
* passed in is inside the range of the pool, but no bucket recognized it,
* either because n_bucket was zero or no Bucket::free call returned true. Our
* options (since this function is noexcept) are to either ignore it and
* return false, ignore it and return true, or to crash.
*
* Returning false means the pointer will be considered a standard heap
* pointer and passed on to free, which will almost certainly cause a heap
* corruption.
*
* There is some robustness argument for just memseting the pointer and
* returning true. In this case it will be assumed to be freed. But, since
* this pointer *is* within the range of the pool, but no bucket claimed it,
* that seems to indicate some existing allocator corruption.
*
* Crashing is bad, heap corruption is worse. So we crash, in this case by
* calling BOTAN_ASSERT and letting the exception handling mechanism
* terminate the process.
*/
BOTAN_ASSERT(false, "Pointer from pool, but no bucket recognized it");
return false;
}
}
|