Skip to content

Commit

Permalink
solana: Add fuzz harness for RateLimitState
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsaigle committed Apr 10, 2024
1 parent 76211b2 commit 89d20bc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion solana/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]
members = [
"programs/*",
"modules/*"
"modules/*",
]
resolver = "2"

Expand Down
8 changes: 6 additions & 2 deletions solana/fuzz/src/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"] }

Expand Down
13 changes: 13 additions & 0 deletions solana/fuzz/src/fuzz_rate_limit.rs
Original file line number Diff line number Diff line change
@@ -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)
});
}
}
2 changes: 2 additions & 0 deletions solana/modules/ntt-messages/src/trimmed_amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 89d20bc

Please sign in to comment.