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
use std::cell::RefCell;
use std::collections::{HashMap, HashSet, VecDeque};
use std::hash::{Hash, Hasher};
use std::rc::Rc;
use std::{mem, ptr};

#[cfg(test)]
use bstr::BString;

use crate::compiler::{RuleId, Var};
use crate::types::{AclEntry, DeprecationNotice, Func, Type, TypeValue};

/// Trait implemented by types that allow looking up for a symbol.
pub(crate) trait SymbolLookup {
    fn lookup(&self, ident: &str) -> Option<Symbol>;
}

/// Kinds of symbol.
///
/// Used by the compiler to determine how to generate code that
/// accesses the symbol.
#[derive(Clone, Debug)]
pub(crate) enum Symbol {
    /// The symbol refers to a WASM-side variable.
    Var { var: Var, type_value: TypeValue },
    /// The symbol refers to a field in a structure.
    Field {
        /// Index the field occupies in its parent structure.
        index: usize,
        /// `true` if the symbol refers to a field in the root structure. If it
        /// is `false` it refers to the structure whose reference is at the top
        /// of the WASM stack.
        is_root: bool,
        /// Type and value for this field.
        type_value: TypeValue,
        /// Access control list (ACL) for accessing this field.
        acl: Option<Vec<AclEntry>>,
        /// If the field is deprecated, this will contain the message shown when
        /// the field is used in a rule.
        deprecation_notice: Option<DeprecationNotice>,
    },
    /// The symbol refers to a rule.
    Rule {
        rule_id: RuleId,
        /// True if the rule is global.
        is_global: bool,
    },
    /// The symbol refers to a function.
    Func(Rc<Func>),
}

impl Hash for Symbol {
    fn hash<H: Hasher>(&self, state: &mut H) {
        mem::discriminant(self).hash(state);
        match self {
            Symbol::Var { var, .. } => {
                var.hash(state);
            }
            Symbol::Field { index, is_root, .. } => {
                index.hash(state);
                is_root.hash(state);
            }
            Symbol::Rule { rule_id, .. } => {
                rule_id.hash(state);
            }
            Symbol::Func(func) => func.hash(state),
        }
    }
}

impl PartialEq for Symbol {
    fn eq(&self, other: &Self) -> bool {
        match self {
            Symbol::Var { var: this_var, .. } => {
                if let Symbol::Var { var: other_var, .. } = other {
                    this_var == other_var
                } else {
                    false
                }
            }
            Symbol::Field {
                index: this_index, is_root: this_is_root, ..
            } => {
                if let Symbol::Field {
                    index: other_index,
                    is_root: other_is_root,
                    ..
                } = other
                {
                    this_index == other_index && this_is_root == other_is_root
                } else {
                    false
                }
            }
            Symbol::Rule { rule_id: this, .. } => {
                if let Symbol::Rule { rule_id: other, .. } = other {
                    this == other
                } else {
                    false
                }
            }
            Symbol::Func(this) => {
                if let Symbol::Func(other) = other {
                    ptr::eq(&**this, &**other)
                } else {
                    false
                }
            }
        }
    }
}

impl Eq for Symbol {}

impl Symbol {
    pub fn ty(&self) -> Type {
        match &self {
            Symbol::Var { var, .. } => var.ty(),
            Symbol::Field { type_value, .. } => type_value.ty(),
            Symbol::Rule { .. } => Type::Bool,
            Symbol::Func(_) => Type::Func,
        }
    }

    pub fn type_value(&self) -> TypeValue {
        match &self {
            Symbol::Var { type_value, .. } => type_value.clone(),
            Symbol::Field { type_value, .. } => type_value.clone(),
            Symbol::Rule { .. } => TypeValue::unknown_bool(),
            Symbol::Func(func) => TypeValue::Func(func.clone()),
        }
    }

    #[cfg(test)]
    fn as_integer(&self) -> Option<i64> {
        if let TypeValue::Integer { value, .. } = self.type_value() {
            value.extract().cloned()
        } else {
            None
        }
    }

    #[cfg(test)]
    fn as_string(&self) -> Option<BString> {
        if let TypeValue::String { value, .. } = self.type_value() {
            value.extract().map(|s| BString::from(s.as_slice()))
        } else {
            None
        }
    }
}

/// Implements [`SymbolLookup`] for `Option<Symbol>` so that lookup
/// operations can be chained.
///
/// For example, you can do:
///
/// ```text
/// symbol_table.lookup("foo").lookup("bar")
/// ```
///
/// If the field `foo` is a structure, this will return the [`Symbol`]
/// for the field `bar` within that structure.
///
/// This can be done because the `Option<Symbol>` returned by the
/// first call to `lookup` also have a `lookup` method.
impl SymbolLookup for Option<Symbol> {
    fn lookup(&self, ident: &str) -> Option<Symbol> {
        if let Some(symbol) = self {
            if let TypeValue::Struct(s) = symbol.type_value() {
                s.lookup(ident)
            } else {
                None
            }
        } else {
            None
        }
    }
}

