Skip to content

Commit 19e88be

Browse files
authored
Flash Ram Sim no malloc support. Integer sign fixes (#140)
* Implement ability to specify flash RamSim memory location instead of using malloc * Fix integer sign for variables and fields using negative error values * Remove malloc from flash ram sim. Update tests to use stack memory for flash ram sim * Remove all traces of WOLFHSM_CFG_FLASH_RAMSIM_MALLOC. Move arg checks to top of function.
1 parent 97e0343 commit 19e88be

File tree

15 files changed

+123
-69
lines changed

15 files changed

+123
-69
lines changed

benchmark/wh_bench.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ int wh_Bench_ClientServer_Posix(void)
562562
{
563563
uint8_t req[BUFFER_SIZE] = {0};
564564
uint8_t resp[BUFFER_SIZE] = {0};
565+
uint8_t memory[FLASH_RAM_SIZE] = {0};
565566

566567
/* Transport memory configuration */
567568
whTransportMemConfig tmcf[1] = {{
@@ -601,6 +602,7 @@ int wh_Bench_ClientServer_Posix(void)
601602
.sectorSize = FLASH_RAM_SIZE / 2,
602603
.pageSize = 8,
603604
.erasedByte = (uint8_t)0,
605+
.memory = memory,
604606
}};
605607
const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB};
606608

@@ -679,4 +681,4 @@ int wh_Bench_ClientServer_Posix(void)
679681

680682
#endif /* WOLFHSM_CFG_TEST_POSIX */
681683

682-
#endif /* WOLFHSM_CFG_BENCH_ENABLE */
684+
#endif /* WOLFHSM_CFG_BENCH_ENABLE */

examples/posix/tcp/wh_server_tcp/wh_server_tcp.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,8 @@ int main(int argc, char** argv)
607607
const char* nvmInitFilePath = NULL;
608608
int keyId = WH_KEYID_ERASED; /* Default key ID if none provided */
609609
int clientId = 12; /* Default client ID if none provided */
610+
uint8_t memory[FLASH_RAM_SIZE] = {0};
611+
610612

611613
/* Parse command-line arguments */
612614
for (int i = 1; i < argc; i++) {
@@ -645,6 +647,7 @@ int main(int argc, char** argv)
645647
.sectorSize = FLASH_RAM_SIZE / 2,
646648
.pageSize = 8,
647649
.erasedByte = (uint8_t)0,
650+
.memory = memory,
648651
}};
649652
const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB};
650653

src/wh_flash_ramsim.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
#include <stdint.h>
2828
#include <stddef.h> /* For NULL */
29-
#include <stdlib.h> /* For malloc/free */
29+
3030
#include <string.h>
3131
#include <stdbool.h>
3232

@@ -59,21 +59,19 @@ int whFlashRamsim_Init(void* context, const void* config)
5959
const whFlashRamsimCfg* cfg = (const whFlashRamsimCfg*)config;
6060

6161
if (ctx == NULL || cfg == NULL || (cfg->sectorSize == 0) ||
62-
(cfg->pageSize == 0) || (cfg->sectorSize % cfg->pageSize != 0)) {
62+
(cfg->pageSize == 0) || (cfg->sectorSize % cfg->pageSize != 0) ||
63+
cfg->memory == NULL || cfg->size == 0) {
6364
return WH_ERROR_BADARGS;
6465
}
6566

67+
memset(ctx, 0, sizeof(*ctx));
6668
ctx->size = cfg->size;
6769
ctx->sectorSize = cfg->sectorSize;
6870
ctx->pageSize = cfg->pageSize;
69-
ctx->memory = (uint8_t*)malloc(ctx->size);
71+
ctx->memory = cfg->memory;
7072
ctx->erasedByte = cfg->erasedByte;
7173
ctx->writeLocked = 0;
7274

73-
if (!ctx->memory) {
74-
return WH_ERROR_BADARGS;
75-
}
76-
7775
/* Initialize memory based on initData or simulate starting from erased flash */
7876
if (cfg->initData != NULL) {
7977
memcpy(ctx->memory, cfg->initData, ctx->size);
@@ -92,11 +90,6 @@ int whFlashRamsim_Cleanup(void* context)
9290
return WH_ERROR_BADARGS;
9391
}
9492

95-
if (ctx->memory != NULL) {
96-
free(ctx->memory);
97-
ctx->memory = NULL;
98-
}
99-
10093
return WH_ERROR_OK;
10194
}
10295

src/wh_nvm_flash.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ static int nfPartition_CheckDataRange(whNvmFlashContext* context,
121121
uint32_t byte_offset,
122122
uint32_t byte_count);
123123

