1
1
# Fuzzing
2
2
3
- Fuzz tests generate a ton of random parameter arguments to the program and then validate that none
3
+ Fuzz tests generate a ton of random parameter arguments to the program and then validate that none
4
4
cause it to crash.
5
5
6
6
## How does it work?
7
7
8
8
Typically, CI will run ` ci-fuzz.sh ` on one of the environments the automated tests are
9
- configured for. Fuzzing is further only effective with a lot of CPU time, indicating that if crash
9
+ configured for. Fuzzing is further only effective with a lot of CPU time, indicating that if crash
10
10
scenarios are discovered on CI with its low runtime constraints, the crash is caused relatively
11
11
easily.
12
12
13
13
## How do I run fuzz tests locally?
14
14
15
- We support multiple fuzzing engines such as ` honggfuzz ` , ` libFuzzer ` and ` AFL ` . You typically won't
16
- need to run the entire suite of different fuzzing tools. For local execution, ` honggfuzz ` should be
17
- more than sufficient.
15
+ We support multiple fuzzing engines such as ` honggfuzz ` , ` libFuzzer ` and ` AFL ` . You typically won't
16
+ need to run the entire suite of different fuzzing tools. For local execution, ` honggfuzz ` should be
17
+ more than sufficient.
18
18
19
19
### Setup
20
20
#### Honggfuzz
@@ -63,12 +63,12 @@ To run fuzzing using `cargo-fuzz / libFuzzer`, run
63
63
rustup install nightly # Note: libFuzzer requires a nightly version of rust.
64
64
cargo +nightly fuzz run --features " libfuzzer_fuzz" msg_ping_target
65
65
```
66
- Note: If you encounter a ` SIGKILL ` during run/build check for OOM in kernel logs and consider
66
+ Note: If you encounter a ` SIGKILL ` during run/build check for OOM in kernel logs and consider
67
67
increasing RAM size for VM.
68
68
69
69
If you wish to just generate fuzzing binary executables for ` libFuzzer ` and not run them:
70
- ``` shell
71
- cargo +nightly fuzz build --features " libfuzzer_fuzz" msg_ping_target
70
+ ``` shell
71
+ cargo +nightly fuzz build --features " libfuzzer_fuzz" msg_ping_target
72
72
# Generates binary artifact in path ./target/aarch64-unknown-linux-gnu/release/msg_ping_target
73
73
# Exact path depends on your system architecture.
74
74
```
@@ -83,7 +83,7 @@ ls ./src/bin/
83
83
84
84
## A fuzz test failed, what do I do?
85
85
86
- You're trying to create a PR, but need to find the underlying cause of that pesky fuzz failure
86
+ You're trying to create a PR, but need to find the underlying cause of that pesky fuzz failure
87
87
blocking the merge?
88
88
89
89
Worry not, for this is easily traced.
@@ -106,7 +106,7 @@ The command "if [ "$(rustup show | grep default | grep stable)" != "" ]; then cd
106
106
```
107
107
108
108
Note that the penultimate stack trace line ends in ` release/full_stack_target] ` . That indicates that
109
- the failing target was ` full_stack ` . To reproduce the error locally, simply copy the hex,
109
+ the failing target was ` full_stack ` . To reproduce the error locally, simply copy the hex,
110
110
and run the following from the ` fuzz ` directory:
111
111
112
112
``` shell
@@ -119,11 +119,11 @@ mkdir -p ./test_cases/$TARGET
119
119
echo $HEX | xxd -r -p > ./test_cases/$TARGET /any_filename_works
120
120
121
121
export RUST_BACKTRACE=1
122
- export RUSTFLAGS=" --cfg=fuzzing"
122
+ export RUSTFLAGS=" --cfg=fuzzing --cfg=secp256k1_fuzz --cfg=hashes_fuzz "
123
123
cargo test
124
124
```
125
125
126
- Note that if the fuzz test failed locally, moving the offending run's trace
126
+ Note that if the fuzz test failed locally, moving the offending run's trace
127
127
to the ` test_cases ` folder should also do the trick; simply replace the ` echo $HEX | ` line above
128
128
with (the trace file name is of course a bit longer than in the example):
129
129
@@ -136,9 +136,9 @@ This will reproduce the failing fuzz input and yield a usable stack trace.
136
136
137
137
## How do I add a new fuzz test?
138
138
139
- 1 . The easiest approach is to take one of the files in ` fuzz/src/ ` , such as
140
- ` process_network_graph.rs ` , and duplicate it, renaming the new file to something more
141
- suitable. For the sake of example, let's call the new fuzz target we're creating
139
+ 1 . The easiest approach is to take one of the files in ` fuzz/src/ ` , such as
140
+ ` process_network_graph.rs ` , and duplicate it, renaming the new file to something more
141
+ suitable. For the sake of example, let's call the new fuzz target we're creating
142
142
` my_fuzzy_experiment ` .
143
143
144
144
2 . In the newly created file ` fuzz/src/my_fuzzy_experiment.rs ` , run a string substitution
@@ -147,12 +147,12 @@ file are `do_test`, `my_fuzzy_experiment_test`, and `my_fuzzy_experiment_run`.
147
147
148
148
3 . Adjust the body (not the signature!) of ` do_test ` as necessary for the new fuzz test.
149
149
150
- 4 . In ` fuzz/src/bin/gen_target.sh ` , add a line reading ` GEN_TEST my_fuzzy_experiment ` to the
150
+ 4 . In ` fuzz/src/bin/gen_target.sh ` , add a line reading ` GEN_TEST my_fuzzy_experiment ` to the
151
151
first group of ` GEN_TEST ` lines (starting in line 9).
152
152
153
153
5 . If your test relies on a new local crate, add that crate as a dependency to ` fuzz/Cargo.toml ` .
154
154
155
- 6 . In ` fuzz/src/lib.rs ` , add the line ` pub mod my_fuzzy_experiment ` . Additionally, if
155
+ 6 . In ` fuzz/src/lib.rs ` , add the line ` pub mod my_fuzzy_experiment ` . Additionally, if
156
156
you added a new crate dependency, add the ` extern crate […] ` import line.
157
157
158
158
7 . Run ` fuzz/src/bin/gen_target.sh ` .
0 commit comments