Merge branch 'fix/missing-block-owner-in-heap-init-alloc' into 'master'

fix(heap): Add block owner to allocs in heap_caps_init()

Closes IDFGH-12450

See merge request espressif/esp-idf!30139
pull/13651/head
Guillaume Souchere 2024-04-12 15:23:16 +08:00
commit 7639933eeb
2 zmienionych plików z 22 dodań i 6 usunięć

Wyświetl plik

@ -69,7 +69,7 @@ void heap_caps_init(void)
num_regions = soc_get_available_memory_regions(regions);
// the following for loop will calculate the number of possible heaps
// based on how many regions were coalesed.
// based on how many regions were coalesced.
size_t num_heaps = num_regions;
//The heap allocator will treat every region given to it as separate. In order to get bigger ranges of contiguous memory,
@ -83,7 +83,7 @@ void heap_caps_init(void)
b->size += a->size;
// remove one heap from the number of heaps as
// 2 regions just got coalesed.
// 2 regions just got coalesced.
num_heaps--;
}
}
@ -132,8 +132,14 @@ void heap_caps_init(void)
heap_t *heaps_array = NULL;
for (size_t i = 0; i < num_heaps; i++) {
if (heap_caps_match(&temp_heaps[i], MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL)) {
/* use the first DRAM heap which can fit the data */
heaps_array = multi_heap_malloc(temp_heaps[i].heap, sizeof(heap_t) * num_heaps);
/* use the first DRAM heap which can fit the data.
* the allocated block won't include the block owner bytes since this operation
* is done by the top level API heap_caps_malloc(). So we need to add it manually
* after successful allocation. Allocate extra 4 bytes for that purpose. */
heaps_array = multi_heap_malloc(temp_heaps[i].heap, MULTI_HEAP_ADD_BLOCK_OWNER_SIZE(sizeof(heap_t) * num_heaps));
assert(heaps_array != NULL);
MULTI_HEAP_SET_BLOCK_OWNER(heaps_array);
heaps_array = (heap_t *)MULTI_HEAP_ADD_BLOCK_OWNER_OFFSET(heaps_array);
if (heaps_array != NULL) {
break;
}
@ -186,7 +192,7 @@ bool heap_caps_check_add_region_allowed(intptr_t heap_start, intptr_t heap_end,
* cannot be added twice. In fact, registering the same memory region as a heap twice
* would cause a corruption and then an exception at runtime.
*
* the existing heap region s(tart) e(nd)
* the existing heap region start end
* |----------------------|
*
* 1.add region (e1<s) |-----| correct: bool condition_1 = end < heap_start;

Wyświetl plik

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
@ -49,6 +49,16 @@ static void check_heap_task_info(TaskHandle_t taskHdl)
// heap_caps_get_per_task_info includes the size of the block owner (4 bytes)
TEST_ASSERT(heap_info.totals[i].size[0] == ALLOC_BYTES + 4);
}
// test that if not 0, the task handle corresponds to an actual task.
// this test is to make sure no rubbish is stored as a task handle.
if (heap_info.totals[i].task != 0) {
// feeding the task name returned by pcTaskGetName() to xTaskGetHandle().
// xTaskGetHandle would return the task handler used as parameter in
// pcTaskGetName if the task handle is valid. Otherwise, it will return
// NULL or just crash if the pointer to the task name is complete nonsense.
TEST_ASSERT_EQUAL(heap_info.totals[i].task, xTaskGetHandle(pcTaskGetName(heap_info.totals[i].task)));
}
}
TEST_ASSERT_TRUE(task_found);
}