Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-Becker committed Dec 23, 2024
1 parent fc8e124 commit 4767941
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
2 changes: 0 additions & 2 deletions crates/decompile/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
pub enum Error {
#[error("Fetch error: {0}")]
FetchError(String),
#[error("Disassembly error: {0}")]
DisassemblyError(#[from] heimdall_disassembler::Error),
#[error("Internal error: {0}")]
Eyre(#[from] eyre::Report),
}
39 changes: 17 additions & 22 deletions crates/vm/src/core/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct Vm {
pub gas_used: u128,
pub events: Vec<Log>,
pub returndata: Vec<u8>,
pub exitcode: u128,
pub stopped: bool,
pub address_access_set: HashSet<U256>,
#[cfg(feature = "step-tracing")]
pub operation_count: u128,
Expand All @@ -57,7 +57,6 @@ pub struct ExecutionResult {
pub gas_used: u128,
pub gas_remaining: u128,
pub returndata: Vec<u8>,
pub exitcode: u128,
pub events: Vec<Log>,
pub instruction: u128,
}
Expand Down Expand Up @@ -130,7 +129,7 @@ impl Vm {
gas_used: 21000,
events: Vec::new(),
returndata: Vec::new(),
exitcode: 255,
stopped: false,
address_access_set: HashSet::new(),
#[cfg(feature = "step-tracing")]
operation_count: 0,
Expand Down Expand Up @@ -177,8 +176,8 @@ impl Vm {
/// vm.exit(0xff, Vec::new());
/// assert_eq!(vm.exitcode, 0xff);
/// ```
pub fn exit(&mut self, code: u128, returndata: Vec<u8>) {
self.exitcode = code;
pub fn exit(&mut self, returndata: Vec<u8>) {
self.stopped = true;
self.returndata = returndata;
}

Expand Down Expand Up @@ -210,7 +209,7 @@ impl Vm {
if amount > self.gas_remaining {
self.gas_used += self.gas_remaining;
self.gas_remaining = 0;
self.exit(9, Vec::new());
self.exit(Vec::new());
return false;
}

Expand Down Expand Up @@ -242,7 +241,7 @@ impl Vm {
fn _step(&mut self) -> Result<Instruction> {
// sanity check
if self.bytecode.len() < self.instruction as usize {
self.exit(2, Vec::new());
self.exit(Vec::new());
return Ok(Instruction {
instruction: self.instruction,
opcode: 0xff,
Expand Down Expand Up @@ -302,7 +301,7 @@ impl Vm {
match opcode {
// STOP
0x00 => {
self.exit(10, Vec::new());
self.exit(Vec::new());
return Ok(Instruction {
instruction: last_instruction,
opcode,
Expand Down Expand Up @@ -1148,7 +1147,7 @@ impl Vm {
.expect("impossible case: bytecode is larger than u128::MAX"))
&& (self.bytecode[pc as usize] != 0x5b)
{
self.exit(790, Vec::new());
self.exit(Vec::new());
return Ok(Instruction {
instruction: last_instruction,
opcode,
Expand Down Expand Up @@ -1181,7 +1180,7 @@ impl Vm {
.expect("impossible case: bytecode is larger than u128::MAX"))
&& (self.bytecode[pc as usize] != 0x5b)
{
self.exit(790, Vec::new());
self.exit(Vec::new());
return Ok(Instruction {
instruction: last_instruction,
opcode,
Expand Down Expand Up @@ -1373,7 +1372,7 @@ impl Vm {
let gas_cost = self.memory.expansion_cost(offset, size);
self.consume_gas(gas_cost);

self.exit(0, self.memory.read(offset, size));
self.exit(self.memory.read(offset, size));
}

// DELEGATECALL, STATICCALL
Expand Down Expand Up @@ -1408,12 +1407,12 @@ impl Vm {
let offset: usize = offset.try_into()?;
let size: usize = size.try_into()?;

self.exit(1, self.memory.read(offset, size));
self.exit(self.memory.read(offset, size));
}

// INVALID & SELFDESTRUCT
_ => {
self.exit(1, Vec::new());
self.exit(Vec::new());
}
}

Expand Down Expand Up @@ -1515,10 +1514,7 @@ impl Vm {
let mut vm_clone = self.clone();

for _ in 0..n {
if vm_clone.bytecode.len() < vm_clone.instruction as usize
|| vm_clone.exitcode != 255
|| !vm_clone.returndata.is_empty()
{
if vm_clone.bytecode.len() < vm_clone.instruction as usize || vm_clone.stopped {
break;
}
states.push(vm_clone.step()?);
Expand Down Expand Up @@ -1557,7 +1553,7 @@ impl Vm {
self.gas_used = 21000;
self.events = Vec::new();
self.returndata = Vec::new();
self.exitcode = 255;
self.stopped = false;
}

/// Executes the code until finished
Expand All @@ -1583,7 +1579,7 @@ impl Vm {
while self.bytecode.len() >= self.instruction as usize {
self.step()?;

if self.exitcode != 255 || !self.returndata.is_empty() {
if self.stopped {
break;
}
}
Expand All @@ -1592,7 +1588,6 @@ impl Vm {
gas_used: self.gas_used,
gas_remaining: self.gas_remaining,
returndata: self.returndata.to_owned(),
exitcode: self.exitcode,
events: self.events.clone(),
instruction: self.instruction,
})
Expand Down Expand Up @@ -1662,7 +1657,7 @@ mod tests {
vm.execute().expect("execution failed!");

assert!(vm.returndata.is_empty());
assert_eq!(vm.exitcode, 10);
assert!(vm.stopped);
}

#[test]
Expand All @@ -1671,7 +1666,7 @@ mod tests {
vm.execute().expect("execution failed!");

assert!(vm.returndata.is_empty());
assert_eq!(vm.exitcode, 255);
assert!(vm.stopped);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/ext/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Vm {
self.step()?;

// this shouldn't be necessary, but it's safer to have it
if self.exitcode != 255 || !self.returndata.is_empty() {
if self.stopped {
break;
}
}
Expand Down Expand Up @@ -316,7 +316,7 @@ impl Vm {
}

// when the vm exits, this path is complete
if vm.exitcode != 255 || !vm.returndata.is_empty() {
if vm.stopped {
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/ext/selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::core::vm::Vm;

/// find all function selectors in the given EVM bytecode.
pub fn find_function_selectors(evm: &Vm) -> HashMap<String, u128> {
todo!();
todo!()
}

#[cfg(test)]
Expand Down

0 comments on commit 4767941

Please sign in to comment.