Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 141 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,15 @@ protobuf-parse = { workspace = true, optional = true }
yara-x-proto = { workspace = true, optional = true }

[dev-dependencies]
criterion = "0.5"
globwalk = { workspace = true }
goldenfile = { workspace = true }
ihex = { workspace = true }
pretty_assertions = { workspace = true }
rayon = { workspace = true }
yara-x-proto-yaml = { workspace = true }
zip = { workspace = true }
zip = { workspace = true }

[[bench]]
name = "dex"
harness = false
41 changes: 41 additions & 0 deletions lib/benches/commons/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::fs::File;
use std::io::Read;
use std::path::Path;

pub fn create_binary_from_ihex(ihex: &str) -> Vec<u8> {
let mut reader = ihex::Reader::new(ihex);
let mut data = Vec::new();
while let Some(Ok(record)) = reader.next() {
if let ihex::Record::Data { value, .. } = record {
data.extend(value);
}
}
data
}

pub fn create_binary_from_zipped_ihex<P: AsRef<Path>>(path: P) -> Vec<u8> {
let path = path.as_ref();
let f = File::open(path)
.unwrap_or_else(|_| panic!("can not open file: {:?}", &path));

let mut zip = zip::ZipArchive::new(f)
.unwrap_or_else(|_| panic!("can not unzip file: {:?}", &path));

let path_without_zip_ext = path.with_extension("");
let inner_file_name =
path_without_zip_ext.file_name().unwrap().to_str().unwrap();

let mut inner_file = zip.by_name(inner_file_name).unwrap_or_else(|_| {
panic!(
"ZIP archive {:?} doesn't contain file: {:?}",
&path, &inner_file_name
)
});

let mut ihex = String::new();
inner_file.read_to_string(&mut ihex).unwrap_or_else(|_| {
panic!("can not read ihex content from : {:?}", &path)
});

create_binary_from_ihex(ihex.as_str())
}
24 changes: 24 additions & 0 deletions lib/benches/dex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

mod commons;

use commons::create_binary_from_zipped_ihex;

fn bench_dex(c: &mut Criterion) {
let data = create_binary_from_zipped_ihex(
"src/modules/dex/tests/testdata/c14c75d58399825287e0ee0fcfede6ec06f93489fb52f70bca2736fae5fceab2.in.zip",
);

let mut group = c.benchmark_group("dex");

group.bench_function("parse", |b| {
b.iter(|| {
let _ = black_box(yara_x::mods::invoke::<yara_x::mods::Dex>(black_box(&data)));
});
});

group.finish();
}

criterion_group!(benches, bench_dex);
criterion_main!(benches);
5 changes: 4 additions & 1 deletion lib/src/modules/dex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ thread_local!(

#[module_main]
fn main(data: &[u8], _meta: Option<&[u8]>) -> Result<Dex, ModuleError> {
CHECKSUM_CACHE.with(|cache| *cache.borrow_mut() = None);
SIGNATURE_CACHE.with(|cache| *cache.borrow_mut() = None);

match parser::Dex::parse(data) {
Ok(dex) => Ok(dex.into()),
Err(_) => {
Expand Down Expand Up @@ -135,7 +138,7 @@ fn contains_method(
Err(_) => return None,
};

Some(dex.methods.binary_search_by(|item| item.name.cmp(&str)).is_ok())
Some(dex.methods.iter().any(|item| item.name.as_deref() == str.as_deref()))
}

/// Function that checks whether the DEX file contains the specified class
Expand Down
3 changes: 2 additions & 1 deletion lib/src/modules/dex/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ fn methods() {
import "dex"
rule test {
condition:
dex.contains_method("getPackageName")
dex.contains_method("getPackageName") and
dex.contains_method("loadLibrary")
}
"#,
&dex
Expand Down
Loading