From 6ca581279811aee9d26d77ed2b7372a6153fe3bf Mon Sep 17 00:00:00 2001 From: Yaron Wittenstein Date: Mon, 7 Oct 2019 16:58:58 +0300 Subject: [PATCH] When `deterministic` feature will be enabled (turned-off by default) it'll guarantee deterministic execution of wasm programs across different hardware/circumstances. This is very useful for Blockchain projects having wasm smart-contracts This is critical for Blockchain projects that require execution to be deterministic in order to reach a consensus of the state transition of each smart-contract transaction. --- Cargo.toml | 4 +++- lib/runtime-core/Cargo.toml | 1 + lib/runtime-core/src/codegen.rs | 3 +++ lib/runtime-core/src/lib.rs | 3 +++ lib/runtime/Cargo.toml | 1 + lib/runtime/src/lib.rs | 12 +++++++----- lib/singlepass-backend/Cargo.toml | 4 ++++ 7 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4d619059e35..93479c41598 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ wabt = "0.9.1" wasmer-clif-backend = { path = "lib/clif-backend" } wasmer-singlepass-backend = { path = "lib/singlepass-backend", optional = true } wasmer-middleware-common = { path = "lib/middleware-common" } -wasmer-runtime = { path = "lib/runtime" } +wasmer-runtime = { path = "lib/runtime", default-features = false } wasmer-runtime-core = { path = "lib/runtime-core" } wasmer-emscripten = { path = "lib/emscripten" } wasmer-llvm-backend = { path = "lib/llvm-backend", optional = true } @@ -101,6 +101,8 @@ backend-singlepass = [ wasi = ["wasmer-wasi"] managed = ["backend-singlepass", "wasmer-runtime-core/managed"] +deterministic = ["wasmer-runtime/deterministic"] + [[example]] name = "plugin" crate-type = ["bin"] diff --git a/lib/runtime-core/Cargo.toml b/lib/runtime-core/Cargo.toml index 7fd9e32e835..ee1fd19ebff 100644 --- a/lib/runtime-core/Cargo.toml +++ b/lib/runtime-core/Cargo.toml @@ -58,3 +58,4 @@ trace = ["debug"] "backend-singlepass" = [] "backend-llvm" = [] managed = [] +deterministic = ["wasmparser/deterministic"] diff --git a/lib/runtime-core/src/codegen.rs b/lib/runtime-core/src/codegen.rs index 3db1b374368..7027d6266a3 100644 --- a/lib/runtime-core/src/codegen.rs +++ b/lib/runtime-core/src/codegen.rs @@ -145,6 +145,9 @@ pub fn validating_parser_config(features: &Features) -> wasmparser::ValidatingPa enable_simd: features.simd, enable_bulk_memory: false, enable_multi_value: false, + + #[cfg(feature = "deterministic")] + deterministic_only: true, }, } } diff --git a/lib/runtime-core/src/lib.rs b/lib/runtime-core/src/lib.rs index 26a76f7dbf6..d933b555f30 100644 --- a/lib/runtime-core/src/lib.rs +++ b/lib/runtime-core/src/lib.rs @@ -145,6 +145,9 @@ pub fn validate_and_report_errors_with_features( enable_multi_value: false, enable_reference_types: false, enable_threads: features.threads, + + #[cfg(feature = "deterministic")] + deterministic_only: true, }, }; let mut parser = wasmparser::ValidatingParser::new(wasm, Some(config)); diff --git a/lib/runtime/Cargo.toml b/lib/runtime/Cargo.toml index 2e42055c2e4..a3a59f7e50e 100644 --- a/lib/runtime/Cargo.toml +++ b/lib/runtime/Cargo.toml @@ -41,6 +41,7 @@ singlepass = ["wasmer-singlepass-backend"] default-backend-singlepass = ["singlepass"] default-backend-llvm = ["llvm"] default-backend-cranelift = ["cranelift"] +deterministic = ["wasmer-singlepass-backend/deterministic"] [[bench]] name = "nginx" diff --git a/lib/runtime/src/lib.rs b/lib/runtime/src/lib.rs index 2945ff3d534..3505de6492d 100644 --- a/lib/runtime/src/lib.rs +++ b/lib/runtime/src/lib.rs @@ -70,7 +70,7 @@ //! let value = add_one.call(42)?; //! //! assert_eq!(value, 43); -//! +//! //! Ok(()) //! } //! ``` @@ -199,13 +199,15 @@ pub fn default_compiler() -> impl Compiler { feature = "default-backend-llvm", any( feature = "default-backend-cranelift", - feature = "default-backend-singlepass" + feature = "default-backend-singlepass", + feature = "deterministic" ) ), all( feature = "default-backend-cranelift", - feature = "default-backend-singlepass" - ) + any(feature = "default-backend-singlepass", feature = "deterministic") + ), + all(feature = "default-backend-singlepass", feature = "deterministic") ))] compile_error!( "The `default-backend-X` features are mutually exclusive. Please choose just one" @@ -214,7 +216,7 @@ pub fn default_compiler() -> impl Compiler { #[cfg(feature = "default-backend-llvm")] use wasmer_llvm_backend::LLVMCompiler as DefaultCompiler; - #[cfg(feature = "default-backend-singlepass")] + #[cfg(any(feature = "default-backend-singlepass", feature = "deterministic"))] use wasmer_singlepass_backend::SinglePassCompiler as DefaultCompiler; #[cfg(feature = "default-backend-cranelift")] diff --git a/lib/singlepass-backend/Cargo.toml b/lib/singlepass-backend/Cargo.toml index 5243cc60150..d6dc63c7a2d 100644 --- a/lib/singlepass-backend/Cargo.toml +++ b/lib/singlepass-backend/Cargo.toml @@ -18,3 +18,7 @@ byteorder = "1.3" nix = "0.15" libc = "0.2.60" smallvec = "0.6" + +[features] +default = [] +deterministic = ["wasmparser/deterministic", "wasmer-runtime-core/deterministic"]