From c4f98553051cbf7fc0443d2d6b68df17927eed86 Mon Sep 17 00:00:00 2001 From: Jonathan Becker Date: Sat, 2 Dec 2023 01:40:33 -0500 Subject: [PATCH] chore(logging): use macro `debug_max!(..)` throughout codebase (#215) --- cache/src/lib.rs | 4 +- cache/src/util.rs | 6 +- cli/src/output.rs | 4 +- common/src/ether/compiler.rs | 13 ++--- common/src/ether/evm/core/types.rs | 20 +++---- common/src/ether/evm/ext/exec/mod.rs | 57 +++++++++---------- common/src/ether/evm/ext/exec/util.rs | 40 +++++-------- common/src/ether/rpc.rs | 8 +-- common/src/ether/selectors.rs | 27 ++++----- common/src/ether/signatures.rs | 44 +++++--------- common/src/utils/http.rs | 15 ++--- common/src/utils/io/logging.rs | 43 +++++++------- common/src/utils/io/macros.rs | 4 +- common/src/utils/strings.rs | 14 ++--- core/src/cfg/mod.rs | 11 ++-- core/src/decompile/analyzers/yul.rs | 8 +-- core/src/decompile/mod.rs | 15 ++--- core/src/decompile/out/abi.rs | 8 +-- .../decompile/out/postprocessers/solidity.rs | 54 +++++++++--------- core/src/decompile/out/postprocessers/yul.rs | 20 +++---- core/src/decompile/resolve.rs | 35 +++++------- core/src/dump/util/mod.rs | 7 ++- core/src/dump/util/threads/indexer.rs | 2 +- core/src/dump/util/threads/tui.rs | 10 ++-- core/src/snapshot/analyze.rs | 6 +- core/src/snapshot/mod.rs | 10 ++-- core/src/snapshot/resolve.rs | 35 +++++------- core/src/snapshot/util/tui.rs | 4 +- 28 files changed, 236 insertions(+), 288 deletions(-) diff --git a/cache/src/lib.rs b/cache/src/lib.rs index 226e392f..ae8e575e 100644 --- a/cache/src/lib.rs +++ b/cache/src/lib.rs @@ -197,7 +197,7 @@ where let binary_vec = decode_hex(&binary_string); if binary_vec.is_err() { - return None + return None; } let cache: Cache = match bincode::deserialize::>(&binary_vec.unwrap()) { @@ -210,7 +210,7 @@ where .as_secs() { delete_cache(key); - return None + return None; } c diff --git a/cache/src/util.rs b/cache/src/util.rs index 138a8dda..7bbf7cbc 100644 --- a/cache/src/util.rs +++ b/cache/src/util.rs @@ -54,13 +54,13 @@ pub fn prettify_bytes(bytes: u64) -> String { format!("{bytes} B") } else if bytes < 1024 * 1024 { let kb = bytes / 1024; - return format!("{kb} KB") + return format!("{kb} KB"); } else if bytes < 1024 * 1024 * 1024 { let mb = bytes / (1024 * 1024); - return format!("{mb} MB") + return format!("{mb} MB"); } else { let gb = bytes / (1024 * 1024 * 1024); - return format!("{gb} GB") + return format!("{gb} GB"); } } diff --git a/cli/src/output.rs b/cli/src/output.rs index 365f5e5c..1eac0dc0 100644 --- a/cli/src/output.rs +++ b/cli/src/output.rs @@ -21,9 +21,9 @@ pub async fn build_output_path( if ADDRESS_REGEX.is_match(target)? { let chain_id = rpc::chain_id(rpc_url).await?; - return Ok(format!("{}/output/{}/{}/{}", cwd, chain_id, target, filename)) + return Ok(format!("{}/output/{}/{}/{}", cwd, chain_id, target, filename)); } else { - return Ok(format!("{}/output/local/{}", cwd, filename)) + return Ok(format!("{}/output/local/{}", cwd, filename)); } } diff --git a/common/src/ether/compiler.rs b/common/src/ether/compiler.rs index 36aa91ae..dda8e8ad 100644 --- a/common/src/ether/compiler.rs +++ b/common/src/ether/compiler.rs @@ -1,11 +1,8 @@ -use crate::utils::io::logging::Logger; +use crate::debug_max; // returns the compiler version used to compile the contract. // for example: (solc, 0.8.10) or (vyper, 0.2.16) pub fn detect_compiler(bytecode: &str) -> (&'static str, String) { - // get a new logger - let logger = Logger::default(); - let mut compiler = "unknown"; let mut version = "unknown".to_string(); @@ -62,10 +59,10 @@ pub fn detect_compiler(bytecode: &str) -> (&'static str, String) { .join("."); } - logger.debug_max(&format!( + debug_max!( "exact compiler version match found due to cbor encoded metadata: {}", version - )); + ); } } else if compiler == "vyper" { let compiler_version = bytecode.split("767970657283").collect::>(); @@ -90,10 +87,10 @@ pub fn detect_compiler(bytecode: &str) -> (&'static str, String) { .join("."); } - logger.debug_max(&format!( + debug_max!( "exact compiler version match found due to cbor encoded metadata: {}", version - )); + ); } } diff --git a/common/src/ether/evm/core/types.rs b/common/src/ether/evm/core/types.rs index 9060eff9..752e1537 100644 --- a/common/src/ether/evm/core/types.rs +++ b/common/src/ether/evm/core/types.rs @@ -31,7 +31,7 @@ pub fn parse_function_parameters(function_signature: &str) -> Option Option> { // if string is empty, return None if string.is_empty() { - return None + return None; } // if the string contains a tuple we cant simply split on commas @@ -57,7 +57,7 @@ fn extract_types_from_string(string: &str) -> Option> { // get balanced encapsulator let (tuple_start, tuple_end, valid) = find_balanced_encapsulator(string, ('(', ')')); if !valid { - return None + return None; } // extract the tuple @@ -78,7 +78,7 @@ fn extract_types_from_string(string: &str) -> Option> { if is_array { let (start, end, valid) = find_balanced_encapsulator(split, ('[', ']')); if !valid { - return None + return None; } let size = split[start + 1..end - 1].to_string(); @@ -156,7 +156,7 @@ fn extract_types_from_string(string: &str) -> Option> { // iterate over the split string and convert each type to a ParamType for string_type in split { if string_type.is_empty() { - continue + continue; } let param_type = to_type(string_type); @@ -191,7 +191,7 @@ pub fn to_type(string: &str) -> ParamType { while string.ends_with(']') { let (start, end, valid) = find_balanced_encapsulator(&string, ('[', ']')); if !valid { - return ParamType::Bytes // default to bytes if invalid + return ParamType::Bytes; // default to bytes if invalid } let size = string[start + 1..end - 1].to_string(); @@ -343,17 +343,17 @@ pub fn get_padding(bytes: &str) -> Padding { if null_byte_indices.is_empty() || null_byte_indices[0] != 0 && null_byte_indices[null_byte_indices.len() - 1] != size - 1 { - return Padding::None + return Padding::None; } // the first byte is a null byte AND the last byte is not a null byte, it is left padded if null_byte_indices[0] == 0 && null_byte_indices[null_byte_indices.len() - 1] != size - 1 { - return Padding::Left + return Padding::Left; } // the first byte is not a null byte AND the last byte is a null byte, it is right padded if null_byte_indices[0] != 0 && null_byte_indices[null_byte_indices.len() - 1] == size - 1 { - return Padding::Right + return Padding::Right; } // get non-null byte indices @@ -365,7 +365,7 @@ pub fn get_padding(bytes: &str) -> Padding { .collect::>(); if non_null_byte_indices.is_empty() { - return Padding::None + return Padding::None; } // check if the there are more null-bytes before the first non-null byte than after the last diff --git a/common/src/ether/evm/ext/exec/mod.rs b/common/src/ether/evm/ext/exec/mod.rs index 2667501c..c3607650 100644 --- a/common/src/ether/evm/ext/exec/mod.rs +++ b/common/src/ether/evm/ext/exec/mod.rs @@ -7,6 +7,7 @@ use self::util::{ stack_diff, stack_item_source_depth_too_deep, }; use crate::{ + debug_max, ether::evm::core::{ stack::Stack, vm::{State, VM}, @@ -36,14 +37,13 @@ impl VM { // this shouldn't be necessary, but it's safer to have it if self.exitcode != 255 || !self.returndata.is_empty() { - break + break; } } // get a new logger let logger = Logger::default(); - - logger.debug_max(&format!("beginning symbolic execution for selector 0x{}", selector)); + debug_max!("beginning symbolic execution for selector 0x{}", selector); // the VM is at the function entry point, begin tracing let mut branch_count = 0; @@ -56,8 +56,7 @@ impl VM { // get a new logger let logger = Logger::default(); - - logger.debug_max("beginning contract-wide symbolic execution"); + debug_max!("beginning contract-wide symbolic execution"); // the VM is at the function entry point, begin tracing let mut branch_count = 0; @@ -92,10 +91,10 @@ impl VM { // if we encounter a JUMPI, create children taking both paths and break if state.last_instruction.opcode == 0x57 { - logger.debug_max(&format!( + debug_max!( "found branch due to JUMPI instruction at {}", state.last_instruction.instruction - )); + ); // jump frame contains: // 1. the instruction (PC) of the JUMPI @@ -111,13 +110,13 @@ impl VM { // if the stack has over 16 items of the same source, it's probably a loop if stack_contains_too_many_of_the_same_item(&vm.stack) { - return vm_trace + return vm_trace; } // if any item on the stack has a depth > 16, it's probably a loop (because of stack // too deep) if stack_item_source_depth_too_deep(&vm.stack) { - return vm_trace + return vm_trace; } // break out of loops @@ -132,7 +131,7 @@ impl VM { // check if any historical stack is the same as the current stack if hist_stack == &vm.stack { - logger.debug_max( + debug_max!( "jump matches loop-detection heuristic: 'jump_path_already_handled'" ); return true @@ -143,13 +142,13 @@ impl VM { if stack_diff.is_empty() { // the stack_diff is empty (the stacks are the same), so we've // already handled this path - logger.debug_max( + debug_max!( "jump matches loop-detection heuristic: 'stack_diff_is_empty'" ); return true } - logger.debug_max(&format!("stack diff: [{}]", stack_diff.iter().map(|frame| format!("{}", frame.value)).collect::>().join(", "))); + debug_max!("stack diff: [{}]", stack_diff.iter().map(|frame| format!("{}", frame.value)).collect::>().join(", ")); // check if the jump condition appears to be recursive if jump_condition_appears_recursive(&stack_diff, &jump_condition) { @@ -174,12 +173,12 @@ impl VM { false }) { - logger.debug_max("jump terminated."); - logger.debug_max(&format!( + debug_max!("jump terminated."); + debug_max!( "adding historical stack {} to jump frame {:?}", &format!("{:#016x?}", vm.stack.hash()), jump_frame - )); + ); // this key exists, but the stack is different, so the jump is new historical_stacks.push(vm.stack.clone()); @@ -190,28 +189,28 @@ impl VM { &vm.stack, historical_stacks, ) { - logger.debug_max("jump terminated."); - logger.debug_max(&format!( + debug_max!("jump terminated."); + debug_max!( "adding historical stack {} to jump frame {:?}", &format!("{:#016x?}", vm.stack.hash()), jump_frame - )); + ); // this key exists, but the stack is different, so the jump is new historical_stacks.push(vm.stack.clone()); - return vm_trace + return vm_trace; } else { - logger.debug_max(&format!( + debug_max!( "adding historical stack {} to jump frame {:?}", &format!("{:#016x?}", vm.stack.hash()), jump_frame - )); - logger.debug_max(&format!( + ); + debug_max!( " - jump condition: {}\n - stack: {}\n - historical stacks: {}", state.last_instruction.input_operations[1].solidify(), vm.stack, historical_stacks.iter().map(|stack| format!("{}", stack)).collect::>().join("\n - ") - )); + ); // this key exists, but the stack is different, so the jump is new historical_stacks.push(vm.stack.clone()); @@ -219,18 +218,18 @@ impl VM { } None => { // this key doesnt exist, so the jump is new - logger.debug_max(&format!("added new jump frame: {:?}", jump_frame)); + debug_max!("added new jump frame: {:?}", jump_frame); handled_jumps.insert(jump_frame, vec![vm.stack.clone()]); } } // we didnt break out, so now we crate branching paths to cover all possibilities *branch_count += 1; - logger.debug_max(&format!( + debug_max!( "creating branching paths at instructions {} (JUMPDEST) and {} (CONTINUE)", state.last_instruction.inputs[0], state.last_instruction.instruction + 1 - )); + ); // we need to create a trace for the path that wasn't taken. if state.last_instruction.inputs[1].is_zero() { @@ -245,7 +244,7 @@ impl VM { // push the current path onto the stack vm_trace.children.push(vm.recursive_map(branch_count, handled_jumps, logger)); - break + break; } else { // push a new vm trace to the children let mut trace_vm = vm.clone(); @@ -258,13 +257,13 @@ impl VM { // push the current path onto the stack vm_trace.children.push(vm.recursive_map(branch_count, handled_jumps, logger)); - break + break; } } // when the vm exits, this path is complete if vm.exitcode != 255 || !vm.returndata.is_empty() { - break + break; } } diff --git a/common/src/ether/evm/ext/exec/util.rs b/common/src/ether/evm/ext/exec/util.rs index 0e46d57f..8db1ecbc 100644 --- a/common/src/ether/evm/ext/exec/util.rs +++ b/common/src/ether/evm/ext/exec/util.rs @@ -2,6 +2,7 @@ use ethers::types::U256; use crate::{ constants::{MEMORY_REGEX, STORAGE_REGEX}, + debug_max, ether::evm::core::stack::{Stack, StackFrame}, utils::io::logging::Logger, }; @@ -30,13 +31,10 @@ pub fn stack_contains_too_many_of_the_same_item(stack: &Stack) -> bool { stack.stack.iter().filter(|f| f.operation.solidify() == solidified_frame_source).count() >= 16 }) { - // get a new logger - let logger = Logger::default(); - - logger.debug_max( + debug_max!( "jump matches loop-detection heuristic: 'stack_contains_too_many_of_the_same_item'", ); - return true + return true; } false @@ -52,7 +50,7 @@ pub fn stack_item_source_depth_too_deep(stack: &Stack) -> bool { logger .debug_max("jump matches loop-detection heuristic: 'stack_item_source_depth_too_deep'"); - return true + return true; } false @@ -72,7 +70,7 @@ pub fn jump_condition_appears_recursive(stack_diff: &[StackFrame], jump_conditio logger .debug_max("jump matches loop-detection heuristic: 'jump_condition_appears_recursive'"); - return true + return true; } false @@ -87,18 +85,15 @@ pub fn jump_condition_contains_mutated_memory_access( if stack_diff.iter().any(|frame| { memory_accesses.any(|_match| { if _match.is_err() { - return false + return false; } let memory_access = _match.unwrap(); let slice = &jump_condition[memory_access.start()..memory_access.end()]; frame.operation.solidify().contains(slice) }) }) { - // get a new logger - let logger = Logger::default(); - - logger.debug_max("jump matches loop-detection heuristic: 'jump_condition_contains_mutated_memory_access'"); - return true + debug_max!("jump matches loop-detection heuristic: 'jump_condition_contains_mutated_memory_access'"); + return true; } false @@ -113,18 +108,15 @@ pub fn jump_condition_contains_mutated_storage_access( if stack_diff.iter().any(|frame| { storage_accesses.any(|_match| { if _match.is_err() { - return false + return false; } let storage_access = _match.unwrap(); let slice = &jump_condition[storage_access.start()..storage_access.end()]; frame.operation.solidify().contains(slice) }) }) { - // get a new logger - let logger = Logger::default(); - - logger.debug_max("jump matches loop-detection heuristic: 'jump_condition_contains_mutated_storage_access'"); - return true + debug_max!("jump matches loop-detection heuristic: 'jump_condition_contains_mutated_storage_access'"); + return true; } false @@ -138,7 +130,7 @@ pub fn jump_condition_historical_diffs_approximately_equal( // break if historical_stacks.len() < 4 // this is an arbitrary number, i picked it randomly :D if historical_stacks.len() < 4 { - return false + return false; } // get the stack diffs for all historical stacks @@ -154,17 +146,15 @@ pub fn jump_condition_historical_diffs_approximately_equal( // check if all stack diffs are exactly length 1 if !stack_diffs.iter().all(|diff| diff.len() == 1) { - return false + return false; } // check if all stack diffs are the same if !stack_diffs.iter().all(|diff| diff[0] == stack_diffs[0][0]) { - return false + return false; } - // get a new logger - let logger = Logger::default(); - logger.debug_max("jump matches loop-detection heuristic: 'jump_condition_historical_diffs_approximately_equal'"); + debug_max!("jump matches loop-detection heuristic: 'jump_condition_historical_diffs_approximately_equal'"); true } diff --git a/common/src/ether/rpc.rs b/common/src/ether/rpc.rs index 55103e02..74aee27e 100644 --- a/common/src/ether/rpc.rs +++ b/common/src/ether/rpc.rs @@ -1,6 +1,6 @@ use std::{str::FromStr, time::Duration}; -use crate::utils::io::logging::Logger; +use crate::{debug_max, utils::io::logging::Logger}; use backoff::ExponentialBackoff; use ethers::{ core::types::Address, @@ -27,7 +27,7 @@ pub async fn chain_id(rpc_url: &str) -> Result> // get a new logger let logger = Logger::default(); - logger.debug_max(&format!("checking chain id for rpc url: '{}'", &rpc_url)); +debug_max!(&format!("checking chain id for rpc url: '{}'", &rpc_url)); // check the cache for a matching rpc url let cache_key = format!("chain_id.{}", &rpc_url.replace('/', "").replace(['.', ':'], "-")); @@ -63,7 +63,7 @@ pub async fn chain_id(rpc_url: &str) -> Result> // cache the results store_cache(&cache_key, chain_id.as_u64(), None); - logger.debug_max(&format!("chain_id is '{}'", &chain_id)); + debug_max!(&format!("chain_id is '{}'", &chain_id)); Ok(chain_id.as_u64()) }) @@ -171,7 +171,7 @@ pub async fn get_transaction( // get a new logger let logger = Logger::default(); - logger.debug_max(&format!( +debug_max!(&format!( "fetching calldata from node for transaction: '{}' .", &transaction_hash )); diff --git a/common/src/ether/selectors.rs b/common/src/ether/selectors.rs index 9bb537fc..0de14478 100644 --- a/common/src/ether/selectors.rs +++ b/common/src/ether/selectors.rs @@ -10,15 +10,13 @@ use tokio::task; use crate::utils::{io::logging::Logger, strings::decode_hex}; use super::{evm::core::vm::VM, signatures::ResolveSelector}; +use crate::debug_max; /// find all function selectors in the given EVM assembly. pub fn find_function_selectors(evm: &VM, assembly: &str) -> HashMap { let mut function_selectors = HashMap::new(); let mut handled_selectors = HashSet::new(); - // get a new logger - let logger = Logger::default(); - // search through assembly for PUSHN (where N <= 4) instructions, optimistically assuming that // they are function selectors let assembly: Vec = assembly.split('\n').map(|line| line.trim().to_string()).collect(); @@ -33,13 +31,15 @@ pub fn find_function_selectors(evm: &VM, assembly: &str) -> HashMap HashMap x, }; - logger.debug_max(&format!( + debug_max!( "found function selector {} at entry point {}", - function_selector, function_entry_point - )); + function_selector, + function_entry_point + ); function_selectors.insert(function_selector, function_entry_point); } @@ -83,13 +84,13 @@ pub fn resolve_entry_point(evm: &VM, selector: &str) -> u128 { jump_condition.contains(" == ") && jump_taken == 1 { - return call.last_instruction.inputs[0].try_into().unwrap_or(0) + return call.last_instruction.inputs[0].try_into().unwrap_or(0); } else if jump_taken == 1 { // if handled_jumps contains the jumpi, we have already handled this jump. // loops aren't supported in the dispatcher, so we can just return 0 if handled_jumps.contains(&call.last_instruction.inputs[0].try_into().unwrap_or(0)) { - return 0 + return 0; } else { handled_jumps.insert(call.last_instruction.inputs[0].try_into().unwrap_or(0)); } @@ -97,7 +98,7 @@ pub fn resolve_entry_point(evm: &VM, selector: &str) -> u128 { } if vm.exitcode != 255 || !vm.returndata.is_empty() { - break + break; } } diff --git a/common/src/ether/signatures.rs b/common/src/ether/signatures.rs index 7b0b4954..c5a5cce6 100644 --- a/common/src/ether/signatures.rs +++ b/common/src/ether/signatures.rs @@ -2,7 +2,10 @@ use async_trait::async_trait; use ethers::abi::Token; use heimdall_cache::{read_cache, store_cache}; -use crate::utils::{http::get_json_from_url, io::logging::Logger, strings::replace_last}; +use crate::{ + debug_max, + utils::{http::get_json_from_url, strings::replace_last}, +}; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] @@ -37,10 +40,7 @@ pub trait ResolveSelector { #[async_trait] impl ResolveSelector for ResolvedError { async fn resolve(selector: &str) -> Option> { - // get a new logger - let logger = Logger::default(); - - logger.debug_max(&format!("resolving error selector {}", &selector)); + debug_max!("resolving error selector {}", &selector); // get cached results if let Some(cached_results) = @@ -49,7 +49,7 @@ impl ResolveSelector for ResolvedError { match cached_results.len() { 0 => return None, _ => { - logger.debug_max(&format!("found cached results for selector: {}", &selector)); + debug_max!("found cached results for selector: {}", &selector); return Some(cached_results); } } @@ -78,11 +78,7 @@ impl ResolveSelector for ResolvedError { .as_array()? .to_vec(); - logger.debug_max(&format!( - "found {} possible functions for selector: {}", - &results.len(), - &selector - )); + debug_max!("found {} possible functions for selector: {}", &results.len(), &selector); let mut signature_list: Vec = Vec::new(); @@ -122,10 +118,7 @@ impl ResolveSelector for ResolvedError { #[async_trait] impl ResolveSelector for ResolvedLog { async fn resolve(selector: &str) -> Option> { - // get a new logger - let logger = Logger::default(); - - logger.debug_max(&format!("resolving event selector {}", &selector)); + debug_max!("resolving event selector {}", &selector); // get cached results if let Some(cached_results) = @@ -134,7 +127,7 @@ impl ResolveSelector for ResolvedLog { match cached_results.len() { 0 => return None, _ => { - logger.debug_max(&format!("found cached results for selector: {}", &selector)); + debug_max!("found cached results for selector: {}", &selector); return Some(cached_results); } } @@ -163,11 +156,7 @@ impl ResolveSelector for ResolvedLog { .as_array()? .to_vec(); - logger.debug_max(&format!( - "found {} possible functions for selector: {}", - &results.len(), - &selector - )); + debug_max!("found {} possible functions for selector: {}", &results.len(), &selector); let mut signature_list: Vec = Vec::new(); @@ -207,10 +196,7 @@ impl ResolveSelector for ResolvedLog { #[async_trait] impl ResolveSelector for ResolvedFunction { async fn resolve(selector: &str) -> Option> { - // get a new logger - let logger = Logger::default(); - - logger.debug_max(&format!("resolving event selector {}", &selector)); + debug_max!("resolving event selector {}", &selector); // get cached results if let Some(cached_results) = @@ -219,7 +205,7 @@ impl ResolveSelector for ResolvedFunction { match cached_results.len() { 0 => return None, _ => { - logger.debug_max(&format!("found cached results for selector: {}", &selector)); + debug_max!("found cached results for selector: {}", &selector); return Some(cached_results); } } @@ -248,11 +234,7 @@ impl ResolveSelector for ResolvedFunction { .as_array()? .to_vec(); - logger.debug_max(&format!( - "found {} possible functions for selector: {}", - &results.len(), - &selector - )); + debug_max!("found {} possible functions for selector: {}", &results.len(), &selector); let mut signature_list: Vec = Vec::new(); diff --git a/common/src/utils/http.rs b/common/src/utils/http.rs index 03630296..7b822d80 100644 --- a/common/src/utils/http.rs +++ b/common/src/utils/http.rs @@ -1,4 +1,4 @@ -use crate::utils::io::logging::Logger; +use crate::debug_max; use async_recursion::async_recursion; use reqwest::Client; use serde_json::Value; @@ -29,10 +29,7 @@ async fn _get_json_from_url( retries_remaining: u8, timeout: u64, ) -> Result, reqwest::Error> { - // get a new logger - let logger = Logger::default(); - - logger.debug_max(&format!("GET {}", &url)); + debug_max!("GET {}", &url); let client = Client::builder() .danger_accept_invalid_certs(true) @@ -42,13 +39,13 @@ async fn _get_json_from_url( let res = match client.get(url).send().await { Ok(res) => { - logger.debug_max(&format!("GET {}: {:?}", &url, &res)); + debug_max!("GET {}: {:?}", &url, &res); res } Err(e) => { - logger.debug_max(&format!("GET {}: {:?}", &url, &e)); + debug_max!("GET {}: {:?}", &url, &e); if retries_remaining == 0 { - return Ok(None) + return Ok(None); } // exponential backoff @@ -56,7 +53,7 @@ async fn _get_json_from_url( let retries_remaining = retries_remaining - 1; let sleep_time = 2u64.pow(retry_count as u32) * 250; async_sleep(Duration::from_millis(sleep_time)).await; - return _get_json_from_url(url, retry_count, retries_remaining, timeout).await + return _get_json_from_url(url, retry_count, retries_remaining, timeout).await; } }; let body = res.text().await?; diff --git a/common/src/utils/io/logging.rs b/common/src/utils/io/logging.rs index 8c1f88ff..71023abb 100644 --- a/common/src/utils/io/logging.rs +++ b/common/src/utils/io/logging.rs @@ -520,7 +520,7 @@ impl Logger { ) -> u8 { // if silent, return the default if self.level == -1 { - return default.expect("Failed to get default option.") + return default.expect("Failed to get default option."); } // log the message with the given class @@ -564,7 +564,7 @@ impl Logger { } else { println!(); } - return default.expect("Failed to get default option.") + return default.expect("Failed to get default option."); } // get input @@ -573,10 +573,10 @@ impl Logger { // check if default was selected if selection.trim() == "" { if let Some(default) = default { - return default + return default; } else { self.error("invalid selection."); - return self.option(function, message, options, default, skip) + return self.option(function, message, options, default, skip); } } @@ -585,7 +585,7 @@ impl Logger { Ok(i) => i, Err(_) => { self.error("invalid selection."); - return self.option(function, message, options, default, skip) + return self.option(function, message, options, default, skip); } }; @@ -862,28 +862,29 @@ mod tests { #[test] fn test_max() { - let (logger, _) = Logger::new("SILENT"); - logger.debug_max("log"); + let (_logger, _) = Logger::new("SILENT"); + use crate::debug_max; + debug_max!("log"); - let (logger, _) = Logger::new("ERROR"); - logger.debug_max("log"); + let (_logger, _) = Logger::new("ERROR"); + debug_max!("log"); - let (logger, _) = Logger::new("WARN"); - logger.debug_max("log"); + let (_logger, _) = Logger::new("WARN"); + debug_max!("log"); - let (logger, _) = Logger::new("INFO"); - logger.debug_max("log"); + let (_logger, _) = Logger::new("INFO"); + debug_max!("log"); - let (logger, _) = Logger::new("DEBUG"); - logger.debug_max("log"); + let (_logger, _) = Logger::new("DEBUG"); + debug_max!("log"); - let (logger, _) = Logger::new("TRACE"); - logger.debug_max("log"); + let (_logger, _) = Logger::new("TRACE"); + debug_max!("log"); - let (logger, _) = Logger::new("ALL"); - logger.debug_max("log"); + let (_logger, _) = Logger::new("ALL"); + debug_max!("log"); - let (logger, _) = Logger::new("MAX"); - logger.debug_max("log"); + let (_logger, _) = Logger::new("MAX"); + debug_max!("log"); } } diff --git a/common/src/utils/io/macros.rs b/common/src/utils/io/macros.rs index af47580a..3f70adc7 100644 --- a/common/src/utils/io/macros.rs +++ b/common/src/utils/io/macros.rs @@ -1,9 +1,9 @@ #[macro_export] macro_rules! debug_max { ($message:expr) => { - heimdall_common::utils::io::logging::Logger::default().debug_max($message); + $crate::utils::io::logging::Logger::default().debug_max($message); }; ($message:expr, $($arg:tt)*) => { - heimdall_common::utils::io::logging::Logger::default().debug_max(&format!($message, $($arg)*)); + $crate::utils::io::logging::Logger::default().debug_max(&format!($message, $($arg)*)); }; } diff --git a/common/src/utils/strings.rs b/common/src/utils/strings.rs index feb5595f..168093d4 100644 --- a/common/src/utils/strings.rs +++ b/common/src/utils/strings.rs @@ -124,7 +124,7 @@ pub fn find_balanced_encapsulator(s: &str, encap: (char, char)) -> (usize, usize } if open == close && open > 0 { end = i; - break + break; } } (start, end + 1, (open == close && end > start && open > 0)) @@ -155,7 +155,7 @@ pub fn find_balanced_encapsulator_backwards(s: &str, encap: (char, char)) -> (us } if open == close && open > 0 { end = i; - break + break; } } (s.len() - end - 1, s.len() - start, (open == close && end > start && open > 0)) @@ -233,7 +233,7 @@ pub fn extract_condition(s: &str, keyword: &str) -> Option { condition = condition.split(", ").collect::>()[0]; } - return Some(condition.trim().to_string()) + return Some(condition.trim().to_string()); } } @@ -323,18 +323,18 @@ pub enum TokenType { pub fn classify_token(token: &str) -> TokenType { // return if the token is a parenthesis if token == "(" || token == ")" { - return TokenType::Control + return TokenType::Control; } // check if the token is an operator let operators = ['+', '-', '*', '/', '=', '>', '<', '!', '&', '|', '%', '^']; if token.chars().all(|c| operators.contains(&c)) { - return TokenType::Operator + return TokenType::Operator; } // check if the token is a constant if token.starts_with("0x") || token.parse::().is_ok() { - return TokenType::Constant + return TokenType::Constant; } // check if the token is a variable @@ -345,7 +345,7 @@ pub fn classify_token(token: &str) -> TokenType { .iter() .any(|keyword| token.contains(keyword)) { - return TokenType::Variable + return TokenType::Variable; } // this token must be a function call diff --git a/core/src/cfg/mod.rs b/core/src/cfg/mod.rs index bcb7af1a..c7623d99 100644 --- a/core/src/cfg/mod.rs +++ b/core/src/cfg/mod.rs @@ -1,8 +1,9 @@ pub mod graph; pub mod output; use derive_builder::Builder; -use heimdall_common::ether::{ - compiler::detect_compiler, rpc::get_code, selectors::find_function_selectors, +use heimdall_common::{ + debug_max, + ether::{compiler::detect_compiler, rpc::get_code, selectors::find_function_selectors}, }; use indicatif::ProgressBar; use std::{fs, time::Duration}; @@ -114,10 +115,10 @@ pub async fn cfg(args: CFGArgs) -> Result, Box Result, Box x.name == resolved_error.name, _ => false, }) { - continue + continue; } abi.push(ABIStructure::Error(ErrorABI { @@ -234,7 +234,7 @@ pub fn build_abi( } _ => false, }) { - continue + continue; } abi.push(ABIStructure::Error(ErrorABI { @@ -272,7 +272,7 @@ pub fn build_abi( ABIStructure::Event(x) => x.name == resolved_event.name, _ => false, }) { - continue + continue; } abi.push(ABIStructure::Event(EventABI { @@ -293,7 +293,7 @@ pub fn build_abi( } _ => false, }) { - continue + continue; } abi.push(ABIStructure::Event(EventABI { diff --git a/core/src/decompile/out/postprocessers/solidity.rs b/core/src/decompile/out/postprocessers/solidity.rs index 6c1c51f2..36faed2c 100644 --- a/core/src/decompile/out/postprocessers/solidity.rs +++ b/core/src/decompile/out/postprocessers/solidity.rs @@ -177,12 +177,12 @@ fn simplify_parentheses(line: &str, paren_index: usize) -> String { // if there is a negation of an expression, remove the parentheses // helps with double negation if first_char == "!" && last_char == ")" { - return true + return true; } // remove the parentheses if the expression is within brackets if first_char == "[" && last_char == "]" { - return true + return true; } // parens required if: @@ -190,14 +190,14 @@ fn simplify_parentheses(line: &str, paren_index: usize) -> String { // - expression is a function call // - expression is the surrounding parens of a conditional if first_char != "(" { - return false + return false; } else if last_char == ")" { - return true + return true; } // don't include instantiations if expression.contains("memory ret") { - return false + return false; } // handle the inside of the expression @@ -207,14 +207,14 @@ fn simplify_parentheses(line: &str, paren_index: usize) -> String { }; let inner_tokens = tokenize(&inside); - return !inner_tokens.iter().any(|tk| classify_token(tk) == TokenType::Operator) + return !inner_tokens.iter().any(|tk| classify_token(tk) == TokenType::Operator); } let mut cleaned: String = line.to_owned(); // skip lines that are defining a function if cleaned.contains("function") { - return cleaned + return cleaned; } // get the nth index of the first open paren @@ -414,7 +414,7 @@ fn convert_access_to_variable(line: &str) -> String { fn contains_unnecessary_assignment(line: &str, lines: &Vec<&str>) -> bool { // skip lines that don't contain an assignment, or contain a return or external calls if !line.contains(" = ") || line.contains("bool success") || line.contains("return") { - return false + return false; } // get var name @@ -423,25 +423,25 @@ fn contains_unnecessary_assignment(line: &str, lines: &Vec<&str>) -> bool { // skip lines that contain assignments to storage if var_name.contains("stor_") { - return false + return false; } //remove unused vars for x in lines { // break if the line contains a function definition if x.contains("function") { - break + break; } if x.contains(" = ") { let assignment = x.split(" = ").map(|x| x.trim()).collect::>(); if assignment[1].contains(var_name) { - return false + return false; } else if assignment[0].contains(var_name) { - return true + return true; } } else if x.contains(var_name) { - return false + return false; } } @@ -456,12 +456,12 @@ fn move_casts_to_declaration(line: &str) -> String { // if line contains "function" wipe the set if cleaned.contains("function") { type_declaration_set.clear(); - return cleaned.to_owned() + return cleaned.to_owned(); } // if the line doesn't contain an instantiation, return if !cleaned.contains(" = ") { - return cleaned.to_owned() + return cleaned.to_owned(); } let instantiation = cleaned.split(" = ").collect::>(); @@ -471,7 +471,7 @@ fn move_casts_to_declaration(line: &str) -> String { Some(x) => { // the match must occur at index 0 if x.start() != 0 { - return cleaned.to_owned() + return cleaned.to_owned(); } // find the matching close paren @@ -480,7 +480,7 @@ fn move_casts_to_declaration(line: &str) -> String { // the close paren must be at the end of the expression if paren_end != instantiation[1].len() - 1 { - return cleaned.to_owned() + return cleaned.to_owned(); } // get the inside of the parens @@ -515,19 +515,19 @@ fn replace_expression_with_var(line: &str) -> String { // skip function definitions if cleaned.contains("function") { - return cleaned + return cleaned; } // iterate over variable map for (var, expression) in var_map.iter() { // skip numeric expressions if expression.parse::().is_ok() { - continue + continue; } // skip expressions that are already variables. i.e, check if they contain a space if !expression.contains(' ') { - continue + continue; } // replace the expression with the variable @@ -576,7 +576,7 @@ fn inherit_infer_mem_type(line: &str) -> String { // if the line does not contains an instantiation, return if !line.contains(" = ") || line.trim().starts_with("stor") { - return cleaned + return cleaned; } let instantiation = line.split(" = ").collect::>(); @@ -597,7 +597,7 @@ fn inherit_infer_mem_type(line: &str) -> String { if cleaned.contains(var) && !type_map.contains_key(var_name) && !var_type.is_empty() { cleaned = format!("{var_type} {cleaned}"); type_map.insert(var_name.to_string(), var_type.to_string()); - break + break; } } } @@ -636,7 +636,7 @@ fn inherit_infer_storage_type(line: &str) { // since the regex is greedy, match the memory brackets let matched_loc = find_balanced_encapsulator(storage_access, ('[', ']')); if !matched_loc.2 { - return + return; } let mut storage_slot = format!("storage{}", storage_access.get(matched_loc.0..matched_loc.1).unwrap()); @@ -680,7 +680,7 @@ fn inherit_infer_storage_type(line: &str) { lhs_type = var_type.to_string(); // continue, so we cannot use this var in rhs - continue + continue; } // check for vars in rhs @@ -765,7 +765,7 @@ fn replace_resolved( // line must contain CustomError_ or Event_ if !cleaned.contains("CustomError_") && !cleaned.contains("Event_") { - return cleaned + return cleaned; } // not the best way to do it, can perf later @@ -805,7 +805,7 @@ fn cleanup( // skip comments if cleaned.starts_with('/') { - return cleaned + return cleaned; } // Find and convert all castings @@ -880,7 +880,7 @@ fn finalize(lines: Vec, bar: &ProgressBar) -> Vec { ) { cleaned_lines.push(line.to_string()); } else { - continue + continue; } } diff --git a/core/src/decompile/out/postprocessers/yul.rs b/core/src/decompile/out/postprocessers/yul.rs index 9134766c..e2d0525a 100644 --- a/core/src/decompile/out/postprocessers/yul.rs +++ b/core/src/decompile/out/postprocessers/yul.rs @@ -67,7 +67,7 @@ fn convert_bitmask_to_casting(line: &str) -> String { let is_rhs_all_ones = arg2.replacen("0x", "", 1).chars().all(|c| c == 'f' || c == 'F'); if !is_lhs_all_ones && !is_rhs_all_ones { index += end_index + 1; - continue // skip if LHS and RHS are not bitwise masks + continue; // skip if LHS and RHS are not bitwise masks } // determine size of bytes based on argument 1 @@ -139,7 +139,7 @@ fn remove_replace_casts(line: &str) -> String { cleaned.replace_range(cast_start - cast_type.len()..=cast_end - 1, &yul_cast); } else { - break + break; } } @@ -157,7 +157,7 @@ fn simplify_parentheses(line: &str, paren_index: usize) -> String { // if there is a negation of an expression, remove the parentheses // helps with double negation if first_char == "iszero" && last_char == ")" { - return true + return true; } // parens required if: @@ -165,14 +165,14 @@ fn simplify_parentheses(line: &str, paren_index: usize) -> String { // - expression is a function call // - expression is the surrounding parens of a conditional if first_char != "(" { - return false + return false; } else if last_char == ")" { - return true + return true; } // don't include instantiations if expression.contains(":=") { - return false + return false; } // handle the inside of the expression @@ -197,7 +197,7 @@ fn simplify_parentheses(line: &str, paren_index: usize) -> String { // skip lines that are defining a function if cleaned.contains("case") { - return cleaned + return cleaned; } // get the nth index of the first open paren @@ -261,7 +261,7 @@ fn add_resolved_events(line: &str, all_resolved_events: HashMap) -> Str // skip comments if cleaned.starts_with('/') { - return cleaned + return cleaned; } // remove double negations diff --git a/core/src/decompile/resolve.rs b/core/src/decompile/resolve.rs index a00fa138..f2c1a73b 100644 --- a/core/src/decompile/resolve.rs +++ b/core/src/decompile/resolve.rs @@ -1,5 +1,5 @@ use super::util::Function; -use heimdall_common::{ether::signatures::ResolvedFunction, utils::io::logging::Logger}; +use heimdall_common::{debug_max, ether::signatures::ResolvedFunction}; /// Given a list of potential [`ResolvedFunction`]s and a [`Function`], return a list of /// [`ResolvedFunction`]s (that is, resolved signatures that were found on a 4byte directory) that @@ -8,12 +8,9 @@ pub fn match_parameters( resolved_functions: Vec, function: &Function, ) -> Vec { - // get a new logger - let logger = Logger::default(); - let mut matched_functions: Vec = Vec::new(); for mut resolved_function in resolved_functions { - logger.debug_max(&format!( + debug_max!( "checking function {}({}) against Unresolved_0x{}({})", &resolved_function.name, &resolved_function.inputs.join(","), @@ -24,60 +21,56 @@ pub fn match_parameters( .map(|(_, types)| types.first().unwrap().clone()) .collect::>() .join(",") - )); + ); // skip checking if length of parameters list is less than the resolved functions inputs resolved_function.inputs.retain(|x| !x.is_empty()); let mut matched = true; // check each parameter type against a list of potential types for (index, input) in resolved_function.inputs.iter().enumerate() { - logger.debug_max(&format!( - " checking for parameter {} with type {}", - &index.to_string(), - &input - )); + debug_max!(" checking for parameter {} with type {}", &index.to_string(), &input); match function.arguments.get(&index) { Some((_, potential_types)) => { // arrays are typically recorded as bytes by the decompiler's potential // types if input.contains("[]") { if !potential_types.contains(&"bytes".to_string()) { - logger.debug_max(&format!( + debug_max!( " parameter {} does not match type {} for function {}({})", &index.to_string(), &input, &resolved_function.name, &resolved_function.inputs.join(",") - )); - continue + ); + continue; } } else if !potential_types.contains(input) { matched = false; - logger.debug_max(&format!( + debug_max!( " parameter {} does not match type {} for function {}({})", &index.to_string(), &input, &resolved_function.name, &resolved_function.inputs.join(",") - )); - break + ); + break; } } None => { // parameter not found matched = false; - logger.debug_max(&format!( + debug_max!( " parameter {} not found for function {}({})", &index.to_string(), &resolved_function.name, &resolved_function.inputs.join(",") - )); - break + ); + break; } } } - logger.debug_max(&format!(" matched: {}", &matched.to_string())); + debug_max!(" matched: {}", &matched.to_string()); if matched { matched_functions.push(resolved_function); } diff --git a/core/src/dump/util/mod.rs b/core/src/dump/util/mod.rs index 18bce332..15ff7164 100644 --- a/core/src/dump/util/mod.rs +++ b/core/src/dump/util/mod.rs @@ -42,8 +42,8 @@ pub async fn get_storage_diff(tx: &Transaction, args: &DumpArgs) -> Option Option &changed.to, Diff::Died(_) => { state.storage.remove(slot); - continue + continue; } _ => continue, }; diff --git a/core/src/dump/util/threads/tui.rs b/core/src/dump/util/threads/tui.rs index 47cb86d9..f97edf54 100644 --- a/core/src/dump/util/threads/tui.rs +++ b/core/src/dump/util/threads/tui.rs @@ -18,7 +18,7 @@ use crate::dump::{ pub fn handle(args: &DumpArgs, output_dir: &str) { // if no TUI is requested, just run the dump if args.no_tui { - return + return; } // create new TUI terminal @@ -67,7 +67,7 @@ pub fn handle(args: &DumpArgs, output_dir: &str) { match command { ":q" | ":quit" => { state.view = TUIView::Killed; - break + break; } ":h" | ":help" => { state.view = TUIView::Help; @@ -127,7 +127,7 @@ pub fn handle(args: &DumpArgs, output_dir: &str) { } drop(state); - continue + continue; } match key.code { @@ -154,7 +154,7 @@ pub fn handle(args: &DumpArgs, output_dir: &str) { value.decode_as_type_index += 1; } } else if i >= scroll_index + selection_size { - break + break; } } } @@ -176,7 +176,7 @@ pub fn handle(args: &DumpArgs, output_dir: &str) { value.decode_as_type_index -= 1; } } else if i >= scroll_index + selection_size { - break + break; } } } diff --git a/core/src/snapshot/analyze.rs b/core/src/snapshot/analyze.rs index 2ece3594..ab18cd53 100644 --- a/core/src/snapshot/analyze.rs +++ b/core/src/snapshot/analyze.rs @@ -159,7 +159,7 @@ pub fn snapshot_trace( ), ); snapshot.payable = false; - continue + continue; } // perform a series of checks to determine if the condition @@ -171,7 +171,7 @@ pub fn snapshot_trace( !conditional.contains("arg") && !conditional.contains("storage")) { - continue + continue; } snapshot.control_statements.insert(format!("if ({}) {{ .. }}", conditional)); @@ -318,7 +318,7 @@ pub fn snapshot_trace( if address.replacen("0x", "", 1).chars().all(|c| c == 'f' || c == '0') || (address.len() > 42 || address.len() < 32) { - continue + continue; } snapshot.addresses.insert(address); diff --git a/core/src/snapshot/mod.rs b/core/src/snapshot/mod.rs index cbe80bba..7a38fe18 100644 --- a/core/src/snapshot/mod.rs +++ b/core/src/snapshot/mod.rs @@ -4,6 +4,7 @@ pub mod menus; pub mod resolve; pub mod structures; pub mod util; +use heimdall_common::debug_max; use std::{ collections::{HashMap, HashSet}, @@ -143,10 +144,10 @@ pub async fn snapshot(args: SnapshotArgs) -> Result Result, function: &Snapshot, ) -> Vec { - // get a new logger - let logger = Logger::default(); - let mut matched_functions: Vec = Vec::new(); for mut resolved_function in resolved_functions { - logger.debug_max(&format!( + debug_max!( "checking function {}({}) against Unresolved_0x{}({})", &resolved_function.name, &resolved_function.inputs.join(","), @@ -25,60 +22,56 @@ pub fn match_parameters( .map(|(_, types)| types.first().unwrap().clone()) .collect::>() .join(",") - )); + ); // skip checking if length of parameters list is less than the resolved functions inputs resolved_function.inputs.retain(|x| !x.is_empty()); let mut matched = true; // check each parameter type against a list of potential types for (index, input) in resolved_function.inputs.iter().enumerate() { - logger.debug_max(&format!( - " checking for parameter {} with type {}", - &index.to_string(), - &input - )); + debug_max!(" checking for parameter {} with type {}", &index.to_string(), &input); match function.arguments.get(&index) { Some((_, potential_types)) => { // arrays are typically recorded as bytes by the decompiler's potential // types if input.contains("[]") { if !potential_types.contains(&"bytes".to_string()) { - logger.debug_max(&format!( + debug_max!( " parameter {} does not match type {} for function {}({})", &index.to_string(), &input, &resolved_function.name, &resolved_function.inputs.join(",") - )); - continue + ); + continue; } } else if !potential_types.contains(input) { matched = false; - logger.debug_max(&format!( + debug_max!( " parameter {} does not match type {} for function {}({})", &index.to_string(), &input, &resolved_function.name, &resolved_function.inputs.join(",") - )); - break + ); + break; } } None => { // parameter not found matched = false; - logger.debug_max(&format!( + debug_max!( " parameter {} not found for function {}({})", &index.to_string(), &resolved_function.name, &resolved_function.inputs.join(",") - )); - break + ); + break; } } } - logger.debug_max(&format!(" matched: {}", &matched.to_string())); + debug_max!(" matched: {}", &matched.to_string()); if matched { matched_functions.push(resolved_function); } diff --git a/core/src/snapshot/util/tui.rs b/core/src/snapshot/util/tui.rs index 06b31e69..5eea9516 100644 --- a/core/src/snapshot/util/tui.rs +++ b/core/src/snapshot/util/tui.rs @@ -86,7 +86,7 @@ pub fn handle( match command { ":q" | ":quit" => { state.view = TUIView::Killed; - break + break; } ":h" | ":help" => { state.view = TUIView::Help; @@ -106,7 +106,7 @@ pub fn handle( } drop(state); - continue + continue; } match key.code {