Added API to deserialize from file

This commit is contained in:
Syrus
2020-05-07 22:15:27 -07:00
parent 63d7df78fa
commit 6fe3d362b2
3 changed files with 37 additions and 10 deletions

View File

@@ -162,9 +162,9 @@ impl Module {
/// Deserializes a a serialized Module binary into a `Module`.
/// > Note: the module has to be serialized before with the `serialize` method.
///
/// # Unsafety
/// # Safety
///
/// This function is inherently `unsafe` as the provided bytes:
/// This function is inherently **unsafe** as the provided bytes:
/// 1. Are going to be deserialized directly into Rust objects.
/// 2. Contains the function assembly bodies and, if intercepted,
/// a malicious actor could inject code into executable
@@ -184,6 +184,28 @@ impl Module {
Ok(Self::from_compiled_module(store, compiled))
}
/// Deserializes a a serialized Module located in a `Path` into a `Module`.
/// > Note: the module has to be serialized before with the `serialize` method.
///
/// # Safety
///
/// Please check [`Module::deserialize`].
///
/// # Usage
///
/// ```ignore
/// # use wasmer::*;
/// # let store = Store::default();
/// let module = Module::deserialize_from_file(&store, path)?;
/// ```
pub unsafe fn deserialize_from_file(
store: &Store,
path: impl AsRef<Path>,
) -> Result<Self, DeserializeError> {
let compiled = store.engine().deserialize_from_file(path.as_ref())?;
Ok(Self::from_compiled_module(store, compiled))
}
fn from_compiled_module(store: &Store, compiled: Arc<CompiledModule>) -> Self {
Module {
store: store.clone(),

View File

@@ -1,6 +1,5 @@
use crate::cache::Cache;
use crate::hash::WasmHash;
use memmap::Mmap;
use std::fs::{create_dir_all, File};
use std::io::{self, Write};
use std::path::PathBuf;
@@ -98,13 +97,7 @@ impl Cache for FileSystemCache {
let filename = key.to_string();
let mut new_path_buf = self.path.clone();
new_path_buf.push(filename);
let file = File::open(new_path_buf)?;
// Note: this is unsafe, but as the `load` function is unsafe
// we don't need to wrap it again with the `unsafe { .. }`
let mmap = Mmap::map(&file)?;
Ok(Module::deserialize(&store, &mmap[..])?)
Ok(Module::deserialize_from_file(&store, new_path_buf)?)
}
fn store(&mut self, key: WasmHash, module: Module) -> Result<(), Self::SerializeError> {

View File

@@ -4,6 +4,7 @@ use crate::error::InstantiationError;
use crate::resolver::Resolver;
use crate::tunables::Tunables;
use crate::{CompiledModule, DeserializeError, SerializeError};
use std::path::Path;
use std::sync::Arc;
use wasm_common::FunctionType;
use wasmer_compiler::CompileError;
@@ -50,4 +51,15 @@ pub trait Engine {
/// Deserializes a WebAssembly module
fn deserialize(&self, bytes: &[u8]) -> Result<Arc<CompiledModule>, DeserializeError>;
/// Deserializes a WebAssembly module from a path
fn deserialize_from_file(
&self,
file_ref: &Path,
) -> Result<Arc<CompiledModule>, DeserializeError> {
// TODO: Return an IoDeserializeError, so we don't need to map the error
let bytes =
std::fs::read(file_ref).map_err(|e| DeserializeError::Generic(format!("{}", e)))?;
self.deserialize(&bytes)
}
}