Browse Source

1

congestion
Charlie Root 5 months ago
parent
commit
4e2b1c0c01
  1. 28
      lib/mem.c
  2. 3
      lib/memory_pool.c

28
lib/mem.c

@ -88,34 +88,30 @@ void u_check(void* ptr, const char* text, const char* location) {
bool error = false; bool error = false;
for (size_t i = 0; i < BOUNDARY_CHECK_SIZE; i++) { for (size_t i = 0; i < BOUNDARY_CHECK_SIZE; i++) {
if (prefix_start[i] != PADDING_FILL) { 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)", 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]);
error = true; error = true;
} }
} }
if (*pre_canary != CANARY) { 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)", 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);
error = true; error = true;
} }
uint8_t* post_start = user + size + 4; uint8_t* post_start = user + size + 4;
for (size_t i = 0; i < BOUNDARY_CHECK_SIZE; i++) { for (size_t i = 0; i < BOUNDARY_CHECK_SIZE; i++) {
if (post_start[i] != PADDING_FILL) { 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)", 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]);
error = true; error = true;
} }
} }
uint32_t* post_canary = (uint32_t*)(user + size); uint32_t* post_canary = (uint32_t*)(user + size);
if (*post_canary != CANARY) { 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)", 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);
error = true; error = true;
} }
if (error) { if (error) {
printf("%s: %s: Allocated at: %s\n", location, text, alloc_loc); DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s: %s: Allocated at: %s", location, text, alloc_loc);
printf("%s: %s: Memory block size (user): %u bytes, total allocated: %u bytes\n", location, text, size, total); DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s: %s: Memory block size (user): %u bytes, total allocated: %u bytes", location, text, size, total);
printf("%s: %s: Full memory dump at base %p:\n", location, text, base); DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s: %s: Full memory dump at base %p:", location, text, base);
hex_dump(base, total); hex_dump(base, total);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -195,7 +191,7 @@ void u_free_impl(void* ptr, const char* location) {
} }
UNLOCK(); UNLOCK();
if (!found) { 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); exit(EXIT_FAILURE);
} }
free(base); free(base);
@ -215,10 +211,10 @@ void u_report_unfreed_blocks(void) {
void* head = allocated_head; void* head = allocated_head;
UNLOCK(); UNLOCK();
if (count == 0) { if (count == 0) {
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "No unfreed memory blocks.\n"); DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "No unfreed memory blocks.");
return; 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; void* curr = head;
while (curr) { while (curr) {
char* alloc_loc = (char*)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); uint32_t size = *(uint32_t*)(prefix_start + BOUNDARY_CHECK_SIZE + 4);
void* user_ptr = prefix_start + BOUNDARY_CHECK_SIZE + 8; void* user_ptr = prefix_start + BOUNDARY_CHECK_SIZE + 8;
u_check(user_ptr, "unfreed_check", "u_report_unfreed_blocks"); 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, " - Allocated at: %s, size: %u bytes", alloc_loc, size);
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, " Full memory dump at %p:\n", user_ptr); DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, " Full memory dump at %p:", user_ptr);
hex_dump(user_ptr, size); hex_dump(user_ptr, size);
curr = *(void**)((uint8_t*)curr + NEXT_OFFSET); curr = *(void**)((uint8_t*)curr + NEXT_OFFSET);
} }

3
lib/memory_pool.c

@ -89,7 +89,8 @@ void memory_pool_destroy(struct memory_pool* pool) {
obj = next; 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_head = NULL;
pool->free_count = 0; pool->free_count = 0;

Loading…
Cancel
Save