Skip to content

Commit a946591

Browse files
committed
fix(forge): do not use metadata in fuzzing
1 parent 0873425 commit a946591

File tree

8 files changed

+35
-30
lines changed

8 files changed

+35
-30
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ chrono = { version = "0.4", default-features = false, features = [
270270
"std",
271271
] }
272272
axum = "0.7"
273+
ciborium = "0.2"
273274
color-eyre = "0.6"
274275
comfy-table = "7"
275276
dirs = "6"

crates/common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ path-slash.workspace = true
7272
anstream.workspace = true
7373
anstyle.workspace = true
7474
terminal_size.workspace = true
75+
ciborium.workspace = true
7576

7677
[build-dependencies]
7778
chrono.workspace = true

crates/common/src/utils.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,20 @@ pub fn erc7201(id: &str) -> B256 {
3737
let x = U256::from_be_bytes(keccak256(id).0) - U256::from(1);
3838
keccak256(x.to_be_bytes::<32>()) & B256::from(!U256::from(0xff))
3939
}
40+
41+
/// Utility function to ignore metadata hash of the given bytecode.
42+
/// This assumes that the metadata is at the end of the bytecode.
43+
pub fn ignore_metadata_hash(bytecode: &[u8]) -> &[u8] {
44+
// Get the last two bytes of the bytecode to find the length of CBOR metadata.
45+
let Some((rest, metadata_len_bytes)) = bytecode.split_last_chunk() else { return bytecode };
46+
let metadata_len = u16::from_be_bytes(*metadata_len_bytes) as usize;
47+
if metadata_len > rest.len() {
48+
return bytecode;
49+
}
50+
let (rest, metadata) = rest.split_at(rest.len() - metadata_len);
51+
if ciborium::from_reader::<ciborium::Value, _>(metadata).is_ok() {
52+
rest
53+
} else {
54+
bytecode
55+
}
56+
}

crates/evm/fuzz/src/strategies/state.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use alloy_primitives::{
55
map::{AddressIndexSet, B256IndexSet, HashMap},
66
Address, Bytes, Log, B256, U256,
77
};
8+
use foundry_common::ignore_metadata_hash;
89
use foundry_config::FuzzDictionaryConfig;
910
use foundry_evm_core::utils::StateChangeset;
1011
use parking_lot::{lock_api::RwLockReadGuard, RawRwLock, RwLock};
@@ -248,7 +249,7 @@ impl FuzzDictionary {
248249
// Insert push bytes
249250
if let Some(code) = &account_info.code {
250251
self.insert_address(*address);
251-
self.collect_push_bytes(code.bytes_slice());
252+
self.collect_push_bytes(ignore_metadata_hash(code.original_byte_slice()));
252253
}
253254
}
254255
}

