Skip to content

Commit

Permalink
Merge branch 'tomas/wasm-cache-handle-err' (#2215)
Browse files Browse the repository at this point in the history
* tomas/wasm-cache-handle-err:
  changelog: add #2215
  wasm: handle cache loading errors
  • Loading branch information
tzemanovic committed Dec 7, 2023
2 parents 76023b7 + 85957df commit 710b169
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/2215-vp-less-permissive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Handle errors on loading WASMs from file-system compilation cache.
([\#2215](https://github.com/anoma/namada/pull/2215))
74 changes: 51 additions & 23 deletions shared/src/vm/wasm/compilation_cache/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,22 @@ impl<N: CacheName, A: WasmCacheAccess> Cache<N, A> {
return Ok(Some((module.clone(), store())));
}

let (module, store) = file_load_module(&self.dir, hash);
tracing::info!(
"{} found {} in file cache.",
N::name(),
hash.to_string()
);
// Put into cache, ignore result if it's full
let _ = in_memory.put_with_weight(*hash, module.clone());
if let Ok((module, store)) =
file_load_module(&self.dir, hash)
{
tracing::info!(
"{} found {} in file cache.",
N::name(),
hash.to_string()
);
// Put into cache, ignore result if it's full
let _ =
in_memory.put_with_weight(*hash, module.clone());

return Ok(Some((module, store)));
return Ok(Some((module, store)));
} else {
return Ok(None);
}
}
Some(Compilation::Compiling) => {
drop(progress);
Expand All @@ -183,11 +189,15 @@ impl<N: CacheName, A: WasmCacheAccess> Cache<N, A> {
let (module, store) = if module_file_exists(&self.dir, hash)
{
tracing::info!(
"Loading {} {} from file.",
"Trying to load {} {} from file.",
N::name(),
hash.to_string()
);
file_load_module(&self.dir, hash)
if let Ok(res) = file_load_module(&self.dir, hash) {
res
} else {
return Ok(None);
}
} else {
return Ok(None);
};
Expand Down Expand Up @@ -246,13 +256,18 @@ impl<N: CacheName, A: WasmCacheAccess> Cache<N, A> {
return Ok(Some((module.clone(), store())));
}

let (module, store) = file_load_module(&self.dir, hash);
tracing::info!(
"{} found {} in file cache.",
N::name(),
hash.to_string()
);
return Ok(Some((module, store)));
if let Ok((module, store)) =
file_load_module(&self.dir, hash)
{
tracing::info!(
"{} found {} in file cache.",
N::name(),
hash.to_string()
);
return Ok(Some((module, store)));
} else {
return Ok(None);
}
}
Some(Compilation::Compiling) => {
drop(progress);
Expand All @@ -270,11 +285,15 @@ impl<N: CacheName, A: WasmCacheAccess> Cache<N, A> {

return if module_file_exists(&self.dir, hash) {
tracing::info!(
"Loading {} {} from file.",
"Trying to load {} {} from file.",
N::name(),
hash.to_string()
);
Ok(Some(file_load_module(&self.dir, hash)))
if let Ok(res) = file_load_module(&self.dir, hash) {
return Ok(Some(res));
} else {
return Ok(None);
}
} else {
Ok(None)
};
Expand Down Expand Up @@ -478,13 +497,22 @@ fn file_write_module(dir: impl AsRef<Path>, module: &Module, hash: &Hash) {
fs_cache.store(CacheHash::new(hash.0), module).unwrap();
}

fn file_load_module(dir: impl AsRef<Path>, hash: &Hash) -> (Module, Store) {
fn file_load_module(
dir: impl AsRef<Path>,
hash: &Hash,
) -> Result<(Module, Store), wasmer::DeserializeError> {
use wasmer_cache::Cache;
let fs_cache = fs_cache(dir, hash);
let store = store();
let hash = CacheHash::new(hash.0);
let module = unsafe { fs_cache.load(&store, hash) }.unwrap();
(module, store)
let module = unsafe { fs_cache.load(&store, hash) };
if let Err(err) = module.as_ref() {
tracing::error!(
"Error loading cached wasm {}: {err}.",
hash.to_string()
);
}
Ok((module?, store))
}

fn fs_cache(dir: impl AsRef<Path>, hash: &Hash) -> FileSystemCache {
Expand Down

0 comments on commit 710b169

Please sign in to comment.