Skip to content

Commit

Permalink
fix: bug in the code emitted by emit_check_for_rule_match
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Jul 11, 2023
1 parent 02a225e commit 1147a21
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions yara-x/src/compiler/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,15 +944,15 @@ fn emit_check_for_rule_match(
offset: MATCHING_RULES_BITMAP_BASE as u32,
},
);
// This is the first operator for the I32ShrU operation.
instr.i32_const(rule_id.0 % 8);

// Compute byte & (1 << (rule_id % 8)), which clears all
// bits except the one we are interested in.
instr.i32_const(1 << (rule_id.0 % 8));
instr.binop(BinaryOp::I32And);
// Now shift the byte to the right, leaving the
// interesting bit as the LSB. So the result is either
// 1 or 0.
instr.i32_const(rule_id.0 % 8);
instr.binop(BinaryOp::I32ShrU);
}

Expand Down
25 changes: 25 additions & 0 deletions yara-x/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,31 @@ fn rule_reuse() {
assert_eq!(scanner.scan(&[]).matching_rules().len(), 9);
}

#[test]
fn rule_reuse_2() {
let rules = crate::compile(
r#"
rule rule_1 {
condition:
true
}
rule rule_2 {
condition:
false
}
rule rule_3 {
condition:
rule_1 and not rule_2
}
"#,
)
.unwrap();

let mut scanner = crate::scanner::Scanner::new(&rules);

assert_eq!(scanner.scan(&[]).matching_rules().len(), 2);
}

#[test]
fn test_defined_1() {
condition_true!(r#"defined 1"#);
Expand Down

0 comments on commit 1147a21

Please sign in to comment.