Skip to content

Commit

Permalink
chore(logging): use macro debug_max!(..) throughout codebase (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-Becker authored Dec 2, 2023
1 parent 9f24033 commit c4f9855
Show file tree
Hide file tree
Showing 28 changed files with 236 additions and 288 deletions.
4 changes: 2 additions & 2 deletions cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ where
let binary_vec = decode_hex(&binary_string);

if binary_vec.is_err() {
return None
return None;
}

let cache: Cache<T> = match bincode::deserialize::<Cache<T>>(&binary_vec.unwrap()) {
Expand All @@ -210,7 +210,7 @@ where
.as_secs()
{
delete_cache(key);
return None
return None;
}

c
Expand Down
6 changes: 3 additions & 3 deletions cache/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down
4 changes: 2 additions & 2 deletions cli/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
13 changes: 5 additions & 8 deletions common/src/ether/compiler.rs
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -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::<Vec<&str>>();
Expand All @@ -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
));
);
}
}

Expand Down
20 changes: 10 additions & 10 deletions common/src/ether/evm/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn parse_function_parameters(function_signature: &str) -> Option<Vec<ParamTy
// remove the function name from the signature, only keep the parameters
let (start, end, valid) = find_balanced_encapsulator(function_signature, ('(', ')'));
if !valid {
return None
return None;
}

let function_inputs = function_signature[start + 1..end - 1].to_string();
Expand All @@ -47,7 +47,7 @@ fn extract_types_from_string(string: &str) -> Option<Vec<ParamType>> {

// 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
Expand All @@ -57,7 +57,7 @@ fn extract_types_from_string(string: &str) -> Option<Vec<ParamType>> {
// get balanced encapsulator
let (tuple_start, tuple_end, valid) = find_balanced_encapsulator(string, ('(', ')'));
if !valid {
return None
return None;
}

// extract the tuple
Expand All @@ -78,7 +78,7 @@ fn extract_types_from_string(string: &str) -> Option<Vec<ParamType>> {
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();
Expand Down Expand Up @@ -156,7 +156,7 @@ fn extract_types_from_string(string: &str) -> Option<Vec<ParamType>> {
// 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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -365,7 +365,7 @@ pub fn get_padding(bytes: &str) -> Padding {
.collect::<Vec<usize>>();

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
Expand Down
57 changes: 28 additions & 29 deletions common/src/ether/evm/ext/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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::<Vec<String>>().join(", ")));
debug_max!("stack diff: [{}]", stack_diff.iter().map(|frame| format!("{}", frame.value)).collect::<Vec<String>>().join(", "));

// check if the jump condition appears to be recursive
if jump_condition_appears_recursive(&stack_diff, &jump_condition) {
Expand All @@ -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());
Expand All @@ -190,47 +189,47 @@ 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::<Vec<String>>().join("\n - ")
));
);

// this key exists, but the stack is different, so the jump is new
historical_stacks.push(vm.stack.clone());
}
}
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() {
Expand All @@ -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();
Expand All @@ -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;
}
}

Expand Down
Loading

0 comments on commit c4f9855

Please sign in to comment.