Move MSB check after final size calculation (#463)

We use the MSB of the size member of a BlockLink_t to track whether not
a block is allocated. Consequently, the size must not be so large that
the MSB is set. The check to see if the MSB in the size is set needs to
be done after the final size (metadata + alignment) is calculated.

Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>
This commit is contained in:
Gaurav-Aggarwal-AWS 2022-02-28 11:59:00 -08:00 committed by GitHub
parent 82be77995e
commit 8eb3585252
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 79 deletions

View file

@ -160,36 +160,36 @@ void * pvPortMalloc( size_t xWantedSize )
xHeapHasBeenInitialised = pdTRUE; xHeapHasBeenInitialised = pdTRUE;
} }
/* Check the requested block size is not so large that the top bit is /* The wanted size must be increased so it can contain a BlockLink_t
* set. The top bit of the block size member of the BlockLink_t structure * structure in addition to the requested amount of bytes. */
* is used to determine who owns the block - the application or the if( ( xWantedSize > 0 ) &&
* kernel, so it must be free. */ ( ( xWantedSize + heapSTRUCT_SIZE ) > xWantedSize ) ) /* Overflow check. */
if( heapBLOCK_SIZE_IS_VALID( xWantedSize ) )
{ {
/* The wanted size must be increased so it can contain a BlockLink_t xWantedSize += heapSTRUCT_SIZE;
* structure in addition to the requested amount of bytes. */
if( ( xWantedSize > 0 ) &&
( ( xWantedSize + heapSTRUCT_SIZE ) > xWantedSize ) ) /* Overflow check */
{
xWantedSize += heapSTRUCT_SIZE;
/* Byte alignment required. Check for overflow. */ /* Byte alignment required. Check for overflow. */
if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) ) if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) )
> xWantedSize ) > xWantedSize )
{ {
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ); xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 ); configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
}
else
{
xWantedSize = 0;
}
} }
else else
{ {
xWantedSize = 0; xWantedSize = 0;
} }
}
else
{
xWantedSize = 0;
}
/* Check the block size we are trying to allocate is not so large that the
* top bit is set. The top bit of the block size member of the BlockLink_t
* structure is used to determine who owns the block - the application or
* the kernel, so it must be free. */
if( heapBLOCK_SIZE_IS_VALID( xWantedSize ) != 0 )
{
if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) ) if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
{ {
/* Blocks are stored in byte order - traverse the list from the start /* Blocks are stored in byte order - traverse the list from the start
@ -274,10 +274,10 @@ void vPortFree( void * pv )
* byte alignment warnings. */ * byte alignment warnings. */
pxLink = ( void * ) puc; pxLink = ( void * ) puc;
configASSERT( heapBLOCK_IS_ALLOCATED( pxLink ) ); configASSERT( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 );
configASSERT( pxLink->pxNextFreeBlock == NULL ); configASSERT( pxLink->pxNextFreeBlock == NULL );
if( heapBLOCK_IS_ALLOCATED( pxLink ) ) if( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 )
{ {
if( pxLink->pxNextFreeBlock == NULL ) if( pxLink->pxNextFreeBlock == NULL )
{ {

View file

@ -146,44 +146,43 @@ void * pvPortMalloc( size_t xWantedSize )
mtCOVERAGE_TEST_MARKER(); mtCOVERAGE_TEST_MARKER();
} }
/* Check the requested block size is not so large that the top bit is /* The wanted size must be increased so it can contain a BlockLink_t
* set. The top bit of the block size member of the BlockLink_t structure * structure in addition to the requested amount of bytes. */
* is used to determine who owns the block - the application or the if( ( xWantedSize > 0 ) &&
* kernel, so it must be free. */ ( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) /* Overflow check. */
if( heapBLOCK_SIZE_IS_VALID( xWantedSize ) )
{ {
/* The wanted size must be increased so it can contain a BlockLink_t xWantedSize += xHeapStructSize;
* structure in addition to the requested amount of bytes. */
if( ( xWantedSize > 0 ) &&
( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) /* Overflow check */
{
xWantedSize += xHeapStructSize;
/* Ensure that blocks are always aligned. */ /* Ensure that blocks are always aligned. */
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 ) if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
{
/* Byte alignment required. Check for overflow. */
if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) ) > xWantedSize )
{ {
/* Byte alignment required. Check for overflow. */ xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) ) configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
> xWantedSize )
{
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
}
else
{
xWantedSize = 0;
}
} }
else else
{ {
mtCOVERAGE_TEST_MARKER(); xWantedSize = 0;
} }
} }
else else
{ {
xWantedSize = 0; mtCOVERAGE_TEST_MARKER();
} }
}
else
{
xWantedSize = 0;
}
/* Check the block size we are trying to allocate is not so large that the
* top bit is set. The top bit of the block size member of the BlockLink_t
* structure is used to determine who owns the block - the application or
* the kernel, so it must be free. */
if( heapBLOCK_SIZE_IS_VALID( xWantedSize ) != 0 )
{
if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) ) if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
{ {
/* Traverse the list from the start (lowest address) block until /* Traverse the list from the start (lowest address) block until
@ -302,10 +301,10 @@ void vPortFree( void * pv )
/* This casting is to keep the compiler from issuing warnings. */ /* This casting is to keep the compiler from issuing warnings. */
pxLink = ( void * ) puc; pxLink = ( void * ) puc;
configASSERT( heapBLOCK_IS_ALLOCATED( pxLink ) ); configASSERT( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 );
configASSERT( pxLink->pxNextFreeBlock == NULL ); configASSERT( pxLink->pxNextFreeBlock == NULL );
if( heapBLOCK_IS_ALLOCATED( pxLink ) ) if( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 )
{ {
if( pxLink->pxNextFreeBlock == NULL ) if( pxLink->pxNextFreeBlock == NULL )
{ {

View file

@ -157,43 +157,43 @@ void * pvPortMalloc( size_t xWantedSize )
vTaskSuspendAll(); vTaskSuspendAll();
{ {
/* Check the requested block size is not so large that the top bit is /* The wanted size is increased so it can contain a BlockLink_t
* set. The top bit of the block size member of the BlockLink_t structure * structure in addition to the requested amount of bytes. */
* is used to determine who owns the block - the application or the if( ( xWantedSize > 0 ) &&
* kernel, so it must be free. */ ( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) /* Overflow check. */
if( heapBLOCK_SIZE_IS_VALID( xWantedSize ) )
{ {
/* The wanted size is increased so it can contain a BlockLink_t xWantedSize += xHeapStructSize;
* structure in addition to the requested amount of bytes. */
if( ( xWantedSize > 0 ) &&
( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) /* Overflow check */
{
xWantedSize += xHeapStructSize;
/* Ensure that blocks are always aligned */ /* Ensure that blocks are always aligned */
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 ) if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
{
/* Byte alignment required. Check for overflow */
if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) ) >
xWantedSize )
{ {
/* Byte alignment required. Check for overflow */ xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) ) >
xWantedSize )
{
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
}
else
{
xWantedSize = 0;
}
} }
else else
{ {
mtCOVERAGE_TEST_MARKER(); xWantedSize = 0;
} }
} }
else else
{ {
xWantedSize = 0; mtCOVERAGE_TEST_MARKER();
} }
}
else
{
xWantedSize = 0;
}
/* Check the block size we are trying to allocate is not so large that the
* top bit is set. The top bit of the block size member of the BlockLink_t
* structure is used to determine who owns the block - the application or
* the kernel, so it must be free. */
if( heapBLOCK_SIZE_IS_VALID( xWantedSize ) != 0 )
{
if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) ) if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
{ {
/* Traverse the list from the start (lowest address) block until /* Traverse the list from the start (lowest address) block until
@ -310,10 +310,10 @@ void vPortFree( void * pv )
/* This casting is to keep the compiler from issuing warnings. */ /* This casting is to keep the compiler from issuing warnings. */
pxLink = ( void * ) puc; pxLink = ( void * ) puc;
configASSERT( heapBLOCK_IS_ALLOCATED( pxLink ) ); configASSERT( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 );
configASSERT( pxLink->pxNextFreeBlock == NULL ); configASSERT( pxLink->pxNextFreeBlock == NULL );
if( heapBLOCK_IS_ALLOCATED( pxLink ) ) if( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 )
{ {
if( pxLink->pxNextFreeBlock == NULL ) if( pxLink->pxNextFreeBlock == NULL )
{ {