Skip to content

Commit

Permalink
Switch FileSystemCache to use checked artifact deserialization
Browse files Browse the repository at this point in the history
We recently introduced safe artifact deserialization methods, which are a
much saner default than the unsafe variants, and only have small performance
overhead.

The CLI was switched to the the new, safe deserialization,
but the cache was not updated.

This commit and switches the FileSystemCache implementation to use the
checked deserialization variants.

NOTE: The Cache::load method should also be made safe, but that is a
breaking change.
  • Loading branch information
theduke committed May 12, 2023
1 parent 47bae1b commit 8c3e817
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ blake3 = "1.0"
criterion = "0.3"
tempfile = "3.4.0"
rand = "0.8.3"
wasmer = { path = "../api", version = "=3.3.0", default-features = false, features = ["sys", "cranelift"] }
wasmer-compiler-singlepass = { path = "../compiler-singlepass", version = "=3.3.0" }

[features]
Expand All @@ -28,4 +29,4 @@ filesystem = []
blake3-pure = ["blake3/pure"]

[package.metadata.docs.rs]
features = ["wasmer/sys"]
features = ["wasmer/sys"]
24 changes: 23 additions & 1 deletion lib/cache/src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Cache for FileSystemCache {
key.to_string()
};
let path = self.path.join(filename);
let ret = Module::deserialize_from_file(engine, path.clone());
let ret = Module::deserialize_from_file_checked(engine, path.clone());
if ret.is_err() {
// If an error occurs while deserializing then we can not trust it anymore
// so delete the cache file
Expand All @@ -127,3 +127,25 @@ impl Cache for FileSystemCache {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_fs_cache() {
let dir = tempfile::tempdir().unwrap();

let mut cache = FileSystemCache::new(dir.path()).unwrap();

let engine = wasmer::Engine::default();

let bytes = include_bytes!("../../wasi/tests/envvar.wasm");

let module = Module::from_binary(&engine, bytes).unwrap();
let key = Hash::generate(bytes);

cache.store(key, &module).unwrap();
let _restored = unsafe { cache.load(&engine, key).unwrap() };
}
}

0 comments on commit 8c3e817

Please sign in to comment.