Skip to content

Commit

Permalink
fix: use an IndexMap instead of HashMap for storing rule patterns
Browse files Browse the repository at this point in the history
This allows iterating over the patterns in the order they are declared in the rule.
  • Loading branch information
plusvic committed Sep 12, 2023
1 parent cef8577 commit df5c64c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
15 changes: 12 additions & 3 deletions yara-x/src/compiler/context.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::collections::VecDeque;
use std::hash::BuildHasherDefault;
use std::mem::size_of;
use std::rc::Rc;

use rustc_hash::FxHashMap;
use rustc_hash::{FxHashMap, FxHasher};
use walrus::ir::InstrSeqId;
use walrus::{FunctionId, ValType};
use yara_x_parser::report::ReportBuilder;
Expand Down Expand Up @@ -49,8 +50,16 @@ pub(in crate::compiler) struct Context<'a, 'src, 'sym> {
/// Rule that is being compiled.
pub current_rule: &'a RuleInfo,

/// IR nodes for patterns defined in the rule being compiled.
pub current_rule_patterns: &'a mut FxHashMap<PatternId, ir::Pattern<'src>>,
/// IR nodes for patterns defined in the rule being compiled. This is an
/// [`IndexMap`] where keys are [`PatternId`]s and values are
/// [`ir::Pattern`]. A `IndexMap` is used instead of `HashMap` because
/// we want to be able to iterate over the patterns in the order they
/// were defined in the rule.
pub current_rule_patterns: &'a mut indexmap::IndexMap<
PatternId,
ir::Pattern<'src>,
BuildHasherDefault<FxHasher>,
>,

/// Warnings generated during the compilation.
pub warnings: &'a mut Vec<Warning>,
Expand Down
3 changes: 1 addition & 2 deletions yara-x/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,7 @@ impl<'a> Compiler<'a> {
let mut ident_and_pattern_ids = Vec::with_capacity(num_patterns);

// Create a map (IdentId, Pattern).
let mut patterns_map: FxHashMap<PatternId, Pattern> =
FxHashMap::default();
let mut patterns_map = indexmap::IndexMap::default();

for (pattern_id, pattern) in
iter::zip(self.next_pattern_id.successors(), patterns)
Expand Down

0 comments on commit df5c64c

Please sign in to comment.