1#![deny(unsafe_code)]
2#![allow(missing_docs)]
3
4pub mod backend;
20pub mod config;
21pub mod fuzz;
22pub mod generator;
23pub mod idl;
24pub mod invariant;
25pub mod model;
26
27#[cfg(test)]
28mod testing;
29
30pub use backend::SvmBackend;
31pub use config::{parse_plan, ConfigError, FuzzPlan, GenesisAccount};
32pub use fuzz::{format_poc, fuzz, run_sequence, shrink, FuzzConfig, Step, Violation};
33pub use generator::AccountPool;
34pub use idl::{anchor_discriminator, parse_idl, IdlError, IdlProgram, SkippedInstruction};
35pub use invariant::{AccountOwnerInvariant, SolanaInvariant, TokenConservationInvariant};
36pub use model::{
37 AccountMeta, AccountRole, ArgKind, Instruction, InstructionSpec, IxOutcome, Pubkey,
38};
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43 use crate::testing::{
44 token_functions_safe, token_functions_vulnerable, MockSvm, AMOUNT_OFFSET, MINT,
45 SUPPLY_OFFSET, TOKEN_ACCT_1, TOKEN_ACCT_2, TOKEN_ACCT_3, TOKEN_PROGRAM,
46 };
47
48 fn pool() -> AccountPool {
49 AccountPool::new(
50 vec![crate::testing::AUTHORITY],
51 vec![TOKEN_ACCT_1, TOKEN_ACCT_2, TOKEN_ACCT_3, MINT],
52 vec![],
53 )
54 }
55
56 fn conservation() -> Box<dyn SolanaInvariant> {
57 Box::new(TokenConservationInvariant::new(
58 "SPL conservation: sum(amounts) == mint supply",
59 MINT,
60 vec![TOKEN_ACCT_1, TOKEN_ACCT_2, TOKEN_ACCT_3],
61 AMOUNT_OFFSET,
62 SUPPLY_OFFSET,
63 ))
64 }
65
66 #[test]
67 fn catches_token_conservation_violation_and_shrinks_it() {
68 let specs = token_functions_vulnerable();
69 let invariants: Vec<Box<dyn SolanaInvariant>> = vec![conservation()];
70 let config = FuzzConfig {
71 seed: 1,
72 max_runs: 300,
73 sequence_depth: 8,
74 };
75
76 let violation = fuzz(
77 || Box::new(MockSvm::default()),
78 TOKEN_PROGRAM,
79 &specs,
80 &pool(),
81 invariants,
82 config,
83 )
84 .expect("the buggy airdrop credits without updating supply — the fuzzer must catch it");
85
86 assert!(violation.invariant_name.contains("conservation"));
87 assert!(violation.message.contains("mint supply"));
88 assert!(
91 violation.sequence.len() <= 3,
92 "expected a minimal reproduction, got {} steps",
93 violation.sequence.len()
94 );
95 assert!(
96 violation
97 .sequence
98 .iter()
99 .any(|(ix, _)| ix.data.first() == Some(&crate::testing::IX_AIRDROP)),
100 "the airdrop instruction must be in the minimal reproduction"
101 );
102 }
103
104 #[test]
105 fn no_false_positive_when_only_the_correct_mint_is_available() {
106 let specs = token_functions_safe();
107 let invariants: Vec<Box<dyn SolanaInvariant>> = vec![conservation()];
108 let config = FuzzConfig {
109 seed: 7,
110 max_runs: 300,
111 sequence_depth: 10,
112 };
113
114 let result = fuzz(
115 || Box::new(MockSvm::default()),
116 TOKEN_PROGRAM,
117 &specs,
118 &pool(),
119 invariants,
120 config,
121 );
122 assert!(
123 result.is_none(),
124 "the correct mint updates supply in lockstep — conservation must hold"
125 );
126 }
127
128 #[test]
129 fn catches_unchecked_owner_reassignment() {
130 let specs = vec![InstructionSpec::new(
134 "reassign",
135 vec![crate::testing::IX_REASSIGN_UNCHECKED],
136 vec![ArgKind::Pubkey],
137 vec![AccountRole::Writable],
138 true,
139 )];
140 let invariants: Vec<Box<dyn SolanaInvariant>> = vec![Box::new(
141 AccountOwnerInvariant::track("owner integrity", TOKEN_ACCT_1),
142 )];
143 let config = FuzzConfig {
144 seed: 3,
145 max_runs: 200,
146 sequence_depth: 4,
147 };
148
149 let violation = fuzz(
150 || Box::new(MockSvm::default()),
151 TOKEN_PROGRAM,
152 &specs,
153 &AccountPool::new(vec![crate::testing::AUTHORITY], vec![TOKEN_ACCT_1], vec![]),
154 invariants,
155 config,
156 )
157 .expect("unchecked owner reassignment must be caught");
158 assert!(violation.message.contains("owner changed"));
159 }
160}