generate the hash at compile time

This commit is contained in:
Mark McCaskey 2019-03-19 10:58:58 -07:00
parent 874b613f56
commit 8e5f250ed0
6 changed files with 38 additions and 27 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::{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");
}

View File

@ -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");

View File

@ -0,0 +1 @@
beb6542931f7f7c823b8d1804b12639fd794711af1f90263c5ed7d338bf4429d1cbe69a531f18144f208c33c49ca01315ffa2b23c170e4c979b1114ffcfbba54

View File

@ -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<P: Into<PathBuf>>(path: P) -> io::Result<Self> {
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<Module, 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 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()?;

View File

@ -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
}