yara-x 1.15.0

A pure Rust implementation of YARA.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
//! Common pieces shared by YARA-X's custom WASM runtimes.
//!
//! The browser backend exposes a small, Wasmtime-like API to the rest of the
//! crate. This module contains the backend-agnostic pieces of that shim so the
//! browser runtime can stay focused on the host `WebAssembly` integration.

use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::Arc;

use anyhow::Result;

/// Synchronize imported module state before invoking a callback.
pub(crate) const CALLBACK_SYNC_BEFORE: u32 = 1 << 0;
/// Synchronize imported module state after invoking a callback.
pub(crate) const CALLBACK_SYNC_AFTER: u32 = 1 << 1;

/// Returns true if callback state must be synchronized before the call.
pub(crate) fn should_sync_before(sync_flags: u32) -> bool {
    sync_flags & CALLBACK_SYNC_BEFORE != 0
}

/// Returns true if callback state must be synchronized after the call.
pub(crate) fn should_sync_after(sync_flags: u32) -> bool {
    sync_flags & CALLBACK_SYNC_AFTER != 0
}

/// Returns the scratch storage needed for callback parameters and results.
///
/// Custom runtimes reuse a single buffer for both directions, matching the
/// way the generated trampolines pass arguments in and read results back out.
pub(crate) fn callback_storage_len(
    params: &[ValType],
    results: &[ValType],
) -> usize {
    params.len().max(results.len())
}

/// Immutable view over a [`Store`].
pub(crate) type StoreContext<'a, T, B> = &'a Store<T, B>;
/// Mutable view over a [`Store`].
pub(crate) type StoreContextMut<'a, T, B> = &'a mut Store<T, B>;

/// Backend contract implemented by each custom runtime.
pub(crate) trait RuntimeBackend:
    Clone + Default + Sized + 'static
{
    /// Per-store runtime state owned by the backend.
    type RuntimeState: Default;
    /// Backend-specific representation of a compiled module.
    type ModuleInner;
    /// Backend-specific representation of an instantiated module.
    type InstanceInner;
    /// Backend-specific representation of a typed function handle.
    type TypedFuncHandle;

    /// Updates the deadline used for interrupting long-running scans.
    fn set_epoch_deadline(runtime: &mut Self::RuntimeState, deadline: u64);
    /// Resets any per-instantiation state before creating a new instance.
    fn prepare_for_instantiation(runtime: &mut Self::RuntimeState);
    /// Clears any runtime state that should not survive store reuse.
    fn reset_for_store_reuse(runtime: &mut Self::RuntimeState);

    /// Creates a global and returns its backend-specific identifier.
    fn create_global(
        runtime: &mut Self::RuntimeState,
        ty: GlobalType,
        value: Val,
    ) -> Result<usize>;

    /// Returns the current value for the global identified by `id`.
    fn get_global(runtime: &mut Self::RuntimeState, id: usize) -> Val;

    /// Updates the global identified by `id`.
    fn set_global(
        runtime: &mut Self::RuntimeState,
        id: usize,
        value: Val,
    ) -> Result<()>;

    /// Creates a memory and returns its backend-specific identifier.
    fn create_memory(
        runtime: &mut Self::RuntimeState,
        ty: MemoryType,
    ) -> Result<usize>;

    /// Returns the current memory contents for the memory identified by `id`.
    fn memory_data<'a>(runtime: &'a Self::RuntimeState, id: usize)
    -> &'a [u8];

    /// Returns mutable access to the memory identified by `id`.
    fn memory_data_mut<'a>(
        runtime: &'a mut Self::RuntimeState,
        id: usize,
    ) -> &'a mut [u8];

    /// Returns a raw pointer to the memory identified by `id`.
    fn memory_data_ptr(runtime: &mut Self::RuntimeState, id: usize)
    -> *mut u8;

    /// Creates a module from raw WASM bytes.
    fn module_from_binary(
        engine: &Engine,
        bytes: &[u8],
    ) -> Result<Self::ModuleInner>;

    /// Instantiates `module` with the functions and externs in `linker`.
    fn instantiate<T: 'static>(
        store: &mut Store<T, Self>,
        linker: &Linker<T, Self>,
        module: &Module<Self>,
    ) -> Result<Self::InstanceInner>;

    /// Returns a handle to the typed function named `name`.
    fn get_typed_func_handle<P, R>(
        instance: &Self::InstanceInner,
        name: &str,
    ) -> Result<Self::TypedFuncHandle>;

    /// Calls a `() -> i32` typed function.
    fn typed_func_call_i32<T>(
        store: &mut Store<T, Self>,
        func: &Self::TypedFuncHandle,
    ) -> Result<i32>;
}

