From 2e9bb3fd76b53268bb2d24539037c6ed2cbbdf1e Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Sat, 21 Dec 2024 20:51:52 +0000 Subject: [PATCH] Better memory errors and in general more runtime memory --- src/elf.cpp | 4 +++- src/rve.cpp | 2 +- src/vm.cpp | 24 ++++++++++++++++-------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/elf.cpp b/src/elf.cpp index 1b13a4e..cf105cd 100644 --- a/src/elf.cpp +++ b/src/elf.cpp @@ -158,7 +158,9 @@ std::vector load_elf(const std::string& filename, size_t memory_size) { for (const Elf32Section& shdr : sectionHeaders) { if (should_load_section(shdr)) { if (shdr.sh_type == SHT_NOBITS) { - throw std::runtime_error("Trying to load section without data"); + // Section contains no data - just skip it. It will be initialized + // to zero. + continue; } file.seekg(shdr.sh_offset); diff --git a/src/rve.cpp b/src/rve.cpp index 2e614f4..1190220 100644 --- a/src/rve.cpp +++ b/src/rve.cpp @@ -10,7 +10,7 @@ #include "vm.hpp" int main(int argc, char *argv[]) { - const size_t MEMORY_SIZE = 512 * 1024; + const size_t MEMORY_SIZE = 64 * 1024 * 1024; bool debug = false; std::string program_filename = ""; diff --git a/src/vm.cpp b/src/vm.cpp index df31cf3..9b8b10f 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -32,7 +32,8 @@ void VM::setreg(size_t regnum, uint32_t value) { void UART::read_mem_u8(uint8_t* dst, size_t addr) { if (addr < UART_ADDR || addr + 1 > UART_ADDR + 8) { - throw std::runtime_error("Memory access out of bounds"); + throw std::runtime_error(std::string("Memory access out of bounds: ") + + std::format("{:x}", addr)); } addr -= UART_ADDR; switch (addr) { @@ -46,7 +47,8 @@ void UART::read_mem_u8(uint8_t* dst, size_t addr) { void UART::write_mem_u8(uint8_t* src, size_t addr) { if (addr < UART_ADDR || addr + 1 > UART_ADDR + 8) { - throw std::runtime_error("Memory access out of bounds"); + throw std::runtime_error(std::string("Memory access out of bounds: ") + + std::format("{:x}", addr)); } addr -= UART_ADDR; switch (addr) { @@ -91,7 +93,8 @@ void VM::read_mem_u8(uint8_t* dst, size_t addr) { return; } } - throw std::runtime_error("Memory access out of bounds"); + throw std::runtime_error(std::string("Memory access out of bounds: ") + + std::format("{:x}", addr)); } void VM::read_mem_u16(uint16_t* dst, size_t addr) { @@ -101,7 +104,8 @@ void VM::read_mem_u16(uint16_t* dst, size_t addr) { return; } } - throw std::runtime_error("Memory access out of bounds"); + throw std::runtime_error(std::string("Memory access out of bounds: ") + + std::format("{:x}", addr)); } void VM::read_mem_u32(uint32_t* dst, size_t addr) { @@ -111,7 +115,8 @@ void VM::read_mem_u32(uint32_t* dst, size_t addr) { return; } } - throw std::runtime_error("Memory access out of bounds"); + throw std::runtime_error(std::string("Memory access out of bounds: ") + + std::format("{:x}", addr)); } void VM::write_mem_u8(uint8_t* src, size_t addr) { @@ -121,7 +126,8 @@ void VM::write_mem_u8(uint8_t* src, size_t addr) { return; } } - throw std::runtime_error("Memory access out of bounds"); + throw std::runtime_error(std::string("Memory access out of bounds: ") + + std::format("{:x}", addr)); } void VM::write_mem_u16(uint16_t* src, size_t addr) { @@ -131,7 +137,8 @@ void VM::write_mem_u16(uint16_t* src, size_t addr) { return; } } - throw std::runtime_error("Memory access out of bounds"); + throw std::runtime_error(std::string("Memory access out of bounds: ") + + std::format("{:x}", addr)); } void VM::write_mem_u32(uint32_t* src, size_t addr) { @@ -141,7 +148,8 @@ void VM::write_mem_u32(uint32_t* src, size_t addr) { return; } } - throw std::runtime_error("Memory access out of bounds"); + throw std::runtime_error(std::string("Memory access out of bounds: ") + + std::format("{:x}", addr)); } std::vector VM::read_memory(size_t start, size_t size) {