Skip to content

Commit 2f4c01a

Browse files
committed
solana: Add overflow checks, tests, and fuzzing
Add simple fuzz harness for TrimmedAmount::trim Add overflow checks for issues discovered while fuzzing Add unit tests to verify that errors are returned from overflow checks Add new errors (in ntt-messages and in the Anchor custom errors) Add some additional documentation for functions
1 parent 9a6c221 commit 2f4c01a

File tree

10 files changed

+327
-29
lines changed

10 files changed

+327
-29
lines changed

solana/fuzz/src/Cargo.lock

+128
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

solana/fuzz/src/Cargo.toml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "ntt-fuzz"
3+
version = "0.0.1"
4+
5+
[[bin]]
6+
name = "ntt-fuzz"
7+
path = "fuzz_trimmed_amount.rs"
8+
9+
edition = "2021"
10+
[dependencies]
11+
ntt-messages = { path = "../../modules/ntt-messages" }
12+
honggfuzz = "0.5"
13+
arbitrary = { version = "1", optional = true, features = ["derive"] }
14+
15+
[workspace]

solana/fuzz/src/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Fuzzing
2+
3+
Requires honggfuzz. Fuzz tests will not run on Apple Silicon.
4+
5+
## Install
6+
7+
```bash
8+
cargo install honggfuzz
9+
```
10+
11+
## Build
12+
13+
```bash
14+
# in solana/fuzz/src
15+
cargo hfuzz build
16+
```
17+
18+
## Run
19+
20+
```bash
21+
cargo hfuzz run ntt-fuzz
22+
```
23+
24+
As more targets are added, other targets for `run` can be found and added as `bins` defined in `Cargo.toml`.
25+
`name` corresponds to the binary used by `cargo hfuzz run`.
26+
```toml
27+
...
28+
[[bin]]
29+
name = "ntt-fuzz"
30+
...
31+
```
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use honggfuzz::fuzz;
2+
use ntt_messages::trimmed_amount::TrimmedAmount;
3+
4+
// #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
5+
6+
fn main() {
7+
loop {
8+
fuzz!(|input: (u64, u8, u8)| {
9+
10+
let _ = TrimmedAmount::trim(input.0, input.1, input.2);
11+
});
12+
}
13+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::fmt::{Display, Formatter};
2+
3+
#[derive(Debug, PartialEq)]
4+
pub enum ScalingError {
5+
OverflowExponent,
6+
OverflowScaledAmount,
7+
}
8+
9+
impl std::error::Error for ScalingError {}
10+
11+
impl Display for ScalingError {
12+
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
13+
match self {
14+
ScalingError::OverflowExponent => write!(
15+
f,
16+
"Overflow: scaling factor exponent exceeds the max value of u64"
17+
),
18+
ScalingError::OverflowScaledAmount => {
19+
write!(f, "Overflow: scaled amount exceeds the max value of u64")
20+
}
21+
}
22+
}
23+
}

solana/modules/ntt-messages/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod chain_id;
2+
pub mod errors;
23
pub mod mode;
34
pub mod ntt;
45
pub mod ntt_manager;

0 commit comments

Comments
 (0)