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-06-06 02:31:59 +03:00
|
|
|
use super::module::FCEModule;
|
2020-06-03 23:19:07 +03:00
|
|
|
use super::*;
|
2020-04-28 05:00:20 +03:00
|
|
|
|
|
|
|
use std::collections::hash_map::Entry;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2020-06-15 17:22:18 +03:00
|
|
|
/// Represent a function type inside FCE.
|
2020-09-16 01:14:15 +03:00
|
|
|
#[derive(Debug)]
|
2020-06-30 19:23:43 +03:00
|
|
|
pub struct FCEFunctionSignature<'a> {
|
2020-06-15 17:22:18 +03:00
|
|
|
pub name: &'a str,
|
2020-09-16 01:14:15 +03:00
|
|
|
pub arguments: &'a Vec<IFunctionArg>,
|
2020-06-30 19:23:43 +03:00
|
|
|
pub output_types: &'a Vec<IType>,
|
2020-06-15 17:22:18 +03:00
|
|
|
}
|
|
|
|
|
2020-06-30 18:17:38 +03:00
|
|
|
/// The base struct of the Fluence Compute Engine.
|
|
|
|
pub struct FCE {
|
|
|
|
// set of modules registered inside FCE
|
|
|
|
modules: HashMap<String, FCEModule>,
|
|
|
|
}
|
|
|
|
|
2020-05-08 20:38:29 +03:00
|
|
|
impl FCE {
|
2020-04-28 05:00:20 +03:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
modules: HashMap::new(),
|
2020-04-28 14:27:08 +03:00
|
|
|
}
|
|
|
|
}
|
2020-04-18 18:27:01 +03:00
|
|
|
|
2020-06-15 17:22:18 +03:00
|
|
|
/// Invoke a function of a module inside FCE by given function name with given arguments.
|
2020-06-30 18:17:38 +03:00
|
|
|
pub fn call<MN: AsRef<str>, FN: AsRef<str>>(
|
2020-06-03 23:19:07 +03:00
|
|
|
&mut self,
|
2020-06-30 18:17:38 +03:00
|
|
|
module_name: MN,
|
|
|
|
func_name: FN,
|
2020-06-03 23:19:07 +03:00
|
|
|
argument: &[IValue],
|
2020-07-17 23:07:02 +03:00
|
|
|
) -> Result<Vec<IValue>> {
|
|
|
|
self.call_(module_name.as_ref(), func_name.as_ref(), argument)
|
|
|
|
}
|
|
|
|
|
2020-08-09 23:02:41 +03:00
|
|
|
fn call_(
|
2020-07-17 23:07:02 +03:00
|
|
|
&mut self,
|
|
|
|
module_name: &str,
|
|
|
|
func_name: &str,
|
|
|
|
argument: &[IValue],
|
|
|
|
) -> Result<Vec<IValue>> {
|
|
|
|
match self.modules.get_mut(module_name) {
|
2020-06-03 23:19:07 +03:00
|
|
|
// TODO: refactor errors
|
2020-06-30 18:17:38 +03:00
|
|
|
Some(module) => module.call(func_name.as_ref(), argument),
|
2020-07-17 23:07:02 +03:00
|
|
|
None => Err(FCEError::NoSuchModule(module_name.to_string())),
|
2020-04-18 18:27:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:22:18 +03:00
|
|
|
/// Load a new module inside FCE.
|
2020-07-17 23:07:02 +03:00
|
|
|
pub fn load_module<S: Into<String>>(
|
2020-04-28 05:00:20 +03:00
|
|
|
&mut self,
|
2020-08-07 23:24:28 +03:00
|
|
|
name: S,
|
2020-04-28 05:00:20 +03:00
|
|
|
wasm_bytes: &[u8],
|
2020-06-03 23:19:07 +03:00
|
|
|
config: FCEModuleConfig,
|
2020-07-17 23:07:02 +03:00
|
|
|
) -> Result<()> {
|
2020-08-07 23:24:28 +03:00
|
|
|
self.load_module_(name.into(), wasm_bytes, config)
|
2020-07-17 23:07:02 +03:00
|
|
|
}
|
|
|
|
|
2020-08-09 23:02:41 +03:00
|
|
|
fn load_module_(
|
2020-07-17 23:07:02 +03:00
|
|
|
&mut self,
|
2020-08-07 23:24:28 +03:00
|
|
|
name: String,
|
2020-07-17 23:07:02 +03:00
|
|
|
wasm_bytes: &[u8],
|
|
|
|
config: FCEModuleConfig,
|
|
|
|
) -> Result<()> {
|
2020-08-08 12:44:27 +03:00
|
|
|
Self::validate_config(&config)?;
|
2020-06-15 17:22:18 +03:00
|
|
|
let _prepared_wasm_bytes = crate::misc::prepare_module(wasm_bytes, config.mem_pages_count)?;
|
2020-04-18 18:27:01 +03:00
|
|
|
|
2020-06-12 01:24:07 +03:00
|
|
|
let module = FCEModule::new(&wasm_bytes, config, &self.modules)?;
|
2020-04-18 18:27:01 +03:00
|
|
|
|
2020-08-07 23:24:28 +03:00
|
|
|
match self.modules.entry(name) {
|
2020-04-29 20:42:28 +03:00
|
|
|
Entry::Vacant(entry) => {
|
2020-06-07 22:57:30 +03:00
|
|
|
entry.insert(module);
|
2020-04-29 20:42:28 +03:00
|
|
|
Ok(())
|
2020-04-29 22:15:17 +03:00
|
|
|
}
|
2020-05-08 20:38:29 +03:00
|
|
|
Entry::Occupied(_) => Err(FCEError::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-06-15 17:22:18 +03:00
|
|
|
/// Unload previously loaded module.
|
2020-08-07 23:24:28 +03:00
|
|
|
pub fn unload_module<S: AsRef<str>>(&mut self, name: S) -> Result<()> {
|
|
|
|
self.unload_module_(name.as_ref())
|
2020-07-17 23:07:02 +03:00
|
|
|
}
|
|
|
|
|
2020-08-09 23:02:41 +03:00
|
|
|
fn unload_module_(&mut self, module_name: &str) -> Result<()> {
|
2020-07-17 23:07:02 +03:00
|
|
|
match self.modules.remove(module_name) {
|
2020-06-30 18:17:38 +03:00
|
|
|
Some(_) => Ok(()),
|
2020-07-17 23:07:02 +03:00
|
|
|
None => Err(FCEError::NoSuchModule(module_name.to_string())),
|
2020-05-09 00:39:42 +03:00
|
|
|
}
|
2020-04-18 18:27:01 +03:00
|
|
|
}
|
2020-06-05 23:12:02 +03:00
|
|
|
|
2020-08-08 23:07:21 +03:00
|
|
|
pub fn module_wasi_state<S: AsRef<str>>(
|
|
|
|
&mut self,
|
|
|
|
module_name: S,
|
|
|
|
) -> Result<&wasmer_wasi::state::WasiState> {
|
|
|
|
self.module_wasi_state_(module_name.as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn module_wasi_state_(&mut self, module_name: &str) -> Result<&wasmer_wasi::state::WasiState> {
|
|
|
|
match self.modules.get_mut(module_name) {
|
|
|
|
Some(module) => Ok(module.get_wasi_state()),
|
|
|
|
None => Err(FCEError::NoSuchModule(module_name.to_string())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 18:17:38 +03:00
|
|
|
/// Return function signatures of all loaded info FCE modules with their names.
|
2020-06-30 19:23:43 +03:00
|
|
|
pub fn interface(&self) -> impl Iterator<Item = (&str, Vec<FCEFunctionSignature<'_>>)> {
|
2020-06-30 18:17:38 +03:00
|
|
|
self.modules.iter().map(|(module_name, module)| {
|
|
|
|
(
|
|
|
|
module_name.as_str(),
|
2020-09-16 01:14:15 +03:00
|
|
|
Self::get_module_function_signatures(module).collect::<Vec<_>>(),
|
2020-06-30 18:17:38 +03:00
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return function signatures exported by module with given name.
|
|
|
|
pub fn module_interface<S: AsRef<str>>(
|
|
|
|
&self,
|
|
|
|
module_name: S,
|
2020-09-16 01:14:15 +03:00
|
|
|
) -> Result<impl Iterator<Item = FCEFunctionSignature<'_>>> {
|
2020-06-30 18:17:38 +03:00
|
|
|
match self.modules.get(module_name.as_ref()) {
|
|
|
|
Some(module) => Ok(Self::get_module_function_signatures(module)),
|
2020-07-11 23:04:55 +03:00
|
|
|
None => Err(FCEError::NoSuchModule(module_name.as_ref().to_string())),
|
2020-06-05 23:12:02 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-30 18:17:38 +03:00
|
|
|
|
2020-09-16 01:14:15 +03:00
|
|
|
/// Return record types of export functions of all loaded info FCE modules.
|
|
|
|
pub fn record_types(&self) -> impl Iterator<Item = &(u64, IRecordType)> {
|
|
|
|
self.modules
|
|
|
|
.iter()
|
|
|
|
.flat_map(|(_, module)| module.get_export_record_types())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return record types exported by module with given name.
|
|
|
|
pub fn module_record_types<S: AsRef<str>>(
|
|
|
|
&self,
|
|
|
|
module_name: S,
|
|
|
|
) -> Result<impl Iterator<Item = &(u64, IRecordType)>> {
|
|
|
|
match self.modules.get(module_name.as_ref()) {
|
|
|
|
Some(module) => Ok(module.get_export_record_types()),
|
|
|
|
None => Err(FCEError::NoSuchModule(module_name.as_ref().to_string())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_module_function_signatures(
|
|
|
|
module: &FCEModule,
|
|
|
|
) -> impl Iterator<Item = FCEFunctionSignature<'_>> {
|
2020-06-30 18:17:38 +03:00
|
|
|
module
|
|
|
|
.get_exports_signatures()
|
2020-09-16 01:14:15 +03:00
|
|
|
.map(|(name, arguments, output_types)| FCEFunctionSignature {
|
2020-06-30 18:17:38 +03:00
|
|
|
name,
|
2020-09-16 01:14:15 +03:00
|
|
|
arguments,
|
2020-06-30 19:23:43 +03:00
|
|
|
output_types,
|
2020-06-30 18:17:38 +03:00
|
|
|
})
|
|
|
|
}
|
2020-08-08 12:44:27 +03:00
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
fn validate_config(config: &FCEModuleConfig) -> Result<()> {
|
|
|
|
use boolinator::Boolinator;
|
|
|
|
|
|
|
|
fn has_only_unique_elements<T>(mut iter: T) -> bool
|
|
|
|
where
|
|
|
|
T: Iterator,
|
|
|
|
T::Item: Eq + std::hash::Hash,
|
|
|
|
{
|
|
|
|
let mut uniq = std::collections::HashSet::new();
|
|
|
|
iter.all(move |x| uniq.insert(x))
|
|
|
|
}
|
|
|
|
|
|
|
|
has_only_unique_elements(config.wasi_envs.iter()).as_result(
|
|
|
|
(),
|
|
|
|
FCEError::InvalidConfig(String::from(
|
|
|
|
"Supplied config contains environment variable with the same names",
|
|
|
|
)),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
has_only_unique_elements(config.wasi_mapped_dirs.iter().map(|(alias, _)| alias)).as_result(
|
|
|
|
(),
|
|
|
|
FCEError::InvalidConfig(String::from(
|
|
|
|
"Supplied config contains mapped dirs with the same aliases",
|
|
|
|
)),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
has_only_unique_elements(config.wasi_preopened_files.iter()).as_result(
|
|
|
|
(),
|
|
|
|
FCEError::InvalidConfig(String::from(
|
|
|
|
"Supplied config contains preopened files with the same file names",
|
|
|
|
)),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-18 18:27:01 +03:00
|
|
|
}
|
2020-06-15 17:22:18 +03:00
|
|
|
|
|
|
|
impl Default for FCE {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|