Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore(mmu): add overflow handling #452

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions tracer/src/emulator/mmu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,16 @@ impl Mmu {
address: effective_address,
post_value: value,
});
} else if effective_address > self.jolt_device.memory_layout.panic {
// address is in the area or zero padding region => stack overflow as stack grows downwards
panic!("Stack overflow: Attempted to write to 0x{:X}, which is in the area or zero padding region.",
effective_address);
} else {
panic!("Unknown memory mapping {:X}.", effective_address);
}
} else if !self.memory.validate_address(effective_address) {
// address is > DRAM_BASE + memory.len() => heap overflow
panic!("Heap overflow: Attempted to write to 0x{:X}, which is beyond the allocated memory.", effective_address);
} else {
self.tracer.push_memory(MemoryState::Write {
address: effective_address,
Expand Down Expand Up @@ -1117,3 +1124,51 @@ impl MemoryWrapper {
self.memory.validate_address(address - DRAM_BASE)
}
}

#[cfg(test)]
mod test_mmu {
use super::*;
use crate::emulator::terminal::DummyTerminal;
use std::rc::Rc;

const MEM_CAPACITY: u64 = 1024 * 1024;

fn setup_mmu(capacity: u64) -> Mmu {
let terminal = Box::new(DummyTerminal::new());
let tracer = Rc::new(Tracer::new());
let mut mmu = Mmu::new(Xlen::Bit64, terminal, tracer);

mmu.init_memory(capacity);

mmu
}

#[test]
#[should_panic(expected = "Heap overflow")]
fn test_heap_overflow() {
let mut mmu = setup_mmu(MEM_CAPACITY);

// Try to write beyond the allocated memory
let overflow_address = DRAM_BASE + MEM_CAPACITY + 1;
mmu.trace_store(overflow_address, 0xc50513);
}

#[test]
#[should_panic(expected = "Stack overflow")]
fn test_stack_overflow() {
let mut mmu = setup_mmu(MEM_CAPACITY);

// Try to write to an address below DRAM_BASE
let invalid_address = DRAM_BASE - 1;
mmu.trace_store(invalid_address, 0xc50513);
}

#[test]
#[should_panic(expected = "Unknown memory mapping")]
fn test_unknown_memory_mapping() {
let mut mmu = setup_mmu(MEM_CAPACITY);

let invalid_address = 1234;
mmu.trace_store(invalid_address, 0xc50513);
}
}