From 7decfe8c1ed6a5da39fffb547a1b27ef0bb04ee1 Mon Sep 17 00:00:00 2001 From: Roberto Vargas Date: Mon, 4 Jun 2018 13:54:09 +0100 Subject: [PATCH] Convert mbedtls_free and mbedtls_calloc into functions When MBEDTLS_PLATFORM_MEMORY is defined but MBEDTLS_PLATFORM_FREE_MACRO or MBEDTLS_PLATFORM_CALLOC_MACRO are not defined then the actual functions used to allocate and free memory are stored in function pointers. These pointers are exposed to the caller, and it means that the caller and the library have to share a data section. In TF-A, we execute in a very constrained environment, where some images are executed from ROM and other images are executed from SRAM. The images that are executed from ROM cannot be modified. The SRAM size is very small and we are moving libraries to the ROM that can be shared between the different SRAM images. These SRAM images could import all the symbols used in mbedtls, but it would create an undesirable hard binary dependency between the different images. For this reason, all the library functions in ROM are accesed using a jump table whose base address is known, allowing the images to execute with different versions of the ROM. This commit changes the function pointers to actual functions, so that the SRAM images only have to use the new exported symbols (mbedtls_calloc and mbedtls_free) using the jump table. In our scenario, mbedtls_platform_set_calloc_free is called from mbedtls_memory_buffer_alloc_init which initializes the function pointers to the internal buffer_alloc_calloc and buffer_alloc_free functions. No functional changes to mbedtls_memory_buffer_alloc_init. Signed-off-by: Roberto Vargas --- include/mbedtls/platform.h | 4 ++-- library/platform.c | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/include/mbedtls/platform.h b/include/mbedtls/platform.h index bba770911..9d9c5293e 100644 --- a/include/mbedtls/platform.h +++ b/include/mbedtls/platform.h @@ -121,8 +121,8 @@ extern "C" { #else /* For size_t */ #include -extern void * (*mbedtls_calloc)( size_t n, size_t size ); -extern void (*mbedtls_free)( void *ptr ); +extern void *mbedtls_calloc( size_t n, size_t size ); +extern void mbedtls_free( void *ptr ); /** * \brief This function dynamically sets the memory-management diff --git a/library/platform.c b/library/platform.c index 9e992875d..b24b2fa65 100644 --- a/library/platform.c +++ b/library/platform.c @@ -51,14 +51,24 @@ static void platform_free_uninit( void *ptr ) #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit #endif /* !MBEDTLS_PLATFORM_STD_FREE */ -void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC; -void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE; +static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC; +static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE; + +void * mbedtls_calloc( size_t nmemb, size_t size ) +{ + return (*mbedtls_calloc_func)( nmemb, size ); +} + +void mbedtls_free( void * ptr ) +{ + (*mbedtls_free_func)( ptr ); +} int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ), void (*free_func)( void * ) ) { - mbedtls_calloc = calloc_func; - mbedtls_free = free_func; + mbedtls_calloc_func = calloc_func; + mbedtls_free_func = free_func; return( 0 ); } #endif /* MBEDTLS_PLATFORM_MEMORY */