blob: 4f96018e30805baf5bc091d1880b008786cdcdb0 [file] [log] [blame]
Paul Bakker6e339b52013-07-03 13:37:05 +02001/*
2 * Buffer-based memory allocator
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker6e339b52013-07-03 13:37:05 +02005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker6e339b52013-07-03 13:37:05 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020031
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010032#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Paul Bakker6e339b52013-07-03 13:37:05 +020033
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010034#include "polarssl/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020035
36#include <string.h>
37
38#if defined(POLARSSL_MEMORY_DEBUG)
39#include <stdio.h>
Manuel Pégourié-Gonnard2fbf3112014-07-12 03:32:40 +020040#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020041#if defined(POLARSSL_MEMORY_BACKTRACE)
42#include <execinfo.h>
43#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020044
Paul Bakker1337aff2013-09-29 14:45:34 +020045#if defined(POLARSSL_THREADING_C)
46#include "polarssl/threading.h"
47#endif
48
Paul Bakker7dc4c442014-02-01 22:50:26 +010049#if defined(POLARSSL_PLATFORM_C)
50#include "polarssl/platform.h"
51#else
52#define polarssl_fprintf fprintf
53#endif
54
Paul Bakker34617722014-06-13 17:20:13 +020055/* Implementation that should never be optimized out by the compiler */
56static void polarssl_zeroize( void *v, size_t n ) {
57 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
58}
59
Paul Bakker6e339b52013-07-03 13:37:05 +020060#define MAGIC1 0xFF00AA55
61#define MAGIC2 0xEE119966
62#define MAX_BT 20
63
64typedef struct _memory_header memory_header;
65struct _memory_header
66{
67 size_t magic1;
68 size_t size;
69 size_t alloc;
70 memory_header *prev;
71 memory_header *next;
Paul Bakker1ef120f2013-07-03 17:20:39 +020072 memory_header *prev_free;
73 memory_header *next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020074#if defined(POLARSSL_MEMORY_BACKTRACE)
75 char **trace;
76 size_t trace_count;
77#endif
78 size_t magic2;
79};
80
81typedef struct
82{
83 unsigned char *buf;
84 size_t len;
85 memory_header *first;
Paul Bakker1ef120f2013-07-03 17:20:39 +020086 memory_header *first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020087 size_t current_alloc_size;
88 int verify;
Paul Bakker891998e2013-07-03 14:45:05 +020089#if defined(POLARSSL_MEMORY_DEBUG)
90 size_t malloc_count;
91 size_t free_count;
92 size_t total_used;
93 size_t maximum_used;
94 size_t header_count;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +010095 size_t maximum_header_count;
Paul Bakker891998e2013-07-03 14:45:05 +020096#endif
Paul Bakker1337aff2013-09-29 14:45:34 +020097#if defined(POLARSSL_THREADING_C)
98 threading_mutex_t mutex;
99#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200100}
101buffer_alloc_ctx;
102
103static buffer_alloc_ctx heap;
104
105#if defined(POLARSSL_MEMORY_DEBUG)
106static void debug_header( memory_header *hdr )
107{
108#if defined(POLARSSL_MEMORY_BACKTRACE)
109 size_t i;
110#endif
111
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200112 polarssl_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
113 "ALLOC(%zu), SIZE(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100114 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
115 hdr->alloc, hdr->size );
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200116 polarssl_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100117 (size_t) hdr->prev_free, (size_t) hdr->next_free );
Paul Bakker6e339b52013-07-03 13:37:05 +0200118
119#if defined(POLARSSL_MEMORY_BACKTRACE)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100120 polarssl_fprintf( stderr, "TRACE: \n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200121 for( i = 0; i < hdr->trace_count; i++ )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100122 polarssl_fprintf( stderr, "%s\n", hdr->trace[i] );
123 polarssl_fprintf( stderr, "\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200124#endif
125}
126
127static void debug_chain()
128{
129 memory_header *cur = heap.first;
130
Paul Bakker7dc4c442014-02-01 22:50:26 +0100131 polarssl_fprintf( stderr, "\nBlock list\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200132 while( cur != NULL )
133 {
134 debug_header( cur );
Paul Bakker6e339b52013-07-03 13:37:05 +0200135 cur = cur->next;
136 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200137
Paul Bakker7dc4c442014-02-01 22:50:26 +0100138 polarssl_fprintf( stderr, "Free list\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200139 cur = heap.first_free;
140
141 while( cur != NULL )
142 {
143 debug_header( cur );
144 cur = cur->next_free;
145 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200146}
147#endif /* POLARSSL_MEMORY_DEBUG */
148
149static int verify_header( memory_header *hdr )
150{
151 if( hdr->magic1 != MAGIC1 )
152 {
153#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100154 polarssl_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200155#endif
156 return( 1 );
157 }
158
159 if( hdr->magic2 != MAGIC2 )
160 {
161#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100162 polarssl_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200163#endif
164 return( 1 );
165 }
166
167 if( hdr->alloc > 1 )
168 {
169#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100170 polarssl_fprintf( stderr, "FATAL: alloc has illegal value\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200171#endif
172 return( 1 );
173 }
174
Paul Bakker1ef120f2013-07-03 17:20:39 +0200175 if( hdr->prev != NULL && hdr->prev == hdr->next )
176 {
177#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100178 polarssl_fprintf( stderr, "FATAL: prev == next\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200179#endif
180 return( 1 );
181 }
182
183 if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
184 {
185#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100186 polarssl_fprintf( stderr, "FATAL: prev_free == next_free\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200187#endif
188 return( 1 );
189 }
190
Paul Bakker6e339b52013-07-03 13:37:05 +0200191 return( 0 );
192}
193
194static int verify_chain()
195{
196 memory_header *prv = heap.first, *cur = heap.first->next;
197
198 if( verify_header( heap.first ) != 0 )
199 {
200#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100201 polarssl_fprintf( stderr, "FATAL: verification of first header "
202 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200203#endif
204 return( 1 );
205 }
206
207 if( heap.first->prev != NULL )
208 {
209#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100210 polarssl_fprintf( stderr, "FATAL: verification failed: "
211 "first->prev != NULL\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200212#endif
213 return( 1 );
214 }
215
216 while( cur != NULL )
217 {
218 if( verify_header( cur ) != 0 )
219 {
220#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100221 polarssl_fprintf( stderr, "FATAL: verification of header "
222 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200223#endif
224 return( 1 );
225 }
226
227 if( cur->prev != prv )
228 {
229#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100230 polarssl_fprintf( stderr, "FATAL: verification failed: "
231 "cur->prev != prv\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200232#endif
233 return( 1 );
234 }
235
236 prv = cur;
237 cur = cur->next;
238 }
239
240 return( 0 );
241}
242
243static void *buffer_alloc_malloc( size_t len )
244{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200245 memory_header *new, *cur = heap.first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200246 unsigned char *p;
247#if defined(POLARSSL_MEMORY_BACKTRACE)
248 void *trace_buffer[MAX_BT];
249 size_t trace_cnt;
250#endif
251
252 if( heap.buf == NULL || heap.first == NULL )
253 return( NULL );
254
255 if( len % POLARSSL_MEMORY_ALIGN_MULTIPLE )
256 {
257 len -= len % POLARSSL_MEMORY_ALIGN_MULTIPLE;
258 len += POLARSSL_MEMORY_ALIGN_MULTIPLE;
259 }
260
261 // Find block that fits
262 //
263 while( cur != NULL )
264 {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200265 if( cur->size >= len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200266 break;
267
Paul Bakker1ef120f2013-07-03 17:20:39 +0200268 cur = cur->next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200269 }
270
271 if( cur == NULL )
272 return( NULL );
273
Paul Bakker1ef120f2013-07-03 17:20:39 +0200274 if( cur->alloc != 0 )
275 {
276#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100277 polarssl_fprintf( stderr, "FATAL: block in free_list but allocated "
278 "data\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200279#endif
280 exit( 1 );
281 }
282
Paul Bakker891998e2013-07-03 14:45:05 +0200283#if defined(POLARSSL_MEMORY_DEBUG)
284 heap.malloc_count++;
285#endif
286
Paul Bakker6e339b52013-07-03 13:37:05 +0200287 // Found location, split block if > memory_header + 4 room left
288 //
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200289 if( cur->size - len < sizeof(memory_header) +
290 POLARSSL_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200291 {
292 cur->alloc = 1;
293
Paul Bakker1ef120f2013-07-03 17:20:39 +0200294 // Remove from free_list
295 //
296 if( cur->prev_free != NULL )
297 cur->prev_free->next_free = cur->next_free;
298 else
299 heap.first_free = cur->next_free;
300
301 if( cur->next_free != NULL )
302 cur->next_free->prev_free = cur->prev_free;
303
304 cur->prev_free = NULL;
305 cur->next_free = NULL;
306
Paul Bakker891998e2013-07-03 14:45:05 +0200307#if defined(POLARSSL_MEMORY_DEBUG)
308 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200309 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200310 heap.maximum_used = heap.total_used;
311#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200312#if defined(POLARSSL_MEMORY_BACKTRACE)
313 trace_cnt = backtrace( trace_buffer, MAX_BT );
314 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
315 cur->trace_count = trace_cnt;
316#endif
317
318 if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
319 exit( 1 );
320
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200321 return( ( (unsigned char *) cur ) + sizeof(memory_header) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200322 }
323
324 p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
325 new = (memory_header *) p;
326
327 new->size = cur->size - len - sizeof(memory_header);
328 new->alloc = 0;
329 new->prev = cur;
330 new->next = cur->next;
331#if defined(POLARSSL_MEMORY_BACKTRACE)
332 new->trace = NULL;
333 new->trace_count = 0;
334#endif
335 new->magic1 = MAGIC1;
336 new->magic2 = MAGIC2;
337
338 if( new->next != NULL )
339 new->next->prev = new;
340
Paul Bakker1ef120f2013-07-03 17:20:39 +0200341 // Replace cur with new in free_list
342 //
343 new->prev_free = cur->prev_free;
344 new->next_free = cur->next_free;
345 if( new->prev_free != NULL )
346 new->prev_free->next_free = new;
347 else
348 heap.first_free = new;
349
350 if( new->next_free != NULL )
351 new->next_free->prev_free = new;
352
Paul Bakker6e339b52013-07-03 13:37:05 +0200353 cur->alloc = 1;
354 cur->size = len;
355 cur->next = new;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200356 cur->prev_free = NULL;
357 cur->next_free = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200358
Paul Bakker891998e2013-07-03 14:45:05 +0200359#if defined(POLARSSL_MEMORY_DEBUG)
360 heap.header_count++;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +0100361 if( heap.header_count > heap.maximum_header_count )
362 heap.maximum_header_count = heap.header_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200363 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200364 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200365 heap.maximum_used = heap.total_used;
366#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200367#if defined(POLARSSL_MEMORY_BACKTRACE)
368 trace_cnt = backtrace( trace_buffer, MAX_BT );
369 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
370 cur->trace_count = trace_cnt;
371#endif
372
373 if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
374 exit( 1 );
375
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200376 return( ( (unsigned char *) cur ) + sizeof(memory_header) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200377}
378
379static void buffer_alloc_free( void *ptr )
380{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200381 memory_header *hdr, *old = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200382 unsigned char *p = (unsigned char *) ptr;
383
Paul Bakker6e339b52013-07-03 13:37:05 +0200384 if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
385 return;
386
387 if( p < heap.buf || p > heap.buf + heap.len )
388 {
389#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100390 polarssl_fprintf( stderr, "FATAL: polarssl_free() outside of managed "
391 "space\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200392#endif
Paul Bakker41350a92013-07-03 15:33:47 +0200393 exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200394 }
395
396 p -= sizeof(memory_header);
397 hdr = (memory_header *) p;
398
399 if( verify_header( hdr ) != 0 )
400 exit( 1 );
401
402 if( hdr->alloc != 1 )
403 {
404#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100405 polarssl_fprintf( stderr, "FATAL: polarssl_free() on unallocated "
406 "data\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200407#endif
Paul Bakker41350a92013-07-03 15:33:47 +0200408 exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200409 }
410
411 hdr->alloc = 0;
412
Paul Bakker891998e2013-07-03 14:45:05 +0200413#if defined(POLARSSL_MEMORY_DEBUG)
414 heap.free_count++;
415 heap.total_used -= hdr->size;
416#endif
417
Paul Bakker6e339b52013-07-03 13:37:05 +0200418 // Regroup with block before
419 //
420 if( hdr->prev != NULL && hdr->prev->alloc == 0 )
421 {
Paul Bakker891998e2013-07-03 14:45:05 +0200422#if defined(POLARSSL_MEMORY_DEBUG)
423 heap.header_count--;
424#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200425 hdr->prev->size += sizeof(memory_header) + hdr->size;
426 hdr->prev->next = hdr->next;
427 old = hdr;
428 hdr = hdr->prev;
429
430 if( hdr->next != NULL )
431 hdr->next->prev = hdr;
432
433#if defined(POLARSSL_MEMORY_BACKTRACE)
434 free( old->trace );
435#endif
436 memset( old, 0, sizeof(memory_header) );
437 }
438
439 // Regroup with block after
440 //
441 if( hdr->next != NULL && hdr->next->alloc == 0 )
442 {
Paul Bakker891998e2013-07-03 14:45:05 +0200443#if defined(POLARSSL_MEMORY_DEBUG)
444 heap.header_count--;
445#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200446 hdr->size += sizeof(memory_header) + hdr->next->size;
447 old = hdr->next;
448 hdr->next = hdr->next->next;
449
Paul Bakker1ef120f2013-07-03 17:20:39 +0200450 if( hdr->prev_free != NULL || hdr->next_free != NULL )
451 {
452 if( hdr->prev_free != NULL )
453 hdr->prev_free->next_free = hdr->next_free;
454 else
455 heap.first_free = hdr->next_free;
456
457 if( hdr->next_free != NULL )
458 hdr->next_free->prev_free = hdr->prev_free;
459 }
460
461 hdr->prev_free = old->prev_free;
462 hdr->next_free = old->next_free;
463
464 if( hdr->prev_free != NULL )
465 hdr->prev_free->next_free = hdr;
466 else
467 heap.first_free = hdr;
468
469 if( hdr->next_free != NULL )
470 hdr->next_free->prev_free = hdr;
471
Paul Bakker6e339b52013-07-03 13:37:05 +0200472 if( hdr->next != NULL )
473 hdr->next->prev = hdr;
474
475#if defined(POLARSSL_MEMORY_BACKTRACE)
476 free( old->trace );
477#endif
478 memset( old, 0, sizeof(memory_header) );
479 }
480
Paul Bakker1ef120f2013-07-03 17:20:39 +0200481 // Prepend to free_list if we have not merged
482 // (Does not have to stay in same order as prev / next list)
483 //
484 if( old == NULL )
485 {
486 hdr->next_free = heap.first_free;
487 heap.first_free->prev_free = hdr;
488 heap.first_free = hdr;
489 }
490
Paul Bakker6e339b52013-07-03 13:37:05 +0200491#if defined(POLARSSL_MEMORY_BACKTRACE)
492 hdr->trace = NULL;
493 hdr->trace_count = 0;
494#endif
495
496 if( ( heap.verify & MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
497 exit( 1 );
498}
499
Paul Bakkerbf796ac2013-09-28 11:06:38 +0200500void memory_buffer_set_verify( int verify )
501{
502 heap.verify = verify;
503}
504
Paul Bakker6e339b52013-07-03 13:37:05 +0200505int memory_buffer_alloc_verify()
506{
507 return verify_chain();
508}
509
510#if defined(POLARSSL_MEMORY_DEBUG)
511void memory_buffer_alloc_status()
512{
Paul Bakker7dc4c442014-02-01 22:50:26 +0100513 polarssl_fprintf( stderr,
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200514 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
515 "%zu bytes (total %zu bytes), malloc / free: %zu / %zu\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100516 heap.header_count, heap.total_used,
517 heap.maximum_header_count, heap.maximum_used,
518 heap.maximum_header_count * sizeof( memory_header )
519 + heap.maximum_used,
520 heap.malloc_count, heap.free_count );
Paul Bakker891998e2013-07-03 14:45:05 +0200521
Paul Bakker6e339b52013-07-03 13:37:05 +0200522 if( heap.first->next == NULL )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100523 polarssl_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200524 else
525 {
Paul Bakker7dc4c442014-02-01 22:50:26 +0100526 polarssl_fprintf( stderr, "Memory currently allocated:\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200527 debug_chain();
528 }
529}
Paul Bakker119602b2014-02-05 15:42:55 +0100530#endif /* POLARSSL_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200531
Paul Bakker1337aff2013-09-29 14:45:34 +0200532#if defined(POLARSSL_THREADING_C)
533static void *buffer_alloc_malloc_mutexed( size_t len )
534{
535 void *buf;
536 polarssl_mutex_lock( &heap.mutex );
537 buf = buffer_alloc_malloc( len );
538 polarssl_mutex_unlock( &heap.mutex );
539 return( buf );
540}
541
542static void buffer_alloc_free_mutexed( void *ptr )
543{
544 polarssl_mutex_lock( &heap.mutex );
545 buffer_alloc_free( ptr );
546 polarssl_mutex_unlock( &heap.mutex );
547}
Paul Bakker9af723c2014-05-01 13:03:14 +0200548#endif /* POLARSSL_THREADING_C */
Paul Bakker1337aff2013-09-29 14:45:34 +0200549
Paul Bakker6e339b52013-07-03 13:37:05 +0200550int memory_buffer_alloc_init( unsigned char *buf, size_t len )
551{
Paul Bakker6e339b52013-07-03 13:37:05 +0200552 memset( &heap, 0, sizeof(buffer_alloc_ctx) );
553 memset( buf, 0, len );
554
Paul Bakker1337aff2013-09-29 14:45:34 +0200555#if defined(POLARSSL_THREADING_C)
556 polarssl_mutex_init( &heap.mutex );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100557 platform_set_malloc_free( buffer_alloc_malloc_mutexed,
558 buffer_alloc_free_mutexed );
Paul Bakker1337aff2013-09-29 14:45:34 +0200559#else
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100560 platform_set_malloc_free( buffer_alloc_malloc, buffer_alloc_free );
Paul Bakker1337aff2013-09-29 14:45:34 +0200561#endif
562
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200563 if( (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE )
564 {
565 buf += POLARSSL_MEMORY_ALIGN_MULTIPLE
566 - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
567 len -= (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
568 }
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{
625 int ret = 0;
626 unsigned char buf[1024];
627 unsigned char *p, *q, *r;
628
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
648 memory_buffer_alloc_free( );
649
650 if( verbose != 0 )
651 polarssl_printf( "passed\n" );
652
653 if( verbose != 0 )
654 polarssl_printf( " MBA test #2 (buf not aligned): " );
655
656 memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
657
658 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 */