-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
grandizzy
wants to merge
1
commit into
foundry-rs:master
Choose a base branch
from
grandizzy:issue-8115
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+18
−4
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}, | ||
|
@@ -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. | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 &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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this always true?
There was a problem hiding this comment.
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. Whencbor_metadata = false
then there is no bytecode hash at all and the last two bytes are codeTo 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