/// Trait for types that can yield an immutable [`StoreContext`].
pub(crate) trait AsContext {
    /// User data stored in the underlying [`Store`].
    type Data;
    /// Runtime backend associated with the underlying [`Store`].
    type Backend: RuntimeBackend;

    /// Returns an immutable store context.
    fn as_context(&self) -> StoreContext<'_, Self::Data, Self::Backend>;
}

/// Trait for types that can yield a mutable [`StoreContextMut`].
pub(crate) trait AsContextMut: AsContext {
    /// Returns a mutable store context.
    fn as_context_mut(
        &mut self,
    ) -> StoreContextMut<'_, Self::Data, Self::Backend>;
}

/// Storage for user data plus backend-specific runtime state.
pub(crate) struct Store<T, B: RuntimeBackend> {
    /// User data associated with the store.
    data: T,
    /// Backend-specific runtime state for globals, memories and callbacks.
    pub(crate) runtime: B::RuntimeState,
    _engine: Engine,
    _backend: PhantomData<B>,
}

impl<T, B: RuntimeBackend> Store<T, B> {
    /// Creates a new store associated with `engine`.
    pub fn new(engine: &Engine, data: T) -> Self {
        Self {
            data,
            runtime: B::RuntimeState::default(),
            _engine: engine.clone(),
            _backend: PhantomData,
        }
    }

    /// Returns the store's user data.
    pub fn data(&self) -> &T {
        &self.data
    }

    /// Returns mutable access to the store's user data.
    pub fn data_mut(&mut self) -> &mut T {
        &mut self.data
    }

    /// Sets the deadline used by the backend for interrupting execution.
    pub fn set_epoch_deadline(&mut self, deadline: u64) {
        B::set_epoch_deadline(&mut self.runtime, deadline);
    }

    /// Registers a callback for deadline expiration.
    ///
    /// Custom runtimes poll the deadline from their runtime state instead of
    /// using a backend callback, so this method is a no-op kept for API
    /// compatibility with Wasmtime.
    pub fn epoch_deadline_callback<F>(&mut self, _callback: F)
    where
        F: FnMut(StoreContextMut<'_, T, B>) -> Result<()> + 'static,
    {
    }
}

impl<T, B: RuntimeBackend> Drop for Store<T, B> {
    fn drop(&mut self) {
        B::reset_for_store_reuse(&mut self.runtime);
    }
}

impl<T, B: RuntimeBackend> AsContext for Store<T, B> {
    type Data = T;
    type Backend = B;

    fn as_context(&self) -> StoreContext<'_, T, B> {
        self
    }
}

impl<T, B: RuntimeBackend> AsContextMut for Store<T, B> {
    fn as_context_mut(&mut self) -> StoreContextMut<'_, T, B> {
        self
    }
}

impl<T, B: RuntimeBackend> AsContext for &Store<T, B> {
    type Data = T;
    type Backend = B;

    fn as_context(&self) -> StoreContext<'_, T, B> {
        self
    }
}

impl<T, B: RuntimeBackend> AsContext for &mut Store<T, B> {
    type Data = T;
    type Backend = B;

    fn as_context(&self) -> StoreContext<'_, T, B> {
        self
    }
}

impl<T, B: RuntimeBackend> AsContextMut for &mut Store<T, B> {
    fn as_context_mut(&mut self) -> StoreContextMut<'_, T, B> {
        self
    }
}

impl<T, B: RuntimeBackend> AsContext for Pin<Box<Store<T, B>>> {
    type Data = T;
    type Backend = B;

    fn as_context(&self) -> StoreContext<'_, T, B> {
        self.as_ref().get_ref()
    }
}

