From 0d0f89ad33b8b68d7fe4502292ebb41879f26096 Mon Sep 17 00:00:00 2001 From: Nunuhara Cabbage Date: Sun, 8 Dec 2019 11:42:51 -0800 Subject: [PATCH] Track free address for HEAP_DEBUG Useful for debugging double free errors. --- vm.c | 11 +++++++++-- vm.h | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/vm.c b/vm.c index 1fcd82a..d81622d 100644 --- a/vm.c +++ b/vm.c @@ -151,9 +151,16 @@ static const char *vm_ptrtype_string(enum vm_pointer_type type) { void heap_unref(int slot) { if (heap[slot].ref <= 0) { +#ifdef DEBUG_HEAP + VM_ERROR("double free of slot %d (%s)\nOriginally allocd at %X\nOriginally freed at %X", + slot, vm_ptrtype_string(heap[slot].type), heap[slot].alloc_addr, heap[slot].free_addr); +#endif VM_ERROR("double free of slot %d (%s)", slot, vm_ptrtype_string(heap[slot].type)); } if (--heap[slot].ref <= 0) { +#ifdef DEBUG_HEAP + heap[slot].free_addr = instr_ptr; +#endif switch (heap[slot].type) { case VM_PAGE: if (heap[slot].page) { @@ -447,9 +454,9 @@ static void hll_call(int libno, int fno) static void function_return(void) { + heap_unref(call_stack[call_stack_ptr-1].page_slot); + instr_ptr = call_stack[call_stack_ptr-1].return_address; call_stack_ptr--; - heap_unref(call_stack[call_stack_ptr].page_slot); - instr_ptr = call_stack[call_stack_ptr].return_address; } static void system_call(int32_t code) diff --git a/vm.h b/vm.h index 6436f3e..59e1469 100644 --- a/vm.h +++ b/vm.h @@ -54,6 +54,7 @@ struct vm_pointer { #ifdef DEBUG_HEAP size_t alloc_addr; size_t ref_addr; + size_t free_addr; #endif };