2019-02-21 19:47:28 +00:00
|
|
|
use crate::{
|
|
|
|
module::{Module, ModuleInfo},
|
|
|
|
sys::Memory,
|
|
|
|
};
|
2019-02-07 00:26:45 +00:00
|
|
|
use sha2::{Digest, Sha256};
|
2019-02-22 01:06:49 +00:00
|
|
|
use std::{io, mem, slice};
|
2019-02-07 00:26:45 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum InvalidFileType {
|
|
|
|
InvalidSize,
|
|
|
|
InvalidMagic,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
IoError(io::Error),
|
|
|
|
DeserializeError(String),
|
|
|
|
SerializeError(String),
|
|
|
|
Unknown(String),
|
|
|
|
InvalidFile(InvalidFileType),
|
|
|
|
InvalidatedCache,
|
|
|
|
}
|
|
|
|
|
2019-02-21 22:00:33 +00:00
|
|
|
impl From<io::Error> for Error {
|
|
|
|
fn from(io_err: io::Error) -> Self {
|
|
|
|
Error::IoError(io_err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The hash of a wasm module.
|
2019-02-21 23:27:20 +00:00
|
|
|
///
|
2019-02-21 22:00:33 +00:00
|
|
|
/// Used as a key when loading and storing modules in a [`Cache`].
|
2019-02-21 23:27:20 +00:00
|
|
|
///
|
2019-02-21 22:00:33 +00:00
|
|
|
/// [`Cache`]: trait.Cache.html
|
2019-02-21 19:47:28 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
|
|
|
pub struct WasmHash([u8; 32]);
|
|
|
|
|
|
|
|
impl WasmHash {
|
2019-02-21 22:00:33 +00:00
|
|
|
/// Hash a wasm module.
|
2019-02-21 23:27:20 +00:00
|
|
|
///
|
|
|
|
/// # Note:
|
2019-02-21 22:00:33 +00:00
|
|
|
/// This does no verification that the supplied data
|
|
|
|
/// is, in fact, a wasm module.
|
2019-02-21 19:47:28 +00:00
|
|
|
pub fn generate(wasm: &[u8]) -> Self {
|
|
|
|
let mut array = [0u8; 32];
|
|
|
|
array.copy_from_slice(Sha256::digest(wasm).as_slice());
|
|
|
|
WasmHash(array)
|
|
|
|
}
|
|
|
|
|
2019-02-21 22:00:33 +00:00
|
|
|
/// Create the hexadecimal representation of the
|
|
|
|
/// stored hash.
|
2019-02-21 19:47:28 +00:00
|
|
|
pub fn encode(self) -> String {
|
|
|
|
hex::encode(self.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn into_array(self) -> [u8; 32] {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:26:45 +00:00
|
|
|
const CURRENT_CACHE_VERSION: u64 = 0;
|
2019-02-22 01:06:49 +00:00
|
|
|
static WASMER_CACHE_MAGIC: [u8; 8] = *b"WASMER\0\0";
|
2019-02-07 00:26:45 +00:00
|
|
|
|
|
|
|
/// The header of a cache file.
|
|
|
|
#[repr(C, packed)]
|
2019-02-22 01:06:49 +00:00
|
|
|
struct ArtifactHeader {
|
2019-02-07 00:26:45 +00:00
|
|
|
magic: [u8; 8], // [W, A, S, M, E, R, \0, \0]
|
|
|
|
version: u64,
|
|
|
|
data_len: u64,
|
|
|
|
wasm_hash: [u8; 32], // Sha256 of the wasm in binary format.
|
|
|
|
}
|
|
|
|
|
2019-02-22 01:06:49 +00:00
|
|
|
impl ArtifactHeader {
|
2019-02-21 19:47:28 +00:00
|
|
|
pub fn read_from_slice(buffer: &[u8]) -> Result<(&Self, &[u8]), Error> {
|
2019-02-22 01:06:49 +00:00
|
|
|
if buffer.len() >= mem::size_of::<ArtifactHeader>() {
|
|
|
|
if &buffer[..8] == &WASMER_CACHE_MAGIC {
|
|
|
|
let (header_slice, body_slice) = buffer.split_at(mem::size_of::<ArtifactHeader>());
|
|
|
|
let header = unsafe { &*(header_slice.as_ptr() as *const ArtifactHeader) };
|
|
|
|
|
|
|
|
if header.version == CURRENT_CACHE_VERSION {
|
|
|
|
Ok((header, body_slice))
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidatedCache)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidFile(InvalidFileType::InvalidMagic))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidFile(InvalidFileType::InvalidSize))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_from_slice_mut(buffer: &mut [u8]) -> Result<(&mut Self, &mut [u8]), Error> {
|
|
|
|
if buffer.len() >= mem::size_of::<ArtifactHeader>() {
|
|
|
|
if &buffer[..8] == &WASMER_CACHE_MAGIC {
|
2019-02-21 19:47:28 +00:00
|
|
|
let (header_slice, body_slice) =
|
2019-02-22 01:06:49 +00:00
|
|
|
buffer.split_at_mut(mem::size_of::<ArtifactHeader>());
|
|
|
|
let header = unsafe { &mut *(header_slice.as_ptr() as *mut ArtifactHeader) };
|
2019-02-07 00:26:45 +00:00
|
|
|
|
|
|
|
if header.version == CURRENT_CACHE_VERSION {
|
|
|
|
Ok((header, body_slice))
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidatedCache)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidFile(InvalidFileType::InvalidMagic))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidFile(InvalidFileType::InvalidSize))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_slice(&self) -> &[u8] {
|
2019-02-22 01:06:49 +00:00
|
|
|
let ptr = self as *const ArtifactHeader as *const u8;
|
|
|
|
unsafe { slice::from_raw_parts(ptr, mem::size_of::<ArtifactHeader>()) }
|
2019-02-07 00:26:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
2019-02-22 01:06:49 +00:00
|
|
|
struct ArtifactInner {
|
2019-02-07 00:26:45 +00:00
|
|
|
info: Box<ModuleInfo>,
|
|
|
|
#[serde(with = "serde_bytes")]
|
2019-02-19 23:36:22 +00:00
|
|
|
backend_metadata: Box<[u8]>,
|
2019-02-21 00:41:41 +00:00
|
|
|
compiled_code: Memory,
|
2019-02-07 00:26:45 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 01:06:49 +00:00
|
|
|
pub struct Artifact {
|
|
|
|
inner: ArtifactInner,
|
2019-02-07 00:26:45 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 01:06:49 +00:00
|
|
|
impl Artifact {
|
2019-02-19 23:36:22 +00:00
|
|
|
pub(crate) fn from_parts(
|
2019-02-07 00:26:45 +00:00
|
|
|
info: Box<ModuleInfo>,
|
2019-02-19 23:36:22 +00:00
|
|
|
backend_metadata: Box<[u8]>,
|
2019-02-21 00:41:41 +00:00
|
|
|
compiled_code: Memory,
|
2019-02-07 00:26:45 +00:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2019-02-22 01:06:49 +00:00
|
|
|
inner: ArtifactInner {
|
2019-02-07 00:26:45 +00:00
|
|
|
info,
|
|
|
|
backend_metadata,
|
|
|
|
compiled_code,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 01:06:49 +00:00
|
|
|
pub fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
|
|
|
let (_, body_slice) = ArtifactHeader::read_from_slice(bytes)?;
|
2019-02-07 00:26:45 +00:00
|
|
|
|
2019-02-22 01:06:49 +00:00
|
|
|
let inner = serde_bench::deserialize(body_slice)
|
|
|
|
.map_err(|e| Error::DeserializeError(format!("{:#?}", e)))?;
|
2019-02-07 00:26:45 +00:00
|
|
|
|
2019-02-22 01:06:49 +00:00
|
|
|
Ok(Artifact { inner })
|
2019-02-07 00:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn info(&self) -> &ModuleInfo {
|
|
|
|
&self.inner.info
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2019-02-21 00:41:41 +00:00
|
|
|
pub fn consume(self) -> (ModuleInfo, Box<[u8]>, Memory) {
|
2019-02-07 00:26:45 +00:00
|
|
|
(
|
|
|
|
*self.inner.info,
|
|
|
|
self.inner.backend_metadata,
|
|
|
|
self.inner.compiled_code,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-02-22 01:06:49 +00:00
|
|
|
pub fn serialize(&self) -> Result<Vec<u8>, Error> {
|
|
|
|
let cache_header = ArtifactHeader {
|
|
|
|
magic: WASMER_CACHE_MAGIC,
|
|
|
|
version: CURRENT_CACHE_VERSION,
|
|
|
|
data_len: 0,
|
|
|
|
wasm_hash: self.inner.info.wasm_hash.into_array(),
|
|
|
|
};
|
2019-02-07 00:26:45 +00:00
|
|
|
|
2019-02-22 01:06:49 +00:00
|
|
|
let mut buffer = cache_header.as_slice().to_vec();
|
2019-02-07 00:26:45 +00:00
|
|
|
|
2019-02-22 01:06:49 +00:00
|
|
|
serde_bench::serialize(&mut buffer, &self.inner)
|
|
|
|
.map_err(|e| Error::SerializeError(e.to_string()))?;
|
2019-02-07 00:26:45 +00:00
|
|
|
|
2019-02-22 01:06:49 +00:00
|
|
|
let data_len = (buffer.len() - mem::size_of::<ArtifactHeader>()) as u64;
|
2019-02-07 00:26:45 +00:00
|
|
|
|
2019-02-22 01:06:49 +00:00
|
|
|
let (header, _) = ArtifactHeader::read_from_slice_mut(&mut buffer)?;
|
|
|
|
header.data_len = data_len;
|
2019-02-07 00:26:45 +00:00
|
|
|
|
2019-02-22 01:06:49 +00:00
|
|
|
Ok(buffer)
|
2019-02-07 00:26:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 22:00:33 +00:00
|
|
|
/// A generic cache for storing and loading compiled wasm modules.
|
2019-02-21 23:27:20 +00:00
|
|
|
///
|
2019-02-21 22:00:33 +00:00
|
|
|
/// The `wasmer-runtime` supplies a naive `FileSystemCache` api.
|
2019-02-21 19:47:28 +00:00
|
|
|
pub trait Cache {
|
|
|
|
type LoadError;
|
|
|
|
type StoreError;
|
|
|
|
|
2019-02-21 22:00:33 +00:00
|
|
|
fn load(&self, key: WasmHash) -> Result<Module, Self::LoadError>;
|
2019-02-21 19:47:28 +00:00
|
|
|
fn store(&mut self, module: Module) -> Result<WasmHash, Self::StoreError>;
|
2019-02-07 00:26:45 +00:00
|
|
|
}
|