Debian Codegraph @

.github/workflows/fuzzing.yml

raw
1 name: Fuzzing
2
3 # spell-checker:ignore (people) taiki-e
4 # spell-checker:ignore (misc) fuzzer PIPESTATUS
5
6 env:
7 CARGO_INCREMENTAL: "0"
8
9 on:
10 pull_request:
11 push:
12 branches:
13 - '*'
14
15 permissions:
16 contents: read # to fetch code (actions/checkout)
17
18 # End the current execution if there is a new changeset in the PR.
19 concurrency:
20 group: ${{ github.workflow }}-${{ github.ref }}
21 cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
22
23 jobs:
24 uufuzz-examples:
25 name: Build and test uufuzz examples
26 runs-on: ubuntu-latest
27 steps:
28 - uses: actions/[email protected]
29 with:
30 persist-credentials: false
31 - name: Build uufuzz library
32 run: |
33 cd fuzz/uufuzz
34 cargo build --release
35 - name: Run uufuzz tests
36 run: |
37 cd fuzz/uufuzz
38 cargo test --lib
39 - name: Build and run uufuzz examples
40 run: |
41 cd fuzz/uufuzz
42 echo "Building all examples..."
43 cargo build --examples --release
44
45 # Run all examples except integration_testing (which has FD issues in CI)
46 for example in examples/*.rs; do
47 example_name=$(basename "$example" .rs)
48 if [ "$example_name" != "integration_testing" ]; then
49 cargo run --example "$example_name" --release
50 fi
51 done
52
53 fuzz-build:
54 name: Build the fuzzers
55 runs-on: ubuntu-latest
56 steps:
57 - uses: actions/[email protected]
58 with:
59 persist-credentials: false
60 - name: Install `cargo-fuzz`
61 uses: taiki-e/install-action@v2
62 with:
63 tool: cargo-fuzz
64 - name: Emulate a nightly toolchain
65 run: |
66 echo "RUSTC_BOOTSTRAP=1" >> "${GITHUB_ENV}"
67 - name: Run `cargo-fuzz build`
68 # Force the correct target
69 # https://github.com/rust-fuzz/cargo-fuzz/issues/398
70 run: cargo fuzz build --target host-tuple
71
72 fuzz-run:
73 needs: fuzz-build
74 name: Fuzz (${{ matrix.group.name }})
75 runs-on: ubuntu-latest
76 timeout-minutes: 15
77 env:
78 RUN_FOR: 60
79 strategy:
80 matrix:
81 group:
82 - name: smoke
83 targets: |
84 fuzz_test:true
85 fuzz_date:true
86 fuzz_expr:true
87 fuzz_printf:true
88 fuzz_echo:true
89 - name: parsers
90 targets: |
91 fuzz_parse_glob:true
92 fuzz_parse_size:false
93 fuzz_parse_time:false
94 fuzz_seq_parse_number:false
95 - name: commands-a
96 targets: |
97 fuzz_seq:false
98 fuzz_sort:false
99 fuzz_wc:false
100 fuzz_cut:false
101 fuzz_split:false
102 - name: commands-b
103 targets: |
104 fuzz_tr:false
105 fuzz_env:false
106 fuzz_cksum:false
107 fuzz_non_utf8_paths:true
108 fuzz_dirname:true
109
110 steps:
111 - uses: actions/[email protected]
112 with:
113 persist-credentials: false
114 - name: Install `cargo-fuzz`
115 uses: taiki-e/install-action@v2
116 with:
117 tool: cargo-fuzz
118 - name: Emulate a nightly toolchain
119 run: |
120 echo "RUSTC_BOOTSTRAP=1" >> "${GITHUB_ENV}"
121 - name: Run ${{ matrix.group.name }} fuzz targets
122 shell: bash
123 run: |
124 set -o pipefail
125 mkdir -p fuzz/stats
126 REQUIRED_TARGET_FAILED=0
127
128 while IFS=: read -r TARGET SHOULD_PASS; do
129 [ -n "$TARGET" ] || continue
130
131 STATS_FILE="fuzz/stats/${TARGET}.txt"
132 # Force the correct target
133 # https://github.com/rust-fuzz/cargo-fuzz/issues/398
134 set +e
135 cargo fuzz run --target host-tuple "$TARGET" -- -max_total_time=${{ env.RUN_FOR }} -timeout=${{ env.RUN_FOR }} -detect_leaks=0 -print_final_stats=1 2>&1 | tee "$STATS_FILE"
136 FUZZ_STATUS=${PIPESTATUS[0]}
137 set -e
138
139 # Save should_pass value to file for summary job to use
140 echo "$SHOULD_PASS" > "fuzz/stats/${TARGET}.should_pass"
141
142 # Print stats to job output for immediate visibility
143 echo "----------------------------------------"
144 echo "FUZZING STATISTICS FOR $TARGET"
145 echo "----------------------------------------"
146 echo "Runs: $(grep -q "stat::number_of_executed_units" "$STATS_FILE" && grep "stat::number_of_executed_units" "$STATS_FILE" | awk '{print $2}' || echo "unknown")"
147 echo "Execution Rate: $(grep -q "stat::average_exec_per_sec" "$STATS_FILE" && grep "stat::average_exec_per_sec" "$STATS_FILE" | awk '{print $2}' || echo "unknown") execs/sec"
148 echo "New Units: $(grep -q "stat::new_units_added" "$STATS_FILE" && grep "stat::new_units_added" "$STATS_FILE" | awk '{print $2}' || echo "unknown")"
149 echo "Expected: $SHOULD_PASS"
150 echo "Exit code: $FUZZ_STATUS"
151 if grep -q "SUMMARY: " "$STATS_FILE"; then
152 echo "Status: $(grep "SUMMARY: " "$STATS_FILE" | head -1)"
153 else
154 echo "Status: Completed"
155 fi
156 echo "----------------------------------------"
157
158 # Add summary to GitHub step summary
159 echo "### Fuzzing Results for $TARGET" >> $GITHUB_STEP_SUMMARY
160 echo "" >> $GITHUB_STEP_SUMMARY
161 echo "| Metric | Value |" >> $GITHUB_STEP_SUMMARY
162 echo "|--------|-------|" >> $GITHUB_STEP_SUMMARY
163
164 if grep -q "stat::number_of_executed_units" "$STATS_FILE"; then
165 echo "| Runs | $(grep "stat::number_of_executed_units" "$STATS_FILE" | awk '{print $2}') |" >> $GITHUB_STEP_SUMMARY
166 fi
167
168 if grep -q "stat::average_exec_per_sec" "$STATS_FILE"; then
169 echo "| Execution Rate | $(grep "stat::average_exec_per_sec" "$STATS_FILE" | awk '{print $2}') execs/sec |" >> $GITHUB_STEP_SUMMARY
170 fi
171
172 if grep -q "stat::new_units_added" "$STATS_FILE"; then
173 echo "| New Units | $(grep "stat::new_units_added" "$STATS_FILE" | awk '{print $2}') |" >> $GITHUB_STEP_SUMMARY
174 fi
175
176 echo "| Should pass | $SHOULD_PASS |" >> $GITHUB_STEP_SUMMARY
177 echo "| Exit code | $FUZZ_STATUS |" >> $GITHUB_STEP_SUMMARY
178
179 if grep -q "SUMMARY: " "$STATS_FILE"; then
180 echo "| Status | $(grep "SUMMARY: " "$STATS_FILE" | head -1) |" >> $GITHUB_STEP_SUMMARY
181 else
182 echo "| Status | Completed |" >> $GITHUB_STEP_SUMMARY
183 fi
184
185 echo "" >> $GITHUB_STEP_SUMMARY
186
187 if [ "$SHOULD_PASS" = "true" ] && [ "$FUZZ_STATUS" -ne 0 ]; then
188 REQUIRED_TARGET_FAILED=1
189 fi
190 done <<< "${{ matrix.group.targets }}"
191
192 exit "$REQUIRED_TARGET_FAILED"
193 - name: Upload Stats
194 if: always()
195 uses: actions/upload-artifact@v7
196 with:
197 name: fuzz-stats-${{ matrix.group.name }}
198 path: |
199 fuzz/stats/*.txt
200 fuzz/stats/*.should_pass
201 retention-days: 5
202 fuzz-summary:
203 needs: fuzz-run
204 name: Fuzzing Summary
205 runs-on: ubuntu-latest
206 if: always()
207 steps:
208 - uses: actions/[email protected]
209 with:
210 persist-credentials: false
211 - name: Download all stats
212 uses: actions/download-artifact@v8
213 with:
214 path: fuzz/stats-artifacts
215 pattern: fuzz-stats-*
216 merge-multiple: true
217 - name: Prepare stats directory
218 run: |
219 mkdir -p fuzz/stats
220 # Debug: List content of stats-artifacts directory
221 echo "Contents of stats-artifacts directory:"
222 find fuzz/stats-artifacts -type f | sort
223
224 # Extract files from the artifact directories - handle nested directories
225 find fuzz/stats-artifacts -type f -name "*.txt" -exec cp {} fuzz/stats/ \;
226 find fuzz/stats-artifacts -type f -name "*.should_pass" -exec cp {} fuzz/stats/ \;
227
228 # Debug information
229 echo "Contents of stats directory after extraction:"
230 ls -la fuzz/stats/
231 echo "Contents of should_pass files (if any):"
232 cat fuzz/stats/*.should_pass 2>/dev/null || echo "No should_pass files found"
233 - name: Generate Summary
234 run: |
235 echo "# Fuzzing Summary" > fuzzing_summary.md
236 echo "" >> fuzzing_summary.md
237 echo "| Target | Runs | Exec/sec | New Units | Should pass | Status |" >> fuzzing_summary.md
238 echo "|--------|------|----------|-----------|-------------|--------|" >> fuzzing_summary.md
239
240 TOTAL_RUNS=0
241 TOTAL_NEW_UNITS=0
242
243 for stat_file in fuzz/stats/*.txt; do
244 TARGET=$(basename "$stat_file" .txt)
245 SHOULD_PASS_FILE="${stat_file%.*}.should_pass"
246
247 # Get expected status
248 if [ -f "$SHOULD_PASS_FILE" ]; then
249 EXPECTED=$(cat "$SHOULD_PASS_FILE")
250 else
251 EXPECTED="unknown"
252 fi
253
254 # Extract runs
255 if grep -q "stat::number_of_executed_units" "$stat_file"; then
256 RUNS=$(grep "stat::number_of_executed_units" "$stat_file" | awk '{print $2}')
257 TOTAL_RUNS=$((TOTAL_RUNS + RUNS))
258 else
259 RUNS="unknown"
260 fi
261
262 # Extract execution rate
263 if grep -q "stat::average_exec_per_sec" "$stat_file"; then
264 EXEC_RATE=$(grep "stat::average_exec_per_sec" "$stat_file" | awk '{print $2}')
265 else
266 EXEC_RATE="unknown"
267 fi
268
269 # Extract new units added
270 if grep -q "stat::new_units_added" "$stat_file"; then
271 NEW_UNITS=$(grep "stat::new_units_added" "$stat_file" | awk '{print $2}')
272 if [[ "$NEW_UNITS" =~ ^[0-9]+$ ]]; then
273 TOTAL_NEW_UNITS=$((TOTAL_NEW_UNITS + NEW_UNITS))
274 fi
275 else
276 NEW_UNITS="unknown"
277 fi
278
279 # Extract status
280 if grep -q "SUMMARY: " "$stat_file"; then
281 STATUS=$(grep "SUMMARY: " "$stat_file" | head -1)
282 else
283 STATUS="Completed"
284 fi
285
286 echo "| $TARGET | $RUNS | $EXEC_RATE | $NEW_UNITS | $EXPECTED | $STATUS |" >> fuzzing_summary.md
287 done
288
289 echo "" >> fuzzing_summary.md
290 echo "## Overall Statistics" >> fuzzing_summary.md
291 echo "" >> fuzzing_summary.md
292 echo "- **Total runs:** $TOTAL_RUNS" >> fuzzing_summary.md
293 echo "- **Total new units discovered:** $TOTAL_NEW_UNITS" >> fuzzing_summary.md
294 echo "- **Average execution rate:** $(grep -h "stat::average_exec_per_sec" fuzz/stats/*.txt | awk '{sum += $2; count++} END {if (count > 0) print sum/count " execs/sec"; else print "unknown"}')" >> fuzzing_summary.md
295
296 # Add count by expected status
297 echo "- **Tests expected to pass:** $(find fuzz/stats -name "*.should_pass" -exec cat {} \; | grep -c "true")" >> fuzzing_summary.md
298 echo "- **Tests expected to fail:** $(find fuzz/stats -name "*.should_pass" -exec cat {} \; | grep -c "false")" >> fuzzing_summary.md
299
300 # Write to GitHub step summary
301 cat fuzzing_summary.md >> $GITHUB_STEP_SUMMARY
302 - name: Show Summary
303 run: |
304 cat fuzzing_summary.md
305 - name: Upload Summary
306 uses: actions/upload-artifact@v7
307 with:
308 name: fuzzing-summary
309 path: fuzzing_summary.md
310 retention-days: 5