Skip to content

Commit b939100

Browse files
authored
Merge pull request #3718 from Prabhat1308/pv/add_cov_script
Add script to generate fuzz coverage
2 parents f5dd77c + 248ba29 commit b939100

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ jobs:
129129
# Will anyone be impressed by your amazing coverage? No
130130
# Maybe if codecov wasn't broken we wouldn't need to do this...
131131
bash <(curl -s https://codecov.io/bash) -f target/codecov.json -t "f421b687-4dc2-4387-ac3d-dc3b2528af57"
132+
- name: Run fuzz coverage generation
133+
run: |
134+
./ci/ci-fuzz.sh
132135
133136
benchmark:
134137
runs-on: ubuntu-latest

ci/ci-fuzz.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
set -eox pipefail
3+
4+
echo -e "\n\nGenerating fuzz coverage report"
5+
# In CI, store coverage in target directory for consistency with other artifacts
6+
COVERAGE_DIR="target/coverage-report"
7+
echo "Installing cargo-llvm-cov..."
8+
# Install cargo-llvm-cov if not already installed
9+
cargo install cargo-llvm-cov --locked
10+
11+
echo "Cleaning up to save disk space..."
12+
rm -rf target/*
13+
echo "Disk cleanup completed"
14+
15+
16+
echo "Running fuzz coverage generation..."
17+
./contrib/generate_fuzz_coverage.sh --output-dir "$COVERAGE_DIR"
18+
echo "Coverage generation completed. Checking results..."
19+
20+
# Upload fuzz coverage to codecov if the file exists (CI only)
21+
if [ -f "target/fuzz-codecov.json" ]; then
22+
echo "Uploading fuzz coverage to codecov..."
23+
bash <(curl -s https://codecov.io/bash) -f "target/fuzz-codecov.json" -F fuzz -t "f421b687-4dc2-4387-ac3d-dc3b2528af57"
24+
fi

contrib/generate_fuzz_coverage.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
set -e
3+
set -x
4+
5+
# Parse command line arguments
6+
OUTPUT_DIR="coverage-report"
7+
while [[ $# -gt 0 ]]; do
8+
case $1 in
9+
--output-dir)
10+
OUTPUT_DIR="$2"
11+
shift 2
12+
;;
13+
*)
14+
echo "Unknown option: $1"
15+
echo "Usage: $0 [--output-dir OUTPUT_DIRECTORY]"
16+
exit 1
17+
;;
18+
esac
19+
done
20+
21+
# Check if we're in the root directory, if so change to fuzz
22+
if [ -d "fuzz" ]; then
23+
cd fuzz
24+
elif [ ! -f "Cargo.toml" ] || ! grep -q "fuzz" Cargo.toml 2>/dev/null; then
25+
echo "Error: Please run this script from the rust-lightning root directory or fuzz directory"
26+
exit 1
27+
fi
28+
29+
# Check if test_cases directory exists and has content
30+
show_corpus_message=false
31+
if [ ! -d "test_cases" ]; then
32+
show_corpus_message=true
33+
elif [ -z "$(find test_cases -name '*' -type f 2>/dev/null | head -1)" ]; then
34+
show_corpus_message=true
35+
fi
36+
37+
if [ "$show_corpus_message" = true ]; then
38+
echo "Warning: No corpus found in test_cases directory."
39+
echo "Generating coverage report without fuzzing corpus."
40+
echo ""
41+
echo "To include fuzzing corpus coverage, create test_cases directories with your corpus:"
42+
echo " mkdir -p test_cases/{target_name}"
43+
echo " cp your_corpus_directory/* test_cases/{target_name}/"
44+
echo ""
45+
echo "Example:"
46+
echo " mkdir -p test_cases/base32"
47+
echo " cp /path/to/your/base32_corpus/* test_cases/base32/"
48+
echo ""
49+
fi
50+
51+
# Create output directory if it doesn't exist
52+
mkdir -p "$OUTPUT_DIR"
53+
54+
export RUSTFLAGS="--cfg=fuzzing --cfg=secp256k1_fuzz --cfg=hashes_fuzz"
55+
56+
# dont run this command when running in CI
57+
if [ "$CI" != "true" ] && [ "$GITHUB_ACTIONS" != "true" ]; then
58+
cargo llvm-cov --html --ignore-filename-regex "fuzz/" --output-dir "$OUTPUT_DIR"
59+
60+
# Check if coverage report was generated successfully
61+
# The report is generated in $OUTPUT_DIR/html/index.html when using --html --output-dir
62+
if [ ! -f "$OUTPUT_DIR/html/index.html" ]; then
63+
echo "Error: Failed to generate coverage report at $OUTPUT_DIR/html/index.html"
64+
echo "Contents of $OUTPUT_DIR:"
65+
ls -la "$OUTPUT_DIR" || echo "Directory $OUTPUT_DIR does not exist"
66+
if [ -d "$OUTPUT_DIR/html" ]; then
67+
echo "Contents of $OUTPUT_DIR/html:"
68+
ls -la "$OUTPUT_DIR/html"
69+
fi
70+
exit 1
71+
fi
72+
echo "Coverage report generated in $OUTPUT_DIR/html/index.html"
73+
fi
74+
75+
# Generate codecov JSON format if running in CI environment
76+
if [ "$CI" = "true" ] || [ "$GITHUB_ACTIONS" = "true" ]; then
77+
echo "CI environment detected, generating codecov JSON format..."
78+
cargo llvm-cov --codecov --ignore-filename-regex "fuzz/" --output-path "$OUTPUT_DIR/fuzz-codecov.json"
79+
80+
if [ -f "$OUTPUT_DIR/fuzz-codecov.json" ] && [[ "$OUTPUT_DIR" == *"target/"* ]]; then
81+
TARGET_DIR="../target"
82+
cp "$OUTPUT_DIR/fuzz-codecov.json" "$TARGET_DIR/fuzz-codecov.json"
83+
echo "Fuzz codecov report copied to $TARGET_DIR/fuzz-codecov.json"
84+
fi
85+
fi
86+
87+
88+

0 commit comments

Comments
 (0)