impl<T, B: RuntimeBackend> AsContextMut for Pin<Box<Store<T, B>>> {
    fn as_context_mut(&mut self) -> StoreContextMut<'_, T, B> {
        // SAFETY: `Store<T, B>` is not self-referential, and callers rely on
        // mutable access to the pinned store.
        unsafe { self.as_mut().get_unchecked_mut() }
    }
}

/// View passed to host callbacks.
pub(crate) struct Caller<'a, T, B: RuntimeBackend> {
    /// Store being used for the callback.
    pub(crate) store: &'a mut Store<T, B>,
}

impl<'a, T, B: RuntimeBackend> Caller<'a, T, B> {
    /// Creates a caller from a mutable store reference.
    pub(crate) fn new(store: &'a mut Store<T, B>) -> Self {
        Self { store }
    }

    /// Returns the store's user data.
    pub fn data(&self) -> &T {
        self.store.data()
    }

    /// Returns mutable access to the store's user data.
    pub fn data_mut(&mut self) -> &mut T {
        self.store.data_mut()
    }
}

impl<T, B: RuntimeBackend> AsContext for Caller<'_, T, B> {
    type Data = T;
    type Backend = B;

    fn as_context(&self) -> StoreContext<'_, T, B> {
        self.store
    }
}

impl<T, B: RuntimeBackend> AsContextMut for Caller<'_, T, B> {
    fn as_context_mut(&mut self) -> StoreContextMut<'_, T, B> {
        self.store
    }
}

/// Minimal configuration object matching the Wasmtime API used by YARA-X.
///
/// All methods are no-ops for the custom runtimes.
#[derive(Clone, Default)]
pub(crate) struct Config;

impl Config {
    /// Matches Wasmtime's `native_unwind_info` option.
    #[cfg(target_env = "musl")]
    pub fn native_unwind_info(&mut self, _enabled: bool) -> &mut Self {
        self
    }

    /// Matches Wasmtime's `cranelift_opt_level` option.
    pub fn cranelift_opt_level(&mut self, _level: OptLevel) -> &mut Self {
        self
    }

    /// Matches Wasmtime's `epoch_interruption` option.
    pub fn epoch_interruption(&mut self, _enabled: bool) -> &mut Self {
        self
    }

    /// Matches Wasmtime's `memory_reservation` option.
    pub fn memory_reservation(&mut self, _bytes: u64) -> &mut Self {
        self
    }

    /// Matches Wasmtime's `memory_reservation_for_growth` option.
    pub fn memory_reservation_for_growth(&mut self, _bytes: u64) -> &mut Self {
        self
    }

    /// Matches Wasmtime's `memory_may_move` option.
    pub fn memory_may_move(&mut self, _enabled: bool) -> &mut Self {
        self
    }
}

/// Optimization level accepted by [`Config::cranelift_opt_level`].
#[derive(Clone, Copy)]
pub(crate) enum OptLevel {
    /// Optimize for both speed and code size.
    SpeedAndSize,
}

/// Minimal engine object matching the Wasmtime API used by YARA-X.
#[derive(Clone, Default)]
pub(crate) struct Engine;

impl Engine {
    /// Creates a new engine from `config`.
    pub fn new(_config: &Config) -> Result<Self> {
        Ok(Self)
    }

    /// Increments the engine epoch.
    ///
    /// Custom runtimes track deadlines without an engine-level epoch counter,
    /// so this is a no-op kept for API compatibility.
    pub fn increment_epoch(&self) {}

    /// Unloads process-wide handlers owned by the engine.
    ///
    /// Custom runtimes do not install such handlers.
    #[cfg(any(
        target_arch = "x86_64",
        target_arch = "aarch64",
        target_arch = "riscv64",
        target_arch = "s390x",
    ))]
    pub fn unload_process_handlers(self) {}
}

/// Value types supported by generated WASM code.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum ValType {
    /// A 64-bit integer.
    I64,
    /// A 32-bit integer.
    I32,
    /// A 64-bit floating-point value.
    F64,
    /// A 32-bit floating-point value.
    F32,
}

/// Raw value passed across host callback trampolines.
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct ValRaw(u64);

impl ValRaw {
    /// Creates a raw value from an `i64`.
    #[inline]
    pub fn i64(value: i64) -> Self {
        Self(value as u64)
    }

