From 6c68bf700bf027ceda11eb070a124e30892fc769 Mon Sep 17 00:00:00 2001 From: Aumetra Weisman Date: Fri, 13 Sep 2024 14:40:08 +0200 Subject: [PATCH] Hash in a manual value --- packages/vm-derive-impl/src/hash_function.rs | 28 +-------- packages/vm/src/modules/file_system_cache.rs | 62 +++++++++++++++++--- 2 files changed, 54 insertions(+), 36 deletions(-) diff --git a/packages/vm-derive-impl/src/hash_function.rs b/packages/vm-derive-impl/src/hash_function.rs index 0fba1c40a..abc5a2c35 100644 --- a/packages/vm-derive-impl/src/hash_function.rs +++ b/packages/vm-derive-impl/src/hash_function.rs @@ -1,34 +1,8 @@ -use std::{ - io::Write, - process::{Command, Stdio}, -}; - use proc_macro2::TokenStream; use quote::quote; use super::{bail, maybe}; -// i do what i must because i can -fn format_code(code: C) -> String -where - C: AsRef<[u8]>, -{ - let mut child = Command::new("rustfmt") - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn() - .unwrap(); - - { - let mut stdin = child.stdin.take().unwrap(); - stdin.write_all(code.as_ref()).unwrap(); - } - - let output = child.wait_with_output().unwrap(); - assert!(output.status.success()); - String::from_utf8(output.stdout).unwrap() -} - pub fn hash_function_impl(attr: TokenStream, input: TokenStream) -> TokenStream { if !attr.is_empty() { bail!(attr, "Unexpected parameters"); @@ -37,7 +11,7 @@ pub fn hash_function_impl(attr: TokenStream, input: TokenStream) -> TokenStream // Just verify that this is actually a function let _: syn::ItemFn = maybe!(syn::parse2(input.clone())); - let display = format_code(input.to_string()); + let display = input.to_string(); let hex_hash = blake3::hash(display.as_bytes()).to_hex(); let hex_hash = hex_hash.as_str(); diff --git a/packages/vm/src/modules/file_system_cache.rs b/packages/vm/src/modules/file_system_cache.rs index 58a8f0175..ece7d73c6 100644 --- a/packages/vm/src/modules/file_system_cache.rs +++ b/packages/vm/src/modules/file_system_cache.rs @@ -20,6 +20,51 @@ use crate::Size; use super::cached_module::engine_size_estimate; use super::CachedModule; +/// This is a value you can manually modify to the cache. +/// You normally _do not_ need to change this value yourself. +/// +/// Cases where you might need to update it yourself, is things like when the memory layout of some types in Rust [std] changes. +/// +/// --- +/// +/// Now follows the legacy documentation of this value: +/// +/// ## Version history: +/// - **v1**:
+/// cosmwasm_vm < 1.0.0-beta5. This is working well up to Wasmer 2.0.0 as +/// [in wasmvm 1.0.0-beta2](https://github.com/CosmWasm/wasmvm/blob/v1.0.0-beta2/libwasmvm/Cargo.lock#L1412-L1413) +/// and [wasmvm 0.16.3](https://github.com/CosmWasm/wasmvm/blob/v0.16.3/libwasmvm/Cargo.lock#L1408-L1409). +/// Versions that ship with Wasmer 2.1.x such [as wasmvm 1.0.0-beta3](https://github.com/CosmWasm/wasmvm/blob/v1.0.0-beta3/libwasmvm/Cargo.lock#L1534-L1535) +/// to [wasmvm 1.0.0-beta5](https://github.com/CosmWasm/wasmvm/blob/v1.0.0-beta5/libwasmvm/Cargo.lock#L1530-L1531) +/// are broken, i.e. they will crash when reading older v1 modules. +/// - **v2**:
+/// Version for cosmwasm_vm 1.0.0-beta5 / wasmvm 1.0.0-beta6 that ships with Wasmer 2.1.1. +/// - **v3**:
+/// Version for Wasmer 2.2.0 which contains a [module breaking change to 2.1.x](https://github.com/wasmerio/wasmer/pull/2747). +/// - **v4**:
+/// Version for Wasmer 2.3.0 which contains a module breaking change to 2.2.0 that was not reflected in +/// the module header version (). In cosmwasm-vm 1.1.0-1.1.1 +/// the old value "v3" is still used along with Wasmer 2.3.0 (bug). From cosmwasm 1.1.2 onwards, this is +/// fixed by bumping to "v4". +/// - **v5**:
+/// A change in memory layout of some types in Rust [std] caused +/// [issues with module deserialization](https://github.com/CosmWasm/wasmvm/issues/426). +/// To work around this, the version was bumped to "v5" here to invalidate these corrupt caches. +/// - **v6**:
+/// Version for cosmwasm_vm 1.3+ which adds a sub-folder with the target identier for the modules. +/// - **v7**:
+/// New version because of Wasmer 2.3.0 -> 4 upgrade. +/// This internally changes how rkyv is used for module serialization, making compatibility unlikely. +/// - **v8**:
+/// New version because of Wasmer 4.1.2 -> 4.2.2 upgrade. +/// Module compatibility between Wasmer versions is not guaranteed. +/// - **v9**:
+/// New version because of Wasmer 4.2.2 -> 4.2.6 upgrade. +/// Module compatibility between Wasmer versions is not guaranteed. +/// - **v10**:
+/// New version because of Metering middleware change. +const MODULE_SERIALIZATION_VERSION: &str = "v10"; + /// Function that actually does the heavy lifting of creating the module version discriminator. /// /// Separated for sanity tests because otherwise the `OnceLock` would cache the result. @@ -29,7 +74,10 @@ fn raw_module_version_discriminator() -> String { let hashes = cosmwasm_vm_derive::collect_hashes(); let mut hasher = blake3::Hasher::new(); + + hasher.update(MODULE_SERIALIZATION_VERSION.as_bytes()); hasher.update(wasmer_version.as_bytes()); + for hash in hashes { hasher.update(hash.as_bytes()); } @@ -410,15 +458,11 @@ mod tests { fn module_version_discriminator_stays_the_same() { let v1 = raw_module_version_discriminator(); let v2 = raw_module_version_discriminator(); - assert_eq!(v1, v2); - } + let v3 = raw_module_version_discriminator(); + let v4 = raw_module_version_discriminator(); - #[test] - fn module_version_discriminator_is_fixed() { - let discriminator = raw_module_version_discriminator(); - assert_eq!( - discriminator, - "ddae2ae211962fc3481ff11cd46413750c692ddeb3d65f3e7a8a6d31ab1f8511" - ); + assert_eq!(v1, v2); + assert_eq!(v2, v3); + assert_eq!(v3, v4); } }