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

feat: serialize compiled rules in a platform-independent way. #202

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion lib/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 18 additions & 6 deletions lib/src/compiler/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ pub struct Rules {
/// string as `&BStr`.
pub(in crate::compiler) lit_pool: BStringPool<LiteralId>,

/// WASM module as in raw form.
pub(in crate::compiler) wasm_mod: Vec<u8>,

/// 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<wasmtime::Module>,

/// Vector with the names of all the imported modules. The vector contains
/// the [`IdentId`] corresponding to the module's identifier.
Expand Down Expand Up @@ -163,6 +163,18 @@ impl Rules {
.with_varint_encoding()
.deserialize::<Self>(&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));

Expand Down Expand Up @@ -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()
}
}

Expand Down
Loading