Skip to content

Commit

Permalink
Non-determinism: remove threads, simd, add stack check
Browse files Browse the repository at this point in the history
  • Loading branch information
damip authored and gterzian committed Apr 11, 2022
1 parent 6e722ee commit 10deac2
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/execution_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use as_ffi_bindings::{Read as ASRead, StringPtr, Write as ASWrite};
use std::sync::Arc;
use wasmer::WasmerEnv;
use wasmer::{
imports, CompilerConfig, Function, ImportObject, Instance, Module, Store, Universal, Val,
imports, CompilerConfig, Features, Function, ImportObject, Instance, Module, Store, Universal,
Val,
};
use wasmer::{wasmparser::Operator, BaseTunables, Pages, Target};
use wasmer_compiler_singlepass::Singlepass;
Expand All @@ -24,17 +25,34 @@ fn create_instance(limit: u64, module: &[u8], env: &Env) -> Result<Instance> {
// See https://docs.rs/wasmer-compiler-singlepass/latest/wasmer_compiler_singlepass/
let mut compiler_config = Singlepass::new();

// NaNs are non-deterministic in WASM: https://github.com/WebAssembly/design/blob/main/Nondeterminism.md
// Ensure determinism by canonicalizing their representation
// Turning-off sources of potential non-determinism,
// see https://github.com/WebAssembly/design/blob/037c6fe94151eb13e30d174f5f7ce851be0a573e/Nondeterminism.md

// Turning-off in the compiler:

// Canonicalize NaN.
compiler_config.canonicalize_nans(true);

// Enable stack check.
compiler_config.enable_stack_check(true);

// Turning-off in wasmer feature flags:
let mut features = Features::new();

// Disable threads.
features.threads(false);

// Turn-off experimental SIMD feature.
features.simd(false);

// Add metering middleware
let metering = Arc::new(Metering::new(limit, |_: &Operator| -> u64 { 1 }));
compiler_config.push_middleware(metering);

let base = BaseTunables::for_target(&Target::default());
let tunables = LimitingTunables::new(base, Pages(max_number_of_pages()));
let store = Store::new_with_tunables(&Universal::new(compiler_config).engine(), tunables);
let engine = Universal::new(compiler_config).features(features).engine();
let store = Store::new_with_tunables(&engine, tunables);
let resolver: ImportObject = imports! {
"env" => {
// Needed by wasm generated by AssemblyScript.
Expand Down

0 comments on commit 10deac2

Please sign in to comment.