blob: 1490264a40197ed1e5b49cc9aabb3dd9391eeb86 [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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 Bakker6e339b52013-07-03 13:37:05 +020030
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010031#include "polarssl/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020032
33#include <string.h>
34
35#if defined(POLARSSL_MEMORY_DEBUG)
36#include <stdio.h>
Manuel Pégourié-Gonnard2fbf3112014-07-12 03:32:40 +020037#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020038#if defined(POLARSSL_MEMORY_BACKTRACE)
39#include <execinfo.h>
40#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020041
Paul Bakker1337aff2013-09-29 14:45:34 +020042#if defined(POLARSSL_THREADING_C)
43#include "polarssl/threading.h"
44#endif
45
Paul Bakker7dc4c442014-02-01 22:50:26 +010046#if defined(POLARSSL_PLATFORM_C)
47#include "polarssl/platform.h"
48#else
49#define polarssl_fprintf fprintf
50#endif
51
Paul Bakker34617722014-06-13 17:20:13 +020052/* Implementation that should never be optimized out by the compiler */
53static void polarssl_zeroize( void *v, size_t n ) {
54 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
55}
56
Paul Bakker6e339b52013-07-03 13:37:05 +020057#define MAGIC1 0xFF00AA55
58#define MAGIC2 0xEE119966
59#define MAX_BT 20
60
61typedef struct _memory_header memory_header;
62struct _memory_header
63{
64 size_t magic1;
65 size_t size;
66 size_t alloc;
67 memory_header *prev;
68 memory_header *next;
Paul Bakker1ef120f2013-07-03 17:20:39 +020069 memory_header *prev_free;
70 memory_header *next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020071#if defined(POLARSSL_MEMORY_BACKTRACE)
72 char **trace;
73 size_t trace_count;
74#endif
75 size_t magic2;
76};
77
78typedef struct
79{
80 unsigned char *buf;
81 size_t len;
82 memory_header *first;
Paul Bakker1ef120f2013-07-03 17:20:39 +020083 memory_header *first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020084 size_t current_alloc_size;
85 int verify;
Paul Bakker891998e2013-07-03 14:45:05 +020086#if defined(POLARSSL_MEMORY_DEBUG)
87 size_t malloc_count;
88 size_t free_count;
89 size_t total_used;
90 size_t maximum_used;
91 size_t header_count;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +010092 size_t maximum_header_count;
Paul Bakker891998e2013-07-03 14:45:05 +020093#endif
Paul Bakker1337aff2013-09-29 14:45:34 +020094#if defined(POLARSSL_THREADING_C)
95 threading_mutex_t mutex;
96#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020097}
98buffer_alloc_ctx;
99
100static buffer_alloc_ctx heap;
101
102#if defined(POLARSSL_MEMORY_DEBUG)
103static void debug_header( memory_header *hdr )
104{
105#if defined(POLARSSL_MEMORY_BACKTRACE)
106 size_t i;
107#endif
108
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200109 polarssl_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
110 "ALLOC(%zu), SIZE(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100111 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
112 hdr->alloc, hdr->size );
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200113 polarssl_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100114 (size_t) hdr->prev_free, (size_t) hdr->next_free );
Paul Bakker6e339b52013-07-03 13:37:05 +0200115
116#if defined(POLARSSL_MEMORY_BACKTRACE)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100117 polarssl_fprintf( stderr, "TRACE: \n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200118 for( i = 0; i < hdr->trace_count; i++ )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100119 polarssl_fprintf( stderr, "%s\n", hdr->trace[i] );
120 polarssl_fprintf( stderr, "\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200121#endif
122}
123
124static void debug_chain()
125{
126 memory_header *cur = heap.first;
127
Paul Bakker7dc4c442014-02-01 22:50:26 +0100128 polarssl_fprintf( stderr, "\nBlock list\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200129 while( cur != NULL )
130 {
131 debug_header( cur );
Paul Bakker6e339b52013-07-03 13:37:05 +0200132 cur = cur->next;
133 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200134
Paul Bakker7dc4c442014-02-01 22:50:26 +0100135 polarssl_fprintf( stderr, "Free list\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200136 cur = heap.first_free;
137
138 while( cur != NULL )
139 {
140 debug_header( cur );
141 cur = cur->next_free;
142 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200143}
144#endif /* POLARSSL_MEMORY_DEBUG */
145
146static int verify_header( memory_header *hdr )
147{
148 if( hdr->magic1 != MAGIC1 )
149 {
150#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100151 polarssl_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200152#endif
153 return( 1 );
154 }
155
156 if( hdr->magic2 != MAGIC2 )
157 {
158#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100159 polarssl_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200160#endif
161 return( 1 );
162 }
163
164 if( hdr->alloc > 1 )
165 {
166#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100167 polarssl_fprintf( stderr, "FATAL: alloc has illegal value\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200168#endif
169 return( 1 );
170 }
171
Paul Bakker1ef120f2013-07-03 17:20:39 +0200172 if( hdr->prev != NULL && hdr->prev == hdr->next )
173 {
174#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100175 polarssl_fprintf( stderr, "FATAL: prev == next\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200176#endif
177 return( 1 );
178 }
179
180 if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
181 {
182#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100183 polarssl_fprintf( stderr, "FATAL: prev_free == next_free\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200184#endif
185 return( 1 );
186 }
187
Paul Bakker6e339b52013-07-03 13:37:05 +0200188 return( 0 );
189}
190
191static int verify_chain()
192{
193 memory_header *prv = heap.first, *cur = heap.first->next;
194
195 if( verify_header( heap.first ) != 0 )
196 {
197#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100198 polarssl_fprintf( stderr, "FATAL: verification of first header "
199 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200200#endif
201 return( 1 );
202 }
203
204 if( heap.first->prev != NULL )
205 {
206#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100207 polarssl_fprintf( stderr, "FATAL: verification failed: "
208 "first->prev != NULL\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200209#endif
210 return( 1 );
211 }
212
213 while( cur != NULL )
214 {
215 if( verify_header( cur ) != 0 )
216 {
217#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100218 polarssl_fprintf( stderr, "FATAL: verification of header "
219 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200220#endif
221 return( 1 );
222 }
223
224 if( cur->prev != prv )
225 {
226#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100227 polarssl_fprintf( stderr, "FATAL: verification failed: "
228 "cur->prev != prv\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200229#endif
230 return( 1 );
231 }
232
233 prv = cur;
234 cur = cur->next;
235 }
236
237 return( 0 );
238}
239
240static void *buffer_alloc_malloc( size_t len )
241{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200242 memory_header *new, *cur = heap.first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200243 unsigned char *p;
244#if defined(POLARSSL_MEMORY_BACKTRACE)
245 void *trace_buffer[MAX_BT];
246 size_t trace_cnt;
247#endif
248
249 if( heap.buf == NULL || heap.first == NULL )
250 return( NULL );
251
252 if( len % POLARSSL_MEMORY_ALIGN_MULTIPLE )
253 {
254 len -= len % POLARSSL_MEMORY_ALIGN_MULTIPLE;
255 len += POLARSSL_MEMORY_ALIGN_MULTIPLE;
256 }
257
258 // Find block that fits
259 //
260 while( cur != NULL )
261 {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200262 if( cur->size >= len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200263 break;
264
Paul Bakker1ef120f2013-07-03 17:20:39 +0200265 cur = cur->next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200266 }
267
268 if( cur == NULL )
269 return( NULL );
270
Paul Bakker1ef120f2013-07-03 17:20:39 +0200271 if( cur->alloc != 0 )
272 {
273#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100274 polarssl_fprintf( stderr, "FATAL: block in free_list but allocated "
275 "data\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200276#endif
277 exit( 1 );
278 }
279
Paul Bakker891998e2013-07-03 14:45:05 +0200280#if defined(POLARSSL_MEMORY_DEBUG)
281 heap.malloc_count++;
282#endif
283
Paul Bakker6e339b52013-07-03 13:37:05 +0200284 // Found location, split block if > memory_header + 4 room left
285 //
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200286 if( cur->size - len < sizeof(memory_header) +
287 POLARSSL_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200288 {
289 cur->alloc = 1;
290
Paul Bakker1ef120f2013-07-03 17:20:39 +0200291 // Remove from free_list
292 //
293 if( cur->prev_free != NULL )
294 cur->prev_free->next_free = cur->next_free;
295 else
296 heap.first_free = cur->next_free;
297
298 if( cur->next_free != NULL )
299 cur->next_free->prev_free = cur->prev_free;
300
301 cur->prev_free = NULL;
302 cur->next_free = NULL;
303
Paul Bakker891998e2013-07-03 14:45:05 +0200304#if defined(POLARSSL_MEMORY_DEBUG)
305 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200306 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200307 heap.maximum_used = heap.total_used;
308#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200309#if defined(POLARSSL_MEMORY_BACKTRACE)
310 trace_cnt = backtrace( trace_buffer, MAX_BT );
311 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
312 cur->trace_count = trace_cnt;
313#endif
314
315 if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
316 exit( 1 );
317
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200318 return( ( (unsigned char *) cur ) + sizeof(memory_header) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200319 }
320
321 p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
322 new = (memory_header *) p;
323
324 new->size = cur->size - len - sizeof(memory_header);
325 new->alloc = 0;
326 new->prev = cur;
327 new->next = cur->next;
328#if defined(POLARSSL_MEMORY_BACKTRACE)
329 new->trace = NULL;
330 new->trace_count = 0;
331#endif
332 new->magic1 = MAGIC1;
333 new->magic2 = MAGIC2;
334
335 if( new->next != NULL )
336 new->next->prev = new;
337
Paul Bakker1ef120f2013-07-03 17:20:39 +0200338 // Replace cur with new in free_list
339 //
340 new->prev_free = cur->prev_free;
341 new->next_free = cur->next_free;
342 if( new->prev_free != NULL )
343 new->prev_free->next_free = new;
344 else
345 heap.first_free = new;
346
347 if( new->next_free != NULL )
348 new->next_free->prev_free = new;
349
Paul Bakker6e339b52013-07-03 13:37:05 +0200350 cur->alloc = 1;
351 cur->size = len;
352 cur->next = new;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200353 cur->prev_free = NULL;
354 cur->next_free = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200355
Paul Bakker891998e2013-07-03 14:45:05 +0200356#if defined(POLARSSL_MEMORY_DEBUG)
357 heap.header_count++;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +0100358 if( heap.header_count > heap.maximum_header_count )
359 heap.maximum_header_count = heap.header_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200360 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200361 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200362 heap.maximum_used = heap.total_used;
363#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200364#if defined(POLARSSL_MEMORY_BACKTRACE)
365 trace_cnt = backtrace( trace_buffer, MAX_BT );
366 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
367 cur->trace_count = trace_cnt;
368#endif
369
370 if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
371 exit( 1 );
372
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200373 return( ( (unsigned char *) cur ) + sizeof(memory_header) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200374}
375
376static void buffer_alloc_free( void *ptr )
377{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200378 memory_header *hdr, *old = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200379 unsigned char *p = (unsigned char *) ptr;
380
Paul Bakker6e339b52013-07-03 13:37:05 +0200381 if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
382 return;
383
384 if( p < heap.buf || p > heap.buf + heap.len )
385 {
386#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100387 polarssl_fprintf( stderr, "FATAL: polarssl_free() outside of managed "
388 "space\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200389#endif
Paul Bakker41350a92013-07-03 15:33:47 +0200390 exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200391 }
392
393 p -= sizeof(memory_header);
394 hdr = (memory_header *) p;
395
396 if( verify_header( hdr ) != 0 )
397 exit( 1 );
398
399 if( hdr->alloc != 1 )
400 {
401#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100402 polarssl_fprintf( stderr, "FATAL: polarssl_free() on unallocated "
403 "data\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200404#endif
Paul Bakker41350a92013-07-03 15:33:47 +0200405 exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200406 }
407
408 hdr->alloc = 0;
409
Paul Bakker891998e2013-07-03 14:45:05 +0200410#if defined(POLARSSL_MEMORY_DEBUG)
411 heap.free_count++;
412 heap.total_used -= hdr->size;
413#endif
414
Paul Bakker6e339b52013-07-03 13:37:05 +0200415 // Regroup with block before
416 //
417 if( hdr->prev != NULL && hdr->prev->alloc == 0 )
418 {
Paul Bakker891998e2013-07-03 14:45:05 +0200419#if defined(POLARSSL_MEMORY_DEBUG)
420 heap.header_count--;
421#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200422 hdr->prev->size += sizeof(memory_header) + hdr->size;
423 hdr->prev->next = hdr->next;
424 old = hdr;
425 hdr = hdr->prev;
426
427 if( hdr->next != NULL )
428 hdr->next->prev = hdr;
429
430#if defined(POLARSSL_MEMORY_BACKTRACE)
431 free( old->trace );
432#endif
433 memset( old, 0, sizeof(memory_header) );
434 }
435
436 // Regroup with block after
437 //
438 if( hdr->next != NULL && hdr->next->alloc == 0 )
439 {
Paul Bakker891998e2013-07-03 14:45:05 +0200440#if defined(POLARSSL_MEMORY_DEBUG)
441 heap.header_count--;
442#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200443 hdr->size += sizeof(memory_header) + hdr->next->size;
444 old = hdr->next;
445 hdr->next = hdr->next->next;
446
Paul Bakker1ef120f2013-07-03 17:20:39 +0200447 if( hdr->prev_free != NULL || hdr->next_free != NULL )
448 {
449 if( hdr->prev_free != NULL )
450 hdr->prev_free->next_free = hdr->next_free;
451 else
452 heap.first_free = hdr->next_free;
453
454 if( hdr->next_free != NULL )
455 hdr->next_free->prev_free = hdr->prev_free;
456 }
457
458 hdr->prev_free = old->prev_free;
459 hdr->next_free = old->next_free;
460
461 if( hdr->prev_free != NULL )
462 hdr->prev_free->next_free = hdr;
463 else
464 heap.first_free = hdr;
465
466 if( hdr->next_free != NULL )
467 hdr->next_free->prev_free = hdr;
468
Paul Bakker6e339b52013-07-03 13:37:05 +0200469 if( hdr->next != NULL )
470 hdr->next->prev = hdr;
471
472#if defined(POLARSSL_MEMORY_BACKTRACE)
473 free( old->trace );
474#endif
475 memset( old, 0, sizeof(memory_header) );
476 }
477
Paul Bakker1ef120f2013-07-03 17:20:39 +0200478 // Prepend to free_list if we have not merged
479 // (Does not have to stay in same order as prev / next list)
480 //
481 if( old == NULL )
482 {
483 hdr->next_free = heap.first_free;
Manuel Pégourié-Gonnard547ff662014-11-26 15:42:16 +0100484 if( heap.first_free != NULL )
485 heap.first_free->prev_free = hdr;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200486 heap.first_free = hdr;
487 }
488
Paul Bakker6e339b52013-07-03 13:37:05 +0200489#if defined(POLARSSL_MEMORY_BACKTRACE)
490 hdr->trace = NULL;
491 hdr->trace_count = 0;
492#endif
493
494 if( ( heap.verify & MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
495 exit( 1 );
496}
497
Paul Bakkerbf796ac2013-09-28 11:06:38 +0200498void memory_buffer_set_verify( int verify )
499{
500 heap.verify = verify;
501}
502
Paul Bakker6e339b52013-07-03 13:37:05 +0200503int memory_buffer_alloc_verify()
504{
505 return verify_chain();
506}
507
508#if defined(POLARSSL_MEMORY_DEBUG)
509void memory_buffer_alloc_status()
510{
Paul Bakker7dc4c442014-02-01 22:50:26 +0100511 polarssl_fprintf( stderr,
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200512 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
513 "%zu bytes (total %zu bytes), malloc / free: %zu / %zu\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100514 heap.header_count, heap.total_used,
515 heap.maximum_header_count, heap.maximum_used,
516 heap.maximum_header_count * sizeof( memory_header )
517 + heap.maximum_used,
518 heap.malloc_count, heap.free_count );
Paul Bakker891998e2013-07-03 14:45:05 +0200519
Paul Bakker6e339b52013-07-03 13:37:05 +0200520 if( heap.first->next == NULL )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100521 polarssl_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200522 else
523 {
Paul Bakker7dc4c442014-02-01 22:50:26 +0100524 polarssl_fprintf( stderr, "Memory currently allocated:\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200525 debug_chain();
526 }
527}
Paul Bakker119602b2014-02-05 15:42:55 +0100528#endif /* POLARSSL_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200529
Paul Bakker1337aff2013-09-29 14:45:34 +0200530#if defined(POLARSSL_THREADING_C)
531static void *buffer_alloc_malloc_mutexed( size_t len )
532{
533 void *buf;
534 polarssl_mutex_lock( &heap.mutex );
535 buf = buffer_alloc_malloc( len );
536 polarssl_mutex_unlock( &heap.mutex );
537 return( buf );
538}
539
540static void buffer_alloc_free_mutexed( void *ptr )
541{
542 polarssl_mutex_lock( &heap.mutex );
543 buffer_alloc_free( ptr );
544 polarssl_mutex_unlock( &heap.mutex );
545}
Paul Bakker9af723c2014-05-01 13:03:14 +0200546#endif /* POLARSSL_THREADING_C */
Paul Bakker1337aff2013-09-29 14:45:34 +0200547
Paul Bakker6e339b52013-07-03 13:37:05 +0200548int memory_buffer_alloc_init( unsigned char *buf, size_t len )
549{
Paul Bakker6e339b52013-07-03 13:37:05 +0200550 memset( &heap, 0, sizeof(buffer_alloc_ctx) );
551 memset( buf, 0, len );
552
Paul Bakker1337aff2013-09-29 14:45:34 +0200553#if defined(POLARSSL_THREADING_C)
554 polarssl_mutex_init( &heap.mutex );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100555 platform_set_malloc_free( buffer_alloc_malloc_mutexed,
556 buffer_alloc_free_mutexed );
Paul Bakker1337aff2013-09-29 14:45:34 +0200557#else
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100558 platform_set_malloc_free( buffer_alloc_malloc, buffer_alloc_free );
Paul Bakker1337aff2013-09-29 14:45:34 +0200559#endif
560
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200561 if( (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE )
562 {
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100563 /* Adjust len first since buf is used in the computation */
564 len -= POLARSSL_MEMORY_ALIGN_MULTIPLE
565 - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200566 buf += POLARSSL_MEMORY_ALIGN_MULTIPLE
567 - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200568 }
569
Paul Bakker6e339b52013-07-03 13:37:05 +0200570 heap.buf = buf;
571 heap.len = len;
572
573 heap.first = (memory_header *) buf;
574 heap.first->size = len - sizeof(memory_header);
575 heap.first->magic1 = MAGIC1;
576 heap.first->magic2 = MAGIC2;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200577 heap.first_free = heap.first;
Paul Bakker6e339b52013-07-03 13:37:05 +0200578 return( 0 );
579}
580
Paul Bakker1337aff2013-09-29 14:45:34 +0200581void memory_buffer_alloc_free()
582{
583#if defined(POLARSSL_THREADING_C)
584 polarssl_mutex_free( &heap.mutex );
585#endif
Paul Bakker34617722014-06-13 17:20:13 +0200586 polarssl_zeroize( &heap, sizeof(buffer_alloc_ctx) );
Paul Bakker1337aff2013-09-29 14:45:34 +0200587}
588
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100589#if defined(POLARSSL_SELF_TEST)
590static int check_pointer( void *p )
591{
592 if( p == NULL )
593 return( -1 );
594
595 if( (size_t) p % POLARSSL_MEMORY_ALIGN_MULTIPLE != 0 )
596 return( -1 );
597
598 return( 0 );
599}
600
601static int check_all_free( )
602{
603 if( heap.current_alloc_size != 0 ||
604 heap.first != heap.first_free ||
605 (void *) heap.first != (void *) heap.buf )
606 {
607 return( -1 );
608 }
609
610 return( 0 );
611}
612
613#define TEST_ASSERT( condition ) \
614 if( ! (condition) ) \
615 { \
616 if( verbose != 0 ) \
617 polarssl_printf( "failed\n" ); \
618 \
619 ret = 1; \
620 goto cleanup; \
621 }
622
623int memory_buffer_alloc_self_test( int verbose )
624{
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100625 unsigned char buf[1024];
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100626 unsigned char *p, *q, *r, *end;
627 int ret = 0;
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100628
629 if( verbose != 0 )
630 polarssl_printf( " MBA test #1 (basic alloc-free cycle): " );
631
632 memory_buffer_alloc_init( buf, sizeof( buf ) );
633
634 p = polarssl_malloc( 1 );
635 q = polarssl_malloc( 128 );
636 r = polarssl_malloc( 16 );
637
638 TEST_ASSERT( check_pointer( p ) == 0 &&
639 check_pointer( q ) == 0 &&
640 check_pointer( r ) == 0 );
641
642 polarssl_free( r );
643 polarssl_free( q );
644 polarssl_free( p );
645
646 TEST_ASSERT( check_all_free( ) == 0 );
647
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100648 /* Memorize end to compare with the next test */
649 end = heap.buf + heap.len;
650
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100651 memory_buffer_alloc_free( );
652
653 if( verbose != 0 )
654 polarssl_printf( "passed\n" );
655
656 if( verbose != 0 )
657 polarssl_printf( " MBA test #2 (buf not aligned): " );
658
659 memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
660
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100661 TEST_ASSERT( heap.buf + heap.len == end );
662
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100663 p = polarssl_malloc( 1 );
664 q = polarssl_malloc( 128 );
665 r = polarssl_malloc( 16 );
666
667 TEST_ASSERT( check_pointer( p ) == 0 &&
668 check_pointer( q ) == 0 &&
669 check_pointer( r ) == 0 );
670
671 polarssl_free( r );
672 polarssl_free( q );
673 polarssl_free( p );
674
675 TEST_ASSERT( check_all_free( ) == 0 );
676
677 memory_buffer_alloc_free( );
678
679 if( verbose != 0 )
680 polarssl_printf( "passed\n" );
681
682 if( verbose != 0 )
683 polarssl_printf( " MBA test #3 (full): " );
684
685 memory_buffer_alloc_init( buf, sizeof( buf ) );
686
687 p = polarssl_malloc( sizeof( buf ) - sizeof( memory_header ) );
688
689 TEST_ASSERT( check_pointer( p ) == 0 );
690 TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
691
692 polarssl_free( p );
693
694 p = polarssl_malloc( sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
695 q = polarssl_malloc( 16 );
696
697 TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
698 TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
699
700 polarssl_free( q );
701
702 TEST_ASSERT( polarssl_malloc( 17 ) == NULL );
703
704 polarssl_free( p );
705
706 TEST_ASSERT( check_all_free( ) == 0 );
707
708 memory_buffer_alloc_free( );
709
710 if( verbose != 0 )
711 polarssl_printf( "passed\n" );
712
713cleanup:
714 memory_buffer_alloc_free( );
715
716 return( ret );
717}
718#endif /* POLARSSL_SELF_TEST */
719
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100720#endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */