Skip to content

Commit 0cfa86a

Browse files
committed
Hash in a manual value
1 parent 60cb249 commit 0cfa86a

File tree

2 files changed

+54
-36
lines changed

2 files changed

+54
-36
lines changed

packages/vm-derive-impl/src/hash_function.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,8 @@
1-
use std::{
2-
io::Write,
3-
process::{Command, Stdio},
4-
};
5-
61
use proc_macro2::TokenStream;
72
use quote::quote;
83

94
use super::{bail, maybe};
105

11-
// i do what i must because i can <https://youtu.be/Y6ljFaKRTrI?t=27>
12-
fn format_code<C>(code: C) -> String
13-
where
14-
C: AsRef<[u8]>,
15-
{
16-
let mut child = Command::new("rustfmt")
17-
.stdin(Stdio::piped())
18-
.stdout(Stdio::piped())
19-
.spawn()
20-
.unwrap();
21-
22-
{
23-
let mut stdin = child.stdin.take().unwrap();
24-
stdin.write_all(code.as_ref()).unwrap();
25-
}
26-
27-
let output = child.wait_with_output().unwrap();
28-
assert!(output.status.success());
29-
String::from_utf8(output.stdout).unwrap()
30-
}
31-
326
pub fn hash_function_impl(attr: TokenStream, input: TokenStream) -> TokenStream {
337
if !attr.is_empty() {
348
bail!(attr, "Unexpected parameters");
@@ -37,7 +11,7 @@ pub fn hash_function_impl(attr: TokenStream, input: TokenStream) -> TokenStream
3711
// Just verify that this is actually a function
3812
let _: syn::ItemFn = maybe!(syn::parse2(input.clone()));
3913

40-
let display = format_code(input.to_string());
14+
let display = input.to_string();
4115
let hex_hash = blake3::hash(display.as_bytes()).to_hex();
4216
let hex_hash = hex_hash.as_str();
4317

packages/vm/src/modules/file_system_cache.rs

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,51 @@ use crate::Size;
2020
use super::cached_module::engine_size_estimate;
2121
use super::CachedModule;
2222

23+
/// This is a value you can manually modify to the cache.
24+
/// You normally _do not_ need to change this value yourself.
25+
///
26+
/// Cases where you might need to update it yourself, is things like when the memory layout of some types in Rust [std] changes.
27+
///
28+
/// ---
29+
///
30+
/// Now follows the legacy documentation of this value:
31+
///
32+
/// ## Version history:
33+
/// - **v1**:<br>
34+
/// cosmwasm_vm < 1.0.0-beta5. This is working well up to Wasmer 2.0.0 as
35+
/// [in wasmvm 1.0.0-beta2](https://github.com/CosmWasm/wasmvm/blob/v1.0.0-beta2/libwasmvm/Cargo.lock#L1412-L1413)
36+
/// and [wasmvm 0.16.3](https://github.com/CosmWasm/wasmvm/blob/v0.16.3/libwasmvm/Cargo.lock#L1408-L1409).
37+
/// 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)
38+
/// to [wasmvm 1.0.0-beta5](https://github.com/CosmWasm/wasmvm/blob/v1.0.0-beta5/libwasmvm/Cargo.lock#L1530-L1531)
39+
/// are broken, i.e. they will crash when reading older v1 modules.
40+
/// - **v2**:<br>
41+
/// Version for cosmwasm_vm 1.0.0-beta5 / wasmvm 1.0.0-beta6 that ships with Wasmer 2.1.1.
42+
/// - **v3**:<br>
43+
/// Version for Wasmer 2.2.0 which contains a [module breaking change to 2.1.x](https://github.com/wasmerio/wasmer/pull/2747).
44+
/// - **v4**:<br>
45+
/// Version for Wasmer 2.3.0 which contains a module breaking change to 2.2.0 that was not reflected in
46+
/// the module header version (<https://github.com/wasmerio/wasmer/issues/3193>). In cosmwasm-vm 1.1.0-1.1.1
47+
/// the old value "v3" is still used along with Wasmer 2.3.0 (bug). From cosmwasm 1.1.2 onwards, this is
48+
/// fixed by bumping to "v4".
49+
/// - **v5**:<br>
50+
/// A change in memory layout of some types in Rust [std] caused
51+
/// [issues with module deserialization](https://github.com/CosmWasm/wasmvm/issues/426).
52+
/// To work around this, the version was bumped to "v5" here to invalidate these corrupt caches.
53+
/// - **v6**:<br>
54+
/// Version for cosmwasm_vm 1.3+ which adds a sub-folder with the target identier for the modules.
55+
/// - **v7**:<br>
56+
/// New version because of Wasmer 2.3.0 -> 4 upgrade.
57+
/// This internally changes how rkyv is used for module serialization, making compatibility unlikely.
58+
/// - **v8**:<br>
59+
/// New version because of Wasmer 4.1.2 -> 4.2.2 upgrade.
60+
/// Module compatibility between Wasmer versions is not guaranteed.
61+
/// - **v9**:<br>
62+
/// New version because of Wasmer 4.2.2 -> 4.2.6 upgrade.
63+
/// Module compatibility between Wasmer versions is not guaranteed.
64+
/// - **v10**:<br>
65+
/// New version because of Metering middleware change.
66+
const MODULE_SERIALIZATION_VERSION: &str = "v10";
67+
2368
/// Function that actually does the heavy lifting of creating the module version discriminator.
2469
///
2570
/// Separated for sanity tests because otherwise the `OnceLock` would cache the result.
@@ -29,7 +74,10 @@ fn raw_module_version_discriminator() -> String {
2974
let hashes = cosmwasm_vm_derive::collect_hashes();
3075

3176
let mut hasher = blake3::Hasher::new();
77+
78+
hasher.update(MODULE_SERIALIZATION_VERSION.as_bytes());
3279
hasher.update(wasmer_version.as_bytes());
80+
3381
for hash in hashes {
3482
hasher.update(hash.as_bytes());
3583
}
@@ -410,15 +458,11 @@ mod tests {
410458
fn module_version_discriminator_stays_the_same() {
411459
let v1 = raw_module_version_discriminator();
412460
let v2 = raw_module_version_discriminator();
413-
assert_eq!(v1, v2);
414-
}
461+
let v3 = raw_module_version_discriminator();
462+
let v4 = raw_module_version_discriminator();
415463

416-
#[test]
417-
fn module_version_discriminator_is_fixed() {
418-
let discriminator = raw_module_version_discriminator();
419-
assert_eq!(
420-
discriminator,
421-
"ddae2ae211962fc3481ff11cd46413750c692ddeb3d65f3e7a8a6d31ab1f8511"
422-
);
464+
assert_eq!(v1, v2);
465+
assert_eq!(v2, v3);
466+
assert_eq!(v3, v4);
423467
}
424468
}

0 commit comments

Comments
 (0)