2017-05-03 04:03:28 -04:00
|
|
|
/*
|
|
|
|
Tests for the capabilities-based memory allocator.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <esp_types.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "unity.h"
|
|
|
|
#include "esp_attr.h"
|
|
|
|
#include "esp_heap_caps.h"
|
|
|
|
#include "esp_spi_flash.h"
|
|
|
|
#include <stdlib.h>
|
2018-06-27 02:46:20 -04:00
|
|
|
#include <sys/param.h>
|
2017-05-03 04:03:28 -04:00
|
|
|
|
2021-01-04 13:38:10 -05:00
|
|
|
#ifndef CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
|
2019-11-10 22:34:37 -05:00
|
|
|
TEST_CASE("Capabilities allocator test", "[heap]")
|
2017-05-03 04:03:28 -04:00
|
|
|
{
|
|
|
|
char *m1, *m2[10];
|
|
|
|
int x;
|
|
|
|
size_t free8start, free32start, free8, free32;
|
|
|
|
|
|
|
|
/* It's important we printf() something before we take the empty heap sizes,
|
|
|
|
as the first printf() in a task allocates heap resources... */
|
|
|
|
printf("Testing capabilities allocator...\n");
|
|
|
|
|
|
|
|
free8start = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
|
|
|
free32start = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
|
|
|
printf("Free 8bit-capable memory (start): %dK, 32-bit capable memory %dK\n", free8start, free32start);
|
2019-11-10 23:57:13 -05:00
|
|
|
TEST_ASSERT(free32start >= free8start);
|
2017-05-03 04:03:28 -04:00
|
|
|
|
|
|
|
printf("Allocating 10K of 8-bit capable RAM\n");
|
|
|
|
m1= heap_caps_malloc(10*1024, MALLOC_CAP_8BIT);
|
|
|
|
printf("--> %p\n", m1);
|
|
|
|
free8 = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
|
|
|
free32 = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
|
|
|
printf("Free 8bit-capable memory (both reduced): %dK, 32-bit capable memory %dK\n", free8, free32);
|
|
|
|
//Both should have gone down by 10K; 8bit capable ram is also 32-bit capable
|
2020-01-16 13:37:19 -05:00
|
|
|
TEST_ASSERT(free8<=(free8start-10*1024));
|
|
|
|
TEST_ASSERT(free32<=(free32start-10*1024));
|
2017-05-03 04:03:28 -04:00
|
|
|
//Assume we got DRAM back
|
|
|
|
TEST_ASSERT((((int)m1)&0xFF000000)==0x3F000000);
|
|
|
|
free(m1);
|
|
|
|
|
2018-06-27 02:46:20 -04:00
|
|
|
//The goal here is to allocate from IRAM. Since there is no external IRAM (yet)
|
|
|
|
//the following gives size of IRAM-only (not D/IRAM) memory.
|
|
|
|
size_t free_iram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL) -
|
|
|
|
heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
|
2018-07-25 02:45:39 -04:00
|
|
|
size_t alloc32 = MIN(free_iram / 2, 10*1024) & (~3);
|
2019-11-10 23:57:13 -05:00
|
|
|
if(free_iram) {
|
|
|
|
printf("Freeing; allocating %u bytes of 32K-capable RAM\n", alloc32);
|
|
|
|
m1 = heap_caps_malloc(alloc32, MALLOC_CAP_32BIT);
|
|
|
|
printf("--> %p\n", m1);
|
|
|
|
//Check that we got IRAM back
|
|
|
|
TEST_ASSERT((((int)m1)&0xFF000000)==0x40000000);
|
|
|
|
free8 = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
|
|
|
free32 = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
|
|
|
printf("Free 8bit-capable memory (after 32-bit): %dK, 32-bit capable memory %dK\n", free8, free32);
|
|
|
|
//Only 32-bit should have gone down by alloc32: 32-bit isn't necessarily 8bit capable
|
2020-01-16 13:37:19 -05:00
|
|
|
TEST_ASSERT(free32<=(free32start-alloc32));
|
2019-11-10 23:57:13 -05:00
|
|
|
TEST_ASSERT(free8==free8start);
|
|
|
|
free(m1);
|
|
|
|
} else {
|
|
|
|
printf("This platform has no 32-bit only capable RAM, jumping to next test \n");
|
|
|
|
}
|
2018-06-27 02:46:20 -04:00
|
|
|
|
2017-05-03 04:03:28 -04:00
|
|
|
printf("Allocating impossible caps\n");
|
|
|
|
m1= heap_caps_malloc(10*1024, MALLOC_CAP_8BIT|MALLOC_CAP_EXEC);
|
|
|
|
printf("--> %p\n", m1);
|
|
|
|
TEST_ASSERT(m1==NULL);
|
2019-11-10 23:57:13 -05:00
|
|
|
|
|
|
|
if(free_iram) {
|
|
|
|
printf("Testing changeover iram -> dram");
|
|
|
|
// priorities will exhaust IRAM first, then start allocating from DRAM
|
|
|
|
for (x=0; x<10; x++) {
|
|
|
|
m2[x]= heap_caps_malloc(alloc32, MALLOC_CAP_32BIT);
|
|
|
|
printf("--> %p\n", m2[x]);
|
|
|
|
}
|
|
|
|
TEST_ASSERT((((int)m2[0])&0xFF000000)==0x40000000);
|
|
|
|
TEST_ASSERT((((int)m2[9])&0xFF000000)==0x3F000000);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
printf("This platform has no IRAM-only so changeover will never occur, jumping to next test\n");
|
2017-05-03 04:03:28 -04:00
|
|
|
}
|
2019-11-10 23:57:13 -05:00
|
|
|
|
2017-05-03 04:03:28 -04:00
|
|
|
printf("Test if allocating executable code still gives IRAM, even with dedicated IRAM region depleted\n");
|
2020-01-02 01:25:33 -05:00
|
|
|
if(free_iram) {
|
2019-11-10 23:57:13 -05:00
|
|
|
// (the allocation should come from D/IRAM)
|
|
|
|
free_iram = heap_caps_get_free_size(MALLOC_CAP_EXEC);
|
|
|
|
m1= heap_caps_malloc(MIN(free_iram / 2, 10*1024), MALLOC_CAP_EXEC);
|
|
|
|
printf("--> %p\n", m1);
|
|
|
|
TEST_ASSERT((((int)m1)&0xFF000000)==0x40000000);
|
|
|
|
for (x=0; x<10; x++) free(m2[x]);
|
2020-01-02 01:25:33 -05:00
|
|
|
|
2019-11-10 23:57:13 -05:00
|
|
|
} else {
|
|
|
|
// (the allocation should come from D/IRAM)
|
|
|
|
free_iram = heap_caps_get_free_size(MALLOC_CAP_EXEC);
|
|
|
|
m1= heap_caps_malloc(MIN(free_iram / 2, 10*1024), MALLOC_CAP_EXEC);
|
|
|
|
printf("--> %p\n", m1);
|
|
|
|
TEST_ASSERT((((int)m1)&0xFF000000)==0x40000000);
|
|
|
|
}
|
|
|
|
|
2017-05-03 04:03:28 -04:00
|
|
|
free(m1);
|
|
|
|
printf("Done.\n");
|
|
|
|
}
|
2020-03-10 11:46:10 -04:00
|
|
|
#endif
|
2017-05-03 04:03:28 -04:00
|
|
|
|
2020-02-26 07:21:59 -05:00
|
|
|
#ifdef CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
|
|
|
|
TEST_CASE("IRAM_8BIT capability test", "[heap]")
|
|
|
|
{
|
|
|
|
uint8_t *ptr;
|
|
|
|
size_t free_size, free_size32, largest_free_size;
|
|
|
|
|
|
|
|
/* need to print something as first printf allocates some heap */
|
|
|
|
printf("IRAM_8BIT capability test\n");
|
|
|
|
|
|
|
|
free_size = heap_caps_get_free_size(MALLOC_CAP_IRAM_8BIT);
|
|
|
|
free_size32 = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
|
|
|
|
|
|
|
largest_free_size = heap_caps_get_largest_free_block(MALLOC_CAP_IRAM_8BIT);
|
|
|
|
|
|
|
|
ptr = heap_caps_malloc(largest_free_size, MALLOC_CAP_IRAM_8BIT);
|
|
|
|
|
|
|
|
TEST_ASSERT((((int)ptr)&0xFF000000)==0x40000000);
|
|
|
|
|
2022-03-03 00:01:54 -05:00
|
|
|
/* As the heap allocator may present an overhead for allocated blocks,
|
2022-04-10 23:36:52 -04:00
|
|
|
* we need to check that the free heap size is now smaller or equal to the former free size. */
|
2022-03-03 00:01:54 -05:00
|
|
|
TEST_ASSERT(heap_caps_get_free_size(MALLOC_CAP_IRAM_8BIT) <= (free_size - heap_caps_get_allocated_size(ptr)));
|
|
|
|
TEST_ASSERT(heap_caps_get_free_size(MALLOC_CAP_32BIT) <= (free_size32 - heap_caps_get_allocated_size(ptr)));
|
2020-02-26 07:21:59 -05:00
|
|
|
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-03 04:03:28 -04:00
|
|
|
TEST_CASE("heap_caps metadata test", "[heap]")
|
|
|
|
{
|
|
|
|
/* need to print something as first printf allocates some heap */
|
|
|
|
printf("heap_caps metadata test\n");
|
|
|
|
heap_caps_print_heap_info(MALLOC_CAP_8BIT);
|
|
|
|
|
|
|
|
multi_heap_info_t original;
|
|
|
|
heap_caps_get_info(&original, MALLOC_CAP_8BIT);
|
|
|
|
|
|
|
|
void *b = heap_caps_malloc(original.largest_free_block, MALLOC_CAP_8BIT);
|
|
|
|
TEST_ASSERT_NOT_NULL(b);
|
|
|
|
|
|
|
|
printf("After allocating %d bytes:\n", original.largest_free_block);
|
|
|
|
heap_caps_print_heap_info(MALLOC_CAP_8BIT);
|
|
|
|
|
|
|
|
multi_heap_info_t after;
|
|
|
|
heap_caps_get_info(&after, MALLOC_CAP_8BIT);
|
2020-05-19 12:32:31 -04:00
|
|
|
TEST_ASSERT(after.largest_free_block <= original.largest_free_block);
|
|
|
|
TEST_ASSERT(after.total_free_bytes <= original.total_free_bytes);
|
2017-05-03 04:03:28 -04:00
|
|
|
|
|
|
|
free(b);
|
|
|
|
heap_caps_get_info(&after, MALLOC_CAP_8BIT);
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2020-01-16 13:37:19 -05:00
|
|
|
printf("\n\n After test, heap status:\n");
|
|
|
|
heap_caps_print_heap_info(MALLOC_CAP_8BIT);
|
|
|
|
|
2018-07-29 21:41:30 -04:00
|
|
|
/* Allow some leeway here, because LWIP sometimes allocates up to 144 bytes in the background
|
|
|
|
as part of timer management.
|
|
|
|
*/
|
|
|
|
TEST_ASSERT_INT32_WITHIN(200, after.total_free_bytes, original.total_free_bytes);
|
|
|
|
TEST_ASSERT_INT32_WITHIN(200, after.largest_free_block, original.largest_free_block);
|
2017-05-03 04:03:28 -04:00
|
|
|
TEST_ASSERT(after.minimum_free_bytes < original.total_free_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Small function runs from IRAM to check that malloc/free/realloc
|
|
|
|
all work OK when cache is disabled...
|
|
|
|
*/
|
2019-07-16 05:33:30 -04:00
|
|
|
static IRAM_ATTR __attribute__((noinline)) bool iram_malloc_test(void)
|
2017-05-03 04:03:28 -04:00
|
|
|
{
|
2017-11-16 23:00:51 -05:00
|
|
|
spi_flash_guard_get()->start(); // Disables flash cache
|
2017-05-03 04:03:28 -04:00
|
|
|
|
|
|
|
bool result = true;
|
2020-01-16 13:37:19 -05:00
|
|
|
void *x = heap_caps_malloc(64, MALLOC_CAP_EXEC);
|
2017-05-03 04:03:28 -04:00
|
|
|
result = result && (x != NULL);
|
2020-01-16 13:37:19 -05:00
|
|
|
void *y = heap_caps_realloc(x, 32, MALLOC_CAP_EXEC);
|
2017-05-03 04:03:28 -04:00
|
|
|
result = result && (y != NULL);
|
|
|
|
heap_caps_free(y);
|
|
|
|
|
2017-11-16 23:00:51 -05:00
|
|
|
spi_flash_guard_get()->end(); // Re-enables flash cache
|
2017-05-03 04:03:28 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-01-16 13:37:19 -05:00
|
|
|
|
2017-05-03 04:03:28 -04:00
|
|
|
TEST_CASE("heap_caps_xxx functions work with flash cache disabled", "[heap]")
|
|
|
|
{
|
|
|
|
TEST_ASSERT( iram_malloc_test() );
|
|
|
|
}
|
2020-04-27 16:09:15 -04:00
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
#ifdef CONFIG_HEAP_ABORT_WHEN_ALLOCATION_FAILS
|
2020-04-27 16:09:15 -04:00
|
|
|
TEST_CASE("When enabled, allocation operation failure generates an abort", "[heap][reset=abort,SW_CPU_RESET]")
|
|
|
|
{
|
2020-11-10 02:40:01 -05:00
|
|
|
const size_t stupid_allocation_size = (128 * 1024 * 1024);
|
2020-04-27 16:09:15 -04:00
|
|
|
void *ptr = heap_caps_malloc(stupid_allocation_size, MALLOC_CAP_DEFAULT);
|
|
|
|
(void)ptr;
|
|
|
|
TEST_FAIL_MESSAGE("should not be reached");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static bool called_user_failed_hook = false;
|
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
void heap_caps_alloc_failed_hook(size_t requested_size, uint32_t caps, const char *function_name)
|
2020-04-27 16:09:15 -04:00
|
|
|
{
|
|
|
|
printf("%s was called but failed to allocate %d bytes with 0x%X capabilities. \n",function_name, requested_size, caps);
|
|
|
|
called_user_failed_hook = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("user provided alloc failed hook must be called when allocation fails", "[heap]")
|
|
|
|
{
|
|
|
|
TEST_ASSERT(heap_caps_register_failed_alloc_callback(heap_caps_alloc_failed_hook) == ESP_OK);
|
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
const size_t stupid_allocation_size = (128 * 1024 * 1024);
|
2020-04-27 16:09:15 -04:00
|
|
|
void *ptr = heap_caps_malloc(stupid_allocation_size, MALLOC_CAP_DEFAULT);
|
|
|
|
TEST_ASSERT(called_user_failed_hook != false);
|
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
called_user_failed_hook = false;
|
2020-04-27 16:09:15 -04:00
|
|
|
ptr = heap_caps_realloc(ptr, stupid_allocation_size, MALLOC_CAP_DEFAULT);
|
|
|
|
TEST_ASSERT(called_user_failed_hook != false);
|
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
called_user_failed_hook = false;
|
2020-04-27 16:09:15 -04:00
|
|
|
ptr = heap_caps_aligned_alloc(0x200, stupid_allocation_size, MALLOC_CAP_DEFAULT);
|
|
|
|
TEST_ASSERT(called_user_failed_hook != false);
|
|
|
|
|
|
|
|
(void)ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("allocation with invalid capability should also trigger the alloc failed hook", "[heap]")
|
|
|
|
{
|
|
|
|
const size_t allocation_size = 64;
|
|
|
|
const uint32_t invalid_cap = MALLOC_CAP_INVALID;
|
|
|
|
|
|
|
|
TEST_ASSERT(heap_caps_register_failed_alloc_callback(heap_caps_alloc_failed_hook) == ESP_OK);
|
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
called_user_failed_hook = false;
|
2020-04-27 16:09:15 -04:00
|
|
|
void *ptr = heap_caps_malloc(allocation_size, invalid_cap);
|
|
|
|
TEST_ASSERT(called_user_failed_hook != false);
|
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
called_user_failed_hook = false;
|
2020-04-27 16:09:15 -04:00
|
|
|
ptr = heap_caps_realloc(ptr, allocation_size, invalid_cap);
|
|
|
|
TEST_ASSERT(called_user_failed_hook != false);
|
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
called_user_failed_hook = false;
|
2020-04-27 16:09:15 -04:00
|
|
|
ptr = heap_caps_aligned_alloc(0x200, allocation_size, invalid_cap);
|
|
|
|
TEST_ASSERT(called_user_failed_hook != false);
|
|
|
|
|
|
|
|
(void)ptr;
|
2020-01-16 13:37:19 -05:00
|
|
|
}
|