From 90eb432c50081a2c0b4e05b75d547afa5ce36dad Mon Sep 17 00:00:00 2001 From: Felix Queissner Date: Mon, 17 Aug 2015 15:41:11 +0200 Subject: [PATCH] Adds config for malloc magic block number. Fixes bug in NativeMethod: Flips parameter list. --- include/config.h | 4 ++++ src/malloc.c | 12 ++++++++++-- src/vm.cpp | 4 ++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/config.h b/include/config.h index f428c54..cd6a1a0 100644 --- a/include/config.h +++ b/include/config.h @@ -11,3 +11,7 @@ /* #define USE_VERBOSE_FREE //*/ + +//* +#define USE_MAGIC_SECURED_MALLOC +//*/ diff --git a/src/malloc.c b/src/malloc.c index 0de1de8..0913326 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -9,7 +9,9 @@ typedef struct List { +#if defined(USE_MAGIC_SECURED_MALLOC) size_t magic; +#endif size_t length; size_t used; struct List *next; @@ -30,9 +32,11 @@ static void print_list() serial_printf(SERIAL_COM1, "malloc list: \n"); while(list != nullptr) { +#if defined(USE_MAGIC_SECURED_MALLOC) if(list->magic != 0xDEADBEEF) { die("malloc::print_list.InvalidMagicNumber"); } +#endif serial_printf(SERIAL_COM1, "[%x -> %x] %d %d\n", list, list->next, list->used, list->length); list = list->next; } @@ -69,9 +73,9 @@ void *malloc(size_t len) listBegin->length = (intptr_t)malloc_heap_end - (intptr_t)malloc_heap_start - sizeof(List); listBegin->used = 0; listBegin->next = nullptr; +#if defined(USE_MAGIC_SECURED_MALLOC) listBegin->magic = 0xDEADBEEF; - - print_list(); +#endif } List *cursor = listBegin; @@ -107,7 +111,9 @@ void *malloc(size_t len) newl->length = newLength; newl->used = 0; newl->next = cursor->next; +#if defined(USE_MAGIC_SECURED_MALLOC) newl->magic = 0xDEADBEEF; +#endif cursor->next = newl; } @@ -141,9 +147,11 @@ void free(void *ptr) if(entry->used == 0) { die_extra("free.InvalidBlock", itoa(ptr, nullptr, 16)); } +#if defined(USE_MAGIC_SECURED_MALLOC) if(entry->magic != 0xDEADBEEF) { die_extra("free.InvalidBlockMagic: ", itoa(entry->magic, nullptr, 16)); } +#endif if(entry->length > 0x5000) { die_extra("free.InvalidSizedBlock: ", itoa(entry->length, nullptr, 10)); diff --git a/src/vm.cpp b/src/vm.cpp index e33a6d6..ce3fc31 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -80,7 +80,7 @@ Variable NativeMethod::invoke(Vector arguments) // Copy arguments size_t stackSize = 0; - for(int i = 0; i < arguments.length(); i++) { + for(size_t i = 0; i < arguments.length(); i++) { if(arguments[i].type != this->parameters[i]) { // die_extra("NativeMethod.InvalidArgumentType", arguments[i].type.name()); } @@ -94,7 +94,7 @@ Variable NativeMethod::invoke(Vector arguments) uint8_t *stack = (uint8_t*)malloc(stackSize); uint8_t *stackPtr = stack; - for(int i = arguments.length() - 1; i >= 0; i--) { + for(size_t i = 0; i < arguments.length(); i++) { switch(arguments[i].type.id) { case TypeID::Bool: *reinterpret_cast(stackPtr) = arguments[i].boolean ? 1 : 0;