From 4ad083bf274a972ed7286b9b52179e30873aa830 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedikt=20B=C3=B6hm?= Date: Tue, 7 Jul 2009 10:34:07 +0200 Subject: implement memory bounds checking --- src/emu/cpu.c | 4 ++++ src/emu/cpu.h | 1 + src/emu/mem.c | 9 +++++++++ src/emu/mem.h | 1 + src/emu/risci.c | 1 - 5 files changed, 15 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/emu/cpu.c b/src/emu/cpu.c index 522e2f2..1ac1517 100644 --- a/src/emu/cpu.c +++ b/src/emu/cpu.c @@ -54,6 +54,10 @@ void trap(int num) fprintf(stderr, "trap: unaligned memory access\n"); abort(); break; + case TRP_SEGV: + fprintf(stderr, "trap: segmentation fault\n"); + abort(); + break; case TRP_DIVBYZERO: fprintf(stderr, "trap: division by zero\n"); abort(); diff --git a/src/emu/cpu.h b/src/emu/cpu.h index 9294d7d..6f2a077 100644 --- a/src/emu/cpu.h +++ b/src/emu/cpu.h @@ -9,6 +9,7 @@ /* cpu traps */ enum { TRP_UNALIGNED, + TRP_SEGV, TRP_DIVBYZERO, TRP_SYSCALL, TRP_ILL, diff --git a/src/emu/mem.c b/src/emu/mem.c index cdbf2ae..21caafd 100644 --- a/src/emu/mem.c +++ b/src/emu/mem.c @@ -1,16 +1,21 @@ #include #include #include +#include #include "cpu.h" #include "mem.h" +#include "log.h" uint8_t *MEM; +uint64_t mem_size = 64 * 1024 * 1024; void store(size_t addr, uint32_t value) { if (addr & 0x2) trap(TRP_UNALIGNED); + if (addr >= mem_size) + trap(TRP_SEGV); memcpy(&MEM[addr], &value, 4); } @@ -19,12 +24,15 @@ uint32_t load(size_t addr) uint32_t tmp32; if (addr & 0x2) trap(TRP_UNALIGNED); + if (addr >= mem_size) + trap(TRP_SEGV); memcpy(&tmp32, &MEM[addr], 4); return tmp32; } void push(uint32_t value) { + //debug("push(%"PRIu32")", value); GPR[SP] -= 4; store(GPR[SP], value); } @@ -32,6 +40,7 @@ void push(uint32_t value) uint32_t pop(void) { uint32_t tmp32 = load(GPR[SP]); + //debug("pop(%"PRIu32")", tmp32); GPR[SP] += 4; return tmp32; } diff --git a/src/emu/mem.h b/src/emu/mem.h index 985a838..eea391e 100644 --- a/src/emu/mem.h +++ b/src/emu/mem.h @@ -5,6 +5,7 @@ #include extern uint8_t *MEM; +extern uint64_t mem_size; void store(size_t addr, uint32_t value); uint32_t load(size_t addr); diff --git a/src/emu/risci.c b/src/emu/risci.c index 4f73615..a7c99bf 100644 --- a/src/emu/risci.c +++ b/src/emu/risci.c @@ -17,7 +17,6 @@ #include "opc.h" bool is_debug = false; -uint64_t mem_size = 64 * 1024 * 1024; uint64_t reg_size = 0; static -- cgit v1.2.3