Skip to content

Commit

Permalink
Makes the target_pc parameter of emit_profile_instruction_count() non…
Browse files Browse the repository at this point in the history
… optional
  • Loading branch information
Lichtso committed Jan 6, 2025
1 parent 3fbf422 commit fbe00a0
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {

match insn.opc {
ebpf::LD_DW_IMM if !self.executable.get_sbpf_version().disable_lddw() => {
self.emit_validate_and_profile_instruction_count(Some(self.pc + 2));
self.emit_validate_and_profile_instruction_count(self.pc + 2);
self.pc += 1;
self.result.pc_section[self.pc] = unsafe { self.anchors[ANCHOR_CALL_UNSUPPORTED_INSTRUCTION].offset_from(self.result.text_section.as_ptr()) as u32 };
ebpf::augment_lddw_unchecked(self.program, &mut insn);
Expand Down Expand Up @@ -719,7 +719,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {

// BPF_JMP class
ebpf::JA => {
self.emit_validate_and_profile_instruction_count(Some(target_pc));
self.emit_validate_and_profile_instruction_count(target_pc);
let jump_offset = self.relative_to_target_pc(target_pc, 5);
self.emit_ins(X86Instruction::jump_immediate(jump_offset));
},
Expand Down Expand Up @@ -789,7 +789,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
|| (insn.opc == ebpf::RETURN && !self.executable.get_sbpf_version().static_syscalls()) {
return Err(EbpfError::UnsupportedInstruction);
}
self.emit_validate_and_profile_instruction_count(Some(0));
self.emit_validate_and_profile_instruction_count(0);

let call_depth_access = X86IndirectAccess::Offset(self.slot_in_vm(RuntimeEnvironmentSlot::CallDepth));
// If env.call_depth == 0, we've reached the exit instruction of the entry point
Expand All @@ -814,7 +814,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
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(Some(self.pc + 1));
self.emit_validate_and_profile_instruction_count(self.pc + 1);
self.emit_ins(X86Instruction::load_immediate(REGISTER_SCRATCH, self.pc as i64)); // Save pc
self.emit_set_exception_kind(EbpfError::ExecutionOverrun);
self.emit_ins(X86Instruction::jump_immediate(self.relative_to_anchor(ANCHOR_THROW_EXCEPTION, 5)));
Expand Down Expand Up @@ -979,8 +979,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
}

#[inline]
fn emit_validate_and_profile_instruction_count(&mut self, target_pc: Option<usize>) {
let target_pc = target_pc.unwrap();
fn emit_validate_and_profile_instruction_count(&mut self, target_pc: usize) {
self.last_instruction_meter_validation_pc = self.pc;
self.emit_ins(X86Instruction::load_immediate(REGISTER_SCRATCH, ((target_pc << 32 | self.pc) as i64) ^ self.immediate_value_key));
self.emit_ins(X86Instruction::call_immediate(self.relative_to_anchor(ANCHOR_VALIDATE_AND_PROFILE_INSTRUCTION_COUNT, 5)));
Expand Down Expand Up @@ -1140,7 +1139,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {

#[inline]
fn emit_syscall_dispatch(&mut self, function: BuiltinFunction<C>) {
self.emit_validate_and_profile_instruction_count(Some(0));
self.emit_validate_and_profile_instruction_count(0);
self.emit_ins(X86Instruction::load_immediate(REGISTER_SCRATCH, function as usize as i64));
self.emit_ins(X86Instruction::call_immediate(self.relative_to_anchor(ANCHOR_EXTERNAL_FUNCTION_CALL, 5)));
self.emit_undo_profile_instruction_count(0);
Expand Down Expand Up @@ -1215,7 +1214,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {

#[inline]
fn emit_conditional_branch_reg(&mut self, op: u8, bitwise: bool, first_operand: X86Register, second_operand: X86Register, target_pc: usize) {
self.emit_validate_and_profile_instruction_count(Some(target_pc));
self.emit_validate_and_profile_instruction_count(target_pc);
if bitwise { // Logical
self.emit_ins(X86Instruction::test(OperandSize::S64, first_operand, second_operand, None));
} else { // Arithmetic
Expand All @@ -1228,7 +1227,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {

#[inline]
fn emit_conditional_branch_imm(&mut self, op: u8, bitwise: bool, immediate: i64, second_operand: X86Register, target_pc: usize) {
self.emit_validate_and_profile_instruction_count(Some(target_pc));
self.emit_validate_and_profile_instruction_count(target_pc);
if self.should_sanitize_constant(immediate) {
self.emit_ins(X86Instruction::mov_mmx(OperandSize::S64, REGISTER_SCRATCH, MM0));
self.emit_sanitized_load_immediate(REGISTER_SCRATCH, immediate);
Expand Down

0 comments on commit fbe00a0

Please sign in to comment.