124-
static uint32_t nfObject_Offset(whNvmFlashContext* context, int partition,
125-
int object_index);
124+
static int nfObject_Offset(whNvmFlashContext* context, int partition,
125+
int object_index, uint32_t *out_object_offset);
126126
static int nfObject_ProgramBegin(whNvmFlashContext* context, int partition,
127127
int object_index, uint32_t epoch, uint32_t start, whNvmMetadata* meta);
128128
static int nfObject_ProgramDataBytes(whNvmFlashContext* context, int partition,
@@ -512,16 +512,19 @@ static int nfPartition_CheckDataRange(whNvmFlashContext* context,
512512
return WH_ERROR_OK;
513513
}
514514

515-
static uint32_t nfObject_Offset(whNvmFlashContext* context, int partition,
516-
int object_index)
515+
static int nfObject_Offset(whNvmFlashContext* context, int partition,
516+
int object_index, uint32_t *out_object_offset)
517517
{
518-
if (context == NULL) {
518+
if (context == NULL || out_object_offset == NULL) {
519519
return WH_ERROR_BADARGS;
520520
}
521521

522-
return nfPartition_Offset(context,partition) +
523-
NF_PARTITION_DIRECTORY_OFFSET +
524-
NF_DIRECTORY_OBJECT_OFFSET(object_index);
522+
523+
*out_object_offset = nfPartition_Offset(context,partition) +
524+
NF_PARTITION_DIRECTORY_OFFSET +
525+
NF_DIRECTORY_OBJECT_OFFSET(object_index);
526+
527+
return WH_ERROR_OK;
525528
}
526529

527530
static int nfObject_ProgramBegin(whNvmFlashContext* context, int partition,
@@ -539,7 +542,10 @@ static int nfObject_ProgramBegin(whNvmFlashContext* context, int partition,
539542
return WH_ERROR_BADARGS;
540543
}
541544

542-
object_offset = nfObject_Offset(context, partition, object_index);
545+
rc = nfObject_Offset(context, partition, object_index, &object_offset);
546+
if (rc != WH_ERROR_OK) {
547+
return rc;
548+
}
543549

544550
/* Program the object epoch */
545551
rc = wh_FlashUnit_Program(
@@ -601,14 +607,18 @@ static int nfObject_ProgramDataBytes(whNvmFlashContext* context, int partition,
601607
static int nfObject_ProgramFinish(whNvmFlashContext* context, int partition,
602608
int object_index, uint32_t byte_count)
603609
{
610+
int rc;
604611
uint32_t object_offset = 0;
605612
whFlashUnit state_count = BASE_STATE | WHFU_BYTES2UNITS(byte_count);
606613

607614
if ((context == NULL) || (context->cb == NULL)) {
608615
return WH_ERROR_BADARGS;
609616
}
610617

611-
object_offset = nfObject_Offset(context, partition, object_index);
618+
rc = nfObject_Offset(context, partition, object_index, &object_offset);
619+
if (rc != WH_ERROR_OK) {
620+
return rc;
621+
}
612622

613623
/* Program the object flag->state_count */
614624
return wh_FlashUnit_Program(

test/wh_test_cert.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
static int whTest_CertNonExportable(whClientContext* client);
5454
#endif
5555

56+
#define FLASH_RAM_SIZE (1024 * 1024) /* 1MB */
57+
#define FLASH_SECTOR_SIZE (128 * 1024) /* 128KB */
58+
#define FLASH_PAGE_SIZE (8) /* 8B */
59+
5660
#ifdef WOLFHSM_CFG_ENABLE_SERVER
5761
/* Run certificate configuration tests */
5862
int whTest_CertServerCfg(whServerConfig* serverCfg)
@@ -599,12 +603,14 @@ int whTest_CertRamSim(void)
599603
.server_id = 124,
600604
}};
601605
/* RamSim Flash state and configuration */
606+
uint8_t memory[FLASH_RAM_SIZE] = {0};
602607
whFlashRamsimCtx fc[1] = {0};
603608
whFlashRamsimCfg fc_conf[1] = {{
604-
.size = 1024 * 1024, /* 1MB Flash */
605-
.sectorSize = 128 * 1024, /* 128KB Sector Size */
606-
.pageSize = 8, /* 8B Page Size */
609+
.size = FLASH_RAM_SIZE, /* 1MB Flash */
610+
.sectorSize = FLASH_SECTOR_SIZE, /* 128KB Sector Size */
611+
.pageSize = FLASH_PAGE_SIZE, /* 8B Page Size */
607612
.erasedByte = ~(uint8_t)0,
613+
.memory = memory,
608614
}};
609615
const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB};
610616

test/wh_test_clientserver.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
#define REPEAT_COUNT 10
6262
#define ONE_MS 1000
6363
#define FLASH_RAM_SIZE (1024 * 1024) /* 1MB */
64+
#define FLASH_SECTOR_SIZE (128 * 1024) /* 128KB */
65+
#define FLASH_PAGE_SIZE (8) /* 8B */
6466

6567
#ifdef WOLFHSM_CFG_DMA
6668
#define DMA_TEST_MEM_NWORDS 3
@@ -708,12 +710,14 @@ int whTest_ClientServerSequential(void)
708710
}};
709711

710712
/* RamSim Flash state and configuration */
713+
uint8_t memory[FLASH_RAM_SIZE] = {0};
711714
whFlashRamsimCtx fc[1] = {0};
712715
whFlashRamsimCfg fc_conf[1] = {{
713-
.size = 1024 * 1024, /* 1MB Flash */
714-
.sectorSize = 128 * 1024, /* 128KB Sector Size */
715-
.pageSize = 8, /* 8B Page Size */
716+
.size = FLASH_RAM_SIZE, /* 1MB Flash */
717+
.sectorSize = FLASH_SECTOR_SIZE, /* 128KB Sector Size */
718+
.pageSize = FLASH_PAGE_SIZE, /* 8B Page Size */
716719
.erasedByte = ~(uint8_t)0,
720+
.memory = memory,
717721
}};
718722
const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB};
719723

@@ -1734,12 +1738,14 @@ static int wh_ClientServer_MemThreadTest(void)
17341738
}};
17351739

