Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump module serialization version #1913

Merged
merged 4 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ and this project adheres to
[#1807]: https://github.com/CosmWasm/cosmwasm/pull/1807
[#1864]: https://github.com/CosmWasm/cosmwasm/pull/1864

### Changed

- cosmwasm-vm: Added `.module` extension to file names in the file system cache
([#1913]).

[#1913]: https://github.com/CosmWasm/cosmwasm/pull/1913

## [1.4.1] - 2023-10-09

## Fixed
Expand Down
28 changes: 18 additions & 10 deletions packages/vm/src/modules/file_system_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ use crate::modules::current_wasmer_module_version;
/// - **v7**:<br>
/// New version because of Wasmer 2.3.0 -> 4 upgrade.
/// This internally changes how rkyv is used for module serialization, making compatibility unlikely.
const MODULE_SERIALIZATION_VERSION: &str = "v7";
/// - **v8**:<br>
/// New version because of Wasmer 4.1.2 -> 4.2.2 upgrade.
/// Module compatibility between Wasmer versions is not guaranteed.
const MODULE_SERIALIZATION_VERSION: &str = "v8";

/// Representation of a directory that contains compiled Wasm artifacts.
pub struct FileSystemCache {
Expand Down Expand Up @@ -118,15 +121,22 @@ impl FileSystemCache {
self.unchecked_modules = unchecked;
}

/// Returns the path to the serialized module with the given checksum.
fn module_file(&self, checksum: &Checksum) -> PathBuf {
let mut path = self.modules_path.clone();
path.push(checksum.to_hex());
path.set_extension("module");
path
}

/// 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,
engine: &impl AsEngineRef,
) -> VmResult<Option<(Module, usize)>> {
let filename = checksum.to_hex();
let file_path = self.modules_path.join(filename);
let file_path = self.module_file(checksum);

let result = if self.unchecked_modules {
unsafe { Module::deserialize_from_file_unchecked(engine, &file_path) }
Expand Down Expand Up @@ -155,8 +165,7 @@ impl FileSystemCache {
mkdir_p(&self.modules_path)
.map_err(|_e| VmError::cache_err("Error creating modules directory"))?;

let filename = checksum.to_hex();
let path = self.modules_path.join(filename);
let path = self.module_file(checksum);
module
.serialize_to_file(&path)
.map_err(|e| VmError::cache_err(format!("Error writing module to disk: {e}")))?;
Expand All @@ -168,8 +177,7 @@ impl FileSystemCache {
///
/// Returns true if the file existed and false if the file did not exist.
pub fn remove(&mut self, checksum: &Checksum) -> VmResult<bool> {
let filename = checksum.to_hex();
let file_path = self.modules_path.join(filename);
let file_path = self.module_file(checksum);

if file_path.exists() {
fs::remove_file(file_path)
Expand Down Expand Up @@ -287,7 +295,7 @@ mod tests {
cache.store(&checksum, &module).unwrap();

let mut globber = glob::glob(&format!(
"{}/v7-wasmer5/**/{}",
"{}/v8-wasmer5/**/{}.module",
tmp_dir.path().to_string_lossy(),
checksum
))
Expand Down Expand Up @@ -365,9 +373,9 @@ mod tests {
assert_eq!(
p.as_os_str(),
if cfg!(windows) {
"modules\\v7-wasmer17\\x86_64-nintendo-fuchsia-gnu-coff-01E9F9FE"
"modules\\v8-wasmer17\\x86_64-nintendo-fuchsia-gnu-coff-01E9F9FE"
} else {
"modules/v7-wasmer17/x86_64-nintendo-fuchsia-gnu-coff-01E9F9FE"
"modules/v8-wasmer17/x86_64-nintendo-fuchsia-gnu-coff-01E9F9FE"
}
);
}
Expand Down
Loading