From 0037fc0eab351fa73a3a7ea0ddea797dbcc4bfbe Mon Sep 17 00:00:00 2001 From: Joseph Hickey Date: Fri, 8 May 2020 15:25:56 -0400 Subject: [PATCH] Fix #679, Alignment warnings in UT Resolve remaining alignment warnings in the unit test and stub code. In es_UT.c test cases were passing a char* as a stack pointer (uint32*) Memory pool code was also using uint8[] buffer as uint32. Most casting becomes unnecessary when defined as the correct type. --- fsw/cfe-core/unit-test/es_UT.c | 101 +++++++++++++--------------- fsw/cfe-core/ut-stubs/ut_es_stubs.c | 52 +++++++------- 2 files changed, 71 insertions(+), 82 deletions(-) diff --git a/fsw/cfe-core/unit-test/es_UT.c b/fsw/cfe-core/unit-test/es_UT.c index 1dae125ff..6a052234c 100644 --- a/fsw/cfe-core/unit-test/es_UT.c +++ b/fsw/cfe-core/unit-test/es_UT.c @@ -4382,6 +4382,7 @@ void TestAPI(void) uint32 Id; uint32 TestObjId; char AppName[32]; + uint32 StackBuf[8]; char CounterName[11]; char CDSName[CFE_MISSION_ES_CDS_MAX_NAME_LENGTH + 2]; int i; @@ -4748,8 +4749,8 @@ void TestAPI(void) Return = CFE_ES_CreateChildTask(&TaskId, "TaskName", TestAPI, - (uint32*) AppName, - 32, + StackBuf, + sizeof(StackBuf), 400, 0); UT_Report(__FILE__, __LINE__, @@ -4764,8 +4765,8 @@ void TestAPI(void) Return = CFE_ES_CreateChildTask(&TaskId, "TaskName", TestAPI, - (uint32*) AppName, - 32, + StackBuf, + sizeof(StackBuf), 400, 0); UT_Report(__FILE__, __LINE__, @@ -4778,8 +4779,8 @@ void TestAPI(void) Return = CFE_ES_CreateChildTask(NULL, "TaskName", TestAPI, - (uint32*) AppName, - 32, + StackBuf, + sizeof(StackBuf), 400, 0); UT_Report(__FILE__, __LINE__, @@ -4792,8 +4793,8 @@ void TestAPI(void) Return = CFE_ES_CreateChildTask(&TaskId, NULL, TestAPI, - (uint32*) AppName, - 32, + StackBuf, + sizeof(StackBuf), 400, 0); UT_Report(__FILE__, __LINE__, @@ -4806,8 +4807,8 @@ void TestAPI(void) Return = CFE_ES_CreateChildTask(NULL, NULL, TestAPI, - (uint32*) AppName, - 32, + StackBuf, + sizeof(StackBuf), 400, 0); UT_Report(__FILE__, __LINE__, @@ -4820,8 +4821,8 @@ void TestAPI(void) Return = CFE_ES_CreateChildTask(&TaskId, "TaskName", NULL, - (uint32*) AppName, - 32, + StackBuf, + sizeof(StackBuf), 2, 0); UT_Report(__FILE__, __LINE__, @@ -4840,8 +4841,8 @@ void TestAPI(void) Return = CFE_ES_CreateChildTask(&TaskId, "TaskName", TestAPI, - (uint32*) AppName, - 32, + StackBuf, + sizeof(StackBuf), 400, 0); UT_Report(__FILE__, __LINE__, @@ -4860,8 +4861,8 @@ void TestAPI(void) Return = CFE_ES_CreateChildTask(&TaskId, "TaskName", TestAPI, - (uint32*) AppName, - 32, + StackBuf, + sizeof(StackBuf), 400, 0); UT_Report(__FILE__, __LINE__, @@ -6247,8 +6248,8 @@ void TestESMempool(void) { CFE_ES_MemHandle_t HandlePtr; uint8 Buffer[CFE_PLATFORM_ES_MAX_BLOCK_SIZE]; - uint8 *address = NULL; - uint8 *address2 = NULL; + uint32 *address = NULL; + uint32 *address2 = NULL; Pool_t *PoolPtr; CFE_ES_MemPoolStats_t Stats; uint32 BlockSizes[4]; @@ -6307,7 +6308,7 @@ void TestESMempool(void) /* Test successfully getting the size of an existing pool buffer */ ES_ResetUnitTest(); UT_Report(__FILE__, __LINE__, - CFE_ES_GetPoolBufInfo(HandlePtr, (uint32 *) address) > 0, + CFE_ES_GetPoolBufInfo(HandlePtr, address) > 0, "CFE_ES_GetPoolBufInfo", "Get pool buffer size; successful"); @@ -6317,7 +6318,7 @@ void TestESMempool(void) ES_ResetUnitTest(); ((Pool_t *) HandlePtr)->UseMutex = CFE_ES_NO_MUTEX; UT_Report(__FILE__, __LINE__, - CFE_ES_GetPoolBufInfo(HandlePtr, (uint32 *) address) > 0, + CFE_ES_GetPoolBufInfo(HandlePtr, address) > 0, "CFE_ES_GetPoolBufInfo", "Get pool buffer size; successful (no mutex)"); ((Pool_t *) HandlePtr)->UseMutex = CFE_ES_USE_MUTEX; @@ -6325,14 +6326,14 @@ void TestESMempool(void) /* Test successfully returning a pool buffer to the memory pool */ ES_ResetUnitTest(); UT_Report(__FILE__, __LINE__, - CFE_ES_PutPoolBuf(HandlePtr, (uint32 *) address) > 0, + CFE_ES_PutPoolBuf(HandlePtr, address) > 0, "CFE_ES_PutPoolBuf", "Return buffer to the memory pool; successful"); /* Test successfully allocating an additional pool buffer */ ES_ResetUnitTest(); UT_Report(__FILE__, __LINE__, - CFE_ES_GetPoolBuf((uint32 **) &address, HandlePtr, 256) > 0, + CFE_ES_GetPoolBuf(&address, HandlePtr, 256) > 0, "CFE_ES_GetPoolBuf", "Allocate pool buffer [2]; successful"); @@ -6342,7 +6343,7 @@ void TestESMempool(void) ES_ResetUnitTest(); ((Pool_t *) HandlePtr)->UseMutex = CFE_ES_NO_MUTEX; UT_Report(__FILE__, __LINE__, - CFE_ES_PutPoolBuf(HandlePtr, (uint32 *) address) > 0, + CFE_ES_PutPoolBuf(HandlePtr, address) > 0, "CFE_ES_PutPoolBuf", "Return buffer to the second memory pool; successful"); ((Pool_t *) HandlePtr)->UseMutex = CFE_ES_USE_MUTEX; @@ -6372,7 +6373,7 @@ void TestESMempool(void) */ ES_ResetUnitTest(); UT_Report(__FILE__, __LINE__, - CFE_ES_GetPoolBuf((uint32 **) &address, + CFE_ES_GetPoolBuf(&address, HandlePtr2, 256) == CFE_ES_ERR_MEM_HANDLE, "CFE_ES_GetPoolBuf", @@ -6393,7 +6394,7 @@ void TestESMempool(void) */ ES_ResetUnitTest(); UT_Report(__FILE__, __LINE__, - CFE_ES_GetPoolBuf((uint32 **) &address, + CFE_ES_GetPoolBuf(&address, HandlePtr, 75000) == CFE_ES_ERR_MEM_BLOCK_SIZE, "CFE_ES_GetPoolBuf", @@ -6404,7 +6405,7 @@ void TestESMempool(void) */ ES_ResetUnitTest(); UT_Report(__FILE__, __LINE__, - CFE_ES_GetPoolBufInfo(HandlePtr, (uint32 *) address) == + CFE_ES_GetPoolBufInfo(HandlePtr, address) == CFE_ES_BUFFER_NOT_IN_POOL, "CFE_ES_GetPoolBufInfo", "Invalid memory pool handle"); @@ -6413,7 +6414,7 @@ void TestESMempool(void) ES_ResetUnitTest(); UT_Report(__FILE__, __LINE__, CFE_ES_PutPoolBuf(HandlePtr, - (uint32 *) address) == CFE_ES_ERR_MEM_HANDLE, + address) == CFE_ES_ERR_MEM_HANDLE, "CFE_ES_PutPoolBuf", "Invalid memory block"); @@ -6532,7 +6533,7 @@ void TestESMempool(void) BdPtr->Allocated = 717; UT_Report(__FILE__, __LINE__, CFE_ES_GetPoolBufInfo(HandlePtr, - (uint32 *) address) == + address) == CFE_ES_ERR_MEM_HANDLE, "CFE_ES_GetPoolBufInfo", "Invalid memory pool handle; unallocated block"); @@ -6545,8 +6546,7 @@ void TestESMempool(void) BdPtr = ((BD_t *)address) - 1; BdPtr->Allocated = 717; UT_Report(__FILE__, __LINE__, - CFE_ES_GetPoolBufInfo(HandlePtr, - (uint32 *) address) == + CFE_ES_GetPoolBufInfo(HandlePtr, address) == CFE_ES_ERR_MEM_HANDLE, "CFE_ES_GetPoolBufInfo", "Invalid memory pool handle; unallocated block (no mutex)"); @@ -6555,8 +6555,7 @@ void TestESMempool(void) /* Test returning a pool buffer using an unallocated block */ ES_ResetUnitTest(); UT_Report(__FILE__, __LINE__, - CFE_ES_PutPoolBuf(HandlePtr, - (uint32 *) address) == CFE_ES_ERR_MEM_HANDLE, + CFE_ES_PutPoolBuf(HandlePtr, address) == CFE_ES_ERR_MEM_HANDLE, "CFE_ES_PutPoolBuf", "Deallocate an unallocated block"); @@ -6566,8 +6565,7 @@ void TestESMempool(void) ES_ResetUnitTest(); ((Pool_t *) HandlePtr)->UseMutex = CFE_ES_NO_MUTEX; UT_Report(__FILE__, __LINE__, - CFE_ES_PutPoolBuf(HandlePtr, - (uint32 *) address) == CFE_ES_ERR_MEM_HANDLE, + CFE_ES_PutPoolBuf(HandlePtr, address) == CFE_ES_ERR_MEM_HANDLE, "CFE_ES_PutPoolBuf", "Deallocate an unallocated block (no mutex)"); ((Pool_t *) HandlePtr)->UseMutex = CFE_ES_USE_MUTEX; @@ -6579,8 +6577,7 @@ void TestESMempool(void) BdPtr->Allocated = 0xaaaa; BdPtr->CheckBits = 717; UT_Report(__FILE__, __LINE__, - CFE_ES_GetPoolBufInfo(HandlePtr, - (uint32 *) address) == + CFE_ES_GetPoolBufInfo(HandlePtr, address) == CFE_ES_ERR_MEM_HANDLE, "CFE_ES_GetPoolBufInfo", "Invalid memory pool handle; check bit pattern"); @@ -6594,8 +6591,7 @@ void TestESMempool(void) BdPtr->Allocated = 0xaaaa; BdPtr->CheckBits = 717; UT_Report(__FILE__, __LINE__, - CFE_ES_GetPoolBufInfo(HandlePtr, - (uint32 *) address) == + CFE_ES_GetPoolBufInfo(HandlePtr, address) == CFE_ES_ERR_MEM_HANDLE, "CFE_ES_GetPoolBufInfo", "Invalid memory pool handle; check bit pattern (no mutex)"); @@ -6606,8 +6602,7 @@ void TestESMempool(void) */ ES_ResetUnitTest(); UT_Report(__FILE__, __LINE__, - CFE_ES_PutPoolBuf(HandlePtr, - (uint32 *) address) == + CFE_ES_PutPoolBuf(HandlePtr, address) == CFE_ES_ERR_MEM_HANDLE, "CFE_ES_PutPoolBuf", "Invalid/corrupted memory descriptor"); @@ -6618,8 +6613,7 @@ void TestESMempool(void) ES_ResetUnitTest(); ((Pool_t *) HandlePtr)->UseMutex = CFE_ES_NO_MUTEX; UT_Report(__FILE__, __LINE__, - CFE_ES_PutPoolBuf(HandlePtr, - (uint32 *) address) == + CFE_ES_PutPoolBuf(HandlePtr, address) == CFE_ES_ERR_MEM_HANDLE, "CFE_ES_PutPoolBuf", "Invalid/corrupted memory descriptor (no mutex)"); @@ -6642,7 +6636,7 @@ void TestESMempool(void) ES_ResetUnitTest(); ((Pool_t *) HandlePtr)->UseMutex = CFE_ES_NO_MUTEX; UT_Report(__FILE__, __LINE__, - CFE_ES_GetPoolBuf((uint32 **) &address, HandlePtr, 256) > 0, + CFE_ES_GetPoolBuf(&address, HandlePtr, 256) > 0, "CFE_ES_GetPoolBuf", "Allocate pool buffer [4]; successful"); ((Pool_t *) HandlePtr)->UseMutex = CFE_ES_USE_MUTEX; @@ -6654,8 +6648,7 @@ void TestESMempool(void) BdPtr->CheckBits = 0x5a5a; BdPtr->Size =CFE_PLATFORM_ES_MAX_BLOCK_SIZE +1; UT_Report(__FILE__, __LINE__, - CFE_ES_PutPoolBuf(HandlePtr, - (uint32 *) address) == CFE_ES_ERR_MEM_HANDLE, + CFE_ES_PutPoolBuf(HandlePtr, address) == CFE_ES_ERR_MEM_HANDLE, "CFE_ES_PutPoolBuf", "Pool buffer size exceeds maximum"); @@ -6667,8 +6660,7 @@ void TestESMempool(void) BdPtr->CheckBits = 0x5a5a; BdPtr->Size =CFE_PLATFORM_ES_MAX_BLOCK_SIZE +1; UT_Report(__FILE__, __LINE__, - CFE_ES_PutPoolBuf(HandlePtr, - (uint32 *) address) == CFE_ES_ERR_MEM_HANDLE, + CFE_ES_PutPoolBuf(HandlePtr, address) == CFE_ES_ERR_MEM_HANDLE, "CFE_ES_PutPoolBuf", "Pool buffer size exceeds maximum (no mutex)"); ((Pool_t *) HandlePtr)->UseMutex = CFE_ES_USE_MUTEX; @@ -6678,7 +6670,7 @@ void TestESMempool(void) */ ES_ResetUnitTest(); UT_Report(__FILE__, __LINE__, - CFE_ES_GetPoolBuf((uint32 **) &address2, + CFE_ES_GetPoolBuf(&address2, HandlePtr, 99000) == CFE_ES_ERR_MEM_BLOCK_SIZE, "CFE_ES_GetPoolBuf", @@ -6694,15 +6686,14 @@ void TestESMempool(void) /* Test returning a pool buffer using a null handle */ ES_ResetUnitTest(); UT_Report(__FILE__, __LINE__, - CFE_ES_PutPoolBuf(0, - (uint32 *) address) == CFE_ES_ERR_MEM_HANDLE, + CFE_ES_PutPoolBuf(0, address) == CFE_ES_ERR_MEM_HANDLE, "CFE_ES_PutPoolBuf", "NULL memory handle"); /* Test allocating a pool buffer using a null handle */ ES_ResetUnitTest(); UT_Report(__FILE__, __LINE__, - CFE_ES_GetPoolBuf((uint32 **) &address, + CFE_ES_GetPoolBuf(&address, 0, 256) == CFE_ES_ERR_MEM_HANDLE, "CFE_ES_GetPoolBuf", @@ -6711,8 +6702,7 @@ void TestESMempool(void) /* Test getting the size of an existing pool buffer using a null handle */ ES_ResetUnitTest(); UT_Report(__FILE__, __LINE__, - CFE_ES_GetPoolBufInfo(0, - (uint32 *) address) == + CFE_ES_GetPoolBufInfo(0, address) == CFE_ES_ERR_MEM_HANDLE, "CFE_ES_GetPoolBufInfo", "NULL memory handle"); @@ -6737,7 +6727,7 @@ void TestESMempool(void) ES_ResetUnitTest(); ((Pool_t *) HandlePtr)->UseMutex = CFE_ES_NO_MUTEX; UT_Report(__FILE__, __LINE__, - CFE_ES_GetPoolBuf((uint32 **) &address2, + CFE_ES_GetPoolBuf(&address2, HandlePtr, 32) == CFE_ES_ERR_MEM_BLOCK_SIZE, "CFE_ES_GetPoolBuf", @@ -6762,8 +6752,7 @@ void TestESMempool(void) ((Pool_t *) HandlePtr)->UseMutex = CFE_ES_NO_MUTEX; for (i=0; i < 25; ++i) { - if (CFE_ES_GetPoolBuf((uint32 **) &address, - HandlePtr, + if (CFE_ES_GetPoolBuf(&address, HandlePtr, 12) == CFE_ES_ERR_MEM_BLOCK_SIZE) { break; diff --git a/fsw/cfe-core/ut-stubs/ut_es_stubs.c b/fsw/cfe-core/ut-stubs/ut_es_stubs.c index 9a77c3d5b..16ba57b4e 100644 --- a/fsw/cfe-core/ut-stubs/ut_es_stubs.c +++ b/fsw/cfe-core/ut-stubs/ut_es_stubs.c @@ -376,34 +376,29 @@ int32 CFE_ES_GetPoolBuf(uint32 **BufPtr, CFE_ES_PoolAlign_t Align; uint8 Bytes[CFE_UT_ES_POOL_STATIC_BLOCK_SIZE]; } Buffer; + uint32 PoolSize; - uint32 Position; - uint8 *PoolPtr; + uint32 PositionStart; + uint32 PositionEnd; + void *PoolPtr; + cpuaddr BufAddrStart; + cpuaddr BufAddrEnd; int32 status; + /* + * Determine the actual alignment of the CFE_ES_PoolAlign_t structure. + * This is done by checking the offset of a struct member of that type following a single byte. + */ + const cpuaddr AlignMask = ((cpuaddr)&((struct { char Byte; CFE_ES_PoolAlign_t Align; } *)0)->Align) - 1; + + status = UT_DEFAULT_IMPL_RC(CFE_ES_GetPoolBuf, Size); if (status > 0) { Size = status; - if (Size < sizeof(CFE_ES_PoolAlign_t)) - { - Size = sizeof(CFE_ES_PoolAlign_t) - 1; - } - else - { - --Size; - } - /* find next higher power of 2 */ - Size |= Size >> 1; - Size |= Size >> 2; - Size |= Size >> 4; - Size |= Size >> 8; - Size |= Size >> 16; - ++Size; - - UT_GetDataBuffer(UT_KEY(CFE_ES_GetPoolBuf), (void **)&PoolPtr, &PoolSize, &Position); + UT_GetDataBuffer(UT_KEY(CFE_ES_GetPoolBuf), (void **)&PoolPtr, &PoolSize, &PositionStart); if (PoolSize == 0) { /* @@ -412,20 +407,25 @@ int32 CFE_ES_GetPoolBuf(uint32 **BufPtr, */ PoolPtr = Buffer.Bytes; PoolSize = sizeof(Buffer); + PositionStart = 0; } - if ((Position + Size) < PoolSize) + BufAddrStart = (cpuaddr)PoolPtr + PositionStart; + BufAddrStart = (BufAddrStart + AlignMask) & ~AlignMask; + BufAddrEnd = (BufAddrStart + Size + AlignMask) & ~AlignMask; + PositionEnd = BufAddrEnd - (cpuaddr)PoolPtr; + + if (PositionEnd <= PoolSize) { - PoolPtr += Position; - *BufPtr = (uint32 *)PoolPtr; - status = Size; - memset(PoolPtr, 0x55, Size); + *BufPtr = (uint32 *)BufAddrStart; + memset((void*)BufAddrStart, 0x55, Size); /* * Unfortunately the UT assert stub library is missing * the ability to set the buffer position, the only way * to do it is by calling CopyFromLocal to advance the position. */ + Size = PositionEnd - PositionStart; while (Size > sizeof(Buffer)) { UT_Stub_CopyFromLocal(UT_KEY(CFE_ES_GetPoolBuf), &Buffer, @@ -443,8 +443,8 @@ int32 CFE_ES_GetPoolBuf(uint32 **BufPtr, * use UT_SetDataBuffer() to register a pool buffer that is * sufficient for the code under test. */ - UtAssert_Failed("Pool buffer empty in %s: need at least %lu bytes", - __func__, (unsigned long)(Position + Size)); + UtAssert_Failed("Pool buffer empty in %s: need at least %lu bytes, given %lu", + __func__, (unsigned long)PositionEnd, (unsigned long)PoolSize); status = -1; } }