1use alloy_primitives::U256;
4use foundry_compilers::utils::canonicalized;
5use serde::{Deserialize, Deserializer, Serialize, de::Error};
6use std::path::PathBuf;
7
8#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
10pub struct FuzzConfig {
11 #[serde(deserialize_with = "deserialize_fuzz_runs")]
13 pub runs: u32,
14 pub run: Option<u32>,
16 pub worker: Option<u32>,
18 pub fail_on_revert: bool,
20 pub max_test_rejects: u32,
23 pub seed: Option<U256>,
25 #[serde(flatten)]
27 pub dictionary: FuzzDictionaryConfig,
28 pub gas_report_samples: u32,
30 #[serde(flatten)]
32 pub corpus: FuzzCorpusConfig,
33 pub failure_persist_dir: Option<PathBuf>,
35 pub show_logs: bool,
37 pub timeout: Option<u32>,
39}
40
41impl Default for FuzzConfig {
42 fn default() -> Self {
43 Self {
44 runs: 256,
45 run: None,
46 worker: None,
47 fail_on_revert: true,
48 max_test_rejects: 65536,
49 seed: None,
50 dictionary: FuzzDictionaryConfig::default(),
51 gas_report_samples: 256,
52 corpus: FuzzCorpusConfig { payable_value_weight: 0, ..Default::default() },
53 failure_persist_dir: None,
54 show_logs: false,
55 timeout: None,
56 }
57 }
58}
59
60impl FuzzConfig {
61 pub fn new(cache_dir: PathBuf) -> Self {
63 Self { failure_persist_dir: Some(cache_dir), ..Default::default() }
64 }
65}
66
67#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
69pub struct FuzzDictionaryConfig {
70 #[serde(deserialize_with = "crate::deserialize_stringified_percent")]
72 pub dictionary_weight: u32,
73 pub include_storage: bool,
75 pub include_push_bytes: bool,
77 pub include_view_values: bool,
82 #[serde(
87 deserialize_with = "crate::deserialize_usize_or_max",
88 serialize_with = "crate::serialize_usize_or_max"
89 )]
90 pub max_fuzz_dictionary_addresses: usize,
91 #[serde(
95 deserialize_with = "crate::deserialize_usize_or_max",
96 serialize_with = "crate::serialize_usize_or_max"
97 )]
98 pub max_fuzz_dictionary_values: usize,
99 #[serde(
103 deserialize_with = "crate::deserialize_usize_or_max",
104 serialize_with = "crate::serialize_usize_or_max"
105 )]
106 pub max_fuzz_dictionary_literals: usize,
107}
108
109impl Default for FuzzDictionaryConfig {
110 fn default() -> Self {
111 const MB: usize = 1024 * 1024;
112
113 Self {
114 dictionary_weight: 40,
115 include_storage: true,
116 include_push_bytes: true,
117 include_view_values: false,
118 max_fuzz_dictionary_addresses: 300 * MB / 20,
119 max_fuzz_dictionary_values: 300 * MB / 32,
120 max_fuzz_dictionary_literals: 200 * MB / 32,
121 }
122 }
123}
124
125#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
126pub struct FuzzCorpusConfig {
127 pub corpus_dir: Option<PathBuf>,
130 pub frontier_dir: Option<PathBuf>,
132 pub frontier_limit: usize,
134 pub corpus_gzip: bool,
136 pub corpus_min_mutations: usize,
139 pub corpus_min_size: usize,
141 pub show_edge_coverage: bool,
143 pub evm_edge_coverage_collision_free: bool,
145 pub evm_edge_coverage_include_call_depth: bool,
147 pub sancov_edges: bool,
151 pub sancov_trace_cmp: bool,
154 pub instrumented_edges: bool,
168 #[serde(deserialize_with = "crate::deserialize_stringified_percent")]
171 pub corpus_random_sequence_weight: u32,
172 #[serde(deserialize_with = "crate::deserialize_stringified_percent")]
174 pub payable_value_weight: u32,
175 #[serde(flatten)]
177 pub mutation_weights: FuzzCorpusMutationWeights,
178}
179
180impl FuzzCorpusConfig {
181 pub const DEFAULT_CORPUS_RANDOM_SEQUENCE_WEIGHT: u32 = 10;
182 pub const ENSEMBLE_CORPUS_RANDOM_SEQUENCE_WEIGHT: u32 = 50;
183 pub const DEFAULT_FRONTIER_LIMIT: usize = 256;
184
185 pub fn with_test(&mut self, contract: &str, test: &str) {
186 if let Some(corpus_dir) = &self.corpus_dir {
187 self.corpus_dir = Some(canonicalized(corpus_dir.join(contract).join(test)));
188 }
189 if let Some(frontier_dir) = &self.frontier_dir {
190 self.frontier_dir = Some(canonicalized(frontier_dir.join(contract).join(test)));
191 }
192 }
193
194 pub const fn collect_edge_coverage(&self) -> bool {
196 self.corpus_dir.is_some()
197 || self.show_edge_coverage
198 || self.sancov_edges
199 || self.instrumented_edges
200 }
201
202 pub const fn collect_evm_edge_coverage(&self) -> bool {
211 !self.sancov_edges
212 && !self.instrumented_edges
213 && (self.corpus_dir.is_some() || self.show_edge_coverage)
214 }
215
216 pub const fn collect_instrumented_edges(&self) -> bool {
218 self.instrumented_edges
219 }
220
221 pub fn collect_evm_cmp_log(&self) -> bool {
228 self.capture_branch_frontiers()
229 || (!self.sancov_edges
230 && self.corpus_dir.is_some()
231 && self.mutation_weights.effective().mutation_weight_cmp > 0)
232 }
233
234 pub const fn capture_branch_frontiers(&self) -> bool {
236 self.frontier_dir.is_some() && self.frontier_limit > 0
237 }
238
239 pub const fn evm_edge_coverage_collision_free(&self) -> bool {
241 self.evm_edge_coverage_collision_free
242 }
243
244 pub const fn evm_edge_coverage_include_call_depth(&self) -> bool {
246 self.evm_edge_coverage_include_call_depth
247 }
248
249 pub const fn collect_sancov_edges(&self) -> bool {
251 self.sancov_edges
252 }
253
254 pub const fn collect_sancov_trace_cmp(&self) -> bool {
256 self.sancov_trace_cmp
257 }
258
259 pub const fn sancov_active(&self) -> bool {
261 self.sancov_edges || self.sancov_trace_cmp
262 }
263
264 pub const fn is_coverage_guided(&self) -> bool {
266 self.corpus_dir.is_some()
267 }
268}
269
270impl Default for FuzzCorpusConfig {
271 fn default() -> Self {
272 Self {
273 corpus_dir: None,
274 frontier_dir: None,
275 frontier_limit: Self::DEFAULT_FRONTIER_LIMIT,
276 corpus_gzip: true,
277 corpus_min_mutations: 5,
278 corpus_min_size: 0,
279 show_edge_coverage: false,
280 evm_edge_coverage_collision_free: true,
281 evm_edge_coverage_include_call_depth: false,
282 sancov_edges: false,
283 sancov_trace_cmp: false,
284 instrumented_edges: false,
285 corpus_random_sequence_weight: Self::DEFAULT_CORPUS_RANDOM_SEQUENCE_WEIGHT,
286 payable_value_weight: 15,
287 mutation_weights: FuzzCorpusMutationWeights::default(),
288 }
289 }
290}
291
292#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
293pub struct FuzzCorpusMutationWeights {
294 pub mutation_weight_splice: u32,
296 pub mutation_weight_repeat: u32,
298 pub mutation_weight_interleave: u32,
300 pub mutation_weight_prefix: u32,
302 pub mutation_weight_suffix: u32,
304 pub mutation_weight_abi: u32,
306 pub mutation_weight_cmp: u32,
308}
309
310impl FuzzCorpusMutationWeights {
311 pub const fn total(&self) -> u64 {
312 self.mutation_weight_splice as u64
313 + self.mutation_weight_repeat as u64
314 + self.mutation_weight_interleave as u64
315 + self.mutation_weight_prefix as u64
316 + self.mutation_weight_suffix as u64
317 + self.mutation_weight_abi as u64
318 + self.mutation_weight_cmp as u64
319 }
320
321 pub fn effective(self) -> Self {
323 if self.total() == 0 { Self::default() } else { self }
324 }
325}
326
327impl Default for FuzzCorpusMutationWeights {
328 fn default() -> Self {
329 Self {
330 mutation_weight_splice: 1,
331 mutation_weight_repeat: 1,
332 mutation_weight_interleave: 1,
333 mutation_weight_prefix: 1,
334 mutation_weight_suffix: 1,
335 mutation_weight_abi: 1,
336 mutation_weight_cmp: 1,
337 }
338 }
339}
340
341fn deserialize_fuzz_runs<'de, D>(deserializer: D) -> Result<u32, D::Error>
342where
343 D: Deserializer<'de>,
344{
345 let runs = u32::deserialize(deserializer)?;
346 if runs == 0 {
347 return Err(D::Error::custom("`fuzz.runs` must be greater than 0"));
348 }
349 Ok(runs)
350}
351
352#[cfg(test)]
353mod tests {
354 use super::*;
355
356 #[test]
357 fn instrumented_edges_replace_evm_edge_coverage() {
358 let corpus = FuzzCorpusConfig {
359 corpus_dir: Some("corpus".into()),
360 instrumented_edges: true,
361 ..Default::default()
362 };
363 assert!(corpus.collect_edge_coverage());
364 assert!(corpus.collect_instrumented_edges());
365 assert!(!corpus.collect_evm_edge_coverage());
366 assert!(corpus.collect_evm_cmp_log());
368
369 let default = FuzzCorpusConfig { corpus_dir: Some("corpus".into()), ..Default::default() };
370 assert!(!default.collect_instrumented_edges());
371 assert!(default.collect_evm_edge_coverage());
372 }
373}