Skip to content

Commit

Permalink
Add target_id
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Apr 19, 2023
1 parent adf463b commit fc6068b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ bench = false

[dependencies]
clru = "0.4.0"
crc32fast = "1.3.2"
# Uses the path when built locally; uses the given version from crates.io when published
cosmwasm-std = { path = "../std", version = "1.2.4", default-features = false }
cosmwasm-crypto = { path = "../crypto", version = "1.2.4" }
Expand Down Expand Up @@ -70,6 +71,7 @@ wat = "1.0"
clap = "2.33.3"
rand = "0.8"
leb128 = "0.2"
target-lexicon = "0.12"

[[bench]]
name = "main"
Expand Down
38 changes: 37 additions & 1 deletion packages/vm/src/modules/file_system_cache.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::fs;
use std::hash::Hash;
use std::io;
use std::path::Path;
use std::path::PathBuf;
use thiserror::Error;

use wasmer::{DeserializeError, Module, Store};
use wasmer::{DeserializeError, Module, Store, Target};

use crate::checksum::Checksum;
use crate::errors::{VmError, VmResult};
Expand Down Expand Up @@ -186,6 +187,17 @@ fn estimate_module_size(module_path: &Path) -> VmResult<usize> {
Ok(module_size)
}

/// Creates an identifier for the Wasmer `Target` that is used for
/// cache invalidation. The output is reasonable human friendly to be useable
/// in file path component.
fn target_id(target: &Target) -> String {
// Use a custom Hasher implementation to avoid randomization.
let mut deterministic_hasher = crc32fast::Hasher::new();
target.hash(&mut deterministic_hasher);
let hash = deterministic_hasher.finalize();
format!("{}-{:08X}", target.triple(), hash) // print 4 byte hash as 8 hex characters
}

#[cfg(test)]
mod tests {
use std::fs;
Expand Down Expand Up @@ -295,4 +307,28 @@ mod tests {
let existed = cache.remove(&checksum).unwrap();
assert!(!existed);
}

#[test]
fn target_id_works() {
let triple = wasmer::Triple {
architecture: wasmer::Architecture::X86_64,
vendor: target_lexicon::Vendor::Nintendo,
operating_system: target_lexicon::OperatingSystem::Fuchsia,
environment: target_lexicon::Environment::Gnu,
binary_format: target_lexicon::BinaryFormat::Coff,
};
let target = Target::new(triple.clone(), wasmer::CpuFeature::POPCNT.into());
let id = target_id(&target);
assert_eq!(id, "x86_64-nintendo-fuchsia-gnu-coff-4721E3F4");
// Changing CPU features changes the hash part
let target = Target::new(triple, wasmer::CpuFeature::AVX512DQ.into());
let id = target_id(&target);
assert_eq!(id, "x86_64-nintendo-fuchsia-gnu-coff-D5C8034F");

// Works for durrect target (hashing is deterministic);
let target = Target::default();
let id1 = target_id(&target);
let id2 = target_id(&target);
assert_eq!(id1, id2);
}
}

0 comments on commit fc6068b

Please sign in to comment.