diff --git a/o1vm/resources/programs/riscv32im/bin/addi_overflow b/o1vm/resources/programs/riscv32im/bin/addi_overflow new file mode 100755 index 0000000000..d6df4b003c Binary files /dev/null and b/o1vm/resources/programs/riscv32im/bin/addi_overflow differ diff --git a/o1vm/resources/programs/riscv32im/src/addi_overflow.S b/o1vm/resources/programs/riscv32im/src/addi_overflow.S new file mode 100644 index 0000000000..c1539b05a4 --- /dev/null +++ b/o1vm/resources/programs/riscv32im/src/addi_overflow.S @@ -0,0 +1,23 @@ +.section .text +.globl _start + +_start: + li t0, 2147483647 # t0 = 2147483647 (Max 32-bit signed int) + addi t1, t0, 1 # t1 = t0 + 1 (Expected: overflow to -2147483648) + + li t2, -2147483648 # t2 = -2147483648 (Min 32-bit signed int) + addi t3, t2, -1 # t3 = t2 + (-1) (Expected: overflow to 2147483647) + + li t4, 123456789 # t4 = 123456789 + addi t5, t4, 0 # t5 = t4 + 0 (Expected: t4 = 123456789) + + # 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 diff --git a/o1vm/tests/test_riscv_elf.rs b/o1vm/tests/test_riscv_elf.rs index 05acfeef76..53ecaa692f 100644 --- a/o1vm/tests/test_riscv_elf.rs +++ b/o1vm/tests/test_riscv_elf.rs @@ -214,3 +214,21 @@ fn test_add_sub_swap() { assert_eq!(witness.registers[T1], 5); assert_eq!(witness.registers[T2], 15); } + +#[test] +fn test_addi_overflow() { + let curr_dir = std::env::current_dir().unwrap(); + let path = curr_dir.join(std::path::PathBuf::from( + "resources/programs/riscv32im/bin/addi_overflow", + )); + let state = o1vm::elf_loader::parse_riscv32(&path).unwrap(); + let mut witness = Env::::create(PAGE_SIZE.try_into().unwrap(), state); + + while !witness.halt { + witness.step(); + } + + assert_eq!(witness.registers[T1], (-2147483648_i32) as u32); + assert_eq!(witness.registers[T3], 2147483647); + assert_eq!(witness.registers[T5], 123456789); +}