blob: 773e54ff06525a46e3de999c42e8fe181c5d9941 [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 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
Rich Evansd08a6052015-02-12 12:17:10 +000033/* No need for the header guard as POLARSSL_MEMORY_BUFFER_ALLOC_C
34 is dependent upon POLARSSL_PLATFORM_C */
35#include "polarssl/platform.h"
36
Paul Bakker6e339b52013-07-03 13:37:05 +020037#include <string.h>
38
Paul Bakker6e339b52013-07-03 13:37:05 +020039#if defined(POLARSSL_MEMORY_BACKTRACE)
40#include <execinfo.h>
41#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020042
Paul Bakker1337aff2013-09-29 14:45:34 +020043#if defined(POLARSSL_THREADING_C)
44#include "polarssl/threading.h"
45#endif
46
Paul Bakker34617722014-06-13 17:20:13 +020047/* Implementation that should never be optimized out by the compiler */
48static void polarssl_zeroize( void *v, size_t n ) {
49 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
50}
51
Paul Bakker6e339b52013-07-03 13:37:05 +020052#define MAGIC1 0xFF00AA55
53#define MAGIC2 0xEE119966
54#define MAX_BT 20
55
56typedef struct _memory_header memory_header;
57struct _memory_header
58{
59 size_t magic1;
60 size_t size;
61 size_t alloc;
62 memory_header *prev;
63 memory_header *next;
Paul Bakker1ef120f2013-07-03 17:20:39 +020064 memory_header *prev_free;
65 memory_header *next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020066#if defined(POLARSSL_MEMORY_BACKTRACE)
67 char **trace;
68 size_t trace_count;
69#endif
70 size_t magic2;
71};
72
73typedef struct
74{
75 unsigned char *buf;
76 size_t len;
77 memory_header *first;
Paul Bakker1ef120f2013-07-03 17:20:39 +020078 memory_header *first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020079 size_t current_alloc_size;
80 int verify;
Paul Bakker891998e2013-07-03 14:45:05 +020081#if defined(POLARSSL_MEMORY_DEBUG)
82 size_t malloc_count;
83 size_t free_count;
84 size_t total_used;
85 size_t maximum_used;
86 size_t header_count;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +010087 size_t maximum_header_count;
Paul Bakker891998e2013-07-03 14:45:05 +020088#endif
Paul Bakker1337aff2013-09-29 14:45:34 +020089#if defined(POLARSSL_THREADING_C)
90 threading_mutex_t mutex;
91#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020092}
93buffer_alloc_ctx;
94
95static buffer_alloc_ctx heap;
96
97#if defined(POLARSSL_MEMORY_DEBUG)
98static void debug_header( memory_header *hdr )
99{
100#if defined(POLARSSL_MEMORY_BACKTRACE)
101 size_t i;
102#endif
103
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200104 polarssl_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
105 "ALLOC(%zu), SIZE(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100106 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
107 hdr->alloc, hdr->size );
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200108 polarssl_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100109 (size_t) hdr->prev_free, (size_t) hdr->next_free );
Paul Bakker6e339b52013-07-03 13:37:05 +0200110
111#if defined(POLARSSL_MEMORY_BACKTRACE)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100112 polarssl_fprintf( stderr, "TRACE: \n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200113 for( i = 0; i < hdr->trace_count; i++ )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100114 polarssl_fprintf( stderr, "%s\n", hdr->trace[i] );
115 polarssl_fprintf( stderr, "\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200116#endif
117}
118
119static void debug_chain()
120{
121 memory_header *cur = heap.first;
122
Paul Bakker7dc4c442014-02-01 22:50:26 +0100123 polarssl_fprintf( stderr, "\nBlock list\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200124 while( cur != NULL )
125 {
126 debug_header( cur );
Paul Bakker6e339b52013-07-03 13:37:05 +0200127 cur = cur->next;
128 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200129
Paul Bakker7dc4c442014-02-01 22:50:26 +0100130 polarssl_fprintf( stderr, "Free list\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200131 cur = heap.first_free;
132
133 while( cur != NULL )
134 {
135 debug_header( cur );
136 cur = cur->next_free;
137 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200138}
139#endif /* POLARSSL_MEMORY_DEBUG */
140
141static int verify_header( memory_header *hdr )
142{
143 if( hdr->magic1 != MAGIC1 )
144 {
145#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100146 polarssl_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200147#endif
148 return( 1 );
149 }
150
151 if( hdr->magic2 != MAGIC2 )
152 {
153#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100154 polarssl_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200155#endif
156 return( 1 );
157 }
158
159 if( hdr->alloc > 1 )
160 {
161#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100162 polarssl_fprintf( stderr, "FATAL: alloc has illegal value\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200163#endif
164 return( 1 );
165 }
166
Paul Bakker1ef120f2013-07-03 17:20:39 +0200167 if( hdr->prev != NULL && hdr->prev == hdr->next )
168 {
169#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100170 polarssl_fprintf( stderr, "FATAL: prev == next\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200171#endif
172 return( 1 );
173 }
174
175 if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
176 {
177#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100178 polarssl_fprintf( stderr, "FATAL: prev_free == next_free\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200179#endif
180 return( 1 );
181 }
182
Paul Bakker6e339b52013-07-03 13:37:05 +0200183 return( 0 );
184}
185
186static int verify_chain()
187{
188 memory_header *prv = heap.first, *cur = heap.first->next;
189
190 if( verify_header( heap.first ) != 0 )
191 {
192#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100193 polarssl_fprintf( stderr, "FATAL: verification of first header "
194 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200195#endif
196 return( 1 );
197 }
198
199 if( heap.first->prev != NULL )
200 {
201#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100202 polarssl_fprintf( stderr, "FATAL: verification failed: "
203 "first->prev != NULL\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200204#endif
205 return( 1 );
206 }
207
208 while( cur != NULL )
209 {
210 if( verify_header( cur ) != 0 )
211 {
212#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100213 polarssl_fprintf( stderr, "FATAL: verification of header "
214 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200215#endif
216 return( 1 );
217 }
218
219 if( cur->prev != prv )
220 {
221#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100222 polarssl_fprintf( stderr, "FATAL: verification failed: "
223 "cur->prev != prv\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200224#endif
225 return( 1 );
226 }
227
228 prv = cur;
229 cur = cur->next;
230 }
231
232 return( 0 );
233}
234
235static void *buffer_alloc_malloc( size_t len )
236{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200237 memory_header *new, *cur = heap.first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200238 unsigned char *p;
239#if defined(POLARSSL_MEMORY_BACKTRACE)
240 void *trace_buffer[MAX_BT];
241 size_t trace_cnt;
242#endif
243
244 if( heap.buf == NULL || heap.first == NULL )
245 return( NULL );
246
247 if( len % POLARSSL_MEMORY_ALIGN_MULTIPLE )
248 {
249 len -= len % POLARSSL_MEMORY_ALIGN_MULTIPLE;
250 len += POLARSSL_MEMORY_ALIGN_MULTIPLE;
251 }
252
253 // Find block that fits
254 //
255 while( cur != NULL )
256 {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200257 if( cur->size >= len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200258 break;
259
Paul Bakker1ef120f2013-07-03 17:20:39 +0200260 cur = cur->next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200261 }
262
263 if( cur == NULL )
264 return( NULL );
265
Paul Bakker1ef120f2013-07-03 17:20:39 +0200266 if( cur->alloc != 0 )
267 {
268#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100269 polarssl_fprintf( stderr, "FATAL: block in free_list but allocated "
270 "data\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200271#endif
272 exit( 1 );
273 }
274
Paul Bakker891998e2013-07-03 14:45:05 +0200275#if defined(POLARSSL_MEMORY_DEBUG)
276 heap.malloc_count++;
277#endif
278
Paul Bakker6e339b52013-07-03 13:37:05 +0200279 // Found location, split block if > memory_header + 4 room left
280 //
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200281 if( cur->size - len < sizeof(memory_header) +
282 POLARSSL_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200283 {
284 cur->alloc = 1;
285
Paul Bakker1ef120f2013-07-03 17:20:39 +0200286 // Remove from free_list
287 //
288 if( cur->prev_free != NULL )
289 cur->prev_free->next_free = cur->next_free;
290 else
291 heap.first_free = cur->next_free;
292
293 if( cur->next_free != NULL )
294 cur->next_free->prev_free = cur->prev_free;
295
296 cur->prev_free = NULL;
297 cur->next_free = NULL;
298
Paul Bakker891998e2013-07-03 14:45:05 +0200299#if defined(POLARSSL_MEMORY_DEBUG)
300 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200301 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200302 heap.maximum_used = heap.total_used;
303#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200304#if defined(POLARSSL_MEMORY_BACKTRACE)
305 trace_cnt = backtrace( trace_buffer, MAX_BT );
306 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
307 cur->trace_count = trace_cnt;
308#endif
309
310 if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
311 exit( 1 );
312
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200313 return( ( (unsigned char *) cur ) + sizeof(memory_header) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200314 }
315
316 p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
317 new = (memory_header *) p;
318
319 new->size = cur->size - len - sizeof(memory_header);
320 new->alloc = 0;
321 new->prev = cur;
322 new->next = cur->next;
323#if defined(POLARSSL_MEMORY_BACKTRACE)
324 new->trace = NULL;
325 new->trace_count = 0;
326#endif
327 new->magic1 = MAGIC1;
328 new->magic2 = MAGIC2;
329
330 if( new->next != NULL )
331 new->next->prev = new;
332
Paul Bakker1ef120f2013-07-03 17:20:39 +0200333 // Replace cur with new in free_list
334 //
335 new->prev_free = cur->prev_free;
336 new->next_free = cur->next_free;
337 if( new->prev_free != NULL )
338 new->prev_free->next_free = new;
339 else
340 heap.first_free = new;
341
342 if( new->next_free != NULL )
343 new->next_free->prev_free = new;
344
Paul Bakker6e339b52013-07-03 13:37:05 +0200345 cur->alloc = 1;
346 cur->size = len;
347 cur->next = new;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200348 cur->prev_free = NULL;
349 cur->next_free = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200350
Paul Bakker891998e2013-07-03 14:45:05 +0200351#if defined(POLARSSL_MEMORY_DEBUG)
352 heap.header_count++;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +0100353 if( heap.header_count > heap.maximum_header_count )
354 heap.maximum_header_count = heap.header_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200355 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200356 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200357 heap.maximum_used = heap.total_used;
358#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200359#if defined(POLARSSL_MEMORY_BACKTRACE)
360 trace_cnt = backtrace( trace_buffer, MAX_BT );
361 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
362 cur->trace_count = trace_cnt;
363#endif
364
365 if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
366 exit( 1 );
367
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200368 return( ( (unsigned char *) cur ) + sizeof(memory_header) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200369}
370
371static void buffer_alloc_free( void *ptr )
372{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200373 memory_header *hdr, *old = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200374 unsigned char *p = (unsigned char *) ptr;
375
Paul Bakker6e339b52013-07-03 13:37:05 +0200376 if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
377 return;
378
379 if( p < heap.buf || p > heap.buf + heap.len )
380 {
381#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100382 polarssl_fprintf( stderr, "FATAL: polarssl_free() outside of managed "
383 "space\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200384#endif
Paul Bakker41350a92013-07-03 15:33:47 +0200385 exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200386 }
387
388 p -= sizeof(memory_header);
389 hdr = (memory_header *) p;
390
391 if( verify_header( hdr ) != 0 )
392 exit( 1 );
393
394 if( hdr->alloc != 1 )
395 {
396#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100397 polarssl_fprintf( stderr, "FATAL: polarssl_free() on unallocated "
398 "data\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200399#endif
Paul Bakker41350a92013-07-03 15:33:47 +0200400 exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200401 }
402
403 hdr->alloc = 0;
404
Paul Bakker891998e2013-07-03 14:45:05 +0200405#if defined(POLARSSL_MEMORY_DEBUG)
406 heap.free_count++;
407 heap.total_used -= hdr->size;
408#endif
409
Paul Bakker6e339b52013-07-03 13:37:05 +0200410 // Regroup with block before
411 //
412 if( hdr->prev != NULL && hdr->prev->alloc == 0 )
413 {
Paul Bakker891998e2013-07-03 14:45:05 +0200414#if defined(POLARSSL_MEMORY_DEBUG)
415 heap.header_count--;
416#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200417 hdr->prev->size += sizeof(memory_header) + hdr->size;
418 hdr->prev->next = hdr->next;
419 old = hdr;
420 hdr = hdr->prev;
421
422 if( hdr->next != NULL )
423 hdr->next->prev = hdr;
424
425#if defined(POLARSSL_MEMORY_BACKTRACE)
426 free( old->trace );
427#endif
428 memset( old, 0, sizeof(memory_header) );
429 }
430
431 // Regroup with block after
432 //
433 if( hdr->next != NULL && hdr->next->alloc == 0 )
434 {
Paul Bakker891998e2013-07-03 14:45:05 +0200435#if defined(POLARSSL_MEMORY_DEBUG)
436 heap.header_count--;
437#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200438 hdr->size += sizeof(memory_header) + hdr->next->size;
439 old = hdr->next;
440 hdr->next = hdr->next->next;
441
Paul Bakker1ef120f2013-07-03 17:20:39 +0200442 if( hdr->prev_free != NULL || hdr->next_free != NULL )
443 {
444 if( hdr->prev_free != NULL )
445 hdr->prev_free->next_free = hdr->next_free;
446 else
447 heap.first_free = hdr->next_free;
448
449 if( hdr->next_free != NULL )
450 hdr->next_free->prev_free = hdr->prev_free;
451 }
452
453 hdr->prev_free = old->prev_free;
454 hdr->next_free = old->next_free;
455
456 if( hdr->prev_free != NULL )
457 hdr->prev_free->next_free = hdr;
458 else
459 heap.first_free = hdr;
460
461 if( hdr->next_free != NULL )
462 hdr->next_free->prev_free = hdr;
463
Paul Bakker6e339b52013-07-03 13:37:05 +0200464 if( hdr->next != NULL )
465 hdr->next->prev = hdr;
466
467#if defined(POLARSSL_MEMORY_BACKTRACE)
468 free( old->trace );
469#endif
470 memset( old, 0, sizeof(memory_header) );
471 }
472
Paul Bakker1ef120f2013-07-03 17:20:39 +0200473 // Prepend to free_list if we have not merged
474 // (Does not have to stay in same order as prev / next list)
475 //
476 if( old == NULL )
477 {
478 hdr->next_free = heap.first_free;
Manuel Pégourié-Gonnard547ff662014-11-26 15:42:16 +0100479 if( heap.first_free != NULL )
480 heap.first_free->prev_free = hdr;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200481 heap.first_free = hdr;
482 }
483
Paul Bakker6e339b52013-07-03 13:37:05 +0200484#if defined(POLARSSL_MEMORY_BACKTRACE)
485 hdr->trace = NULL;
486 hdr->trace_count = 0;
487#endif
488
489 if( ( heap.verify & MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
490 exit( 1 );
491}
492
Paul Bakkerbf796ac2013-09-28 11:06:38 +0200493void memory_buffer_set_verify( int verify )
494{
495 heap.verify = verify;
496}
497
Paul Bakker6e339b52013-07-03 13:37:05 +0200498int memory_buffer_alloc_verify()
499{
500 return verify_chain();
501}
502
503#if defined(POLARSSL_MEMORY_DEBUG)
504void memory_buffer_alloc_status()
505{
Paul Bakker7dc4c442014-02-01 22:50:26 +0100506 polarssl_fprintf( stderr,
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200507 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
508 "%zu bytes (total %zu bytes), malloc / free: %zu / %zu\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100509 heap.header_count, heap.total_used,
510 heap.maximum_header_count, heap.maximum_used,
511 heap.maximum_header_count * sizeof( memory_header )
512 + heap.maximum_used,
513 heap.malloc_count, heap.free_count );
Paul Bakker891998e2013-07-03 14:45:05 +0200514
Paul Bakker6e339b52013-07-03 13:37:05 +0200515 if( heap.first->next == NULL )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100516 polarssl_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200517 else
518 {
Paul Bakker7dc4c442014-02-01 22:50:26 +0100519 polarssl_fprintf( stderr, "Memory currently allocated:\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200520 debug_chain();
521 }
522}
Paul Bakker119602b2014-02-05 15:42:55 +0100523#endif /* POLARSSL_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200524
Paul Bakker1337aff2013-09-29 14:45:34 +0200525#if defined(POLARSSL_THREADING_C)
526static void *buffer_alloc_malloc_mutexed( size_t len )
527{
528 void *buf;
529 polarssl_mutex_lock( &heap.mutex );
530 buf = buffer_alloc_malloc( len );
531 polarssl_mutex_unlock( &heap.mutex );
532 return( buf );
533}
534
535static void buffer_alloc_free_mutexed( void *ptr )
536{
537 polarssl_mutex_lock( &heap.mutex );
538 buffer_alloc_free( ptr );
539 polarssl_mutex_unlock( &heap.mutex );
540}
Paul Bakker9af723c2014-05-01 13:03:14 +0200541#endif /* POLARSSL_THREADING_C */
Paul Bakker1337aff2013-09-29 14:45:34 +0200542
Paul Bakker6e339b52013-07-03 13:37:05 +0200543int memory_buffer_alloc_init( unsigned char *buf, size_t len )
544{
Paul Bakker6e339b52013-07-03 13:37:05 +0200545 memset( &heap, 0, sizeof(buffer_alloc_ctx) );
546 memset( buf, 0, len );
547
Paul Bakker1337aff2013-09-29 14:45:34 +0200548#if defined(POLARSSL_THREADING_C)
549 polarssl_mutex_init( &heap.mutex );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100550 platform_set_malloc_free( buffer_alloc_malloc_mutexed,
551 buffer_alloc_free_mutexed );
Paul Bakker1337aff2013-09-29 14:45:34 +0200552#else
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100553 platform_set_malloc_free( buffer_alloc_malloc, buffer_alloc_free );
Paul Bakker1337aff2013-09-29 14:45:34 +0200554#endif
555
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200556 if( (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE )
557 {
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100558 /* Adjust len first since buf is used in the computation */
559 len -= POLARSSL_MEMORY_ALIGN_MULTIPLE
560 - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200561 buf += POLARSSL_MEMORY_ALIGN_MULTIPLE
562 - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200563 }
564
Paul Bakker6e339b52013-07-03 13:37:05 +0200565 heap.buf = buf;
566 heap.len = len;
567
568 heap.first = (memory_header *) buf;
569 heap.first->size = len - sizeof(memory_header);
570 heap.first->magic1 = MAGIC1;
571 heap.first->magic2 = MAGIC2;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200572 heap.first_free = heap.first;
Paul Bakker6e339b52013-07-03 13:37:05 +0200573 return( 0 );
574}
575
Paul Bakker1337aff2013-09-29 14:45:34 +0200576void memory_buffer_alloc_free()
577{
578#if defined(POLARSSL_THREADING_C)
579 polarssl_mutex_free( &heap.mutex );
580#endif
Paul Bakker34617722014-06-13 17:20:13 +0200581 polarssl_zeroize( &heap, sizeof(buffer_alloc_ctx) );
Paul Bakker1337aff2013-09-29 14:45:34 +0200582}
583
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100584#if defined(POLARSSL_SELF_TEST)
585static int check_pointer( void *p )
586{
587 if( p == NULL )
588 return( -1 );
589
590 if( (size_t) p % POLARSSL_MEMORY_ALIGN_MULTIPLE != 0 )
591 return( -1 );
592
593 return( 0 );
594}
595
596static int check_all_free( )
597{
598 if( heap.current_alloc_size != 0 ||
599 heap.first != heap.first_free ||
600 (void *) heap.first != (void *) heap.buf )
601 {
602 return( -1 );
603 }
604
605 return( 0 );
606}
607
608#define TEST_ASSERT( condition ) \
609 if( ! (condition) ) \
610 { \
611 if( verbose != 0 ) \
612 polarssl_printf( "failed\n" ); \
613 \
614 ret = 1; \
615 goto cleanup; \
616 }
617
618int memory_buffer_alloc_self_test( int verbose )
619{
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100620 unsigned char buf[1024];
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100621 unsigned char *p, *q, *r, *end;
622 int ret = 0;
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100623
624 if( verbose != 0 )
625 polarssl_printf( " MBA test #1 (basic alloc-free cycle): " );
626
627 memory_buffer_alloc_init( buf, sizeof( buf ) );
628
629 p = polarssl_malloc( 1 );
630 q = polarssl_malloc( 128 );
631 r = polarssl_malloc( 16 );
632
633 TEST_ASSERT( check_pointer( p ) == 0 &&
634 check_pointer( q ) == 0 &&
635 check_pointer( r ) == 0 );
636
637 polarssl_free( r );
638 polarssl_free( q );
639 polarssl_free( p );
640
641 TEST_ASSERT( check_all_free( ) == 0 );
642
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100643 /* Memorize end to compare with the next test */
644 end = heap.buf + heap.len;
645
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100646 memory_buffer_alloc_free( );
647
648 if( verbose != 0 )
649 polarssl_printf( "passed\n" );
650
651 if( verbose != 0 )
652 polarssl_printf( " MBA test #2 (buf not aligned): " );
653
654 memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
655
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100656 TEST_ASSERT( heap.buf + heap.len == end );
657
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100658 p = polarssl_malloc( 1 );
659 q = polarssl_malloc( 128 );
660 r = polarssl_malloc( 16 );
661
662 TEST_ASSERT( check_pointer( p ) == 0 &&
663 check_pointer( q ) == 0 &&
664 check_pointer( r ) == 0 );
665
666 polarssl_free( r );
667 polarssl_free( q );
668 polarssl_free( p );
669
670 TEST_ASSERT( check_all_free( ) == 0 );
671
672 memory_buffer_alloc_free( );
673
674 if( verbose != 0 )
675 polarssl_printf( "passed\n" );
676
677 if( verbose != 0 )
678 polarssl_printf( " MBA test #3 (full): " );
679
680 memory_buffer_alloc_init( buf, sizeof( buf ) );
681
682 p = polarssl_malloc( sizeof( buf ) - sizeof( memory_header ) );
683
684 TEST_ASSERT( check_pointer( p ) == 0 );
685 TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
686
687 polarssl_free( p );
688
689 p = polarssl_malloc( sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
690 q = polarssl_malloc( 16 );
691
692 TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
693 TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
694
695 polarssl_free( q );
696
697 TEST_ASSERT( polarssl_malloc( 17 ) == NULL );
698
699 polarssl_free( p );
700
701 TEST_ASSERT( check_all_free( ) == 0 );
702
703 memory_buffer_alloc_free( );
704
705 if( verbose != 0 )
706 polarssl_printf( "passed\n" );
707
708cleanup:
709 memory_buffer_alloc_free( );
710
711 return( ret );
712}
713#endif /* POLARSSL_SELF_TEST */
714
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100715#endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */