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
//! Browser runtime implemented on top of the host WebAssembly API.
//!
//! This backend stores a Rust-side view of globals and memories while the
//! actual execution happens in the browser's WebAssembly runtime.

use anyhow::{Result, anyhow};
use js_sys::{
    Array, BigInt, Function, Object, Reflect, Uint8Array, WebAssembly,
};
use wasm_bindgen::closure::Closure;
use wasm_bindgen::{JsCast, JsValue};

use super::common::{self, RuntimeBackend};

/// Browser runtime backend.
#[derive(Clone, Default)]
pub(crate) struct Backend;

pub(crate) use super::common::{
    AsContext, AsContextMut, Config, Engine, OptLevel,
};
/// Alias for [`common::Caller`] specialized for the browser backend.
pub(crate) type Caller<'a, T> = common::Caller<'a, T, Backend>;
/// Alias for [`common::Instance`] specialized for the browser backend.
pub(crate) type Instance = common::Instance<Backend>;
/// Alias for [`common::Linker`] specialized for the browser backend.
pub(crate) type Linker<T> = common::Linker<T, Backend>;
/// Alias for [`common::Memory`] specialized for the browser backend.
pub(crate) type Memory = common::Memory;
/// Alias for [`common::Module`] specialized for the browser backend.
pub(crate) type Module = common::Module<Backend>;
/// Alias for [`common::Store`] specialized for the browser backend.
pub(crate) type Store<T> = common::Store<T, Backend>;
/// Alias for [`common::TypedFunc`] specialized for the browser backend.
pub(crate) type TypedFunc<P, R> = common::TypedFunc<P, R, Backend>;

/// Shared Wasmtime-like runtime types used by the browser backend.
pub(crate) use super::common::{
    Extern, FuncType, Global, GlobalType, MemoryType, Mutability, Val, ValRaw,
    ValType,
};

struct GlobalInner {
    val_type: ValType,
    mutability: Mutability,
    js_global: WebAssembly::Global,
}

struct MemoryInner {
    js_memory: WebAssembly::Memory,
    cache: Vec<u8>,
}

/// Browser-specific runtime state stored alongside each [`Store`].
#[derive(Default)]
pub(crate) struct RuntimeState {
    globals: Vec<GlobalInner>,
    memories: Vec<MemoryInner>,
    import_callbacks:
        Vec<Closure<dyn FnMut(JsValue, JsValue, JsValue, JsValue) -> JsValue>>,
}

impl RuntimeState {
    fn sync_memory_from_js(&mut self) {
        // The browser runtime owns the actual linear memory. Keep the Rust-side
        // cache aligned with it before Rust reads from memory-backed state.
        for memory in &mut self.memories {
            let js = Uint8Array::new(&memory.js_memory.buffer());
            if memory.cache.len() != js.length() as usize {
                memory.cache.resize(js.length() as usize, 0);
            }
            js.copy_to(memory.cache.as_mut_slice());
        }
    }

    fn sync_memory_to_js(&mut self) {
        // Writes performed through the Rust-side cache must be copied back into
        // the browser's WebAssembly memory before guest code observes them.
        for memory in &mut self.memories {
            let js = Uint8Array::new(&memory.js_memory.buffer());
            if memory.cache.len() != js.length() as usize {
                memory.cache.resize(js.length() as usize, 0);
            }
            js.copy_from(memory.cache.as_slice());
        }
    }
}

impl RuntimeBackend for Backend {
    type RuntimeState = RuntimeState;
    type ModuleInner = Vec<u8>;
    type InstanceInner = WebAssembly::Instance;
    type TypedFuncHandle = Function;

    fn set_epoch_deadline(_runtime: &mut Self::RuntimeState, _deadline: u64) {}

    fn prepare_for_instantiation(runtime: &mut Self::RuntimeState) {
        runtime.import_callbacks.clear();
    }

    fn reset_for_store_reuse(runtime: &mut Self::RuntimeState) {
        runtime.import_callbacks.clear();
        runtime.globals.clear();
        runtime.memories.clear();
    }

    fn create_global(
        runtime: &mut Self::RuntimeState,
        ty: GlobalType,
        value: Val,
    ) -> Result<usize> {
        let descriptor = Object::new();
        Reflect::set(
            &descriptor,
            &JsValue::from_str("value"),
            &JsValue::from_str(val_type_name(ty.val_type)),
        )
        .map_err(js_error)?;
        Reflect::set(
            &descriptor,
            &JsValue::from_str("mutable"),
            &JsValue::from_bool(matches!(ty.mutability, Mutability::Var)),
        )
        .map_err(js_error)?;

        let js_global =
            WebAssembly::Global::new(&descriptor, &val_to_js(value))
                .map_err(js_error)?;

        runtime.globals.push(GlobalInner {
            val_type: ty.val_type,
            mutability: ty.mutability,
            js_global,
        });

        Ok(runtime.globals.len() - 1)
    }