17361740
/* RamSim Flash state and configuration */
1741+
uint8_t memory[FLASH_RAM_SIZE] = {0};
17371742
whFlashRamsimCtx fc[1] = {0};
17381743
whFlashRamsimCfg fc_conf[1] = {{
17391744
.size = FLASH_RAM_SIZE,
17401745
.sectorSize = FLASH_RAM_SIZE/2,
17411746
.pageSize = 8,
17421747
.erasedByte = (uint8_t)0,
1748+
.memory = memory,
17431749
}};
17441750
const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB};
17451751

@@ -1825,12 +1831,14 @@ static int wh_ClientServer_PosixMemMapThreadTest(void)
18251831
}};
18261832

18271833
/* RamSim Flash state and configuration */
1834+
uint8_t memory[FLASH_RAM_SIZE] = {0};
18281835
whFlashRamsimCtx fc[1] = {0};
18291836
whFlashRamsimCfg fc_conf[1] = {{
18301837
.size = FLASH_RAM_SIZE,
18311838
.sectorSize = FLASH_RAM_SIZE / 2,
18321839
.pageSize = 8,
18331840
.erasedByte = (uint8_t)0,
1841+
.memory = memory,
18341842
}};
18351843
const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB};
18361844

test/wh_test_crypto.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
#include "port/posix/posix_flash_file.h"
6565
#endif
6666

67+
#define FLASH_RAM_SIZE (1024 * 1024) /* 1MB */
68+
#define FLASH_SECTOR_SIZE (128 * 1024) /* 128KB */
69+
#define FLASH_PAGE_SIZE (8) /* 8B */
70+
6771
enum {
6872
/* Total size needs to fit:
6973
* - Transport CSR (whTransportMemCsr)
@@ -3028,12 +3032,14 @@ static int wh_ClientServer_MemThreadTest(void)
30283032
}};
30293033

30303034
/* RamSim Flash state and configuration */
3035+
uint8_t memory[FLASH_RAM_SIZE] = {0};
30313036
whFlashRamsimCtx fc[1] = {0};
30323037
whFlashRamsimCfg fc_conf[1] = {{
3033-
.size = 1024 * 1024, /* 1MB Flash */
3034-
.sectorSize = 128 * 1024, /* 128KB Sector Size */
3035-
.pageSize = 8, /* 8B Page Size */
3038+
.size = FLASH_RAM_SIZE, /* 1MB Flash */
3039+
.sectorSize = FLASH_SECTOR_SIZE, /* 128KB Sector Size */
3040+
.pageSize = FLASH_PAGE_SIZE, /* 8B Page Size */
30363041
.erasedByte = ~(uint8_t)0,
3042+
.memory = memory,
30373043
}};
30383044
const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB};
30393045

test/wh_test_flash_ramsim.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ int whTest_Flash_RamSim(void)
6262
{
6363
int ret;
6464
whFlashRamsimCtx ctx;
65+
uint8_t memory[TEST_FLASH_SIZE] = {0};
6566
whFlashRamsimCfg cfg = {.size = TEST_FLASH_SIZE,
6667
.sectorSize = TEST_SECTOR_SIZE,
6768
.pageSize = TEST_PAGE_SIZE,
68-
.erasedByte = 0xFF};
69+
.erasedByte = 0xFF,
70+
.memory = memory,
71+
};
6972

