Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test platform abstraction for memory #6429

Draft
wants to merge 6 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog.d/platform-macros.without-module.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Features
* You can now use a custom heap allocator by setting
MBEDTLS_PLATFORM_CALLOC_MACRO and MBEDTLS_PLATFORM_FREE_MACRO
without enabling MBEDTLS_PLATFORM_C.
10 changes: 0 additions & 10 deletions include/mbedtls/check_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,11 +463,6 @@
#error "MBEDTLS_PLATFORM_FPRINTF_MACRO and MBEDTLS_PLATFORM_STD_FPRINTF/MBEDTLS_PLATFORM_FPRINTF_ALT cannot be defined simultaneously"
#endif

#if defined(MBEDTLS_PLATFORM_FREE_MACRO) &&\
( !defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_PLATFORM_MEMORY) )
#error "MBEDTLS_PLATFORM_FREE_MACRO defined, but not all prerequisites"
#endif

#if defined(MBEDTLS_PLATFORM_FREE_MACRO) &&\
defined(MBEDTLS_PLATFORM_STD_FREE)
#error "MBEDTLS_PLATFORM_FREE_MACRO and MBEDTLS_PLATFORM_STD_FREE cannot be defined simultaneously"
Expand All @@ -477,11 +472,6 @@
#error "MBEDTLS_PLATFORM_CALLOC_MACRO must be defined if MBEDTLS_PLATFORM_FREE_MACRO is"
#endif

#if defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&\
( !defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_PLATFORM_MEMORY) )
#error "MBEDTLS_PLATFORM_CALLOC_MACRO defined, but not all prerequisites"
#endif

#if defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&\
defined(MBEDTLS_PLATFORM_STD_CALLOC)
#error "MBEDTLS_PLATFORM_CALLOC_MACRO and MBEDTLS_PLATFORM_STD_CALLOC cannot be defined simultaneously"
Expand Down
10 changes: 5 additions & 5 deletions include/mbedtls/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ extern "C" {
/*
* The function pointers for calloc and free.
*/
#if defined(MBEDTLS_PLATFORM_MEMORY)
#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
#else

#elif defined(MBEDTLS_PLATFORM_MEMORY)

/* For size_t */
#include <stddef.h>
extern void *mbedtls_calloc( size_t n, size_t size );
Expand All @@ -158,11 +159,10 @@ extern void mbedtls_free( void *ptr );
*/
int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
void (*free_func)( void * ) );
#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
#else /* !MBEDTLS_PLATFORM_MEMORY */
#else /* !MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
#define mbedtls_free free
#define mbedtls_calloc calloc
#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
#endif /* !MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */

/*
* The function pointers for fprintf
Expand Down
23 changes: 23 additions & 0 deletions tests/configs/user-config-for-test.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,26 @@
#define MBEDTLS_PSA_ACCEL_ALG_HMAC

#endif /* PSA_CRYPTO_DRIVER_TEST_ALL */



#if defined(MBEDTLS_TEST_PLATFORM_MACROS)
/* A build for testing where the platform macros are defined to be functions
* in the test framework that log that the function was called, then call the
* standard function. See tests/configs/user-config-for-test.h.
*
* Currently implemented only for calloc/free, to be extended eventually
* to all such macros.
*/

#include <stddef.h>

#if defined(MBEDTLS_PLATFORM_C)
#define MBEDTLS_PLATFORM_MEMORY
#endif
void *mbedtls_test_platform_calloc_macro( size_t, size_t );
#define MBEDTLS_PLATFORM_CALLOC_MACRO mbedtls_test_platform_calloc_macro
void mbedtls_test_platform_free_macro( void* );
#define MBEDTLS_PLATFORM_FREE_MACRO mbedtls_test_platform_free_macro

#endif
55 changes: 55 additions & 0 deletions tests/include/test/platform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/** Utilities for testing platform functions.
*/