    /// Creates a raw value from an `i32`.
    #[inline]
    pub fn i32(value: i32) -> Self {
        Self((value as u32) as u64)
    }

    /// Creates a raw value from the bit pattern of an `f64`.
    #[inline]
    pub fn f64(value: u64) -> Self {
        Self(value)
    }

    /// Creates a raw value from the bit pattern of an `f32`.
    #[inline]
    pub fn f32(value: u32) -> Self {
        Self(value as u64)
    }

    /// Returns the value interpreted as an `i64`.
    #[inline]
    pub fn get_i64(self) -> i64 {
        self.0 as i64
    }

    /// Returns the value interpreted as an `i32`.
    #[inline]
    pub fn get_i32(self) -> i32 {
        self.0 as u32 as i32
    }

    /// Returns the raw bit pattern for an `f64`.
    #[inline]
    pub fn get_f64(self) -> u64 {
        self.0
    }

    /// Returns the raw bit pattern for an `f32`.
    #[inline]
    pub fn get_f32(self) -> u32 {
        self.0 as u32
    }
}

/// Function signature used when wiring imports.
#[derive(Clone)]
pub(crate) struct FuncType {
    /// Function parameter types.
    pub(crate) params: Vec<ValType>,
    /// Function result types.
    pub(crate) results: Vec<ValType>,
}

impl FuncType {
    /// Creates a new function signature.
    ///
    /// The `engine` argument is unused by the custom runtimes and is kept only
    /// for compatibility with Wasmtime's API.
    pub fn new(
        _engine: &Engine,
        params: Vec<ValType>,
        results: impl IntoIterator<Item = ValType>,
    ) -> Self {
        Self { params, results: results.into_iter().collect() }
    }
}

