From ec42f0274dabccf25331555805053203e130906a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Mei=C3=9Fner?= Date: Fri, 12 Apr 2024 10:37:13 +0200 Subject: [PATCH] Fix - JIT randomized start padding length (#556) * Adds MAX_START_PADDING_LENGTH. * Make cargo clippy happy. --- src/jit.rs | 5 +++-- src/static_analysis.rs | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/jit.rs b/src/jit.rs index 798a543b..7a6d676c 100644 --- a/src/jit.rs +++ b/src/jit.rs @@ -28,6 +28,7 @@ use crate::{ const MAX_EMPTY_PROGRAM_MACHINE_CODE_LENGTH: usize = 4096; const MAX_MACHINE_CODE_LENGTH_PER_INSTRUCTION: usize = 110; const MACHINE_CODE_PER_INSTRUCTION_METER_CHECKPOINT: usize = 13; +const MAX_START_PADDING_LENGTH: usize = 256; pub struct JitProgram { /// OS page size in bytes and the alignment of the sections @@ -340,7 +341,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> { } } - let mut code_length_estimate = MAX_EMPTY_PROGRAM_MACHINE_CODE_LENGTH + MAX_MACHINE_CODE_LENGTH_PER_INSTRUCTION * pc; + let mut code_length_estimate = MAX_EMPTY_PROGRAM_MACHINE_CODE_LENGTH + MAX_START_PADDING_LENGTH + MAX_MACHINE_CODE_LENGTH_PER_INSTRUCTION * pc; if config.noop_instruction_rate != 0 { code_length_estimate += code_length_estimate / config.noop_instruction_rate as usize; } @@ -377,7 +378,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> { // Randomized padding at the start before random intervals begin if self.config.noop_instruction_rate != 0 { - for _ in 0..self.diversification_rng.gen_range(0..self.config.noop_instruction_rate) { + for _ in 0..self.diversification_rng.gen_range(0..MAX_START_PADDING_LENGTH) { // X86Instruction::noop().emit(self)?; self.emit::(0x90); } diff --git a/src/static_analysis.rs b/src/static_analysis.rs index 6bea90a1..aa5d0ae3 100644 --- a/src/static_analysis.rs +++ b/src/static_analysis.rs @@ -180,7 +180,9 @@ impl<'a> Analysis<'a> { } let mut result = Self { // Removes the generic ContextObject which is safe because we are not going to execute the program - executable: unsafe { std::mem::transmute(executable) }, + executable: unsafe { + std::mem::transmute::<&Executable, &Executable>(executable) + }, instructions, functions, cfg_nodes: BTreeMap::new(),