Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set_module_data api #143

Merged
merged 3 commits into from
Apr 30, 2024
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
16 changes: 9 additions & 7 deletions boreal/src/evaluator/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ use crate::compiler::module::{
BoundedValueIndex, ModuleExpression, ModuleExpressionKind, ModuleOperations, ValueOperation,
};
use crate::memory::Region;
use crate::module::{EvalContext, Module, ModuleDataMap, ScanContext, Value as ModuleValue};
use crate::module::{
EvalContext, Module, ModuleDataMap, ModuleUserData, ScanContext, Value as ModuleValue,
};

use super::{Evaluator, PoisonKind, Value};

#[derive(Debug)]
pub struct EvalData {
pub struct EvalData<'scanner> {
pub values: Vec<(&'static str, ModuleValue)>,
pub data_map: ModuleDataMap,
pub data_map: ModuleDataMap<'scanner>,
}

impl EvalData {
pub fn new(modules: &[Box<dyn Module>]) -> Self {
let mut data_map = ModuleDataMap::default();
impl<'scanner> EvalData<'scanner> {
pub fn new(modules: &[Box<dyn Module>], user_data: &'scanner ModuleUserData) -> Self {
let mut data_map = ModuleDataMap::new(user_data);

let values = modules
.iter()
Expand Down Expand Up @@ -216,7 +218,7 @@ mod tests {
fn test_types_traits() {
test_type_traits_non_clonable(EvalData {
values: Vec::new(),
data_map: ModuleDataMap::default(),
data_map: ModuleDataMap::new(&HashMap::new()),
});
}
}
3 changes: 2 additions & 1 deletion boreal/src/module/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ pub struct Data {
}

impl ModuleData for Console {
type Data = Data;
type PrivateData = Data;
type UserData = ();
}

impl Console {
Expand Down
3 changes: 2 additions & 1 deletion boreal/src/module/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ impl Data {
}

impl ModuleData for Dex {
type Data = Data;
type PrivateData = Data;
type UserData = ();
}

fn parse_file(mem: &[u8], data: &mut Data) -> Option<HashMap<&'static str, Value>> {
Expand Down
3 changes: 2 additions & 1 deletion boreal/src/module/dotnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ pub struct Data {
}

impl ModuleData for Dotnet {
type Data = Data;
type PrivateData = Data;
type UserData = ();
}

fn parse_file<HEADERS: ImageNtHeaders>(
Expand Down
3 changes: 2 additions & 1 deletion boreal/src/module/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ impl Module for Elf {
}

impl ModuleData for Elf {
type Data = Data;
type PrivateData = Data;
type UserData = ();
}

#[derive(Default)]
Expand Down
3 changes: 2 additions & 1 deletion boreal/src/module/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ pub struct Cache {
}

impl ModuleData for Hash {
type Data = Data;
type PrivateData = Data;
type UserData = ();
}

fn compute_hash_from_bytes<D: Digest>(bytes: &[u8]) -> Value {
Expand Down
3 changes: 2 additions & 1 deletion boreal/src/module/macho.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,8 @@ struct FileData {
}

impl ModuleData for MachO {
type Data = Data;
type PrivateData = Data;
type UserData = ();
}

impl MachO {
Expand Down
3 changes: 2 additions & 1 deletion boreal/src/module/magic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ pub enum CacheEntry {
}

impl ModuleData for Magic {
type Data = Data;
type PrivateData = Data;
type UserData = ();
}

impl Magic {
Expand Down
43 changes: 28 additions & 15 deletions boreal/src/module/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,18 +627,19 @@ mod tests {
use super::*;

macro_rules! ctx {
() => {
($v:expr) => {
EvalContext {
mem: &mut Memory::Direct(b""),
module_data: &ModuleDataMap::default(),
module_data: &ModuleDataMap::new($v),
process_memory: false,
}
};
}

#[test]
fn test_in_range_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::in_range(&mut ctx, vec![]).is_none());
assert!(Math::in_range(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -650,7 +651,8 @@ mod tests {

#[test]
fn test_deviation_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::deviation(&mut ctx, vec![]).is_none());
assert!(Math::deviation(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -664,7 +666,8 @@ mod tests {

#[test]
fn test_mean_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::mean(&mut ctx, vec![]).is_none());
assert!(Math::mean(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -674,7 +677,8 @@ mod tests {

#[test]
fn test_serial_correlation_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::serial_correlation(&mut ctx, vec![]).is_none());
assert!(Math::serial_correlation(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -684,7 +688,8 @@ mod tests {

#[test]
fn test_monte_carlo_pi_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::monte_carlo_pi(&mut ctx, vec![]).is_none());
assert!(Math::monte_carlo_pi(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -694,7 +699,8 @@ mod tests {

#[test]
fn test_entropy_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::entropy(&mut ctx, vec![]).is_none());
assert!(Math::entropy(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -704,7 +710,8 @@ mod tests {

#[test]
fn test_min_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::min(&mut ctx, vec![]).is_none());
assert!(Math::min(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -714,7 +721,8 @@ mod tests {

#[test]
fn test_max_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::max(&mut ctx, vec![]).is_none());
assert!(Math::max(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -724,23 +732,26 @@ mod tests {

#[test]
fn test_to_number_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::to_number(&mut ctx, vec![]).is_none());
assert!(Math::to_number(&mut ctx, vec![0.into()]).is_none());
}

#[test]
fn test_abs_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::abs(&mut ctx, vec![]).is_none());
assert!(Math::abs(&mut ctx, vec![0.5.into()]).is_none());
}

#[test]
fn test_count_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::count(&mut ctx, vec![]).is_none());
assert!(Math::count(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -749,7 +760,8 @@ mod tests {

#[test]
fn test_percentage_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::percentage(&mut ctx, vec![]).is_none());
assert!(Math::percentage(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -758,7 +770,8 @@ mod tests {

#[test]
fn test_mode_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::mode(&mut ctx, vec![0.5.into()]).is_none());
}
Expand Down
Loading
Loading