Skip to content

Commit d18a07d

Browse files
Add fuzz tests
Added fuzzing tests: - As unit tests using `QuickCheck` - As `cargo fuzz` tests Both sets of tests use the same set fuzzing functions, running `cargo fuzz` is just a lot more intense.
1 parent 86ef42f commit d18a07d

12 files changed

+533
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ thiserror = "1.0.30"
2222

2323
[dev-dependencies]
2424
hex = "0.4.3"
25+
quickcheck = "1"

fuzz/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
target
2+
corpus
3+
artifacts
4+
coverage

fuzz/Cargo.lock

Lines changed: 129 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fuzz/Cargo.toml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
[package]
2+
name = "monero-fuzz"
3+
version = "0.0.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[package.metadata]
8+
cargo-fuzz = true
9+
10+
[dependencies]
11+
libfuzzer-sys = "0.4"
12+
13+
[dependencies.randomx-rs]
14+
path = ".."
15+
16+
# Prevent this from interfering with workspaces
17+
[workspace]
18+
members = ["."]
19+
20+
[profile.release]
21+
debug = 1
22+
23+
[[bin]]
24+
name = "randomx_alloc_cache"
25+
path = "fuzz_targets/randomx_alloc_cache.rs"
26+
test = false
27+
doc = false
28+
29+
[[bin]]
30+
name = "randomx_create_vm_with_cache_only"
31+
path = "fuzz_targets/randomx_create_vm_with_cache_only.rs"
32+
test = false
33+
doc = false
34+
35+
[[bin]]
36+
name = "randomx_create_vm_with_cache_and_dataset"
37+
path = "fuzz_targets/randomx_create_vm_with_cache_and_dataset.rs"
38+
test = false
39+
doc = false
40+
41+
[[bin]]
42+
name = "randomx_vm_calculate_hash_with_cache_only"
43+
path = "fuzz_targets/randomx_vm_calculate_hash_with_cache_only.rs"
44+
test = false
45+
doc = false
46+
47+
[[bin]]
48+
name = "randomx_vm_calculate_hash_with_cache_and_dataset"
49+
path = "fuzz_targets/randomx_vm_calculate_hash_with_cache_and_dataset.rs"
50+
test = false
51+
doc = false

fuzz/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Fuzzing randomx-rs
2+
3+
See https://rust-fuzz.github.io/book/cargo-fuzz.html for more information on fuzzing with cargo-fuzz.
4+
Install `cargo-fuzz` as per [installation instructions](https://rust-fuzz.github.io/book/cargo-fuzz/setup.html).
5+
6+
7+
**Note:** Fuzzing is not supported on Windows yet.
8+
9+
To get a list of fuzz targets, from a terminal in the project root, run
10+
```
11+
cargo fuzz list
12+
```
13+
14+
To run a fuzz test, from a terminal in the project root, run
15+
```
16+
cargo +nightly fuzz run --release <fuzz_target_name>
17+
```
18+
To run fuzz tests involving a cache and dataset, on error `libFuzzer: out-of-memory (malloc(2181038016))`, pass
19+
`-- -rss_limit_mb=<ram_upper_limit>` as argument to allow using more than 2 GB of RAM - 3GB recommended.
20+
```
21+
cargo +nightly fuzz run --release <fuzz_target_name> -- -rss_limit_mb=3221225472
22+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![no_main]
2+
3+
use libfuzzer_sys::fuzz_target;
4+
use randomx_rs::test_utils::fuzz_randomx_alloc_cache;
5+
6+
fuzz_target!(|data: &[u8]| {
7+
fuzz_randomx_alloc_cache(data.to_vec());
8+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![no_main]
2+
3+
use libfuzzer_sys::fuzz_target;
4+
use randomx_rs::test_utils::fuzz_randomx_create_vm_with_cache_and_dataset;
5+
6+
fuzz_target!(|data: &[u8]| {
7+
fuzz_randomx_create_vm_with_cache_and_dataset(data.to_vec());
8+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![no_main]
2+
3+
use libfuzzer_sys::fuzz_target;
4+
use randomx_rs::test_utils::fuzz_randomx_create_vm_with_cache_only;
5+
6+
fuzz_target!(|data: &[u8]| {
7+
fuzz_randomx_create_vm_with_cache_only(data.to_vec());
8+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![no_main]
2+
3+
use libfuzzer_sys::fuzz_target;
4+
use randomx_rs::test_utils::fuzz_randomx_vm_calculate_hash_with_cache_and_dataset;
5+
6+
fuzz_target!(|data: &[u8]| {
7+
fuzz_randomx_vm_calculate_hash_with_cache_and_dataset(data.to_vec());
8+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![no_main]
2+
3+
use libfuzzer_sys::fuzz_target;
4+
use randomx_rs::test_utils::fuzz_randomx_vm_calculate_hash_with_cache_only;
5+
6+
fuzz_target!(|data: &[u8]| {
7+
fuzz_randomx_vm_calculate_hash_with_cache_only(data.to_vec());
8+
});

0 commit comments

Comments
 (0)