Skip to content
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
242 changes: 110 additions & 132 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ walrus = "0.26.1"
wasm-bindgen = "0.2.117"
wasm-bindgen-test = "0.3.67"
wasm-opt = "0.116.1"
wasmtime = { version = "40.0.4", default-features = false }
wasmtime = { version = "43.0.1", default-features = false }
x509-parser = "0.18.0"
yansi = "1.0.1"
yara-x = { path = "lib", version = "1.15.0" }
Expand Down
2 changes: 1 addition & 1 deletion lib/src/compiler/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ pub struct InvalidModifier {
#[associated_enum(CompileError)]
#[error(code = "E034", title = "potentially slow loop")]
#[label(
"this range can be very large",
"this range can be very large",
loc
)]
pub struct PotentiallySlowLoop {
Expand Down
9 changes: 6 additions & 3 deletions lib/src/compiler/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::slice::Iter;
use std::time::Instant;

use aho_corasick::AhoCorasick;
use anyhow::anyhow;
#[cfg(feature = "logging")]
use log::*;
use regex_automata::meta::Regex;
Expand Down Expand Up @@ -225,11 +226,13 @@ impl Rules {
#[cfg(feature = "logging")]
let start = Instant::now();

rules.compiled_wasm_mod =
Some(wasm::runtime::Module::from_binary(
rules.compiled_wasm_mod = Some(
wasm::runtime::Module::from_binary(
wasm::get_engine(),
rules.wasm_mod.as_slice(),
)?);
)
.map_err(|e| SerializationError::from(anyhow!(e)))?,
);

#[cfg(feature = "logging")]
info!("WASM build time: {:?}", Instant::elapsed(&start));
Expand Down
20 changes: 7 additions & 13 deletions lib/src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ use crate::types::{
};
use crate::wasm::integer::RangedInteger;
use crate::wasm::runtime::{
AsContext, AsContextMut, Caller, Config, Engine, FuncType, Linker, ValRaw,
ValType,
AsContext, AsContextMut, Caller, Config, Engine, FuncType, Linker,
Trampoline, TrampolineResult, ValRaw, ValType,
};
use crate::wasm::string::RuntimeString;
use crate::wasm::string::String as _;
Expand Down Expand Up @@ -279,7 +279,7 @@ impl WasmExport {
pub(crate) trait WasmExportedFn {
/// Returns the function that will be passed to the selected runtime linker
/// while linking the WASM code to this function.
fn trampoline(&'static self) -> TrampolineFn;
fn trampoline(&'static self) -> Trampoline<ScanContext<'static, 'static>>;

/// Returns a [`Vec<ValType>`] with the types of the function's
/// arguments
Expand All @@ -302,13 +302,6 @@ pub(crate) trait WasmExportedFn {
}
}

type TrampolineFn = Box<
dyn Fn(Caller<'_, ScanContext>, &mut [ValRaw]) -> anyhow::Result<()>
+ Send
+ Sync
+ 'static,
>;

const MAX_RESULTS: usize = 4;
type WasmResultArray<T> = SmallVec<[T; MAX_RESULTS]>;

Expand Down Expand Up @@ -689,11 +682,11 @@ macro_rules! impl_wasm_exported_fn {
#[allow(unused_variables)]
#[allow(non_snake_case)]
#[allow(unused_mut)]
fn trampoline(&'static self) -> TrampolineFn {
fn trampoline(&'static self) -> Trampoline<ScanContext<'static, 'static>> {
Box::new(
|mut caller: Caller<'_, ScanContext>,
args_and_results: &mut [ValRaw]|
-> anyhow::Result<()> {
-> TrampolineResult {
let mut i = 0;
$(
let $args = args_and_results[i].raw_into(caller.data_mut());
Expand All @@ -707,7 +700,8 @@ macro_rules! impl_wasm_exported_fn {
let num_results = result_slice.len();

args_and_results[0..num_results].clone_from_slice(result_slice);
anyhow::Ok(())

TrampolineResult::Ok(())
},
)
}
Expand Down
3 changes: 3 additions & 0 deletions lib/src/wasm/runtime/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub(crate) use super::common::{
ValType,
};

pub(crate) type Trampoline<T> = common::Trampoline<T, Backend>;
pub(crate) type TrampolineResult = common::TrampolineResult;

struct GlobalInner {
val_type: ValType,
mutability: Mutability,
Expand Down
21 changes: 13 additions & 8 deletions lib/src/wasm/runtime/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,15 @@ pub(crate) struct Linker<T, B: RuntimeBackend> {
_phantom: PhantomData<T>,
}

pub(crate) type Trampoline<T, B> = Box<
dyn Fn(Caller<'_, T, B>, &mut [ValRaw]) -> TrampolineResult
+ Send
+ Sync
+ 'static,
>;

pub(crate) type TrampolineResult = Result<()>;

impl<T: 'static, B: RuntimeBackend> Linker<T, B> {
/// Creates an empty linker.
///
Expand All @@ -552,13 +561,8 @@ impl<T: 'static, B: RuntimeBackend> Linker<T, B> {
name: &str,
ty: FuncType,
sync_flags: u32,
trampoline: Box<
dyn Fn(Caller<'_, T, B>, &mut [ValRaw]) -> Result<()>
+ Send
+ Sync
+ 'static,
>,
) -> Result<()> {
trampoline: Trampoline<T, B>,
) -> TrampolineResult {
// This mirrors Wasmtime's unchecked registration API. The generated
// WASM determines the ABI, so the runtime only needs to record the
// metadata and trampoline here.
Expand All @@ -571,7 +575,8 @@ impl<T: 'static, B: RuntimeBackend> Linker<T, B> {
sync_flags,
trampoline: Arc::from(trampoline),
});
Ok(())

TrampolineResult::Ok(())
}

/// Defines an extern import.
Expand Down
39 changes: 29 additions & 10 deletions lib/src/wasm/runtime/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
//! This adapter exists only to normalize a couple of APIs so the rest of the
//! crate can talk to native and custom runtimes through the same interface.

use anyhow::Result;

use crate::errors::SerializationError;
use anyhow::anyhow;
use std::mem::transmute;
/// Wasmtime types re-exported by the native runtime.
pub(crate) use wasmtime::{
AsContext, AsContextMut, Caller, Config, Engine, Extern, FuncType, Global,
Expand All @@ -15,8 +16,14 @@ pub(crate) use wasmtime::{
/// Thin wrapper around [`wasmtime::Linker`] with a backend-neutral API.
pub(crate) struct Linker<T>(wasmtime::Linker<T>);

type Trampoline<T> =
dyn Fn(Caller<'_, T>, &mut [ValRaw]) -> Result<()> + Send + Sync + 'static;
pub(crate) type Trampoline<T> = Box<
dyn Fn(Caller<'_, T>, &mut [ValRaw]) -> TrampolineResult
+ Send
+ Sync
+ 'static,
>;

pub(crate) type TrampolineResult = wasmtime::Result<()>;

impl<T: 'static> Linker<T> {
/// Creates a new linker.
Expand All @@ -35,11 +42,21 @@ impl<T: 'static> Linker<T> {
name: &str,
ty: FuncType,
sync_flags: u32,
trampoline: Box<Trampoline<T>>,
) -> Result<()> {
trampoline: Trampoline<T>,
) -> TrampolineResult {
let _ = sync_flags;
unsafe {
self.0.func_new_unchecked(module, name, ty, trampoline).map(|_| ())
self.0
.func_new_unchecked(module, name, ty, move |caller, args| {
trampoline(
caller,
transmute::<
&mut [std::mem::MaybeUninit<ValRaw>],
&mut [ValRaw],
>(args),
)
})
.map(|_| ())
}
}

Expand All @@ -50,7 +67,7 @@ impl<T: 'static> Linker<T> {
module: &str,
name: &str,
item: impl Into<Extern>,
) -> Result<&mut Self> {
) -> wasmtime::Result<&mut Self> {
self.0.define(store, module, name, item)?;
Ok(self)
}
Expand All @@ -60,7 +77,9 @@ impl<T: 'static> Linker<T> {
&self,
store: impl AsContextMut<Data = T>,
module: &Module,
) -> Result<Instance> {
self.0.instantiate(store, module)
) -> Result<Instance, SerializationError> {
self.0
.instantiate(store, module)
.map_err(|e| SerializationError::InvalidWASM(anyhow!(e)))
}
}
2 changes: 1 addition & 1 deletion ls/src/features/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub fn compiler_diagnostics(
|meta| {
matches!(
meta.value,
yara_x_parser::ast::MetaValue::Float(_)
yara_x_parser::ast::MetaValue::Bool(_)
)
},
format!(
Expand Down
Loading