Track free address for HEAP_DEBUG

Useful for debugging double free errors.
This commit is contained in:
Nunuhara Cabbage
2019-12-08 11:42:51 -08:00
parent 430c6fbbb6
commit 0d0f89ad33
2 changed files with 10 additions and 2 deletions
+9 -2
View File
@@ -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)
+1
View File
@@ -54,6 +54,7 @@ struct vm_pointer {
#ifdef DEBUG_HEAP
size_t alloc_addr;
size_t ref_addr;
size_t free_addr;
#endif
};