Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-Becker committed Oct 16, 2024
1 parent 56ccb52 commit 1a20b3d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
1 change: 1 addition & 0 deletions crates/decompile/src/interfaces/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub struct AnalyzedFunction {
#[derive(Clone, Debug)]
pub struct StorageFrame {
pub operation: WrappedOpcode,
pub value: U256,
}

#[derive(Clone, Debug)]
Expand Down
37 changes: 20 additions & 17 deletions crates/decompile/src/utils/heuristics/extcall.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use alloy::primitives::U256;
use alloy_dyn_abi::{DynSolType, DynSolValue};
use heimdall_common::utils::strings::encode_hex_reduced;
use heimdall_common::utils::{
hex::ToLowerHex,
strings::{encode_hex, encode_hex_reduced},
};
use heimdall_vm::core::{opcodes::opcode_name, vm::State};

use crate::{
Expand All @@ -21,15 +24,20 @@ pub fn extcall_heuristic(
let gas = format!("gas: {}", instruction.input_operations[0].solidify());
let address = instruction.input_operations[1].solidify();
let value = format!("value: {}", instruction.input_operations[2].solidify());
let calldata = function.get_memory_range(instruction.inputs[3], instruction.inputs[4]);
let memory = function.get_memory_range(instruction.inputs[3], instruction.inputs[4]);
let extcalldata = memory
.iter()
.map(|x| x.value.to_lower_hex().trim_start_matches("0x").to_owned())
.collect::<Vec<String>>()
.join("");

// build the modifier w/ gas and value
let modifier = format!("{{ {}, {} }}", gas, value);

// check if the external call is a precompiled contract
match decode_precompile(
instruction.inputs[1],
&calldata,
&memory,
&instruction.input_operations[5],
) {
(true, precompile_logic) => {
Expand All @@ -38,13 +46,7 @@ pub fn extcall_heuristic(
_ => {
function.logic.push(format!(
"(bool success, bytes memory ret0) = address({}).call{}(abi.encode({}));",
address,
modifier,
calldata
.iter()
.map(|x| x.operation.solidify())
.collect::<Vec<String>>()
.join(", ")
address, modifier, extcalldata
));
}
}
Expand All @@ -54,15 +56,20 @@ pub fn extcall_heuristic(
0xfa | 0xf4 => {
let gas = format!("gas: {}", instruction.input_operations[0].solidify());
let address = instruction.input_operations[1].solidify();
let calldata = function.get_memory_range(instruction.inputs[2], instruction.inputs[3]);
let memory = function.get_memory_range(instruction.inputs[2], instruction.inputs[3]);
let extcalldata = memory
.iter()
.map(|x| x.value.to_lower_hex().trim_start_matches("0x").to_owned())
.collect::<Vec<String>>()
.join("");

// build the modifier w/ gas
let modifier = format!("{{ {} }}", gas);

// check if the external call is a precompiled contract
match decode_precompile(
instruction.inputs[1],
&calldata,
&memory,
&instruction.input_operations[4],
) {
(true, precompile_logic) => {
Expand All @@ -74,11 +81,7 @@ pub fn extcall_heuristic(
address,
opcode_name(instruction.opcode).to_lowercase(),
modifier,
calldata
.iter()
.map(|x| x.operation.solidify())
.collect::<Vec<String>>()
.join(", ")
extcalldata
));
}
}
Expand Down
11 changes: 6 additions & 5 deletions crates/decompile/src/utils/heuristics/solidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ pub fn solidity_heuristic(
// MSTORE / MSTORE8
0x52 | 0x53 => {
let key = instruction.inputs[0];
let value = instruction.inputs[1];
let operation = instruction.input_operations[1].to_owned();

// add the mstore to the function's memory map
function.memory.insert(key, StorageFrame { operation });
function.memory.insert(key, StorageFrame { operation, value });
function.logic.push(format!(
"memory[{}] = {};",
encode_hex_reduced(key),
Expand All @@ -95,10 +96,10 @@ pub fn solidity_heuristic(

// perform a series of checks to determine if the condition
// is added by the compiler and can be ignored
if (conditional.contains("msg.data.length") && conditional.contains("0x04")) ||
VARIABLE_SIZE_CHECK_REGEX.is_match(&conditional).unwrap_or(false) ||
(conditional.replace('!', "") == "success") ||
(conditional == "!msg.value")
if (conditional.contains("msg.data.length") && conditional.contains("0x04"))
|| VARIABLE_SIZE_CHECK_REGEX.is_match(&conditional).unwrap_or(false)
|| (conditional.replace('!', "") == "success")
|| (conditional == "!msg.value")
{
return Ok(());
}
Expand Down
7 changes: 4 additions & 3 deletions crates/decompile/src/utils/heuristics/yul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ pub fn yul_heuristic(
// MSTORE / MSTORE8
0x52 | 0x53 => {
let key = instruction.inputs[0];
let value = instruction.inputs[1];
let operation = instruction.input_operations[1].clone();

// add the mstore to the function's memory map
function.memory.insert(key, StorageFrame { operation });
function.memory.insert(key, StorageFrame { operation, value });
function.logic.push(format!(
"{}({}, {})",
opcode_name(instruction.opcode).to_lowercase(),
Expand Down Expand Up @@ -76,8 +77,8 @@ pub fn yul_heuristic(
// CALLDATACOPY, CODECOPY, EXTCODECOPY, RETURNDATACOPY, TSTORE,
// SSTORE, RETURN, SELFDESTRUCT, LOG0, LOG1, LOG2, LOG3, LOG4
// we simply want to add the operation to the function's logic
0x37 | 0x39 | 0x3c | 0x3e | 0x55 | 0x5d | 0xf0 | 0xf1 | 0xf2 | 0xf4 | 0xf5 | 0xfa |
0xff | 0xA0 | 0xA1 | 0xA2 | 0xA3 | 0xA4 => {
0x37 | 0x39 | 0x3c | 0x3e | 0x55 | 0x5d | 0xf0 | 0xf1 | 0xf2 | 0xf4 | 0xf5 | 0xfa
| 0xff | 0xA0 | 0xA1 | 0xA2 | 0xA3 | 0xA4 => {
function.logic.push(format!(
"{}({})",
opcode_name(instruction.opcode).to_lowercase(),
Expand Down

0 comments on commit 1a20b3d

Please sign in to comment.