7073
uint8_t testData[TEST_PAGE_SIZE] = {0};
7174
uint8_t readData[TEST_PAGE_SIZE] = {0};
@@ -201,4 +204,4 @@ int whTest_Flash_RamSim(void)
201204
return 0;
202205
}
203206

204-
#endif /* WOLFHSM_CFG_ENABLE_SERVER */
207+
#endif /* WOLFHSM_CFG_ENABLE_SERVER */

test/wh_test_nvm_flash.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
#include "port/posix/posix_flash_file.h"
4242
#endif
4343

44+
#define FLASH_RAM_SIZE (1024 * 1024) /* 1MB */
45+
#define FLASH_SECTOR_SIZE (4096) /* 4KB */
46+
#define FLASH_PAGE_SIZE (8) /* 8B */
47+
4448
#if defined(WOLFHSM_CFG_TEST_VERBOSE)
4549
static void _HexDump(const char* p, size_t data_len)
4650
{
@@ -469,13 +473,15 @@ int whTest_NvmFlashCfg(whNvmFlashConfig* cfg)
469473
int whTest_NvmFlash_RamSim(void)
470474
{
471475
/* HAL Flash state and configuration */
476+
uint8_t memory[FLASH_RAM_SIZE] = {0};
472477
const whFlashCb myCb[1] = {WH_FLASH_RAMSIM_CB};
473478
whFlashRamsimCtx myHalFlashCtx[1] = {0};
474479
whFlashRamsimCfg myHalFlashCfg[1] = {{
475-
.size = 1024 * 1024, /* 1MB Flash */
476-
.sectorSize = 4096, /* 4KB Sector Size */
477-
.pageSize = 8, /* 8B Page Size */
480+
.size = FLASH_RAM_SIZE, /* 1MB Flash */
481+
.sectorSize = FLASH_SECTOR_SIZE, /* 4KB Sector Size */
482+
.pageSize = FLASH_PAGE_SIZE, /* 8B Page Size */
478483
.erasedByte = (uint8_t)0,
484+
.memory = memory,
479485
}};
480486

481487
WH_TEST_RETURN_ON_FAIL(whTest_Flash(myCb, myHalFlashCtx, myHalFlashCfg));
@@ -538,4 +544,4 @@ int whTest_NvmFlash(void)
538544
return 0;
539545
}
540546

541-
#endif /* WOLFHSM_CFG_ENABLE_SERVER */
547+
#endif /* WOLFHSM_CFG_ENABLE_SERVER */

test/wh_test_server_img_mgr.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454

5555
#include "wh_test_common.h"
5656

57+
#define FLASH_RAM_SIZE (1024 * 1024) /* 1MB */
58+
#define FLASH_SECTOR_SIZE (128 * 1024) /* 128KB */
59+
#define FLASH_PAGE_SIZE (8) /* 8B */
60+
5761
/* Test data to be "verified" */
5862
static const uint8_t testData[] = {
5963
0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64,
@@ -1214,12 +1218,14 @@ int whTest_ServerImgMgr(void)
12141218
}};
12151219

12161220
/* RamSim Flash state and configuration */
1221+
uint8_t memory[FLASH_RAM_SIZE] = {0};
12171222
whFlashRamsimCtx fc[1] = {0};
12181223
whFlashRamsimCfg fc_conf[1] = {{
1219-
.size = 1024 * 1024, /* 1MB Flash */
1220-
.sectorSize = 128 * 1024, /* 128KB Sector Size */
1221-
.pageSize = 8, /* 8B Page Size */
1224+
.size = FLASH_RAM_SIZE, /* 1MB Flash */
1225+
.sectorSize = FLASH_SECTOR_SIZE, /* 128KB Sector Size */
1226+
.pageSize = FLASH_PAGE_SIZE, /* 8B Page Size */
12221227
.erasedByte = ~(uint8_t)0,
1228+
.memory = memory,
12231229
}};
12241230
const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB};
12251231

@@ -1297,4 +1303,4 @@ int whTest_ServerImgMgr(void)
12971303

12981304
#endif /* WOLFHSM_CFG_NO_CRYPTO */
12991305
#endif /* WOLFHSM_CFG_SERVER_IMG_MGR && WOLFHSM_CFG_ENABLE_SERVER &&
1300-
!WOLFHSM_CFG_NO_CRYPTO */
1306+
!WOLFHSM_CFG_NO_CRYPTO */

0 commit comments

Comments
 (0)