From 1242af8e66f06a63702720c3f250e696d5923776 Mon Sep 17 00:00:00 2001 From: John Saigle Date: Tue, 5 Mar 2024 15:16:24 -0500 Subject: [PATCH] solana: Add fuzz harness for RateLimitState --- solana/Cargo.toml | 2 +- solana/fuzz/src/Cargo.toml | 8 ++++++-- solana/fuzz/src/fuzz_rate_limit.rs | 13 +++++++++++++ solana/modules/ntt-messages/src/trimmed_amount.rs | 2 ++ 4 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 solana/fuzz/src/fuzz_rate_limit.rs diff --git a/solana/Cargo.toml b/solana/Cargo.toml index 49269f5c1..572946884 100644 --- a/solana/Cargo.toml +++ b/solana/Cargo.toml @@ -1,7 +1,7 @@ [workspace] members = [ "programs/*", - "modules/*" + "modules/*", ] resolver = "2" diff --git a/solana/fuzz/src/Cargo.toml b/solana/fuzz/src/Cargo.toml index 36d7d723f..5d0bac850 100644 --- a/solana/fuzz/src/Cargo.toml +++ b/solana/fuzz/src/Cargo.toml @@ -1,14 +1,18 @@ [package] name = "ntt-fuzz" version = "0.0.1" +edition = "2021" [[bin]] -name = "ntt-fuzz" +name = "fuzz-trimmed-amount" path = "fuzz_trimmed_amount.rs" +[[bin]] +name = "fuzz-rate-limit-state" +path = "fuzz_rate_limit.rs" -edition = "2021" [dependencies] ntt-messages = { path = "../../modules/ntt-messages" } +example-native-token-transfers = { path = "../../programs/example-native-token-transfers" } honggfuzz = "0.5" arbitrary = { version = "1", optional = true, features = ["derive"] } diff --git a/solana/fuzz/src/fuzz_rate_limit.rs b/solana/fuzz/src/fuzz_rate_limit.rs new file mode 100644 index 000000000..7b01c86ee --- /dev/null +++ b/solana/fuzz/src/fuzz_rate_limit.rs @@ -0,0 +1,13 @@ +use honggfuzz::fuzz; +use example_native_token_transfers::queue::rate_limit::RateLimitState; + +fn main() { + loop { + fuzz!(|input: (u64, u64)| { + let (limit, new_limit) = input; + + let mut rls = RateLimitState::new(limit); + rls.set_limit(new_limit) + }); + } +} diff --git a/solana/modules/ntt-messages/src/trimmed_amount.rs b/solana/modules/ntt-messages/src/trimmed_amount.rs index 9bfa03694..877ca6328 100644 --- a/solana/modules/ntt-messages/src/trimmed_amount.rs +++ b/solana/modules/ntt-messages/src/trimmed_amount.rs @@ -61,6 +61,7 @@ impl TrimmedAmount { return Ok(amount); } if from_decimals > to_decimals { + // [`u64::checked_pow`] expects a u32 argument let power: u32 = (from_decimals - to_decimals).into(); // Exponentiation will overflow u64 when `power` is greater than 18 let scaling_factor: u64 = 10u64 @@ -69,6 +70,7 @@ impl TrimmedAmount { Ok(amount / scaling_factor) } else { + // [`u64::checked_pow`] expects a u32 argument let power: u32 = (to_decimals - from_decimals).into(); // Exponentiation will overflow u64 when `power` is greater than 18