Skip to content

Commit

Permalink
Merge #2202
Browse files Browse the repository at this point in the history
2202: Include fuzzers in fmt and clippy. r=nlewycky a=nlewycky



Co-authored-by: Nick Lewycky <nick@wasmer.io>
  • Loading branch information
bors[bot] and nlewycky committed Mar 19, 2021
2 parents 14669d3 + 0193b72 commit f55ac48
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 25 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ test-packages:
cargo test -p wasmer-cache --release
cargo test -p wasmer-engine --release
cargo test -p wasmer-derive --release
cargo check --manifest-path fuzz/Cargo.toml $(compiler_features) --release


# We want to run all the tests for all available compilers. The C API
Expand Down Expand Up @@ -739,9 +740,11 @@ lint-packages:
RUSTFLAGS=${RUSTFLAGS} cargo clippy --manifest-path lib/cli/Cargo.toml $(compiler_features)
RUSTFLAGS=${RUSTFLAGS} cargo clippy -p wasmer-cache
RUSTFLAGS=${RUSTFLAGS} cargo clippy -p wasmer-engine
RUSTFLAGS=${RUSTFLAGS} cargo clippy --manifest-path fuzz/Cargo.toml $(compiler_features)

lint-formatting:
cargo fmt --all -- --check
cargo fmt --manifest-path fuzz/Cargo.toml -- --check

lint: lint-formatting lint-packages

Expand Down
2 changes: 1 addition & 1 deletion fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 21 additions & 14 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ cargo-fuzz = true
anyhow = "1"
wasm-smith = { git = "https://github.com/bytecodealliance/wasm-tools" }
libfuzzer-sys = "0.4.0"
[dependencies.wasmer]
path = "../lib/api"
[dependencies.wasmer-compiler-cranelift]
path = "../lib/compiler-cranelift"
[dependencies.wasmer-compiler-llvm]
path = "../lib/compiler-llvm"
[dependencies.wasmer-compiler-singlepass]
path = "../lib/compiler-singlepass"
[dependencies.wasmer-engine-jit]
path = "../lib/engine-jit"
[dependencies.wasmer-engine-native]
path = "../lib/engine-native"
[dependencies.wasmer-middlewares]
path = "../lib/middlewares"
wasmer = { path = "../lib/api" }
wasmer-compiler-cranelift = { path = "../lib/compiler-cranelift", optional = true }
wasmer-compiler-llvm = { path = "../lib/compiler-llvm", optional = true }
wasmer-compiler-singlepass = { path = "../lib/compiler-singlepass", optional = true }
wasmer-engine-jit = { path = "../lib/engine-jit", optional = true }
wasmer-engine-native = { path = "../lib/engine-native", optional = true }
wasmer-middlewares = { path = "../lib/middlewares" }

[features]
cranelift = [ "wasmer-compiler-cranelift" ]
llvm = [ "wasmer-compiler-llvm" ]
singlepass = [ "wasmer-compiler-singlepass" ]
jit = [ "wasmer-engine-jit" ]
native = [ "wasmer-engine-native" ]

# Prevent this from interfering with workspaces
[workspace]
Expand All @@ -34,27 +34,34 @@ members = ["."]
[[bin]]
name = "equivalence_jit"
path = "fuzz_targets/equivalence_jit.rs"
required-features = ["jit"]

[[bin]]
name = "jit_cranelift"
path = "fuzz_targets/jit_cranelift.rs"
required-features = ["jit", "cranelift"]

[[bin]]
name = "jit_llvm"
path = "fuzz_targets/jit_llvm.rs"
required-features = ["jit", "llvm"]

[[bin]]
name = "jit_singlepass"
path = "fuzz_targets/jit_singlepass.rs"
required-features = ["jit", "singlepass"]

[[bin]]
name = "metering"
path = "fuzz_targets/metering.rs"
required-features = ["jit", "cranelift"]

[[bin]]
name = "native_cranelift"
path = "fuzz_targets/native_cranelift.rs"
required-features = ["native", "cranelift"]

[[bin]]
name = "validate"
path = "fuzz_targets/validate.rs"
required-features = ["jit", "cranelift"]
31 changes: 24 additions & 7 deletions fuzz/fuzz_targets/equivalence_jit.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#![no_main]
#![deny(unused_variables)]

use anyhow::Result;
use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target};
use wasm_smith::{Config, ConfiguredModule};
use wasmer::{imports, CompilerConfig, Instance, Module, Store, Val};
#[cfg(feature = "cranelift")]
use wasmer_compiler_cranelift::Cranelift;
#[cfg(feature = "llvm")]
use wasmer_compiler_llvm::LLVM;
#[cfg(feature = "singlepass")]
use wasmer_compiler_singlepass::Singlepass;
use wasmer_engine_jit::JIT;