/*
* Copyright The Mbed TLS Contributors
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef MBEDTLS_TEST_PLATFORM_H
#define MBEDTLS_TEST_PLATFORM_H

/** Counters keeping track of how many times each platform function was
* called. It's up to the implementation of the platform function to
* update an instance of this structure. */
typedef struct
{
size_t calloc; /*!< mbedtls_calloc */
size_t free; /*!< mbedtls_free */
} mbedtls_test_platform_function_counters_t;

#if defined(MBEDTLS_TEST_PLATFORM_MACROS)
/* This macro should be set via tests/configs/user-config-for-test.h, which
* sets each mbedtls_test_platform_xxx_macro functions defined here as
* implementations of the corresponding platform abstraction. */

/** Counters keeping track of how many times the platform functions were
* called. They are reset at the beginning of each test case. */
extern mbedtls_test_platform_function_counters_t mbedtls_test_platform_macro_counters;

/** Reset ::mbedtls_test_platform_macro_counters to zero. */
void mbedtls_test_reset_platform_macro_counters( void );

/** An implementation of mbedtls_calloc() that updates
* ::mbedtls_test_platform_macro_counters. */
void *mbedtls_test_platform_calloc_macro( size_t nbmem, size_t size );

/** An implementation of mbedtls_free() that updates
* ::mbedtls_test_platform_macro_counters. */
void mbedtls_test_platform_free_macro( void* ptr );

#endif /* MBEDTLS_TEST_PLATFORM_MACROS */

#endif /* MBEDTLS_TEST_PLATFORM_H */
44 changes: 29 additions & 15 deletions tests/scripts/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2522,6 +2522,35 @@ component_test_no_platform () {
make CC=gcc CFLAGS='-Werror -Wall -Wextra -Os' test
}

component_test_platform_macros_without_module () {
msg "build: with platform macros without platform.c"
scripts/config.py full
scripts/config.py unset MBEDTLS_PLATFORM_C
scripts/config.py unset MBEDTLS_PLATFORM_MEMORY
scripts/config.py unset MBEDTLS_PLATFORM_PRINTF_ALT
scripts/config.py unset MBEDTLS_PLATFORM_FPRINTF_ALT
scripts/config.py unset MBEDTLS_PLATFORM_SNPRINTF_ALT
scripts/config.py unset MBEDTLS_PLATFORM_VSNPRINTF_ALT
scripts/config.py unset MBEDTLS_PLATFORM_TIME_ALT
scripts/config.py unset MBEDTLS_PLATFORM_EXIT_ALT
scripts/config.py unset MBEDTLS_PLATFORM_SETBUF_ALT
scripts/config.py unset MBEDTLS_PLATFORM_NV_SEED_ALT
scripts/config.py unset MBEDTLS_ENTROPY_NV_SEED
make CFLAGS="$ASAN_CFLAGS -DMBEDTLS_TEST_PLATFORM_MACROS -DMBEDTLS_USER_CONFIG_FILE='\"../tests/configs/user-config-for-test.h\"' -O2" LDFLAGS="$ASAN_CFLAGS"

msg "test: with platform macros and platform.c"
make test
}

component_test_platform_macros_with_module () {
msg "build: with platform macros and platform.c"
scripts/config.py full
make CFLAGS="$ASAN_CFLAGS -DMBEDTLS_TEST_PLATFORM_MACROS -DMBEDTLS_USER_CONFIG_FILE='\"../tests/configs/user-config-for-test.h\"' -O2" LDFLAGS="$ASAN_CFLAGS"

msg "test: with platform macros and platform.c"
make test
}

