From 83e516ca4d0aad9e55b3457068817da64a555a00 Mon Sep 17 00:00:00 2001 From: Danny Willems Date: Mon, 23 Dec 2024 15:07:02 +0100 Subject: [PATCH] o1vm/riscv32im: add tests for add with overflows --- .../programs/riscv32im/bin/add_overflow | Bin 0 -> 468 bytes .../programs/riscv32im/src/add_overflow.S | 31 ++++++++++++++++++ o1vm/tests/test_riscv_elf.rs | 20 +++++++++++ 3 files changed, 51 insertions(+) create mode 100755 o1vm/resources/programs/riscv32im/bin/add_overflow create mode 100644 o1vm/resources/programs/riscv32im/src/add_overflow.S diff --git a/o1vm/resources/programs/riscv32im/bin/add_overflow b/o1vm/resources/programs/riscv32im/bin/add_overflow new file mode 100755 index 0000000000000000000000000000000000000000..0af6dd1bcc4eec53b8f47e2c125f106151701e74 GIT binary patch literal 468 zcma)2F;2rk5L`P1A|ZvK`v4LRl|_z}G!#%#Qe&MRN1PM~>+TYvG$43FKEM-@Qt&MV z4+t|hPFmKQo!Ql_Y|r!c?M)O#(pD@#!YQDSz#|NMWI2&o&Y(MZr21$N!0Df+kGDiq z%|tKJ<#HhM8OzJ+^?Rc_16jXiqWWK(b$=!bh83Xp!GJmd1F8q^y~iHsSNCYn`g|eL z9c0%nF0|FvFfDa;o*Jr&u5>zPTiV<^?w58_%<=+8=(3zohZM$?kByi*pF@cx{=o+q zOeRSvo`NLxyAl(Ev)Mcp{D%M5A&g7_zf$KJz?VQ<%!_plFw=TKV^(dZ+_ia<;oH^` K7N^^3=lcaNdO?5y literal 0 HcmV?d00001 diff --git a/o1vm/resources/programs/riscv32im/src/add_overflow.S b/o1vm/resources/programs/riscv32im/src/add_overflow.S new file mode 100644 index 0000000000..1d3616f36b --- /dev/null +++ b/o1vm/resources/programs/riscv32im/src/add_overflow.S @@ -0,0 +1,31 @@ +# The addition performed by `add` is simply a bit-wise addition +# The lowest 32 bits are kept, and no overflow bit is kept. +.section .text +.globl _start + +_start: + # Large positive values + # It is equal to + # 0b01111111111111111111111111111111 + + # 0b00000000000000000000000000000001 = + # 0b10000000000000000000000000000000 = + li t0, 2147483647 # t0 = 2147483647 (Max 32-bit signed int) + li t1, 1 # t1 = 1 + add t2, t0, t1 # t2 = t0 + t1 + + # 0b11111111111111111111111111111111 + + # 0b00000000000000000000000000000001 = + # 0b00000000000000000000000000000000 = + li t3, 0b11111111111111111111111111111111 + add t4, t3, t1 # t4 = t3 + t1 (Expected: overflow, wrap around) + + # 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 de5709fc23..7d3d7be7d2 100644 --- a/o1vm/tests/test_riscv_elf.rs +++ b/o1vm/tests/test_riscv_elf.rs @@ -158,3 +158,23 @@ fn test_add_2() { assert_eq!(witness.registers[T5], 912); // t5 = t0 + t2 assert_eq!(witness.registers[T6], 1368); // t6 = t4 + x0 (Copy t4 to t6) } + +#[test] +fn test_add_overflow() { + let curr_dir = std::env::current_dir().unwrap(); + let path = curr_dir.join(std::path::PathBuf::from( + "resources/programs/riscv32im/bin/add_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[T0], 0b01111111111111111111111111111111); + assert_eq!(witness.registers[T1], 0b00000000000000000000000000000001); + assert_eq!(witness.registers[T2], 0b10000000000000000000000000000000); + assert_eq!(witness.registers[T3], 0b11111111111111111111111111111111); + assert_eq!(witness.registers[T4], 0b00000000000000000000000000000000); +}