This commit is contained in:
vms 2020-06-01 12:14:12 +03:00
parent 9d3d605190
commit 6e2f17e07f
11 changed files with 124 additions and 47 deletions

View File

@ -32,10 +32,16 @@ pub unsafe fn put(file_content_ptr: *mut u8, file_content_size: usize) {
let cmd = format!("put {}", file_content); let cmd = format!("put {}", file_content);
ipfs(file_content.as_ptr() as _, file_content.len() as _); ipfs(file_content.as_ptr() as _, file_content.len() as _);
let result = "Hello from IPFS node, take your hash".to_string();
*RESULT_PTR.get_mut() = result.as_ptr() as _;
*RESULT_SIZE.get_mut() = result.len();
std::mem::forget(result);
} }
#[no_mangle] #[no_mangle]
pub unsafe fn get(hash_ptr: *mut u8, hash_size: usize, t: i32) { pub unsafe fn get(hash_ptr: *mut u8, hash_size: usize) {
let hash = String::from_raw_parts( let hash = String::from_raw_parts(
hash_ptr, hash_ptr,
hash_size, hash_size,
@ -47,6 +53,12 @@ pub unsafe fn get(hash_ptr: *mut u8, hash_size: usize, t: i32) {
let cmd = format!("get {}", hash); let cmd = format!("get {}", hash);
ipfs(cmd.as_ptr() as _, cmd.len() as _); ipfs(cmd.as_ptr() as _, cmd.len() as _);
let result = "Hello from IPFS node, take your file".to_string();
*RESULT_PTR.get_mut() = result.as_ptr() as _;
*RESULT_SIZE.get_mut() = result.len();
std::mem::forget(result);
} }
#[link(wasm_import_module = "logger")] #[link(wasm_import_module = "logger")]

View File

@ -16,12 +16,17 @@
use std::alloc::{alloc as global_alloc, dealloc as global_dealloc, Layout}; use std::alloc::{alloc as global_alloc, dealloc as global_dealloc, Layout};
use crate::log_utf8_string;
use std::ptr::NonNull; use std::ptr::NonNull;
/// Allocates memory area of specified size and returns its address. /// Allocates memory area of specified size and returns its address.
#[no_mangle] #[no_mangle]
pub unsafe fn allocate(size: usize) -> NonNull<u8> { pub unsafe fn allocate(size: usize) -> NonNull<u8> {
let layout: Layout = Layout::from_size_align(size, std::mem::align_of::<u8>()).unwrap(); let layout: Layout = Layout::from_size_align(size, std::mem::align_of::<u8>()).unwrap();
let msg = format!("wasm_node: calling allocate with {}\n", size);
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
NonNull::new_unchecked(global_alloc(layout)) NonNull::new_unchecked(global_alloc(layout))
} }
@ -29,5 +34,9 @@ pub unsafe fn allocate(size: usize) -> NonNull<u8> {
#[no_mangle] #[no_mangle]
pub unsafe fn deallocate(ptr: NonNull<u8>, size: usize) { pub unsafe fn deallocate(ptr: NonNull<u8>, size: usize) {
let layout = Layout::from_size_align(size, std::mem::align_of::<u8>()).unwrap(); let layout = Layout::from_size_align(size, std::mem::align_of::<u8>()).unwrap();
let msg = format!("wasm_node: calling deallocate with {:?} {}\n", ptr, size);
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
global_dealloc(ptr.as_ptr(), layout); global_dealloc(ptr.as_ptr(), layout);
} }

View File

@ -16,26 +16,39 @@
use std::sync::atomic::AtomicUsize; use std::sync::atomic::AtomicUsize;
use crate::log_utf8_string;
pub static mut RESULT_PTR: AtomicUsize = AtomicUsize::new(0); pub static mut RESULT_PTR: AtomicUsize = AtomicUsize::new(0);
pub static mut RESULT_SIZE: AtomicUsize = AtomicUsize::new(0); pub static mut RESULT_SIZE: AtomicUsize = AtomicUsize::new(0);
#[no_mangle] #[no_mangle]
pub unsafe fn get_result_ptr() -> usize { pub unsafe fn get_result_ptr() -> usize {
let msg = format!("wasm_node: calling get_result_ptr\n");
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
*RESULT_PTR.get_mut() *RESULT_PTR.get_mut()
} }
#[no_mangle] #[no_mangle]
pub unsafe fn get_result_size() -> usize { pub unsafe fn get_result_size() -> usize {
let msg = format!("wasm_node: calling get_result_size\n");
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
*RESULT_SIZE.get_mut() *RESULT_SIZE.get_mut()
} }
#[no_mangle] #[no_mangle]
pub unsafe fn set_result_ptr(ptr: usize) { pub unsafe fn set_result_ptr(ptr: usize) {
let msg = format!("wasm_node: calling set_result_ptr with {}\n", ptr);
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
*RESULT_PTR.get_mut() = ptr; *RESULT_PTR.get_mut() = ptr;
} }
#[no_mangle] #[no_mangle]
pub unsafe fn set_result_size(size: usize) { pub unsafe fn set_result_size(size: usize) {
let msg = format!("wasm_node: calling set_result_size with {}\n", size);
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
*RESULT_SIZE.get_mut() = size; *RESULT_SIZE.get_mut() = size;
} }

View File

@ -11,10 +11,10 @@
(@interface type (func (result i32))) ;; 3 (@interface type (func (result i32))) ;; 3
;; result setter functions ;; result setter functions
(@interface type (func (param string))) ;; 4 (@interface type (func (param string))) ;; 4
;; import ipfs put/get function ;; import ipfs put/get function
(@interface type (func (param string) (result string))) ;; 5 (@interface type (func (param i32 i32))) ;; 5
;; import ipfs put/get function ;; import ipfs put/get function
(@interface type (func (param string) (result string))) ;; 6 (@interface type (func (param string) (result string))) ;; 6
@ -24,20 +24,27 @@
(@interface export "allocate" (func 0)) ;; 0 (@interface export "allocate" (func 0)) ;; 0
(@interface export "deallocate" (func 1)) ;; 1 (@interface export "deallocate" (func 1)) ;; 1
(@interface export "get_result_size" (func 3)) ;; 3 (@interface export "get_result_size" (func 3)) ;; 2
(@interface export "get_result_ptr" (func 3)) ;; 4 (@interface export "get_result_ptr" (func 3)) ;; 3
(@interface export "set_result_size" (func 4)) ;; 5 (@interface export "set_result_size" (func 4)) ;; 4
(@interface export "set_result_ptr" (func 4)) ;; 6 (@interface export "set_result_ptr" (func 4)) ;; 5
(@interface export "put" (func 5)) ;; 8 (@interface export "put" (func 5)) ;; 6
(@interface export "get" (func 5)) ;; 7 (@interface export "get" (func 5)) ;; 7
(@interface func (type 6) (@interface func (type 6)
arg.get 0
string.size
call-core 0 ;; call allocate
arg.get 0 arg.get 0
string.lower_memory string.lower_memory
call-core 9 ;; call node.get call-core 7 ;; call node.get
call-core 5 ;; call set_result_size call-core 3 ;; call get_result_ptr
call-core 6 ;; call set_result_ptr call-core 2 ;; call get_result_size
string.lift_memory
call-core 3 ;; call get_result_ptr
call-core 2 ;; call get_result_size
call-core 1 ;; call deallocate
) )
;; Implementations ;; Implementations

View File

@ -16,12 +16,17 @@
use std::alloc::{alloc as global_alloc, dealloc as global_dealloc, Layout}; use std::alloc::{alloc as global_alloc, dealloc as global_dealloc, Layout};
use crate::log_utf8_string;
use std::ptr::NonNull; use std::ptr::NonNull;
/// Allocates memory area of specified size and returns its address. /// Allocates memory area of specified size and returns its address.
#[no_mangle] #[no_mangle]
pub unsafe fn allocate(size: usize) -> NonNull<u8> { pub unsafe fn allocate(size: usize) -> NonNull<u8> {
let layout: Layout = Layout::from_size_align(size, std::mem::align_of::<u8>()).unwrap(); let layout: Layout = Layout::from_size_align(size, std::mem::align_of::<u8>()).unwrap();
let msg = format!("wasm_rpc: calling allocate with {}\n", size);
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
NonNull::new_unchecked(global_alloc(layout)) NonNull::new_unchecked(global_alloc(layout))
} }
@ -29,5 +34,9 @@ pub unsafe fn allocate(size: usize) -> NonNull<u8> {
#[no_mangle] #[no_mangle]
pub unsafe fn deallocate(ptr: NonNull<u8>, size: usize) { pub unsafe fn deallocate(ptr: NonNull<u8>, size: usize) {
let layout = Layout::from_size_align(size, std::mem::align_of::<u8>()).unwrap(); let layout = Layout::from_size_align(size, std::mem::align_of::<u8>()).unwrap();
let msg = format!("wasm_rpc: calling deallocate with {:?} {}\n", ptr, size);
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
global_dealloc(ptr.as_ptr(), layout); global_dealloc(ptr.as_ptr(), layout);
} }

View File

@ -16,26 +16,39 @@
use std::sync::atomic::AtomicUsize; use std::sync::atomic::AtomicUsize;
use crate::log_utf8_string;
pub static mut RESULT_PTR: AtomicUsize = AtomicUsize::new(0); pub static mut RESULT_PTR: AtomicUsize = AtomicUsize::new(0);
pub static mut RESULT_SIZE: AtomicUsize = AtomicUsize::new(0); pub static mut RESULT_SIZE: AtomicUsize = AtomicUsize::new(0);
#[no_mangle] #[no_mangle]
pub unsafe fn get_result_ptr() -> usize { pub unsafe fn get_result_ptr() -> usize {
let msg = format!("wasm_rpc: calling get_result_ptr\n");
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
*RESULT_PTR.get_mut() *RESULT_PTR.get_mut()
} }
#[no_mangle] #[no_mangle]
pub unsafe fn get_result_size() -> usize { pub unsafe fn get_result_size() -> usize {
let msg = format!("wasm_rpc: calling get_result_size\n");
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
*RESULT_SIZE.get_mut() *RESULT_SIZE.get_mut()
} }
#[no_mangle] #[no_mangle]
pub unsafe fn set_result_ptr(ptr: usize) { pub unsafe fn set_result_ptr(ptr: usize) {
let msg = format!("wasm_rpc: calling set_result_ptr with {}\n", ptr);
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
*RESULT_PTR.get_mut() = ptr; *RESULT_PTR.get_mut() = ptr;
} }
#[no_mangle] #[no_mangle]
pub unsafe fn set_result_size(size: usize) { pub unsafe fn set_result_size(size: usize) {
let msg = format!("wasm_rpc: calling set_result_size with {}\n", size);
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
*RESULT_SIZE.get_mut() = size; *RESULT_SIZE.get_mut() = size;
} }

View File

@ -59,9 +59,13 @@
arg.get 1 arg.get 1
string.lift_memory string.lift_memory
call-core 7 ;; call node.get that returns string call-core 7 ;; call node.get that returns string
dup
string.size
call-core 0 ;; call allocate
swap2
string.lower_memory string.lower_memory
call-core 5 ;; call set_result_size call-core 6 ;; call set_result_size
call-core 6 ;; call set_result_ptr call-core 5 ;; call set_result_ptr
) )
;; Implementations ;; Implementations

View File

@ -68,7 +68,9 @@ impl WITFunction {
} }
pub fn from_import(wit_module: Arc<WITModule>, func_name: String) -> Result<Self, WITFCEError> { pub fn from_import(wit_module: Arc<WITModule>, func_name: String) -> Result<Self, WITFCEError> {
let (inputs, outputs) = wit_module.as_ref().get_func_signature(&func_name)?; let func_type = wit_module.as_ref().get_func_signature(&func_name)?;
let inputs = func_type.0.clone();
let outputs = func_type.1.clone();
println!("from_import: {:?}", inputs); println!("from_import: {:?}", inputs);
let inner = WITFunctionInner::Import { let inner = WITFunctionInner::Import {
@ -126,7 +128,6 @@ impl wasm::structures::LocalImport for WITFunction {
match &self.inner { match &self.inner {
WITFunctionInner::Export { func, .. } => { WITFunctionInner::Export { func, .. } => {
println!("calling with {:?}", arguments);
func.as_ref() func.as_ref()
.call(&arguments.iter().map(ival_to_wval).collect::<Vec<Value>>()) .call(&arguments.iter().map(ival_to_wval).collect::<Vec<Value>>())
.map(|results| results.iter().map(wval_to_ival).collect()) .map(|results| results.iter().map(wval_to_ival).collect())
@ -137,14 +138,13 @@ impl wasm::structures::LocalImport for WITFunction {
func_name, func_name,
.. ..
} => { } => {
println!("calling {} with {:?}", func_name, arguments);
let mut tt = wit_module.clone(); let mut tt = wit_module.clone();
unsafe { unsafe {
let result = Arc::get_mut_unchecked(&mut tt) let result = Arc::get_mut_unchecked(&mut tt)
.call(func_name, arguments) .call(func_name, arguments)
.map_err(|_| ()); .map_err(|_| ());
println!("result is {:?}", result); // println!("result is {:?}", result);
result result
} }
} }

View File

@ -89,12 +89,7 @@ impl WITInstance {
let export_func = let export_func =
std::mem::transmute::<DynFunc<'_>, DynFunc<'static>>(export_func); std::mem::transmute::<DynFunc<'_>, DynFunc<'static>>(export_func);
let tt = WITFunction::from_export(export_func)?; let tt = WITFunction::from_export(export_func)?;
println!( println!("{}, {} - {:?}", export_id, export.name, tt.inputs());
"{}, {} - {:?}",
export_id,
export.name,
tt.inputs()
);
Ok((export_id, tt)) Ok((export_id, tt))
} }
}) })

View File

@ -21,7 +21,7 @@ use crate::instance::wit_function::WITFunction;
use crate::instance::wit_instance::WITInstance; use crate::instance::wit_instance::WITInstance;
use wasmer_interface_types as wit; use wasmer_interface_types as wit;
use wasmer_interface_types::ast::Interfaces; use wasmer_interface_types::ast::{Interfaces, Type};
use wasmer_interface_types::interpreter::Interpreter; use wasmer_interface_types::interpreter::Interpreter;
use wasmer_interface_types::values::InterfaceValue; use wasmer_interface_types::values::InterfaceValue;
use wasmer_runtime::{compile, ImportObject}; use wasmer_runtime::{compile, ImportObject};
@ -43,8 +43,7 @@ type WITInterpreter =
pub struct WITModule { pub struct WITModule {
instance: WasmerInstance, instance: WasmerInstance,
wit_instance: Arc<WITInstance>, wit_instance: Arc<WITInstance>,
func_name_to_idx: HashMap<String, usize>, funcs: HashMap<String, (WITInterpreter, Vec<InterfaceType>, Vec<InterfaceType>)>,
funcs: HashMap<String, WITInterpreter>,
} }
impl WITModule { impl WITModule {
@ -71,7 +70,7 @@ impl WITModule {
let mut wit_instance = Arc::new_uninit(); let mut wit_instance = Arc::new_uninit();
let callable_exports = Self::extract_exports(&interfaces)?; let callable_exports = Self::extract_wit_exports(&interfaces)?;
let mut import_object = Self::adjust_imports(&interfaces, wit_instance.clone())?; let mut import_object = Self::adjust_imports(&interfaces, wit_instance.clone())?;
import_object.extend(imports); import_object.extend(imports);
@ -86,7 +85,6 @@ impl WITModule {
Ok(Self { Ok(Self {
instance: wasmer_instance, instance: wasmer_instance,
wit_instance, wit_instance,
func_name_to_idx: HashMap::new(),
funcs: callable_exports, funcs: callable_exports,
}) })
} }
@ -96,17 +94,14 @@ impl WITModule {
function_name: &str, function_name: &str,
args: &[InterfaceValue], args: &[InterfaceValue],
) -> Result<Vec<InterfaceValue>, WITFCEError> { ) -> Result<Vec<InterfaceValue>, WITFCEError> {
println!("here, func name is {}, args = {:?}", function_name, args);
match self.funcs.get(function_name) { match self.funcs.get(function_name) {
Some(func) => { Some(func) => {
let tt = Arc::make_mut(&mut self.wit_instance); let tt = Arc::make_mut(&mut self.wit_instance);
let result = func.run(args, tt)?.as_slice().to_owned(); let result = func.0.run(args, tt)?.as_slice().to_owned();
println!("here {:?}", result);
Ok(result) Ok(result)
} }
None => { None => {
println!("no func");
Err(WITFCEError::NoSuchFunction(format!( Err(WITFCEError::NoSuchFunction(format!(
"{} hasn't been found while calling", "{} hasn't been found while calling",
function_name function_name
@ -118,12 +113,9 @@ impl WITModule {
pub fn get_func_signature( pub fn get_func_signature(
&self, &self,
function_name: &str, function_name: &str,
) -> Result<(Vec<InterfaceType>, Vec<InterfaceType>), WITFCEError> { ) -> Result<(&Vec<InterfaceType>, &Vec<InterfaceType>), WITFCEError> {
match self.func_name_to_idx.get(function_name) { match self.funcs.get(function_name) {
Some(func_idx) => { Some((_, inputs, outputs)) => Ok((inputs, outputs)),
println!("func_idx: {}", func_idx);
self.wit_instance.as_ref().get_func_signature(*func_idx)
},
None => Err(WITFCEError::NoSuchFunction(format!( None => Err(WITFCEError::NoSuchFunction(format!(
"{} has't been found during its signature looking up", "{} has't been found during its signature looking up",
function_name function_name
@ -131,9 +123,12 @@ impl WITModule {
} }
} }
fn extract_exports( fn extract_wit_exports(
interfaces: &Interfaces, interfaces: &Interfaces,
) -> Result<HashMap<String, WITInterpreter>, WITFCEError> { ) -> Result<
HashMap<String, (WITInterpreter, Vec<InterfaceType>, Vec<InterfaceType>)>,
WITFCEError,
> {
let exports_type_to_names = interfaces let exports_type_to_names = interfaces
.exports .exports
.iter() .iter()
@ -160,12 +155,32 @@ impl WITModule {
format!("adapter function with idx = {} hasn't been found during extracting exports by implementations", i.adapter_function_type) format!("adapter function with idx = {} hasn't been found during extracting exports by implementations", i.adapter_function_type)
))?; ))?;
for export_function_name in export_function_names.iter() { if i.adapter_function_type >= interfaces.types.len() as u32 {
println!("export func name {}", export_function_name); // TODO: change error type
return Err(WITFCEError::NoSuchFunction(format!(
"{} function id is bigger than WIT interface types count",
i.adapter_function_type
)));
};
// TODO: handle errors if let Type::Function { inputs, outputs } =
let interpreter: WITInterpreter = adapter_instructions.try_into().unwrap(); &interfaces.types[i.adapter_function_type as usize]
wit_callable_exports.insert(export_function_name.to_owned(), interpreter); {
for export_function_name in export_function_names.iter() {
println!("export func name {}", export_function_name);
// TODO: handle errors
let interpreter: WITInterpreter = adapter_instructions.try_into().unwrap();
wit_callable_exports.insert(
export_function_name.to_owned(),
(interpreter, inputs.clone(), outputs.clone()),
);
}
} else {
return Err(WITFCEError::NoSuchFunction(format!(
"type with idx = {} isn't a function type",
i.adapter_function_type
)));
} }
} }

View File

@ -78,7 +78,7 @@ fn ipfs_call(ctx: &mut Ctx, ptr: i32, size: i32) {
let wasm_ptr = WasmPtr::<u8, Array>::new(ptr as _); let wasm_ptr = WasmPtr::<u8, Array>::new(ptr as _);
match wasm_ptr.get_utf8_string(ctx.memory(0), size as _) { match wasm_ptr.get_utf8_string(ctx.memory(0), size as _) {
Some(msg) => print!("ipfs_call {}", msg), Some(msg) => println!("host ipfs_call: {}", msg),
None => print!("fce logger: incorrect UTF8 string's been supplied to logger"), None => println!("fce logger: incorrect UTF8 string's been supplied to logger"),
} }
} }