Skip to content

Commit

Permalink
Hash in a manual value
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Sep 13, 2024
1 parent 60cb249 commit 6c68bf7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 36 deletions.
28 changes: 1 addition & 27 deletions packages/vm-derive-impl/src/hash_function.rs
Original file line number Diff line number Diff line change
@@ -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 <https://youtu.be/Y6ljFaKRTrI?t=27>
fn format_code<C>(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");
Expand All @@ -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();

Expand Down
62 changes: 53 additions & 9 deletions packages/vm/src/modules/file_system_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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**:<br>
/// 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**:<br>
/// Version for cosmwasm_vm 1.0.0-beta5 / wasmvm 1.0.0-beta6 that ships with Wasmer 2.1.1.
/// - **v3**:<br>
/// Version for Wasmer 2.2.0 which contains a [module breaking change to 2.1.x](https://github.com/wasmerio/wasmer/pull/2747).
/// - **v4**:<br>
/// 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 (<https://github.com/wasmerio/wasmer/issues/3193>). 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**:<br>
/// 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**:<br>
/// Version for cosmwasm_vm 1.3+ which adds a sub-folder with the target identier for the modules.
/// - **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.
/// - **v8**:<br>
/// New version because of Wasmer 4.1.2 -> 4.2.2 upgrade.
/// Module compatibility between Wasmer versions is not guaranteed.
/// - **v9**:<br>
/// New version because of Wasmer 4.2.2 -> 4.2.6 upgrade.
/// Module compatibility between Wasmer versions is not guaranteed.
/// - **v10**:<br>
/// 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.
Expand All @@ -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());
}
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 6c68bf7

Please sign in to comment.