Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store and restore hash from a compiled module #4654

Merged
merged 18 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

1 change: 1 addition & 0 deletions lib/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ tracing = { version = "0.1" }
wat = { version = "=1.0.71", optional = true }
rustc-demangle = "0.1"
shared-buffer = { workspace = true }
xxhash-rust = { version = "0.8.10", features = ["xxh64"] }
maminrayej marked this conversation as resolved.
Show resolved Hide resolved

# Dependencies and Development Dependencies for `sys`.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
9 changes: 8 additions & 1 deletion lib/api/src/js/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct Module {
type_hints: Option<ModuleTypeHints>,
#[cfg(feature = "js-serializable-module")]
raw_bytes: Option<Bytes>,
hash: [u8; 8],
maminrayej marked this conversation as resolved.
Show resolved Hide resolved
}

// Module implements `structuredClone` in js, so it's safe it to make it Send.
Expand Down Expand Up @@ -115,7 +116,8 @@ impl Module {
type_hints,
name,
#[cfg(feature = "js-serializable-module")]
raw_bytes: Some(binary),
raw_bytes: Some(binary.clone()),
maminrayej marked this conversation as resolved.
Show resolved Hide resolved
hash: xxhash_rust::xxh64::xxh64(binary.as_ref(), 0).to_ne_bytes(),
}
}

Expand Down Expand Up @@ -210,6 +212,10 @@ impl Module {
self.name.as_ref().map(|s| s.as_ref())
}

pub fn hash(&self) -> [u8; 8] {
self.hash
}

maminrayej marked this conversation as resolved.
Show resolved Hide resolved
pub fn serialize(&self) -> Result<Bytes, SerializeError> {
#[cfg(feature = "js-serializable-module")]
return self.raw_bytes.clone().ok_or(SerializeError::Generic(
Expand Down Expand Up @@ -457,6 +463,7 @@ impl From<WebAssembly::Module> for Module {
type_hints: None,
#[cfg(feature = "js-serializable-module")]
raw_bytes: None,
hash: [0u8; 8],
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion lib/api/src/jsc/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,14 @@ impl Module {
///
pub(crate) unsafe fn from_js_module(module: JSObject, binary: impl IntoBytes) -> Self {
let binary = binary.into_bytes();

// The module is now validated, so we can safely parse it's types
let info = crate::module_info_polyfill::translate_module(&binary[..])
let mut info = crate::module_info_polyfill::translate_module(&binary[..])
maminrayej marked this conversation as resolved.
Show resolved Hide resolved
.unwrap()
.info;

info.hash = xxhash_rust::xxh64::xxh64(binary.as_ref(), 0).to_ne_bytes();

Self {
module,
name: info.name.clone(),
Expand Down Expand Up @@ -175,6 +178,10 @@ impl Module {
self.name.as_ref().map(|s| s.as_ref())
}

pub fn hash(&self) -> [u8; 8] {
self.info.hash
}

pub fn serialize(&self) -> Result<Bytes, SerializeError> {
return self.raw_bytes.clone().ok_or(SerializeError::Generic(
"Not able to serialize module".to_string(),
Expand Down
5 changes: 5 additions & 0 deletions lib/api/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,11 @@ impl Module {
self.0.name()
}

/// Returns the xxhash module hash
pub fn hash(&self) -> [u8; 8] {
self.0.hash()
}

/// Sets the name of the current module.
/// This is normally useful for stacktraces and debugging.
///
Expand Down
4 changes: 4 additions & 0 deletions lib/api/src/sys/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ impl Module {
self.info().name.as_deref()
}

pub(crate) fn hash(&self) -> [u8; 8] {
self.info().hash
}

pub(crate) fn set_name(&mut self, name: &str) -> bool {
Arc::get_mut(&mut self.artifact).map_or(false, |artifact| {
artifact.set_module_info_name(name.to_string())
Expand Down
5 changes: 1 addition & 4 deletions lib/cli/src/commands/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,10 +712,7 @@ impl ExecutableTarget {
let engine = runtime.engine();
pb.set_message("Deserializing pre-compiled WebAssembly module");
let module = unsafe { Module::deserialize_from_file(&engine, path)? };
let module_hash = {
let wasm = std::fs::read(path)?;
ModuleHash::xxhash(wasm)
};
let module_hash = ModuleHash::xxhash_from_bytes(module.hash());

Ok(ExecutableTarget::WebAssembly {
module,
Expand Down
1 change: 1 addition & 0 deletions lib/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ serde = { version = "1.0", features = ["derive"], optional = true }
thiserror = "1.0"
serde_bytes = { version = "0.11", optional = true }
smallvec = "1.6"
xxhash-rust = { version = "0.8.10", features = ["xxh64"] }

backtrace = "0.3"
memmap2 = "0.5"
Expand Down
3 changes: 3 additions & 0 deletions lib/compiler/src/translator/environ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ impl<'data> ModuleEnvironment<'data> {
assert!(self.module_translation_state.is_none());
let module_translation_state = translate_module(data, &mut self)?;
self.module_translation_state = Some(module_translation_state);

self.module.hash = xxhash_rust::xxh64::xxh64(data, 0).to_ne_bytes();

Ok(self)
}

Expand Down
8 changes: 8 additions & 0 deletions lib/types/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ pub struct ModuleInfo {
#[cfg_attr(feature = "enable-serde", serde(skip_serializing, skip_deserializing))]
pub id: ModuleId,

// FIXME: We need ModuleHash here
maminrayej marked this conversation as resolved.
Show resolved Hide resolved
/// hash of the module
pub hash: [u8; 8],
maminrayej marked this conversation as resolved.
Show resolved Hide resolved

/// The name of this wasm module, often found in the wasm file.
pub name: Option<String>,

Expand Down Expand Up @@ -182,6 +186,8 @@ pub struct ModuleInfo {
#[archive_attr(derive(CheckBytes, Debug))]
pub struct ArchivableModuleInfo {
name: Option<String>,
// FIXME: we need ModuleHash here
hash: [u8; 8],
imports: IndexMap<ImportKey, ImportIndex>,
exports: IndexMap<String, ExportIndex>,
start_function: Option<FunctionIndex>,
Expand All @@ -207,6 +213,7 @@ impl From<ModuleInfo> for ArchivableModuleInfo {
fn from(it: ModuleInfo) -> Self {
Self {
name: it.name,
hash: it.hash,
imports: it.imports,
exports: it.exports,
start_function: it.start_function,
Expand Down Expand Up @@ -235,6 +242,7 @@ impl From<ArchivableModuleInfo> for ModuleInfo {
Self {
id: Default::default(),
name: it.name,
hash: it.hash,
imports: it.imports,
exports: it.exports,
start_function: it.start_function,
Expand Down
Loading