/// Host callback invoked from generated WASM code.
///
/// The callback receives raw parameter storage and writes any results back into
/// that same storage.
pub(crate) type HostFunc<T, B> =
    Arc<dyn Fn(Caller<'_, T, B>, &mut [ValRaw]) -> Result<()> + Send + Sync>;

/// Function registered in a [`Linker`].
pub(crate) struct RegisteredFunc<T, B: RuntimeBackend> {
    /// Module namespace for the import.
    pub(crate) module: String,
    /// Name of the imported function.
    pub(crate) name: String,
    /// Function signature.
    pub(crate) ty: FuncType,
    /// Callback synchronization flags.
    pub(crate) sync_flags: u32,
    /// Host trampoline invoked when the import is called.
    pub(crate) trampoline: HostFunc<T, B>,
}

/// Handle to a global defined in the store.
#[derive(Clone, Copy)]
pub(crate) struct Global {
    /// Backend-specific global identifier.
    pub(crate) id: usize,
}

/// Handle to a memory defined in the store.
#[derive(Clone, Copy)]
pub(crate) struct Memory {
    /// Backend-specific memory identifier.
    pub(crate) id: usize,
}

/// Extern item that can be defined in a [`Linker`].
pub(crate) enum Extern {
    /// A global variable.
    Global(Global),
    /// A linear memory.
    Memory(Memory),
}

impl From<Global> for Extern {
    fn from(value: Global) -> Self {
        Self::Global(value)
    }
}

impl From<Memory> for Extern {
    fn from(value: Memory) -> Self {
        Self::Memory(value)
    }
}

/// Extern registered in a [`Linker`].
pub(crate) struct DefinedExtern {
    /// Module namespace for the import.
    pub(crate) module: String,
    /// Name of the imported extern.
    pub(crate) name: String,
    /// Extern value to be defined.
    pub(crate) value: Extern,
}

/// Linker used for registering imports and instantiating modules.
pub(crate) struct Linker<T, B: RuntimeBackend> {
    /// Functions registered in the linker.
    pub(crate) functions: Vec<RegisteredFunc<T, B>>,
    /// Externs registered in the linker.
    pub(crate) externs: Vec<DefinedExtern>,
    _phantom: PhantomData<T>,
}

impl<T: 'static, B: RuntimeBackend> Linker<T, B> {
    /// Creates an empty linker.
    ///
    /// The `engine` argument is unused by the custom runtimes and is kept only
    /// for compatibility with Wasmtime's API.
    pub fn new(_engine: &Engine) -> Self {
        Self {
            functions: Vec::new(),
            externs: Vec::new(),
            _phantom: PhantomData,
        }
    }

    /// Registers a function import without validating its ABI.
    ///
    /// The generated WASM determines the function signature, so custom
    /// runtimes only need to record the metadata and trampoline here.
    pub unsafe fn func_new_unchecked(
        &mut self,
        module: &str,
        name: &str,
        ty: FuncType,
        sync_flags: u32,
        trampoline: Box<
            dyn Fn(Caller<'_, T, B>, &mut [ValRaw]) -> Result<()>
                + Send
                + Sync
                + 'static,
        >,
    ) -> Result<()> {
        // 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.
        // Store the trampoline and metadata; the backend turns these into
        // actual imports when the module is instantiated.
        self.functions.push(RegisteredFunc {
            module: module.to_owned(),
            name: name.to_owned(),
            ty,
            sync_flags,
            trampoline: Arc::from(trampoline),
        });
        Ok(())
    }

    /// Defines an extern import.
    ///
    /// The `store` argument is unused by the custom runtimes and is kept only
    /// for compatibility with Wasmtime's API.
    pub fn define(
        &mut self,
        _store: StoreContext<'_, T, B>,
        module: &str,
        name: &str,
        value: impl Into<Extern>,
    ) -> Result<&mut Self> {
        self.externs.push(DefinedExtern {
            module: module.to_owned(),
            name: name.to_owned(),
            value: value.into(),
        });
        Ok(self)
    }

    /// Instantiates `module` with the imports currently registered.
    pub fn instantiate(
        &self,
        store: StoreContextMut<'_, T, B>,
        module: &Module<B>,
    ) -> Result<Instance<B>> {
        // Clear any instance-local state before wiring a fresh set of imports.
        B::prepare_for_instantiation(&mut store.runtime);
        Ok(Instance {
            inner: B::instantiate(store, self, module)?,
            _backend: PhantomData,
        })
    }
}

/// Whether a global can be mutated after creation.
#[derive(Clone, Copy)]
pub(crate) enum Mutability {
    /// Immutable global.
    Const,
    /// Mutable global.
    Var,
}

/// Type information for a global.
pub(crate) struct GlobalType {
    /// Value type stored in the global.
    pub(crate) val_type: ValType,
    /// Whether the global can be mutated.
    pub(crate) mutability: Mutability,
}

impl GlobalType {
    /// Creates a new global type.
    pub fn new(val_type: ValType, mutability: Mutability) -> Self {
        Self { val_type, mutability }
    }
}

/// Runtime value stored in globals and returned from typed functions.
#[derive(Clone, Copy)]
pub(crate) enum Val {
    /// A 32-bit integer.
    I32(i32),
    /// A 64-bit integer.
    I64(i64),
    /// Raw bits for a 32-bit floating-point value.
    F32(u32),
    /// Raw bits for a 64-bit floating-point value.
    F64(u64),
}

impl Val {
    /// Returns the value as an `i64`, if applicable.
    pub fn i64(self) -> Option<i64> {
        match self {
            Self::I64(v) => Some(v),
            _ => None,
        }
    }
}

impl Global {
    /// Creates a new global in `store`.
    pub fn new<T, B: RuntimeBackend>(
        store: StoreContextMut<'_, T, B>,
        ty: GlobalType,
        value: Val,
    ) -> Result<Self> {
        Ok(Self { id: B::create_global(&mut store.runtime, ty, value)? })
    }

    /// Returns the current value of the global.
    pub fn get<T, B: RuntimeBackend>(
        &self,
        store: StoreContextMut<'_, T, B>,
    ) -> Val {
        B::get_global(&mut store.runtime, self.id)
    }

    /// Updates the value stored in the global.
    pub fn set<T, B: RuntimeBackend>(
        &self,
        store: StoreContextMut<'_, T, B>,
        value: Val,
    ) -> Result<()> {
        B::set_global(&mut store.runtime, self.id, value)
    }
}

/// Type information for a memory.
pub(crate) struct MemoryType {
    /// Initial size in WASM pages.
    pub(crate) initial: u32,
    /// Maximum size in WASM pages, if any.
    pub(crate) maximum: Option<u32>,
}

impl MemoryType {
    /// Creates a new memory type.
    pub fn new(initial: u32, maximum: Option<u32>) -> Self {
        Self { initial, maximum }
    }
}

impl Memory {
    /// Creates a new memory in `store`.
    pub fn new<T, B: RuntimeBackend>(
        store: StoreContextMut<'_, T, B>,
        ty: MemoryType,
    ) -> Result<Self> {
        Ok(Self { id: B::create_memory(&mut store.runtime, ty)? })
    }

    /// Returns the current contents of the memory.
    pub fn data<'a, T, B: RuntimeBackend>(
        &self,
        store: StoreContext<'a, T, B>,
    ) -> &'a [u8] {
        B::memory_data(&store.runtime, self.id)
    }

    /// Returns mutable access to the memory contents.
    pub fn data_mut<'a, T, B: RuntimeBackend>(
        &self,
        store: StoreContextMut<'a, T, B>,
    ) -> &'a mut [u8] {
        B::memory_data_mut(&mut store.runtime, self.id)
    }

    /// Returns a raw pointer to the memory contents.
    pub fn data_ptr<T, B: RuntimeBackend>(
        &self,
        store: StoreContextMut<'_, T, B>,
    ) -> *mut u8 {
        B::memory_data_ptr(&mut store.runtime, self.id)
    }
}

