Skip to content

Commit

Permalink
o1vm/riscv32im: test dividing by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
dannywillems committed Dec 23, 2024
1 parent b32afd0 commit 27a0dd4
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
Binary file added o1vm/resources/programs/riscv32im/bin/div_by_zero
Binary file not shown.
Binary file added o1vm/resources/programs/riscv32im/bin/divu_by_zero
Binary file not shown.
19 changes: 19 additions & 0 deletions o1vm/resources/programs/riscv32im/src/div_by_zero.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.section .text
.globl _start

_start:
# Signed division by zero (div)
li t0, 42 # t0 = 42
li t1, 0 # t1 = 0
div t2, t0, t1 # t2 = t0 / t1 (Expected: division by zero)

# Custom exit syscall
li a0, 0
li a1, 0
li a2, 0
li a3, 0
li a4, 0
li a5, 0
li a6, 0
li a7, 42
ecall
19 changes: 19 additions & 0 deletions o1vm/resources/programs/riscv32im/src/divu_by_zero.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.section .text
.globl _start

_start:
# Unsigned division by zero (div)
li t0, 42 # t0 = 42
li t1, 0 # t1 = 0
divu t2, t0, t1 # t2 = t0 / t1 (Expected: division by zero)

# Custom exit syscall
li a0, 0
li a1, 0
li a2, 0
li a3, 0
li a4, 0
li a5, 0
li a6, 0
li a7, 42
ecall
30 changes: 30 additions & 0 deletions o1vm/tests/test_riscv_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,33 @@ fn test_slt() {
assert_eq!(witness.registers[T5], 0);
assert_eq!(witness.registers[T6], 0);
}

#[test]
#[should_panic]
fn test_div_by_zero() {
let curr_dir = std::env::current_dir().unwrap();
let path = curr_dir.join(std::path::PathBuf::from(
"resources/programs/riscv32im/bin/div_by_zero",
));
let state = o1vm::elf_loader::parse_riscv32(&path).unwrap();
let mut witness = Env::<Fp>::create(PAGE_SIZE.try_into().unwrap(), state);

while !witness.halt {
witness.step();
}
}

#[test]
#[should_panic]
fn test_divu_by_zero() {
let curr_dir = std::env::current_dir().unwrap();
let path = curr_dir.join(std::path::PathBuf::from(
"resources/programs/riscv32im/bin/divu_by_zero",
));
let state = o1vm::elf_loader::parse_riscv32(&path).unwrap();
let mut witness = Env::<Fp>::create(PAGE_SIZE.try_into().unwrap(), state);

while !witness.halt {
witness.step();
}
}

0 comments on commit 27a0dd4

Please sign in to comment.