From 071deec1c31748e79d2c19443848b99938aee340 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Wed, 3 Sep 2025 13:18:09 +0200 Subject: [PATCH] ere-risc0: allocator alignments test (#120) Signed-off-by: Ignacio Hagopian --- crates/ere-risc0/src/lib.rs | 22 ++++++++++++++++++++++ tests/risc0/allocs_alignment/Cargo.toml | 13 +++++++++++++ tests/risc0/allocs_alignment/src/main.rs | 11 +++++++++++ 3 files changed, 46 insertions(+) create mode 100644 tests/risc0/allocs_alignment/Cargo.toml create mode 100644 tests/risc0/allocs_alignment/src/main.rs diff --git a/crates/ere-risc0/src/lib.rs b/crates/ere-risc0/src/lib.rs index 8ed5f5c..ded9148 100644 --- a/crates/ere-risc0/src/lib.rs +++ b/crates/ere-risc0/src/lib.rs @@ -341,4 +341,26 @@ mod tests { zkvm.prove(&inputs).unwrap_err(); } } + + #[test] + fn test_aligned_allocs() { + let program = RV32_IM_RISC0_ZKVM_ELF + .compile(&testing_guest_directory("risc0", "allocs_alignment")) + .unwrap(); + + for i in 1..=16_usize { + let zkvm = EreRisc0::new(program.clone(), ProverResourceType::Cpu).unwrap(); + + let mut input = Input::new(); + input.write(i); + + if i.is_power_of_two() { + zkvm.execute(&input) + .expect("Power of two alignment should execute successfully"); + } else { + zkvm.execute(&input) + .expect_err("Non-power of two aligment is expected to fail"); + } + } + } } diff --git a/tests/risc0/allocs_alignment/Cargo.toml b/tests/risc0/allocs_alignment/Cargo.toml new file mode 100644 index 0000000..9f8df7f --- /dev/null +++ b/tests/risc0/allocs_alignment/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "ere-test-risc0-allocs-alignment" +version = "0.1.0" +edition = "2021" + +[workspace] + +[dependencies] +risc0-zkvm = { version = "3.0.1", default-features = false, features = [ + "std", + "unstable", +] } +risc0-zkvm-platform = { version = "=2.0.4" } diff --git a/tests/risc0/allocs_alignment/src/main.rs b/tests/risc0/allocs_alignment/src/main.rs new file mode 100644 index 0000000..2f7f764 --- /dev/null +++ b/tests/risc0/allocs_alignment/src/main.rs @@ -0,0 +1,11 @@ +use risc0_zkvm::guest::env; + +fn main() { + let alignment = env::read::(); + + let layout = std::alloc::Layout::from_size_align(1, alignment).unwrap(); + let ptr = unsafe { std::alloc::alloc(layout) }; + if ptr.is_null() { + panic!("allocation failed"); + } +}