/// A symbol table is a structure used for resolving symbols during the
/// compilation process.
///
/// A symbol table is basically a map, where keys are identifiers and
/// values are [`Symbol`] instances that contain information about the
/// type and possibly the current value for that identifier. [`SymbolTable`]
/// implements the [`SymbolLookup`] trait, so symbols are found in the
/// table by using the [`SymbolLookup::lookup`] method.
///
/// When the identifier represents a nested structure, the returned
/// [`Symbol`] will be of type [`Type::Struct`], which encapsulates another
/// object that also implements the [`SymbolLookup`] trait, possibly another
/// [`SymbolTable`].
pub(crate) struct SymbolTable {
    map: HashMap<String, Symbol>,
    used: RefCell<HashSet<String>>,
}

impl SymbolTable {
    /// Creates a new symbol table.
    pub fn new() -> Self {
        Self { map: HashMap::new(), used: RefCell::new(HashSet::new()) }
    }

    /// Inserts a new symbol into the symbol table.
    ///
    /// If the symbol was already in the table it gets updated and the old
    /// value is returned. If the symbol was not in the table [`None`] is
    /// returned.
    pub fn insert<I>(&mut self, ident: I, symbol: Symbol) -> Option<Symbol>
    where
        I: Into<String>,
    {
        self.map.insert(ident.into(), symbol)
    }

    /// Returns true if the symbol table already contains a symbol with
    /// the given identifier.
    #[inline]
    pub fn contains<I>(&self, ident: I) -> bool
    where
        I: AsRef<str>,
    {
        self.map.contains_key(ident.as_ref())
    }

    /// Returns true if a lookup operation for the given identifier resulted
    /// in a symbol being returned.
    #[inline]
    pub fn used<I>(&self, ident: I) -> bool
    where
        I: AsRef<str>,
    {
        self.used.borrow().contains(ident.as_ref())
    }
}

impl Default for SymbolTable {
    fn default() -> Self {
        SymbolTable::new()
    }
}

impl SymbolLookup for &SymbolTable {
    fn lookup(&self, ident: &str) -> Option<Symbol> {
        if let Some(symbol) = self.map.get(ident) {
            self.used.borrow_mut().insert(ident.to_string());
            Some(symbol.clone())
        } else {
            None
        }
    }
}

impl SymbolLookup for SymbolTable {
    fn lookup(&self, ident: &str) -> Option<Symbol> {
        (&self).lookup(ident)
    }
}

impl SymbolLookup for RefCell<SymbolTable> {
    fn lookup(&self, ident: &str) -> Option<Symbol> {
        self.borrow().lookup(ident)
    }
}

/// A set of stacked symbol tables.
///
/// As the name suggests, this type represents a set of symbol tables stacked
/// one on top of each other. The `lookup` operation is performed first on the
/// symbol table at the top of the stack, and if the symbol is not found, it
/// keeps calling the `lookup` function on the next symbol table until the
/// symbol is found, or the bottom of the stack is reached.
///
/// If the symbol table at the top of the stack contains an identifier "foo",
/// it hides any other identifier "foo" that may exist on a symbol table that
/// is deeper in the stack.
pub(crate) struct StackedSymbolTable {
    stack: VecDeque<Rc<RefCell<SymbolTable>>>,
}

impl StackedSymbolTable {
    /// Creates a new [`StackedSymbolTable`].
    pub(crate) fn new() -> Self {
        Self { stack: VecDeque::new() }
    }

    /// Creates a new symbol table and pushes it into the stack.
    pub(crate) fn push_new(&mut self) -> Rc<RefCell<SymbolTable>> {
        let symbol_table = Rc::new(RefCell::new(SymbolTable::new()));
        self.stack.push_back(symbol_table.clone());
        symbol_table
    }

    /// Pushes a new symbol table into the stack.
    pub(crate) fn push(&mut self, symbol_table: Rc<RefCell<SymbolTable>>) {
        self.stack.push_back(symbol_table)
    }

    /// Pop a symbol table from the stack.
    ///
    /// Returns the symbol table removed from the stack or None if the stack
    /// was empty.
    pub(crate) fn pop(&mut self) -> Option<Rc<RefCell<SymbolTable>>> {
        self.stack.pop_back()
    }

    /// Returns the number of symbol tables in the stack.
    #[inline]
    pub(crate) fn len(&self) -> usize {
        self.stack.len()
    }

