heap: fix the boundary checks when adding a new region

This commit is contained in:
Guillaume Souchere 2022-10-05 15:05:08 +02:00
parent 2cce5e98b1
commit 2b5a844640
2 changed files with 6 additions and 3 deletions

View File

@ -189,14 +189,14 @@ bool heap_caps_check_add_region_allowed(intptr_t heap_start, intptr_t heap_end,
* 3.add region (s3>=s && e3<e) |---------------| correct: bool condition_3 = start >= heap_start && end < heap_end; * 3.add region (s3>=s && e3<e) |---------------| correct: bool condition_3 = start >= heap_start && end < heap_end;
* |--------------| correct * |--------------| correct
* *
* 4.add region (s4<e && e4>e) |------------------------| wrong: bool condition_4 = start < heap_end && end > heap_end; * 4.add region (s4<e && e4>=e) |------------------------| wrong: bool condition_4 = start < heap_end && end >= heap_end;
* |---------------------| wrong * |---------------------| wrong
* *
* 5.add region (s5>=e) |----| correct: bool condition_5 = start >= heap_end; * 5.add region (s5>=e) |----| correct: bool condition_5 = start >= heap_end;
*/ */
bool condition_2 = start < heap_start && end > heap_start; // if true then region not allowed bool condition_2 = start < heap_start && end > heap_start; // if true then region not allowed
bool condition_4 = start < heap_end && end > heap_end; // if true then region not allowed bool condition_4 = start < heap_end && end >= heap_end; // if true then region not allowed
return (condition_2 || condition_4) ? false: true; return (condition_2 || condition_4) ? false: true;
} }

View File

@ -87,6 +87,9 @@ TEST_CASE("Add .bss memory to heap region runtime", "[heap]")
printf("Before %"PRIu32" after %"PRIu32"\n", before_free, after_free); printf("Before %"PRIu32" after %"PRIu32"\n", before_free, after_free);
/* allow for some 'heap overhead' from accounting structures */ /* allow for some 'heap overhead' from accounting structures */
TEST_ASSERT(after_free >= before_free + BUF_SZ - HEAP_OVERHEAD_MAX); TEST_ASSERT(after_free >= before_free + BUF_SZ - HEAP_OVERHEAD_MAX);
/* Twice add must be failed */
TEST_ASSERT( (heap_caps_add_region((intptr_t)s_buffer, (intptr_t)s_buffer + BUF_SZ) != ESP_OK) );
} }
extern esp_err_t heap_caps_check_add_region_allowed(intptr_t heap_start, intptr_t heap_end, intptr_t start, intptr_t end); extern esp_err_t heap_caps_check_add_region_allowed(intptr_t heap_start, intptr_t heap_end, intptr_t start, intptr_t end);
@ -98,10 +101,10 @@ TEST_CASE("Add heap region address range checks", "[heap]")
TEST_ASSERT_TRUE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x0, 0x1000)); TEST_ASSERT_TRUE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x0, 0x1000));
TEST_ASSERT_TRUE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x1000, 0x2000)); TEST_ASSERT_TRUE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x1000, 0x2000));
TEST_ASSERT_TRUE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x1000, 0x3000));
TEST_ASSERT_TRUE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x3000, 0x4000)); TEST_ASSERT_TRUE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x3000, 0x4000));
TEST_ASSERT_FALSE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x0, 0x2000)); TEST_ASSERT_FALSE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x0, 0x2000));
TEST_ASSERT_FALSE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x0, 0x4000)); TEST_ASSERT_FALSE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x0, 0x4000));
TEST_ASSERT_FALSE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x1000, 0x4000)); TEST_ASSERT_FALSE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x1000, 0x4000));
TEST_ASSERT_FALSE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x2000, 0x4000)); TEST_ASSERT_FALSE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x2000, 0x4000));
TEST_ASSERT_FALSE(heap_caps_check_add_region_allowed(heap_start, heap_end, 0x1000, 0x3000));
} }