Skip to content

Commit

Permalink
runtime: test panic
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurélien Nicolas committed Sep 2, 2024
1 parent 8982cc7 commit 8285d59
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ceno_emul/src/rv32im.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub enum TrapCause {
LoadAccessFault(ByteAddr),
StoreAddressMisaligned(ByteAddr),
StoreAccessFault,
EnvironmentCallFromUserMode,
EcallError,
}

#[derive(Clone, Debug, Default)]
Expand Down
2 changes: 1 addition & 1 deletion ceno_emul/src/vm_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl EmuContext for VMState {
self.succeeded = true;
Ok(true)
} else {
self.trap(TrapCause::EnvironmentCallFromUserMode)
self.trap(TrapCause::EcallError)
}
}

Expand Down
2 changes: 1 addition & 1 deletion ceno_emul/tests/data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
```bash
cd ceno_rt
cargo build --release --examples
cp ../target/riscv32im-unknown-none-elf/release/examples/ceno_rt_mini ../ceno_emul/tests/data/
cp ../target/riscv32im-unknown-none-elf/release/examples/ceno_rt_{mini,panic,mem} ../ceno_emul/tests/data/
```
Binary file added ceno_emul/tests/data/ceno_rt_panic
Binary file not shown.
19 changes: 19 additions & 0 deletions ceno_emul/tests/test_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ fn test_ceno_rt_mini() -> Result<()> {
Ok(())
}

#[test]
fn test_ceno_rt_panic() -> Result<()> {
let mut state = VMState::new(CENO_PLATFORM);

// Load an ELF program in memory.
let program_elf = include_bytes!("./data/ceno_rt_panic");
let program = Program::load_elf(program_elf, u32::MAX)?;
for (addr, word) in program.image.iter() {
let addr = ByteAddr(*addr).waddr();
state.init_memory(addr, *word);
}
assert_eq!(program.entry, CENO_PLATFORM.pc_start());

let res = run(&mut state);
assert!(matches!(res, Err(e) if e.to_string().contains("EcallError")));

Ok(())
}

#[test]
fn test_ceno_rt_mem() -> Result<()> {
let mut state = VMState::new(CENO_PLATFORM);
Expand Down
10 changes: 10 additions & 0 deletions ceno_rt/examples/ceno_rt_panic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![no_main]
#![no_std]

#[allow(unused_imports)]
use ceno_rt;

#[no_mangle]
fn main() {
panic!("This is a panic message!");
}

0 comments on commit 8285d59

Please sign in to comment.