Skip to content

Commit

Permalink
feat(example): add overflow stack example
Browse files Browse the repository at this point in the history
  • Loading branch information
4rgon4ut committed Oct 23, 2024
1 parent 2d96ea7 commit ee22f69
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "jolt"
version = "0.1.0"
authors = [
# author of original Spartan paper and code base
# author of original Spartan paper and code base
"Srinath Setty <srinath@microsoft.com>",
# authors who contributed to the Arkworks Spartan fork
"Zhenfei Zhang <zhenfei.zhang@hotmail.com>",
Expand Down Expand Up @@ -46,6 +46,10 @@ members = [
"examples/stdlib/guest",
"examples/muldiv",
"examples/muldiv/guest",
"examples/overflow",
"examples/overflow/guest",


]

[features]
Expand Down
8 changes: 8 additions & 0 deletions examples/overflow/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "overflow"
version = "0.1.0"
edition = "2021"

[dependencies]
jolt-sdk = { path = "../../jolt-sdk", features = ["host"] }
guest = { package = "overflow-guest", path = "./guest" }
14 changes: 14 additions & 0 deletions examples/overflow/guest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "overflow-guest"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "guest"
path = "./src/lib.rs"

[features]
guest = []

[dependencies]
jolt = { package = "jolt-sdk", path = "../../../jolt-sdk" }
12 changes: 12 additions & 0 deletions examples/overflow/guest/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![cfg_attr(feature = "guest", no_std)]
#![no_main]

const ARRAY_SIZE: usize = 2048;

#[jolt::provable(stack_size = 1024)]
fn overflow_stack() -> u32 {
let arr = [1; ARRAY_SIZE];

let sum = arr.iter().fold(0u32, |acc, &x| acc.wrapping_add(x));
sum
}
21 changes: 21 additions & 0 deletions examples/overflow/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::panic;

pub fn main() {
let (prove_overflow_atack, verify_overflow_stack) = guest::build_overflow_stack();

let result = panic::catch_unwind(|| {
let (_, proof) = prove_overflow_atack();
verify_overflow_stack(proof);
});

match &result {
Err(e) => match e.downcast_ref::<String>() {
Some(msg) => println!("Panic occurred with message: {}", msg),
None => println!("Panic occurred with unknown message type"),
},
Ok(_) => println!("No panic occurred"),
}

// println!("output: {}", output);
// println!("valid: {}", is_valid);
}
7 changes: 5 additions & 2 deletions tracer/src/emulator/mmu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl Mmu {
// less then DRAM_BASE and greater then panic => zero_padding region
assert!(
effective_address <= self.jolt_device.memory_layout.termination,
"Stack overflow: Attempted to write to 0x{:X}, which is in the area or zero padding region.",
"Stack overflow: Attempted to write to 0x{:X}, which is in the area of zero padding region.",
effective_address
);
// less then panic => jolt_device region (i.e. input/output)
Expand Down Expand Up @@ -673,7 +673,10 @@ impl Mmu {
0x0c000000..=0x0fffffff => self.plic.store(effective_address, value),
0x10000000..=0x100000ff => self.uart.store(effective_address, value),
0x10001000..=0x10001FFF => self.disk.store(effective_address, value),
_ => self.assert_effective_address(effective_address),
_ => {
self.assert_effective_address(effective_address);
self.jolt_device.store(effective_address, value);
}
},
};
}
Expand Down

0 comments on commit ee22f69

Please sign in to comment.