Skip to content

Commit 02969f3

Browse files
committed
refacto: make EvalContext mutable in modules
The memory object will be mutable in it. Make this change now to avoid polluting the diff of the next commit.
1 parent f2a7355 commit 02969f3

File tree

12 files changed

+134
-134
lines changed

12 files changed

+134
-134
lines changed

boreal/src/compiler/module.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub enum ModuleExpressionKind {
5050
/// A value coming from a function exposed by a module.
5151
StaticFunction {
5252
/// The function to call.
53-
fun: fn(&EvalContext, Vec<Value>) -> Option<Value>,
53+
fun: fn(&mut EvalContext, Vec<Value>) -> Option<Value>,
5454
},
5555
}
5656

@@ -622,7 +622,7 @@ mod tests {
622622
use crate::test_helpers::test_type_traits_non_clonable;
623623

624624
#[cfg_attr(coverage_nightly, coverage(off))]
625-
fn test_fun(_ctx: &EvalContext, args: Vec<Value>) -> Option<Value> {
625+
fn test_fun(_ctx: &mut EvalContext, args: Vec<Value>) -> Option<Value> {
626626
drop(args);
627627
None
628628
}

boreal/src/evaluator/module.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ pub(super) fn evaluate_expr(
102102
.take(*nb_arguments)
103103
.map(expr_value_to_module_value)
104104
.collect();
105-
let eval_ctx = EvalContext {
105+
let mut eval_ctx = EvalContext {
106106
mem: &evaluator.scan_data.mem,
107107
module_data: &evaluator.scan_data.module_values.data_map,
108108
process_memory: evaluator.scan_data.params.process_memory,
109109
};
110-
let value = fun(&eval_ctx, arguments).ok_or(PoisonKind::Undefined)?;
110+
let value = fun(&mut eval_ctx, arguments).ok_or(PoisonKind::Undefined)?;
111111
evaluate_ops(evaluator, &value, ops, expressions)
112112
}
113113
}
@@ -153,12 +153,12 @@ fn evaluate_ops(
153153
.take(*nb_arguments)
154154
.map(expr_value_to_module_value)
155155
.collect();
156-
let eval_ctx = EvalContext {
156+
let mut eval_ctx = EvalContext {
157157
mem: &evaluator.scan_data.mem,
158158
module_data: &evaluator.scan_data.module_values.data_map,
159159
process_memory: evaluator.scan_data.params.process_memory,
160160
};
161-
let new_value = fun(&eval_ctx, arguments).ok_or(PoisonKind::Undefined)?;
161+
let new_value = fun(&mut eval_ctx, arguments).ok_or(PoisonKind::Undefined)?;
162162
// Avoid cloning the value if possible
163163
return if operations.peek().is_none() {
164164
Ok(new_value)

boreal/src/module/elf.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ fn get_symbols<Elf: FileHeader>(
568568

569569
impl Elf {
570570
#[cfg(feature = "hash")]
571-
fn import_md5(ctx: &EvalContext, _: Vec<Value>) -> Option<Value> {
571+
fn import_md5(ctx: &mut EvalContext, _: Vec<Value>) -> Option<Value> {
572572
use md5::{Digest, Md5};
573573

574574
let data = ctx.module_data.get::<Self>()?;
@@ -581,7 +581,7 @@ impl Elf {
581581
}
582582

583583
#[cfg(feature = "hash")]
584-
fn telfhash(ctx: &EvalContext, _: Vec<Value>) -> Option<Value> {
584+
fn telfhash(ctx: &mut EvalContext, _: Vec<Value>) -> Option<Value> {
585585
const EXCLUDED_STRINGS: &[&[u8]; 8] = &[
586586
b"__libc_start_main",
587587
b"main",

boreal/src/module/hash.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn compute_hash<D: Digest>(bytes: &[u8]) -> Value {
8787
}
8888

8989
impl Hash {
90-
fn md5(ctx: &EvalContext, args: Vec<Value>) -> Option<Value> {
90+
fn md5(ctx: &mut EvalContext, args: Vec<Value>) -> Option<Value> {
9191
match get_args(args)? {
9292
Args::Bytes(s) => Some(compute_hash::<Md5>(&s)),
9393
Args::Range(offset, end) => {
@@ -111,7 +111,7 @@ impl Hash {
111111
}
112112
}
113113

114-
fn sha1(ctx: &EvalContext, args: Vec<Value>) -> Option<Value> {
114+
fn sha1(ctx: &mut EvalContext, args: Vec<Value>) -> Option<Value> {
115115
match get_args(args)? {
116116
Args::Bytes(s) => Some(compute_hash::<Sha1>(&s)),
117117
Args::Range(offset, end) => {
@@ -135,7 +135,7 @@ impl Hash {
135135
}
136136
}
137137

138-
fn sha2(ctx: &EvalContext, args: Vec<Value>) -> Option<Value> {
138+
fn sha2(ctx: &mut EvalContext, args: Vec<Value>) -> Option<Value> {
139139
match get_args(args)? {
140140
Args::Bytes(s) => Some(compute_hash::<Sha256>(&s)),
141141
Args::Range(offset, end) => {
@@ -159,7 +159,7 @@ impl Hash {
159159
}
160160
}
161161

162-
fn checksum32(ctx: &EvalContext, args: Vec<Value>) -> Option<Value> {
162+
fn checksum32(ctx: &mut EvalContext, args: Vec<Value>) -> Option<Value> {
163163
apply(ctx, args, |s| {
164164
let checksum = s
165165
.iter()
@@ -168,7 +168,7 @@ impl Hash {
168168
})
169169
}
170170

171-
fn crc32(ctx: &EvalContext, args: Vec<Value>) -> Option<Value> {
171+
fn crc32(ctx: &mut EvalContext, args: Vec<Value>) -> Option<Value> {
172172
apply(ctx, args, |s| {
173173
let crc = crc32fast::hash(s);
174174
Value::Integer(i64::from(crc))
@@ -201,7 +201,7 @@ fn get_args(args: Vec<Value>) -> Option<Args> {
201201
}
202202
}
203203

204-
fn apply<F>(ctx: &EvalContext, args: Vec<Value>, fun: F) -> Option<Value>
204+
fn apply<F>(ctx: &mut EvalContext, args: Vec<Value>, fun: F) -> Option<Value>
205205
where
206206
F: FnOnce(&[u8]) -> Value,
207207
{

boreal/src/module/macho.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ impl ModuleData for MachO {
721721
}
722722

723723
impl MachO {
724-
fn file_index_for_arch(ctx: &EvalContext, args: Vec<Value>) -> Option<Value> {
724+
fn file_index_for_arch(ctx: &mut EvalContext, args: Vec<Value>) -> Option<Value> {
725725
let mut args = args.into_iter();
726726
let v1: i64 = args.next()?.try_into().ok()?;
727727
let v2 = args.next().and_then(|v| i64::try_from(v).ok());
@@ -742,7 +742,7 @@ impl MachO {
742742
None
743743
}
744744

745-
fn entry_point_for_arch(ctx: &EvalContext, args: Vec<Value>) -> Option<Value> {
745+
fn entry_point_for_arch(ctx: &mut EvalContext, args: Vec<Value>) -> Option<Value> {
746746
let mut args = args.into_iter();
747747
let v1: i64 = args.next()?.try_into().ok()?;
748748
let v2 = args.next().and_then(|v| i64::try_from(v).ok());

0 commit comments

Comments
 (0)