mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-08 21:58:20 +00:00
Added API to deserialize from file
This commit is contained in:
@@ -162,9 +162,9 @@ impl Module {
|
|||||||
/// Deserializes a a serialized Module binary into a `Module`.
|
/// Deserializes a a serialized Module binary into a `Module`.
|
||||||
/// > Note: the module has to be serialized before with the `serialize` method.
|
/// > 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.
|
/// 1. Are going to be deserialized directly into Rust objects.
|
||||||
/// 2. Contains the function assembly bodies and, if intercepted,
|
/// 2. Contains the function assembly bodies and, if intercepted,
|
||||||
/// a malicious actor could inject code into executable
|
/// a malicious actor could inject code into executable
|
||||||
@@ -184,6 +184,28 @@ impl Module {
|
|||||||
Ok(Self::from_compiled_module(store, compiled))
|
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 {
|
fn from_compiled_module(store: &Store, compiled: Arc<CompiledModule>) -> Self {
|
||||||
Module {
|
Module {
|
||||||
store: store.clone(),
|
store: store.clone(),
|
||||||
|
|||||||
9
lib/cache/src/filesystem.rs
vendored
9
lib/cache/src/filesystem.rs
vendored
@@ -1,6 +1,5 @@
|
|||||||
use crate::cache::Cache;
|
use crate::cache::Cache;
|
||||||
use crate::hash::WasmHash;
|
use crate::hash::WasmHash;
|
||||||
use memmap::Mmap;
|
|
||||||
use std::fs::{create_dir_all, File};
|
use std::fs::{create_dir_all, File};
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
@@ -98,13 +97,7 @@ impl Cache for FileSystemCache {
|
|||||||
let filename = key.to_string();
|
let filename = key.to_string();
|
||||||
let mut new_path_buf = self.path.clone();
|
let mut new_path_buf = self.path.clone();
|
||||||
new_path_buf.push(filename);
|
new_path_buf.push(filename);
|
||||||
|
Ok(Module::deserialize_from_file(&store, new_path_buf)?)
|
||||||
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[..])?)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn store(&mut self, key: WasmHash, module: Module) -> Result<(), Self::SerializeError> {
|
fn store(&mut self, key: WasmHash, module: Module) -> Result<(), Self::SerializeError> {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ use crate::error::InstantiationError;
|
|||||||
use crate::resolver::Resolver;
|
use crate::resolver::Resolver;
|
||||||
use crate::tunables::Tunables;
|
use crate::tunables::Tunables;
|
||||||
use crate::{CompiledModule, DeserializeError, SerializeError};
|
use crate::{CompiledModule, DeserializeError, SerializeError};
|
||||||
|
use std::path::Path;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use wasm_common::FunctionType;
|
use wasm_common::FunctionType;
|
||||||
use wasmer_compiler::CompileError;
|
use wasmer_compiler::CompileError;
|
||||||
@@ -50,4 +51,15 @@ pub trait Engine {
|
|||||||
|
|
||||||
/// Deserializes a WebAssembly module
|
/// Deserializes a WebAssembly module
|
||||||
fn deserialize(&self, bytes: &[u8]) -> Result<Arc<CompiledModule>, DeserializeError>;
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user