summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenedikt Böhm <bb@xnull.de>2009-07-07 10:34:07 +0200
committerBenedikt Böhm <bb@xnull.de>2009-07-07 10:34:07 +0200
commit4ad083bf274a972ed7286b9b52179e30873aa830 (patch)
treec23346d09d906d61855ff1dfe3f42f43d0bc374d
parent2ee0ef30cb01415cdb9cdbf4adc5ad279f44f661 (diff)
downloadswppy-4ad083bf274a972ed7286b9b52179e30873aa830.tar.gz
swppy-4ad083bf274a972ed7286b9b52179e30873aa830.tar.xz
swppy-4ad083bf274a972ed7286b9b52179e30873aa830.zip
implement memory bounds checking
-rw-r--r--src/emu/cpu.c4
-rw-r--r--src/emu/cpu.h1
-rw-r--r--src/emu/mem.c9
-rw-r--r--src/emu/mem.h1
-rw-r--r--src/emu/risci.c1
5 files changed, 15 insertions, 1 deletions
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 <stdint.h>
#include <sys/types.h>
#include <string.h>
+#include <inttypes.h>
#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 <sys/types.h>
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