blob: b7d583b008c0c1629ddff226f7f690f453fbb1f6 [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
33#include <string.h>
34
35#if defined(POLARSSL_MEMORY_DEBUG)
Rich Evans00ab4702015-02-06 13:43:58 +000036#if defined(POLARSSL_PLATFORM_C)
37#include "polarssl/platform.h"
38#else
Paul Bakker6e339b52013-07-03 13:37:05 +020039#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000040#define polarssl_fprintf fprintf
41#endif /* POLARSSL_PLATFORM_C */
42#endif /* POLARSSL_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +020043#if defined(POLARSSL_MEMORY_BACKTRACE)
44#include <execinfo.h>
45#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020046
Paul Bakker1337aff2013-09-29 14:45:34 +020047#if defined(POLARSSL_THREADING_C)
48#include "polarssl/threading.h"
49#endif
50
Paul Bakker34617722014-06-13 17:20:13 +020051/* Implementation that should never be optimized out by the compiler */
52static void polarssl_zeroize( void *v, size_t n ) {
53 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
54}
55
Paul Bakker6e339b52013-07-03 13:37:05 +020056#define MAGIC1 0xFF00AA55
57#define MAGIC2 0xEE119966
58#define MAX_BT 20
59
60typedef struct _memory_header memory_header;
61struct _memory_header
62{
63 size_t magic1;
64 size_t size;
65 size_t alloc;
66 memory_header *prev;
67 memory_header *next;
Paul Bakker1ef120f2013-07-03 17:20:39 +020068 memory_header *prev_free;
69 memory_header *next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020070#if defined(POLARSSL_MEMORY_BACKTRACE)
71 char **trace;
72 size_t trace_count;
73#endif
74 size_t magic2;
75};
76
77typedef struct
78{
79 unsigned char *buf;
80 size_t len;
81 memory_header *first;
Paul Bakker1ef120f2013-07-03 17:20:39 +020082 memory_header *first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020083 size_t current_alloc_size;
84 int verify;
Paul Bakker891998e2013-07-03 14:45:05 +020085#if defined(POLARSSL_MEMORY_DEBUG)
86 size_t malloc_count;
87 size_t free_count;
88 size_t total_used;
89 size_t maximum_used;
90 size_t header_count;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +010091 size_t maximum_header_count;
Paul Bakker891998e2013-07-03 14:45:05 +020092#endif
Paul Bakker1337aff2013-09-29 14:45:34 +020093#if defined(POLARSSL_THREADING_C)
94 threading_mutex_t mutex;
95#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020096}
97buffer_alloc_ctx;
98
99static buffer_alloc_ctx heap;
100
101#if defined(POLARSSL_MEMORY_DEBUG)
102static void debug_header( memory_header *hdr )
103{
104#if defined(POLARSSL_MEMORY_BACKTRACE)
105 size_t i;
106#endif
107
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200108 polarssl_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
109 "ALLOC(%zu), SIZE(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100110 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
111 hdr->alloc, hdr->size );
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200112 polarssl_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100113 (size_t) hdr->prev_free, (size_t) hdr->next_free );
Paul Bakker6e339b52013-07-03 13:37:05 +0200114
115#if defined(POLARSSL_MEMORY_BACKTRACE)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100116 polarssl_fprintf( stderr, "TRACE: \n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200117 for( i = 0; i < hdr->trace_count; i++ )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100118 polarssl_fprintf( stderr, "%s\n", hdr->trace[i] );
119 polarssl_fprintf( stderr, "\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200120#endif
121}
122
123static void debug_chain()
124{
125 memory_header *cur = heap.first;
126
Paul Bakker7dc4c442014-02-01 22:50:26 +0100127 polarssl_fprintf( stderr, "\nBlock list\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200128 while( cur != NULL )
129 {
130 debug_header( cur );
Paul Bakker6e339b52013-07-03 13:37:05 +0200131 cur = cur->next;
132 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200133
Paul Bakker7dc4c442014-02-01 22:50:26 +0100134 polarssl_fprintf( stderr, "Free list\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200135 cur = heap.first_free;
136
137 while( cur != NULL )
138 {
139 debug_header( cur );
140 cur = cur->next_free;
141 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200142}
143#endif /* POLARSSL_MEMORY_DEBUG */
144
145static int verify_header( memory_header *hdr )
146{
147 if( hdr->magic1 != MAGIC1 )
148 {
149#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100150 polarssl_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200151#endif
152 return( 1 );
153 }
154
155 if( hdr->magic2 != MAGIC2 )
156 {
157#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100158 polarssl_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200159#endif
160 return( 1 );
161 }
162
163 if( hdr->alloc > 1 )
164 {
165#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100166 polarssl_fprintf( stderr, "FATAL: alloc has illegal value\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200167#endif
168 return( 1 );
169 }
170
Paul Bakker1ef120f2013-07-03 17:20:39 +0200171 if( hdr->prev != NULL && hdr->prev == hdr->next )
172 {
173#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100174 polarssl_fprintf( stderr, "FATAL: prev == next\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200175#endif
176 return( 1 );
177 }
178
179 if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
180 {
181#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100182 polarssl_fprintf( stderr, "FATAL: prev_free == next_free\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200183#endif
184 return( 1 );
185 }
186
Paul Bakker6e339b52013-07-03 13:37:05 +0200187 return( 0 );
188}
189
190static int verify_chain()
191{
192 memory_header *prv = heap.first, *cur = heap.first->next;
193
194 if( verify_header( heap.first ) != 0 )
195 {
196#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100197 polarssl_fprintf( stderr, "FATAL: verification of first header "
198 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200199#endif
200 return( 1 );
201 }
202
203 if( heap.first->prev != NULL )
204 {
205#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100206 polarssl_fprintf( stderr, "FATAL: verification failed: "
207 "first->prev != NULL\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200208#endif
209 return( 1 );
210 }
211
212 while( cur != NULL )
213 {
214 if( verify_header( cur ) != 0 )
215 {
216#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100217 polarssl_fprintf( stderr, "FATAL: verification of header "
218 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200219#endif
220 return( 1 );
221 }
222
223 if( cur->prev != prv )
224 {
225#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100226 polarssl_fprintf( stderr, "FATAL: verification failed: "
227 "cur->prev != prv\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200228#endif
229 return( 1 );
230 }
231
232 prv = cur;
233 cur = cur->next;
234 }
235
236 return( 0 );
237}
238
239static void *buffer_alloc_malloc( size_t len )
240{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200241 memory_header *new, *cur = heap.first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200242 unsigned char *p;
243#if defined(POLARSSL_MEMORY_BACKTRACE)
244 void *trace_buffer[MAX_BT];
245 size_t trace_cnt;
246#endif
247
248 if( heap.buf == NULL || heap.first == NULL )
249 return( NULL );
250
251 if( len % POLARSSL_MEMORY_ALIGN_MULTIPLE )
252 {
253 len -= len % POLARSSL_MEMORY_ALIGN_MULTIPLE;
254 len += POLARSSL_MEMORY_ALIGN_MULTIPLE;
255 }
256
257 // Find block that fits
258 //
259 while( cur != NULL )
260 {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200261 if( cur->size >= len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200262 break;
263
Paul Bakker1ef120f2013-07-03 17:20:39 +0200264 cur = cur->next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200265 }
266
267 if( cur == NULL )
268 return( NULL );
269
Paul Bakker1ef120f2013-07-03 17:20:39 +0200270 if( cur->alloc != 0 )
271 {
272#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100273 polarssl_fprintf( stderr, "FATAL: block in free_list but allocated "
274 "data\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200275#endif
276 exit( 1 );
277 }
278
Paul Bakker891998e2013-07-03 14:45:05 +0200279#if defined(POLARSSL_MEMORY_DEBUG)
280 heap.malloc_count++;
281#endif
282
Paul Bakker6e339b52013-07-03 13:37:05 +0200283 // Found location, split block if > memory_header + 4 room left
284 //
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200285 if( cur->size - len < sizeof(memory_header) +
286 POLARSSL_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200287 {
288 cur->alloc = 1;
289
Paul Bakker1ef120f2013-07-03 17:20:39 +0200290 // Remove from free_list
291 //
292 if( cur->prev_free != NULL )
293 cur->prev_free->next_free = cur->next_free;
294 else
295 heap.first_free = cur->next_free;
296
297 if( cur->next_free != NULL )
298 cur->next_free->prev_free = cur->prev_free;
299
300 cur->prev_free = NULL;
301 cur->next_free = NULL;
302
Paul Bakker891998e2013-07-03 14:45:05 +0200303#if defined(POLARSSL_MEMORY_DEBUG)
304 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200305 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200306 heap.maximum_used = heap.total_used;
307#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200308#if defined(POLARSSL_MEMORY_BACKTRACE)
309 trace_cnt = backtrace( trace_buffer, MAX_BT );
310 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
311 cur->trace_count = trace_cnt;
312#endif
313
314 if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
315 exit( 1 );
316
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200317 return( ( (unsigned char *) cur ) + sizeof(memory_header) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200318 }
319
320 p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
321 new = (memory_header *) p;
322
323 new->size = cur->size - len - sizeof(memory_header);
324 new->alloc = 0;
325 new->prev = cur;
326 new->next = cur->next;
327#if defined(POLARSSL_MEMORY_BACKTRACE)
328 new->trace = NULL;
329 new->trace_count = 0;
330#endif
331 new->magic1 = MAGIC1;
332 new->magic2 = MAGIC2;
333
334 if( new->next != NULL )
335 new->next->prev = new;
336
Paul Bakker1ef120f2013-07-03 17:20:39 +0200337 // Replace cur with new in free_list
338 //
339 new->prev_free = cur->prev_free;
340 new->next_free = cur->next_free;
341 if( new->prev_free != NULL )
342 new->prev_free->next_free = new;
343 else
344 heap.first_free = new;
345
346 if( new->next_free != NULL )
347 new->next_free->prev_free = new;
348
Paul Bakker6e339b52013-07-03 13:37:05 +0200349 cur->alloc = 1;
350 cur->size = len;
351 cur->next = new;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200352 cur->prev_free = NULL;
353 cur->next_free = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200354
Paul Bakker891998e2013-07-03 14:45:05 +0200355#if defined(POLARSSL_MEMORY_DEBUG)
356 heap.header_count++;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +0100357 if( heap.header_count > heap.maximum_header_count )
358 heap.maximum_header_count = heap.header_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200359 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200360 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200361 heap.maximum_used = heap.total_used;
362#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200363#if defined(POLARSSL_MEMORY_BACKTRACE)
364 trace_cnt = backtrace( trace_buffer, MAX_BT );
365 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
366 cur->trace_count = trace_cnt;
367#endif
368
369 if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
370 exit( 1 );
371
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200372 return( ( (unsigned char *) cur ) + sizeof(memory_header) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200373}
374
375static void buffer_alloc_free( void *ptr )
376{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200377 memory_header *hdr, *old = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200378 unsigned char *p = (unsigned char *) ptr;
379
Paul Bakker6e339b52013-07-03 13:37:05 +0200380 if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
381 return;
382
383 if( p < heap.buf || p > heap.buf + heap.len )
384 {
385#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100386 polarssl_fprintf( stderr, "FATAL: polarssl_free() outside of managed "
387 "space\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200388#endif
Paul Bakker41350a92013-07-03 15:33:47 +0200389 exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200390 }
391
392 p -= sizeof(memory_header);
393 hdr = (memory_header *) p;
394
395 if( verify_header( hdr ) != 0 )
396 exit( 1 );
397
398 if( hdr->alloc != 1 )
399 {
400#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100401 polarssl_fprintf( stderr, "FATAL: polarssl_free() on unallocated "
402 "data\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200403#endif
Paul Bakker41350a92013-07-03 15:33:47 +0200404 exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200405 }
406
407 hdr->alloc = 0;
408
Paul Bakker891998e2013-07-03 14:45:05 +0200409#if defined(POLARSSL_MEMORY_DEBUG)
410 heap.free_count++;
411 heap.total_used -= hdr->size;
412#endif
413
Paul Bakker6e339b52013-07-03 13:37:05 +0200414 // Regroup with block before
415 //
416 if( hdr->prev != NULL && hdr->prev->alloc == 0 )
417 {
Paul Bakker891998e2013-07-03 14:45:05 +0200418#if defined(POLARSSL_MEMORY_DEBUG)
419 heap.header_count--;
420#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200421 hdr->prev->size += sizeof(memory_header) + hdr->size;
422 hdr->prev->next = hdr->next;
423 old = hdr;
424 hdr = hdr->prev;
425
426 if( hdr->next != NULL )
427 hdr->next->prev = hdr;
428
429#if defined(POLARSSL_MEMORY_BACKTRACE)
430 free( old->trace );
431#endif
432 memset( old, 0, sizeof(memory_header) );
433 }
434
435 // Regroup with block after
436 //
437 if( hdr->next != NULL && hdr->next->alloc == 0 )
438 {
Paul Bakker891998e2013-07-03 14:45:05 +0200439#if defined(POLARSSL_MEMORY_DEBUG)
440 heap.header_count--;
441#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200442 hdr->size += sizeof(memory_header) + hdr->next->size;
443 old = hdr->next;
444 hdr->next = hdr->next->next;
445
Paul Bakker1ef120f2013-07-03 17:20:39 +0200446 if( hdr->prev_free != NULL || hdr->next_free != NULL )
447 {
448 if( hdr->prev_free != NULL )
449 hdr->prev_free->next_free = hdr->next_free;
450 else
451 heap.first_free = hdr->next_free;
452
453 if( hdr->next_free != NULL )
454 hdr->next_free->prev_free = hdr->prev_free;
455 }
456
457 hdr->prev_free = old->prev_free;
458 hdr->next_free = old->next_free;
459
460 if( hdr->prev_free != NULL )
461 hdr->prev_free->next_free = hdr;
462 else
463 heap.first_free = hdr;
464
465 if( hdr->next_free != NULL )
466 hdr->next_free->prev_free = hdr;
467
Paul Bakker6e339b52013-07-03 13:37:05 +0200468 if( hdr->next != NULL )
469 hdr->next->prev = hdr;
470
471#if defined(POLARSSL_MEMORY_BACKTRACE)
472 free( old->trace );
473#endif
474 memset( old, 0, sizeof(memory_header) );
475 }
476
Paul Bakker1ef120f2013-07-03 17:20:39 +0200477 // Prepend to free_list if we have not merged
478 // (Does not have to stay in same order as prev / next list)
479 //
480 if( old == NULL )
481 {
482 hdr->next_free = heap.first_free;
Manuel Pégourié-Gonnard547ff662014-11-26 15:42:16 +0100483 if( heap.first_free != NULL )
484 heap.first_free->prev_free = hdr;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200485 heap.first_free = hdr;
486 }
487
Paul Bakker6e339b52013-07-03 13:37:05 +0200488#if defined(POLARSSL_MEMORY_BACKTRACE)
489 hdr->trace = NULL;
490 hdr->trace_count = 0;
491#endif
492
493 if( ( heap.verify & MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
494 exit( 1 );
495}
496
Paul Bakkerbf796ac2013-09-28 11:06:38 +0200497void memory_buffer_set_verify( int verify )
498{
499 heap.verify = verify;
500}
501
Paul Bakker6e339b52013-07-03 13:37:05 +0200502int memory_buffer_alloc_verify()
503{
504 return verify_chain();
505}
506
507#if defined(POLARSSL_MEMORY_DEBUG)
508void memory_buffer_alloc_status()
509{
Paul Bakker7dc4c442014-02-01 22:50:26 +0100510 polarssl_fprintf( stderr,
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200511 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
512 "%zu bytes (total %zu bytes), malloc / free: %zu / %zu\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100513 heap.header_count, heap.total_used,
514 heap.maximum_header_count, heap.maximum_used,
515 heap.maximum_header_count * sizeof( memory_header )
516 + heap.maximum_used,
517 heap.malloc_count, heap.free_count );
Paul Bakker891998e2013-07-03 14:45:05 +0200518
Paul Bakker6e339b52013-07-03 13:37:05 +0200519 if( heap.first->next == NULL )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100520 polarssl_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200521 else
522 {
Paul Bakker7dc4c442014-02-01 22:50:26 +0100523 polarssl_fprintf( stderr, "Memory currently allocated:\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200524 debug_chain();
525 }
526}
Paul Bakker119602b2014-02-05 15:42:55 +0100527#endif /* POLARSSL_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200528
Paul Bakker1337aff2013-09-29 14:45:34 +0200529#if defined(POLARSSL_THREADING_C)
530static void *buffer_alloc_malloc_mutexed( size_t len )
531{
532 void *buf;
533 polarssl_mutex_lock( &heap.mutex );
534 buf = buffer_alloc_malloc( len );
535 polarssl_mutex_unlock( &heap.mutex );
536 return( buf );
537}
538
539static void buffer_alloc_free_mutexed( void *ptr )
540{
541 polarssl_mutex_lock( &heap.mutex );
542 buffer_alloc_free( ptr );
543 polarssl_mutex_unlock( &heap.mutex );
544}
Paul Bakker9af723c2014-05-01 13:03:14 +0200545#endif /* POLARSSL_THREADING_C */
Paul Bakker1337aff2013-09-29 14:45:34 +0200546
Paul Bakker6e339b52013-07-03 13:37:05 +0200547int memory_buffer_alloc_init( unsigned char *buf, size_t len )
548{
Paul Bakker6e339b52013-07-03 13:37:05 +0200549 memset( &heap, 0, sizeof(buffer_alloc_ctx) );
550 memset( buf, 0, len );
551
Paul Bakker1337aff2013-09-29 14:45:34 +0200552#if defined(POLARSSL_THREADING_C)
553 polarssl_mutex_init( &heap.mutex );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100554 platform_set_malloc_free( buffer_alloc_malloc_mutexed,
555 buffer_alloc_free_mutexed );
Paul Bakker1337aff2013-09-29 14:45:34 +0200556#else
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100557 platform_set_malloc_free( buffer_alloc_malloc, buffer_alloc_free );
Paul Bakker1337aff2013-09-29 14:45:34 +0200558#endif
559
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200560 if( (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE )
561 {
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100562 /* Adjust len first since buf is used in the computation */
563 len -= POLARSSL_MEMORY_ALIGN_MULTIPLE
564 - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200565 buf += POLARSSL_MEMORY_ALIGN_MULTIPLE
566 - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200567 }
568
Paul Bakker6e339b52013-07-03 13:37:05 +0200569 heap.buf = buf;
570 heap.len = len;
571
572 heap.first = (memory_header *) buf;
573 heap.first->size = len - sizeof(memory_header);
574 heap.first->magic1 = MAGIC1;
575 heap.first->magic2 = MAGIC2;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200576 heap.first_free = heap.first;
Paul Bakker6e339b52013-07-03 13:37:05 +0200577 return( 0 );
578}
579
Paul Bakker1337aff2013-09-29 14:45:34 +0200580void memory_buffer_alloc_free()
581{
582#if defined(POLARSSL_THREADING_C)
583 polarssl_mutex_free( &heap.mutex );
584#endif
Paul Bakker34617722014-06-13 17:20:13 +0200585 polarssl_zeroize( &heap, sizeof(buffer_alloc_ctx) );
Paul Bakker1337aff2013-09-29 14:45:34 +0200586}
587
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100588#if defined(POLARSSL_SELF_TEST)
589static int check_pointer( void *p )
590{
591 if( p == NULL )
592 return( -1 );
593
594 if( (size_t) p % POLARSSL_MEMORY_ALIGN_MULTIPLE != 0 )
595 return( -1 );
596
597 return( 0 );
598}
599
600static int check_all_free( )
601{
602 if( heap.current_alloc_size != 0 ||
603 heap.first != heap.first_free ||
604 (void *) heap.first != (void *) heap.buf )
605 {
606 return( -1 );
607 }
608
609 return( 0 );
610}
611
612#define TEST_ASSERT( condition ) \
613 if( ! (condition) ) \
614 { \
615 if( verbose != 0 ) \
616 polarssl_printf( "failed\n" ); \
617 \
618 ret = 1; \
619 goto cleanup; \
620 }
621
622int memory_buffer_alloc_self_test( int verbose )
623{
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100624 unsigned char buf[1024];
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100625 unsigned char *p, *q, *r, *end;
626 int ret = 0;
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100627
628 if( verbose != 0 )
629 polarssl_printf( " MBA test #1 (basic alloc-free cycle): " );
630
631 memory_buffer_alloc_init( buf, sizeof( buf ) );
632
633 p = polarssl_malloc( 1 );
634 q = polarssl_malloc( 128 );
635 r = polarssl_malloc( 16 );
636
637 TEST_ASSERT( check_pointer( p ) == 0 &&
638 check_pointer( q ) == 0 &&
639 check_pointer( r ) == 0 );
640
641 polarssl_free( r );
642 polarssl_free( q );
643 polarssl_free( p );
644
645 TEST_ASSERT( check_all_free( ) == 0 );
646
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100647 /* Memorize end to compare with the next test */
648 end = heap.buf + heap.len;
649
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100650 memory_buffer_alloc_free( );
651
652 if( verbose != 0 )
653 polarssl_printf( "passed\n" );
654
655 if( verbose != 0 )
656 polarssl_printf( " MBA test #2 (buf not aligned): " );
657
658 memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
659
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100660 TEST_ASSERT( heap.buf + heap.len == end );
661
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100662 p = polarssl_malloc( 1 );
663 q = polarssl_malloc( 128 );
664 r = polarssl_malloc( 16 );
665
666 TEST_ASSERT( check_pointer( p ) == 0 &&
667 check_pointer( q ) == 0 &&
668 check_pointer( r ) == 0 );
669
670 polarssl_free( r );
671 polarssl_free( q );
672 polarssl_free( p );
673
674 TEST_ASSERT( check_all_free( ) == 0 );
675
676 memory_buffer_alloc_free( );
677
678 if( verbose != 0 )
679 polarssl_printf( "passed\n" );
680
681 if( verbose != 0 )
682 polarssl_printf( " MBA test #3 (full): " );
683
684 memory_buffer_alloc_init( buf, sizeof( buf ) );
685
686 p = polarssl_malloc( sizeof( buf ) - sizeof( memory_header ) );
687
688 TEST_ASSERT( check_pointer( p ) == 0 );
689 TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
690
691 polarssl_free( p );
692
693 p = polarssl_malloc( sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
694 q = polarssl_malloc( 16 );
695
696 TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
697 TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
698
699 polarssl_free( q );
700
701 TEST_ASSERT( polarssl_malloc( 17 ) == NULL );
702
703 polarssl_free( p );
704
705 TEST_ASSERT( check_all_free( ) == 0 );
706
707 memory_buffer_alloc_free( );
708
709 if( verbose != 0 )
710 polarssl_printf( "passed\n" );
711
712cleanup:
713 memory_buffer_alloc_free( );
714
715 return( ret );
716}
717#endif /* POLARSSL_SELF_TEST */
718
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100719#endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */