1use alloy_primitives::U256;
4use foundry_compilers::utils::canonicalized;
5use serde::{Deserialize, Serialize};
6use std::path::PathBuf;
7
8#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
10pub struct FuzzConfig {
11 pub runs: u32,
13 pub run: Option<u32>,
15 pub worker: Option<u32>,
17 pub fail_on_revert: bool,
19 pub max_test_rejects: u32,
22 pub seed: Option<U256>,
24 #[serde(flatten)]
26 pub dictionary: FuzzDictionaryConfig,
27 pub gas_report_samples: u32,
29 #[serde(flatten)]
31 pub corpus: FuzzCorpusConfig,
32 pub failure_persist_dir: Option<PathBuf>,
34 pub show_logs: bool,
36 pub timeout: Option<u32>,
38}
39
40impl Default for FuzzConfig {
41 fn default() -> Self {
42 Self {
43 runs: 256,
44 run: None,
45 worker: None,
46 fail_on_revert: true,
47 max_test_rejects: 65536,
48 seed: None,
49 dictionary: FuzzDictionaryConfig::default(),
50 gas_report_samples: 256,
51 corpus: FuzzCorpusConfig { payable_value_weight: 0, ..Default::default() },
52 failure_persist_dir: None,
53 show_logs: false,
54 timeout: None,
55 }
56 }
57}
58
59impl FuzzConfig {
60 pub fn new(cache_dir: PathBuf) -> Self {
62 Self { failure_persist_dir: Some(cache_dir), ..Default::default() }
63 }
64}
65
66#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
68pub struct FuzzDictionaryConfig {
69 #[serde(deserialize_with = "crate::deserialize_stringified_percent")]
71 pub dictionary_weight: u32,
72 pub include_storage: bool,
74 pub include_push_bytes: bool,
76 #[serde(
81 deserialize_with = "crate::deserialize_usize_or_max",
82 serialize_with = "crate::serialize_usize_or_max"
83 )]
84 pub max_fuzz_dictionary_addresses: usize,
85 #[serde(
88 deserialize_with = "crate::deserialize_usize_or_max",
89 serialize_with = "crate::serialize_usize_or_max"
90 )]
91 pub max_fuzz_dictionary_values: usize,
92 #[serde(
96 deserialize_with = "crate::deserialize_usize_or_max",
97 serialize_with = "crate::serialize_usize_or_max"
98 )]
99 pub max_fuzz_dictionary_literals: usize,
100}
101
102impl Default for FuzzDictionaryConfig {
103 fn default() -> Self {
104 const MB: usize = 1024 * 1024;
105
106 Self {
107 dictionary_weight: 40,
108 include_storage: true,
109 include_push_bytes: true,
110 max_fuzz_dictionary_addresses: 300 * MB / 20,
111 max_fuzz_dictionary_values: 300 * MB / 32,
112 max_fuzz_dictionary_literals: 200 * MB / 32,
113 }
114 }
115}
116
117#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
118pub struct FuzzCorpusConfig {
119 pub corpus_dir: Option<PathBuf>,
122 pub frontier_dir: Option<PathBuf>,
124 pub frontier_limit: usize,
126 pub corpus_gzip: bool,
128 pub corpus_min_mutations: usize,
131 pub corpus_min_size: usize,
133 pub show_edge_coverage: bool,
135 pub evm_edge_coverage_collision_free: bool,
137 pub evm_edge_coverage_include_call_depth: bool,
139 pub sancov_edges: bool,
143 pub sancov_trace_cmp: bool,
146 #[serde(deserialize_with = "crate::deserialize_stringified_percent")]
149 pub corpus_random_sequence_weight: u32,
150 #[serde(deserialize_with = "crate::deserialize_stringified_percent")]
152 pub payable_value_weight: u32,
153 #[serde(flatten)]
155 pub mutation_weights: FuzzCorpusMutationWeights,
156}
157
158impl FuzzCorpusConfig {
159 pub const DEFAULT_CORPUS_RANDOM_SEQUENCE_WEIGHT: u32 = 10;
160 pub const ENSEMBLE_CORPUS_RANDOM_SEQUENCE_WEIGHT: u32 = 50;
161 pub const DEFAULT_FRONTIER_LIMIT: usize = 256;
162
163 pub fn with_test(&mut self, contract: &str, test: &str) {
164 if let Some(corpus_dir) = &self.corpus_dir {
165 self.corpus_dir = Some(canonicalized(corpus_dir.join(contract).join(test)));
166 }
167 if let Some(frontier_dir) = &self.frontier_dir {
168 self.frontier_dir = Some(canonicalized(frontier_dir.join(contract).join(test)));
169 }
170 }
171
172 pub const fn collect_edge_coverage(&self) -> bool {
174 self.corpus_dir.is_some() || self.show_edge_coverage || self.sancov_edges
175 }
176
177 pub const fn collect_evm_edge_coverage(&self) -> bool {
184 !self.sancov_edges && (self.corpus_dir.is_some() || self.show_edge_coverage)
185 }
186
187 pub fn collect_evm_cmp_log(&self) -> bool {
194 self.capture_branch_frontiers()
195 || (!self.sancov_edges
196 && self.corpus_dir.is_some()
197 && self.mutation_weights.effective().mutation_weight_cmp > 0)
198 }
199
200 pub const fn capture_branch_frontiers(&self) -> bool {
202 self.frontier_dir.is_some() && self.frontier_limit > 0
203 }
204
205 pub const fn evm_edge_coverage_collision_free(&self) -> bool {
207 self.evm_edge_coverage_collision_free
208 }
209
210 pub const fn evm_edge_coverage_include_call_depth(&self) -> bool {
212 self.evm_edge_coverage_include_call_depth
213 }
214
215 pub const fn collect_sancov_edges(&self) -> bool {
217 self.sancov_edges
218 }
219
220 pub const fn collect_sancov_trace_cmp(&self) -> bool {
222 self.sancov_trace_cmp
223 }
224
225 pub const fn sancov_active(&self) -> bool {
227 self.sancov_edges || self.sancov_trace_cmp
228 }
229
230 pub const fn is_coverage_guided(&self) -> bool {
232 self.corpus_dir.is_some()
233 }
234}
235
236impl Default for FuzzCorpusConfig {
237 fn default() -> Self {
238 Self {
239 corpus_dir: None,
240 frontier_dir: None,
241 frontier_limit: Self::DEFAULT_FRONTIER_LIMIT,
242 corpus_gzip: true,
243 corpus_min_mutations: 5,
244 corpus_min_size: 0,
245 show_edge_coverage: false,
246 evm_edge_coverage_collision_free: true,
247 evm_edge_coverage_include_call_depth: false,
248 sancov_edges: false,
249 sancov_trace_cmp: false,
250 corpus_random_sequence_weight: Self::DEFAULT_CORPUS_RANDOM_SEQUENCE_WEIGHT,
251 payable_value_weight: 15,
252 mutation_weights: FuzzCorpusMutationWeights::default(),
253 }
254 }
255}
256
257#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
258pub struct FuzzCorpusMutationWeights {
259 pub mutation_weight_splice: u32,
261 pub mutation_weight_repeat: u32,
263 pub mutation_weight_interleave: u32,
265 pub mutation_weight_prefix: u32,
267 pub mutation_weight_suffix: u32,
269 pub mutation_weight_abi: u32,
271 pub mutation_weight_cmp: u32,
273}
274
275impl FuzzCorpusMutationWeights {
276 pub const fn total(&self) -> u64 {
277 self.mutation_weight_splice as u64
278 + self.mutation_weight_repeat as u64
279 + self.mutation_weight_interleave as u64
280 + self.mutation_weight_prefix as u64
281 + self.mutation_weight_suffix as u64
282 + self.mutation_weight_abi as u64
283 + self.mutation_weight_cmp as u64
284 }
285
286 pub fn effective(self) -> Self {
288 if self.total() == 0 { Self::default() } else { self }
289 }
290}
291
292impl Default for FuzzCorpusMutationWeights {
293 fn default() -> Self {
294 Self {
295 mutation_weight_splice: 1,
296 mutation_weight_repeat: 1,
297 mutation_weight_interleave: 1,
298 mutation_weight_prefix: 1,
299 mutation_weight_suffix: 1,
300 mutation_weight_abi: 1,
301 mutation_weight_cmp: 1,
302 }
303 }
304}