blob: 456b47137d6b97e04ad8c11eda4030a4616db553 [file] [log] [blame]
Paul Bakker6e339b52013-07-03 13:37:05 +02001/*
2 * Buffer-based memory allocator
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker6e339b52013-07-03 13:37:05 +02005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker6e339b52013-07-03 13:37:05 +02007 *
Paul Bakker6e339b52013-07-03 13:37:05 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker6e339b52013-07-03 13:37:05 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020028
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010029#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010030#include "polarssl/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020031
32#include <string.h>
33
Rich Evans00ab4702015-02-06 13:43:58 +000034#if defined(POLARSSL_PLATFORM_C)
35#include "polarssl/platform.h"
36#else
Paul Bakker6e339b52013-07-03 13:37:05 +020037#include <stdio.h>
Rich Evans77d36382015-01-30 12:12:11 +000038#define polarssl_exit exit
Rich Evans00ab4702015-02-06 13:43:58 +000039#define polarssl_fprintf fprintf
40#endif /* POLARSSL_PLATFORM_C */
Rich Evans77d36382015-01-30 12:12:11 +000041
Paul Bakker6e339b52013-07-03 13:37:05 +020042#if defined(POLARSSL_MEMORY_BACKTRACE)
43#include <execinfo.h>
44#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020045
Paul Bakker1337aff2013-09-29 14:45:34 +020046#if defined(POLARSSL_THREADING_C)
47#include "polarssl/threading.h"
48#endif
49
Paul Bakker34617722014-06-13 17:20:13 +020050/* Implementation that should never be optimized out by the compiler */
51static void polarssl_zeroize( void *v, size_t n ) {
52 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
53}
54
Paul Bakker6e339b52013-07-03 13:37:05 +020055#define MAGIC1 0xFF00AA55
56#define MAGIC2 0xEE119966
57#define MAX_BT 20
58
59typedef struct _memory_header memory_header;
60struct _memory_header
61{
62 size_t magic1;
63 size_t size;
64 size_t alloc;
65 memory_header *prev;
66 memory_header *next;
Paul Bakker1ef120f2013-07-03 17:20:39 +020067 memory_header *prev_free;
68 memory_header *next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020069#if defined(POLARSSL_MEMORY_BACKTRACE)
70 char **trace;
71 size_t trace_count;
72#endif
73 size_t magic2;
74};
75
76typedef struct
77{
78 unsigned char *buf;
79 size_t len;
80 memory_header *first;
Paul Bakker1ef120f2013-07-03 17:20:39 +020081 memory_header *first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020082 size_t current_alloc_size;
83 int verify;
Paul Bakker891998e2013-07-03 14:45:05 +020084#if defined(POLARSSL_MEMORY_DEBUG)
85 size_t malloc_count;
86 size_t free_count;
87 size_t total_used;
88 size_t maximum_used;
89 size_t header_count;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +010090 size_t maximum_header_count;
Paul Bakker891998e2013-07-03 14:45:05 +020091#endif
Paul Bakker1337aff2013-09-29 14:45:34 +020092#if defined(POLARSSL_THREADING_C)
93 threading_mutex_t mutex;
94#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020095}
96buffer_alloc_ctx;
97
98static buffer_alloc_ctx heap;
99
100#if defined(POLARSSL_MEMORY_DEBUG)
101static void debug_header( memory_header *hdr )
102{
103#if defined(POLARSSL_MEMORY_BACKTRACE)
104 size_t i;
105#endif
106
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200107 polarssl_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
108 "ALLOC(%zu), SIZE(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100109 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
110 hdr->alloc, hdr->size );
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200111 polarssl_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100112 (size_t) hdr->prev_free, (size_t) hdr->next_free );
Paul Bakker6e339b52013-07-03 13:37:05 +0200113
114#if defined(POLARSSL_MEMORY_BACKTRACE)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100115 polarssl_fprintf( stderr, "TRACE: \n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200116 for( i = 0; i < hdr->trace_count; i++ )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100117 polarssl_fprintf( stderr, "%s\n", hdr->trace[i] );
118 polarssl_fprintf( stderr, "\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200119#endif
120}
121
122static void debug_chain()
123{
124 memory_header *cur = heap.first;
125
Paul Bakker7dc4c442014-02-01 22:50:26 +0100126 polarssl_fprintf( stderr, "\nBlock list\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200127 while( cur != NULL )
128 {
129 debug_header( cur );
Paul Bakker6e339b52013-07-03 13:37:05 +0200130 cur = cur->next;
131 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200132
Paul Bakker7dc4c442014-02-01 22:50:26 +0100133 polarssl_fprintf( stderr, "Free list\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200134 cur = heap.first_free;
135
136 while( cur != NULL )
137 {
138 debug_header( cur );
139 cur = cur->next_free;
140 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200141}
142#endif /* POLARSSL_MEMORY_DEBUG */
143
144static int verify_header( memory_header *hdr )
145{
146 if( hdr->magic1 != MAGIC1 )
147 {
148#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100149 polarssl_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200150#endif
151 return( 1 );
152 }
153
154 if( hdr->magic2 != MAGIC2 )
155 {
156#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100157 polarssl_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200158#endif
159 return( 1 );
160 }
161
162 if( hdr->alloc > 1 )
163 {
164#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100165 polarssl_fprintf( stderr, "FATAL: alloc has illegal value\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200166#endif
167 return( 1 );
168 }
169
Paul Bakker1ef120f2013-07-03 17:20:39 +0200170 if( hdr->prev != NULL && hdr->prev == hdr->next )
171 {
172#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100173 polarssl_fprintf( stderr, "FATAL: prev == next\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200174#endif
175 return( 1 );
176 }
177
178 if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
179 {
180#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100181 polarssl_fprintf( stderr, "FATAL: prev_free == next_free\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200182#endif
183 return( 1 );
184 }
185
Paul Bakker6e339b52013-07-03 13:37:05 +0200186 return( 0 );
187}
188
189static int verify_chain()
190{
191 memory_header *prv = heap.first, *cur = heap.first->next;
192
193 if( verify_header( heap.first ) != 0 )
194 {
195#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100196 polarssl_fprintf( stderr, "FATAL: verification of first header "
197 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200198#endif
199 return( 1 );
200 }
201
202 if( heap.first->prev != NULL )
203 {
204#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100205 polarssl_fprintf( stderr, "FATAL: verification failed: "
206 "first->prev != NULL\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200207#endif
208 return( 1 );
209 }
210
211 while( cur != NULL )
212 {
213 if( verify_header( cur ) != 0 )
214 {
215#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100216 polarssl_fprintf( stderr, "FATAL: verification of header "
217 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200218#endif
219 return( 1 );
220 }
221
222 if( cur->prev != prv )
223 {
224#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100225 polarssl_fprintf( stderr, "FATAL: verification failed: "
226 "cur->prev != prv\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200227#endif
228 return( 1 );
229 }
230
231 prv = cur;
232 cur = cur->next;
233 }
234
235 return( 0 );
236}
237
238static void *buffer_alloc_malloc( size_t len )
239{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200240 memory_header *new, *cur = heap.first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200241 unsigned char *p;
242#if defined(POLARSSL_MEMORY_BACKTRACE)
243 void *trace_buffer[MAX_BT];
244 size_t trace_cnt;
245#endif
246
247 if( heap.buf == NULL || heap.first == NULL )
248 return( NULL );
249
250 if( len % POLARSSL_MEMORY_ALIGN_MULTIPLE )
251 {
252 len -= len % POLARSSL_MEMORY_ALIGN_MULTIPLE;
253 len += POLARSSL_MEMORY_ALIGN_MULTIPLE;
254 }
255
256 // Find block that fits
257 //
258 while( cur != NULL )
259 {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200260 if( cur->size >= len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200261 break;
262
Paul Bakker1ef120f2013-07-03 17:20:39 +0200263 cur = cur->next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200264 }
265
266 if( cur == NULL )
267 return( NULL );
268
Paul Bakker1ef120f2013-07-03 17:20:39 +0200269 if( cur->alloc != 0 )
270 {
271#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100272 polarssl_fprintf( stderr, "FATAL: block in free_list but allocated "
273 "data\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200274#endif
Rich Evans77d36382015-01-30 12:12:11 +0000275 polarssl_exit( 1 );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200276 }
277
Paul Bakker891998e2013-07-03 14:45:05 +0200278#if defined(POLARSSL_MEMORY_DEBUG)
279 heap.malloc_count++;
280#endif
281
Paul Bakker6e339b52013-07-03 13:37:05 +0200282 // Found location, split block if > memory_header + 4 room left
283 //
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200284 if( cur->size - len < sizeof(memory_header) +
285 POLARSSL_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200286 {
287 cur->alloc = 1;
288
Paul Bakker1ef120f2013-07-03 17:20:39 +0200289 // Remove from free_list
290 //
291 if( cur->prev_free != NULL )
292 cur->prev_free->next_free = cur->next_free;
293 else
294 heap.first_free = cur->next_free;
295
296 if( cur->next_free != NULL )
297 cur->next_free->prev_free = cur->prev_free;
298
299 cur->prev_free = NULL;
300 cur->next_free = NULL;
301
Paul Bakker891998e2013-07-03 14:45:05 +0200302#if defined(POLARSSL_MEMORY_DEBUG)
303 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200304 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200305 heap.maximum_used = heap.total_used;
306#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200307#if defined(POLARSSL_MEMORY_BACKTRACE)
308 trace_cnt = backtrace( trace_buffer, MAX_BT );
309 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
310 cur->trace_count = trace_cnt;
311#endif
312
313 if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000314 polarssl_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200315
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200316 return( ( (unsigned char *) cur ) + sizeof(memory_header) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200317 }
318
319 p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
320 new = (memory_header *) p;
321
322 new->size = cur->size - len - sizeof(memory_header);
323 new->alloc = 0;
324 new->prev = cur;
325 new->next = cur->next;
326#if defined(POLARSSL_MEMORY_BACKTRACE)
327 new->trace = NULL;
328 new->trace_count = 0;
329#endif
330 new->magic1 = MAGIC1;
331 new->magic2 = MAGIC2;
332
333 if( new->next != NULL )
334 new->next->prev = new;
335
Paul Bakker1ef120f2013-07-03 17:20:39 +0200336 // Replace cur with new in free_list
337 //
338 new->prev_free = cur->prev_free;
339 new->next_free = cur->next_free;
340 if( new->prev_free != NULL )
341 new->prev_free->next_free = new;
342 else
343 heap.first_free = new;
344
345 if( new->next_free != NULL )
346 new->next_free->prev_free = new;
347
Paul Bakker6e339b52013-07-03 13:37:05 +0200348 cur->alloc = 1;
349 cur->size = len;
350 cur->next = new;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200351 cur->prev_free = NULL;
352 cur->next_free = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200353
Paul Bakker891998e2013-07-03 14:45:05 +0200354#if defined(POLARSSL_MEMORY_DEBUG)
355 heap.header_count++;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +0100356 if( heap.header_count > heap.maximum_header_count )
357 heap.maximum_header_count = heap.header_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200358 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200359 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200360 heap.maximum_used = heap.total_used;
361#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200362#if defined(POLARSSL_MEMORY_BACKTRACE)
363 trace_cnt = backtrace( trace_buffer, MAX_BT );
364 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
365 cur->trace_count = trace_cnt;
366#endif
367
368 if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000369 polarssl_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200370
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200371 return( ( (unsigned char *) cur ) + sizeof(memory_header) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200372}
373
374static void buffer_alloc_free( void *ptr )
375{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200376 memory_header *hdr, *old = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200377 unsigned char *p = (unsigned char *) ptr;
378
Paul Bakker6e339b52013-07-03 13:37:05 +0200379 if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
380 return;
381
382 if( p < heap.buf || p > heap.buf + heap.len )
383 {
384#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100385 polarssl_fprintf( stderr, "FATAL: polarssl_free() outside of managed "
386 "space\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200387#endif
Rich Evans77d36382015-01-30 12:12:11 +0000388 polarssl_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200389 }
390
391 p -= sizeof(memory_header);
392 hdr = (memory_header *) p;
393
394 if( verify_header( hdr ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000395 polarssl_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200396
397 if( hdr->alloc != 1 )
398 {
399#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100400 polarssl_fprintf( stderr, "FATAL: polarssl_free() on unallocated "
401 "data\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200402#endif
Rich Evans77d36382015-01-30 12:12:11 +0000403 polarssl_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200404 }
405
406 hdr->alloc = 0;
407
Paul Bakker891998e2013-07-03 14:45:05 +0200408#if defined(POLARSSL_MEMORY_DEBUG)
409 heap.free_count++;
410 heap.total_used -= hdr->size;
411#endif
412
Paul Bakker6e339b52013-07-03 13:37:05 +0200413 // Regroup with block before
414 //
415 if( hdr->prev != NULL && hdr->prev->alloc == 0 )
416 {
Paul Bakker891998e2013-07-03 14:45:05 +0200417#if defined(POLARSSL_MEMORY_DEBUG)
418 heap.header_count--;
419#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200420 hdr->prev->size += sizeof(memory_header) + hdr->size;
421 hdr->prev->next = hdr->next;
422 old = hdr;
423 hdr = hdr->prev;
424
425 if( hdr->next != NULL )
426 hdr->next->prev = hdr;
427
428#if defined(POLARSSL_MEMORY_BACKTRACE)
429 free( old->trace );
430#endif
431 memset( old, 0, sizeof(memory_header) );
432 }
433
434 // Regroup with block after
435 //
436 if( hdr->next != NULL && hdr->next->alloc == 0 )
437 {
Paul Bakker891998e2013-07-03 14:45:05 +0200438#if defined(POLARSSL_MEMORY_DEBUG)
439 heap.header_count--;
440#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200441 hdr->size += sizeof(memory_header) + hdr->next->size;
442 old = hdr->next;
443 hdr->next = hdr->next->next;
444
Paul Bakker1ef120f2013-07-03 17:20:39 +0200445 if( hdr->prev_free != NULL || hdr->next_free != NULL )
446 {
447 if( hdr->prev_free != NULL )
448 hdr->prev_free->next_free = hdr->next_free;
449 else
450 heap.first_free = hdr->next_free;
451
452 if( hdr->next_free != NULL )
453 hdr->next_free->prev_free = hdr->prev_free;
454 }
455
456 hdr->prev_free = old->prev_free;
457 hdr->next_free = old->next_free;
458
459 if( hdr->prev_free != NULL )
460 hdr->prev_free->next_free = hdr;
461 else
462 heap.first_free = hdr;
463
464 if( hdr->next_free != NULL )
465 hdr->next_free->prev_free = hdr;
466
Paul Bakker6e339b52013-07-03 13:37:05 +0200467 if( hdr->next != NULL )
468 hdr->next->prev = hdr;
469
470#if defined(POLARSSL_MEMORY_BACKTRACE)
471 free( old->trace );
472#endif
473 memset( old, 0, sizeof(memory_header) );
474 }
475
Paul Bakker1ef120f2013-07-03 17:20:39 +0200476 // Prepend to free_list if we have not merged
477 // (Does not have to stay in same order as prev / next list)
478 //
479 if( old == NULL )
480 {
481 hdr->next_free = heap.first_free;
Manuel Pégourié-Gonnard547ff662014-11-26 15:42:16 +0100482 if( heap.first_free != NULL )
483 heap.first_free->prev_free = hdr;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200484 heap.first_free = hdr;
485 }
486
Paul Bakker6e339b52013-07-03 13:37:05 +0200487#if defined(POLARSSL_MEMORY_BACKTRACE)
488 hdr->trace = NULL;
489 hdr->trace_count = 0;
490#endif
491
492 if( ( heap.verify & MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000493 polarssl_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200494}
495
Paul Bakkerbf796ac2013-09-28 11:06:38 +0200496void memory_buffer_set_verify( int verify )
497{
498 heap.verify = verify;
499}
500
Paul Bakker6e339b52013-07-03 13:37:05 +0200501int memory_buffer_alloc_verify()
502{
503 return verify_chain();
504}
505
506#if defined(POLARSSL_MEMORY_DEBUG)
507void memory_buffer_alloc_status()
508{
Paul Bakker7dc4c442014-02-01 22:50:26 +0100509 polarssl_fprintf( stderr,
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200510 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
511 "%zu bytes (total %zu bytes), malloc / free: %zu / %zu\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100512 heap.header_count, heap.total_used,
513 heap.maximum_header_count, heap.maximum_used,
514 heap.maximum_header_count * sizeof( memory_header )
515 + heap.maximum_used,
516 heap.malloc_count, heap.free_count );
Paul Bakker891998e2013-07-03 14:45:05 +0200517
Paul Bakker6e339b52013-07-03 13:37:05 +0200518 if( heap.first->next == NULL )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100519 polarssl_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200520 else
521 {
Paul Bakker7dc4c442014-02-01 22:50:26 +0100522 polarssl_fprintf( stderr, "Memory currently allocated:\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200523 debug_chain();
524 }
525}
Paul Bakker119602b2014-02-05 15:42:55 +0100526#endif /* POLARSSL_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200527
Paul Bakker1337aff2013-09-29 14:45:34 +0200528#if defined(POLARSSL_THREADING_C)
529static void *buffer_alloc_malloc_mutexed( size_t len )
530{
531 void *buf;
532 polarssl_mutex_lock( &heap.mutex );
533 buf = buffer_alloc_malloc( len );
534 polarssl_mutex_unlock( &heap.mutex );
535 return( buf );
536}
537
538static void buffer_alloc_free_mutexed( void *ptr )
539{
540 polarssl_mutex_lock( &heap.mutex );
541 buffer_alloc_free( ptr );
542 polarssl_mutex_unlock( &heap.mutex );
543}
Paul Bakker9af723c2014-05-01 13:03:14 +0200544#endif /* POLARSSL_THREADING_C */
Paul Bakker1337aff2013-09-29 14:45:34 +0200545
Paul Bakker6e339b52013-07-03 13:37:05 +0200546int memory_buffer_alloc_init( unsigned char *buf, size_t len )
547{
Paul Bakker6e339b52013-07-03 13:37:05 +0200548 memset( &heap, 0, sizeof(buffer_alloc_ctx) );
549 memset( buf, 0, len );
550
Paul Bakker1337aff2013-09-29 14:45:34 +0200551#if defined(POLARSSL_THREADING_C)
552 polarssl_mutex_init( &heap.mutex );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100553 platform_set_malloc_free( buffer_alloc_malloc_mutexed,
554 buffer_alloc_free_mutexed );
Paul Bakker1337aff2013-09-29 14:45:34 +0200555#else
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100556 platform_set_malloc_free( buffer_alloc_malloc, buffer_alloc_free );
Paul Bakker1337aff2013-09-29 14:45:34 +0200557#endif
558
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200559 if( (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE )
560 {
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100561 /* Adjust len first since buf is used in the computation */
562 len -= POLARSSL_MEMORY_ALIGN_MULTIPLE
563 - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200564 buf += POLARSSL_MEMORY_ALIGN_MULTIPLE
565 - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200566 }
567
Paul Bakker6e339b52013-07-03 13:37:05 +0200568 heap.buf = buf;
569 heap.len = len;
570
571 heap.first = (memory_header *) buf;
572 heap.first->size = len - sizeof(memory_header);
573 heap.first->magic1 = MAGIC1;
574 heap.first->magic2 = MAGIC2;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200575 heap.first_free = heap.first;
Paul Bakker6e339b52013-07-03 13:37:05 +0200576 return( 0 );
577}
578
Paul Bakker1337aff2013-09-29 14:45:34 +0200579void memory_buffer_alloc_free()
580{
581#if defined(POLARSSL_THREADING_C)
582 polarssl_mutex_free( &heap.mutex );
583#endif
Paul Bakker34617722014-06-13 17:20:13 +0200584 polarssl_zeroize( &heap, sizeof(buffer_alloc_ctx) );
Paul Bakker1337aff2013-09-29 14:45:34 +0200585}
586
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100587#if defined(POLARSSL_SELF_TEST)
588static int check_pointer( void *p )
589{
590 if( p == NULL )
591 return( -1 );
592
593 if( (size_t) p % POLARSSL_MEMORY_ALIGN_MULTIPLE != 0 )
594 return( -1 );
595
596 return( 0 );
597}
598
599static int check_all_free( )
600{
601 if( heap.current_alloc_size != 0 ||
602 heap.first != heap.first_free ||
603 (void *) heap.first != (void *) heap.buf )
604 {
605 return( -1 );
606 }
607
608 return( 0 );
609}
610
611#define TEST_ASSERT( condition ) \
612 if( ! (condition) ) \
613 { \
614 if( verbose != 0 ) \
615 polarssl_printf( "failed\n" ); \
616 \
617 ret = 1; \
618 goto cleanup; \
619 }
620
621int memory_buffer_alloc_self_test( int verbose )
622{
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100623 unsigned char buf[1024];
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100624 unsigned char *p, *q, *r, *end;
625 int ret = 0;
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100626
627 if( verbose != 0 )
628 polarssl_printf( " MBA test #1 (basic alloc-free cycle): " );
629
630 memory_buffer_alloc_init( buf, sizeof( buf ) );
631
632 p = polarssl_malloc( 1 );
633 q = polarssl_malloc( 128 );
634 r = polarssl_malloc( 16 );
635
636 TEST_ASSERT( check_pointer( p ) == 0 &&
637 check_pointer( q ) == 0 &&
638 check_pointer( r ) == 0 );
639
640 polarssl_free( r );
641 polarssl_free( q );
642 polarssl_free( p );
643
644 TEST_ASSERT( check_all_free( ) == 0 );
645
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100646 /* Memorize end to compare with the next test */
647 end = heap.buf + heap.len;
648
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100649 memory_buffer_alloc_free( );
650
651 if( verbose != 0 )
652 polarssl_printf( "passed\n" );
653
654 if( verbose != 0 )
655 polarssl_printf( " MBA test #2 (buf not aligned): " );
656
657 memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
658
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100659 TEST_ASSERT( heap.buf + heap.len == end );
660
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100661 p = polarssl_malloc( 1 );
662 q = polarssl_malloc( 128 );
663 r = polarssl_malloc( 16 );
664
665 TEST_ASSERT( check_pointer( p ) == 0 &&
666 check_pointer( q ) == 0 &&
667 check_pointer( r ) == 0 );
668
669 polarssl_free( r );
670 polarssl_free( q );
671 polarssl_free( p );
672
673 TEST_ASSERT( check_all_free( ) == 0 );
674
675 memory_buffer_alloc_free( );
676
677 if( verbose != 0 )
678 polarssl_printf( "passed\n" );
679
680 if( verbose != 0 )
681 polarssl_printf( " MBA test #3 (full): " );
682
683 memory_buffer_alloc_init( buf, sizeof( buf ) );
684
685 p = polarssl_malloc( sizeof( buf ) - sizeof( memory_header ) );
686
687 TEST_ASSERT( check_pointer( p ) == 0 );
688 TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
689
690 polarssl_free( p );
691
692 p = polarssl_malloc( sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
693 q = polarssl_malloc( 16 );
694
695 TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
696 TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
697
698 polarssl_free( q );
699
700 TEST_ASSERT( polarssl_malloc( 17 ) == NULL );
701
702 polarssl_free( p );
703
704 TEST_ASSERT( check_all_free( ) == 0 );
705
706 memory_buffer_alloc_free( );
707
708 if( verbose != 0 )
709 polarssl_printf( "passed\n" );
710
711cleanup:
712 memory_buffer_alloc_free( );
713
714 return( ret );
715}
716#endif /* POLARSSL_SELF_TEST */
717
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100718#endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */