Skip to content

Commit

Permalink
fix: stack overflow with iterative tree traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
WilfredTA committed Aug 5, 2023
1 parent 9569aa4 commit 87a072e
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
components: rustfmt, clippy

- name: cargo test
run: cargo test
run: RUST_MIN_STACK=8388608 cargo test

- name: cargo fmt
run: cargo fmt --all -- --check
Expand Down
135 changes: 118 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ ethers-solc = { git = "https://github.com/gakonst/ethers-rs", features = ["full"
paste = "1.0.12"
rlp = "0.5.2"
serde = "1.0.164"
backtrace-on-stack-overflow = "0.3.0"
justerror = "1.1.0"
thiserror = "1.0.44"

[target.'cfg(any(target_os = "windows", target_os = "macos"))'.dependencies]
z3 = {version = "0.11.2", features = ["static-link-z3"]}
Expand Down
1 change: 0 additions & 1 deletion examples/simple_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use z3::ast::*;
pub const SIMPLE_TOKEN: &str = r#"608060405234801561001057600080fd5b50600436106100415760003560e01c806327e235e314610046578063a9059cbb14610078578063f8b2cb4f1461008d575b600080fd5b610066610054366004610143565b60006020819052908152604090205481565b60405190815260200160405180910390f35b61008b610086366004610165565b6100b6565b005b61006661009b366004610143565b6001600160a01b031660009081526020819052604090205490565b336000908152602081905260409020548111156100d257600080fd5b33600090815260208190526040812080548392906100f19084906101a5565b90915550506001600160a01b0382166000908152602081905260408120805483929061011e9084906101be565b90915550505050565b80356001600160a01b038116811461013e57600080fd5b919050565b60006020828403121561015557600080fd5b61015e82610127565b9392505050565b6000806040838503121561017857600080fd5b61018183610127565b946020939093013593505050565b634e487b7160e01b600052601160045260246000fd5b818103818111156101b8576101b861018f565b92915050565b808201808211156101b8576101b861018f56fea264697066735822122041d29a76727a31c9e4ec86703553a675712f98921d6c4bc9b23f274a963de18264736f6c63430008130033"#;

fn main() {
unsafe {backtrace_on_stack_overflow::enable();}
let pgm = Parser::with_pgm(SIMPLE_TOKEN).parse();
let mut evm = Evm::new(pgm);

Expand Down
7 changes: 3 additions & 4 deletions src/instruction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use super::smt::*;
#[Error]
pub enum InstructionError {
StackEmpty{
instruction: Instruction,
pc: usize,
}
}
Expand Down Expand Up @@ -252,16 +251,16 @@ impl Instruction {
}
impl<'ctx> MachineInstruction<'ctx, 32> for Instruction {
type Error = InstructionError;
fn exec(&self, mach: &EvmState) -> Result<MachineRecord<32>, InstructionError> {
fn exec(&self, mach: &EvmState) -> MachineRecord<32> {
match self {
Instruction::Stop => Ok(MachineRecord {
Instruction::Stop => MachineRecord {
halt: true,
stack: None,
mem: None,
constraints: None,
storage: None,
pc: (mach.pc(), mach.pc()),
}),
},
Instruction::Add => {
let stack = mach.stack();
let [stack_top, stack_top2] = stack.peek_top().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub trait Machine<const STACK_ITEM_SZ: usize> {

pub trait MachineInstruction<'ctx, const SZ: usize> {
type Error;
fn exec(&self, mach: &EvmState) -> Result<MachineRecord<SZ>, Self::Error>;
fn exec(&self, mach: &EvmState) -> MachineRecord<SZ>;
}

pub trait MachineComponent {
Expand Down
8 changes: 6 additions & 2 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ fn can_run_simple_storage_pgm() {

let execution = evm.exec();
let leaf = execution.states.leaves();
assert_eq!(2, leaf.len());
eprintln!("LEAVES: {:#?}", leaf);

{
let leaf = execution.states.leaves();
// as seen here https://bytegraph.xyz/bytecode/e5987a6f24f8af926faddae88de7980f/graph
assert_eq!(7, leaf.len());
}
let final_tree = leaf.get(1).unwrap().clone();
// eprintln!("FINAL TREE: {:#?}", final_tree);
let mut mem_val = final_tree.val.mem_read(bvi(64));
Expand Down

0 comments on commit 87a072e

Please sign in to comment.