Expand All @@ -27,6 +31,7 @@ impl Config for ExportedFunctionConfig {
}
}

#[cfg(feature = "singlepass")]
fn maybe_instantiate_singlepass(wasm_bytes: &[u8]) -> Result<Option<Instance>> {
let compiler = Singlepass::default();
let store = Store::new(&JIT::new(compiler).engine());
Expand All @@ -45,6 +50,7 @@ fn maybe_instantiate_singlepass(wasm_bytes: &[u8]) -> Result<Option<Instance>> {
Ok(Some(instance))
}

#[cfg(feature = "cranelift")]
fn maybe_instantiate_cranelift(wasm_bytes: &[u8]) -> Result<Option<Instance>> {
let mut compiler = Cranelift::default();
compiler.canonicalize_nans(true);
Expand All @@ -55,6 +61,7 @@ fn maybe_instantiate_cranelift(wasm_bytes: &[u8]) -> Result<Option<Instance>> {
Ok(Some(instance))
}

#[cfg(feature = "llvm")]
fn maybe_instantiate_llvm(wasm_bytes: &[u8]) -> Result<Option<Instance>> {
let mut compiler = LLVM::default();
compiler.canonicalize_nans(true);
Expand All @@ -78,15 +85,14 @@ impl PartialEq for InstanceResult {
if let InstanceResult::Error(other_message) = other {
return self_message == other_message;
}
return false;
}
InstanceResult::Values(self_values) => {
if let InstanceResult::Values(other_values) = other {
return self_values == other_values;
}
return false;
}
}
false
}
}

Expand All @@ -109,8 +115,7 @@ fn evaluate_instance(instance: Result<Instance>) -> Vec<InstanceResult> {
// TODO: support functions which take params.
if f.ty().params().is_empty() {
let result = f.call(&[]);
let result = if result.is_ok() {
let values = result.unwrap();
let result = if let Ok(values) = result {
InstanceResult::Values(values.into())
} else {
let err = result.unwrap_err();
Expand All @@ -129,16 +134,28 @@ fuzz_target!(|module: ConfiguredModule<ExportedFunctionConfig>| {
module.ensure_termination(100000);
let wasm_bytes = module.to_bytes();

let singlepass = maybe_instantiate_singlepass(&wasm_bytes).transpose().map(evaluate_instance);
let cranelift = maybe_instantiate_cranelift(&wasm_bytes).transpose().map(evaluate_instance);
let llvm = maybe_instantiate_llvm(&wasm_bytes).transpose().map(evaluate_instance);
#[cfg(feature = "singlepass")]
let singlepass = maybe_instantiate_singlepass(&wasm_bytes)
.transpose()
.map(evaluate_instance);
#[cfg(feature = "cranelift")]
let cranelift = maybe_instantiate_cranelift(&wasm_bytes)
.transpose()
.map(evaluate_instance);
#[cfg(feature = "llvm")]
let llvm = maybe_instantiate_llvm(&wasm_bytes)
.transpose()
.map(evaluate_instance);

#[cfg(all(feature = "singlepass", feature = "cranelift"))]
if singlepass.is_some() && cranelift.is_some() {
assert_eq!(singlepass.as_ref().unwrap(), cranelift.as_ref().unwrap());
}
#[cfg(all(feature = "singlepass", feature = "llvm"))]
if singlepass.is_some() && llvm.is_some() {
assert_eq!(singlepass.as_ref().unwrap(), llvm.as_ref().unwrap());
}
#[cfg(all(feature = "cranelift", feature = "llvm"))]
if cranelift.is_some() && llvm.is_some() {
assert_eq!(cranelift.as_ref().unwrap(), llvm.as_ref().unwrap());
}
Expand Down
3 changes: 1 addition & 2 deletions fuzz/fuzz_targets/jit_singlepass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ fuzz_target!(|module: ConfiguredModule<NoImportsConfig>| {
Ok(_) => {}
Err(e) => {
let error_message = format!("{}", e);
if
error_message
if error_message
.contains("RuntimeError: memory out of bounds: data segment does not fit")
|| error_message
.contains("RuntimeError: table out of bounds: elements segment does not fit")
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/metering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target};
use std::sync::Arc;
use wasm_smith::{Config, ConfiguredModule};
use wasmer::{imports, CompilerConfig, Instance, Module, Store};
use wasmer::wasmparser::Operator;
use wasmer::{imports, CompilerConfig, Instance, Module, Store};
use wasmer_compiler_cranelift::Cranelift;
use wasmer_engine_jit::JIT;
use wasmer_middlewares::Metering;
Expand Down

0 comments on commit f55ac48

Please sign in to comment.