    /// Removes the symbol tables at the top of the stack,
    /// keeping only the bottom `len`.
    ///
    /// If the stack has more than `len` elements this has no effect.
    #[inline]
    pub(crate) fn truncate(&mut self, len: usize) {
        self.stack.truncate(len)
    }
}

impl SymbolLookup for StackedSymbolTable {
    fn lookup(&self, ident: &str) -> Option<Symbol> {
        // Look for the identifier starting at the top of the stack, and
        // going down the stack until it's found or the bottom of the
        // stack is reached.
        for t in self.stack.iter().rev() {
            let symbol = t.lookup(ident);
            if symbol.is_some() {
                return symbol;
            }
        }
        // The symbol was not found in any of the symbol tables..
        None
    }
}

#[cfg(test)]
#[cfg(feature = "test_proto2-module")]
mod tests {
    use bstr::BString;

    use crate::symbols::SymbolLookup;
    use crate::types::{Struct, Type};

    #[test]
    fn message_lookup() {
        use protobuf::{Enum, MessageFull};

        use crate::modules::protos::test_proto2::TestProto2;
        use crate::modules::protos::test_proto2::test_proto2::Enumeration;
        use crate::modules::protos::test_proto2::test_proto2::Enumeration2;

        let test = Struct::from_proto_descriptor_and_msg(
            &TestProto2::descriptor(),
            None,
            true,
        );

        assert_eq!(test.lookup("int32_zero").unwrap().ty(), Type::Integer);

        assert_eq!(test.lookup("string_foo").unwrap().ty(), Type::String);

        assert_eq!(
            test.lookup("nested").lookup("nested_int32_zero").unwrap().ty(),
            Type::Integer
        );

        assert_eq!(
            test.lookup("Enumeration").lookup("ITEM_1").unwrap().as_integer(),
            Some(Enumeration::ITEM_1.value() as i64)
        );

        assert_eq!(
            test.lookup("items").lookup("ITEM_4").unwrap().as_integer(),
            Some(Enumeration2::ITEM_4.value() as i64)
        );
    }

    #[test]
    fn message_dyn_lookup() {
        use protobuf::{Enum, Message, MessageField, MessageFull};

        use crate::modules::protos::test_proto2::NestedProto2;
        use crate::modules::protos::test_proto2::TestProto2;
        use crate::modules::protos::test_proto2::test_proto2::Enumeration;

        let mut test = TestProto2::new();
        let mut nested = NestedProto2::new();

        test.set_int32_zero(0);
        test.set_int64_zero(0);
        test.set_sint32_zero(0);
        test.set_sint64_zero(0);
        test.set_uint32_zero(0);
        test.set_uint64_zero(0);
        test.set_fixed32_zero(0);
        test.set_fixed64_zero(0);
        test.set_sfixed32_zero(0);
        test.set_sfixed64_zero(0);
        test.set_float_zero(0.0);
        test.set_double_zero(0.0);

        test.set_int32_one(1);
        test.set_int64_one(1);
        test.set_sint32_one(1);
        test.set_sint64_one(1);
        test.set_uint32_one(1);
        test.set_uint64_one(1);
        test.set_fixed32_one(1);
        test.set_fixed64_one(1);
        test.set_sfixed32_one(1);
        test.set_sfixed64_one(1);
        test.set_float_one(1.0);
        test.set_double_one(1.0);

        test.set_string_foo("foo".into());
        test.set_string_bar("bar".into());

        test.set_bytes_foo("foo".into());
        test.set_bytes_bar("bar".into());
        test.set_bytes_raw(b"\x00\x02".into());

        nested.set_nested_int32_zero(0);

        test.nested = MessageField::some(nested);

        let mut buf = Vec::new();
        test.write_to_vec(&mut buf).unwrap();

        let descriptor = TestProto2::descriptor();
        let message = descriptor.parse_from_bytes(buf.as_slice()).unwrap();
        let structure = Struct::from_proto_descriptor_and_msg(
            &descriptor,
            Some(message.as_ref()),
            true,
        );

        assert_eq!(
            structure.lookup("int32_zero").unwrap().as_integer(),
            Some(0)
        );

        assert_eq!(
            structure.lookup("int32_one").unwrap().as_integer(),
            Some(1)
        );

        assert_eq!(
            structure.lookup("string_foo").unwrap().as_string(),
            Some(BString::from(b"foo"))
        );

        assert_eq!(
            structure
                .lookup("nested")
                .lookup("nested_int32_zero")
                .unwrap()
                .as_integer(),
            Some(0)
        );

        assert_eq!(
            structure
                .lookup("Enumeration")
                .lookup("ITEM_1")
                .unwrap()
                .as_integer(),
            Some(Enumeration::ITEM_1.value() as i64)
        );
    }
}