wafrift_evolution/search/mod.rs
1//! Search algorithms for evolutionary WAF bypass discovery.
2
3use crate::evolution::{Chromosome, GenePool};
4use crate::types::{Budget, EvolutionError, OracleVerdict, SearchStats};
5use rand::rngs::StdRng;
6
7/// A candidate requested for evaluation, with a stable evaluation ID.
8#[derive(Debug, Clone)]
9pub struct EvalCandidate {
10 /// Stable ID used to correlate results.
11 pub id: u64,
12 /// The chromosome to evaluate.
13 pub chromosome: Chromosome,
14}
15
16/// Result of submitting evaluations back to the algorithm.
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum SubmitResult {
19 /// The algorithm accepted all results.
20 Accepted,
21 /// Some evaluation IDs were unknown.
22 UnknownIds(usize),
23}
24
25/// Core trait implemented by all search algorithms.
26///
27/// Each algorithm manages its own internal state (population, archive,
28/// temperature, tabu list, etc.). The [`EvolutionEngine`](crate::evolution::EvolutionEngine)
29/// handles caching, budgeting, and batching on top of this trait.
30pub trait SearchAlgorithm: Send + Sync + std::fmt::Debug {
31 /// Algorithm name.
32 fn name(&self) -> &'static str;
33
34 /// Initialize the algorithm with a seed population.
35 fn initialize(&mut self, population: Vec<Chromosome>, gene_pool: &GenePool, rng: &mut StdRng);
36
37 /// Request up to `n` candidates for parallel evaluation.
38 ///
39 /// Returns candidates with stable IDs. The caller evaluates them and
40 /// later calls [`submit_evaluations`](SearchAlgorithm::submit_evaluations).
41 fn request_evaluations(&mut self, n: usize, rng: &mut StdRng) -> Vec<EvalCandidate>;
42
43 /// Submit evaluation results.
44 ///
45 /// The ID in each tuple must match an ID previously returned by
46 /// `request_evaluations`.
47 fn submit_evaluations(&mut self, results: Vec<(u64, OracleVerdict)>);
48
49 /// Check whether the algorithm thinks search should stop.
50 fn should_terminate(&self, stats: &SearchStats, budget: &Budget) -> bool;
51
52 /// Get the best chromosome found so far.
53 fn best(&self) -> Option<&Chromosome>;
54
55 /// Serialize internal state to bytes.
56 fn checkpoint(&self) -> Result<Vec<u8>, EvolutionError>;
57
58 /// Restore internal state from bytes.
59 fn restore(&mut self, bytes: &[u8]) -> Result<(), EvolutionError>;
60}
61
62pub mod hill_climb;
63pub mod map_elites;
64pub mod novelty;
65pub mod sim_anneal;
66pub mod tabu;
67
68pub use hill_climb::HillClimbing;
69pub use map_elites::MapElites;
70pub use novelty::NoveltySearch;
71pub use sim_anneal::SimulatedAnnealing;
72pub use tabu::TabuSearch;