Skip to content

Commit

Permalink
⚒️ Finish ELF tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Sep 24, 2023
1 parent a4b0ca4 commit 98d4454
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion crates/mipsevm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod mips;
pub use mips::InstrumentedState;

mod patch;
pub use patch::{load_elf, patch_go, patch_stack};
pub use patch::{load_elf, patch_go, patch_stack, MultiReader};

#[cfg(test)]
mod test_utils;
1 change: 0 additions & 1 deletion crates/mipsevm/src/mips/instrumented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ mod test {
}

#[test]
#[ignore]
fn test_claim() {
let elf_bytes = include_bytes!("../../../../example/bin/claim.elf");
let mut state = load_elf(elf_bytes).unwrap();
Expand Down
1 change: 1 addition & 0 deletions crates/mipsevm/src/mips/mips_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ where
break;
}
}
v0 = a2;
}
Ok(Fd::PreimageWrite) => {
let effective_address = a1 & 0xFFFFFFFC;
Expand Down
9 changes: 6 additions & 3 deletions crates/mipsevm/src/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn load_elf(raw: &[u8]) -> Result<State> {
continue;
}

let section_data = elf.segment_data(&header)?;
let section_data = &elf.segment_data(&header)?[..header.p_filesz as usize];
let mut reader: Box<dyn Read> = Box::new(section_data);

if header.p_filesz != header.p_memsz {
Expand Down Expand Up @@ -117,6 +117,9 @@ pub fn patch_go(raw: &[u8], state: &State) -> Result<()> {
let name = string_table.get(symbol_idx as usize)?;

if GO_SYMBOLS.contains(&name) {
// MIPS32 patch: ret (pseudo instruction)
// 03e00008 = jr $ra = ret (pseudo instruction)
// 00000000 = nop (executes with delay-slot, but does nothing)
state.memory.borrow_mut().set_memory_range(
symbol.st_value as u32,
[0x03, 0xe0, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00].as_slice(),
Expand Down Expand Up @@ -147,7 +150,7 @@ pub fn patch_stack(state: &mut State) -> Result<()> {
// Allocate 1 page for the initial stack data, and 16KB = 4 pages for the stack to grow.
state.memory.borrow_mut().set_memory_range(
ptr - 4 * page::PAGE_SIZE as u32,
[0; page::PAGE_SIZE * 5].as_slice(),
[0u8; page::PAGE_SIZE * 5].as_slice(),
)?;
state.registers[29] = ptr;

Expand Down Expand Up @@ -176,7 +179,7 @@ pub fn patch_stack(state: &mut State) -> Result<()> {
}

/// A multi reader is a reader that reads from the first reader until it returns 0, then reads from the second reader.
struct MultiReader<R1: Read, R2: Read>(R1, R2);
pub struct MultiReader<R1: Read, R2: Read>(R1, R2);

impl<R1: Read, R2: Read> Read for MultiReader<R1, R2> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
Expand Down
30 changes: 21 additions & 9 deletions crates/mipsevm/src/test_utils/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,11 @@ mod test {
test_utils::{ClaimTestOracle, StaticOracle, BASE_ADDR_END, END_ADDR},
Address, InstrumentedState, Memory, State,
};
use elf::{endian::AnyEndian, ElfBytes};
use revm::primitives::ExecutionResult;
use std::{
cell::RefCell,
fs,
io::{self, BufReader},
io::{self, BufReader, BufWriter},
path::PathBuf,
rc::Rc,
};
Expand Down Expand Up @@ -493,7 +492,6 @@ mod test {
}

#[test]
#[ignore]
fn test_claim_evm() {
let mut mips_evm = MipsEVM::new();
mips_evm.try_init().unwrap();
Expand All @@ -503,12 +501,11 @@ mod test {
patch::patch_go(elf_bytes, &mut state).unwrap();
patch::patch_stack(&mut state).unwrap();

let mut instrumented = InstrumentedState::new(
state,
ClaimTestOracle::default(),
io::stdout(),
io::stderr(),
);
let out_buf = BufWriter::new(Vec::default());
let err_buf = BufWriter::new(Vec::default());

let mut instrumented =
InstrumentedState::new(state, ClaimTestOracle::default(), out_buf, err_buf);

for i in 0..2_000_000 {
if instrumented.state.exited {
Expand Down Expand Up @@ -537,5 +534,20 @@ mod test {

assert!(instrumented.state.exited, "Must complete program");
assert_eq!(instrumented.state.exit_code, 0, "Must exit with 0");

assert_eq!(
String::from_utf8(instrumented.std_out.buffer().to_vec()).unwrap(),
format!(
"computing {} * {} + {}\nclaim {} is good!\n",
ClaimTestOracle::S,
ClaimTestOracle::A,
ClaimTestOracle::B,
ClaimTestOracle::S * ClaimTestOracle::A + ClaimTestOracle::B
)
);
assert_eq!(
String::from_utf8(instrumented.std_err.buffer().to_vec()).unwrap(),
"started!"
);
}
}

0 comments on commit 98d4454

Please sign in to comment.