/// Compiled WASM module.
pub(crate) struct Module<B: RuntimeBackend> {
    /// Backend-specific module representation.
    pub(crate) inner: B::ModuleInner,
    _backend: PhantomData<B>,
}

impl<B: RuntimeBackend> Module<B> {
    /// Compiles a module from raw WASM bytes.
    pub fn from_binary(engine: &Engine, bytes: &[u8]) -> Result<Self> {
        Ok(Self {
            inner: B::module_from_binary(engine, bytes)?,
            _backend: PhantomData,
        })
    }

    /// Deserializes a module from bytes previously returned by [`serialize`].
    ///
    /// Custom runtimes simply rebuild the module from raw WASM bytes, while
    /// the native runtime preserves Wasmtime's unsafe deserialization API.
    pub unsafe fn deserialize(engine: &Engine, bytes: &[u8]) -> Result<Self> {
        Self::from_binary(engine, bytes)
    }
}

/// Instantiated module.
pub(crate) struct Instance<B: RuntimeBackend> {
    /// Backend-specific instance representation.
    pub(crate) inner: B::InstanceInner,
    _backend: PhantomData<B>,
}

impl<B: RuntimeBackend> Instance<B> {
    /// Returns a typed function exported by this instance.
    ///
    /// The `store` argument is unused by the custom runtimes and is kept only
    /// for compatibility with Wasmtime's API.
    pub fn get_typed_func<P, R>(
        &self,
        _store: impl AsContextMut<Backend = B>,
        name: &str,
    ) -> Result<TypedFunc<P, R, B>> {
        Ok(TypedFunc {
            inner: B::get_typed_func_handle::<P, R>(&self.inner, name)?,
            _params: PhantomData,
            _results: PhantomData,
            _backend: PhantomData,
        })
    }
}

/// Typed function exported by an [`Instance`].
pub(crate) struct TypedFunc<P, R, B: RuntimeBackend> {
    inner: B::TypedFuncHandle,
    _params: PhantomData<P>,
    _results: PhantomData<R>,
    _backend: PhantomData<B>,
}

impl<B: RuntimeBackend> TypedFunc<(), i32, B> {
    /// Calls a `() -> i32` function.
    pub fn call<T>(
        &self,
        store: StoreContextMut<'_, T, B>,
        _params: (),
    ) -> Result<i32> {
        B::typed_func_call_i32(store, &self.inner)
    }
}

/// Returns the zero value for `ty`.
pub(crate) fn default_val(ty: ValType) -> Val {
    match ty {
        ValType::I32 => Val::I32(0),
        ValType::I64 => Val::I64(0),
        ValType::F32 => Val::F32(0),
        ValType::F64 => Val::F64(0),
    }
}