From f89c47e54f16359006c33bc15bc48e4b71907e80 Mon Sep 17 00:00:00 2001 From: Danny Willems Date: Mon, 23 Dec 2024 15:43:53 +0100 Subject: [PATCH] o1vm/riscv32im: test for addi with overflow --- .../programs/riscv32im/bin/addi_overflow | Bin 0 -> 476 bytes .../programs/riscv32im/src/addi_overflow.S | 23 ++++++++++++++++++ o1vm/tests/test_riscv_elf.rs | 18 ++++++++++++++ 3 files changed, 41 insertions(+) create mode 100755 o1vm/resources/programs/riscv32im/bin/addi_overflow create mode 100644 o1vm/resources/programs/riscv32im/src/addi_overflow.S diff --git a/o1vm/resources/programs/riscv32im/bin/addi_overflow b/o1vm/resources/programs/riscv32im/bin/addi_overflow new file mode 100755 index 0000000000000000000000000000000000000000..d6df4b003cf2fae4971a200ab5abc1f71a7ad5be GIT binary patch literal 476 zcma)2F;2rk5L`QiL_$Kq6|_h+sjTEkNrMDbliT6ViFcyH=2Lqx721Fa& z{s=9uxf-GwZId|#1IVVCUq~(UUYyCi8!J|0nalWr+d?PS@P47k>0^?@2%TlKX^(|c z*;6g1#wJiik)2p)oElN&(q~6e^{z-cXY^?1QvQZr>i|Yh0>4t_>cH24FV2nY7~pKH X0gW^B+49ck$_(GOPO&)OPXE4Nx)(+# literal 0 HcmV?d00001 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 a4ab2e526e..077b634270 100644 --- a/o1vm/tests/test_riscv_elf.rs +++ b/o1vm/tests/test_riscv_elf.rs @@ -213,3 +213,21 @@ fn test_add_sub_swap() { assert_eq!(witness.registers[T0], 10); assert_eq!(witness.registers[T1], 5); } + +#[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); +}