Skip to content

Commit 6f85c4b

Browse files
committed
fuzz: add documentation for generating coverage reports
Signed-off-by: Renato Westphal <[email protected]>
1 parent 7320b72 commit 6f85c4b

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

fuzz/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,68 @@ You can therefore run the following(still from the root directory) to fuzz all t
8484
```
8585

8686
This will run each of the fuzz targets we have created for 5 minutes.
87+
88+
### Generate code-coverage data
89+
90+
Code coverage data helps identify which code paths are exercised during fuzzing. This information shows which parts of the code remain untested, so you can take steps such as adding fuzz targets with different entry points or expanding the corpus with new seed inputs. You can find more details Gin the [Rust Fuzz Book](https://rust-fuzz.github.io/book/cargo-fuzz/coverage.html).
91+
92+
The following instructions use the `isis_pdu_decode` fuzz target as an example.
93+
94+
#### 1. Run the fuzz target
95+
96+
Begin by running the fuzz target for a long period to exercise as many code paths as possible. The `-j` option allows multiple fuzzer instances to run in parallel while sharing the same corpus.
97+
```
98+
cargo fuzz run -j 8 isis_pdu_decode
99+
```
100+
101+
#### 2. Generate coverage data
102+
103+
Once the fuzz run has completed, generate coverage information:
104+
```
105+
cargo fuzz coverage isis_pdu_decode
106+
```
107+
108+
#### 3. Generate a coverage report
109+
110+
A text-based coverage report can be generated using `llvm-cov` as follows:
111+
```
112+
~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-cov report \
113+
-instr-profile=fuzz/coverage/isis_pdu_decode/coverage.profdata \
114+
target/x86_64-unknown-linux-gnu/coverage/x86_64-unknown-linux-gnu/release/isis_pdu_decode \
115+
$(find holo-isis/src/packet -name '*.rs')
116+
```
117+
118+
Example output:
119+
```
120+
$ ~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-cov report \
121+
-instr-profile=fuzz/coverage/isis_pdu_decode/coverage.profdata \
122+
target/x86_64-unknown-linux-gnu/coverage/x86_64-unknown-linux-gnu/release/isis_pdu_decode \
123+
$(find holo-isis/src/packet -name '*.rs')
124+
Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover
125+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
126+
tlv.rs 1954 1098 43.81% 122 100 18.03% 1311 706 46.15% 0 0 -
127+
pdu.rs 1898 1241 34.62% 114 107 6.14% 1299 934 28.10% 0 0 -
128+
error.rs 100 94 6.00% 5 3 40.00% 53 47 11.32% 0 0 -
129+
mod.rs 198 150 24.24% 31 24 22.58% 164 126 23.17% 0 0 -
130+
consts.rs 3 3 0.00% 1 1 0.00% 3 3 0.00% 0 0 -
131+
subtlvs/capability.rs 222 118 46.85% 19 14 26.32% 151 76 49.67% 0 0 -
132+
subtlvs/prefix.rs 280 158 43.57% 20 15 25.00% 177 90 49.15% 0 0 -
133+
subtlvs/neighbor.rs 254 158 37.80% 23 15 34.78% 176 84 52.27% 0 0 -
134+
auth.rs 56 56 0.00% 5 5 0.00% 46 46 0.00% 0 0 - -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
135+
TOTAL 4965 3076 38.05% 340 284 16.47% 3380 2112 37.51% 0 0 -
136+
```
137+
138+
#### 4. Generate an HTML coverage report
139+
140+
Generating an HTML report lets you visually identify which lines were not executed during fuzzing:
141+
```
142+
~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-cov show \
143+
-instr-profile=fuzz/coverage/isis_pdu_decode/coverage.profdata \
144+
target/x86_64-unknown-linux-gnu/coverage/x86_64-unknown-linux-gnu/release/isis_pdu_decode \
145+
-format=html \
146+
$(find holo-isis/src/packet -name '*.rs') > cov-isis-pdu-decode.html
147+
```
148+
149+
In the resulting HTML report, lines highlighted in red indicate code that was not executed, while other lines represent covered code paths.
150+
151+
Since most fuzz targets focus on packet decoding, it's normal for code related to packet encoding and various helper functions to remain uncovered.

0 commit comments

Comments
 (0)