Skip to content

Commit

Permalink
Fix - JIT randomized start padding length (#556)
Browse files Browse the repository at this point in the history
* Adds MAX_START_PADDING_LENGTH.

* Make cargo clippy happy.
  • Loading branch information
Lichtso authored Apr 12, 2024
1 parent 40a30d8 commit ec42f02
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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::<u8>(0x90);
}
Expand Down
4 changes: 3 additions & 1 deletion src/static_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C>, &Executable<TestContextObject>>(executable)
},
instructions,
functions,
cfg_nodes: BTreeMap::new(),
Expand Down

0 comments on commit ec42f02

Please sign in to comment.