diff --git a/lib/runtime-core/Cargo.toml b/lib/runtime-core/Cargo.toml index c6ac22f4059..5ef5c38c961 100644 --- a/lib/runtime-core/Cargo.toml +++ b/lib/runtime-core/Cargo.toml @@ -46,3 +46,5 @@ field-offset = "0.1.1" [features] debug = [] +[build-dependencies] +blake2b_simd = "0.4.1" \ No newline at end of file diff --git a/lib/runtime-core/build.rs b/lib/runtime-core/build.rs new file mode 100644 index 00000000000..e91238bc6bd --- /dev/null +++ b/lib/runtime-core/build.rs @@ -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"); +} diff --git a/lib/runtime-core/src/cache.rs b/lib/runtime-core/src/cache.rs index 1cb80465573..93e14b0f9b3 100644 --- a/lib/runtime-core/src/cache.rs +++ b/lib/runtime-core/src/cache.rs @@ -207,3 +207,7 @@ pub trait Cache { fn load(&self, key: WasmHash) -> Result; 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")); diff --git a/lib/runtime/src/cache.rs b/lib/runtime/src/cache.rs index 6ebbf10171c..4b2768bc6ea 100644 --- a/lib/runtime/src/cache.rs +++ b/lib/runtime/src/cache.rs @@ -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>(path: P) -> io::Result { - 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()?; diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index e36e99cb613..1af94188749 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -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());