2019-02-07 00:26:45 +00:00
|
|
|
use crate::{module::ModuleInfo, sys::Memory};
|
|
|
|
use memmap::Mmap;
|
|
|
|
use serde_bench::{deserialize, serialize};
|
|
|
|
use sha2::{Digest, Sha256};
|
|
|
|
use std::{
|
|
|
|
fs::File,
|
|
|
|
io::{self, Seek, SeekFrom, Write},
|
|
|
|
mem,
|
|
|
|
path::Path,
|
|
|
|
slice,
|
2019-02-21 00:41:41 +00:00
|
|
|
sync::Arc,
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
const CURRENT_CACHE_VERSION: u64 = 0;
|
|
|
|
|
|
|
|
/// The header of a cache file.
|
|
|
|
#[repr(C, packed)]
|
|
|
|
struct CacheHeader {
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CacheHeader {
|
|
|
|
pub fn read_from_slice(buffer: &[u8]) -> Result<(&CacheHeader, &[u8]), Error> {
|
|
|
|
if buffer.len() >= mem::size_of::<CacheHeader>() {
|
|
|
|
if &buffer[..8] == "WASMER\0\0".as_bytes() {
|
|
|
|
let (header_slice, body_slice) = buffer.split_at(mem::size_of::<CacheHeader>());
|
|
|
|
let header = unsafe { &*(header_slice.as_ptr() as *const CacheHeader) };
|
|
|
|
|
|
|
|
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] {
|
|
|
|
let ptr = self as *const CacheHeader as *const u8;
|
|
|
|
unsafe { slice::from_raw_parts(ptr, mem::size_of::<CacheHeader>()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct CacheInner {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Cache {
|
|
|
|
inner: CacheInner,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Cache {
|
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 {
|
|
|
|
inner: CacheInner {
|
|
|
|
info,
|
|
|
|
backend_metadata,
|
|
|
|
compiled_code,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn open<P>(path: P) -> Result<Cache, Error>
|
|
|
|
where
|
|
|
|
P: AsRef<Path>,
|
|
|
|
{
|
|
|
|
let file = File::open(path).map_err(|e| Error::IoError(e))?;
|
|
|
|
|
|
|
|
let mmap = unsafe { Mmap::map(&file).map_err(|e| Error::IoError(e))? };
|
|
|
|
|
|
|
|
let (header, body_slice) = CacheHeader::read_from_slice(&mmap[..])?;
|
|
|
|
|
|
|
|
let inner =
|
|
|
|
deserialize(body_slice).map_err(|e| Error::DeserializeError(format!("{:#?}", e)))?;
|
|
|
|
|
2019-02-21 00:41:41 +00:00
|
|
|
Ok(Cache { 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,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn store<P>(&self, path: P) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
P: AsRef<Path>,
|
|
|
|
{
|
|
|
|
let mut file = File::create(path).map_err(|e| Error::IoError(e))?;
|
|
|
|
|
|
|
|
let mut buffer = Vec::new();
|
|
|
|
|
|
|
|
serialize(&mut buffer, &self.inner).map_err(|e| Error::SerializeError(e.to_string()))?;
|
|
|
|
|
|
|
|
let data_len = buffer.len() as u64;
|
|
|
|
|
|
|
|
file.seek(SeekFrom::Start(mem::size_of::<CacheHeader>() as u64))
|
|
|
|
.map_err(|e| Error::IoError(e))?;
|
|
|
|
|
|
|
|
file.write(buffer.as_slice())
|
|
|
|
.map_err(|e| Error::IoError(e))?;
|
|
|
|
|
|
|
|
file.seek(SeekFrom::Start(0))
|
|
|
|
.map_err(|e| Error::Unknown(e.to_string()))?;
|
|
|
|
|
2019-02-19 23:36:22 +00:00
|
|
|
let wasm_hash = self.inner.info.wasm_hash.into_array();
|
2019-02-07 00:26:45 +00:00
|
|
|
|
|
|
|
let cache_header = CacheHeader {
|
|
|
|
magic: [
|
|
|
|
'W' as u8, 'A' as u8, 'S' as u8, 'M' as u8, 'E' as u8, 'R' as u8, 0, 0,
|
|
|
|
],
|
|
|
|
version: CURRENT_CACHE_VERSION,
|
|
|
|
data_len,
|
|
|
|
wasm_hash,
|
|
|
|
};
|
|
|
|
|
|
|
|
file.write(cache_header.as_slice())
|
|
|
|
.map_err(|e| Error::IoError(e))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hash_data(data: &[u8]) -> [u8; 32] {
|
|
|
|
let mut array = [0u8; 32];
|
|
|
|
array.copy_from_slice(Sha256::digest(data).as_slice());
|
|
|
|
array
|
|
|
|
}
|