From f1a22b102842be235d1e42b176cedfe65e2f7706 Mon Sep 17 00:00:00 2001 From: ahuoguo Date: Thu, 22 Aug 2024 19:55:05 +0800 Subject: [PATCH 1/4] wasm-smith --- .github/workflows/rust.yml | 16 ++++++++++++++++ Cargo.toml | 1 + fuzz/.gitignore | 4 ++++ fuzz/Cargo.toml | 31 +++++++++++++++++++++++++++++++ fuzz/README.md | 23 +++++++++++++++++++++++ fuzz/fuzz_targets/component.rs | 26 ++++++++++++++++++++++++++ fuzz/fuzz_targets/module.rs | 27 +++++++++++++++++++++++++++ tests/round_trip_component.rs | 3 +++ 8 files changed, 131 insertions(+) create mode 100644 fuzz/.gitignore create mode 100644 fuzz/Cargo.toml create mode 100644 fuzz/README.md create mode 100644 fuzz/fuzz_targets/component.rs create mode 100644 fuzz/fuzz_targets/module.rs 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..223cd4fa --- /dev/null +++ b/fuzz/fuzz_targets/component.rs @@ -0,0 +1,26 @@ +// fuzzing roundtrips for component +#![no_main] + +use libfuzzer_sys::fuzz_target; +use wasm_smith::Component; +use orca; + +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..c5c0773a --- /dev/null +++ b/fuzz/fuzz_targets/module.rs @@ -0,0 +1,27 @@ +// fuzzing roundtrips for modules + +#![no_main] + +use libfuzzer_sys::fuzz_target; +use wasm_smith::Module; +use orca; + +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); +}); diff --git a/tests/round_trip_component.rs b/tests/round_trip_component.rs index 3e21b8ce..6dad7a50 100644 --- a/tests/round_trip_component.rs +++ b/tests/round_trip_component.rs @@ -77,6 +77,9 @@ mod round_trip { make_round_trip_tests_component!("handwritten/components", add); + make_round_trip_tests_component!("handwritten/components", fuzz); + + make_round_trip_tests_component!("wizard/components", func_loop); make_round_trip_tests_component!("spec-test/components", if_test); From c7ce25bef9a3fd7b57e5a67c3d5b0b075fe6bbc4 Mon Sep 17 00:00:00 2001 From: ahuoguo Date: Thu, 22 Aug 2024 19:56:45 +0800 Subject: [PATCH 2/4] cargo fmt --- tests/round_trip_component.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/round_trip_component.rs b/tests/round_trip_component.rs index 6dad7a50..953ca49f 100644 --- a/tests/round_trip_component.rs +++ b/tests/round_trip_component.rs @@ -79,7 +79,6 @@ mod round_trip { make_round_trip_tests_component!("handwritten/components", fuzz); - make_round_trip_tests_component!("wizard/components", func_loop); make_round_trip_tests_component!("spec-test/components", if_test); From 42ecef43cfb657473438d23333669cfcb410e046 Mon Sep 17 00:00:00 2001 From: ahuoguo Date: Thu, 22 Aug 2024 19:57:14 +0800 Subject: [PATCH 3/4] fmt --- fuzz/fuzz_targets/component.rs | 10 ++++++---- fuzz/fuzz_targets/module.rs | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/fuzz/fuzz_targets/component.rs b/fuzz/fuzz_targets/component.rs index 223cd4fa..989731b9 100644 --- a/fuzz/fuzz_targets/component.rs +++ b/fuzz/fuzz_targets/component.rs @@ -2,8 +2,8 @@ #![no_main] use libfuzzer_sys::fuzz_target; -use wasm_smith::Component; use orca; +use wasm_smith::Component; fuzz_target!(|comp: Component| { let wasm_bytes = comp.to_bytes(); @@ -11,15 +11,17 @@ fuzz_target!(|comp: Component| { 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"); + + 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"); + 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 index c5c0773a..38ad2444 100644 --- a/fuzz/fuzz_targets/module.rs +++ b/fuzz/fuzz_targets/module.rs @@ -3,8 +3,8 @@ #![no_main] use libfuzzer_sys::fuzz_target; -use wasm_smith::Module; use orca; +use wasm_smith::Module; fuzz_target!(|module: Module| { let wasm_bytes = module.to_bytes(); @@ -12,15 +12,17 @@ fuzz_target!(|module: Module| { 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"); + + 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"); + let roundtrip = wasmprinter::print_bytes(roundtrip_bytes.clone()) + .expect("couldn't convert roundtrip Wasm to wat"); // println!("roundtrip:\n {:?}", roundtrip); assert_eq!(original, roundtrip); From 37dbd1c02faeedd6e1644d3088ef7a5cd2c17b87 Mon Sep 17 00:00:00 2001 From: ahuoguo Date: Thu, 22 Aug 2024 20:07:42 +0800 Subject: [PATCH 4/4] rm extra test --- tests/round_trip_component.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/round_trip_component.rs b/tests/round_trip_component.rs index 953ca49f..3e21b8ce 100644 --- a/tests/round_trip_component.rs +++ b/tests/round_trip_component.rs @@ -77,8 +77,6 @@ mod round_trip { make_round_trip_tests_component!("handwritten/components", add); - make_round_trip_tests_component!("handwritten/components", fuzz); - make_round_trip_tests_component!("wizard/components", func_loop); make_round_trip_tests_component!("spec-test/components", if_test);