Skip to content

Commit 62e10c6

Browse files
authored
ci: fuzzers in the Github Actions CI. (#29)
* Add fuzzing CI. * fuzz dependency change: diffutils -> diffutilslib * fix fuzz build.
1 parent c68d386 commit 62e10c6

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

.github/workflows/fuzzing.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Fuzzing
2+
3+
# spell-checker:ignore fuzzer
4+
5+
on:
6+
pull_request:
7+
push:
8+
branches:
9+
- main
10+
11+
permissions:
12+
contents: read
13+
14+
concurrency:
15+
group: ${{ github.workflow }}-${{ github.ref }}
16+
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
17+
18+
jobs:
19+
fuzz-build:
20+
name: Build the fuzzers
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v4
24+
- uses: dtolnay/rust-toolchain@nightly
25+
- name: Install `cargo-fuzz`
26+
run: cargo install cargo-fuzz
27+
- uses: Swatinem/rust-cache@v2
28+
with:
29+
shared-key: "cargo-fuzz-cache-key"
30+
cache-directories: "fuzz/target"
31+
- name: Run `cargo-fuzz build`
32+
run: cargo +nightly fuzz build
33+
34+
fuzz-run:
35+
needs: fuzz-build
36+
name: Run the fuzzers
37+
runs-on: ubuntu-latest
38+
timeout-minutes: 5
39+
env:
40+
RUN_FOR: 60
41+
strategy:
42+
matrix:
43+
test-target:
44+
- { name: fuzz_ed, should_pass: true }
45+
- { name: fuzz_normal, should_pass: true }
46+
- { name: fuzz_patch, should_pass: true }
47+
steps:
48+
- uses: actions/checkout@v4
49+
- uses: dtolnay/rust-toolchain@nightly
50+
- name: Install `cargo-fuzz`
51+
run: cargo install cargo-fuzz
52+
- uses: Swatinem/rust-cache@v2
53+
with:
54+
shared-key: "cargo-fuzz-cache-key"
55+
cache-directories: "fuzz/target"
56+
- name: Restore Cached Corpus
57+
uses: actions/cache/restore@v4
58+
with:
59+
key: corpus-cache-${{ matrix.test-target.name }}
60+
path: |
61+
fuzz/corpus/${{ matrix.test-target.name }}
62+
- name: Run ${{ matrix.test-target.name }} for XX seconds
63+
shell: bash
64+
continue-on-error: ${{ !matrix.test-target.name.should_pass }}
65+
run: |
66+
cargo +nightly fuzz run ${{ matrix.test-target.name }} -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0
67+
- name: Save Corpus Cache
68+
uses: actions/cache/save@v4
69+
with:
70+
key: corpus-cache-${{ matrix.test-target.name }}
71+
path: |
72+
fuzz/corpus/${{ matrix.test-target.name }}

fuzz/fuzz_targets/fuzz_ed.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
#![no_main]
22
#[macro_use]
33
extern crate libfuzzer_sys;
4-
use diffutils::{ed_diff, normal_diff, unified_diff};
4+
use diffutilslib::ed_diff;
5+
use diffutilslib::ed_diff::DiffError;
56
use std::fs::{self, File};
67
use std::io::Write;
78
use std::process::Command;
89

10+
fn diff_w(expected: &[u8], actual: &[u8], filename: &str) -> Result<Vec<u8>, DiffError> {
11+
let mut output = ed_diff::diff(expected, actual)?;
12+
writeln!(&mut output, "w {filename}").unwrap();
13+
Ok(output)
14+
}
15+
916
fuzz_target!(|x: (Vec<u8>, Vec<u8>)| {
1017
let (mut from, mut to) = x;
1118
from.push(b'\n');
@@ -30,7 +37,7 @@ fuzz_target!(|x: (Vec<u8>, Vec<u8>)| {
3037
} else {
3138
return;
3239
}
33-
let diff = ed_diff::diff_w(&from, &to, "target/fuzz.file").unwrap();
40+
let diff = diff_w(&from, &to, "target/fuzz.file").unwrap();
3441
File::create("target/fuzz.file.original")
3542
.unwrap()
3643
.write_all(&from)

fuzz/fuzz_targets/fuzz_normal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![no_main]
22
#[macro_use]
33
extern crate libfuzzer_sys;
4-
use diffutils::{normal_diff, unified_diff};
4+
use diffutilslib::normal_diff;
55

66
use std::fs::{self, File};
77
use std::io::Write;

fuzz/fuzz_targets/fuzz_patch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![no_main]
22
#[macro_use]
33
extern crate libfuzzer_sys;
4-
use diffutils::{normal_diff, unified_diff};
4+
use diffutilslib::unified_diff;
55
use std::fs::{self, File};
66
use std::io::Write;
77
use std::process::Command;

0 commit comments

Comments
 (0)