@@ -10,6 +10,11 @@ configured for. Fuzzing is further only effective with a lot of CPU time, indica
1010scenarios are discovered on CI with its low runtime constraints, the crash is caused relatively
1111easily.
1212
13+ The ` fuzz/ ` directory now contains three crates:
14+ - ` fuzz/ ` , the shared fuzz target logic and corpus directories
15+ - ` fuzz/fuzz-fake-hashes ` , the fuzz targets that require ` --cfg=hashes_fuzz `
16+ - ` fuzz/fuzz-real-hashes ` , the real-hashes fuzz targets, currently ` chanmon_consistency_target `
17+
1318## How do I run fuzz tests locally?
1419
1520We support multiple fuzzing engines such as ` honggfuzz ` , ` libFuzzer ` and ` AFL ` . You typically won't
@@ -47,34 +52,46 @@ cargo install --force cargo-fuzz
4752To run fuzzing using ` honggfuzz ` , do
4853
4954``` shell
55+ cd fuzz
5056export CPU_COUNT=1 # replace as needed
5157export HFUZZ_BUILD_ARGS=" --features honggfuzz_fuzz"
5258export HFUZZ_RUN_ARGS=" -n $CPU_COUNT --exit_upon_crash"
59+ export HFUZZ_WORKSPACE=" ./hfuzz_workspace"
5360
5461export TARGET=" msg_ping_target" # replace with the target to be fuzzed
55- cargo hfuzz run $TARGET
62+ export RUSTFLAGS=" --cfg=fuzzing --cfg=secp256k1_fuzz --cfg=hashes_fuzz"
63+ cargo hfuzz run --manifest-path fuzz-fake-hashes/Cargo.toml $TARGET
5664```
5765
58- (Or, for a prettier output, replace the last line with ` cargo --color always hfuzz run $TARGET ` .)
66+ (For ` fuzz-real-hashes ` , use
67+ ` RUSTFLAGS="--cfg=fuzzing --cfg=secp256k1_fuzz" cargo hfuzz run --manifest-path fuzz-real-hashes/Cargo.toml chanmon_consistency_target ` .)
68+ For a prettier output, replace the last line with
69+ ` cargo --color always hfuzz run --manifest-path fuzz-fake-hashes/Cargo.toml $TARGET ` .
5970
6071#### cargo-fuzz / libFuzzer
6172To run fuzzing using ` cargo-fuzz / libFuzzer ` , run
6273
6374``` shell
6475rustup install nightly # Note: libFuzzer requires a nightly version of rust.
76+ cd fuzz
6577export RUSTFLAGS=" --cfg=fuzzing --cfg=secp256k1_fuzz --cfg=hashes_fuzz"
66- cargo +nightly fuzz run --features " libfuzzer_fuzz" msg_ping_target
78+ cargo +nightly fuzz run --manifest-path fuzz-fake-hashes/Cargo.toml -- features " libfuzzer_fuzz" msg_ping_target
6779```
6880Note: If you encounter a ` SIGKILL ` during run/build check for OOM in kernel logs and consider
6981increasing RAM size for VM.
7082
83+ For ` fuzz-real-hashes ` , use
84+ ` RUSTFLAGS="--cfg=fuzzing --cfg=secp256k1_fuzz" cargo +nightly fuzz run --manifest-path fuzz-real-hashes/Cargo.toml --features "libfuzzer_fuzz" chanmon_consistency_target ` .
85+
7186##### Fast builds for development
7287
7388The default build uses LTO and single codegen unit, which is slow. For faster iteration during
7489development, use the ` -D ` (dev) flag:
7590
7691``` shell
77- cargo +nightly fuzz run --features " libfuzzer_fuzz" -D msg_ping_target
92+ cd fuzz
93+ RUSTFLAGS=" --cfg=fuzzing --cfg=secp256k1_fuzz --cfg=hashes_fuzz" \
94+ cargo +nightly fuzz run --manifest-path fuzz-fake-hashes/Cargo.toml --features " libfuzzer_fuzz" -D msg_ping_target
7895```
7996
8097The ` -D ` flag builds in development mode with faster compilation (still has optimizations via
@@ -83,7 +100,9 @@ sanitizer instrumentation, but subsequent builds will be fast.
83100
84101If you wish to just generate fuzzing binary executables for ` libFuzzer ` and not run them:
85102``` shell
86- cargo +nightly fuzz build --features " libfuzzer_fuzz" msg_ping_target
103+ cd fuzz
104+ RUSTFLAGS=" --cfg=fuzzing --cfg=secp256k1_fuzz --cfg=hashes_fuzz" \
105+ cargo +nightly fuzz build --manifest-path fuzz-fake-hashes/Cargo.toml --features " libfuzzer_fuzz" msg_ping_target
87106# Generates binary artifact in path ./target/aarch64-unknown-linux-gnu/release/msg_ping_target
88107# Exact path depends on your system architecture.
89108```
@@ -93,7 +112,8 @@ You can upload the build artifact generated above to `ClusterFuzz` for distribut
93112To see a list of available fuzzing targets, run:
94113
95114``` shell
96- ls ./src/bin/
115+ ls ./fuzz-fake-hashes/src/bin/
116+ ls ./fuzz-real-hashes/src/bin/
97117```
98118
99119## A fuzz test failed, what do I do?
@@ -134,7 +154,8 @@ mkdir -p ./test_cases/$TARGET
134154echo $HEX | xxd -r -p > ./test_cases/$TARGET /any_filename_works
135155
136156export RUST_BACKTRACE=1
137- cargo test
157+ RUSTFLAGS=" --cfg=fuzzing --cfg=secp256k1_fuzz --cfg=hashes_fuzz" \
158+ cargo test --manifest-path fuzz-fake-hashes/Cargo.toml --bin " ${TARGET} _target"
138159```
139160
140161Note that if the fuzz test failed locally, moving the offending run's trace
@@ -151,7 +172,10 @@ Alternatively, you can use the `stdin_fuzz` feature to pipe the crash input dire
151172creating test case files on disk:
152173
153174``` shell
154- echo -ne ' \x2d\x31\x36\x38\x37\x34\x09\x01...' | cargo run --features stdin_fuzz --bin full_stack_target
175+ cd fuzz
176+ echo -ne ' \x2d\x31\x36\x38\x37\x34\x09\x01...' | \
177+ RUSTFLAGS=" --cfg=fuzzing --cfg=secp256k1_fuzz --cfg=hashes_fuzz" \
178+ cargo run --manifest-path fuzz-fake-hashes/Cargo.toml --features stdin_fuzz --bin full_stack_target
155179```
156180
157181Panics will abort the process directly (the crate uses ` panic = "abort" ` ), resulting in a
@@ -171,10 +195,13 @@ file are `do_test`, `my_fuzzy_experiment_test`, and `my_fuzzy_experiment_run`.
171195
1721963 . Adjust the body (not the signature!) of ` do_test ` as necessary for the new fuzz test.
173197
174- 4 . In ` fuzz/src/bin/gen_target.sh ` , add a line reading ` GEN_TEST my_fuzzy_experiment ` to the
175- first group of ` GEN_TEST ` lines (starting in line 9).
198+ 4 . In ` fuzz/src/bin/gen_target.sh ` , add a line reading ` GEN_FAKE_HASHES_TEST my_fuzzy_experiment `
199+ to the appropriate target list. Use ` GEN_REAL_HASHES_TEST ` only for targets that must run without
200+ ` hashes_fuzz ` .
176201
1772025 . If your test relies on a new local crate, add that crate as a dependency to ` fuzz/Cargo.toml ` .
203+ If the dependency is only needed by a specific runner crate or fuzz engine setup, add it to the
204+ matching target crate under ` fuzz/fuzz-fake-hashes/Cargo.toml ` or ` fuzz/fuzz-real-hashes/Cargo.toml ` instead.
178205
1792066 . In ` fuzz/src/lib.rs ` , add the line ` pub mod my_fuzzy_experiment ` . Additionally, if
180207you added a new crate dependency, add the ` extern crate […] ` import line.
0 commit comments