From c33ceee9a30f19285f6d9bcc290a73a47052b52c Mon Sep 17 00:00:00 2001 From: "Victor M. Alvarez" Date: Fri, 20 Sep 2024 17:39:48 +0200 Subject: [PATCH] feat: serialize compiled rules in a platform-independent way. --- lib/src/compiler/mod.rs | 3 ++- lib/src/compiler/rules.rs | 24 ++++++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/src/compiler/mod.rs b/lib/src/compiler/mod.rs index 613126d96..004a8514d 100644 --- a/lib/src/compiler/mod.rs +++ b/lib/src/compiler/mod.rs @@ -714,8 +714,9 @@ impl<'a> Compiler<'a> { let mut rules = Rules { serialized_globals, + wasm_mod, + compiled_wasm_mod: Some(compiled_wasm_mod), relaxed_re_syntax: self.relaxed_re_syntax, - wasm_mod: compiled_wasm_mod, ac: None, num_patterns: self.next_pattern_id.0 as usize, ident_pool: self.ident_pool, diff --git a/lib/src/compiler/rules.rs b/lib/src/compiler/rules.rs index b78b7ce93..41b61c4ee 100644 --- a/lib/src/compiler/rules.rs +++ b/lib/src/compiler/rules.rs @@ -48,12 +48,12 @@ pub struct Rules { /// string as `&BStr`. pub(in crate::compiler) lit_pool: BStringPool, + /// WASM module as in raw form. + pub(in crate::compiler) wasm_mod: Vec, + /// WASM module already compiled into native code for the current platform. - #[serde( - serialize_with = "serialize_wasm_mod", - deserialize_with = "deserialize_wasm_mod" - )] - pub(in crate::compiler) wasm_mod: wasmtime::Module, + #[serde(skip)] + pub(in crate::compiler) compiled_wasm_mod: Option, /// Vector with the names of all the imported modules. The vector contains /// the [`IdentId`] corresponding to the module's identifier. @@ -163,6 +163,18 @@ impl Rules { .with_varint_encoding() .deserialize::(&bytes[magic.len()..])?; + // Compile the WASM module for the current platform. This panics + // if the WASM code is invalid, which should not happen as the code is + // emitted by YARA itself. If this ever happens is probably because + // wrong WASM code is being emitted. + rules.compiled_wasm_mod = Some( + wasmtime::Module::from_binary( + &crate::wasm::ENGINE, + rules.wasm_mod.as_slice(), + ) + .expect("WASM module is not valid"), + ); + #[cfg(feature = "logging")] info!("Deserialization time: {:?}", Instant::elapsed(&start)); @@ -421,7 +433,7 @@ impl Rules { #[inline] pub(crate) fn wasm_mod(&self) -> &wasmtime::Module { - &self.wasm_mod + self.compiled_wasm_mod.as_ref().unwrap() } }