-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore(mmu): add overflow handling (#452)
* enchance(mmu): add stack/heap overflow handling in trace_store * test(mmu): add tests * chore(mmu): cleanup * chore(mmu): separate address assertion * doc(mmu): add comments * chore(mmu): add termination bit to assertion * feat(example): add overflow stack example * feat(example): add heap example and valid case * chore(mmu): cleanup * chore(examples): fix naming * chore: fix clippy warn * chore: fix build warn
- Loading branch information
Showing
8 changed files
with
197 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "overflow-guest" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[features] | ||
guest = [] | ||
|
||
[dependencies] | ||
jolt = { package = "jolt-sdk", path = "../../../jolt-sdk" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#![cfg_attr(feature = "guest", no_std)] | ||
|
||
extern crate alloc; | ||
use alloc::vec; | ||
use alloc::vec::Vec; | ||
|
||
#[jolt::provable(stack_size = 1024)] | ||
fn overflow_stack() -> u32 { | ||
let arr = [1u32; 1024]; | ||
arr.iter().sum() | ||
} | ||
|
||
#[jolt::provable(stack_size = 8192)] | ||
fn allocate_stack_with_increased_size() -> u32 { | ||
overflow_stack() | ||
} | ||
|
||
#[jolt::provable(memory_size = 4096)] | ||
fn overflow_heap() -> u32 { | ||
let mut vectors = Vec::new(); | ||
|
||
loop { | ||
let v = vec![1u32; 1024]; | ||
vectors.extend(v); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#![cfg_attr(feature = "guest", no_std)] | ||
#![no_main] | ||
|
||
#[allow(unused_imports)] | ||
use overflow_guest::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::any::Any; | ||
use std::panic; | ||
|
||
pub fn main() { | ||
let (prove_overflow_stack, _) = guest::build_overflow_stack(); | ||
|
||
let res = panic::catch_unwind(|| { | ||
// trying to allocate 1024 elems array and sum it up | ||
// with stack_size=1024, should panic | ||
let (_, _) = prove_overflow_stack(); | ||
}); | ||
handle_result(res); | ||
|
||
// now lets try to overflow the heap, should also panic | ||
let (prove_overflow_heap, _) = guest::build_overflow_heap(); | ||
|
||
let res = panic::catch_unwind(|| { | ||
let (_, _) = prove_overflow_heap(); | ||
}); | ||
handle_result(res); | ||
|
||
// valid case for stack allocation, calls overflow_stack() under the hood | ||
// but with stack_size=8192 | ||
let (prove_allocate_stack_with_increased_size, verfiy_allocate_stack_with_increased_size) = | ||
guest::build_allocate_stack_with_increased_size(); | ||
|
||
let (output, proof) = prove_allocate_stack_with_increased_size(); | ||
let is_valid = verfiy_allocate_stack_with_increased_size(proof); | ||
|
||
println!("output: {}", output); | ||
println!("valid: {}", is_valid); | ||
} | ||
|
||
fn handle_result(res: Result<(), Box<dyn Any + Send>>) { | ||
if let Err(e) = &res { | ||
if let Some(msg) = e.downcast_ref::<String>() { | ||
println!("--> Panic occurred with message: {}\n", msg); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters