2 * Buffer-based memory allocator
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
29 #include "mbedtls/memory_buffer_alloc.h"
31 /* No need for the header guard as MBEDTLS_MEMORY_BUFFER_ALLOC_C
32 is dependent upon MBEDTLS_PLATFORM_C */
33 #include "mbedtls/platform.h"
34 #include "mbedtls/platform_util.h"
38 #if defined(MBEDTLS_MEMORY_BACKTRACE)
42 #if defined(MBEDTLS_THREADING_C)
43 #include "mbedtls/threading.h"
46 #define MAGIC1 0xFF00AA55
47 #define MAGIC2 0xEE119966
50 typedef struct _memory_header memory_header;
58 memory_header *prev_free;
59 memory_header *next_free;
60 #if defined(MBEDTLS_MEMORY_BACKTRACE)
72 memory_header *first_free;
74 #if defined(MBEDTLS_MEMORY_DEBUG)
80 size_t maximum_header_count;
82 #if defined(MBEDTLS_THREADING_C)
83 mbedtls_threading_mutex_t mutex;
88 static buffer_alloc_ctx heap;
90 #if defined(MBEDTLS_MEMORY_DEBUG)
91 static void debug_header( memory_header *hdr )
93 #if defined(MBEDTLS_MEMORY_BACKTRACE)
97 mbedtls_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
98 "ALLOC(%zu), SIZE(%10zu)\n",
99 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
100 hdr->alloc, hdr->size );
101 mbedtls_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
102 (size_t) hdr->prev_free, (size_t) hdr->next_free );
104 #if defined(MBEDTLS_MEMORY_BACKTRACE)
105 mbedtls_fprintf( stderr, "TRACE: \n" );
106 for( i = 0; i < hdr->trace_count; i++ )
107 mbedtls_fprintf( stderr, "%s\n", hdr->trace[i] );
108 mbedtls_fprintf( stderr, "\n" );
112 static void debug_chain( void )
114 memory_header *cur = heap.first;
116 mbedtls_fprintf( stderr, "\nBlock list\n" );
123 mbedtls_fprintf( stderr, "Free list\n" );
124 cur = heap.first_free;
129 cur = cur->next_free;
132 #endif /* MBEDTLS_MEMORY_DEBUG */
134 static int verify_header( memory_header *hdr )
136 if( hdr->magic1 != MAGIC1 )
138 #if defined(MBEDTLS_MEMORY_DEBUG)
139 mbedtls_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
144 if( hdr->magic2 != MAGIC2 )
146 #if defined(MBEDTLS_MEMORY_DEBUG)
147 mbedtls_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
154 #if defined(MBEDTLS_MEMORY_DEBUG)
155 mbedtls_fprintf( stderr, "FATAL: alloc has illegal value\n" );
160 if( hdr->prev != NULL && hdr->prev == hdr->next )
162 #if defined(MBEDTLS_MEMORY_DEBUG)
163 mbedtls_fprintf( stderr, "FATAL: prev == next\n" );
168 if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
170 #if defined(MBEDTLS_MEMORY_DEBUG)
171 mbedtls_fprintf( stderr, "FATAL: prev_free == next_free\n" );
179 static int verify_chain( void )
181 memory_header *prv = heap.first, *cur;
183 if( prv == NULL || verify_header( prv ) != 0 )
185 #if defined(MBEDTLS_MEMORY_DEBUG)
186 mbedtls_fprintf( stderr, "FATAL: verification of first header "
192 if( heap.first->prev != NULL )
194 #if defined(MBEDTLS_MEMORY_DEBUG)
195 mbedtls_fprintf( stderr, "FATAL: verification failed: "
196 "first->prev != NULL\n" );
201 cur = heap.first->next;
205 if( verify_header( cur ) != 0 )
207 #if defined(MBEDTLS_MEMORY_DEBUG)
208 mbedtls_fprintf( stderr, "FATAL: verification of header "
214 if( cur->prev != prv )
216 #if defined(MBEDTLS_MEMORY_DEBUG)
217 mbedtls_fprintf( stderr, "FATAL: verification failed: "
218 "cur->prev != prv\n" );
230 static void *buffer_alloc_calloc( size_t n, size_t size )
232 memory_header *new, *cur = heap.first_free;
235 size_t original_len, len;
236 #if defined(MBEDTLS_MEMORY_BACKTRACE)
237 void *trace_buffer[MAX_BT];
241 if( heap.buf == NULL || heap.first == NULL )
244 original_len = len = n * size;
246 if( n == 0 || size == 0 || len / n != size )
248 else if( len > (size_t)-MBEDTLS_MEMORY_ALIGN_MULTIPLE )
251 if( len % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
253 len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
254 len += MBEDTLS_MEMORY_ALIGN_MULTIPLE;
257 // Find block that fits
261 if( cur->size >= len )
264 cur = cur->next_free;
270 if( cur->alloc != 0 )
272 #if defined(MBEDTLS_MEMORY_DEBUG)
273 mbedtls_fprintf( stderr, "FATAL: block in free_list but allocated "
279 #if defined(MBEDTLS_MEMORY_DEBUG)
283 // Found location, split block if > memory_header + 4 room left
285 if( cur->size - len < sizeof(memory_header) +
286 MBEDTLS_MEMORY_ALIGN_MULTIPLE )
290 // Remove from free_list
292 if( cur->prev_free != NULL )
293 cur->prev_free->next_free = cur->next_free;
295 heap.first_free = cur->next_free;
297 if( cur->next_free != NULL )
298 cur->next_free->prev_free = cur->prev_free;
300 cur->prev_free = NULL;
301 cur->next_free = NULL;
303 #if defined(MBEDTLS_MEMORY_DEBUG)
304 heap.total_used += cur->size;
305 if( heap.total_used > heap.maximum_used )
306 heap.maximum_used = heap.total_used;
308 #if defined(MBEDTLS_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;
314 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
317 ret = (unsigned char *) cur + sizeof( memory_header );
318 memset( ret, 0, original_len );
323 p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
324 new = (memory_header *) p;
326 new->size = cur->size - len - sizeof(memory_header);
329 new->next = cur->next;
330 #if defined(MBEDTLS_MEMORY_BACKTRACE)
332 new->trace_count = 0;
334 new->magic1 = MAGIC1;
335 new->magic2 = MAGIC2;
337 if( new->next != NULL )
338 new->next->prev = new;
340 // Replace cur with new in free_list
342 new->prev_free = cur->prev_free;
343 new->next_free = cur->next_free;
344 if( new->prev_free != NULL )
345 new->prev_free->next_free = new;
347 heap.first_free = new;
349 if( new->next_free != NULL )
350 new->next_free->prev_free = new;
355 cur->prev_free = NULL;
356 cur->next_free = NULL;
358 #if defined(MBEDTLS_MEMORY_DEBUG)
360 if( heap.header_count > heap.maximum_header_count )
361 heap.maximum_header_count = heap.header_count;
362 heap.total_used += cur->size;
363 if( heap.total_used > heap.maximum_used )
364 heap.maximum_used = heap.total_used;
366 #if defined(MBEDTLS_MEMORY_BACKTRACE)
367 trace_cnt = backtrace( trace_buffer, MAX_BT );
368 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
369 cur->trace_count = trace_cnt;
372 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
375 ret = (unsigned char *) cur + sizeof( memory_header );
376 memset( ret, 0, original_len );
381 static void buffer_alloc_free( void *ptr )
383 memory_header *hdr, *old = NULL;
384 unsigned char *p = (unsigned char *) ptr;
386 if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
389 if( p < heap.buf || p >= heap.buf + heap.len )
391 #if defined(MBEDTLS_MEMORY_DEBUG)
392 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() outside of managed "
398 p -= sizeof(memory_header);
399 hdr = (memory_header *) p;
401 if( verify_header( hdr ) != 0 )
404 if( hdr->alloc != 1 )
406 #if defined(MBEDTLS_MEMORY_DEBUG)
407 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() on unallocated "
415 #if defined(MBEDTLS_MEMORY_DEBUG)
417 heap.total_used -= hdr->size;
420 #if defined(MBEDTLS_MEMORY_BACKTRACE)
423 hdr->trace_count = 0;
426 // Regroup with block before
428 if( hdr->prev != NULL && hdr->prev->alloc == 0 )
430 #if defined(MBEDTLS_MEMORY_DEBUG)
433 hdr->prev->size += sizeof(memory_header) + hdr->size;
434 hdr->prev->next = hdr->next;
438 if( hdr->next != NULL )
439 hdr->next->prev = hdr;
441 memset( old, 0, sizeof(memory_header) );
444 // Regroup with block after
446 if( hdr->next != NULL && hdr->next->alloc == 0 )
448 #if defined(MBEDTLS_MEMORY_DEBUG)
451 hdr->size += sizeof(memory_header) + hdr->next->size;
453 hdr->next = hdr->next->next;
455 if( hdr->prev_free != NULL || hdr->next_free != NULL )
457 if( hdr->prev_free != NULL )
458 hdr->prev_free->next_free = hdr->next_free;
460 heap.first_free = hdr->next_free;
462 if( hdr->next_free != NULL )
463 hdr->next_free->prev_free = hdr->prev_free;
466 hdr->prev_free = old->prev_free;
467 hdr->next_free = old->next_free;
469 if( hdr->prev_free != NULL )
470 hdr->prev_free->next_free = hdr;
472 heap.first_free = hdr;
474 if( hdr->next_free != NULL )
475 hdr->next_free->prev_free = hdr;
477 if( hdr->next != NULL )
478 hdr->next->prev = hdr;
480 memset( old, 0, sizeof(memory_header) );
483 // Prepend to free_list if we have not merged
484 // (Does not have to stay in same order as prev / next list)
488 hdr->next_free = heap.first_free;
489 if( heap.first_free != NULL )
490 heap.first_free->prev_free = hdr;
491 heap.first_free = hdr;
494 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
498 void mbedtls_memory_buffer_set_verify( int verify )
500 heap.verify = verify;
503 int mbedtls_memory_buffer_alloc_verify( void )
505 return verify_chain();
508 #if defined(MBEDTLS_MEMORY_DEBUG)
509 void mbedtls_memory_buffer_alloc_status( void )
511 mbedtls_fprintf( stderr,
512 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
513 "%zu bytes (total %zu bytes), alloc / free: %zu / %zu\n",
514 heap.header_count, heap.total_used,
515 heap.maximum_header_count, heap.maximum_used,
516 heap.maximum_header_count * sizeof( memory_header )
518 heap.alloc_count, heap.free_count );
520 if( heap.first->next == NULL )
522 mbedtls_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
526 mbedtls_fprintf( stderr, "Memory currently allocated:\n" );
531 void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
533 *max_used = heap.maximum_used;
534 *max_blocks = heap.maximum_header_count;
537 void mbedtls_memory_buffer_alloc_max_reset( void )
539 heap.maximum_used = 0;
540 heap.maximum_header_count = 0;
543 void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
545 *cur_used = heap.total_used;
546 *cur_blocks = heap.header_count;
548 #endif /* MBEDTLS_MEMORY_DEBUG */
550 #if defined(MBEDTLS_THREADING_C)
551 static void *buffer_alloc_calloc_mutexed( size_t n, size_t size )
554 if( mbedtls_mutex_lock( &heap.mutex ) != 0 )
556 buf = buffer_alloc_calloc( n, size );
557 if( mbedtls_mutex_unlock( &heap.mutex ) )
562 static void buffer_alloc_free_mutexed( void *ptr )
564 /* We have to good option here, but corrupting the heap seems
565 * worse than loosing memory. */
566 if( mbedtls_mutex_lock( &heap.mutex ) )
568 buffer_alloc_free( ptr );
569 (void) mbedtls_mutex_unlock( &heap.mutex );
571 #endif /* MBEDTLS_THREADING_C */
573 void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len )
575 memset( &heap, 0, sizeof( buffer_alloc_ctx ) );
577 #if defined(MBEDTLS_THREADING_C)
578 mbedtls_mutex_init( &heap.mutex );
579 mbedtls_platform_set_calloc_free( buffer_alloc_calloc_mutexed,
580 buffer_alloc_free_mutexed );
582 mbedtls_platform_set_calloc_free( buffer_alloc_calloc, buffer_alloc_free );
585 if( len < sizeof( memory_header ) + MBEDTLS_MEMORY_ALIGN_MULTIPLE )
587 else if( (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
589 /* Adjust len first since buf is used in the computation */
590 len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
591 - (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
592 buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
593 - (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
596 memset( buf, 0, len );
601 heap.first = (memory_header *)buf;
602 heap.first->size = len - sizeof( memory_header );
603 heap.first->magic1 = MAGIC1;
604 heap.first->magic2 = MAGIC2;
605 heap.first_free = heap.first;
608 void mbedtls_memory_buffer_alloc_free( void )
610 #if defined(MBEDTLS_THREADING_C)
611 mbedtls_mutex_free( &heap.mutex );
613 mbedtls_platform_zeroize( &heap, sizeof(buffer_alloc_ctx) );
616 #if defined(MBEDTLS_SELF_TEST)
617 static int check_pointer( void *p )
622 if( (size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0 )
628 static int check_all_free( void )
631 #if defined(MBEDTLS_MEMORY_DEBUG)
632 heap.total_used != 0 ||
634 heap.first != heap.first_free ||
635 (void *) heap.first != (void *) heap.buf )
643 #define TEST_ASSERT( condition ) \
644 if( ! (condition) ) \
647 mbedtls_printf( "failed\n" ); \
653 int mbedtls_memory_buffer_alloc_self_test( int verbose )
655 unsigned char buf[1024];
656 unsigned char *p, *q, *r, *end;
660 mbedtls_printf( " MBA test #1 (basic alloc-free cycle): " );
662 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
664 p = mbedtls_calloc( 1, 1 );
665 q = mbedtls_calloc( 1, 128 );
666 r = mbedtls_calloc( 1, 16 );
668 TEST_ASSERT( check_pointer( p ) == 0 &&
669 check_pointer( q ) == 0 &&
670 check_pointer( r ) == 0 );
676 TEST_ASSERT( check_all_free( ) == 0 );
678 /* Memorize end to compare with the next test */
679 end = heap.buf + heap.len;
681 mbedtls_memory_buffer_alloc_free( );
684 mbedtls_printf( "passed\n" );
687 mbedtls_printf( " MBA test #2 (buf not aligned): " );
689 mbedtls_memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
691 TEST_ASSERT( heap.buf + heap.len == end );
693 p = mbedtls_calloc( 1, 1 );
694 q = mbedtls_calloc( 1, 128 );
695 r = mbedtls_calloc( 1, 16 );
697 TEST_ASSERT( check_pointer( p ) == 0 &&
698 check_pointer( q ) == 0 &&
699 check_pointer( r ) == 0 );
705 TEST_ASSERT( check_all_free( ) == 0 );
707 mbedtls_memory_buffer_alloc_free( );
710 mbedtls_printf( "passed\n" );
713 mbedtls_printf( " MBA test #3 (full): " );
715 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
717 p = mbedtls_calloc( 1, sizeof( buf ) - sizeof( memory_header ) );
719 TEST_ASSERT( check_pointer( p ) == 0 );
720 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
724 p = mbedtls_calloc( 1, sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
725 q = mbedtls_calloc( 1, 16 );
727 TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
728 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
732 TEST_ASSERT( mbedtls_calloc( 1, 17 ) == NULL );
736 TEST_ASSERT( check_all_free( ) == 0 );
738 mbedtls_memory_buffer_alloc_free( );
741 mbedtls_printf( "passed\n" );
744 mbedtls_memory_buffer_alloc_free( );
748 #endif /* MBEDTLS_SELF_TEST */
750 #endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */