diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 03325024..c7e91914 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -36,3 +36,19 @@ jobs: cargo build --verbose - name: Run tests run: cargo test --verbose + + fuzz: + name: Fuzzing + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Build + run: | + rustup update nightly + cargo install cargo-fuzz + cargo +nightly fuzz build + + - name: Fuzz + run: | + cargo +nightly fuzz run module -- -max_total_time=180 + cargo +nightly fuzz run component -- -max_total_time=180 \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 7fc055bc..c0460d49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ wasmparser = "0.215.0" tempfile = "3.10.1" serde_json = "1.0.121" log = "0.4.22" +wasm-smith = "0.215.0" [dev-dependencies] wasmprinter = "0.215.0" diff --git a/fuzz/.gitignore b/fuzz/.gitignore new file mode 100644 index 00000000..1a45eee7 --- /dev/null +++ b/fuzz/.gitignore @@ -0,0 +1,4 @@ +target +corpus +artifacts +coverage diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml new file mode 100644 index 00000000..50bff2a2 --- /dev/null +++ b/fuzz/Cargo.toml @@ -0,0 +1,31 @@ +[package] +name = "orca-fuzz" +version = "0.0.0" +publish = false +edition = "2021" + +[package.metadata] +cargo-fuzz = true + +[dependencies] +libfuzzer-sys = "0.4" +wasm-smith = "0.215.0" +wasmprinter = "0.215.0" + + +[dependencies.orca] +path = ".." + +[[bin]] +name = "component" +path = "fuzz_targets/component.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "module" +path = "fuzz_targets/module.rs" +test = false +doc = false +bench = false diff --git a/fuzz/README.md b/fuzz/README.md new file mode 100644 index 00000000..25de02fe --- /dev/null +++ b/fuzz/README.md @@ -0,0 +1,23 @@ +How to run the fuzzer: + +``` +rustup update nightly +``` + +``` +cargo install cargo-fuzz +``` + +``` +cargo +nightly fuzz build +``` + +To run roundtrips for modules. +``` +cargo +nightly fuzz run orca +``` + +To run roundtrips for components. +``` +cargo +nightly fuzz run component +``` \ No newline at end of file diff --git a/fuzz/fuzz_targets/component.rs b/fuzz/fuzz_targets/component.rs new file mode 100644 index 00000000..989731b9 --- /dev/null +++ b/fuzz/fuzz_targets/component.rs @@ -0,0 +1,28 @@ +// fuzzing roundtrips for component +#![no_main] + +use libfuzzer_sys::fuzz_target; +use orca; +use wasm_smith::Component; + +fuzz_target!(|comp: Component| { + let wasm_bytes = comp.to_bytes(); + // write to file + use std::fs::File; + use std::io::prelude::*; + let mut file = File::create("temp.wat").unwrap(); + + let original = wasmprinter::print_bytes(wasm_bytes.clone()) + .expect("couldn't convert original Wasm to wat"); + file.write_all(&original.clone().into_bytes()).unwrap(); + // println!("original:\n {:?}", original); + + let mut wasm_comp = orca::Component::parse(&wasm_bytes, false).unwrap(); + let roundtrip_bytes = wasm_comp.encode(); + + let roundtrip = wasmprinter::print_bytes(roundtrip_bytes.clone()) + .expect("couldn't convert roundtrip Wasm to wat"); + // println!("roundtrip:\n {:?}", roundtrip); + + assert_eq!(original, roundtrip); +}); diff --git a/fuzz/fuzz_targets/module.rs b/fuzz/fuzz_targets/module.rs new file mode 100644 index 00000000..38ad2444 --- /dev/null +++ b/fuzz/fuzz_targets/module.rs @@ -0,0 +1,29 @@ +// fuzzing roundtrips for modules + +#![no_main] + +use libfuzzer_sys::fuzz_target; +use orca; +use wasm_smith::Module; + +fuzz_target!(|module: Module| { + let wasm_bytes = module.to_bytes(); + // write to file + use std::fs::File; + use std::io::prelude::*; + let mut file = File::create("temp.wat").unwrap(); + + let original = wasmprinter::print_bytes(wasm_bytes.clone()) + .expect("couldn't convert original Wasm to wat"); + file.write_all(&original.clone().into_bytes()).unwrap(); + // println!("original:\n {:?}", original); + + let mut wasm_module = orca::Module::parse(&wasm_bytes, false).unwrap(); + let roundtrip_bytes = wasm_module.encode(); + + let roundtrip = wasmprinter::print_bytes(roundtrip_bytes.clone()) + .expect("couldn't convert roundtrip Wasm to wat"); + // println!("roundtrip:\n {:?}", roundtrip); + + assert_eq!(original, roundtrip); +});