From 0e49dfeb0f57fb16d98c3f9c12b7a00e80f184e4 Mon Sep 17 00:00:00 2001 From: Peter Bex Date: Sat, 17 Oct 2015 16:44:25 +0200 Subject: [PATCH 2/4] Try to detect corrupted data before performing GC_REALLOC. If an "impossible" object in the stack or heap: one that has a size that's larger than the memory area that contains it, we know we have a data corruption on our hands. Panic immediately instead of going in for the reallocating GC. This prevents the memory state from being mutated any more, which may help in debugging. --- runtime.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/runtime.c b/runtime.c index dda983c..af10825 100644 --- a/runtime.c +++ b/runtime.c @@ -3627,6 +3627,11 @@ C_regparm void C_fcall really_mark(C_word *x) bytes = (h & C_BYTEBLOCK_BIT) ? n : n * sizeof(C_word); if(((C_byte *)p2 + bytes + sizeof(C_word)) > tospace_limit) { + /* Detect impossibilities before GC_REALLOC to preserve state: */ + if (C_in_stackp((C_word)p) && bytes > stack_size) + panic(C_text("Detected corrupted data in stack")); + if (C_in_heapp((C_word)p) && bytes > (heap_size / 2)) + panic(C_text("Detected corrupted data in heap")); if(C_heap_size_is_fixed) panic(C_text("out of memory - heap full")); -- 2.1.4