From 84bfd066a8a6a4d90557d0bcf76c9fe81b93fc74 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 22 Aug 2023 18:58:44 -0500 Subject: [PATCH] fixed: arena.clear bug --- core/alloc/arena.onyx | 2 ++ core/alloc/heap.onyx | 20 +++++++++++++++++++- interpreter/src/vm/vm_instrs.h | 4 +++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/core/alloc/arena.onyx b/core/alloc/arena.onyx index 7e736b1b..d602b544 100644 --- a/core/alloc/arena.onyx +++ b/core/alloc/arena.onyx @@ -131,12 +131,14 @@ free :: (arena: &Arena) { #doc "Clears and frees every page, except for first page." clear :: (arena: &Arena) { walker := arena.first_arena.next; + while walker != null { next := walker.next; raw_free(arena.backing_allocator, walker); walker = next; } + arena.first_arena.next = null; arena.size = sizeof rawptr; } diff --git a/core/alloc/heap.onyx b/core/alloc/heap.onyx index 992fd46b..cb3615a1 100644 --- a/core/alloc/heap.onyx +++ b/core/alloc/heap.onyx @@ -207,7 +207,7 @@ get_freed_size :: () => { #if Enable_Stack_Trace { trace := runtime.info.get_stack_trace(); for trace { - log(.Error, "Core", core.tprintf("in {} ({}:{})", it.info.func_name, it.info.file, it.current.line)); + log(.Error, "Core", core.tprintf("in {} ({}:{})", it.info.func_name, it.info.file, it.current_line)); } } } @@ -229,6 +229,24 @@ get_freed_size :: () => { trace(); return; } + + } else { + // + // If not in debug mode, still catch the wierd cases and prevent them from breaking + // the free list, as this will certainly cause terrible bugs that take hours to fix. + // + + if cast(uintptr) hb_ptr < cast(uintptr) __heap_start { + return; + } + + if hb_ptr.size & Allocated_Flag != Allocated_Flag { + return; + } + + if hb_ptr.magic_number != Alloc_Block_Magic_Number { + return; + } } hb_ptr.size &= ~Allocated_Flag; diff --git a/interpreter/src/vm/vm_instrs.h b/interpreter/src/vm/vm_instrs.h index 131f3ee7..a9430f00 100644 --- a/interpreter/src/vm/vm_instrs.h +++ b/interpreter/src/vm/vm_instrs.h @@ -282,7 +282,9 @@ OVMI_INSTR_EXEC(reg_set) { OVMI_INSTR_EXEC(idx_arr) { ovm_static_integer_array_t data_elem = state->program->static_data[instr->a]; - ovm_assert(VAL(instr->b).u32 < (u32) data_elem.len); + if (VAL(instr->b).u32 >= (u32) data_elem.len) { + OVMI_EXCEPTION_HOOK; + } VAL(instr->r).i32 = state->program->static_integers[data_elem.start_idx + VAL(instr->b).u32]; VAL(instr->r).type = OVM_TYPE_I32; -- 2.25.1