add FCE config validation

This commit is contained in:
vms 2020-08-08 12:44:27 +03:00
parent 5f0a91d9ec
commit 6c1ec4b9dc
5 changed files with 57 additions and 7 deletions

7
Cargo.lock generated
View File

@ -130,6 +130,12 @@ dependencies = [
"digest 0.9.0",
]
[[package]]
name = "boolinator"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfa8873f51c92e232f9bac4065cddef41b714152812bfc5f7672ba16d6ef8cd9"
[[package]]
name = "byteorder"
version = "1.3.4"
@ -443,6 +449,7 @@ dependencies = [
name = "fce"
version = "0.1.0"
dependencies = [
"boolinator",
"fce-wit-interfaces",
"fce-wit-parser",
"log",

View File

@ -22,6 +22,7 @@ wasmer-wasi = { package = "wasmer-wasi-fl", version = "0.17.0" }
serde = { version = "1.0.114", default-features = false, features = [ "derive" ] }
multimap = "0.8.1"
boolinator = "2.4.0"
parity-wasm = "0.41.0"
pwasm-utils = "0.12.0"
log = "0.4.8"

View File

@ -80,6 +80,7 @@ impl FCE {
wasm_bytes: &[u8],
config: FCEModuleConfig,
) -> Result<()> {
Self::validate_config(&config)?;
let _prepared_wasm_bytes = crate::misc::prepare_module(wasm_bytes, config.mem_pages_count)?;
let module = FCEModule::new(&wasm_bytes, config, &self.modules)?;
@ -136,6 +137,43 @@ impl FCE {
})
.collect::<Vec<_>>()
}
#[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(())
}
}
impl Default for FCE {

View File

@ -55,6 +55,9 @@ pub enum FCEError {
/// Incorrect WIT section.
IncorrectWIT(String),
/// Invalid FCE config (such as duplicated envs, preopened_files or mapped_dirs).
InvalidConfig(String),
}
impl Error for FCEError {}
@ -78,6 +81,7 @@ impl std::fmt::Display for FCEError {
}
FCEError::WITParseError(err) => write!(f, "{}", err),
FCEError::IncorrectWIT(err_msg) => write!(f, "{}", err_msg),
FCEError::InvalidConfig(err_msg) => write!(f, "{}", err_msg),
}
}
}

View File

@ -86,7 +86,7 @@ pub(crate) struct FCEModule {
impl FCEModule {
pub(crate) fn new(
wasm_bytes: &[u8],
fce_module_config: FCEModuleConfig,
config: FCEModuleConfig,
modules: &HashMap<String, FCEModule>,
) -> Result<Self> {
let wasmer_module = compile(&wasm_bytes)?;
@ -97,15 +97,15 @@ impl FCEModule {
let import_object = Self::adjust_wit_imports(&fce_wit, wit_instance.clone())?;
let mut wasi_import_object = wasmer_wasi::generate_import_object_for_version(
fce_module_config.wasi_version,
config.wasi_version,
vec![],
fce_module_config.wasi_envs.clone(),
fce_module_config.wasi_preopened_files.clone(),
fce_module_config.wasi_mapped_dirs.clone(),
config.wasi_envs.clone(),
config.wasi_preopened_files.clone(),
config.wasi_mapped_dirs.clone(),
);
wasi_import_object.extend(import_object.clone());
wasi_import_object.extend(fce_module_config.imports.clone());
wasi_import_object.extend(config.imports.clone());
let wasmer_instance = wasmer_module.instantiate(&wasi_import_object)?;
let wit_instance = unsafe {
@ -127,7 +127,7 @@ impl FCEModule {
Ok(Self {
wasmer_instance: Box::new(wasmer_instance),
import_object,
host_import_object: fce_module_config.imports,
host_import_object: config.imports,
exports_funcs,
})
}