Skip to content

Commit

Permalink
fix: onchain-scanner panics when unwrapping a Result. (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-chia authored Feb 26, 2024
1 parent 1f65c65 commit 48917a8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
6 changes: 5 additions & 1 deletion src/evm/bytecode_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ pub fn all_bytecode(bytes: &Vec<u8>) -> Vec<(usize, u8)> {
let mut res = Vec::new();

while i < bytes.len() - cbor_len {
let op = *bytes.get(i).unwrap();
let op = if let Some(op) = bytes.get(i) {
*op
} else {
break;
};
res.push((i, op));
i += 1;
if (0x60..=0x7f).contains(&op) {
Expand Down
25 changes: 9 additions & 16 deletions src/evm/middlewares/call_printer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, fmt::Debug, fs::OpenOptions, io::Write};
use std::{collections::HashMap, fmt::Debug};

use bytes::Bytes;
use itertools::Itertools;
Expand All @@ -13,6 +13,7 @@ use crate::evm::{
middlewares::middleware::{Middleware, MiddlewareType},
srcmap::{RawSourceMapInfo, SOURCE_MAP_PROVIDER},
types::{as_u64, convert_u256_to_h160, EVMAddress, EVMFuzzState, EVMU256},
utils,
};

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
Expand Down Expand Up @@ -94,22 +95,14 @@ impl CallPrinter {
}

pub fn save_trace(&self, path: &str) {
let mut file = OpenOptions::new()
.create(true)
.write(true)
.append(false)
.open(path)
.unwrap();
file.write_all(self.get_trace().as_bytes()).unwrap();
utils::try_write_file(path, &self.get_trace(), false).unwrap();

let mut json_file = OpenOptions::new()
.create(true)
.write(true)
.append(false)
.open(format!("{}.json", path))
.unwrap();
let json = serde_json::to_string(&self.results).unwrap();
json_file.write_all(json.as_bytes()).unwrap();
utils::try_write_file(
&format!("{}.json", path),
&serde_json::to_string(&self.results).unwrap(),
false,
)
.unwrap();
}

fn translate_address(&self, a: EVMAddress) -> String {
Expand Down
12 changes: 2 additions & 10 deletions src/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,7 @@ pub fn evm_main(mut args: EvmArgs) {

let work_dir = args.work_dir.clone();
let work_path = Path::new(work_dir.as_str());
if !work_path.exists() {
std::fs::create_dir(work_path).unwrap();
}
let _ = std::fs::create_dir_all(work_path);

let mut target_type: EVMTargetType = match args.target_type {
Some(v) => EVMTargetType::from_str(v.as_str()),
Expand Down Expand Up @@ -777,12 +775,6 @@ pub fn evm_main(mut args: EvmArgs) {

let abis_json = format!("{}/abis.json", args.work_dir.clone().as_str());

let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(abis_json)
.expect("Failed to open or create abis.json");

writeln!(file, "{}", json_str).expect("Failed to write abis to abis.json");
utils::try_write_file(&abis_json, &json_str, true).unwrap();
evm_fuzzer(config, &mut state)
}
32 changes: 32 additions & 0 deletions src/evm/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::io::Write;

use colored::Colorize;
use regex::Regex;
use revm_primitives::U256;
Expand Down Expand Up @@ -79,6 +81,36 @@ pub fn remove_color(input: &str) -> String {
reg.replace_all(input, "").to_string()
}

pub fn try_write_file(path: &str, data: &str, append: bool) -> Result<(), String> {
let mut retry = 3;
while retry > 0 {
retry -= 1;

match std::fs::OpenOptions::new()
.create(true)
.write(true)
.append(append)
.open(path)
{
Ok(mut file) => match file.write_all(data.as_bytes()) {
Ok(_) => return Ok(()),
Err(e) => {
if retry <= 0 {
return Err(format!("Failed to write to file: {}", e));
}
}
},
Err(e) => {
if retry <= 0 {
return Err(format!("Failed to create or open file: {}", e));
}
}
}
}

Ok(())
}

fn get_rgb_by_address(addr: &str) -> (u8, u8, u8) {
let default = vec![0x00, 0x76, 0xff];
// 8 is the length of `0x` + 3 bytes
Expand Down

0 comments on commit 48917a8

Please sign in to comment.