From 177abfe846197678959d158f098510239fd63d2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Autin?= Date: Sat, 11 Mar 2023 23:49:20 +0100 Subject: [PATCH] Renamed Machine::init_machine and fixed stack overflow by allocating main_memory to the heap --- src/main.rs | 2 +- src/simulator/loader.rs | 2 +- src/simulator/machine.rs | 67 +++++++++++++++++++++------------------- src/simulator/mem_cmp.rs | 17 +++++----- 4 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1bde63e..78b9e54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,6 @@ use kernel::system::System; use simulator::machine::Machine; fn main() { - let machine = Machine::_init_machine(); + let machine = Machine::init_machine(); let _system = System::new(machine); } diff --git a/src/simulator/loader.rs b/src/simulator/loader.rs index b7b4b36..4b84b83 100644 --- a/src/simulator/loader.rs +++ b/src/simulator/loader.rs @@ -18,7 +18,7 @@ use std::io::BufRead; pub fn _load(path : &str, instruction_size: i32) -> Machine { let file = fs::File::open(path).expect("Wrong filename"); let reader = io::BufReader::new(file); - let mut machine = Machine::_init_machine(); + let mut machine = Machine::init_machine(); for (i,line) in reader.lines().enumerate() { let res = u64::from_str_radix(&line.unwrap(), 16); diff --git a/src/simulator/machine.rs b/src/simulator/machine.rs index 8ed2eb4..357b837 100644 --- a/src/simulator/machine.rs +++ b/src/simulator/machine.rs @@ -73,7 +73,7 @@ pub struct Machine { pub sp: usize, pub int_reg : Register, pub fp_reg : Register, - pub main_memory : [u8 ; MEM_SIZE], + pub main_memory : Vec, pub shiftmask : [u64 ; 64], pub registers_trace : String, // for tests pub interrupt: Interrupt @@ -84,7 +84,7 @@ pub struct Machine { impl Machine { - pub fn _init_machine() -> Machine { + pub fn init_machine() -> Machine { let mut shiftmask : [u64 ; 64] = [0 ; 64]; let mut value : u64 = 0xffffffff; @@ -99,7 +99,7 @@ impl Machine { sp: 0, int_reg : Register::::init(), fp_reg : Register::::init(), - main_memory : [0; MEM_SIZE], + main_memory : vec![0_u8; MEM_SIZE], shiftmask, interrupt: Interrupt::new(), registers_trace : String::from("") @@ -700,9 +700,14 @@ mod test { use crate::simulator::{machine::Machine, mem_cmp}; + #[test] + fn test_init_machine() { + let _ = Machine::init_machine(); + } + #[test] fn test_read_memory() { - let mut m = Machine::_init_machine(); + let mut m = Machine::init_machine(); m.main_memory[4] = 43; m.main_memory[5] = 150; assert_eq!((43 << 8) + 150, Machine::read_memory(&mut m, 2, 4)); @@ -710,7 +715,7 @@ mod test { #[test] fn test_write_memory() { - let mut m = Machine::_init_machine(); + let mut m = Machine::init_machine(); Machine::write_memory(&mut m, 2, 6, (43 << 8) + 150); assert_eq!(43, m.main_memory[6]); assert_eq!(150, m.main_memory[7]); @@ -723,11 +728,11 @@ mod test { #[test] fn test_comp() { - let mut m = Machine::_init_machine(); + let mut m = Machine::init_machine(); let path_before = "memoryComp.txt".to_string(); let path_after = "memoryCompEnd.txt".to_string(); - let memory_before = mem_cmp::MemChecker::from(&path_before); - let memory_after = mem_cmp::MemChecker::from(&path_after); + let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap(); + let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap(); mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m); Machine::run(&mut m); @@ -740,11 +745,11 @@ mod test { #[test] fn test_div() { - let mut m = Machine::_init_machine(); - let path_before = "memoryDiv.txt".to_string(); - let path_after = "memoryDivEnd.txt".to_string(); - let memory_before = mem_cmp::MemChecker::from(&path_before); - let memory_after = mem_cmp::MemChecker::from(&path_after); + let mut m = Machine::init_machine(); + let path_before = "memoryDiv.txt"; + let path_after = "memoryDivEnd.txt"; + let memory_before = mem_cmp::MemChecker::from(path_before).unwrap(); + let memory_after = mem_cmp::MemChecker::from(path_after).unwrap(); mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m); Machine::run(&mut m); @@ -757,11 +762,11 @@ mod test { #[test] fn test_if() { - let mut m = Machine::_init_machine(); + let mut m = Machine::init_machine(); let path_before = "memoryIf.txt".to_string(); let path_after = "memoryIfEnd.txt".to_string(); - let memory_before = mem_cmp::MemChecker::from(&path_before); - let memory_after = mem_cmp::MemChecker::from(&path_after); + let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap(); + let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap(); mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m); Machine::run(&mut m); @@ -774,11 +779,11 @@ mod test { #[test] fn test_jump() { - let mut m = Machine::_init_machine(); + let mut m = Machine::init_machine(); let path_before = "memoryJump.txt".to_string(); let path_after = "memoryJumpEnd.txt".to_string(); - let memory_before = mem_cmp::MemChecker::from(&path_before); - let memory_after = mem_cmp::MemChecker::from(&path_after); + let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap(); + let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap(); mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m); Machine::run(&mut m); @@ -791,11 +796,11 @@ mod test { #[test] fn test_mul() { - let mut m = Machine::_init_machine(); + let mut m = Machine::init_machine(); let path_before = "memoryMul.txt".to_string(); let path_after = "memoryMulEnd.txt".to_string(); - let memory_before = mem_cmp::MemChecker::from(&path_before); - let memory_after = mem_cmp::MemChecker::from(&path_after); + let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap(); + let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap(); mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m); Machine::run(&mut m); @@ -808,11 +813,11 @@ mod test { #[test] fn test_ret() { - let mut m = Machine::_init_machine(); + let mut m = Machine::init_machine(); let path_before = "memoryRet.txt".to_string(); let path_after = "memoryRetEnd.txt".to_string(); - let memory_before = mem_cmp::MemChecker::from(&path_before); - let memory_after = mem_cmp::MemChecker::from(&path_after); + let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap(); + let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap(); mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m); Machine::run(&mut m); @@ -825,11 +830,11 @@ mod test { #[test] fn test_sub() { - let mut m = Machine::_init_machine(); + let mut m = Machine::init_machine(); let path_before = "memorySub.txt".to_string(); let path_after = "memorySubEnd.txt".to_string(); - let memory_before = mem_cmp::MemChecker::from(&path_before); - let memory_after = mem_cmp::MemChecker::from(&path_after); + let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap(); + let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap(); mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m); Machine::run(&mut m); @@ -842,11 +847,11 @@ mod test { #[test] fn test_switch() { - let mut m = Machine::_init_machine(); + let mut m = Machine::init_machine(); let path_before = "memorySwitch.txt".to_string(); let path_after = "memorySwitchEnd.txt".to_string(); - let memory_before = mem_cmp::MemChecker::from(&path_before); - let memory_after = mem_cmp::MemChecker::from(&path_after); + let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap(); + let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap(); mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m); Machine::run(&mut m); diff --git a/src/simulator/mem_cmp.rs b/src/simulator/mem_cmp.rs index 780437e..6822d8d 100644 --- a/src/simulator/mem_cmp.rs +++ b/src/simulator/mem_cmp.rs @@ -1,7 +1,4 @@ -use std::fs; -use std::io::BufRead; -use std::io::BufReader; -use std::io::Lines; +use std::{fs, io::{BufRead, BufReader, Lines, Error}}; use crate::Machine; const MEM_SIZE : usize = 4096; @@ -117,9 +114,9 @@ impl MemChecker{ /// /// ### Return /// Mem-checker filled - pub fn from(path: &String) -> MemChecker { + pub fn from(path: &str) -> Result { - let file = fs::File::open(path).expect("Wrong filename"); + let file = fs::File::open(path)?; let reader = BufReader::new(file); let mut lines = reader.lines(); @@ -155,7 +152,7 @@ impl MemChecker{ } - MemChecker{pc, sp, sections} + Ok(MemChecker{pc, sp, sections}) } @@ -311,8 +308,8 @@ mod tests { #[test] fn test_fill_memory(){ let path = "osef".to_string(); - let m_c = MemChecker::from(&path); - let mut machine = Machine::_init_machine(); + let m_c = MemChecker::from(&path).unwrap(); + let mut machine = Machine::init_machine(); MemChecker::fill_memory_from_mem_checker(&m_c, &mut machine); @@ -334,7 +331,7 @@ mod tests { #[test] fn test_create_mem_checker(){ let path: String = "osef".to_string(); - let m_c = MemChecker::from(&path); + let m_c = MemChecker::from(&path).unwrap(); MemChecker::print_mem_checker(&m_c); }