forked from wasmerio/wasmer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fuzz: add a test for for deterministic compilation
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
// ); | ||
}); |