heap: add dynamic poisoning threshold in pytest env to allow test with known memory leak to pass

This commit is contained in:
Guillaume Souchere
2022-10-05 15:03:05 +02:00
parent a29627f59d
commit 2cce5e98b1
2 changed files with 26 additions and 9 deletions

View File

@@ -8,7 +8,12 @@
#include "unity_test_runner.h" #include "unity_test_runner.h"
#include "esp_heap_caps.h" #include "esp_heap_caps.h"
#define TEST_MEMORY_LEAK_THRESHOLD (-3000) #define TEST_MEMORY_LEAK_THRESHOLD_DEFAULT -100
static int leak_threshold = TEST_MEMORY_LEAK_THRESHOLD_DEFAULT;
void set_leak_threshold(int threshold)
{
leak_threshold = threshold;
}
static size_t before_free_8bit; static size_t before_free_8bit;
static size_t before_free_32bit; static size_t before_free_32bit;
@@ -17,7 +22,7 @@ static void check_leak(size_t before_free, size_t after_free, const char *type)
{ {
ssize_t delta = after_free - before_free; ssize_t delta = after_free - before_free;
printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta); printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta);
TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak"); TEST_ASSERT_MESSAGE(delta >= leak_threshold, "memory leak");
} }
void setUp(void) void setUp(void)
@@ -32,6 +37,8 @@ void tearDown(void)
size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
check_leak(before_free_8bit, after_free_8bit, "8BIT"); check_leak(before_free_8bit, after_free_8bit, "8BIT");
check_leak(before_free_32bit, after_free_32bit, "32BIT"); check_leak(before_free_32bit, after_free_32bit, "32BIT");
leak_threshold = TEST_MEMORY_LEAK_THRESHOLD_DEFAULT;
} }
void app_main(void) void app_main(void)

View File

@@ -17,11 +17,14 @@
#include "../tlsf/tlsf.h" #include "../tlsf/tlsf.h"
extern void set_leak_threshold(int threshold);
/* NOTE: This is not a well-formed unit test, it leaks memory */ /* NOTE: This is not a well-formed unit test, it leaks memory */
TEST_CASE("Allocate new heap at runtime", "[heap][ignore]") TEST_CASE("Allocate new heap at runtime", "[heap]")
{ {
// 60 bytes of overhead in multi_heap + size of control_t from tlsf // 84 bytes of overhead to account for multi_heap structs and eventual
const size_t HEAP_OVERHEAD_MAX = tlsf_size() + 60; // poisoning bytes + size of control_t from tlsf
const size_t HEAP_OVERHEAD_MAX = tlsf_size() + 84;
const size_t MIN_HEAP_SIZE = HEAP_OVERHEAD_MAX + tlsf_block_size_min(); const size_t MIN_HEAP_SIZE = HEAP_OVERHEAD_MAX + tlsf_block_size_min();
const size_t BUF_SZ = MIN_HEAP_SIZE; const size_t BUF_SZ = MIN_HEAP_SIZE;
void *buffer = malloc(BUF_SZ); void *buffer = malloc(BUF_SZ);
@@ -33,15 +36,19 @@ TEST_CASE("Allocate new heap at runtime", "[heap][ignore]")
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);
// set the leak threshold to a bigger value as this test leaks memory
set_leak_threshold(-3000);
} }
/* NOTE: This is not a well-formed unit test, it leaks memory and /* NOTE: This is not a well-formed unit test, it leaks memory and
may fail if run twice in a row without a reset. may fail if run twice in a row without a reset.
*/ */
TEST_CASE("Allocate new heap with new capability", "[heap][ignore]") TEST_CASE("Allocate new heap with new capability", "[heap]")
{ {
// 60 bytes of multi_heap structures overhead + size of control_t from tlsf // 84 bytes of overhead to account for multi_heap structs and eventual
const size_t HEAP_OVERHEAD = tlsf_size() + 60; // poisoning bytes + size of control_t from tlsf
const size_t HEAP_OVERHEAD = tlsf_size() + 84;
const size_t MIN_HEAP_SIZE = HEAP_OVERHEAD + tlsf_block_size_min(); const size_t MIN_HEAP_SIZE = HEAP_OVERHEAD + tlsf_block_size_min();
const size_t BUF_SZ = MIN_HEAP_SIZE; const size_t BUF_SZ = MIN_HEAP_SIZE;
const size_t ALLOC_SZ = tlsf_block_size_min(); const size_t ALLOC_SZ = tlsf_block_size_min();
@@ -58,13 +65,16 @@ TEST_CASE("Allocate new heap with new capability", "[heap][ignore]")
/* ta-da, it's now possible! */ /* ta-da, it's now possible! */
TEST_ASSERT_NOT_NULL( heap_caps_malloc(ALLOC_SZ, MALLOC_CAP_INVENTED) ); TEST_ASSERT_NOT_NULL( heap_caps_malloc(ALLOC_SZ, MALLOC_CAP_INVENTED) );
// set the leak threshold to a bigger value as this test leaks memory
set_leak_threshold(-3000);
} }
/* NOTE: This is not a well-formed unit test. /* NOTE: This is not a well-formed unit test.
* If run twice without a reset, it will failed. * If run twice without a reset, it will failed.
*/ */
TEST_CASE("Add .bss memory to heap region runtime", "[heap][ignore]") TEST_CASE("Add .bss memory to heap region runtime", "[heap]")
{ {
#define HEAP_OVERHEAD_MAX 3248 #define HEAP_OVERHEAD_MAX 3248
#define BUF_SZ 3260 #define BUF_SZ 3260