mirror of
https://github.com/FreeRTOS/FreeRTOS-Kernel.git
synced 2025-04-19 21:11:57 -04:00
Improve heap2 bounds checking (#224)
* Improve heap bounds checking in pvPortMalloc
This commit is contained in:
parent
b5020cb3d8
commit
c7a9a01c94
|
@ -22,7 +22,6 @@
|
||||||
* https://www.FreeRTOS.org
|
* https://www.FreeRTOS.org
|
||||||
* https://github.com/FreeRTOS
|
* https://github.com/FreeRTOS
|
||||||
*
|
*
|
||||||
* 1 tab == 4 spaces!
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
@ -72,13 +71,20 @@ void * pvPortMalloc( size_t xWantedSize )
|
||||||
void * pvReturn = NULL;
|
void * pvReturn = NULL;
|
||||||
static uint8_t * pucAlignedHeap = NULL;
|
static uint8_t * pucAlignedHeap = NULL;
|
||||||
|
|
||||||
/* Ensure that blocks are always aligned to the required number of bytes. */
|
/* Ensure that blocks are always aligned. */
|
||||||
#if ( portBYTE_ALIGNMENT != 1 )
|
#if ( portBYTE_ALIGNMENT != 1 )
|
||||||
{
|
{
|
||||||
if( xWantedSize & portBYTE_ALIGNMENT_MASK )
|
if( xWantedSize & portBYTE_ALIGNMENT_MASK )
|
||||||
{
|
{
|
||||||
/* Byte alignment required. */
|
/* 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -91,8 +97,9 @@ void * pvPortMalloc( size_t xWantedSize )
|
||||||
pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) & ucHeap[ portBYTE_ALIGNMENT ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
|
pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) & ucHeap[ portBYTE_ALIGNMENT ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check there is enough room left for the allocation. */
|
/* Check there is enough room left for the allocation and. */
|
||||||
if( ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) &&
|
if( ( xWantedSize > 0 ) && /* valid size */
|
||||||
|
( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) &&
|
||||||
( ( xNextFreeByte + xWantedSize ) > xNextFreeByte ) ) /* Check for overflow. */
|
( ( xNextFreeByte + xWantedSize ) > xNextFreeByte ) ) /* Check for overflow. */
|
||||||
{
|
{
|
||||||
/* Return the next free byte then increment the index past this
|
/* Return the next free byte then increment the index past this
|
||||||
|
|
|
@ -22,7 +22,6 @@
|
||||||
* https://www.FreeRTOS.org
|
* https://www.FreeRTOS.org
|
||||||
* https://github.com/FreeRTOS
|
* https://github.com/FreeRTOS
|
||||||
*
|
*
|
||||||
* 1 tab == 4 spaces!
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -132,21 +131,32 @@ void * pvPortMalloc( size_t xWantedSize )
|
||||||
xHeapHasBeenInitialised = pdTRUE;
|
xHeapHasBeenInitialised = pdTRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The wanted size is increased so it can contain a BlockLink_t
|
/* The wanted size must be increased so it can contain a BlockLink_t
|
||||||
* structure in addition to the requested amount of bytes. */
|
* structure in addition to the requested amount of bytes. */
|
||||||
if( xWantedSize > 0 )
|
if( ( xWantedSize > 0 ) &&
|
||||||
|
( ( xWantedSize + heapSTRUCT_SIZE ) > xWantedSize ) ) /* Overflow check */
|
||||||
{
|
{
|
||||||
xWantedSize += heapSTRUCT_SIZE;
|
xWantedSize += heapSTRUCT_SIZE;
|
||||||
|
|
||||||
/* Ensure that blocks are always aligned to the required number of bytes. */
|
/* Byte alignment required. Check for overflow. */
|
||||||
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0 )
|
if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) )
|
||||||
|
> xWantedSize )
|
||||||
{
|
{
|
||||||
/* Byte alignment required. */
|
|
||||||
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
|
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
|
||||||
|
configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
xWantedSize = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
xWantedSize = 0;
|
||||||
|
}
|
||||||
|
|
||||||
if( ( xWantedSize > 0 ) && ( xWantedSize < configADJUSTED_HEAP_SIZE ) )
|
|
||||||
|
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
|
||||||
* (smallest) block until one of adequate size is found. */
|
* (smallest) block until one of adequate size is found. */
|
||||||
|
|
|
@ -136,19 +136,27 @@ void * pvPortMalloc( size_t xWantedSize )
|
||||||
* kernel, so it must be free. */
|
* kernel, so it must be free. */
|
||||||
if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
|
if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
|
||||||
{
|
{
|
||||||
/* The wanted size is increased so it can contain a BlockLink_t
|
/* The wanted size must be increased so it can contain a BlockLink_t
|
||||||
* structure in addition to the requested amount of bytes. */
|
* structure in addition to the requested amount of bytes. */
|
||||||
if( xWantedSize > 0 )
|
if( ( xWantedSize > 0 ) &&
|
||||||
|
( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) /* Overflow check */
|
||||||
{
|
{
|
||||||
xWantedSize += xHeapStructSize;
|
xWantedSize += xHeapStructSize;
|
||||||
|
|
||||||
/* Ensure that blocks are always aligned to the required number
|
/* Ensure that blocks are always aligned. */
|
||||||
* of bytes. */
|
|
||||||
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
|
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
|
||||||
{
|
{
|
||||||
/* Byte alignment required. */
|
/* 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
|
||||||
{
|
{
|
||||||
|
@ -157,13 +165,13 @@ void * pvPortMalloc( size_t xWantedSize )
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mtCOVERAGE_TEST_MARKER();
|
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
|
||||||
* one of adequate size is found. */
|
* one of adequate size is found. */
|
||||||
pxPreviousBlock = &xStart;
|
pxPreviousBlock = &xStart;
|
||||||
pxBlock = xStart.pxNextFreeBlock;
|
pxBlock = xStart.pxNextFreeBlock;
|
||||||
|
|
||||||
|
@ -174,7 +182,7 @@ void * pvPortMalloc( size_t xWantedSize )
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If the end marker was reached then a block of adequate size
|
/* If the end marker was reached then a block of adequate size
|
||||||
* was not found. */
|
* was not found. */
|
||||||
if( pxBlock != pxEnd )
|
if( pxBlock != pxEnd )
|
||||||
{
|
{
|
||||||
/* Return the memory space pointed to - jumping over the
|
/* Return the memory space pointed to - jumping over the
|
||||||
|
|
|
@ -22,7 +22,6 @@
|
||||||
* https://www.FreeRTOS.org
|
* https://www.FreeRTOS.org
|
||||||
* https://github.com/FreeRTOS
|
* https://github.com/FreeRTOS
|
||||||
*
|
*
|
||||||
* 1 tab == 4 spaces!
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -150,16 +149,24 @@ void * pvPortMalloc( size_t xWantedSize )
|
||||||
{
|
{
|
||||||
/* The wanted size is increased so it can contain a BlockLink_t
|
/* The wanted size is increased so it can contain a BlockLink_t
|
||||||
* structure in addition to the requested amount of bytes. */
|
* structure in addition to the requested amount of bytes. */
|
||||||
if( xWantedSize > 0 )
|
if( ( xWantedSize > 0 ) &&
|
||||||
|
( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) /* Overflow check */
|
||||||
{
|
{
|
||||||
xWantedSize += xHeapStructSize;
|
xWantedSize += xHeapStructSize;
|
||||||
|
|
||||||
/* Ensure that blocks are always aligned to the required number
|
/* Ensure that blocks are always aligned */
|
||||||
* of bytes. */
|
|
||||||
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
|
if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
|
||||||
{
|
{
|
||||||
/* Byte alignment required. */
|
/* 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
|
||||||
{
|
{
|
||||||
|
@ -168,13 +175,13 @@ void * pvPortMalloc( size_t xWantedSize )
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mtCOVERAGE_TEST_MARKER();
|
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
|
||||||
* one of adequate size is found. */
|
* one of adequate size is found. */
|
||||||
pxPreviousBlock = &xStart;
|
pxPreviousBlock = &xStart;
|
||||||
pxBlock = xStart.pxNextFreeBlock;
|
pxBlock = xStart.pxNextFreeBlock;
|
||||||
|
|
||||||
|
@ -185,7 +192,7 @@ void * pvPortMalloc( size_t xWantedSize )
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If the end marker was reached then a block of adequate size
|
/* If the end marker was reached then a block of adequate size
|
||||||
* was not found. */
|
* was not found. */
|
||||||
if( pxBlock != pxEnd )
|
if( pxBlock != pxEnd )
|
||||||
{
|
{
|
||||||
/* Return the memory space pointed to - jumping over the
|
/* Return the memory space pointed to - jumping over the
|
||||||
|
|
Loading…
Reference in a new issue