Add tests to check "freed memory fill" behaviour in debug mode

This commit is contained in:
Frank Richter 2021-12-19 21:57:06 +01:00
parent 1cf7ca021d
commit 691eb0d8ed

View File

@ -15,6 +15,7 @@ terms of the MIT license. A copy of the license can be found in the file
bool check_zero_init(uint8_t* p, size_t size); bool check_zero_init(uint8_t* p, size_t size);
#if MI_DEBUG >= 2 #if MI_DEBUG >= 2
bool check_debug_fill_uninit(uint8_t* p, size_t size); bool check_debug_fill_uninit(uint8_t* p, size_t size);
bool check_debug_fill_freed(uint8_t* p, size_t size);
#endif #endif
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -269,6 +270,22 @@ int main(void) {
result &= check_debug_fill_uninit(p, malloc_size); result &= check_debug_fill_uninit(p, malloc_size);
mi_free(p); mi_free(p);
}); });
CHECK_BODY("fill-freed-small", {
size_t malloc_size = MI_SMALL_SIZE_MAX / 2;
uint8_t* p = (uint8_t*)mi_malloc(malloc_size);
mi_free(p);
// First sizeof(void*) bytes will contain housekeeping data, skip these
result = check_debug_fill_freed(p + sizeof(void*), malloc_size - sizeof(void*));
});
CHECK_BODY("fill-freed-large", {
size_t malloc_size = MI_SMALL_SIZE_MAX * 2;
uint8_t* p = (uint8_t*)mi_malloc(malloc_size);
mi_free(p);
// First sizeof(void*) bytes will contain housekeeping data, skip these
result = check_debug_fill_freed(p + sizeof(void*), malloc_size - sizeof(void*));
});
#endif #endif
// --------------------------------------------------- // ---------------------------------------------------
@ -301,4 +318,15 @@ bool check_debug_fill_uninit(uint8_t* p, size_t size) {
} }
return result; return result;
} }
bool check_debug_fill_freed(uint8_t* p, size_t size) {
if(!p)
return false;
bool result = true;
for (size_t i = 0; i < size; ++i) {
result &= p[i] == MI_DEBUG_FREED;
}
return result;
}
#endif #endif