diff --git a/ceno_emul/src/rv32im.rs b/ceno_emul/src/rv32im.rs index 0e69877e8..e50c184dc 100644 --- a/ceno_emul/src/rv32im.rs +++ b/ceno_emul/src/rv32im.rs @@ -91,7 +91,7 @@ pub enum TrapCause { LoadAccessFault(ByteAddr), StoreAddressMisaligned(ByteAddr), StoreAccessFault, - EnvironmentCallFromUserMode, + EcallError, } #[derive(Clone, Debug, Default)] diff --git a/ceno_emul/src/vm_state.rs b/ceno_emul/src/vm_state.rs index 6bccfc035..92d0b33a4 100644 --- a/ceno_emul/src/vm_state.rs +++ b/ceno_emul/src/vm_state.rs @@ -90,7 +90,7 @@ impl EmuContext for VMState { self.succeeded = true; Ok(true) } else { - self.trap(TrapCause::EnvironmentCallFromUserMode) + self.trap(TrapCause::EcallError) } } diff --git a/ceno_emul/tests/data/README.md b/ceno_emul/tests/data/README.md index e71cf5728..746886507 100644 --- a/ceno_emul/tests/data/README.md +++ b/ceno_emul/tests/data/README.md @@ -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/ ``` \ No newline at end of file diff --git a/ceno_emul/tests/data/ceno_rt_panic b/ceno_emul/tests/data/ceno_rt_panic new file mode 100755 index 000000000..927fa7df0 Binary files /dev/null and b/ceno_emul/tests/data/ceno_rt_panic differ diff --git a/ceno_emul/tests/test_elf.rs b/ceno_emul/tests/test_elf.rs index e3e548e76..9e0f516d3 100644 --- a/ceno_emul/tests/test_elf.rs +++ b/ceno_emul/tests/test_elf.rs @@ -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); diff --git a/ceno_rt/examples/ceno_rt_panic.rs b/ceno_rt/examples/ceno_rt_panic.rs new file mode 100644 index 000000000..24a0b20b4 --- /dev/null +++ b/ceno_rt/examples/ceno_rt_panic.rs @@ -0,0 +1,10 @@ +#![no_main] +#![no_std] + +#[allow(unused_imports)] +use ceno_rt; + +#[no_mangle] +fn main() { + panic!("This is a panic message!"); +}