about summary refs log tree commit diff stats
path: root/subx
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2019-05-11 19:11:13 -0700
committerKartik Agaram <vc@akkartik.com>2019-05-11 19:11:13 -0700
commit6362c51d11ef27335875469e0ff06347357f46b1 (patch)
treef7efb1941266b97f2a21166a83b1c5b9e5f734f1 /subx
parentff167ad77a615ef0506dce1e21c973533ca83088 (diff)
downloadmu-6362c51d11ef27335875469e0ff06347357f46b1.tar.gz
5155 - check for overflow in mmap segments
Diffstat (limited to 'subx')
-rw-r--r--subx/012elf.cc8
-rw-r--r--subx/020syscalls.cc5
2 files changed, 12 insertions, 1 deletions
diff --git a/subx/012elf.cc b/subx/012elf.cc
index a77c6056..2fea60db 100644
--- a/subx/012elf.cc
+++ b/subx/012elf.cc
@@ -90,7 +90,13 @@ void load_elf_contents(uint8_t* elf_contents, size_t size, int argc, char* argv[
 
 void push(uint32_t val) {
   Reg[ESP].u -= 4;
-  assert(Reg[ESP].u >= STACK_SEGMENT);
+  if (Reg[ESP].u < STACK_SEGMENT) {
+    raise << "The stack overflowed its segment. "
+          << "Maybe SPACE_FOR_SEGMENT should be larger? "
+          << "Or you need to carve out an exception for the stack segment "
+          << "to be larger.\n" << end();
+    exit(1);
+  }
   trace(Callstack_depth+1, "run") << "decrementing ESP to 0x" << HEXWORD << Reg[ESP].u << end();
   trace(Callstack_depth+1, "run") << "pushing value 0x" << HEXWORD << val << end();
   write_mem_u32(Reg[ESP].u, val);
diff --git a/subx/020syscalls.cc b/subx/020syscalls.cc
index 6b9faa2c..444c9fd5 100644
--- a/subx/020syscalls.cc
+++ b/subx/020syscalls.cc
@@ -122,5 +122,10 @@ uint32_t new_segment(uint32_t length) {
   uint32_t result = Next_segment;
   Mem.push_back(vma(Next_segment, Next_segment+length));
   Next_segment -= SPACE_FOR_SEGMENT;
+  if (Next_segment <= DATA_SEGMENT) {
+    raise << "Allocated too many segments; the VM ran out of memory. "
+          << "Maybe SPACE_FOR_SEGMENT can be smaller?\n" << end();
+    exit(1);
+  }
   return result;
 }