Skip to content

Commit

Permalink
fuzz: add a test for for deterministic compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
Anbang Wen authored and Amanieu committed Nov 22, 2021
1 parent b32e8d9 commit 4e46c80
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
5 changes: 5 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,8 @@ required-features = ["universal", "cranelift"]
name = "dylib_cranelift"
path = "fuzz_targets/dylib_cranelift.rs"
required-features = ["dylib", "cranelift"]

[[bin]]
name = "deterministic"
path = "fuzz_targets/deterministic.rs"
required-features = ["dylib", "cranelift"]
77 changes: 77 additions & 0 deletions fuzz/fuzz_targets/deterministic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#![no_main]

use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target};
use wasm_smith::{Config, ConfiguredModule};
use wasmer::{CompilerConfig, Engine, Module, Store};
use wasmer_compiler_cranelift::Cranelift;
use wasmer_compiler_llvm::LLVM;
use wasmer_compiler_singlepass::Singlepass;
use wasmer_engine_jit::JIT;
use wasmer_engine_native::Native;

#[derive(Arbitrary, Debug, Default, Copy, Clone)]
struct NoImportsConfig;
impl Config for NoImportsConfig {
fn max_imports(&self) -> usize {
0
}
fn max_memory_pages(&self) -> u32 {
// https://github.com/wasmerio/wasmer/issues/2187
65535
}
fn allow_start_export(&self) -> bool {
false
}
}

fn compile_and_compare(name: &str, engine: impl Engine, wasm: &[u8]) {
let store = Store::new(&engine);

// compile for first time
let module = Module::new(&store, wasm).unwrap();
let first = module.serialize().unwrap();

// compile for second time
let module = Module::new(&store, wasm).unwrap();
let second = module.serialize().unwrap();

if first != second {
panic!("non-deterministic compilation from {}", name);
}
}

fuzz_target!(|module: ConfiguredModule<NoImportsConfig>| {
let wasm_bytes = module.to_bytes();

let mut compiler = Cranelift::default();
compiler.canonicalize_nans(true);
compiler.enable_verifier();
compile_and_compare(
"jit-cranelift",
JIT::new(compiler.clone()).engine(),
&wasm_bytes,
);
compile_and_compare(
"native-cranelift",
Native::new(compiler).engine(),
&wasm_bytes,
);

// let mut compiler = LLVM::default();
// compiler.canonicalize_nans(true);
// compiler.enable_verifier();
// compile_and_compare("jit-llvm", JIT::new(compiler.clone()).engine(), &wasm_bytes);
// compile_and_compare("native-llvm", Native::new(compiler).engine(), &wasm_bytes);

// let compiler = Singlepass::default();
// compile_and_compare(
// "jit-singlepass",
// JIT::new(compiler.clone()).engine(),
// &wasm_bytes,
// );
// compile_and_compare(
// "native-singlepass",
// Native::new(compiler).engine(),
// &wasm_bytes,
// );
});

0 comments on commit 4e46c80

Please sign in to comment.