tests: Isolate mbedtls_param_failed() long jump

In preparation of moving mbedtls_param_failed() to test
common code, isolate mbedtls_param_failed() long
jump data and set up from unit test data and code.

Signed-off-by: Ronald Cron <ronald.cron@arm.com>
This commit is contained in:
Ronald Cron 2020-07-01 15:17:05 +02:00
parent 76883ec853
commit 579fd28527
2 changed files with 62 additions and 15 deletions

View file

@ -259,16 +259,17 @@ typedef struct data_tag
*
* \param TEST The test expression to be tested.
*/
#define TEST_INVALID_PARAM( TEST ) \
do { \
memcpy(jmp_tmp, param_fail_jmp, sizeof(jmp_buf)); \
if( setjmp( param_fail_jmp ) == 0 ) \
{ \
TEST; \
test_fail( #TEST, __LINE__, __FILE__ ); \
goto exit; \
} \
memcpy(param_fail_jmp, jmp_tmp, sizeof(jmp_buf)); \
#define TEST_INVALID_PARAM( TEST ) \
do { \
memcpy( jmp_tmp, mbedtls_test_param_failed_get_state_buf( ), \
sizeof( jmp_tmp ) ); \
if( setjmp( mbedtls_test_param_failed_get_state_buf( ) ) == 0 ) \
{ \
TEST; \
test_fail( #TEST, __LINE__, __FILE__ ); \
goto exit; \
} \
mbedtls_test_param_failed_reset_state( ); \
} while( 0 )
#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
@ -373,12 +374,13 @@ typedef struct
uint8_t expected_call;
uint8_t expected_call_happened;
jmp_buf state;
mbedtls_test_param_failed_location_record_t location_record;
}
param_failed_ctx_t;
static param_failed_ctx_t param_failed_ctx;
jmp_buf param_fail_jmp;
jmp_buf jmp_tmp;
#endif
@ -478,6 +480,47 @@ int mbedtls_test_param_failed_check_expected_call( void )
return( -1 );
}
/**
* \brief Get a pointer to the object of type jmp_buf holding the execution
* state information used by mbedtls_param_failed() to do a long jump.
*
* \note If a call to mbedtls_param_failed() is not expected in the sense
* that there is no call to mbedtls_test_param_failed_expect_call()
* preceding it, then mbedtls_param_failed() will try to restore the
* execution to the state stored in the jmp_buf object whose address
* is returned by the present function.
*
* \note The returned pointer is of type void* as its type is opaque,
* implementation dependent (jmp_buf is an array type not the type of
* one element of an array).
*
* \return Address of the object of type jmp_buf holding the execution state
* information used by mbedtls_param_failed() to do a long jump.
*/
void* mbedtls_test_param_failed_get_state_buf( void )
{
return &param_failed_ctx.state[0];
}
/**
* \brief Reset the execution state used by mbedtls_param_failed() to do a
* long jump.
*
* \note If a call to mbedtls_param_failed() is not expected in the sense
* that there is no call to mbedtls_test_param_failed_expect_call()
* preceding it, then mbedtls_param_failed() will try to restore the
* execution state that this function reset.
*
* \note It is recommended to reset the execution state when the state
* is not relevant anymore. That way an unexpected call to
* mbedtls_param_failed() will not trigger a long jump with
* undefined behavior but rather a long jump that will rather fault.
*/
void mbedtls_test_param_failed_reset_state( void )
{
memset( param_failed_ctx.state, 0, sizeof( param_failed_ctx.state ) );
}
void mbedtls_param_failed( const char *failure_condition,
const char *file,
int line )
@ -495,9 +538,13 @@ void mbedtls_param_failed( const char *failure_condition,
}
else
{
/* ...else we treat this as an error */
/* ...else try a long jump. If the execution state has not been set-up
* or reset then the long jump buffer is all zero's and the call will
* with high probability fault, emphasizing there is something to look
* at.
*/
longjmp( param_fail_jmp, 1 );
longjmp( param_failed_ctx.state, 1 );
}
}
#endif

View file

@ -169,7 +169,7 @@ void execute_function_ptr(TestWrapper_t fp, void **params)
#if defined(MBEDTLS_CHECK_PARAMS)
mbedtls_test_param_failed_location_record_t location_record;
if ( setjmp( param_fail_jmp ) == 0 )
if ( setjmp( mbedtls_test_param_failed_get_state_buf( ) ) == 0 )
{
fp( params );
}
@ -183,7 +183,7 @@ void execute_function_ptr(TestWrapper_t fp, void **params)
test_info.result = TEST_RESULT_FAILED;
}
memset( param_fail_jmp, 0, sizeof(jmp_buf) );
mbedtls_test_param_failed_reset_state( );
#else
fp( params );
#endif