diff --git a/crates/wasmtime/src/config.rs b/crates/wasmtime/src/config.rs index 0b3420e53468..4b3e3c4843c4 100644 --- a/crates/wasmtime/src/config.rs +++ b/crates/wasmtime/src/config.rs @@ -68,6 +68,16 @@ impl Default for ModuleVersionStrategy { } } +impl std::hash::Hash for ModuleVersionStrategy { + fn hash(&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. /// diff --git a/crates/wasmtime/src/engine.rs b/crates/wasmtime/src/engine.rs index 42ae0b737fbd..521aa118afbb 100644 --- a/crates/wasmtime/src/engine.rs +++ b/crates/wasmtime/src/engine.rs @@ -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; @@ -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(()) + } } diff --git a/crates/wasmtime/src/module.rs b/crates/wasmtime/src/module.rs index 369cca21d4e0..71c1237e7ad9 100644 --- a/crates/wasmtime/src/module.rs +++ b/crates/wasmtime/src/module.rs @@ -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); } }