secure_heap.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. /* Standard includes. */
  29. #include <stdint.h>
  30. /* Secure context heap includes. */
  31. #include "secure_heap.h"
  32. /* Secure port macros. */
  33. #include "secure_port_macros.h"
  34. /**
  35. * @brief Total heap size.
  36. */
  37. #ifndef secureconfigTOTAL_HEAP_SIZE
  38. #define secureconfigTOTAL_HEAP_SIZE ( ( ( size_t ) ( 10 * 1024 ) ) )
  39. #endif
  40. /* No test marker by default. */
  41. #ifndef mtCOVERAGE_TEST_MARKER
  42. #define mtCOVERAGE_TEST_MARKER()
  43. #endif
  44. /* No tracing by default. */
  45. #ifndef traceMALLOC
  46. #define traceMALLOC( pvReturn, xWantedSize )
  47. #endif
  48. /* No tracing by default. */
  49. #ifndef traceFREE
  50. #define traceFREE( pv, xBlockSize )
  51. #endif
  52. /* Block sizes must not get too small. */
  53. #define secureheapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize << 1 ) )
  54. /* Assumes 8bit bytes! */
  55. #define secureheapBITS_PER_BYTE ( ( size_t ) 8 )
  56. /*-----------------------------------------------------------*/
  57. /* Allocate the memory for the heap. */
  58. #if ( configAPPLICATION_ALLOCATED_HEAP == 1 )
  59. /* The application writer has already defined the array used for the RTOS
  60. * heap - probably so it can be placed in a special segment or address. */
  61. extern uint8_t ucHeap[ secureconfigTOTAL_HEAP_SIZE ];
  62. #else /* configAPPLICATION_ALLOCATED_HEAP */
  63. static uint8_t ucHeap[ secureconfigTOTAL_HEAP_SIZE ];
  64. #endif /* configAPPLICATION_ALLOCATED_HEAP */
  65. /**
  66. * @brief The linked list structure.
  67. *
  68. * This is used to link free blocks in order of their memory address.
  69. */
  70. typedef struct A_BLOCK_LINK
  71. {
  72. struct A_BLOCK_LINK * pxNextFreeBlock; /**< The next free block in the list. */
  73. size_t xBlockSize; /**< The size of the free block. */
  74. } BlockLink_t;
  75. /*-----------------------------------------------------------*/
  76. /**
  77. * @brief Called automatically to setup the required heap structures the first
  78. * time pvPortMalloc() is called.
  79. */
  80. static void prvHeapInit( void );
  81. /**
  82. * @brief Inserts a block of memory that is being freed into the correct
  83. * position in the list of free memory blocks.
  84. *
  85. * The block being freed will be merged with the block in front it and/or the
  86. * block behind it if the memory blocks are adjacent to each other.
  87. *
  88. * @param[in] pxBlockToInsert The block being freed.
  89. */
  90. static void prvInsertBlockIntoFreeList( BlockLink_t * pxBlockToInsert );
  91. /*-----------------------------------------------------------*/
  92. /**
  93. * @brief The size of the structure placed at the beginning of each allocated
  94. * memory block must by correctly byte aligned.
  95. */
  96. static const size_t xHeapStructSize = ( sizeof( BlockLink_t ) + ( ( size_t ) ( secureportBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) secureportBYTE_ALIGNMENT_MASK );
  97. /**
  98. * @brief Create a couple of list links to mark the start and end of the list.
  99. */
  100. static BlockLink_t xStart, * pxEnd = NULL;
  101. /**
  102. * @brief Keeps track of the number of free bytes remaining, but says nothing
  103. * about fragmentation.
  104. */
  105. static size_t xFreeBytesRemaining = 0U;
  106. static size_t xMinimumEverFreeBytesRemaining = 0U;
  107. /**
  108. * @brief Gets set to the top bit of an size_t type.
  109. *
  110. * When this bit in the xBlockSize member of an BlockLink_t structure is set
  111. * then the block belongs to the application. When the bit is free the block is
  112. * still part of the free heap space.
  113. */
  114. static size_t xBlockAllocatedBit = 0;
  115. /*-----------------------------------------------------------*/
  116. static void prvHeapInit( void )
  117. {
  118. BlockLink_t * pxFirstFreeBlock;
  119. uint8_t * pucAlignedHeap;
  120. size_t uxAddress;
  121. size_t xTotalHeapSize = secureconfigTOTAL_HEAP_SIZE;
  122. /* Ensure the heap starts on a correctly aligned boundary. */
  123. uxAddress = ( size_t ) ucHeap;
  124. if( ( uxAddress & secureportBYTE_ALIGNMENT_MASK ) != 0 )
  125. {
  126. uxAddress += ( secureportBYTE_ALIGNMENT - 1 );
  127. uxAddress &= ~( ( size_t ) secureportBYTE_ALIGNMENT_MASK );
  128. xTotalHeapSize -= uxAddress - ( size_t ) ucHeap;
  129. }
  130. pucAlignedHeap = ( uint8_t * ) uxAddress;
  131. /* xStart is used to hold a pointer to the first item in the list of free
  132. * blocks. The void cast is used to prevent compiler warnings. */
  133. xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
  134. xStart.xBlockSize = ( size_t ) 0;
  135. /* pxEnd is used to mark the end of the list of free blocks and is inserted
  136. * at the end of the heap space. */
  137. uxAddress = ( ( size_t ) pucAlignedHeap ) + xTotalHeapSize;
  138. uxAddress -= xHeapStructSize;
  139. uxAddress &= ~( ( size_t ) secureportBYTE_ALIGNMENT_MASK );
  140. pxEnd = ( void * ) uxAddress;
  141. pxEnd->xBlockSize = 0;
  142. pxEnd->pxNextFreeBlock = NULL;
  143. /* To start with there is a single free block that is sized to take up the
  144. * entire heap space, minus the space taken by pxEnd. */
  145. pxFirstFreeBlock = ( void * ) pucAlignedHeap;
  146. pxFirstFreeBlock->xBlockSize = uxAddress - ( size_t ) pxFirstFreeBlock;
  147. pxFirstFreeBlock->pxNextFreeBlock = pxEnd;
  148. /* Only one block exists - and it covers the entire usable heap space. */
  149. xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
  150. xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;
  151. /* Work out the position of the top bit in a size_t variable. */
  152. xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * secureheapBITS_PER_BYTE ) - 1 );
  153. }
  154. /*-----------------------------------------------------------*/
  155. static void prvInsertBlockIntoFreeList( BlockLink_t * pxBlockToInsert )
  156. {
  157. BlockLink_t * pxIterator;
  158. uint8_t * puc;
  159. /* Iterate through the list until a block is found that has a higher address
  160. * than the block being inserted. */
  161. for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )
  162. {
  163. /* Nothing to do here, just iterate to the right position. */
  164. }
  165. /* Do the block being inserted, and the block it is being inserted after
  166. * make a contiguous block of memory? */
  167. puc = ( uint8_t * ) pxIterator;
  168. if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )
  169. {
  170. pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;
  171. pxBlockToInsert = pxIterator;
  172. }
  173. else
  174. {
  175. mtCOVERAGE_TEST_MARKER();
  176. }
  177. /* Do the block being inserted, and the block it is being inserted before
  178. * make a contiguous block of memory? */
  179. puc = ( uint8_t * ) pxBlockToInsert;
  180. if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )
  181. {
  182. if( pxIterator->pxNextFreeBlock != pxEnd )
  183. {
  184. /* Form one big block from the two blocks. */
  185. pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;
  186. pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;
  187. }
  188. else
  189. {
  190. pxBlockToInsert->pxNextFreeBlock = pxEnd;
  191. }
  192. }
  193. else
  194. {
  195. pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;
  196. }
  197. /* If the block being inserted plugged a gab, so was merged with the block
  198. * before and the block after, then it's pxNextFreeBlock pointer will have
  199. * already been set, and should not be set here as that would make it point
  200. * to itself. */
  201. if( pxIterator != pxBlockToInsert )
  202. {
  203. pxIterator->pxNextFreeBlock = pxBlockToInsert;
  204. }
  205. else
  206. {
  207. mtCOVERAGE_TEST_MARKER();
  208. }
  209. }
  210. /*-----------------------------------------------------------*/
  211. void * pvPortMalloc( size_t xWantedSize )
  212. {
  213. BlockLink_t * pxBlock, * pxPreviousBlock, * pxNewBlockLink;
  214. void * pvReturn = NULL;
  215. /* If this is the first call to malloc then the heap will require
  216. * initialisation to setup the list of free blocks. */
  217. if( pxEnd == NULL )
  218. {
  219. prvHeapInit();
  220. }
  221. else
  222. {
  223. mtCOVERAGE_TEST_MARKER();
  224. }
  225. /* Check the requested block size is not so large that the top bit is set.
  226. * The top bit of the block size member of the BlockLink_t structure is used
  227. * to determine who owns the block - the application or the kernel, so it
  228. * must be free. */
  229. if( ( xWantedSize & xBlockAllocatedBit ) == 0 )
  230. {
  231. /* The wanted size is increased so it can contain a BlockLink_t
  232. * structure in addition to the requested amount of bytes. */
  233. if( xWantedSize > 0 )
  234. {
  235. xWantedSize += xHeapStructSize;
  236. /* Ensure that blocks are always aligned to the required number of
  237. * bytes. */
  238. if( ( xWantedSize & secureportBYTE_ALIGNMENT_MASK ) != 0x00 )
  239. {
  240. /* Byte alignment required. */
  241. xWantedSize += ( secureportBYTE_ALIGNMENT - ( xWantedSize & secureportBYTE_ALIGNMENT_MASK ) );
  242. secureportASSERT( ( xWantedSize & secureportBYTE_ALIGNMENT_MASK ) == 0 );
  243. }
  244. else
  245. {
  246. mtCOVERAGE_TEST_MARKER();
  247. }
  248. }
  249. else
  250. {
  251. mtCOVERAGE_TEST_MARKER();
  252. }
  253. if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
  254. {
  255. /* Traverse the list from the start (lowest address) block until
  256. * one of adequate size is found. */
  257. pxPreviousBlock = &xStart;
  258. pxBlock = xStart.pxNextFreeBlock;
  259. while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
  260. {
  261. pxPreviousBlock = pxBlock;
  262. pxBlock = pxBlock->pxNextFreeBlock;
  263. }
  264. /* If the end marker was reached then a block of adequate size was
  265. * not found. */
  266. if( pxBlock != pxEnd )
  267. {
  268. /* Return the memory space pointed to - jumping over the
  269. * BlockLink_t structure at its start. */
  270. pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );
  271. /* This block is being returned for use so must be taken out
  272. * of the list of free blocks. */
  273. pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
  274. /* If the block is larger than required it can be split into
  275. * two. */
  276. if( ( pxBlock->xBlockSize - xWantedSize ) > secureheapMINIMUM_BLOCK_SIZE )
  277. {
  278. /* This block is to be split into two. Create a new
  279. * block following the number of bytes requested. The void
  280. * cast is used to prevent byte alignment warnings from the
  281. * compiler. */
  282. pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
  283. secureportASSERT( ( ( ( size_t ) pxNewBlockLink ) & secureportBYTE_ALIGNMENT_MASK ) == 0 );
  284. /* Calculate the sizes of two blocks split from the single
  285. * block. */
  286. pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
  287. pxBlock->xBlockSize = xWantedSize;
  288. /* Insert the new block into the list of free blocks. */
  289. prvInsertBlockIntoFreeList( pxNewBlockLink );
  290. }
  291. else
  292. {
  293. mtCOVERAGE_TEST_MARKER();
  294. }
  295. xFreeBytesRemaining -= pxBlock->xBlockSize;
  296. if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )
  297. {
  298. xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;
  299. }
  300. else
  301. {
  302. mtCOVERAGE_TEST_MARKER();
  303. }
  304. /* The block is being returned - it is allocated and owned by
  305. * the application and has no "next" block. */
  306. pxBlock->xBlockSize |= xBlockAllocatedBit;
  307. pxBlock->pxNextFreeBlock = NULL;
  308. }
  309. else
  310. {
  311. mtCOVERAGE_TEST_MARKER();
  312. }
  313. }
  314. else
  315. {
  316. mtCOVERAGE_TEST_MARKER();
  317. }
  318. }
  319. else
  320. {
  321. mtCOVERAGE_TEST_MARKER();
  322. }
  323. traceMALLOC( pvReturn, xWantedSize );
  324. #if ( secureconfigUSE_MALLOC_FAILED_HOOK == 1 )
  325. {
  326. if( pvReturn == NULL )
  327. {
  328. extern void vApplicationMallocFailedHook( void );
  329. vApplicationMallocFailedHook();
  330. }
  331. else
  332. {
  333. mtCOVERAGE_TEST_MARKER();
  334. }
  335. }
  336. #endif /* if ( secureconfigUSE_MALLOC_FAILED_HOOK == 1 ) */
  337. secureportASSERT( ( ( ( size_t ) pvReturn ) & ( size_t ) secureportBYTE_ALIGNMENT_MASK ) == 0 );
  338. return pvReturn;
  339. }
  340. /*-----------------------------------------------------------*/
  341. void vPortFree( void * pv )
  342. {
  343. uint8_t * puc = ( uint8_t * ) pv;
  344. BlockLink_t * pxLink;
  345. if( pv != NULL )
  346. {
  347. /* The memory being freed will have an BlockLink_t structure immediately
  348. * before it. */
  349. puc -= xHeapStructSize;
  350. /* This casting is to keep the compiler from issuing warnings. */
  351. pxLink = ( void * ) puc;
  352. /* Check the block is actually allocated. */
  353. secureportASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );
  354. secureportASSERT( pxLink->pxNextFreeBlock == NULL );
  355. if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )
  356. {
  357. if( pxLink->pxNextFreeBlock == NULL )
  358. {
  359. /* The block is being returned to the heap - it is no longer
  360. * allocated. */
  361. pxLink->xBlockSize &= ~xBlockAllocatedBit;
  362. secureportDISABLE_NON_SECURE_INTERRUPTS();
  363. {
  364. /* Add this block to the list of free blocks. */
  365. xFreeBytesRemaining += pxLink->xBlockSize;
  366. traceFREE( pv, pxLink->xBlockSize );
  367. prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
  368. }
  369. secureportENABLE_NON_SECURE_INTERRUPTS();
  370. }
  371. else
  372. {
  373. mtCOVERAGE_TEST_MARKER();
  374. }
  375. }
  376. else
  377. {
  378. mtCOVERAGE_TEST_MARKER();
  379. }
  380. }
  381. }
  382. /*-----------------------------------------------------------*/
  383. size_t xPortGetFreeHeapSize( void )
  384. {
  385. return xFreeBytesRemaining;
  386. }
  387. /*-----------------------------------------------------------*/
  388. size_t xPortGetMinimumEverFreeHeapSize( void )
  389. {
  390. return xMinimumEverFreeBytesRemaining;
  391. }
  392. /*-----------------------------------------------------------*/