2020-04-18 18:27:01 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2020 Fluence Labs Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-04-28 05:00:20 +03:00
|
|
|
use crate::vm::module::frank_result::FrankResult;
|
2020-04-29 20:42:28 +03:00
|
|
|
use crate::vm::module::{FrankModule, ModuleAPI};
|
2020-04-28 14:27:08 +03:00
|
|
|
use crate::vm::{config::Config, errors::FrankError, service::FrankService};
|
2020-04-28 05:00:20 +03:00
|
|
|
|
|
|
|
use sha2::{digest::generic_array::GenericArray, digest::FixedOutput};
|
|
|
|
use std::collections::hash_map::Entry;
|
|
|
|
use std::collections::HashMap;
|
2020-04-28 14:27:08 +03:00
|
|
|
use wasmer_runtime::{func, Ctx};
|
2020-04-29 20:42:28 +03:00
|
|
|
use wasmer_runtime_core::import::{ImportObject, Namespace};
|
2020-04-28 05:00:20 +03:00
|
|
|
|
2020-04-18 18:27:01 +03:00
|
|
|
pub struct Frank {
|
2020-04-29 20:42:28 +03:00
|
|
|
// set of modules registered inside Frank
|
2020-04-28 05:00:20 +03:00
|
|
|
modules: HashMap<String, FrankModule>,
|
2020-04-29 20:42:28 +03:00
|
|
|
|
|
|
|
// contains ABI of each registered module in specific format for Wasmer
|
|
|
|
abi_import_object: ImportObject,
|
2020-04-18 18:27:01 +03:00
|
|
|
}
|
|
|
|
|
2020-04-28 05:00:20 +03:00
|
|
|
impl Frank {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
modules: HashMap::new(),
|
2020-04-29 20:42:28 +03:00
|
|
|
abi_import_object: ImportObject::new(),
|
2020-04-28 14:27:08 +03:00
|
|
|
}
|
|
|
|
}
|
2020-04-29 20:42:28 +03:00
|
|
|
|
|
|
|
/// Adds ABI of a module with provided module name to the abi_import_object.
|
|
|
|
fn create_import_object(module: &FrankModule, config: &Config) -> Namespace {
|
|
|
|
let mut namespace = Namespace::new();
|
|
|
|
|
|
|
|
// TODO: introduce a macro for such things
|
|
|
|
let allocate = module.abi.allocate.clone();
|
|
|
|
namespace.insert(
|
|
|
|
config.allocate_fn_name.clone(),
|
|
|
|
func!(move |_ctx: &mut Ctx, size: i32| -> i32 {
|
|
|
|
allocate
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.call(size)
|
|
|
|
.expect("allocate failed")
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
let invoke = module.abi.invoke.clone();
|
|
|
|
namespace.insert(
|
|
|
|
config.invoke_fn_name.clone(),
|
|
|
|
func!(move |_ctx: &mut Ctx, offset: i32, size: i32| -> i32 {
|
|
|
|
invoke
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.call(offset, size)
|
|
|
|
.expect("invoke failed")
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
let deallocate = module.abi.deallocate.clone();
|
|
|
|
namespace.insert(
|
|
|
|
config.deallocate_fn_name.clone(),
|
|
|
|
func!(move |_ctx: &mut Ctx, ptr: i32, size: i32| {
|
|
|
|
deallocate
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.call(ptr, size)
|
|
|
|
.expect("deallocate failed");
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
let store = module.abi.store.clone();
|
|
|
|
namespace.insert(
|
|
|
|
config.store_fn_name.clone(),
|
|
|
|
func!(move |_ctx: &mut Ctx, offset: i32, value: i32| {
|
|
|
|
store
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.call(offset, value)
|
|
|
|
.expect("store failed")
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
let load = module.abi.load.clone();
|
|
|
|
namespace.insert(
|
|
|
|
config.load_fn_name.clone(),
|
|
|
|
func!(move |_ctx: &mut Ctx, offset: i32| -> i32 {
|
|
|
|
load.as_ref().unwrap().call(offset).expect("load failed")
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
namespace
|
|
|
|
}
|
2020-04-18 18:27:01 +03:00
|
|
|
}
|
|
|
|
|
2020-04-28 05:00:20 +03:00
|
|
|
impl FrankService for Frank {
|
|
|
|
fn invoke(&mut self, module_name: String, argument: &[u8]) -> Result<FrankResult, FrankError> {
|
|
|
|
match self.modules.entry(module_name) {
|
|
|
|
Entry::Vacant(_) => Err(FrankError::NoSuchModule),
|
|
|
|
Entry::Occupied(mut module) => module.get_mut().invoke(argument),
|
2020-04-18 18:27:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 05:00:20 +03:00
|
|
|
fn register_module(
|
|
|
|
&mut self,
|
|
|
|
module_name: String,
|
|
|
|
wasm_bytes: &[u8],
|
|
|
|
config: Config,
|
|
|
|
) -> Result<(), FrankError> {
|
|
|
|
let prepared_wasm_bytes =
|
|
|
|
crate::vm::prepare::prepare_module(wasm_bytes, config.mem_pages_count)?;
|
|
|
|
|
2020-04-29 20:42:28 +03:00
|
|
|
let module = FrankModule::new(
|
|
|
|
&prepared_wasm_bytes,
|
|
|
|
config.clone(),
|
|
|
|
self.abi_import_object.clone_ref(),
|
|
|
|
)?;
|
2020-04-18 18:27:01 +03:00
|
|
|
|
2020-04-29 20:42:28 +03:00
|
|
|
// registers ABI of newly registered module in abi_import_object
|
|
|
|
let namespace = Frank::create_import_object(&module, &config);
|
|
|
|
self.abi_import_object.register(module_name.clone(), namespace);
|
2020-04-18 18:27:01 +03:00
|
|
|
|
2020-04-29 20:42:28 +03:00
|
|
|
match self.modules.entry(module_name) {
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
entry.insert(module);
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
Entry::Occupied(_) => Err(FrankError::NonUniqueModuleName)
|
|
|
|
}
|
2020-04-28 05:00:20 +03:00
|
|
|
}
|
2020-04-18 18:27:01 +03:00
|
|
|
|
2020-04-28 05:00:20 +03:00
|
|
|
fn unregister_module(&mut self, module_name: &str) -> Result<(), FrankError> {
|
|
|
|
self.modules
|
|
|
|
.remove(module_name)
|
|
|
|
.ok_or_else(|| FrankError::NoSuchModule)?;
|
|
|
|
// unregister abi from a dispatcher
|
2020-04-18 18:27:01 +03:00
|
|
|
|
2020-04-28 05:00:20 +03:00
|
|
|
Ok(())
|
2020-04-18 18:27:01 +03:00
|
|
|
}
|
|
|
|
|
2020-04-28 05:00:20 +03:00
|
|
|
fn compute_state_hash(
|
|
|
|
&mut self,
|
|
|
|
) -> GenericArray<u8, <sha2::Sha256 as FixedOutput>::OutputSize> {
|
|
|
|
use sha2::Digest;
|
2020-04-18 18:27:01 +03:00
|
|
|
|
2020-04-28 05:00:20 +03:00
|
|
|
let mut hasher = sha2::Sha256::new();
|
2020-04-18 18:27:01 +03:00
|
|
|
|
2020-04-28 05:00:20 +03:00
|
|
|
let sha256_size = 256;
|
|
|
|
let mut hash_vec: Vec<u8> = Vec::with_capacity(self.modules.len() * sha256_size);
|
|
|
|
for (_, module) in self.modules.iter_mut() {
|
|
|
|
hash_vec.extend_from_slice(module.compute_state_hash().as_slice());
|
|
|
|
}
|
2020-04-18 18:27:01 +03:00
|
|
|
|
2020-04-28 05:00:20 +03:00
|
|
|
hasher.input(hash_vec);
|
|
|
|
hasher.result()
|
2020-04-18 18:27:01 +03:00
|
|
|
}
|
|
|
|
}
|