component_build_no_std_function () {
# catch compile bugs in _uninit functions
msg "build: full config with NO_STD_FUNCTION, make, gcc" # ~ 30s
Expand Down Expand Up @@ -2705,18 +2734,6 @@ component_test_no_date_time () {
make test
}

component_test_platform_calloc_macro () {
msg "build: MBEDTLS_PLATFORM_{CALLOC/FREE}_MACRO enabled (ASan build)"
scripts/config.py set MBEDTLS_PLATFORM_MEMORY
scripts/config.py set MBEDTLS_PLATFORM_CALLOC_MACRO calloc
scripts/config.py set MBEDTLS_PLATFORM_FREE_MACRO free
CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
make

msg "test: MBEDTLS_PLATFORM_{CALLOC/FREE}_MACRO enabled (ASan build)"
make test
}

component_test_malloc_0_null () {
msg "build: malloc(0) returns NULL (ASan+UBSan build)"
scripts/config.py full
Expand Down Expand Up @@ -2770,7 +2787,6 @@ component_test_aes_fewer_tables_and_rom_tables () {
component_test_ctr_drbg_aes_256_sha_256 () {
msg "build: full + MBEDTLS_ENTROPY_FORCE_SHA256 (ASan build)"
scripts/config.py full
scripts/config.py unset MBEDTLS_MEMORY_BUFFER_ALLOC_C
scripts/config.py set MBEDTLS_ENTROPY_FORCE_SHA256
CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
make
Expand All @@ -2782,7 +2798,6 @@ component_test_ctr_drbg_aes_256_sha_256 () {
component_test_ctr_drbg_aes_128_sha_512 () {
msg "build: full + MBEDTLS_CTR_DRBG_USE_128_BIT_KEY (ASan build)"
scripts/config.py full
scripts/config.py unset MBEDTLS_MEMORY_BUFFER_ALLOC_C
scripts/config.py set MBEDTLS_CTR_DRBG_USE_128_BIT_KEY
CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
make
Expand All @@ -2794,7 +2809,6 @@ component_test_ctr_drbg_aes_128_sha_512 () {
component_test_ctr_drbg_aes_128_sha_256 () {
msg "build: full + MBEDTLS_CTR_DRBG_USE_128_BIT_KEY + MBEDTLS_ENTROPY_FORCE_SHA256 (ASan build)"
scripts/config.py full
scripts/config.py unset MBEDTLS_MEMORY_BUFFER_ALLOC_C
scripts/config.py set MBEDTLS_CTR_DRBG_USE_128_BIT_KEY
scripts/config.py set MBEDTLS_ENTROPY_FORCE_SHA256
CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
Expand Down
5 changes: 5 additions & 0 deletions tests/src/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <test/constant_flow.h>
#include <test/helpers.h>
#include <test/macros.h>
#include <test/platform.h>
#include <string.h>

/*----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -98,6 +99,10 @@ void mbedtls_test_info_reset( void )
mbedtls_test_info.filename = 0;
memset( mbedtls_test_info.line1, 0, sizeof( mbedtls_test_info.line1 ) );
memset( mbedtls_test_info.line2, 0, sizeof( mbedtls_test_info.line2 ) );

#if defined(MBEDTLS_TEST_PLATFORM_MACROS)
mbedtls_test_reset_platform_macro_counters( );
#endif /* MBEDTLS_TEST_PLATFORM_MACROS */
}

int mbedtls_test_equal( const char *test, int line_no, const char* filename,
Expand Down
47 changes: 47 additions & 0 deletions tests/src/platform_test_helpers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright The Mbed TLS Contributors
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <stdlib.h>
#include <string.h>

#include <test/helpers.h>
#include <test/macros.h>
#include <test/platform.h>

#if defined(MBEDTLS_TEST_PLATFORM_MACROS)

mbedtls_test_platform_function_counters_t mbedtls_test_platform_macro_counters;

void mbedtls_test_reset_platform_macro_counters( void )
{
memset( &mbedtls_test_platform_macro_counters,
0, sizeof( mbedtls_test_platform_macro_counters ) );
}

void *mbedtls_test_platform_calloc_macro( size_t nbmem, size_t size )
{
++mbedtls_test_platform_macro_counters.calloc;
return( calloc( nbmem, size ) );
}

void mbedtls_test_platform_free_macro( void* ptr )
{
++mbedtls_test_platform_macro_counters.free;
free( ptr );
}

#endif /* MBEDTLS_TEST_PLATFORM_MACROS */
5 changes: 5 additions & 0 deletions tests/suites/test_suite_platform_functions.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Memory macros
memory_macros:

Memory variables
memory_variables:
Loading