diff --git a/lib/runtime-core/Cargo.toml b/lib/runtime-core/Cargo.toml index c6ac22f40..5ef5c38c9 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 000000000..015c4785f --- /dev/null +++ b/lib/runtime-core/build.rs @@ -0,0 +1,26 @@ +use blake2b_simd::blake2bp; +use std::{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!("CARGO_MANIFEST_DIR"); + 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 13eb00398..7c29b0237 100644 --- a/lib/runtime-core/src/cache.rs +++ b/lib/runtime-core/src/cache.rs @@ -208,13 +208,5 @@ pub trait Cache { fn store(&mut self, key: WasmHash, module: Module) -> Result<(), Self::StoreError>; } -const WASMER_VERSION: &'static str = env!("CARGO_PKG_VERSION"); - -/// Returns a unique ID generated from the version of Wasmer for use with cache versioning -pub fn cache_versioned_sub_directory() -> String { - let mut state = blake2bp::State::new(); - state.update(WASMER_VERSION.as_bytes()); - - let hasher = state.finalize(); - hasher.to_hex().as_str().to_owned() -} +/// A unique ID generated from the version of Wasmer for use with cache versioning +pub const WASMER_VERSION_HASH: &'static str = include_str!("../wasmer_version_hash.txt"); diff --git a/lib/runtime-core/wasmer_version_hash.txt b/lib/runtime-core/wasmer_version_hash.txt new file mode 100644 index 000000000..4694153e0 --- /dev/null +++ b/lib/runtime-core/wasmer_version_hash.txt @@ -0,0 +1 @@ +beb6542931f7f7c823b8d1804b12639fd794711af1f90263c5ed7d338bf4429d1cbe69a531f18144f208c33c49ca01315ffa2b23c170e4c979b1114ffcfbba54 \ No newline at end of file diff --git a/lib/runtime/src/cache.rs b/lib/runtime/src/cache.rs index 3eab262ea..af22f9696 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::{cache_versioned_sub_directory, Artifact, Cache, WasmHash}; +pub use wasmer_runtime_core::cache::{Artifact, Cache, WasmHash, WASMER_VERSION_HASH}; /// Representation of a directory that contains compiled wasm artifacts. /// @@ -36,7 +36,6 @@ pub use wasmer_runtime_core::cache::{cache_versioned_sub_directory, Artifact, Ca /// ``` pub struct FileSystemCache { path: PathBuf, - versioned_sub_directory: String, } impl FileSystemCache { @@ -48,16 +47,12 @@ impl FileSystemCache { /// 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 versioned_sub_directory = cache_versioned_sub_directory(); if path.exists() { let metadata = path.metadata()?; if metadata.is_dir() { if !metadata.permissions().readonly() { - Ok(Self { - path, - versioned_sub_directory, - }) + Ok(Self { path }) } else { // This directory is readonly. Err(io::Error::new( @@ -78,10 +73,7 @@ impl FileSystemCache { } else { // Create the directory and any parent directories if they don't yet exist. create_dir_all(&path)?; - Ok(Self { - path, - versioned_sub_directory, - }) + Ok(Self { path }) } } } @@ -93,7 +85,7 @@ impl Cache for FileSystemCache { fn load(&self, key: WasmHash) -> Result { let filename = key.encode(); let mut new_path_buf = self.path.clone(); - new_path_buf.push(&self.versioned_sub_directory); + new_path_buf.push(WASMER_VERSION_HASH); new_path_buf.push(filename); let file = File::open(new_path_buf)?; let mmap = unsafe { Mmap::map(&file)? }; @@ -105,7 +97,7 @@ impl Cache for FileSystemCache { fn store(&mut self, key: WasmHash, module: Module) -> Result<(), CacheError> { let filename = key.encode(); let mut new_path_buf = self.path.clone(); - new_path_buf.push(&self.versioned_sub_directory); + new_path_buf.push(WASMER_VERSION_HASH); new_path_buf.push(filename); let serialized_cache = module.cache()?; diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index d70b68df8..bb9f22c28 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -12,9 +12,7 @@ use structopt::StructOpt; use wasmer::webassembly::InstanceABI; use wasmer::*; use wasmer_emscripten; -use wasmer_runtime::cache::{ - cache_versioned_sub_directory, 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.")] @@ -73,7 +71,7 @@ fn get_cache_dir() -> PathBuf { Err(_) => { // We use a temporal directory for saving cache files let mut temp_dir = env::temp_dir(); - temp_dir.push(cache_versioned_sub_directory()); + temp_dir.push(WASMER_VERSION_HASH); temp_dir.push("wasmer"); temp_dir }