Skip to content

Commit

Permalink
Honor ModuleVersionStrategy in Engine::precompile_compatibility_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
ianks committed May 25, 2023
1 parent 83ac69c commit e7a07a1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
10 changes: 10 additions & 0 deletions crates/wasmtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ impl Default for ModuleVersionStrategy {
}
}

impl std::hash::Hash for ModuleVersionStrategy {
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
match self {
Self::WasmtimeVersion => env!("CARGO_PKG_VERSION").hash(hasher),
Self::Custom(s) => s.hash(hasher),
Self::None => {}
};
}
}

/// Global configuration options used to create an [`Engine`](crate::Engine)
/// and customize its behavior.
///
Expand Down
29 changes: 28 additions & 1 deletion crates/wasmtime/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ mod tests {
hash::{Hash, Hasher},
};

use crate::{Config, Engine, Module, OptLevel};
use crate::{Config, Engine, Module, ModuleVersionStrategy, OptLevel};

use anyhow::Result;
use tempfile::TempDir;
Expand Down Expand Up @@ -727,4 +727,31 @@ mod tests {
let opt_speed_hash = hash_for_config(&cfg);
assert_ne!(opt_none_hash, opt_speed_hash)
}

#[test]
fn precompile_compatibility_key_accounts_for_module_version_strategy() -> Result<()> {
fn hash_for_config(cfg: &Config) -> u64 {
let engine = Engine::new(cfg).expect("Config should be valid");
let mut hasher = DefaultHasher::new();
engine.precompile_compatibility_hash().hash(&mut hasher);
hasher.finish()
}
let mut cfg_custom_version = Config::new();
cfg_custom_version.module_version(ModuleVersionStrategy::Custom("1.0.1111".to_string()))?;
let custom_version_hash = hash_for_config(&cfg_custom_version);

let mut cfg_default_version = Config::new();
cfg_default_version.module_version(ModuleVersionStrategy::WasmtimeVersion)?;
let default_version_hash = hash_for_config(&cfg_default_version);

let mut cfg_none_version = Config::new();
cfg_none_version.module_version(ModuleVersionStrategy::None)?;
let none_version_hash = hash_for_config(&cfg_none_version);

assert_ne!(custom_version_hash, default_version_hash);
assert_ne!(custom_version_hash, none_version_hash);
assert_ne!(default_version_hash, none_version_hash);

Ok(())
}
}
2 changes: 1 addition & 1 deletion crates/wasmtime/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ impl std::hash::Hash for HashedEngineCompileEnv<'_> {
config.features.hash(hasher);

// Catch accidental bugs of reusing across crate versions.
env!("CARGO_PKG_VERSION").hash(hasher);
config.module_version.hash(hasher);
}
}

Expand Down

0 comments on commit e7a07a1

Please sign in to comment.