Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime): Wasmer 1.0 runner #3799

Merged
merged 60 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
17c9c7d
initial add wasmer 1.0 and features
ailisp Jan 5, 2021
bef831c
wasmtime neard compiles
ailisp Jan 5, 2021
22a3610
wasmtime neard compiles
ailisp Jan 5, 2021
d248733
all driver code
ailisp Jan 5, 2021
4408a0e
wip wasmer 1.0
ailisp Jan 6, 2021
ee61fbc
translate unsafe wasmer 0.17 ctx.data as safe 1.0 myEnv.logic
ailisp Jan 7, 2021
7b82b8d
fix most compile errors except wasmer error conversion
ailisp Jan 7, 2021
cac1740
fix more errors, stuck in wasmer 1.0 cannot import native func return…
ailisp Jan 9, 2021
3e52892
fix HostFunction trait not implemented issue
ailisp Jan 9, 2021
e064e6a
fix HostFunction trait not implemented issue
ailisp Jan 9, 2021
cd5d291
fix compilation errors
ailisp Jan 9, 2021
746ea2b
fix vmlogic inject
ailisp Jan 11, 2021
a63ec4a
adding all into_vm_errors, fixed some tests
ailisp Jan 12, 2021
a83d0ab
avx check and no nan check needed now
ailisp Jan 12, 2021
e563ad3
fix two more tests
ailisp Jan 12, 2021
2d1c129
fix runner stuck infinite
ailisp Jan 14, 2021
6c7ad15
fix runtime error downcast error
ailisp Jan 14, 2021
6e064f6
refactor vmlogicerror into vmerror
ailisp Jan 14, 2021
11be303
fix runtime error on call initializer
ailisp Jan 14, 2021
beccd74
fix all vm error test
ailisp Jan 14, 2021
fc4aca9
rename VMKind::Wasmer to Wasmer0
ailisp Jan 15, 2021
b3264ee
add cranelift-frontend to cargo deny
ailisp Jan 15, 2021
c5417e8
resolve conflict
ailisp Jan 15, 2021
cbd5169
fix cargo deny
ailisp Jan 15, 2021
69a77e0
fix test workspace
ailisp Jan 15, 2021
43fb8a3
workaround all-features
ailisp Jan 15, 2021
20dbb3f
reuse import reference to vmlogicreference
ailisp Jan 15, 2021
d3f2ad2
test_stack_overflow for wasmer1
ailisp Jan 15, 2021
1a2c845
fix test_stack_overflow for wasmer1
ailisp Jan 15, 2021
672e029
Merge branch 'master' into wasmer-1.0
ailisp Jan 15, 2021
b802df2
upgrade to git wasmer to get internal of RuntimeError
ailisp Jan 16, 2021
5061b4c
test wasmer trap and user error
ailisp Jan 16, 2021
5d632f5
merge master
ailisp Jan 19, 2021
841482d
rename
ailisp Jan 20, 2021
50911ae
Merge branch 'master' into wasmer-1.0
ailisp Jan 20, 2021
0f0751e
Merge branch 'master' into wasmer-1.0
olonho Jan 27, 2021
1638987
fix a compilation error from merge
ailisp Feb 1, 2021
82f69a8
actix 0.9 also works, so undo it
ailisp Feb 2, 2021
d4c8ad3
merge master resolve conflict
ailisp Feb 3, 2021
d0f377b
enable ability to build neard with wasmer 1.0
ailisp Feb 3, 2021
f452f9d
cache in wasmer1
ailisp Feb 4, 2021
afa9b25
remove delay detector
ailisp Feb 5, 2021
df0b5bf
fix feature set for ci
ailisp Feb 5, 2021
1848478
use fork released wasmer
ailisp Feb 5, 2021
e202e37
merge master
ailisp Feb 5, 2021
6f32f4e
resolve conflict in Cargo.lock
ailisp Feb 5, 2021
bae0e08
cargo deny
ailisp Feb 5, 2021
7fc2454
wasmer 1.0.2
ailisp Feb 5, 2021
9ca118e
error into
ailisp Feb 12, 2021
56e3786
typo
ailisp Feb 12, 2021
51ffe38
precompile wasmer1
ailisp Feb 12, 2021
cc338ff
wasmer1 by default, optional wasmer 0
ailisp Feb 13, 2021
b1fb114
test pass again
ailisp Feb 13, 2021
e22752a
resolve conflict
ailisp Feb 13, 2021
365ccbf
fix all tests
ailisp Feb 17, 2021
ee1b5ea
wasmer0 as default, all vm enabled in tests
ailisp Feb 17, 2021
61b8514
fix cargo check all features
ailisp Feb 17, 2021
c565693
Merge branch 'master' into wasmer-1.0
ailisp Feb 17, 2021
d7e2e1c
nit
ailisp Feb 23, 2021
6cec70f
Merge branch 'master' into wasmer-1.0
ailisp Feb 23, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
460 changes: 435 additions & 25 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,5 @@ protocol_feature_forward_chunk_parts = ["neard/protocol_feature_forward_chunk_pa
nightly_protocol = ["near-primitives/nightly_protocol", "near-jsonrpc/nightly_protocol"]
nightly_protocol_features = ["nightly_protocol", "neard/nightly_protocol_features", "protocol_feature_evm"]
protocol_feature_evm = ["neard/protocol_feature_evm", "testlib/protocol_feature_evm", "near-runtime-standalone/protocol_feature_evm", "runtime-params-estimator/protocol_feature_evm"]
wasmtime_vm = ["neard/wasmtime_vm"]
wasmer1_vm = ["neard/wasmer1_vm"]
2 changes: 2 additions & 0 deletions neard/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ protocol_feature_rectify_inflation = ["near-epoch-manager/protocol_feature_recti
protocol_feature_evm = ["near-primitives/protocol_feature_evm", "node-runtime/protocol_feature_evm", "near-chain-configs/protocol_feature_evm", "near-chain/protocol_feature_evm"]
nightly_protocol_features = ["nightly_protocol", "near-primitives/nightly_protocol_features", "near-client/nightly_protocol_features", "near-epoch-manager/nightly_protocol_features", "near-store/nightly_protocol_features", "protocol_feature_forward_chunk_parts", "protocol_feature_rectify_inflation", "protocol_feature_evm"]
nightly_protocol = ["near-primitives/nightly_protocol", "near-jsonrpc/nightly_protocol"]
wasmtime_vm = ["node-runtime/wasmtime_vm"]
wasmer1_vm = ["node-runtime/wasmer1_vm"]

[[bin]]
path = "src/main.rs"
Expand Down
8 changes: 8 additions & 0 deletions runtime/near-vm-errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ pub enum FunctionCallError {
WasmUnknownError,
HostError(HostError),
EvmError(EvmError),
/// An error message when wasmer 1.0 returns a wasmer::RuntimeError
/// In wasmer 1.0 the internal of runtime error is not pub, so we cannot tell it's a UserError
/// or Trap, and which kind of Trap. The pub thing we have is a error message returns by wasmer,
/// Indicate the reason of RuntimeError, and if it's a Trap what kind of trap it is.
WasmerRuntimeError(String),
ailisp marked this conversation as resolved.
Show resolved Hide resolved
}
#[derive(
Debug, Clone, PartialEq, Eq, BorshDeserialize, BorshSerialize, Deserialize, Serialize, RpcError,
Expand Down Expand Up @@ -272,6 +277,8 @@ pub enum VMLogicError {
EvmError(EvmError),
}

impl std::error::Error for VMLogicError {}

/// An error that is caused by an operation on an inconsistent state.
/// E.g. a deserialization error or an integer overflow.
#[derive(Debug, Clone, PartialEq, Eq, BorshDeserialize, BorshSerialize, Deserialize, Serialize)]
Expand Down Expand Up @@ -336,6 +343,7 @@ impl fmt::Display for FunctionCallError {
write!(f, "Unknown error during Wasm contract execution")
}
FunctionCallError::EvmError(e) => write!(f, "EVM: {:?}", e),
FunctionCallError::WasmerRuntimeError(e) => write!(f, "Wasmer Runtime: {}", e),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/near-vm-logic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ serde_json = {version= "1", features= ["preserve_order"]}
default = ["costs_counting"]
protocol_feature_evm = ["near-runtime-fees/protocol_feature_evm"]
wasmtime_default = []

wasmer1_default = []

# Use this feature to enable counting of fees and costs applied.
costs_counting = []
Expand Down
15 changes: 13 additions & 2 deletions runtime/near-vm-logic/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ use std::hash::{Hash, Hasher};

#[derive(Clone, Copy, Debug, Hash, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub enum VMKind {
/// Wasmer VM.
/// Wasmer 0.17.x VM.
Wasmer,
ailisp marked this conversation as resolved.
Show resolved Hide resolved
/// Wasmtime VM.
Wasmtime,
/// Wasmer 1.x VM.
Wasmer1,
}

impl Default for VMKind {
Expand All @@ -19,12 +21,21 @@ impl Default for VMKind {
VMKind::Wasmer
}

#[cfg(feature = "wasmer1_default")]
fn default() -> Self {
VMKind::Wasmer1
}

#[cfg(feature = "wasmtime_default")]
fn default() -> Self {
VMKind::Wasmtime
}

#[cfg(all(not(feature = "wasmer_default"), not(feature = "wasmtime_default")))]
#[cfg(all(
not(feature = "wasmer_default"),
not(feature = "wasmer1_default"),
not(feature = "wasmtime_default")
))]
fn default() -> Self {
VMKind::Wasmer
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/near-vm-runner-standalone/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ strum = "0.18"
num-rational = { version = "0.2.4" }

near-vm-logic = { path = "../near-vm-logic", version = "2.3.0", features = ["costs_counting"]}
near-vm-runner = { path = "../near-vm-runner", version = "2.3.0", features = ["wasmtime_vm"] }
near-vm-runner = { path = "../near-vm-runner", version = "2.3.0", features = ["wasmtime_vm", "wasmer1_vm"] }
near-runtime-fees = { path = "../near-runtime-fees", version = "2.3.0" }

[features]
Expand Down
1 change: 1 addition & 0 deletions runtime/near-vm-runner-standalone/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ fn main() {
Some(value) => match value {
"wasmtime" => VMKind::Wasmtime,
"wasmer" => VMKind::Wasmer,
"wasmer1" => VMKind::Wasmer1,
_ => VMKind::default(),
},
None => VMKind::default(),
Expand Down
6 changes: 5 additions & 1 deletion runtime/near-vm-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ This crate implements the specification of the interface that Near blockchain ex
borsh = "0.7.1"
wasmer-runtime = { version="0.17.1", features = ["default-backend-singlepass"], default-features = false, package = "wasmer-runtime-near" }
wasmer-runtime-core = {version = "0.17.1", package = "wasmer-runtime-core-near" }
ailisp marked this conversation as resolved.
Show resolved Hide resolved
wasmer = { version = "1.0.0", features = ["singlepass"], optional = true }
wasmer-types = { version = "1.0.0", optional = true}
pwasm-utils = "0.12"
parity-wasm = "0.41"
wasmtime = { version = "0.20.0", default-features = false, optional = true }
Expand All @@ -33,11 +35,13 @@ wabt = "0.9"
bencher = "^0.1.5"

[features]
default = [ "wasmer_default" ]
default = [ "wasmer_default", "wasmer", "wasmer-types" ]
wasmtime_vm = [ "wasmtime", "anyhow"]
wasmer1_vm = [ "wasmer", "wasmer-types" ]
lightbeam = ["wasmtime/lightbeam"]
wasmer_default = []
wasmtime_default = ["wasmtime_vm"]
wasmer1_default = [ "wasmer1_vm" ]
no_cpu_compatibility_checks = []
protocol_feature_evm = ["near-runtime-fees/protocol_feature_evm", "near-evm-runner/protocol_feature_evm"]

Expand Down
40 changes: 40 additions & 0 deletions runtime/near-vm-runner/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ struct ImportReference(*mut c_void);
unsafe impl Send for ImportReference {}
unsafe impl Sync for ImportReference {}

#[derive(Clone)]
pub struct VMLogicReference(pub *mut c_void);
unsafe impl Send for VMLogicReference {}
unsafe impl Sync for VMLogicReference {}
ailisp marked this conversation as resolved.
Show resolved Hide resolved

// Wasm has only i32/i64 types, so Wasmtime 0.17 only accepts
// external functions taking i32/i64 type.
// Remove, once using version with https://github.com/bytecodealliance/wasmtime/issues/1829
Expand Down Expand Up @@ -38,6 +43,27 @@ macro_rules! wrapped_imports {
)*
}

#[cfg(feature = "wasmer1_vm")]
pub mod wasmer1_ext {
use near_vm_logic::VMLogic;
use wasmer::{WasmerEnv, Memory};
use crate::imports::VMLogicReference;
#[derive(WasmerEnv, Clone)]
pub struct MyEnv {
pub memory: Memory,
pub logic: VMLogicReference,
}

type VMResult<T> = ::std::result::Result<T, near_vm_logic::VMLogicError>;
$(
#[allow(unused_parens)]
pub fn $func(env: &MyEnv, $( $arg_name: $arg_type ),* ) -> VMResult<($( $returns ),*)> {
let logic: &mut VMLogic = unsafe { &mut *(env.logic.0 as *mut VMLogic<'_>) };
logic.$func( $( $arg_name, )* )
}
)*
}

#[cfg(feature = "wasmtime_vm")]
pub mod wasmtime_ext {
use near_vm_logic::{VMLogic, VMLogicError};
Expand Down Expand Up @@ -94,6 +120,20 @@ macro_rules! wrapped_imports {
}
}

#[cfg(feature = "wasmer1_vm")]
pub(crate) fn build_wasmer1(store: &wasmer::Store, memory: wasmer::Memory, logic: &mut VMLogic<'_>) ->
wasmer::ImportObject {
let env = wasmer1_ext::MyEnv {logic: VMLogicReference(logic as * mut _ as * mut c_void), memory: memory.clone()};
wasmer::imports! {
"env" => {
"memory" => memory,
$(
stringify!($func) => wasmer::Function::new_native_with_env(&store, env.clone(), wasmer1_ext::$func),
)*
ailisp marked this conversation as resolved.
Show resolved Hide resolved
},
}
}

#[cfg(feature = "wasmtime_vm")]
pub(crate) fn link_wasmtime(
linker: &mut wasmtime::Linker,
Expand Down
5 changes: 5 additions & 0 deletions runtime/near-vm-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ mod memory;
pub mod prepare;
mod runner;
mod wasmer_runner;

#[cfg(feature = "wasmtime_vm")]
mod wasmtime_runner;

#[cfg(feature = "wasmer1_vm")]
mod wasmer1_runner;

pub use near_vm_errors::VMError;
pub use runner::compile_module;
pub use runner::run;
Expand Down
52 changes: 52 additions & 0 deletions runtime/near-vm-runner/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ pub fn run_vm<'a>(
cache: Option<&'a dyn CompiledContractCache>,
) -> (Option<VMOutcome>, Option<VMError>) {
use crate::wasmer_runner::run_wasmer;

#[cfg(feature = "wasmtime_vm")]
use crate::wasmtime_runner::wasmtime_runner::run_wasmtime;

#[cfg(feature = "wasmer1_vm")]
use crate::wasmer1_runner::run_wasmer1;

match vm_kind {
VMKind::Wasmer => run_wasmer(
code_hash,
Expand Down Expand Up @@ -90,6 +95,22 @@ pub fn run_vm<'a>(
VMKind::Wasmtime => {
panic!("Wasmtime is not supported, compile with '--features wasmtime_vm'")
}
#[cfg(feature = "wasmer1_vm")]
VMKind::Wasmer1 => run_wasmer1(
code_hash,
code,
method_name,
ext,
context,
wasm_config,
fees_config,
promise_results,
None,
current_protocol_version,
cache,
),
#[cfg(not(feature = "wasmer1_vm"))]
VMKind::Wasmer1 => panic!("Wasmer1 is not supported, compile with '--features wasmer1_vm'"),
}
}

Expand All @@ -108,8 +129,12 @@ pub fn run_vm_profiled<'a>(
cache: Option<&'a dyn CompiledContractCache>,
) -> (Option<VMOutcome>, Option<VMError>) {
use crate::wasmer_runner::run_wasmer;

#[cfg(feature = "wasmtime_vm")]
use crate::wasmtime_runner::wasmtime_runner::run_wasmtime;

#[cfg(feature = "wasmer1_vm")]
use crate::wasmer1_runner::run_wasmer1;
match vm_kind {
VMKind::Wasmer => run_wasmer(
code_hash,
Expand Down Expand Up @@ -142,13 +167,33 @@ pub fn run_vm_profiled<'a>(
VMKind::Wasmtime => {
panic!("Wasmtime is not supported, compile with '--features wasmtime_vm'")
}
#[cfg(feature = "wasmer1_vm")]
VMKind::Wasmer1 => run_wasmer1(
code_hash,
code,
method_name,
ext,
context,
wasm_config,
fees_config,
promise_results,
Some(profile),
current_protocol_version,
cache,
),
#[cfg(not(feature = "wasmer1_vm"))]
VMKind::Wasmer1 => panic!("Wasmer1 is not supported, compile with '--features wasmer1_vm'"),
}
}

pub fn with_vm_variants(runner: fn(VMKind) -> ()) {
runner(VMKind::Wasmer);

#[cfg(feature = "wasmtime_vm")]
runner(VMKind::Wasmtime);

#[cfg(feature = "wasmer1_vm")]
runner(VMKind::Wasmer1);
}

/// Used for testing cost of compiling a module
Expand All @@ -167,6 +212,13 @@ pub fn compile_module(vm_kind: VMKind, code: &Vec<u8>) -> bool {
VMKind::Wasmtime => {
panic!("Wasmtime is not supported, compile with '--features wasmtime_vm'")
}
#[cfg(feature = "wasmer1_vm")]
VMKind::Wasmer1 => {
use crate::wasmer1_runner::compile_module;
compile_module(code)
}
#[cfg(not(feature = "wasmer1_vm"))]
VMKind::Wasmer1 => panic!("Wasmer1 is not supported, compile with '--features wasm1_vm'"),
ailisp marked this conversation as resolved.
Show resolved Hide resolved
};
false
}
Loading