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

[WIP] fix(fuzz): strip metadata when collecting push bytes #8139

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
20 changes: 17 additions & 3 deletions crates/evm/fuzz/src/strategies/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use parking_lot::{lock_api::RwLockReadGuard, RawRwLock, RwLock};
use revm::{
db::{CacheDB, DatabaseRef, DbAccount},
interpreter::opcode,
primitives::AccountInfo,
primitives::{AccountInfo, Bytecode},
};
use std::{
collections::{BTreeMap, HashMap},
Expand Down Expand Up @@ -245,12 +245,26 @@ impl FuzzDictionary {
// Insert push bytes
if let Some(code) = &account_info.code {
self.insert_address(*address);
self.collect_push_bytes(code.bytes_slice());
self.collect_push_bytes(code);
}
}
}

fn collect_push_bytes(&mut self, code: &[u8]) {
fn collect_push_bytes(&mut self, code: &Bytecode) {
let mut code = code.original_byte_slice();
if code.is_empty() {
return;
}

// Remove metadata by looking up the last two bytes of original bytecode.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this always true?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For initcode, no. Sometimes e.g. long strings in a constructor are after the metadata hash.

For runtime code, yes, IF the metadata hash is present. When bytecode_hash = none there is still a metadata hash containing the solidity version, so the last two bytes are still the length. When cbor_metadata = false then there is no bytecode hash at all and the last two bytes are code

To know for certain if what you have is a metadata hash, without knowing the compilation settings, you'd assume the last two bytes are the length of the metadata hash, then try CBOR decoding that hash. If it decodes successfully, it's a metadata hash, otherwise it's code.

Here is some old, probably bad, rust code I had that did this using ciborium: https://github.com/ScopeLift/cove-backend/blob/d8c875c5a64bf89a874892bc8ed57751c47b1233/src/bytecode.rs#L53-L97

if code.len() > 2 {
let metadata_len = &code[code.len() - 2..];
let metadata_len = u16::from_be_bytes([metadata_len[0], metadata_len[1]]).into();
if code.len() > metadata_len {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could still fail due to the -2, but not sure if possible

perhaps metadata_offset = metadata_len + 2

&code[..code.len() - metadata_offset];

code = &code[..code.len() - 2 - metadata_len];
}
}

let mut i = 0;
let len = code.len().min(PUSH_BYTE_ANALYSIS_LIMIT);
while i < len {
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/tests/it/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ async fn test_persist_fuzz_failure() {
async fn test_scrape_bytecode() {
let filter = Filter::new(".*", ".*", ".*fuzz/FuzzScrapeBytecode.t.sol");
let mut runner = TEST_DATA_DEFAULT.runner();
runner.test_options.fuzz.runs = 2000;
runner.test_options.fuzz.dictionary.dictionary_weight = 100;
runner.test_options.fuzz.seed = Some(U256::from(6u32));
let suite_result = runner.test_collect(&filter);

Expand Down
Loading