blob: 3713f8056c74ef4c345c9d4c355c8a4a65ea04b4 [file] [log] [blame]
Paul Bakker6e339b52013-07-03 13:37:05 +02001/*
2 * Buffer-based memory allocator
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker6e339b52013-07-03 13:37:05 +02005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker6e339b52013-07-03 13:37:05 +02007 *
Paul Bakker6e339b52013-07-03 13:37:05 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker6e339b52013-07-03 13:37:05 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020028
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010029#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010030#include "polarssl/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020031
Rich Evansd08a6052015-02-12 12:17:10 +000032/* No need for the header guard as POLARSSL_MEMORY_BUFFER_ALLOC_C
33 is dependent upon POLARSSL_PLATFORM_C */
34#include "polarssl/platform.h"
35
Paul Bakker6e339b52013-07-03 13:37:05 +020036#include <string.h>
37
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 Bakker34617722014-06-13 17:20:13 +020046/* Implementation that should never be optimized out by the compiler */
47static void polarssl_zeroize( void *v, size_t n ) {
48 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
49}
50
Paul Bakker6e339b52013-07-03 13:37:05 +020051#define MAGIC1 0xFF00AA55
52#define MAGIC2 0xEE119966
53#define MAX_BT 20
54
55typedef struct _memory_header memory_header;
56struct _memory_header
57{
58 size_t magic1;
59 size_t size;
60 size_t alloc;
61 memory_header *prev;
62 memory_header *next;
Paul Bakker1ef120f2013-07-03 17:20:39 +020063 memory_header *prev_free;
64 memory_header *next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020065#if defined(POLARSSL_MEMORY_BACKTRACE)
66 char **trace;
67 size_t trace_count;
68#endif
69 size_t magic2;
70};
71
72typedef struct
73{
74 unsigned char *buf;
75 size_t len;
76 memory_header *first;
Paul Bakker1ef120f2013-07-03 17:20:39 +020077 memory_header *first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020078 size_t current_alloc_size;
79 int verify;
Paul Bakker891998e2013-07-03 14:45:05 +020080#if defined(POLARSSL_MEMORY_DEBUG)
81 size_t malloc_count;
82 size_t free_count;
83 size_t total_used;
84 size_t maximum_used;
85 size_t header_count;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +010086 size_t maximum_header_count;
Paul Bakker891998e2013-07-03 14:45:05 +020087#endif
Paul Bakker1337aff2013-09-29 14:45:34 +020088#if defined(POLARSSL_THREADING_C)
89 threading_mutex_t mutex;
90#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020091}
92buffer_alloc_ctx;
93
94static buffer_alloc_ctx heap;
95
96#if defined(POLARSSL_MEMORY_DEBUG)
97static void debug_header( memory_header *hdr )
98{
99#if defined(POLARSSL_MEMORY_BACKTRACE)
100 size_t i;
101#endif
102
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200103 polarssl_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
104 "ALLOC(%zu), SIZE(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100105 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
106 hdr->alloc, hdr->size );
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200107 polarssl_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100108 (size_t) hdr->prev_free, (size_t) hdr->next_free );
Paul Bakker6e339b52013-07-03 13:37:05 +0200109
110#if defined(POLARSSL_MEMORY_BACKTRACE)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100111 polarssl_fprintf( stderr, "TRACE: \n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200112 for( i = 0; i < hdr->trace_count; i++ )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100113 polarssl_fprintf( stderr, "%s\n", hdr->trace[i] );
114 polarssl_fprintf( stderr, "\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200115#endif
116}
117
118static void debug_chain()
119{
120 memory_header *cur = heap.first;
121
Paul Bakker7dc4c442014-02-01 22:50:26 +0100122 polarssl_fprintf( stderr, "\nBlock list\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200123 while( cur != NULL )
124 {
125 debug_header( cur );
Paul Bakker6e339b52013-07-03 13:37:05 +0200126 cur = cur->next;
127 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200128
Paul Bakker7dc4c442014-02-01 22:50:26 +0100129 polarssl_fprintf( stderr, "Free list\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200130 cur = heap.first_free;
131
132 while( cur != NULL )
133 {
134 debug_header( cur );
135 cur = cur->next_free;
136 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200137}
138#endif /* POLARSSL_MEMORY_DEBUG */
139
140static int verify_header( memory_header *hdr )
141{
142 if( hdr->magic1 != MAGIC1 )
143 {
144#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100145 polarssl_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200146#endif
147 return( 1 );
148 }
149
150 if( hdr->magic2 != MAGIC2 )
151 {
152#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100153 polarssl_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200154#endif
155 return( 1 );
156 }
157
158 if( hdr->alloc > 1 )
159 {
160#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100161 polarssl_fprintf( stderr, "FATAL: alloc has illegal value\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200162#endif
163 return( 1 );
164 }
165
Paul Bakker1ef120f2013-07-03 17:20:39 +0200166 if( hdr->prev != NULL && hdr->prev == hdr->next )
167 {
168#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100169 polarssl_fprintf( stderr, "FATAL: prev == next\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200170#endif
171 return( 1 );
172 }
173
174 if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
175 {
176#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100177 polarssl_fprintf( stderr, "FATAL: prev_free == next_free\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200178#endif
179 return( 1 );
180 }
181
Paul Bakker6e339b52013-07-03 13:37:05 +0200182 return( 0 );
183}
184
185static int verify_chain()
186{
187 memory_header *prv = heap.first, *cur = heap.first->next;
188
189 if( verify_header( heap.first ) != 0 )
190 {
191#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100192 polarssl_fprintf( stderr, "FATAL: verification of first header "
193 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200194#endif
195 return( 1 );
196 }
197
198 if( heap.first->prev != NULL )
199 {
200#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100201 polarssl_fprintf( stderr, "FATAL: verification failed: "
202 "first->prev != NULL\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200203#endif
204 return( 1 );
205 }
206
207 while( cur != NULL )
208 {
209 if( verify_header( cur ) != 0 )
210 {
211#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100212 polarssl_fprintf( stderr, "FATAL: verification of header "
213 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200214#endif
215 return( 1 );
216 }
217
218 if( cur->prev != prv )
219 {
220#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100221 polarssl_fprintf( stderr, "FATAL: verification failed: "
222 "cur->prev != prv\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200223#endif
224 return( 1 );
225 }
226
227 prv = cur;
228 cur = cur->next;
229 }
230
231 return( 0 );
232}
233
234static void *buffer_alloc_malloc( size_t len )
235{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200236 memory_header *new, *cur = heap.first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200237 unsigned char *p;
238#if defined(POLARSSL_MEMORY_BACKTRACE)
239 void *trace_buffer[MAX_BT];
240 size_t trace_cnt;
241#endif
242
243 if( heap.buf == NULL || heap.first == NULL )
244 return( NULL );
245
246 if( len % POLARSSL_MEMORY_ALIGN_MULTIPLE )
247 {
248 len -= len % POLARSSL_MEMORY_ALIGN_MULTIPLE;
249 len += POLARSSL_MEMORY_ALIGN_MULTIPLE;
250 }
251
252 // Find block that fits
253 //
254 while( cur != NULL )
255 {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200256 if( cur->size >= len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200257 break;
258
Paul Bakker1ef120f2013-07-03 17:20:39 +0200259 cur = cur->next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200260 }
261
262 if( cur == NULL )
263 return( NULL );
264
Paul Bakker1ef120f2013-07-03 17:20:39 +0200265 if( cur->alloc != 0 )
266 {
267#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100268 polarssl_fprintf( stderr, "FATAL: block in free_list but allocated "
269 "data\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200270#endif
Rich Evans77d36382015-01-30 12:12:11 +0000271 polarssl_exit( 1 );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200272 }
273
Paul Bakker891998e2013-07-03 14:45:05 +0200274#if defined(POLARSSL_MEMORY_DEBUG)
275 heap.malloc_count++;
276#endif
277
Paul Bakker6e339b52013-07-03 13:37:05 +0200278 // Found location, split block if > memory_header + 4 room left
279 //
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200280 if( cur->size - len < sizeof(memory_header) +
281 POLARSSL_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200282 {
283 cur->alloc = 1;
284
Paul Bakker1ef120f2013-07-03 17:20:39 +0200285 // Remove from free_list
286 //
287 if( cur->prev_free != NULL )
288 cur->prev_free->next_free = cur->next_free;
289 else
290 heap.first_free = cur->next_free;
291
292 if( cur->next_free != NULL )
293 cur->next_free->prev_free = cur->prev_free;
294
295 cur->prev_free = NULL;
296 cur->next_free = NULL;
297
Paul Bakker891998e2013-07-03 14:45:05 +0200298#if defined(POLARSSL_MEMORY_DEBUG)
299 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200300 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200301 heap.maximum_used = heap.total_used;
302#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200303#if defined(POLARSSL_MEMORY_BACKTRACE)
304 trace_cnt = backtrace( trace_buffer, MAX_BT );
305 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
306 cur->trace_count = trace_cnt;
307#endif
308
309 if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000310 polarssl_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200311
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200312 return( ( (unsigned char *) cur ) + sizeof(memory_header) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200313 }
314
315 p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
316 new = (memory_header *) p;
317
318 new->size = cur->size - len - sizeof(memory_header);
319 new->alloc = 0;
320 new->prev = cur;
321 new->next = cur->next;
322#if defined(POLARSSL_MEMORY_BACKTRACE)
323 new->trace = NULL;
324 new->trace_count = 0;
325#endif
326 new->magic1 = MAGIC1;
327 new->magic2 = MAGIC2;
328
329 if( new->next != NULL )
330 new->next->prev = new;
331
Paul Bakker1ef120f2013-07-03 17:20:39 +0200332 // Replace cur with new in free_list
333 //
334 new->prev_free = cur->prev_free;
335 new->next_free = cur->next_free;
336 if( new->prev_free != NULL )
337 new->prev_free->next_free = new;
338 else
339 heap.first_free = new;
340
341 if( new->next_free != NULL )
342 new->next_free->prev_free = new;
343
Paul Bakker6e339b52013-07-03 13:37:05 +0200344 cur->alloc = 1;
345 cur->size = len;
346 cur->next = new;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200347 cur->prev_free = NULL;
348 cur->next_free = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200349
Paul Bakker891998e2013-07-03 14:45:05 +0200350#if defined(POLARSSL_MEMORY_DEBUG)
351 heap.header_count++;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +0100352 if( heap.header_count > heap.maximum_header_count )
353 heap.maximum_header_count = heap.header_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200354 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200355 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200356 heap.maximum_used = heap.total_used;
357#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200358#if defined(POLARSSL_MEMORY_BACKTRACE)
359 trace_cnt = backtrace( trace_buffer, MAX_BT );
360 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
361 cur->trace_count = trace_cnt;
362#endif
363
364 if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000365 polarssl_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200366
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200367 return( ( (unsigned char *) cur ) + sizeof(memory_header) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200368}
369
370static void buffer_alloc_free( void *ptr )
371{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200372 memory_header *hdr, *old = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200373 unsigned char *p = (unsigned char *) ptr;
374
Paul Bakker6e339b52013-07-03 13:37:05 +0200375 if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
376 return;
377
378 if( p < heap.buf || p > heap.buf + heap.len )
379 {
380#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100381 polarssl_fprintf( stderr, "FATAL: polarssl_free() outside of managed "
382 "space\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200383#endif
Rich Evans77d36382015-01-30 12:12:11 +0000384 polarssl_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200385 }
386
387 p -= sizeof(memory_header);
388 hdr = (memory_header *) p;
389
390 if( verify_header( hdr ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000391 polarssl_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200392
393 if( hdr->alloc != 1 )
394 {
395#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100396 polarssl_fprintf( stderr, "FATAL: polarssl_free() on unallocated "
397 "data\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200398#endif
Rich Evans77d36382015-01-30 12:12:11 +0000399 polarssl_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200400 }
401
402 hdr->alloc = 0;
403
Paul Bakker891998e2013-07-03 14:45:05 +0200404#if defined(POLARSSL_MEMORY_DEBUG)
405 heap.free_count++;
406 heap.total_used -= hdr->size;
407#endif
408
Paul Bakker6e339b52013-07-03 13:37:05 +0200409 // Regroup with block before
410 //
411 if( hdr->prev != NULL && hdr->prev->alloc == 0 )
412 {
Paul Bakker891998e2013-07-03 14:45:05 +0200413#if defined(POLARSSL_MEMORY_DEBUG)
414 heap.header_count--;
415#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200416 hdr->prev->size += sizeof(memory_header) + hdr->size;
417 hdr->prev->next = hdr->next;
418 old = hdr;
419 hdr = hdr->prev;
420
421 if( hdr->next != NULL )
422 hdr->next->prev = hdr;
423
424#if defined(POLARSSL_MEMORY_BACKTRACE)
425 free( old->trace );
426#endif
427 memset( old, 0, sizeof(memory_header) );
428 }
429
430 // Regroup with block after
431 //
432 if( hdr->next != NULL && hdr->next->alloc == 0 )
433 {
Paul Bakker891998e2013-07-03 14:45:05 +0200434#if defined(POLARSSL_MEMORY_DEBUG)
435 heap.header_count--;
436#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200437 hdr->size += sizeof(memory_header) + hdr->next->size;
438 old = hdr->next;
439 hdr->next = hdr->next->next;
440
Paul Bakker1ef120f2013-07-03 17:20:39 +0200441 if( hdr->prev_free != NULL || hdr->next_free != NULL )
442 {
443 if( hdr->prev_free != NULL )
444 hdr->prev_free->next_free = hdr->next_free;
445 else
446 heap.first_free = hdr->next_free;
447
448 if( hdr->next_free != NULL )
449 hdr->next_free->prev_free = hdr->prev_free;
450 }
451
452 hdr->prev_free = old->prev_free;
453 hdr->next_free = old->next_free;
454
455 if( hdr->prev_free != NULL )
456 hdr->prev_free->next_free = hdr;
457 else
458 heap.first_free = hdr;
459
460 if( hdr->next_free != NULL )
461 hdr->next_free->prev_free = hdr;
462
Paul Bakker6e339b52013-07-03 13:37:05 +0200463 if( hdr->next != NULL )
464 hdr->next->prev = hdr;
465
466#if defined(POLARSSL_MEMORY_BACKTRACE)
467 free( old->trace );
468#endif
469 memset( old, 0, sizeof(memory_header) );
470 }
471
Paul Bakker1ef120f2013-07-03 17:20:39 +0200472 // Prepend to free_list if we have not merged
473 // (Does not have to stay in same order as prev / next list)
474 //
475 if( old == NULL )
476 {
477 hdr->next_free = heap.first_free;
Manuel Pégourié-Gonnard547ff662014-11-26 15:42:16 +0100478 if( heap.first_free != NULL )
479 heap.first_free->prev_free = hdr;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200480 heap.first_free = hdr;
481 }
482
Paul Bakker6e339b52013-07-03 13:37:05 +0200483#if defined(POLARSSL_MEMORY_BACKTRACE)
484 hdr->trace = NULL;
485 hdr->trace_count = 0;
486#endif
487
488 if( ( heap.verify & MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000489 polarssl_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200490}
491
Paul Bakkerbf796ac2013-09-28 11:06:38 +0200492void memory_buffer_set_verify( int verify )
493{
494 heap.verify = verify;
495}
496
Paul Bakker6e339b52013-07-03 13:37:05 +0200497int memory_buffer_alloc_verify()
498{
499 return verify_chain();
500}
501
502#if defined(POLARSSL_MEMORY_DEBUG)
503void memory_buffer_alloc_status()
504{
Paul Bakker7dc4c442014-02-01 22:50:26 +0100505 polarssl_fprintf( stderr,
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200506 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
507 "%zu bytes (total %zu bytes), malloc / free: %zu / %zu\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100508 heap.header_count, heap.total_used,
509 heap.maximum_header_count, heap.maximum_used,
510 heap.maximum_header_count * sizeof( memory_header )
511 + heap.maximum_used,
512 heap.malloc_count, heap.free_count );
Paul Bakker891998e2013-07-03 14:45:05 +0200513
Paul Bakker6e339b52013-07-03 13:37:05 +0200514 if( heap.first->next == NULL )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100515 polarssl_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200516 else
517 {
Paul Bakker7dc4c442014-02-01 22:50:26 +0100518 polarssl_fprintf( stderr, "Memory currently allocated:\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200519 debug_chain();
520 }
521}
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100522
523void memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
524{
525 *max_used = heap.maximum_used;
526 *max_blocks = heap.maximum_header_count;
527}
528
529void memory_buffer_alloc_max_reset( void )
530{
531 heap.maximum_used = 0;
532 heap.maximum_header_count = 0;
533}
534
535void memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
536{
537 *cur_used = heap.total_used;
538 *cur_blocks = heap.header_count;
539}
Paul Bakker119602b2014-02-05 15:42:55 +0100540#endif /* POLARSSL_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200541
Paul Bakker1337aff2013-09-29 14:45:34 +0200542#if defined(POLARSSL_THREADING_C)
543static void *buffer_alloc_malloc_mutexed( size_t len )
544{
545 void *buf;
546 polarssl_mutex_lock( &heap.mutex );
547 buf = buffer_alloc_malloc( len );
548 polarssl_mutex_unlock( &heap.mutex );
549 return( buf );
550}
551
552static void buffer_alloc_free_mutexed( void *ptr )
553{
554 polarssl_mutex_lock( &heap.mutex );
555 buffer_alloc_free( ptr );
556 polarssl_mutex_unlock( &heap.mutex );
557}
Paul Bakker9af723c2014-05-01 13:03:14 +0200558#endif /* POLARSSL_THREADING_C */
Paul Bakker1337aff2013-09-29 14:45:34 +0200559
Paul Bakker6e339b52013-07-03 13:37:05 +0200560int memory_buffer_alloc_init( unsigned char *buf, size_t len )
561{
Paul Bakker6e339b52013-07-03 13:37:05 +0200562 memset( &heap, 0, sizeof(buffer_alloc_ctx) );
563 memset( buf, 0, len );
564
Paul Bakker1337aff2013-09-29 14:45:34 +0200565#if defined(POLARSSL_THREADING_C)
566 polarssl_mutex_init( &heap.mutex );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100567 platform_set_malloc_free( buffer_alloc_malloc_mutexed,
568 buffer_alloc_free_mutexed );
Paul Bakker1337aff2013-09-29 14:45:34 +0200569#else
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100570 platform_set_malloc_free( buffer_alloc_malloc, buffer_alloc_free );
Paul Bakker1337aff2013-09-29 14:45:34 +0200571#endif
572
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200573 if( (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE )
574 {
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100575 /* Adjust len first since buf is used in the computation */
576 len -= POLARSSL_MEMORY_ALIGN_MULTIPLE
577 - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200578 buf += POLARSSL_MEMORY_ALIGN_MULTIPLE
579 - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200580 }
581
Paul Bakker6e339b52013-07-03 13:37:05 +0200582 heap.buf = buf;
583 heap.len = len;
584
585 heap.first = (memory_header *) buf;
586 heap.first->size = len - sizeof(memory_header);
587 heap.first->magic1 = MAGIC1;
588 heap.first->magic2 = MAGIC2;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200589 heap.first_free = heap.first;
Paul Bakker6e339b52013-07-03 13:37:05 +0200590 return( 0 );
591}
592
Paul Bakker1337aff2013-09-29 14:45:34 +0200593void memory_buffer_alloc_free()
594{
595#if defined(POLARSSL_THREADING_C)
596 polarssl_mutex_free( &heap.mutex );
597#endif
Paul Bakker34617722014-06-13 17:20:13 +0200598 polarssl_zeroize( &heap, sizeof(buffer_alloc_ctx) );
Paul Bakker1337aff2013-09-29 14:45:34 +0200599}
600
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100601#if defined(POLARSSL_SELF_TEST)
602static int check_pointer( void *p )
603{
604 if( p == NULL )
605 return( -1 );
606
607 if( (size_t) p % POLARSSL_MEMORY_ALIGN_MULTIPLE != 0 )
608 return( -1 );
609
610 return( 0 );
611}
612
613static int check_all_free( )
614{
615 if( heap.current_alloc_size != 0 ||
616 heap.first != heap.first_free ||
617 (void *) heap.first != (void *) heap.buf )
618 {
619 return( -1 );
620 }
621
622 return( 0 );
623}
624
625#define TEST_ASSERT( condition ) \
626 if( ! (condition) ) \
627 { \
628 if( verbose != 0 ) \
629 polarssl_printf( "failed\n" ); \
630 \
631 ret = 1; \
632 goto cleanup; \
633 }
634
635int memory_buffer_alloc_self_test( int verbose )
636{
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100637 unsigned char buf[1024];
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100638 unsigned char *p, *q, *r, *end;
639 int ret = 0;
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100640
641 if( verbose != 0 )
642 polarssl_printf( " MBA test #1 (basic alloc-free cycle): " );
643
644 memory_buffer_alloc_init( buf, sizeof( buf ) );
645
646 p = polarssl_malloc( 1 );
647 q = polarssl_malloc( 128 );
648 r = polarssl_malloc( 16 );
649
650 TEST_ASSERT( check_pointer( p ) == 0 &&
651 check_pointer( q ) == 0 &&
652 check_pointer( r ) == 0 );
653
654 polarssl_free( r );
655 polarssl_free( q );
656 polarssl_free( p );
657
658 TEST_ASSERT( check_all_free( ) == 0 );
659
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100660 /* Memorize end to compare with the next test */
661 end = heap.buf + heap.len;
662
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100663 memory_buffer_alloc_free( );
664
665 if( verbose != 0 )
666 polarssl_printf( "passed\n" );
667
668 if( verbose != 0 )
669 polarssl_printf( " MBA test #2 (buf not aligned): " );
670
671 memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
672
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100673 TEST_ASSERT( heap.buf + heap.len == end );
674
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100675 p = polarssl_malloc( 1 );
676 q = polarssl_malloc( 128 );
677 r = polarssl_malloc( 16 );
678
679 TEST_ASSERT( check_pointer( p ) == 0 &&
680 check_pointer( q ) == 0 &&
681 check_pointer( r ) == 0 );
682
683 polarssl_free( r );
684 polarssl_free( q );
685 polarssl_free( p );
686
687 TEST_ASSERT( check_all_free( ) == 0 );
688
689 memory_buffer_alloc_free( );
690
691 if( verbose != 0 )
692 polarssl_printf( "passed\n" );
693
694 if( verbose != 0 )
695 polarssl_printf( " MBA test #3 (full): " );
696
697 memory_buffer_alloc_init( buf, sizeof( buf ) );
698
699 p = polarssl_malloc( sizeof( buf ) - sizeof( memory_header ) );
700
701 TEST_ASSERT( check_pointer( p ) == 0 );
702 TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
703
704 polarssl_free( p );
705
706 p = polarssl_malloc( sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
707 q = polarssl_malloc( 16 );
708
709 TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
710 TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
711
712 polarssl_free( q );
713
714 TEST_ASSERT( polarssl_malloc( 17 ) == NULL );
715
716 polarssl_free( p );
717
718 TEST_ASSERT( check_all_free( ) == 0 );
719
720 memory_buffer_alloc_free( );
721
722 if( verbose != 0 )
723 polarssl_printf( "passed\n" );
724
725cleanup:
726 memory_buffer_alloc_free( );
727
728 return( ret );
729}
730#endif /* POLARSSL_SELF_TEST */
731
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100732#endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */