117117#define heapALLOCATE_BLOCK ( pxBlock ) ( ( pxBlock->xBlockSize ) |= heapBLOCK_ALLOCATED_BITMASK )
118118#define heapFREE_BLOCK ( pxBlock ) ( ( pxBlock->xBlockSize ) &= ~heapBLOCK_ALLOCATED_BITMASK )
119119
120- /*-----------------------------------------------------------*/
121-
122- /* Define the linked list structure. This is used to link free blocks in order
123- * of their memory address. */
124- typedef struct A_BLOCK_LINK
125- {
126- struct A_BLOCK_LINK * pxNextFreeBlock ; /**< The next free block in the list. */
127- size_t xBlockSize ; /**< The size of the free block. */
128- } BlockLink_t ;
129-
130120/* Setting configENABLE_HEAP_PROTECTOR to 1 enables heap block pointers
131121 * protection using an application supplied canary value to catch heap
132122 * corruption should a heap buffer overflow occur.
133123 */
134124#if ( configENABLE_HEAP_PROTECTOR == 1 )
135125
136- /**
137- * @brief Application provided function to get a random value to be used as canary.
138- *
139- * @param pxHeapCanary [out] Output parameter to return the canary value.
140- */
141- extern void vApplicationGetRandomHeapCanary ( portPOINTER_SIZE_TYPE * pxHeapCanary );
142-
143- /* Canary value for protecting internal heap pointers. */
144- PRIVILEGED_DATA static portPOINTER_SIZE_TYPE xHeapCanary ;
145-
146-
147126/* Macro to load/store BlockLink_t pointers to memory. By XORing the
148127 * pointers with a random canary value, heap overflows will result
149128 * in randomly unpredictable pointer values which will be caught by
150129 * heapVALIDATE_BLOCK_POINTER assert. */
151130 #define heapPROTECT_BLOCK_POINTER ( pxBlock ) ( ( BlockLink_t * ) ( ( ( portPOINTER_SIZE_TYPE ) ( pxBlock ) ) ^ xHeapCanary ) )
152131
132+ /* Assert that a heap block pointer is within the heap bounds. */
133+ #define heapVALIDATE_BLOCK_POINTER ( pxBlock ) \
134+ configASSERT( ( pucHeapHighAddress != NULL ) && \
135+ ( pucHeapLowAddress != NULL ) && \
136+ ( ( uint8_t * ) ( pxBlock ) >= pucHeapLowAddress ) && \
137+ ( ( uint8_t * ) ( pxBlock ) < pucHeapHighAddress ) )
138+
153139#else /* if ( configENABLE_HEAP_PROTECTOR == 1 ) */
154140
155- #define heapPROTECT_BLOCK_POINTER ( pxBlock ) ( pxBlock )
141+ #define heapPROTECT_BLOCK_POINTER ( pxBlock ) ( pxBlock )
142+
143+ #define heapVALIDATE_BLOCK_POINTER ( pxBlock ) ( pxBlock )
156144
157145#endif /* configENABLE_HEAP_PROTECTOR */
158146
159- /* Highest and lowest heap addresses used for heap block bounds checking. */
160- PRIVILEGED_DATA static uint8_t * pucHeapHighAddress = NULL ;
161- PRIVILEGED_DATA static uint8_t * pucHeapLowAddress = NULL ;
147+ /*-----------------------------------------------------------*/
162148
163- /* Assert that a heap block pointer is within the heap bounds. */
164- #define heapVALIDATE_BLOCK_POINTER ( pxBlock ) \
165- configASSERT( ( pucHeapHighAddress != NULL ) && \
166- ( pucHeapLowAddress != NULL ) && \
167- ( ( uint8_t * ) ( pxBlock ) >= pucHeapLowAddress ) && \
168- ( ( uint8_t * ) ( pxBlock ) < pucHeapHighAddress ) )
149+ /* Define the linked list structure. This is used to link free blocks in order
150+ * of their memory address. */
151+ typedef struct A_BLOCK_LINK
152+ {
153+ struct A_BLOCK_LINK * pxNextFreeBlock ; /**< The next free block in the list. */
154+ size_t xBlockSize ; /**< The size of the free block. */
155+ } BlockLink_t ;
169156
170157/*-----------------------------------------------------------*/
171158
@@ -177,6 +164,17 @@ PRIVILEGED_DATA static uint8_t * pucHeapLowAddress = NULL;
177164 */
178165static void prvInsertBlockIntoFreeList ( BlockLink_t * pxBlockToInsert ) PRIVILEGED_FUNCTION ;
179166void vPortDefineHeapRegions ( const HeapRegion_t * const pxHeapRegions ) PRIVILEGED_FUNCTION ;
167+
168+ #if ( configENABLE_HEAP_PROTECTOR == 1 )
169+
170+ /**
171+ * @brief Application provided function to get a random value to be used as canary.
172+ *
173+ * @param pxHeapCanary [out] Output parameter to return the canary value.
174+ */
175+ extern void vApplicationGetRandomHeapCanary ( portPOINTER_SIZE_TYPE * pxHeapCanary );
176+ #endif /* configENABLE_HEAP_PROTECTOR */
177+
180178/*-----------------------------------------------------------*/
181179
182180/* The size of the structure placed at the beginning of each allocated memory
@@ -194,6 +192,17 @@ PRIVILEGED_DATA static size_t xMinimumEverFreeBytesRemaining = 0U;
194192PRIVILEGED_DATA static size_t xNumberOfSuccessfulAllocations = 0 ;
195193PRIVILEGED_DATA static size_t xNumberOfSuccessfulFrees = 0 ;
196194
195+ #if ( configENABLE_HEAP_PROTECTOR == 1 )
196+
197+ /* Canary value for protecting internal heap pointers. */
198+ PRIVILEGED_DATA static portPOINTER_SIZE_TYPE xHeapCanary ;
199+
200+ /* Highest and lowest heap addresses used for heap block bounds checking. */
201+ PRIVILEGED_DATA static uint8_t * pucHeapHighAddress = NULL ;
202+ PRIVILEGED_DATA static uint8_t * pucHeapLowAddress = NULL ;
203+
204+ #endif /* configENABLE_HEAP_PROTECTOR */
205+
197206/*-----------------------------------------------------------*/
198207
199208void * pvPortMalloc ( size_t xWantedSize )
0 commit comments