Support different types in the parameter store

The test framework stores size_t and int32_t values in the parameter store
by converting them all to int. This is ok in practice, since we assume int
covers int32_t and we don't have test data larger than 2GB. But it's
confusing and error-prone. So make the parameter store a union, which allows
size_t values not to be potentially truncated and makes the code a little
clearer.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2022-12-04 13:10:55 +01:00
parent 5226eb5cd3
commit b3c2eaf00f
5 changed files with 54 additions and 13 deletions

View file

@ -0,0 +1,38 @@
/**
* \file arguments.h
*
* \brief Manipulation of test arguments.
*
* Much of the code is in host_test.function, to be migrated here later.
*/
/*
* 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 TEST_ARGUMENTS_H
#define TEST_ARGUMENTS_H
#include "mbedtls/build_info.h"
#include <stdint.h>
#include <stdlib.h>
typedef union {
size_t len;
int32_t s32;
} mbedtls_test_argument_t;
#endif /* TEST_ARGUMENTS_H */

View file

@ -471,7 +471,7 @@ def parse_function_arguments(line):
continue
if re.search(INT_CHECK_REGEX, arg.strip()):
args.append('int')
args_dispatch.append('*( (int *) params[%d] )' % arg_idx)
args_dispatch.append('((mbedtls_test_argument_t*)params[%d])->s32' % arg_idx)
elif re.search(CHAR_CHECK_REGEX, arg.strip()):
args.append('char*')
args_dispatch.append('(char *) params[%d]' % arg_idx)
@ -479,7 +479,7 @@ def parse_function_arguments(line):
args.append('hex')
# create a structure
pointer_initializer = '(uint8_t *) params[%d]' % arg_idx
len_initializer = '*( (uint32_t *) params[%d] )' % (arg_idx+1)
len_initializer = '((mbedtls_test_argument_t*)params[%d])->len' % (arg_idx+1)
local_vars += """ data_t data%d = {%s, %s};
""" % (arg_idx, pointer_initializer, len_initializer)

View file

@ -485,9 +485,10 @@ class ParseFuncSignature(TestCase):
args, local, arg_dispatch = parse_function_arguments(line)
self.assertEqual(args, ['char*', 'int', 'int'])
self.assertEqual(local, '')
self.assertEqual(arg_dispatch, ['(char *) params[0]',
'*( (int *) params[1] )',
'*( (int *) params[2] )'])
self.assertEqual(arg_dispatch,
['(char *) params[0]',
'((mbedtls_test_argument_t*)params[1])->s32',
'((mbedtls_test_argument_t*)params[2])->s32'])
def test_hex_params(self):
"""
@ -499,10 +500,10 @@ class ParseFuncSignature(TestCase):
self.assertEqual(args, ['char*', 'hex', 'int'])
self.assertEqual(local,
' data_t data1 = {(uint8_t *) params[1], '
'*( (uint32_t *) params[2] )};\n')
'((mbedtls_test_argument_t*)params[2])->len};\n')
self.assertEqual(arg_dispatch, ['(char *) params[0]',
'&data1',
'*( (int *) params[3] )'])
'((mbedtls_test_argument_t*)params[3])->s32'])
def test_unsupported_arg(self):
"""

View file

@ -2,6 +2,7 @@
/*----------------------------------------------------------------------------*/
/* Headers */
#include <test/arguments.h>
#include <test/helpers.h>
#include <test/macros.h>
#include <test/random.h>

View file

@ -28,7 +28,7 @@ int verify_string(char **str)
* integer value.
*
* \param str Input string.
* \param value Pointer to int for output value.
* \param p_value Pointer to output value.
*
* \return 0 if success else 1
*/
@ -203,7 +203,8 @@ static int parse_arguments(char *buf, size_t len, char **params,
*
* \return 0 for success else 1
*/
static int convert_params(size_t cnt, char **params, int32_t *int_params_store)
static int convert_params(size_t cnt, char **params,
mbedtls_test_argument_t *int_params_store)
{
char **cur = params;
char **out = params;
@ -221,7 +222,7 @@ static int convert_params(size_t cnt, char **params, int32_t *int_params_store)
break;
}
} else if (strcmp(type, "int") == 0) {
if (verify_int(val, int_params_store) == 0) {
if (verify_int(val, &int_params_store->s32) == 0) {
*out++ = (char *) int_params_store++;
} else {
ret = (DISPATCH_INVALID_TEST_DATA);
@ -235,7 +236,7 @@ static int convert_params(size_t cnt, char **params, int32_t *int_params_store)
mbedtls_test_unhexify((unsigned char *) val, strlen(val),
val, &len) == 0);
*int_params_store = len;
int_params_store->len = len;
*out++ = val;
*out++ = (char *) (int_params_store++);
} else {
@ -244,7 +245,7 @@ static int convert_params(size_t cnt, char **params, int32_t *int_params_store)
}
} else if (strcmp(type, "exp") == 0) {
int exp_id = strtol(val, NULL, 10);
if (get_expression(exp_id, int_params_store) == 0) {
if (get_expression(exp_id, &int_params_store->s32) == 0) {
*out++ = (char *) int_params_store++;
} else {
ret = (DISPATCH_INVALID_TEST_DATA);
@ -463,7 +464,7 @@ int execute_tests(int argc, const char **argv)
char buf[5000];
char *params[50];
/* Store for processed integer params. */
int32_t int_params[50];
mbedtls_test_argument_t int_params[50];
void *pointer;
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
int stdout_fd = -1;