Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix concolic solution conflict with abi #262

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 31 additions & 34 deletions src/evm/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use crate::input::ConciseSerde;
use crate::mutation_utils::{byte_mutator, byte_mutator_with_expansion};
use crate::state::{HasCaller, HasItyState};
use itertools::Itertools;
use libafl_bolts::impl_serdeany;
use libafl::inputs::{HasBytesVec, Input};
use libafl::mutators::MutationResult;
use libafl::prelude::HasMetadata;
use libafl_bolts::bolts_prelude::Rand;
use libafl::state::{HasMaxSize, HasRand, State};
use libafl_bolts::bolts_prelude::Rand;
use libafl_bolts::impl_serdeany;
use once_cell::sync::Lazy;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -81,7 +81,7 @@ impl ABIAddressToInstanceMap {

/// Add an ABI instance to the map
pub fn add(&mut self, address: EVMAddress, abi: BoxedABI) {
self.map.entry(address).or_insert_with(Vec::new);
self.map.entry(address).or_default();
self.map.get_mut(&address).unwrap().push(abi);
}
}
Expand Down Expand Up @@ -119,7 +119,7 @@ pub trait ABI: CloneABI {
/// Get the ABI type of args
fn get_type(&self) -> ABILossyType;
/// Set the bytes to args, used for decoding
fn set_bytes(&mut self, bytes: Vec<u8>);
fn set_bytes(&mut self, bytes: Vec<u8>) -> bool;
/// Convert args to string (for debugging)
fn to_string(&self) -> String;
fn as_any(&mut self) -> &mut dyn Any;
Expand Down Expand Up @@ -252,21 +252,6 @@ impl BoxedABI {
}
}

/// Convert function hash and args to string (for debugging)
// pub fn to_string(&self) -> String {
// if self.function == [0; 4] {
// format!("Stepping with return: {}", hex::encode(self.b.to_string()))
// } else {
// let function_name = unsafe {
// FUNCTION_SIG
// .get(&self.function)
// .unwrap_or(&hex::encode(self.function))
// .clone()
// };
// format!("{}{}", function_name, self.b.to_string())
// }
// }

/// todo: remove this
pub fn get_concolic(self) -> Vec<Box<Expr>> {
[
Expand All @@ -280,8 +265,8 @@ impl BoxedABI {
}

/// Set the bytes to args, used for decoding
pub fn set_bytes(&mut self, bytes: Vec<u8>) {
self.b.set_bytes(bytes[4..].to_vec());
pub fn set_bytes(&mut self, bytes: Vec<u8>) -> bool {
self.b.set_bytes(bytes[4..].to_vec())
}
}

Expand Down Expand Up @@ -517,8 +502,9 @@ impl ABI for AEmpty {
TEmpty
}

fn set_bytes(&mut self, bytes: Vec<u8>) {
fn set_bytes(&mut self, bytes: Vec<u8>) -> bool {
assert!(bytes.is_empty());
true
}

fn to_string(&self) -> String {
Expand Down Expand Up @@ -598,8 +584,12 @@ impl ABI for A256 {
T256
}

fn set_bytes(&mut self, bytes: Vec<u8>) {
fn set_bytes(&mut self, bytes: Vec<u8>) -> bool {
if bytes.len() < 32 {
return false;
}
self.data = bytes[..32].to_vec();
true
}

fn to_string(&self) -> String {
Expand Down Expand Up @@ -686,12 +676,13 @@ impl ABI for ADynamic {
self
}

fn set_bytes(&mut self, bytes: Vec<u8>) {
fn set_bytes(&mut self, bytes: Vec<u8>) -> bool {
if bytes.is_empty() {
self.data = Vec::new();
return;
return true;
}
self.data = bytes[32..32 + get_size(&bytes)].to_vec();
true
}

fn get_concolic(&self) -> Vec<Box<Expr>> {
Expand Down Expand Up @@ -834,7 +825,7 @@ impl ABI for AArray {

// Input: packed concrete bytes produced by get_concolic
// Set the bytes in self.data accordingly
fn set_bytes(&mut self, bytes: Vec<u8>) {
fn set_bytes(&mut self, bytes: Vec<u8>) -> bool {
let base_offset = if self.dynamic_size {
let array_size = get_size(&bytes);
while self.data.len() < array_size {
Expand Down Expand Up @@ -862,10 +853,15 @@ impl ABI for AArray {
unreachable!()
}
};
item.b
.set_bytes(bytes[item_offset + base_offset..].to_vec());

let start = item_offset + base_offset;
if start + size >= bytes.len() {
return false;
}
item.b.set_bytes(bytes[start..].to_vec());
offset += size;
}
true
}

fn get_concolic(&self) -> Vec<Box<Expr>> {
Expand Down Expand Up @@ -1006,8 +1002,9 @@ impl ABI for AUnknown {
TUnknown
}

fn set_bytes(&mut self, bytes: Vec<u8>) {
fn set_bytes(&mut self, bytes: Vec<u8>) -> bool {
self.concrete.b.set_bytes(bytes);
true
}

fn to_string(&self) -> String {
Expand All @@ -1029,19 +1026,19 @@ impl ABI for AUnknown {

/// Create a [`BoxedABI`] with default arg given the ABI type in string
pub fn get_abi_type_boxed(abi_name: &str) -> BoxedABI {
return BoxedABI {
BoxedABI {
b: get_abi_type(abi_name, &None),
function: [0; 4],
};
}
}

/// Create a [`BoxedABI`] with default arg given the ABI type in string and address
/// todo: remove this function
pub fn get_abi_type_boxed_with_address(abi_name: &str, address: Vec<u8>) -> BoxedABI {
return BoxedABI {
BoxedABI {
b: get_abi_type(abi_name, &Some(address)),
function: [0; 4],
};
}
}

/// Split a string with parenthesis
Expand Down Expand Up @@ -1170,7 +1167,7 @@ fn get_abi_type_basic(
if let Some(stripped) = abi_name.strip_prefix("uint") {
let len = stripped.parse::<usize>().unwrap();
assert!(len % 8 == 0 && len >= 8);
return get_abi_type_basic("uint", len / 8, with_address);
get_abi_type_basic("uint", len / 8, with_address)
} else if let Some(stripped) = abi_name.strip_prefix("int") {
let len = stripped.parse::<usize>().unwrap();
assert!(len % 8 == 0 && len >= 8);
Expand Down
Loading