    fn get_global(runtime: &mut Self::RuntimeState, id: usize) -> Val {
        let inner = &runtime.globals[id];
        js_to_val(&inner.js_global.value(), inner.val_type)
            .unwrap_or_else(|_| common::default_val(inner.val_type))
    }

    fn set_global(
        runtime: &mut Self::RuntimeState,
        id: usize,
        value: Val,
    ) -> Result<()> {
        let inner = &runtime.globals[id];
        if !matches!(inner.mutability, Mutability::Var) {
            return Err(anyhow!("attempted to set immutable global"));
        }
        inner.js_global.set_value(&val_to_js(value));
        Ok(())
    }

    fn create_memory(
        runtime: &mut Self::RuntimeState,
        ty: MemoryType,
    ) -> Result<usize> {
        let descriptor = Object::new();
        Reflect::set(
            &descriptor,
            &JsValue::from_str("initial"),
            &JsValue::from_f64(ty.initial as f64),
        )
        .map_err(js_error)?;
        if let Some(max) = ty.maximum {
            Reflect::set(
                &descriptor,
                &JsValue::from_str("maximum"),
                &JsValue::from_f64(max as f64),
            )
            .map_err(js_error)?;
        }

        let js_memory =
            WebAssembly::Memory::new(&descriptor).map_err(js_error)?;
        let cache = vec![0_u8; ty.initial as usize * 65_536];

        runtime.memories.push(MemoryInner { js_memory, cache });
        Ok(runtime.memories.len() - 1)
    }

    fn memory_data<'a>(
        runtime: &'a Self::RuntimeState,
        id: usize,
    ) -> &'a [u8] {
        runtime.memories[id].cache.as_slice()
    }

    fn memory_data_mut<'a>(
        runtime: &'a mut Self::RuntimeState,
        id: usize,
    ) -> &'a mut [u8] {
        runtime.memories[id].cache.as_mut_slice()
    }

    fn memory_data_ptr(
        runtime: &mut Self::RuntimeState,
        id: usize,
    ) -> *mut u8 {
        runtime.memories[id].cache.as_mut_ptr()
    }

    fn module_from_binary(
        _engine: &Engine,
        bytes: &[u8],
    ) -> Result<Self::ModuleInner> {
        // Validate eagerly so errors surface at compile/load time while still
        // keeping the original bytes for later instantiation and serialization.
        let wasm = Uint8Array::from(bytes);
        let _ = WebAssembly::Module::new(&wasm.into()).map_err(js_error)?;
        Ok(bytes.to_vec())
    }

    fn instantiate<T: 'static>(
        store: &mut Store<T>,
        linker: &Linker<T>,
        module: &Module,
    ) -> Result<Self::InstanceInner> {
        let imports = Object::new();

        for defined in &linker.externs {
            let ns = ensure_namespace(&imports, &defined.module)?;
            let value: JsValue = match defined.value {
                Extern::Global(global) => {
                    store.runtime.globals[global.id].js_global.clone().into()
                }
                Extern::Memory(memory) => {
                    store.runtime.memories[memory.id].js_memory.clone().into()
                }
            };

            Reflect::set(
                &ns,
                &JsValue::from_str(defined.name.as_str()),
                &value,
            )
            .map_err(js_error)?;
        }

        let store_ptr = store as *mut Store<T>;

        for import in &linker.functions {
            let ns = ensure_namespace(&imports, import.module.as_str())?;
            let params = import.ty.params.clone();
            let results = import.ty.results.clone();
            let sync_flags = import.sync_flags;
            let trampoline = std::sync::Arc::clone(&import.trampoline);

            let callback = Closure::wrap(Box::new(
                move |a0: JsValue,
                      a1: JsValue,
                      a2: JsValue,
                      a3: JsValue|
                      -> JsValue {
                    let store = unsafe { &mut *store_ptr };

                    if common::should_sync_before(sync_flags) {
                        store.runtime.sync_memory_from_js();
                    }

                    let mut args_and_results = vec![
                        ValRaw::default();
                        common::callback_storage_len(&params, &results)
                    ];

                    let incoming = [a0, a1, a2, a3];

                    for (idx, ty) in params.iter().enumerate() {
                        args_and_results[idx] =
                            js_to_valraw(&incoming[idx], *ty)
                                .unwrap_or_default();
                    }

                    let caller = Caller::new(store);

                    if trampoline(caller, &mut args_and_results).is_err() {
                        return JsValue::UNDEFINED;
                    }

                    if common::should_sync_after(sync_flags) {
                        store.runtime.sync_memory_to_js();
                    }

                    match results.len() {
                        0 => JsValue::UNDEFINED,
                        1 => valraw_to_js(args_and_results[0], results[0]),
                        _ => {
                            let js = Array::new();
                            for (idx, ty) in results.iter().enumerate() {
                                js.push(&valraw_to_js(
                                    args_and_results[idx],
                                    *ty,
                                ));
                            }
                            js.into()
                        }
                    }
                },
            )
                as Box<
                    dyn FnMut(JsValue, JsValue, JsValue, JsValue) -> JsValue,
                >);

            Reflect::set(
                &ns,
                &JsValue::from_str(import.name.as_str()),
                callback.as_ref().unchecked_ref::<Function>(),
            )
            .map_err(js_error)?;

            // Keep the closure alive for at least as long as the instance. If
            // this is dropped, the browser may garbage-collect the callback
            // even though the instantiated module still imports it.
            store.runtime.import_callbacks.push(callback);
        }

        let bytes = Uint8Array::from(module.inner.as_slice());
        let js_module =
            WebAssembly::Module::new(&bytes.into()).map_err(js_error)?;
        WebAssembly::Instance::new(&js_module, &imports).map_err(js_error)
    }

    fn get_typed_func_handle<P, R>(
        instance: &Self::InstanceInner,
        name: &str,
    ) -> Result<Self::TypedFuncHandle> {
        let exports = instance.exports();
        let value = Reflect::get(&exports, &JsValue::from_str(name))
            .map_err(js_error)?;
        value
            .dyn_into::<Function>()
            .map_err(|_| anyhow!("export `{name}` is not a function"))
    }

    fn typed_func_call_i32<T>(
        store: &mut Store<T>,
        func: &Self::TypedFuncHandle,
    ) -> Result<i32> {
        // Guest code reads and writes the browser-owned memory directly, so
        // copy the cache in before the call and back out afterwards.
        store.runtime.sync_memory_to_js();

        let value = func.call0(&JsValue::UNDEFINED).map_err(js_error)?;

        store.runtime.sync_memory_from_js();

        if let Some(v) = value.as_f64() {
            return Ok(v as i32);
        }

        Err(anyhow!("main returned a non-number"))
    }
}

