From 4e2b1c0c01d979ae326b190cbaf38b77ef9cc277 Mon Sep 17 00:00:00 2001 From: Charlie Root Date: Tue, 21 Apr 2026 15:33:46 +0300 Subject: [PATCH] 1 --- lib/mem.c | 28 ++++++++++++---------------- lib/memory_pool.c | 3 ++- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/lib/mem.c b/lib/mem.c index 912f7182..757233a7 100644 --- a/lib/mem.c +++ b/lib/mem.c @@ -88,34 +88,30 @@ void u_check(void* ptr, const char* text, const char* location) { bool error = false; for (size_t i = 0; i < BOUNDARY_CHECK_SIZE; i++) { if (prefix_start[i] != PADDING_FILL) { - printf("%s: %s: underflow prefix corrupted at %p, byte %zu (expected 0x%02X, got 0x%02X)\n", location, text, ptr, i, PADDING_FILL, prefix_start[i]); - DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s: %s: underflow prefix corrupted at %p, byte %zu (expected 0x%02X, got 0x%02X)\n", location, text, ptr, i, PADDING_FILL, prefix_start[i]); + DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s: %s: underflow prefix corrupted at %p, byte %zu (expected 0x%02X, got 0x%02X)", location, text, ptr, i, PADDING_FILL, prefix_start[i]); error = true; } } if (*pre_canary != CANARY) { - printf("%s: %s: underflow canary corrupted at %p (expected 0x%X, got 0x%X)\n", location, text, ptr, CANARY, *pre_canary); - DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s: %s: underflow canary corrupted at %p (expected 0x%X, got 0x%X)\n", location, text, ptr, CANARY, *pre_canary); + DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s: %s: underflow canary corrupted at %p (expected 0x%X, got 0x%X)", location, text, ptr, CANARY, *pre_canary); error = true; } uint8_t* post_start = user + size + 4; for (size_t i = 0; i < BOUNDARY_CHECK_SIZE; i++) { if (post_start[i] != PADDING_FILL) { - printf("%s: %s: overflow prefix corrupted at %p, byte %zu (expected 0x%02X, got 0x%02X)\n", location, text, ptr, i, PADDING_FILL, post_start[i]); - DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s: %s: overflow prefix corrupted at %p, byte %zu (expected 0x%02X, got 0x%02X)\n", location, text, ptr, i, PADDING_FILL, post_start[i]); + DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s: %s: overflow prefix corrupted at %p, byte %zu (expected 0x%02X, got 0x%02X)", location, text, ptr, i, PADDING_FILL, post_start[i]); error = true; } } uint32_t* post_canary = (uint32_t*)(user + size); if (*post_canary != CANARY) { - printf("%s: %s: overflow canary corrupted at %p (expected 0x%X, got 0x%X)\n", location, text, ptr, CANARY, *post_canary); - DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s: %s: overflow canary corrupted at %p (expected 0x%X, got 0x%X)\n", location, text, ptr, CANARY, *post_canary); + DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s: %s: overflow canary corrupted at %p (expected 0x%X, got 0x%X)", location, text, ptr, CANARY, *post_canary); error = true; } if (error) { - printf("%s: %s: Allocated at: %s\n", location, text, alloc_loc); - printf("%s: %s: Memory block size (user): %u bytes, total allocated: %u bytes\n", location, text, size, total); - printf("%s: %s: Full memory dump at base %p:\n", location, text, base); + DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s: %s: Allocated at: %s", location, text, alloc_loc); + DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s: %s: Memory block size (user): %u bytes, total allocated: %u bytes", location, text, size, total); + DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s: %s: Full memory dump at base %p:", location, text, base); hex_dump(base, total); exit(EXIT_FAILURE); } @@ -195,7 +191,7 @@ void u_free_impl(void* ptr, const char* location) { } UNLOCK(); if (!found) { - DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s: u_free: Attempt to free unallocated or already freed pointer %p\n", location, ptr); + DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s: u_free: Attempt to free unallocated or already freed pointer %p", location, ptr); exit(EXIT_FAILURE); } free(base); @@ -215,10 +211,10 @@ void u_report_unfreed_blocks(void) { void* head = allocated_head; UNLOCK(); if (count == 0) { - DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "No unfreed memory blocks.\n"); + DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "No unfreed memory blocks."); return; } - DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Unfreed memory blocks: %zu\n", count); + DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Unfreed memory blocks: %zu", count); void* curr = head; while (curr) { char* alloc_loc = (char*)curr; @@ -226,8 +222,8 @@ void u_report_unfreed_blocks(void) { uint32_t size = *(uint32_t*)(prefix_start + BOUNDARY_CHECK_SIZE + 4); void* user_ptr = prefix_start + BOUNDARY_CHECK_SIZE + 8; u_check(user_ptr, "unfreed_check", "u_report_unfreed_blocks"); - DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, " - Allocated at: %s, size: %u bytes\n", alloc_loc, size); - DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, " Full memory dump at %p:\n", user_ptr); + DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, " - Allocated at: %s, size: %u bytes", alloc_loc, size); + DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, " Full memory dump at %p:", user_ptr); hex_dump(user_ptr, size); curr = *(void**)((uint8_t*)curr + NEXT_OFFSET); } diff --git a/lib/memory_pool.c b/lib/memory_pool.c index 98b3c548..4e0d3afa 100644 --- a/lib/memory_pool.c +++ b/lib/memory_pool.c @@ -89,7 +89,8 @@ void memory_pool_destroy(struct memory_pool* pool) { obj = next; } - DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "pool_destroy: allocations=%zu, reuse=%zu, current pool size=%d", pool->allocations, pool->reuse_count, cnt); +// if (cnt>MEMORY_POOL_MAX_FREE) + DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "pool_destroy: allocations=%zu, reuse=%zu, current pool size=%d", pool->allocations, pool->reuse_count, cnt); pool->free_head = NULL; pool->free_count = 0;