278: Add versioning to cache r=lachlansneff a=MarkMcCaskey

resolves #272 

Co-authored-by: Mark <mark@marks-macbook-pro.local>
Co-authored-by: Mark McCaskey <mark@wasmer.io>
Co-authored-by: Lachlan Sneff <lachlan.sneff@gmail.com>
This commit is contained in:
bors[bot] 2019-03-19 23:25:11 +00:00
commit 4a74382105
5 changed files with 47 additions and 5 deletions

View File

@ -46,3 +46,5 @@ field-offset = "0.1.1"
[features]
debug = []
[build-dependencies]
blake2b_simd = "0.4.1"

26
lib/runtime-core/build.rs Normal file
View File

@ -0,0 +1,26 @@
use blake2b_simd::blake2bp;
use std::{env, fs, io::Write, path::PathBuf};
const WASMER_VERSION: &'static str = env!("CARGO_PKG_VERSION");
fn main() {
let mut state = blake2bp::State::new();
state.update(WASMER_VERSION.as_bytes());
let hasher = state.finalize();
let hash_string = hasher.to_hex().as_str().to_owned();
let crate_dir = env::var("OUT_DIR").unwrap();
let wasmer_version_hash_file = {
let mut path = PathBuf::from(&crate_dir);
path.push("wasmer_version_hash.txt");
path
};
let mut f_out = fs::File::create(wasmer_version_hash_file)
.expect("Could not create file for wasmer hash value");
f_out
.write_all(hash_string.as_bytes())
.expect("Could not write to file for wasmer hash value");
}

View File

@ -207,3 +207,7 @@ pub trait Cache {
fn load(&self, key: WasmHash) -> Result<Module, Self::LoadError>;
fn store(&mut self, key: WasmHash, module: Module) -> Result<(), Self::StoreError>;
}
/// A unique ID generated from the version of Wasmer for use with cache versioning
pub const WASMER_VERSION_HASH: &'static str =
include_str!(concat!(env!("OUT_DIR"), "/wasmer_version_hash.txt"));

View File

@ -7,7 +7,7 @@ use std::{
};
use wasmer_runtime_core::cache::Error as CacheError;
pub use wasmer_runtime_core::cache::{Artifact, Cache, WasmHash};
pub use wasmer_runtime_core::cache::{Artifact, Cache, WasmHash, WASMER_VERSION_HASH};
/// Representation of a directory that contains compiled wasm artifacts.
///
@ -40,12 +40,17 @@ pub struct FileSystemCache {
impl FileSystemCache {
/// Construct a new `FileSystemCache` around the specified directory.
/// The contents of the cache are stored in sub-versioned directories.
///
/// # Note:
/// This method is unsafe because there's no way to ensure the artifacts
/// stored in this cache haven't been corrupted or tampered with.
pub unsafe fn new<P: Into<PathBuf>>(path: P) -> io::Result<Self> {
let path: PathBuf = path.into();
let path: PathBuf = {
let mut path = path.into();
path.push(WASMER_VERSION_HASH);
path
};
if path.exists() {
let metadata = path.metadata()?;

View File

@ -12,7 +12,7 @@ use structopt::StructOpt;
use wasmer::webassembly::InstanceABI;
use wasmer::*;
use wasmer_emscripten;
use wasmer_runtime::cache::{Cache as BaseCache, FileSystemCache, WasmHash};
use wasmer_runtime::cache::{Cache as BaseCache, FileSystemCache, WasmHash, WASMER_VERSION_HASH};
#[derive(Debug, StructOpt)]
#[structopt(name = "wasmer", about = "Wasm execution runtime.")]
@ -48,9 +48,11 @@ struct Run {
#[derive(Debug, StructOpt)]
enum Cache {
/// Clear the cache
#[structopt(name = "clean")]
Clean,
/// Display the location of the cache
#[structopt(name = "dir")]
Dir,
}
@ -72,6 +74,7 @@ fn get_cache_dir() -> PathBuf {
// We use a temporal directory for saving cache files
let mut temp_dir = env::temp_dir();
temp_dir.push("wasmer");
temp_dir.push(WASMER_VERSION_HASH);
temp_dir
}
}
@ -194,8 +197,10 @@ fn main() {
Cache::Clean => {
use std::fs;
let cache_dir = get_cache_dir();
fs::remove_dir_all(cache_dir.clone()).expect("Can't remove cache dir");
fs::create_dir(cache_dir.clone()).expect("Can't create cache dir");
if cache_dir.exists() {
fs::remove_dir_all(cache_dir.clone()).expect("Can't remove cache dir");
}
fs::create_dir_all(cache_dir.clone()).expect("Can't create cache dir");
}
Cache::Dir => {
println!("{}", get_cache_dir().to_string_lossy());