fixed: arena.clear bug
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 22 Aug 2023 23:58:44 +0000 (18:58 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 22 Aug 2023 23:58:44 +0000 (18:58 -0500)
core/alloc/arena.onyx
core/alloc/heap.onyx
interpreter/src/vm/vm_instrs.h

index 7e736b1b8d14d71679a4983a638cc1cd1b2c94e6..d602b5441cd1762dee65d7e98af56235b74b6798 100644 (file)
@@ -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;
 }
 
index 992fd46b4770c7747f367277ac92b5a529a55080..cb3615a18348adb055126d1c6dedb897f0428051 100644 (file)
@@ -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;
index 131f3ee72d3a0d7a0f95181e872f907bc74e8e57..a9430f00434facfebc40d9a2494b2a6dbf99815a 100644 (file)
@@ -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;