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-05-03 19:29:33 +03:00
|
|
|
use wasmer_runtime::func;
|
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
|
|
|
|
2020-04-29 22:15:17 +03:00
|
|
|
/// Extracts ABI of a module into Namespace.
|
2020-04-29 20:42:28 +03:00
|
|
|
fn create_import_object(module: &FrankModule, config: &Config) -> Namespace {
|
|
|
|
let mut namespace = Namespace::new();
|
2020-04-30 00:52:37 +03:00
|
|
|
let module_abi = module.get_abi();
|
2020-04-29 20:42:28 +03:00
|
|
|
|
|
|
|
// TODO: introduce a macro for such things
|
2020-04-30 00:52:37 +03:00
|
|
|
let allocate = module_abi.allocate.clone().unwrap();
|
2020-04-29 20:42:28 +03:00
|
|
|
namespace.insert(
|
|
|
|
config.allocate_fn_name.clone(),
|
2020-05-03 19:29:33 +03:00
|
|
|
func!(move |size: i32| -> i32 { allocate.call(size).expect("allocate failed") }),
|
2020-04-29 20:42:28 +03:00
|
|
|
);
|
|
|
|
|
2020-04-30 00:52:37 +03:00
|
|
|
let invoke = module_abi.invoke.clone().unwrap();
|
2020-04-29 20:42:28 +03:00
|
|
|
namespace.insert(
|
|
|
|
config.invoke_fn_name.clone(),
|
2020-05-03 19:29:33 +03:00
|
|
|
func!(move |offset: i32, size: i32| -> i32 {
|
2020-04-30 00:52:37 +03:00
|
|
|
invoke.call(offset, size).expect("invoke failed")
|
2020-04-29 20:42:28 +03:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2020-04-30 00:52:37 +03:00
|
|
|
let deallocate = module_abi.deallocate.clone().unwrap();
|
2020-04-29 20:42:28 +03:00
|
|
|
namespace.insert(
|
|
|
|
config.deallocate_fn_name.clone(),
|
2020-05-03 19:29:33 +03:00
|
|
|
func!(move |ptr: i32, size: i32| {
|
2020-04-30 00:52:37 +03:00
|
|
|
deallocate.call(ptr, size).expect("deallocate failed");
|
2020-04-29 20:42:28 +03:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2020-04-30 00:52:37 +03:00
|
|
|
let store = module_abi.store.clone().unwrap();
|
2020-04-29 20:42:28 +03:00
|
|
|
namespace.insert(
|
|
|
|
config.store_fn_name.clone(),
|
2020-05-03 19:29:33 +03:00
|
|
|
func!(move |offset: i32, value: i32| {
|
2020-04-30 00:52:37 +03:00
|
|
|
store.call(offset, value).expect("store failed")
|
2020-04-29 20:42:28 +03:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2020-04-30 00:52:37 +03:00
|
|
|
let load = module_abi.load.clone().unwrap();
|
2020-04-29 20:42:28 +03:00
|
|
|
namespace.insert(
|
|
|
|
config.load_fn_name.clone(),
|
2020-05-03 19:29:33 +03:00
|
|
|
func!(move |offset: i32| -> i32 { load.call(offset).expect("load failed") }),
|
2020-04-29 20:42:28 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
namespace
|
|
|
|
}
|
2020-04-18 18:27:01 +03:00
|
|
|
}
|
|
|
|
|
2020-04-29 22:15:17 +03:00
|
|
|
impl Default for Frank {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 05:00:20 +03:00
|
|
|
impl FrankService for Frank {
|
2020-04-29 22:15:17 +03:00
|
|
|
fn invoke(&mut self, module_name: &str, argument: &[u8]) -> Result<FrankResult, FrankError> {
|
|
|
|
match self.modules.get_mut(module_name) {
|
|
|
|
Some(module) => module.invoke(argument),
|
2020-04-29 23:20:22 +03:00
|
|
|
None => Err(FrankError::NoSuchModule),
|
2020-04-18 18:27:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 22:15:17 +03:00
|
|
|
fn register_module<S>(
|
2020-04-28 05:00:20 +03:00
|
|
|
&mut self,
|
2020-04-29 22:15:17 +03:00
|
|
|
module_name: S,
|
2020-04-28 05:00:20 +03:00
|
|
|
wasm_bytes: &[u8],
|
|
|
|
config: Config,
|
2020-04-29 22:15:17 +03:00
|
|
|
) -> Result<(), FrankError>
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
2020-04-28 05:00:20 +03:00
|
|
|
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(),
|
2020-05-02 12:31:31 +03:00
|
|
|
self.abi_import_object.clone(),
|
2020-04-29 20:42:28 +03:00
|
|
|
)?;
|
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);
|
2020-04-29 22:15:17 +03:00
|
|
|
let module_name: String = module_name.into();
|
|
|
|
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(())
|
2020-04-29 22:15:17 +03:00
|
|
|
}
|
|
|
|
Entry::Occupied(_) => Err(FrankError::NonUniqueModuleName),
|
2020-04-29 20:42:28 +03:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|