Skip to content

Commit 6b44755

Browse files
committed
fix: compilation error introduced in previous commit.
1 parent 178e2d6 commit 6b44755

6 files changed

Lines changed: 46 additions & 35 deletions

File tree

lib/src/compiler/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub enum SerializationError {
6262

6363
/// Error occurred while deserializing WASM code.
6464
#[error("invalid YARA-X compiled rules file")]
65-
InvalidWASM(#[from] wasmtime::Error),
65+
InvalidWASM(#[from] anyhow::Error),
6666
}
6767

6868
/// Error returned when rule compilation fails.
@@ -652,7 +652,7 @@ pub struct InvalidModifier {
652652
#[associated_enum(CompileError)]
653653
#[error(code = "E034", title = "potentially slow loop")]
654654
#[label(
655-
"this range can be very large",
655+
"this range can be very large",
656656
loc
657657
)]
658658
pub struct PotentiallySlowLoop {

lib/src/compiler/rules.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::slice::Iter;
66
use std::time::Instant;
77

88
use aho_corasick::AhoCorasick;
9+
use anyhow::anyhow;
910
#[cfg(feature = "logging")]
1011
use log::*;
1112
use regex_automata::meta::Regex;
@@ -225,11 +226,13 @@ impl Rules {
225226
#[cfg(feature = "logging")]
226227
let start = Instant::now();
227228

228-
rules.compiled_wasm_mod =
229-
Some(wasm::runtime::Module::from_binary(
229+
rules.compiled_wasm_mod = Some(
230+
wasm::runtime::Module::from_binary(
230231
wasm::get_engine(),
231232
rules.wasm_mod.as_slice(),
232-
)?);
233+
)
234+
.map_err(|e| SerializationError::from(anyhow!(e)))?,
235+
);
233236

234237
#[cfg(feature = "logging")]
235238
info!("WASM build time: {:?}", Instant::elapsed(&start));

lib/src/wasm/mod.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ use crate::types::{
9999
};
100100
use crate::wasm::integer::RangedInteger;
101101
use crate::wasm::runtime::{
102-
AsContext, AsContextMut, Caller, Config, Engine, FuncType, Linker, ValRaw,
103-
ValType,
102+
AsContext, AsContextMut, Caller, Config, Engine, FuncType, Linker,
103+
Trampoline, TrampolineResult, ValRaw, ValType,
104104
};
105105
use crate::wasm::string::RuntimeString;
106106
use crate::wasm::string::String as _;
@@ -279,7 +279,7 @@ impl WasmExport {
279279
pub(crate) trait WasmExportedFn {
280280
/// Returns the function that will be passed to the selected runtime linker
281281
/// while linking the WASM code to this function.
282-
fn trampoline(&'static self) -> TrampolineFn;
282+
fn trampoline(&'static self) -> Trampoline<ScanContext<'static, 'static>>;
283283

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

305-
type TrampolineFn = Box<
306-
dyn Fn(Caller<'_, ScanContext>, &mut [ValRaw]) -> wasmtime::Result<()>
307-
+ Send
308-
+ Sync
309-
+ 'static,
310-
>;
311-
312305
const MAX_RESULTS: usize = 4;
313306
type WasmResultArray<T> = SmallVec<[T; MAX_RESULTS]>;
314307

@@ -689,11 +682,11 @@ macro_rules! impl_wasm_exported_fn {
689682
#[allow(unused_variables)]
690683
#[allow(non_snake_case)]
691684
#[allow(unused_mut)]
692-
fn trampoline(&'static self) -> TrampolineFn {
685+
fn trampoline(&'static self) -> Trampoline<ScanContext<'static, 'static>> {
693686
Box::new(
694687
|mut caller: Caller<'_, ScanContext>,
695688
args_and_results: &mut [ValRaw]|
696-
-> wasmtime::Result<()> {
689+
-> TrampolineResult {
697690
let mut i = 0;
698691
$(
699692
let $args = args_and_results[i].raw_into(caller.data_mut());
@@ -708,7 +701,7 @@ macro_rules! impl_wasm_exported_fn {
708701

709702
args_and_results[0..num_results].clone_from_slice(result_slice);
710703

711-
wasmtime::Result::Ok(())
704+
TrampolineResult::Ok(())
712705
},
713706
)
714707
}

lib/src/wasm/runtime/browser.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ pub(crate) use super::common::{
4040
ValType,
4141
};
4242

43+
pub(crate) type Trampoline<T> = common::Trampoline<T, Backend>;
44+
pub(crate) type TrampolineResult = common::TrampolineResult;
45+
4346
struct GlobalInner {
4447
val_type: ValType,
4548
mutability: Mutability,

lib/src/wasm/runtime/common.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,15 @@ pub(crate) struct Linker<T, B: RuntimeBackend> {
529529
_phantom: PhantomData<T>,
530530
}
531531

532+
pub(crate) type Trampoline<T, B> = Box<
533+
dyn Fn(Caller<'_, T, B>, &mut [ValRaw]) -> TrampolineResult
534+
+ Send
535+
+ Sync
536+
+ 'static,
537+
>;
538+
539+
pub(crate) type TrampolineResult = Result<()>;
540+
532541
impl<T: 'static, B: RuntimeBackend> Linker<T, B> {
533542
/// Creates an empty linker.
534543
///
@@ -552,13 +561,8 @@ impl<T: 'static, B: RuntimeBackend> Linker<T, B> {
552561
name: &str,
553562
ty: FuncType,
554563
sync_flags: u32,
555-
trampoline: Box<
556-
dyn Fn(Caller<'_, T, B>, &mut [ValRaw]) -> Result<()>
557-
+ Send
558-
+ Sync
559-
+ 'static,
560-
>,
561-
) -> Result<()> {
564+
trampoline: Trampoline<T, B>,
565+
) -> TrampolineResult {
562566
// This mirrors Wasmtime's unchecked registration API. The generated
563567
// WASM determines the ABI, so the runtime only needs to record the
564568
// metadata and trampoline here.
@@ -571,7 +575,8 @@ impl<T: 'static, B: RuntimeBackend> Linker<T, B> {
571575
sync_flags,
572576
trampoline: Arc::from(trampoline),
573577
});
574-
Ok(())
578+
579+
TrampolineResult::Ok(())
575580
}
576581

577582
/// Defines an extern import.

lib/src/wasm/runtime/native.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
//! This adapter exists only to normalize a couple of APIs so the rest of the
44
//! crate can talk to native and custom runtimes through the same interface.
55
6+
use crate::errors::SerializationError;
7+
use anyhow::anyhow;
68
use std::mem::transmute;
7-
89
/// Wasmtime types re-exported by the native runtime.
910
pub(crate) use wasmtime::{
1011
AsContext, AsContextMut, Caller, Config, Engine, Extern, FuncType, Global,
@@ -15,10 +16,14 @@ pub(crate) use wasmtime::{
1516
/// Thin wrapper around [`wasmtime::Linker`] with a backend-neutral API.
1617
pub(crate) struct Linker<T>(wasmtime::Linker<T>);
1718

18-
type Trampoline<T> = dyn Fn(Caller<'_, T>, &mut [ValRaw]) -> wasmtime::Result<()>
19-
+ Send
20-
+ Sync
21-
+ 'static;
19+
pub(crate) type Trampoline<T> = Box<
20+
dyn Fn(Caller<'_, T>, &mut [ValRaw]) -> TrampolineResult
21+
+ Send
22+
+ Sync
23+
+ 'static,
24+
>;
25+
26+
pub(crate) type TrampolineResult = wasmtime::Result<()>;
2227

2328
impl<T: 'static> Linker<T> {
2429
/// Creates a new linker.
@@ -37,8 +42,8 @@ impl<T: 'static> Linker<T> {
3742
name: &str,
3843
ty: FuncType,
3944
sync_flags: u32,
40-
trampoline: Box<Trampoline<T>>,
41-
) -> wasmtime::Result<()> {
45+
trampoline: Trampoline<T>,
46+
) -> TrampolineResult {
4247
let _ = sync_flags;
4348
unsafe {
4449
self.0
@@ -72,7 +77,9 @@ impl<T: 'static> Linker<T> {
7277
&self,
7378
store: impl AsContextMut<Data = T>,
7479
module: &Module,
75-
) -> wasmtime::Result<Instance> {
76-
self.0.instantiate(store, module)
80+
) -> Result<Instance, SerializationError> {
81+
self.0
82+
.instantiate(store, module)
83+
.map_err(|e| SerializationError::InvalidWASM(anyhow!(e)))
7784
}
7885
}

0 commit comments

Comments
 (0)