1 /* 2 * Copyright (c) 2001-2022, Arm Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #ifndef _RUN_INTEGRATION_HELPER_H_ 8 #define _RUN_INTEGRATION_HELPER_H_ 9 10 #include "mbedtls/build_info.h" 11 12 #include <inttypes.h> 13 14 /* cc lib */ 15 #include "cc_rnd_common.h" 16 #include "cc_pal_types.h" 17 #include "cc_pal_perf.h" 18 19 #include "mbedtls/platform.h" 20 #include "mbedtls/ctr_drbg.h" 21 /* pal */ 22 #include "test_pal_mem.h" 23 #include "test_pal_mem_s.h" 24 25 #include "run_integration_profiler.h" 26 27 #define RUNIT_ALIGN32_SLACK (sizeof(uint32_t) - 1) 28 29 #define RUNIT_DEBUG_ALLOC(_pStruct) do {} while(0); // RUNIT_PRINT_DBG("%s[%p]\n", #_pStruct, _pStruct) 30 31 /************************************************************ 32 * 33 * Alloc macros 34 * 35 ************************************************************/ 36 #define FREE_IF_NOT_NULL(_buff) \ 37 if (_buff.buf != NULL) { \ 38 mbedtls_free(_buff.buf); \ 39 } 40 41 #define ALLOC_VERIFY(_buff, _size) \ 42 do { \ 43 if (_buff == NULL) \ 44 { \ 45 RUNIT_PRINT_ERROR("Failed to allocate %u bytes for %s\n", (unsigned int)_size, #_buff); \ 46 rc = RUNIT_ERROR__FAIL; \ 47 goto bail; \ 48 } \ 49 } while(0) 50 51 /** 52 * Allocate 8 bit aligned buf using mbedtls 53 * the buf will be unaligned on purpose 54 */ 55 #define MALLOC(_buff, _pStruct, _size) \ 56 do { \ 57 runIt_malloc(&_buff, _size); \ 58 ALLOC_VERIFY(_buff.buf, _size); \ 59 } while(0) 60 61 #define ALLOC(_buff, _pStruct, _size) \ 62 do { \ 63 MALLOC(_buff, _pStruct, _size); \ 64 _pStruct = (void*)((void*)GET_UNALIGNED_PTR(_buff)); \ 65 RUNIT_DEBUG_ALLOC(_pStruct); \ 66 } while(0) 67 68 /** Allocate 32 bit aligned buf using mbedtls */ 69 #define ALLOC_STRUCT(_type, _buff, _pStruct) \ 70 do { \ 71 MALLOC(_buff, _pStruct, sizeof(_type)); \ 72 _pStruct = (_type*)((void*)GET_PTR(_buff)); \ 73 RUNIT_DEBUG_ALLOC(_pStruct); \ 74 } while(0) 75 76 /** 77 * Allocat 8 bit aligned buf using mbedtls and copy contents from other buf 78 * the buf will be unaligned on purpose 79 */ 80 #define ALLOC_AND_COPY(_buff, _pStruct, _src_buf, _size) \ 81 do { \ 82 ALLOC(_buff, _pStruct, _size); \ 83 RUNIT_DEBUG_ALLOC(_pStruct); \ 84 memcpy((uint8_t*)_pStruct, (uint8_t*)_src_buf, _size); \ 85 RUNIT_DEBUG_ALLOC(_pStruct); \ 86 } while(0) 87 88 /** Allocate 32 bit aligned buf using mbedtls and copy contents from other buf */ 89 #define ALLOC32_AND_COPY(_buff, _pStruct, _src_buf, _size) \ 90 do { \ 91 ALLOC(_buff, _pStruct, _size); \ 92 _pStruct = (void*)((void*)GET_PTR(_buff)); \ 93 memcpy((uint8_t*)_pStruct, (uint8_t*)_src_buf, _size); \ 94 RUNIT_DEBUG_ALLOC(_pStruct); \ 95 } while(0) 96 97 /** Allocate 32 bit aligned buf using mbedtls */ 98 #define ALLOC32(_buff, _pStruct, _size) \ 99 do { \ 100 ALLOC(_buff, _pStruct, _size); \ 101 _pStruct = (void*)((void*)GET_PTR(_buff)); \ 102 RUNIT_DEBUG_ALLOC(_pStruct); \ 103 } while(0) 104 105 #ifdef ARCH_V8M 106 #define RUNIT_TEST_PAL_ALLOC Test_PalDMAContigBufferAlloc_s 107 #define RUNIT_TEST_PAL_FREE Test_PalDMAContigBufferFree_s 108 #else 109 #define RUNIT_TEST_PAL_ALLOC Test_PalDMAContigBufferAlloc 110 #define RUNIT_TEST_PAL_FREE Test_PalDMAContigBufferFree 111 #endif 112 113 /** Allocate 32 bit aligned buf using pal */ 114 #define ALLOC_BUFF_ALIGN32(_bufWithSlack, _alignedBuff, _wantedBuffSize) \ 115 do { \ 116 _bufWithSlack = (uint8_t*)RUNIT_TEST_PAL_ALLOC(_wantedBuffSize + RUNIT_ALIGN32_SLACK);\ 117 ALLOC_VERIFY(_bufWithSlack, _wantedBuffSize + RUNIT_ALIGN32_SLACK); \ 118 runIt_buffAlign32(_bufWithSlack, &_alignedBuff, _wantedBuffSize, #_bufWithSlack); \ 119 }while(0) 120 121 /** 122 * 123 * Assert Macros 124 * 125 */ 126 #define RUNIT_ASSERT_WITH_RESULT(_c, _exp) \ 127 do { \ 128 int _ret = 0; \ 129 runItPerfType_t _type = PERF_TYPE_TEST_NOT_SET; \ 130 runItPerfData_t _data = 0; \ 131 _type = runIt_perfTypeFromStr(#_c, ""); \ 132 RUNIT_PRINT_DBG("running %-30.30s\n", #_c); \ 133 _data = runIt_perfOpenNewEntry(_type); \ 134 if( ( _ret = (int)_c) != _exp ) \ 135 { \ 136 RUNIT_PRINT_ERROR( "failed with rc 0x%08x\n", _ret ); \ 137 rc = RUNIT_ERROR__FAIL; \ 138 runIt_perfCloseEntry(_data, _type); \ 139 goto bail; \ 140 } \ 141 runIt_perfCloseEntry(_data, _type); \ 142 } while(0) 143 144 #define RUNIT_ASSERT(_c) \ 145 do { \ 146 if( !(_c)) \ 147 { \ 148 RUNIT_PRINT_ERROR( "failed\n" ); \ 149 rc = RUNIT_ERROR__FAIL; \ 150 goto bail; \ 151 } \ 152 } \ 153 while(0) 154 155 #define RUNIT_API(_c) \ 156 do { \ 157 runItPerfType_t _type = PERF_TYPE_TEST_NOT_SET; \ 158 runItPerfData_t _data = 0; \ 159 _type = runIt_perfTypeFromStr(#_c, ""); \ 160 RUNIT_PRINT_DBG("running %-30.30s\n", #_c); \ 161 _data = runIt_perfOpenNewEntry(_type); \ 162 _c; \ 163 runIt_perfCloseEntry(_data, _type); \ 164 } while(0) 165 166 #define RUNIT_API_ASSIGNMENT(_res, _c) \ 167 do { \ 168 runItPerfType_t _type = PERF_TYPE_TEST_NOT_SET; \ 169 runItPerfData_t _data = 0; \ 170 _type = runIt_perfTypeFromStr(#_c, ""); \ 171 RUNIT_PRINT_DBG("running %-30.30s\n", #_c); \ 172 _data = runIt_perfOpenNewEntry(_type); \ 173 _res = _c; \ 174 runIt_perfCloseEntry(_data, _type); \ 175 } while(0) 176 177 #define RUNIT_ASSERT_API(_c) \ 178 do { \ 179 runItPerfType_t _type = PERF_TYPE_TEST_NOT_SET; \ 180 runItPerfData_t _data = 0; \ 181 _type = runIt_perfTypeFromStr(#_c, ""); \ 182 RUNIT_PRINT_DBG("running %-30.30s\n", #_c); \ 183 _data = runIt_perfOpenNewEntry(_type); \ 184 RUNIT_ASSERT(_c); \ 185 runIt_perfCloseEntry(_data, _type); \ 186 } while(0) 187 188 #define RUNIT_ASSERT_W_PARAM(_s, _c) \ 189 do { \ 190 runItPerfType_t _type = PERF_TYPE_TEST_NOT_SET; \ 191 runItPerfData_t _data = 0; \ 192 _type = runIt_perfTypeFromStr(#_c, _s); \ 193 RUNIT_PRINT_DBG("running %-30.30s\n", #_c); \ 194 _data = runIt_perfOpenNewEntry(_type); \ 195 RUNIT_ASSERT(_c); \ 196 runIt_perfCloseEntry(_data, _type); \ 197 } while(0) 198 199 #define GET_PTR(_c) (_c.ba) 200 #define GET_UNALIGNED_PTR(_c) ((uint8_t*)(_c.ba) + 1) 201 202 #define RUNIT_PERF_REG_API_W_PARAM(_c, _s, _is_hw) \ 203 runIt_perfEntryInit(#_c, _s, _is_hw) 204 205 #define RUNIT_PERF_REG_API(_c, _is_hw) \ 206 runIt_perfEntryInit(#_c, "", _is_hw) 207 /************************************************************ 208 * 209 * type decelerations 210 * 211 ************************************************************/ 212 typedef struct rnd_buf_info 213 { 214 unsigned char *buf; 215 size_t length; 216 } rnd_buf_info; 217 218 typedef struct RunItPtr 219 { 220 uint8_t* buf; 221 uint32_t* ba; 222 size_t length; 223 } RunItPtr; 224 225 /************************************************************ 226 * 227 * externs 228 * 229 ******************* *****************************************/ 230 extern CCRndContext_t* gpRndContext; 231 extern mbedtls_ctr_drbg_context* gpRndState; 232 extern uint32_t gRunItStackSize; 233 234 /************************************************************ 235 * 236 * function prototypes 237 * 238 ************************************************************/ 239 void runIt_buffAlign32(uint8_t* pBuff, uint32_t **ppBuffAligned, uint32_t wantedBuffSize, const char * name); 240 int runIt_unhexify(unsigned char *obuf, const char *ibuf); 241 void runIt_hexify(unsigned char *obuf, const unsigned char *ibuf, int len); 242 int runIt_rndBufferRand(void *rng_state, unsigned char *output, size_t len); 243 int runIt_rand(void *rng_state, unsigned char *output, size_t len); 244 int runIt_free(RunItPtr *ptrElement); 245 int runIt_malloc(RunItPtr *ptrElement, size_t length); 246 uint32_t runIt_flashReadWrap(void* flashAddress, uint8_t *memDst, uint32_t sizeToRead, void* context); 247 int runIt_buildRandomBuffer(uint8_t *pBuf, size_t bufSize); 248 #endif //_RUN_INTEGRATION_HELPER_H_ 249