fn ensure_namespace(root: &Object, module_name: &str) -> Result<Object> {
    let key = JsValue::from_str(module_name);

    let existing = Reflect::get(root, &key).map_err(js_error)?;
    if existing.is_object() {
        return Ok(existing.unchecked_into::<Object>());
    }

    let namespace = Object::new();
    Reflect::set(root, &key, &namespace).map_err(js_error)?;
    Ok(namespace)
}

fn js_to_valraw(value: &JsValue, ty: ValType) -> Result<ValRaw> {
    Ok(match ty {
        ValType::I32 => ValRaw::i32(value.as_f64().unwrap_or(0.0) as i32),
        ValType::I64 => ValRaw::i64(js_to_i64(value)?),
        ValType::F32 => {
            ValRaw::f32((value.as_f64().unwrap_or(0.0) as f32).to_bits())
        }
        ValType::F64 => ValRaw::f64(value.as_f64().unwrap_or(0.0).to_bits()),
    })
}

fn valraw_to_js(value: ValRaw, ty: ValType) -> JsValue {
    match ty {
        ValType::I32 => JsValue::from_f64(value.get_i32() as f64),
        ValType::I64 => BigInt::from(value.get_i64()).into(),
        ValType::F32 => {
            JsValue::from_f64(f32::from_bits(value.get_f32()) as f64)
        }
        ValType::F64 => JsValue::from_f64(f64::from_bits(value.get_f64())),
    }
}

fn val_to_js(value: Val) -> JsValue {
    match value {
        Val::I32(v) => JsValue::from_f64(v as f64),
        Val::I64(v) => BigInt::from(v).into(),
        Val::F32(v) => JsValue::from_f64(f32::from_bits(v) as f64),
        Val::F64(v) => JsValue::from_f64(f64::from_bits(v)),
    }
}

fn js_to_val(value: &JsValue, ty: ValType) -> Result<Val> {
    Ok(match ty {
        ValType::I32 => Val::I32(value.as_f64().unwrap_or(0.0) as i32),
        ValType::I64 => Val::I64(js_to_i64(value)?),
        ValType::F32 => {
            Val::F32((value.as_f64().unwrap_or(0.0) as f32).to_bits())
        }
        ValType::F64 => Val::F64(value.as_f64().unwrap_or(0.0).to_bits()),
    })
}

fn js_to_i64(value: &JsValue) -> Result<i64> {
    if let Some(v) = value.as_f64() {
        return Ok(v as i64);
    }

    // `wasm-bindgen` represents `i64` values as `BigInt` in JS.
    let bigint = BigInt::from(value.clone());
    let text: String =
        bigint.to_string(10).map_err(|err| js_error(err.into()))?.into();

    text.parse::<i64>()
        .map_err(|err| anyhow!("invalid i64 value `{text}`: {err}"))
}

fn val_type_name(ty: ValType) -> &'static str {
    match ty {
        ValType::I32 => "i32",
        ValType::I64 => "i64",
        ValType::F32 => "f32",
        ValType::F64 => "f64",
    }
}

fn js_error(err: JsValue) -> anyhow::Error {
    if let Some(message) = err.as_string() {
        anyhow!("{message}")
    } else {
        anyhow!("{err:?}")
    }
}