Skip to content

Commit

Permalink
Merge pull request #1663 from CosmWasm/migrate-away-from-loupe
Browse files Browse the repository at this point in the history
Migrate away from loupe
  • Loading branch information
webmaster128 authored Apr 20, 2023
2 parents bb9c38f + cdfaf8d commit e40146a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ and this project adheres to
- cosmwasm-vm: Limit number of imports during static validation ([#1629]).
- cosmwasm-vm: The `check_contract` example was removed. Please use the crate
[cosmwasm-check](https://crates.io/crates/cosmwasm-check) instead ([#1511]).
- cosmwasm-vm: Avoid using loupe for getting the `Module` size in the file
system cache to prepare for the Wasmer 3 upgrade.

[#1511]: https://github.com/CosmWasm/cosmwasm/issues/1511
[#1629]: https://github.com/CosmWasm/cosmwasm/pull/1629
Expand Down
14 changes: 6 additions & 8 deletions packages/vm/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,8 @@ where

// Try to get module from file system cache
let store = make_runtime_store(Some(cache.instance_memory_limit));
if let Some(module) = cache.fs_cache.load(checksum, &store)? {
if let Some((module, module_size)) = cache.fs_cache.load(checksum, &store)? {
cache.stats.hits_fs_cache = cache.stats.hits_fs_cache.saturating_add(1);
let module_size = loupe::size_of_val(&module);
return cache
.pinned_memory_cache
.store(checksum, module, module_size);
Expand All @@ -274,8 +273,7 @@ where
let code = self.load_wasm_with_path(&cache.wasm_path, checksum)?;
let module = compile(&code, Some(cache.instance_memory_limit), &[])?;
// Store into the fs cache too
cache.fs_cache.store(checksum, &module)?;
let module_size = loupe::size_of_val(&module);
let module_size = cache.fs_cache.store(checksum, &module)?;
cache
.pinned_memory_cache
.store(checksum, module, module_size)
Expand Down Expand Up @@ -334,9 +332,9 @@ where

// Get module from file system cache
let store = make_runtime_store(Some(cache.instance_memory_limit));
if let Some(module) = cache.fs_cache.load(checksum, &store)? {
if let Some((module, module_size)) = cache.fs_cache.load(checksum, &store)? {
cache.stats.hits_fs_cache = cache.stats.hits_fs_cache.saturating_add(1);
let module_size = loupe::size_of_val(&module);

cache
.memory_cache
.store(checksum, module.clone(), module_size)?;
Expand All @@ -351,8 +349,8 @@ where
let wasm = self.load_wasm_with_path(&cache.wasm_path, checksum)?;
cache.stats.misses = cache.stats.misses.saturating_add(1);
let module = compile(&wasm, Some(cache.instance_memory_limit), &[])?;
cache.fs_cache.store(checksum, &module)?;
let module_size = loupe::size_of_val(&module);
let module_size = cache.fs_cache.store(checksum, &module)?;

cache
.memory_cache
.store(checksum, module.clone(), module_size)?;
Expand Down
31 changes: 24 additions & 7 deletions packages/vm/src/modules/file_system_cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fs;
use std::io;
use std::path::Path;
use std::path::PathBuf;
use thiserror::Error;

Expand Down Expand Up @@ -103,13 +104,16 @@ impl FileSystemCache {

/// Loads a serialized module from the file system and returns a module (i.e. artifact + store),
/// along with the size of the serialized module.
pub fn load(&self, checksum: &Checksum, store: &Store) -> VmResult<Option<Module>> {
pub fn load(&self, checksum: &Checksum, store: &Store) -> VmResult<Option<(Module, usize)>> {
let filename = checksum.to_hex();
let file_path = self.latest_modules_path().join(filename);

let result = unsafe { Module::deserialize_from_file(store, file_path) };
let result = unsafe { Module::deserialize_from_file(store, &file_path) };
match result {
Ok(module) => Ok(Some(module)),
Ok(module) => {
let module_size = module_size(&file_path)?;
Ok(Some((module, module_size)))
}
Err(DeserializeError::Io(err)) => match err.kind() {
io::ErrorKind::NotFound => Ok(None),
_ => Err(VmError::cache_err(format!(
Expand All @@ -125,17 +129,18 @@ impl FileSystemCache {
}

/// Stores a serialized module to the file system. Returns the size of the serialized module.
pub fn store(&mut self, checksum: &Checksum, module: &Module) -> VmResult<()> {
pub fn store(&mut self, checksum: &Checksum, module: &Module) -> VmResult<usize> {
let modules_dir = self.latest_modules_path();
mkdir_p(&modules_dir)
.map_err(|_e| VmError::cache_err("Error creating modules directory"))?;

let filename = checksum.to_hex();
let path = modules_dir.join(filename);
module
.serialize_to_file(path)
.serialize_to_file(&path)
.map_err(|e| VmError::cache_err(format!("Error writing module to disk: {}", e)))?;
Ok(())
let module_size = module_size(&path)?;
Ok(module_size)
}

/// Removes a serialized module from the file system.
Expand Down Expand Up @@ -164,6 +169,17 @@ impl FileSystemCache {
}
}

/// Returns the size of the module stored on disk
fn module_size(module_path: &Path) -> VmResult<usize> {
let module_size: usize = module_path
.metadata()
.map_err(|_e| VmError::cache_err("Error getting file metadata"))? // ensure error message is not system specific
.len()
.try_into()
.expect("Could not convert file size to usize");
Ok(module_size)
}

#[cfg(test)]
mod tests {
use std::fs;
Expand Down Expand Up @@ -212,7 +228,8 @@ mod tests {
// Check the returned module is functional.
// This is not really testing the cache API but better safe than sorry.
{
let cached_module = cached.unwrap();
let (cached_module, module_size) = cached.unwrap();
assert_eq!(module_size, module.serialize().unwrap().len());
let import_object = imports! {};
let instance = WasmerInstance::new(&cached_module, &import_object).unwrap();
set_remaining_points(&instance, TESTING_GAS_LIMIT);
Expand Down

0 comments on commit e40146a

Please sign in to comment.