From 4d8e2f8d798627d7328ce1d6ec462cc34c84a69a Mon Sep 17 00:00:00 2001 From: Yaron Wittenstein Date: Thu, 25 Jul 2019 13:19:45 +0300 Subject: [PATCH] Closes #9. The last wasmer version seems to solve the SEGFAULT the specific wasmer fix-issue: [fix Drop of uninit Ctx; use MaybeUninit](https://github.com/wasmerio/wasmer/pull/576) --- crates/svm-wasmer-c-api/Cargo.lock | 12 +- crates/svm-wasmer-c-api/src/instance.rs | 41 +++++-- crates/svm-wasmer/tests/storage.rs | 148 ++---------------------- 3 files changed, 45 insertions(+), 156 deletions(-) diff --git a/crates/svm-wasmer-c-api/Cargo.lock b/crates/svm-wasmer-c-api/Cargo.lock index d550e16ad..90d55636c 100644 --- a/crates/svm-wasmer-c-api/Cargo.lock +++ b/crates/svm-wasmer-c-api/Cargo.lock @@ -1177,7 +1177,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "wasmer-clif-backend" version = "0.5.7" -source = "git+https://github.com/spacemeshos/wasmer?branch=develop#c767b265d7a40a596094c707992e4812d1b779ad" +source = "git+https://github.com/spacemeshos/wasmer?branch=develop#6cc31d3b49f1299b746125d81b88f589169239c3" dependencies = [ "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "cranelift-codegen 0.31.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1227,7 +1227,7 @@ dependencies = [ [[package]] name = "wasmer-runtime" version = "0.5.7" -source = "git+https://github.com/spacemeshos/wasmer?branch=develop#c767b265d7a40a596094c707992e4812d1b779ad" +source = "git+https://github.com/spacemeshos/wasmer?branch=develop#6cc31d3b49f1299b746125d81b88f589169239c3" dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "memmap 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1239,7 +1239,7 @@ dependencies = [ [[package]] name = "wasmer-runtime-c-api" version = "0.5.7" -source = "git+https://github.com/spacemeshos/wasmer?branch=develop#c767b265d7a40a596094c707992e4812d1b779ad" +source = "git+https://github.com/spacemeshos/wasmer?branch=develop#6cc31d3b49f1299b746125d81b88f589169239c3" dependencies = [ "cbindgen 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1250,7 +1250,7 @@ dependencies = [ [[package]] name = "wasmer-runtime-core" version = "0.5.7" -source = "git+https://github.com/spacemeshos/wasmer?branch=develop#c767b265d7a40a596094c707992e4812d1b779ad" +source = "git+https://github.com/spacemeshos/wasmer?branch=develop#6cc31d3b49f1299b746125d81b88f589169239c3" dependencies = [ "bincode 1.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "blake2b_simd 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1279,7 +1279,7 @@ dependencies = [ [[package]] name = "wasmer-singlepass-backend" version = "0.5.7" -source = "git+https://github.com/spacemeshos/wasmer?branch=develop#c767b265d7a40a596094c707992e4812d1b779ad" +source = "git+https://github.com/spacemeshos/wasmer?branch=develop#6cc31d3b49f1299b746125d81b88f589169239c3" dependencies = [ "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "colored 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1297,7 +1297,7 @@ dependencies = [ [[package]] name = "wasmer-win-exception-handler" version = "0.5.7" -source = "git+https://github.com/spacemeshos/wasmer?branch=develop#c767b265d7a40a596094c707992e4812d1b779ad" +source = "git+https://github.com/spacemeshos/wasmer?branch=develop#6cc31d3b49f1299b746125d81b88f589169239c3" dependencies = [ "bindgen 0.46.0 (registry+https://github.com/rust-lang/crates.io-index)", "cmake 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/crates/svm-wasmer-c-api/src/instance.rs b/crates/svm-wasmer-c-api/src/instance.rs index 57199bf0c..51df225d8 100644 --- a/crates/svm-wasmer-c-api/src/instance.rs +++ b/crates/svm-wasmer-c-api/src/instance.rs @@ -9,7 +9,7 @@ use svm_storage::memory::{MemKVStore, MemPageCache32, MemPages}; use wasmer_runtime::{func, imports, ImportObject}; use wasmer_runtime_c_api::{ - export::wasmer_import_export_value, + export::{wasmer_import_export_kind, wasmer_import_export_value}, import::{wasmer_import_func_t, wasmer_import_t}, instance::wasmer_instance_t, wasmer_byte_array, wasmer_result_t, @@ -93,8 +93,8 @@ mod tests { return 123; } - fn cast_str_to_wasmer_byte_array(s: String) -> wasmer_byte_array { - let bytes_vec = s.into_bytes(); + fn cast_str_to_wasmer_byte_array(s: &str) -> wasmer_byte_array { + let bytes_vec = s.bytes(); let bytes_len: u32 = bytes_vec.len() as u32; let boxed_bytes = Box::new(bytes_vec); @@ -103,7 +103,7 @@ mod tests { wasmer_byte_array { bytes, bytes_len } } - macro_rules! func_as_wasmer_import_export_value { + macro_rules! func_as_wasmer_import_func_t { ($func: path, $params: expr, $returns: expr) => { unsafe { let export = Box::new(Export::Function { @@ -112,14 +112,15 @@ mod tests { signature: Arc::new(FuncSig::new($params, $returns)), }); - Box::into_raw(export) as *mut wasmer_import_func_t + Box::into_raw(export) as *const wasmer_import_func_t } }; } #[test] fn create_import_object() { - let import_object: *const *const c_void; + let import_object: *mut *mut c_void = + unsafe { std::mem::MaybeUninit::uninit().assume_init() }; let node_data = NodeData { ip: [192, 168, 1, 10], @@ -129,15 +130,31 @@ mod tests { let addr_ptr: *const u8 = Address::from(0x11_22_33_44).as_ptr(); let node_data_ptr: *const c_void = &node_data as *const NodeData as *const _; - let imports: *mut wasmer_import_t; - let imports_len: libc::c_int = 1; - let params = vec![Type::I32]; let returns = vec![Type::I64]; + let get_balance_ptr: *const wasmer_import_func_t = + func_as_wasmer_import_func_t!(get_balance, params, returns); + + let mut get_balance_import = wasmer_import_t { + module_name: cast_str_to_wasmer_byte_array("env"), + import_name: cast_str_to_wasmer_byte_array("get_balance"), + tag: wasmer_import_export_kind::WASM_FUNCTION, + value: wasmer_import_export_value { + func: get_balance_ptr, + }, + }; - let get_balance_ptr: *mut wasmer_import_export_value = - func_as_wasmer_import_export_value!(get_balance, params, returns) as *mut _; + let imports: *mut wasmer_import_t = &mut get_balance_import as *mut _; + let imports_len: libc::c_int = 1; - // wasmer_svm_import_object(import_object, addr_ptr, node_data_ptr, + unsafe { + let _ = wasmer_svm_import_object( + import_object, + addr_ptr, + node_data_ptr, + imports, + imports_len, + ); + } } } diff --git a/crates/svm-wasmer/tests/storage.rs b/crates/svm-wasmer/tests/storage.rs index 97cb95768..e6155165a 100644 --- a/crates/svm-wasmer/tests/storage.rs +++ b/crates/svm-wasmer/tests/storage.rs @@ -3,105 +3,14 @@ use std::cell::Cell; use svm_storage::memory::{MemKVStore, MemPageCache32, MemPages}; use svm_wasmer::*; -use wasmer_runtime::{error, func, imports, Func, Module}; +use wasmer_runtime::{func, imports, Func}; // injecting the `wasmer svm storage vmcalls` implemented with `MemPageCache<[u8; 32]>` as the `PageCache` type include_wasmer_svm_storage_vmcalls!(MemPageCache32); -fn wasmer_compile_module_func(wasm: &str) -> error::CompileResult { - let wasm = wabt::wat2wasm(&wasm).unwrap(); - - wasmer_runtime::compile(&wasm) -} - -const WASM_MEM_TO_REG_COPY: &'static str = r#" - (module - ;; import `svm` vmcalls - (func $svm_mem_to_reg_copy (import "svm" "mem_to_reg_copy") (param i32 i32 i32 i32)) - (memory 1) ;; memory `0` (default) is initialized with a `1 page` - ;; exported function to be called - (func (export "do_copy_to_reg") (param i32 i32 i32) - i32.const 0 ;; $src_mem_idx - get_local 0 ;; $src_mem_ptr - get_local 1 ;; len - get_local 2 ;; dst_reg - call $svm_mem_to_reg_copy))"#; - -const WASM_REG_TO_MEM_COPY: &'static str = r#" - (module - ;; import `svm` vmcalls - (func $svm_reg_to_mem_copy (import "svm" "reg_to_mem_copy") (param i32 i32 i32 i32)) - (memory 1) ;; memory `0` (default) is initialized with a `1 page` - ;; exported function to be called - (func (export "do_copy_to_mem") (param i32 i32 i32) - get_local 0 ;; src_reg - get_local 1 ;; len - i32.const 0 ;; dst_mem_idx - get_local 2 ;; dst_mem_ptr - call $svm_reg_to_mem_copy))"#; - -const WASM_STORAGE_TO_REG_COPY: &'static str = r#" - (module - ;; import `svm` vmcalls - (func $storage_read_to_reg (import "svm" "storage_read_to_reg") (param i32 i32 i32 i32 i32)) - (memory 1) ;; memory `0` (default) is initialized with a `1 page` - ;; exported function to be called - (func (export "do_copy_to_reg") (param i32 i32 i32 i32 i32) - get_local 0 ;; src_page - get_local 1 ;; src_slice - get_local 2 ;; offset - get_local 3 ;; len - get_local 4 ;; dst_reg - call $storage_read_to_reg))"#; - -const WASM_STORAGE_TO_MEM_COPY: &'static str = r#" - (module - ;; import `svm` vmcalls - (func $storage_read_to_mem (import "svm" "storage_read_to_mem") (param i32 i32 i32 i32 i32 i32)) - (memory 1) ;; memory `0` (default) is initialized with a `1 page` - ;; exported function to be called - (func (export "do_copy_to_mem") (param i32 i32 i32 i32 i32) - get_local 0 ;; src_page - get_local 1 ;; src_slice - get_local 2 ;; offset - get_local 3 ;; len - i32.const 0 ;; dst_mem_idx - get_local 4 ;; dst_mem_ptr - call $storage_read_to_mem))"#; - -const WASM_STORAGE_WRITE_FROM_MEM: &'static str = r#" - (module - ;; import `svm` vmcalls - (func $storage_write_from_mem (import "svm" "storage_write_from_mem") (param i32 i32 i32 i32 i32 i32)) - (memory 1) ;; memory `0` (default) is initialized with a `1 page` - ;; exported function to be called - (func (export "do_write_from_mem") (param i32 i32 i32 i32 i32) - i32.const 0 ;; src_mem_idx - get_local 0 ;; src_mem_ptr - get_local 1 ;; len - get_local 2 ;; dst_page - get_local 3 ;; dst_slice - get_local 4 ;; dst_offset - call $storage_write_from_mem))"#; - -const WASM_STORAGE_WRITE_FROM_REG: &'static str = r#" - (module - ;; import `svm` vmcalls - (func $storage_write_from_reg (import "svm" "storage_write_from_reg") (param i32 i32 i32 i32 i32)) - (memory 1) ;; memory `0` (default) is initialized with a `1 page` - ;; exported function to be called - (func (export "do_write_from_reg") (param i32 i32 i32 i32 i32) - get_local 0 ;; src_reg - get_local 1 ;; len - get_local 2 ;; dst_page - get_local 3 ;; dst_slice - get_local 4 ;; dst_offset - call $storage_write_from_reg))"#; - macro_rules! wasmer_compile_module { ($wasm:expr) => {{ let wasm = wabt::wat2wasm(&$wasm).unwrap(); - wasmer_runtime::compile(&wasm).unwrap() }}; } @@ -113,36 +22,6 @@ macro_rules! wasmer_compile_module_file { }}; } -#[test] -#[ignore] -fn vmcalls_mem_to_reg_copy_sefault_bug() { - let module = wasmer_compile_module_file!("wasm/mem_to_reg_copy.wast"); - - let import_object = imports! { - lazy_create_svm_state_gen!(std::ptr::null(), Address::from(0x12_34_56_78), MemKVStore, MemPages, MemPageCache32, 5, 100), - - "svm" => { - "mem_to_reg_copy" => func!(mem_to_reg_copy), - }, - }; - - let instance = module.instantiate(&import_object).unwrap(); - - // initializing memory #0 cells `200..203` with values `10, 20, 30` respectively - wasmer_ctx_mem_cells_write!(instance.context(), 0, 200, &[10, 20, 30]); - - // asserting register content is empty prior copy - let reg = wasmer_ctx_reg!(instance.context(), 2, MemPageCache32); - assert_eq!([0, 0, 0, 0, 0, 0, 0, 0], reg.get()); - - let do_copy: Func<(i32, i32, i32)> = instance.func("do_copy_to_reg").unwrap(); - assert!(do_copy.call(200, 3, 2).is_ok()); - - // asserting register content is `10, 20, 30, 0, ... 0` - let reg = wasmer_ctx_reg!(instance.context(), 2, MemPageCache32); - assert_eq!([10, 20, 30, 0, 0, 0, 0, 0], reg.get()); -} - #[test] fn vmcalls_empty_wasm() { let wasm = r#" @@ -155,8 +34,7 @@ fn vmcalls_empty_wasm() { #[test] fn vmcalls_mem_to_reg_copy() { - let module = wasmer_compile_module_func(WASM_MEM_TO_REG_COPY).unwrap(); - // let module = wasmer_compile_module_file!("wasm/mem_to_reg_copy.wast"); + let module = wasmer_compile_module_file!("wasm/mem_to_reg_copy.wast"); let import_object = imports! { lazy_create_svm_state_gen!(std::ptr::null(), Address::from(0x12_34_56_78), MemKVStore, MemPages, MemPageCache32, 5, 100), @@ -185,8 +63,7 @@ fn vmcalls_mem_to_reg_copy() { #[test] fn vmcalls_reg_to_mem_copy() { - let module = wasmer_compile_module_func(WASM_REG_TO_MEM_COPY).unwrap(); - // let module = wasmer_compile_module_file!("wasm/reg_to_mem_copy.wast"); + let module = wasmer_compile_module_file!("wasm/reg_to_mem_copy.wast"); let import_object = imports! { lazy_create_svm_state_gen!(std::ptr::null(), Address::from(0x12_34_56_78), MemKVStore, MemPages, MemPageCache32, 5, 100), @@ -216,8 +93,7 @@ fn vmcalls_reg_to_mem_copy() { #[test] fn vmcalls_storage_read_an_empty_page_slice_to_reg() { - let module = wasmer_compile_module_func(WASM_STORAGE_TO_REG_COPY).unwrap(); - // let module = wasmer_compile_module_file!("wasm/storage_to_reg_copy.wast"); + let module = wasmer_compile_module_file!("wasm/storage_to_reg_copy.wast"); let import_object = imports! { lazy_create_svm_state_gen!(std::ptr::null(), Address::from(0x12_34_56_78), MemKVStore, MemPages, MemPageCache32, 5, 100), @@ -246,8 +122,7 @@ fn vmcalls_storage_read_an_empty_page_slice_to_reg() { #[test] fn vmcalls_storage_read_non_empty_page_slice_to_reg() { - let module = wasmer_compile_module_func(WASM_STORAGE_TO_REG_COPY).unwrap(); - // let module = wasmer_compile_module_file!("wasm/storage_to_reg_copy.wast"); + let module = wasmer_compile_module_file!("wasm/storage_to_reg_copy.wast"); let import_object = imports! { lazy_create_svm_state_gen!(std::ptr::null(), Address::from(0x12_34_56_78), MemKVStore, MemPages, MemPageCache32, 5, 100), @@ -280,8 +155,7 @@ fn vmcalls_storage_read_non_empty_page_slice_to_reg() { #[test] fn vmcalls_storage_read_an_empty_page_slice_to_mem() { - let module = wasmer_compile_module_func(WASM_STORAGE_TO_MEM_COPY).unwrap(); - // let module = wasmer_compile_module_file!("wasm/storage_to_mem_copy.wast"); + let module = wasmer_compile_module_file!("wasm/storage_to_mem_copy.wast"); let import_object = imports! { lazy_create_svm_state_gen!(std::ptr::null(), Address::from(0x12_34_56_78), MemKVStore, MemPages, MemPageCache32, 5, 100), @@ -308,8 +182,7 @@ fn vmcalls_storage_read_an_empty_page_slice_to_mem() { #[test] fn vmcalls_storage_read_non_empty_page_slice_to_mem() { - let module = wasmer_compile_module_func(WASM_STORAGE_TO_MEM_COPY).unwrap(); - // let module = wasmer_compile_module_file!("wasm/storage_to_mem_copy.wast"); + let module = wasmer_compile_module_file!("wasm/storage_to_mem_copy.wast"); let import_object = imports! { lazy_create_svm_state_gen!(std::ptr::null(), Address::from(0x12_34_56_78), MemKVStore, MemPages, MemPageCache32, 5, 100), @@ -337,8 +210,8 @@ fn vmcalls_storage_read_non_empty_page_slice_to_mem() { #[test] fn vmcalls_storage_write_from_mem() { - let module = wasmer_compile_module_func(WASM_STORAGE_WRITE_FROM_MEM).unwrap(); - // let module = wasmer_compile_module_file!("wasm/storage_write_from_mem.wast"); + // let module = wasmer_compile_module_func(WASM_STORAGE_WRITE_FROM_MEM).unwrap(); + let module = wasmer_compile_module_file!("wasm/storage_write_from_mem.wast"); let import_object = imports! { lazy_create_svm_state_gen!(std::ptr::null(), Address::from(0x12_34_56_78), MemKVStore, MemPages, MemPageCache32, 5, 100), @@ -367,8 +240,7 @@ fn vmcalls_storage_write_from_mem() { #[test] fn vmcalls_storage_write_from_reg() { - let module = wasmer_compile_module_func(WASM_STORAGE_WRITE_FROM_REG).unwrap(); - // let module = wasmer_compile_module_file!("wasm/storage_write_from_reg.wast"); + let module = wasmer_compile_module_file!("wasm/storage_write_from_reg.wast"); let import_object = imports! { lazy_create_svm_state_gen!(std::ptr::null(), Address::from(0x12_34_56_78), MemKVStore, MemPages, MemPageCache32, 5, 100),