crates/forge/tests/it/invariant.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,9 +1114,9 @@ Encountered 1 failing test in test/InvariantSequenceLenTest.t.sol:InvariantSeque
11141114
[FAIL: invariant increment failure]
11151115
[Sequence] (original: 4, shrunk: 4)
11161116
sender=0x00000000000000000000000000000000000018dE addr=[src/Counter.sol:Counter]0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f calldata=setNumber(uint256) args=[1931387396117645594923 [1.931e21]]
1117-
sender=0x00000000000000000000000000000000000009d5 addr=[src/Counter.sol:Counter]0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f calldata=increment() args=[]
1118-
sender=0x0000000000000000000000000000000000000105 addr=[src/Counter.sol:Counter]0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f calldata=increment() args=[]
1119-
sender=0x00000000000000000000000000000000000009B2 addr=[src/Counter.sol:Counter]0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f calldata=setNumber(uint256) args=[996881781832960761274744263729582347 [9.968e35]]
1117+
sender=0x0000000000000000000000000000000000000C37 addr=[src/Counter.sol:Counter]0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f calldata=increment() args=[]
1118+
sender=0x0000000000000000000000000000000000000106 addr=[src/Counter.sol:Counter]0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f calldata=increment() args=[]
1119+
sender=0x0000000000000000000000000000000000001684 addr=[src/Counter.sol:Counter]0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f calldata=setNumber(uint256) args=[996881781832960761274744263729582347 [9.968e35]]
11201120
invariant_increment() (runs: 0, calls: 0, reverts: 0)
11211121
11221122
Encountered a total of 1 failing tests, 0 tests succeeded
@@ -1138,11 +1138,11 @@ Encountered 1 failing test in test/InvariantSequenceLenTest.t.sol:InvariantSeque
11381138
[Sequence] (original: 4, shrunk: 4)
11391139
vm.prank(0x00000000000000000000000000000000000018dE);
11401140
Counter(0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f).setNumber(1931387396117645594923);
1141-
vm.prank(0x00000000000000000000000000000000000009d5);
1141+
vm.prank(0x0000000000000000000000000000000000000C37);
11421142
Counter(0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f).increment();
1143-
vm.prank(0x0000000000000000000000000000000000000105);
1143+
vm.prank(0x0000000000000000000000000000000000000106);
11441144
Counter(0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f).increment();
1145-
vm.prank(0x00000000000000000000000000000000000009B2);
1145+
vm.prank(0x0000000000000000000000000000000000001684);
11461146
Counter(0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f).setNumber(996881781832960761274744263729582347);
11471147
invariant_increment() (runs: 0, calls: 0, reverts: 0)
11481148
@@ -1163,9 +1163,9 @@ Encountered 1 failing test in test/InvariantSequenceLenTest.t.sol:InvariantSeque
11631163
[FAIL: invariant_increment replay failure]
11641164
[Sequence] (original: 4, shrunk: 4)
11651165
sender=0x00000000000000000000000000000000000018dE addr=[src/Counter.sol:Counter]0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f calldata=setNumber(uint256) args=[1931387396117645594923 [1.931e21]]
1166-
sender=0x00000000000000000000000000000000000009d5 addr=[src/Counter.sol:Counter]0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f calldata=increment() args=[]
1167-
sender=0x0000000000000000000000000000000000000105 addr=[src/Counter.sol:Counter]0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f calldata=increment() args=[]
1168-
sender=0x00000000000000000000000000000000000009B2 addr=[src/Counter.sol:Counter]0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f calldata=setNumber(uint256) args=[996881781832960761274744263729582347 [9.968e35]]
1166+
sender=0x0000000000000000000000000000000000000C37 addr=[src/Counter.sol:Counter]0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f calldata=increment() args=[]
1167+
sender=0x0000000000000000000000000000000000000106 addr=[src/Counter.sol:Counter]0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f calldata=increment() args=[]
1168+
sender=0x0000000000000000000000000000000000001684 addr=[src/Counter.sol:Counter]0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f calldata=setNumber(uint256) args=[996881781832960761274744263729582347 [9.968e35]]
11691169
invariant_increment() (runs: 1, calls: 1, reverts: 1)
11701170
11711171
Encountered a total of 1 failing tests, 0 tests succeeded

crates/verify/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ regex = { workspace = true, default-features = false }
4040
yansi.workspace = true
4141
itertools.workspace = true
4242

43-
ciborium = "0.2"
44-
4543
[dev-dependencies]
4644
tokio = { workspace = true, features = ["macros"] }
4745
foundry-test-utils.workspace = true

crates/verify/src/utils.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use foundry_block_explorers::{
99
contract::{ContractCreationData, ContractMetadata, Metadata},
1010
errors::EtherscanError,
1111
};
12-
use foundry_common::{abi::encode_args, compile::ProjectCompiler, provider::RetryProvider, shell};
12+
use foundry_common::{
13+
abi::encode_args, compile::ProjectCompiler, ignore_metadata_hash, provider::RetryProvider,
14+
shell,
15+
};
1316
use foundry_compilers::artifacts::{BytecodeHash, CompactContractBytecode, EvmVersion};
1417
use foundry_config::Config;
1518
use foundry_evm::{
@@ -204,22 +207,6 @@ fn try_extract_and_compare_bytecode(mut local_bytecode: &[u8], mut bytecode: &[u
204207
local_bytecode == bytecode
205208
}
206209

207-
/// This assumes that the metadata is at the end of the bytecode.
208-
fn ignore_metadata_hash(bytecode: &[u8]) -> &[u8] {
209-
// Get the last two bytes of the bytecode to find the length of CBOR metadata.
210-
let Some((rest, metadata_len_bytes)) = bytecode.split_last_chunk() else { return bytecode };
211-
let metadata_len = u16::from_be_bytes(*metadata_len_bytes) as usize;
212-
if metadata_len > rest.len() {
213-
return bytecode;
214-
}
215-
let (rest, metadata) = rest.split_at(rest.len() - metadata_len);
216-
if ciborium::from_reader::<ciborium::Value, _>(metadata).is_ok() {
217-
rest
218-
} else {
219-
bytecode
220-
}
221-
}
222-
223210
fn find_mismatch_in_settings(
224211
etherscan_settings: &Metadata,
225212
local_settings: &Config,

0 commit comments

Comments
 (0)