test_multi_heap.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. #include "catch.hpp"
  2. #include "multi_heap.h"
  3. #include "../multi_heap_config.h"
  4. #include "../tlsf/tlsf_common.h"
  5. #include "../tlsf/tlsf_block_functions.h"
  6. #include <string.h>
  7. #include <assert.h>
  8. /* Insurance against accidentally using libc heap functions in tests */
  9. #undef free
  10. #define free #error
  11. #undef malloc
  12. #define malloc #error
  13. #undef calloc
  14. #define calloc #error
  15. #undef realloc
  16. #define realloc #error
  17. TEST_CASE("multi_heap simple allocations", "[multi_heap]")
  18. {
  19. uint8_t small_heap[4 * 1024];
  20. multi_heap_handle_t heap = multi_heap_register(small_heap, sizeof(small_heap));
  21. size_t test_alloc_size = (multi_heap_free_size(heap) + 4) / 2;
  22. printf("New heap:\n");
  23. multi_heap_dump(heap);
  24. printf("*********************\n");
  25. uint8_t *buf = (uint8_t *)multi_heap_malloc(heap, test_alloc_size);
  26. printf("small_heap %p buf %p\n", small_heap, buf);
  27. REQUIRE( buf != NULL );
  28. REQUIRE((intptr_t)buf >= (intptr_t)small_heap);
  29. REQUIRE( (intptr_t)buf < (intptr_t)(small_heap + sizeof(small_heap)));
  30. REQUIRE( multi_heap_get_allocated_size(heap, buf) >= test_alloc_size );
  31. REQUIRE( multi_heap_get_allocated_size(heap, buf) < test_alloc_size + 16);
  32. memset(buf, 0xEE, test_alloc_size);
  33. REQUIRE( multi_heap_malloc(heap, test_alloc_size) == NULL );
  34. multi_heap_free(heap, buf);
  35. printf("Empty?\n");
  36. multi_heap_dump(heap);
  37. printf("*********************\n");
  38. /* Now there should be space for another allocation */
  39. buf = (uint8_t *)multi_heap_malloc(heap, test_alloc_size);
  40. REQUIRE( buf != NULL );
  41. multi_heap_free(heap, buf);
  42. REQUIRE( multi_heap_free_size(heap) > multi_heap_minimum_free_size(heap) );
  43. }
  44. TEST_CASE("multi_heap fragmentation", "[multi_heap]")
  45. {
  46. uint8_t small_heap[4 * 1024];
  47. multi_heap_handle_t heap = multi_heap_register(small_heap, sizeof(small_heap));
  48. const size_t alloc_size = 128;
  49. void *p[4];
  50. for (int i = 0; i < 4; i++) {
  51. multi_heap_dump(heap);
  52. REQUIRE( multi_heap_check(heap, true) );
  53. p[i] = multi_heap_malloc(heap, alloc_size);
  54. printf("%d = %p ****->\n", i, p[i]);
  55. multi_heap_dump(heap);
  56. REQUIRE( p[i] != NULL );
  57. }
  58. printf("allocated %p %p %p %p\n", p[0], p[1], p[2], p[3]);
  59. REQUIRE( multi_heap_malloc(heap, alloc_size * 5) == NULL ); /* no room to allocate 5*alloc_size now */
  60. printf("4 allocations:\n");
  61. multi_heap_dump(heap);
  62. printf("****************\n");
  63. multi_heap_free(heap, p[0]);
  64. multi_heap_free(heap, p[1]);
  65. multi_heap_free(heap, p[3]);
  66. printf("1 allocations:\n");
  67. multi_heap_dump(heap);
  68. printf("****************\n");
  69. void *big = multi_heap_malloc(heap, alloc_size * 3);
  70. //Blocks in TLSF are organized in different form, so this makes no sense
  71. multi_heap_free(heap, big);
  72. multi_heap_free(heap, p[2]);
  73. printf("0 allocations:\n");
  74. multi_heap_dump(heap);
  75. printf("****************\n");
  76. big = multi_heap_malloc(heap, alloc_size * 2);
  77. //Blocks in TLSF are organized in different form, so this makes no sense
  78. multi_heap_free(heap, big);
  79. }
  80. /* Test that malloc/free does not leave free space fragmented */
  81. TEST_CASE("multi_heap defrag", "[multi_heap]")
  82. {
  83. void *p[4];
  84. uint8_t small_heap[4 * 1024];
  85. multi_heap_info_t info, info2;
  86. multi_heap_handle_t heap = multi_heap_register(small_heap, sizeof(small_heap));
  87. printf("0 ---\n");
  88. multi_heap_dump(heap);
  89. REQUIRE( multi_heap_check(heap, true) );
  90. multi_heap_get_info(heap, &info);
  91. REQUIRE( 0 == info.allocated_blocks );
  92. REQUIRE( 1 == info.free_blocks );
  93. printf("1 ---\n");
  94. p[0] = multi_heap_malloc(heap, 128);
  95. p[1] = multi_heap_malloc(heap, 32);
  96. multi_heap_dump(heap);
  97. REQUIRE( multi_heap_check(heap, true) );
  98. printf("2 ---\n");
  99. multi_heap_free(heap, p[0]);
  100. p[2] = multi_heap_malloc(heap, 64);
  101. multi_heap_dump(heap);
  102. REQUIRE( p[2] == p[0] );
  103. REQUIRE( multi_heap_check(heap, true) );
  104. printf("3 ---\n");
  105. multi_heap_free(heap, p[2]);
  106. p[3] = multi_heap_malloc(heap, 32);
  107. multi_heap_dump(heap);
  108. REQUIRE( p[3] == p[0] );
  109. REQUIRE( multi_heap_check(heap, true) );
  110. multi_heap_get_info(heap, &info2);
  111. REQUIRE( 2 == info2.allocated_blocks );
  112. REQUIRE( 2 == info2.free_blocks );
  113. multi_heap_free(heap, p[0]);
  114. multi_heap_free(heap, p[1]);
  115. multi_heap_get_info(heap, &info2);
  116. REQUIRE( 0 == info2.allocated_blocks );
  117. REQUIRE( 1 == info2.free_blocks );
  118. REQUIRE( info.total_free_bytes == info2.total_free_bytes );
  119. }
  120. /* Test that malloc/free does not leave free space fragmented
  121. Note: With fancy poisoning, realloc is implemented as malloc-copy-free and this test does not apply.
  122. */
  123. #ifndef MULTI_HEAP_POISONING_SLOW
  124. TEST_CASE("multi_heap defrag realloc", "[multi_heap]")
  125. {
  126. void *p[4];
  127. uint8_t small_heap[4 * 1024];
  128. multi_heap_info_t info, info2;
  129. multi_heap_handle_t heap = multi_heap_register(small_heap, sizeof(small_heap));
  130. printf("0 ---\n");
  131. multi_heap_dump(heap);
  132. REQUIRE( multi_heap_check(heap, true) );
  133. multi_heap_get_info(heap, &info);
  134. REQUIRE( 0 == info.allocated_blocks );
  135. REQUIRE( 1 == info.free_blocks );
  136. printf("1 ---\n");
  137. p[0] = multi_heap_malloc(heap, 128);
  138. p[1] = multi_heap_malloc(heap, 32);
  139. multi_heap_dump(heap);
  140. REQUIRE( multi_heap_check(heap, true) );
  141. printf("2 ---\n");
  142. p[2] = multi_heap_realloc(heap, p[0], 64);
  143. multi_heap_dump(heap);
  144. REQUIRE( p[2] == p[0] );
  145. REQUIRE( multi_heap_check(heap, true) );
  146. printf("3 ---\n");
  147. p[3] = multi_heap_realloc(heap, p[2], 32);
  148. multi_heap_dump(heap);
  149. REQUIRE( p[3] == p[0] );
  150. REQUIRE( multi_heap_check(heap, true) );
  151. multi_heap_get_info(heap, &info2);
  152. REQUIRE( 2 == info2.allocated_blocks );
  153. REQUIRE( 2 == info2.free_blocks );
  154. multi_heap_free(heap, p[0]);
  155. multi_heap_free(heap, p[1]);
  156. multi_heap_get_info(heap, &info2);
  157. REQUIRE( 0 == info2.allocated_blocks );
  158. REQUIRE( 1 == info2.free_blocks );
  159. REQUIRE( info.total_free_bytes == info2.total_free_bytes );
  160. }
  161. #endif
  162. TEST_CASE("multi_heap many random allocations", "[multi_heap]")
  163. {
  164. uint8_t big_heap[8 * 1024];
  165. const int NUM_POINTERS = 64;
  166. printf("Running multi-allocation test...\n");
  167. void *p[NUM_POINTERS] = { 0 };
  168. size_t s[NUM_POINTERS] = { 0 };
  169. multi_heap_handle_t heap = multi_heap_register(big_heap, sizeof(big_heap));
  170. const size_t initial_free = multi_heap_free_size(heap);
  171. const int ITERATIONS = 10000;
  172. for (int i = 0; i < ITERATIONS; i++) {
  173. /* check all pointers allocated so far are valid inside big_heap */
  174. for (int j = 0; j < NUM_POINTERS; j++) {
  175. if (p[j] != NULL) {
  176. }
  177. }
  178. uint8_t n = rand() % NUM_POINTERS;
  179. if (rand() % 4 == 0) {
  180. /* 1 in 4 iterations, try to realloc the buffer instead
  181. of using malloc/free
  182. */
  183. size_t new_size = rand() % 1024;
  184. void *new_p = multi_heap_realloc(heap, p[n], new_size);
  185. printf("realloc %p -> %p (%zu -> %zu)\n", p[n], new_p, s[n], new_size);
  186. multi_heap_check(heap, true);
  187. if (new_size == 0 || new_p != NULL) {
  188. p[n] = new_p;
  189. s[n] = new_size;
  190. if (new_size > 0) {
  191. REQUIRE( p[n] >= big_heap );
  192. REQUIRE( p[n] < big_heap + sizeof(big_heap) );
  193. memset(p[n], n, new_size);
  194. }
  195. }
  196. continue;
  197. }
  198. if (p[n] != NULL) {
  199. if (s[n] > 0) {
  200. /* Verify pre-existing contents of p[n] */
  201. uint8_t compare[s[n]];
  202. memset(compare, n, s[n]);
  203. /*REQUIRE*/assert( memcmp(compare, p[n], s[n]) == 0 );
  204. }
  205. REQUIRE( multi_heap_check(heap, true) );
  206. multi_heap_free(heap, p[n]);
  207. printf("freed %p (%zu)\n", p[n], s[n]);
  208. if (!multi_heap_check(heap, true)) {
  209. printf("FAILED iteration %d after freeing %p\n", i, p[n]);
  210. multi_heap_dump(heap);
  211. REQUIRE(0);
  212. }
  213. }
  214. s[n] = rand() % 1024;
  215. REQUIRE( multi_heap_check(heap, true) );
  216. p[n] = multi_heap_malloc(heap, s[n]);
  217. printf("malloc %p (%zu)\n", p[n], s[n]);
  218. if (p[n] != NULL) {
  219. REQUIRE( p[n] >= big_heap );
  220. REQUIRE( p[n] < big_heap + sizeof(big_heap) );
  221. }
  222. if (!multi_heap_check(heap, true)) {
  223. printf("FAILED iteration %d after mallocing %p (%zu bytes)\n", i, p[n], s[n]);
  224. multi_heap_dump(heap);
  225. REQUIRE(0);
  226. }
  227. if (p[n] != NULL) {
  228. memset(p[n], n, s[n]);
  229. }
  230. }
  231. for (int i = 0; i < NUM_POINTERS; i++) {
  232. multi_heap_free(heap, p[i]);
  233. if (!multi_heap_check(heap, true)) {
  234. printf("FAILED during cleanup after freeing %p\n", p[i]);
  235. multi_heap_dump(heap);
  236. REQUIRE(0);
  237. }
  238. }
  239. REQUIRE( initial_free == multi_heap_free_size(heap) );
  240. }
  241. TEST_CASE("multi_heap_get_info() function", "[multi_heap]")
  242. {
  243. uint8_t heapdata[4 * 1024];
  244. multi_heap_handle_t heap = multi_heap_register(heapdata, sizeof(heapdata));
  245. multi_heap_info_t before, after, freed;
  246. multi_heap_get_info(heap, &before);
  247. printf("before: total_free_bytes %zu\ntotal_allocated_bytes %zu\nlargest_free_block %zu\nminimum_free_bytes %zu\nallocated_blocks %zu\nfree_blocks %zu\ntotal_blocks %zu\n",
  248. before.total_free_bytes,
  249. before.total_allocated_bytes,
  250. before.largest_free_block,
  251. before.minimum_free_bytes,
  252. before.allocated_blocks,
  253. before.free_blocks,
  254. before.total_blocks);
  255. REQUIRE( 0 == before.allocated_blocks );
  256. REQUIRE( 0 == before.total_allocated_bytes );
  257. REQUIRE( before.total_free_bytes == before.minimum_free_bytes );
  258. void *x = multi_heap_malloc(heap, 32);
  259. multi_heap_get_info(heap, &after);
  260. printf("after: total_free_bytes %zu\ntotal_allocated_bytes %zu\nlargest_free_block %zu\nminimum_free_bytes %zu\nallocated_blocks %zu\nfree_blocks %zu\ntotal_blocks %zu\n",
  261. after.total_free_bytes,
  262. after.total_allocated_bytes,
  263. after.largest_free_block,
  264. after.minimum_free_bytes,
  265. after.allocated_blocks,
  266. after.free_blocks,
  267. after.total_blocks);
  268. REQUIRE( 1 == after.allocated_blocks );
  269. REQUIRE( 32 == after.total_allocated_bytes );
  270. REQUIRE( after.minimum_free_bytes < before.minimum_free_bytes);
  271. REQUIRE( after.minimum_free_bytes > 0 );
  272. multi_heap_free(heap, x);
  273. multi_heap_get_info(heap, &freed);
  274. printf("freed: total_free_bytes %zu\ntotal_allocated_bytes %zu\nlargest_free_block %zu\nminimum_free_bytes %zu\nallocated_blocks %zu\nfree_blocks %zu\ntotal_blocks %zu\n",
  275. freed.total_free_bytes,
  276. freed.total_allocated_bytes,
  277. freed.largest_free_block,
  278. freed.minimum_free_bytes,
  279. freed.allocated_blocks,
  280. freed.free_blocks,
  281. freed.total_blocks);
  282. REQUIRE( 0 == freed.allocated_blocks );
  283. REQUIRE( 0 == freed.total_allocated_bytes );
  284. REQUIRE( before.total_free_bytes == freed.total_free_bytes );
  285. REQUIRE( after.minimum_free_bytes == freed.minimum_free_bytes );
  286. }
  287. TEST_CASE("multi_heap minimum-size allocations", "[multi_heap]")
  288. {
  289. uint8_t heapdata[4096];
  290. void *p[sizeof(heapdata) / sizeof(void *)] = {NULL};
  291. const size_t NUM_P = sizeof(p) / sizeof(void *);
  292. size_t allocated_size = 0;
  293. multi_heap_handle_t heap = multi_heap_register(heapdata, sizeof(heapdata));
  294. size_t before_free = multi_heap_free_size(heap);
  295. size_t i;
  296. for (i = 0; i < NUM_P; i++) {
  297. //TLSF minimum block size is 4 bytes
  298. p[i] = multi_heap_malloc(heap, 1);
  299. if (p[i] == NULL) {
  300. break;
  301. }
  302. }
  303. REQUIRE( i < NUM_P); // Should have run out of heap before we ran out of pointers
  304. printf("Allocated %zu minimum size chunks\n", i);
  305. REQUIRE(multi_heap_free_size(heap) < before_free);
  306. multi_heap_check(heap, true);
  307. /* Free in random order */
  308. bool has_allocations = true;
  309. while (has_allocations) {
  310. i = rand() % NUM_P;
  311. multi_heap_free(heap, p[i]);
  312. p[i] = NULL;
  313. multi_heap_check(heap, true);
  314. has_allocations = false;
  315. for (i = 0; i < NUM_P && !has_allocations; i++) {
  316. has_allocations = (p[i] != NULL);
  317. }
  318. }
  319. /* all freed! */
  320. REQUIRE( before_free == multi_heap_free_size(heap) );
  321. }
  322. TEST_CASE("multi_heap_realloc()", "[multi_heap]")
  323. {
  324. const uint32_t PATTERN = 0xABABDADA;
  325. uint8_t small_heap[4 * 1024];
  326. multi_heap_handle_t heap = multi_heap_register(small_heap, sizeof(small_heap));
  327. uint32_t *a = (uint32_t *)multi_heap_malloc(heap, 64);
  328. uint32_t *b = (uint32_t *)multi_heap_malloc(heap, 32);
  329. REQUIRE( a != NULL );
  330. REQUIRE( b != NULL );
  331. REQUIRE( b > a); /* 'b' takes the block after 'a' */
  332. *a = PATTERN;
  333. uint32_t *c = (uint32_t *)multi_heap_realloc(heap, a, 72);
  334. REQUIRE( multi_heap_check(heap, true));
  335. REQUIRE( c != NULL );
  336. REQUIRE( c > b ); /* 'a' moves, 'c' takes the block after 'b' */
  337. REQUIRE( *c == PATTERN );
  338. #ifndef MULTI_HEAP_POISONING_SLOW
  339. // "Slow" poisoning implementation doesn't reallocate in place, so these
  340. // test will fail...
  341. uint32_t *d = (uint32_t *)multi_heap_realloc(heap, c, 36);
  342. REQUIRE( multi_heap_check(heap, true) );
  343. REQUIRE( c == d ); /* 'c' block should be shrunk in-place */
  344. REQUIRE( *d == PATTERN);
  345. uint32_t *e = (uint32_t *)multi_heap_malloc(heap, 64);
  346. REQUIRE( multi_heap_check(heap, true));
  347. REQUIRE( a == e ); /* 'e' takes the block formerly occupied by 'a' */
  348. multi_heap_free(heap, d);
  349. uint32_t *f = (uint32_t *)multi_heap_realloc(heap, b, 64);
  350. REQUIRE( multi_heap_check(heap, true) );
  351. REQUIRE( f == b ); /* 'b' should be extended in-place, over space formerly occupied by 'd' */
  352. #ifdef MULTI_HEAP_POISONING
  353. #define TOO_MUCH 7420 + 1
  354. #else
  355. #define TOO_MUCH 7420 + 1
  356. #endif
  357. /* not enough contiguous space left in the heap */
  358. uint32_t *g = (uint32_t *)multi_heap_realloc(heap, e, TOO_MUCH);
  359. REQUIRE( g == NULL );
  360. multi_heap_free(heap, f);
  361. /* try again */
  362. g = (uint32_t *)multi_heap_realloc(heap, e, 128);
  363. REQUIRE( multi_heap_check(heap, true) );
  364. REQUIRE( e == g ); /* 'g' extends 'e' in place, into the space formerly held by 'f' */
  365. #endif
  366. }
  367. // TLSF only accepts heaps aligned to 4-byte boundary so
  368. // only aligned allocation tests make sense.
  369. TEST_CASE("multi_heap aligned allocations", "[multi_heap]")
  370. {
  371. uint8_t test_heap[4 * 1024];
  372. multi_heap_handle_t heap = multi_heap_register(test_heap, sizeof(test_heap));
  373. uint32_t aligments = 0; // starts from alignment by 4-byte boundary
  374. size_t old_size = multi_heap_free_size(heap);
  375. size_t leakage = 1024;
  376. printf("[ALIGNED_ALLOC] heap_size before: %d \n", old_size);
  377. printf("New heap:\n");
  378. multi_heap_dump(heap);
  379. printf("*********************\n");
  380. for(;aligments <= 256; aligments++) {
  381. //Use some stupid size value to test correct alignment even in strange
  382. //memory layout objects:
  383. uint8_t *buf = (uint8_t *)multi_heap_aligned_alloc(heap, (aligments + 137), aligments );
  384. if(((aligments & (aligments - 1)) != 0) || (!aligments)) {
  385. REQUIRE( buf == NULL );
  386. } else {
  387. REQUIRE( buf != NULL );
  388. REQUIRE((intptr_t)buf >= (intptr_t)test_heap);
  389. REQUIRE((intptr_t)buf < (intptr_t)(test_heap + sizeof(test_heap)));
  390. printf("[ALIGNED_ALLOC] alignment required: %u \n", aligments);
  391. printf("[ALIGNED_ALLOC] address of allocated memory: %p \n\n", (void *)buf);
  392. //Address of obtained block must be aligned with selected value
  393. REQUIRE(((intptr_t)buf & (aligments - 1)) == 0);
  394. //Write some data, if it corrupts memory probably the heap
  395. //canary verification will fail:
  396. memset(buf, 0xA5, (aligments + 137));
  397. multi_heap_free(heap, buf);
  398. }
  399. }
  400. /* Check that TLSF doesn't allocate a memory space smaller than required.
  401. * In any case, TLSF will write data in the previous block than the one
  402. * allocated. Thus, we should try to get/allocate this previous block. If
  403. * the poisoned filled pattern has beeen overwritten by TLSF, then this
  404. * previous block will trigger an exception.
  405. * More info on this bug in !16296. */
  406. const size_t size = 50; /* TLSF will round the size up */
  407. uint8_t *buf1 = (uint8_t *)multi_heap_aligned_alloc(heap, size, 4);
  408. uint8_t *buf2 = (uint8_t *)multi_heap_aligned_alloc(heap, size, 4);
  409. multi_heap_free(heap, buf1);
  410. /* By specifying a size equal of the gap between buf1 and buf2. We are
  411. * trying to force TLSF to allocate two consecutive blocks. */
  412. buf1 = (uint8_t *)multi_heap_aligned_alloc(heap, buf2 - buf1, 4);
  413. multi_heap_free(heap, buf2);
  414. printf("[ALIGNED_ALLOC] heap_size after: %d \n", multi_heap_free_size(heap));
  415. REQUIRE((old_size - multi_heap_free_size(heap)) <= leakage);
  416. }
  417. // TLSF has some overhead when allocating blocks, check that overhead
  418. TEST_CASE("multi_heap allocation overhead", "[multi_heap]")
  419. {
  420. uint8_t heapdata[4 * 1024];
  421. size_t alloc_size = 256;
  422. multi_heap_handle_t heap = multi_heap_register(heapdata, sizeof(heapdata));
  423. size_t free_bytes_1 = multi_heap_free_size(heap);
  424. /* Allocate any amount of data, in any case there will be an overhead */
  425. void *x = multi_heap_malloc(heap, alloc_size);
  426. /* free_bytes_2 should be free_bytes_1 - alloc_size - overhead.
  427. * We don't know the value of overhead, let's check that it is non-zero */
  428. size_t free_bytes_2 = multi_heap_free_size(heap);
  429. REQUIRE( free_bytes_1 > free_bytes_2 );
  430. REQUIRE( free_bytes_1 - free_bytes_2 > alloc_size );
  431. multi_heap_free(heap, x);
  432. }
  433. /* This test will corrupt the memory of a free block in the heap and check
  434. * that in the case of comprehensive poisoning the heap corruption is detected
  435. * by multi_heap_check(). For light poisoning and no poisoning, the test will
  436. * check that multi_heap_check() does not report the corruption.
  437. */
  438. TEST_CASE("multi_heap poisoning detection", "[multi_heap]")
  439. {
  440. const size_t HEAP_SIZE = 4 * 1024;
  441. /* define heap related data */
  442. uint8_t heap_mem[HEAP_SIZE];
  443. memset(heap_mem, 0x00, HEAP_SIZE);
  444. /* register the heap memory. One free block only will be available */
  445. multi_heap_handle_t heap = multi_heap_register(heap_mem, HEAP_SIZE);
  446. /* offset in memory at which to find the first free memory byte */
  447. const size_t free_memory_offset = sizeof(multi_heap_info_t) + sizeof(control_t) + block_header_overhead;
  448. /* block header of the free block under test in the heap () */
  449. const block_header_t* block = (block_header_t*)(heap_mem + free_memory_offset - sizeof(block_header_t));
  450. /* actual number of bytes potentially filled with the free pattern in the free block under test */
  451. const size_t effective_free_size = block_size(block) - block_header_overhead - offsetof(block_header_t, next_free);
  452. /* variable used in the test */
  453. size_t affected_byte = 0x00;
  454. uint8_t original_value = 0x00;
  455. uint8_t corrupted_value = 0x00;
  456. /* repeat the corruption a few times to cover more of the free memory */
  457. for (size_t i = 0; i < effective_free_size; i++)
  458. {
  459. /* corrupt random bytes in the heap (it needs to be bytes from free memory in
  460. * order to check that the comprehensive poisoning is doing its job) */
  461. affected_byte = free_memory_offset + i;
  462. corrupted_value = (rand() % UINT8_MAX) | 1;
  463. /* keep the good value in store in order to check that when we set the byte back
  464. * to its original value, multi_heap_check() no longer returns the heap corruption. */
  465. original_value = heap_mem[affected_byte];
  466. /* make sure we are not replacing the original value with the same value */
  467. heap_mem[affected_byte] ^= corrupted_value;
  468. bool is_heap_ok = multi_heap_check(heap, true);
  469. #ifdef CONFIG_HEAP_POISONING_COMPREHENSIVE
  470. /* check that multi_heap_check() detects the corruption */
  471. REQUIRE(is_heap_ok == false);
  472. #else
  473. /* the comprehensive corruption is not checked in the multi_heap_check() */
  474. REQUIRE(is_heap_ok == true);
  475. #endif
  476. /* fix the corruption */
  477. heap_mem[affected_byte] = original_value;
  478. /* check that multi_heap_check() stops reporting the corruption */
  479. is_heap_ok = multi_heap_check(heap, true);
  480. REQUIRE(is_heap_ok == true);
  481. }
  482. }