-
Notifications
You must be signed in to change notification settings - Fork 175
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
Refactor - JIT noop insertion #584
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,11 @@ use rand::{thread_rng, Rng}; | |
#[cfg(feature = "shuttle-test")] | ||
use shuttle::rand::{thread_rng, Rng}; | ||
|
||
use rand::{rngs::SmallRng, SeedableRng}; | ||
use rand::{ | ||
distributions::{Distribution, Uniform}, | ||
rngs::SmallRng, | ||
SeedableRng, | ||
}; | ||
use std::{fmt::Debug, mem, ptr}; | ||
|
||
use crate::{ | ||
|
@@ -321,6 +325,7 @@ pub struct JitCompiler<'a, C: ContextObject> { | |
pc: usize, | ||
last_instruction_meter_validation_pc: usize, | ||
next_noop_insertion: u32, | ||
noop_range: Uniform<u32>, | ||
runtime_environment_key: i32, | ||
diversification_rng: SmallRng, | ||
stopwatch_is_active: bool, | ||
|
@@ -372,6 +377,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> { | |
pc: 0, | ||
last_instruction_meter_validation_pc: 0, | ||
next_noop_insertion: if config.noop_instruction_rate == 0 { u32::MAX } else { diversification_rng.gen_range(0..config.noop_instruction_rate * 2) }, | ||
noop_range: Uniform::new_inclusive(0, config.noop_instruction_rate * 2), | ||
runtime_environment_key, | ||
diversification_rng, | ||
stopwatch_is_active: false, | ||
|
@@ -393,7 +399,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> { | |
self.emit_subroutines(); | ||
|
||
while self.pc * ebpf::INSN_SIZE < self.program.len() { | ||
if self.offset_in_text_section + MAX_MACHINE_CODE_LENGTH_PER_INSTRUCTION > self.result.text_section.len() { | ||
if self.offset_in_text_section + MAX_MACHINE_CODE_LENGTH_PER_INSTRUCTION * 2 >= self.result.text_section.len() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this overestimation is too high. We could decrease There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is high yes, but it can only run into the end one sBPF instruction earlier. And that should be covered by |
||
return Err(EbpfError::ExhaustedTextSegment(self.pc)); | ||
} | ||
let mut insn = ebpf::get_insn_unchecked(self.program, self.pc); | ||
|
@@ -723,7 +729,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> { | |
} | ||
|
||
// Bumper in case there was no final exit | ||
if self.offset_in_text_section + MAX_MACHINE_CODE_LENGTH_PER_INSTRUCTION > self.result.text_section.len() { | ||
if self.offset_in_text_section + MAX_MACHINE_CODE_LENGTH_PER_INSTRUCTION * 2 >= self.result.text_section.len() { | ||
return Err(EbpfError::ExhaustedTextSegment(self.pc)); | ||
} | ||
self.emit_validate_and_profile_instruction_count(true, Some(self.pc + 2)); | ||
|
@@ -786,7 +792,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> { | |
pub fn emit_ins(&mut self, instruction: X86Instruction) { | ||
instruction.emit(self); | ||
if self.next_noop_insertion == 0 { | ||
self.next_noop_insertion = self.diversification_rng.gen_range(0..self.config.noop_instruction_rate * 2); | ||
self.next_noop_insertion = self.noop_range.sample(&mut self.diversification_rng); | ||
// X86Instruction::noop().emit(self)?; | ||
self.emit::<u8>(0x90); | ||
} else { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to this PR, but I found out that
SmallRng
isn't good when security is a concern:https://docs.rs/rand/latest/rand/rngs/struct.SmallRng.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PRNG here needs to be fast, not high quality. The actual security comes from diversity of seeds in validators across the cluster.