Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Commit

Permalink
Closes #9. The last wasmer version seems to solve the SEGFAULT
Browse files Browse the repository at this point in the history
the specific wasmer fix-issue:
[fix Drop of uninit Ctx; use MaybeUninit](wasmerio/wasmer#576)
  • Loading branch information
YaronWittenstein committed Jul 25, 2019
1 parent 0e3a0fd commit 4d8e2f8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 156 deletions.
12 changes: 6 additions & 6 deletions crates/svm-wasmer-c-api/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 29 additions & 12 deletions crates/svm-wasmer-c-api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand All @@ -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],
Expand All @@ -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,
);
}
}
}
148 changes: 10 additions & 138 deletions crates/svm-wasmer/tests/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Module> {
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()
}};
}
Expand All @@ -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#"
Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 4d8e2f8

Please sign in to comment.