Skip to content

Commit

Permalink
Cleanup - Rename disable_lddw to enable_lddw for consistency (#570)
Browse files Browse the repository at this point in the history
* Adds disable_lddw checks in interpreter and JIT.

* Inverts disable_lddw to enable_lddw.
  • Loading branch information
Lichtso authored May 21, 2024
1 parent 899cd91 commit 7ef666b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl<'a, 'b, C: ContextObject> Interpreter<'a, 'b, C> {
self.vm.stack_pointer = self.vm.stack_pointer.overflowing_add(insn.imm as u64).0;
}

ebpf::LD_DW_IMM => {
ebpf::LD_DW_IMM if self.executable.get_sbpf_version().enable_lddw() => {
ebpf::augment_lddw_unchecked(self.program, &mut insn);
self.reg[dst] = insn.imm as u64;
self.reg[11] += 1;
Expand Down Expand Up @@ -337,7 +337,7 @@ impl<'a, 'b, C: ContextObject> Interpreter<'a, 'b, C> {
ebpf::MOV64_REG => self.reg[dst] = self.reg[src],
ebpf::ARSH64_IMM => self.reg[dst] = (self.reg[dst] as i64).wrapping_shr(insn.imm as u32) as u64,
ebpf::ARSH64_REG => self.reg[dst] = (self.reg[dst] as i64).wrapping_shr(self.reg[src] as u32) as u64,
ebpf::HOR64_IMM if self.executable.get_sbpf_version().disable_lddw() => {
ebpf::HOR64_IMM if !self.executable.get_sbpf_version().enable_lddw() => {
self.reg[dst] |= (insn.imm as u64).wrapping_shl(32);
}

Expand Down
10 changes: 5 additions & 5 deletions src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,16 +329,16 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {

// Scan through program to find actual number of instructions
let mut pc = 0;
if executable.get_sbpf_version().disable_lddw() {
pc = program.len() / ebpf::INSN_SIZE;
} else {
if executable.get_sbpf_version().enable_lddw() {
while (pc + 1) * ebpf::INSN_SIZE <= program.len() {
let insn = ebpf::get_insn_unchecked(program, pc);
pc += match insn.opc {
ebpf::LD_DW_IMM => 2,
_ => 1,
};
}
} else {
pc = program.len() / ebpf::INSN_SIZE;
}

let mut code_length_estimate = MAX_EMPTY_PROGRAM_MACHINE_CODE_LENGTH + MAX_START_PADDING_LENGTH + MAX_MACHINE_CODE_LENGTH_PER_INSTRUCTION * pc;
Expand Down Expand Up @@ -414,7 +414,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
self.emit_ins(X86Instruction::alu(OperandSize::S64, 0x81, 0, REGISTER_PTR_TO_VM, insn.imm, Some(stack_ptr_access)));
}

ebpf::LD_DW_IMM => {
ebpf::LD_DW_IMM if self.executable.get_sbpf_version().enable_lddw() => {
self.emit_validate_and_profile_instruction_count(true, Some(self.pc + 2));
self.pc += 1;
self.result.pc_section[self.pc] = self.anchors[ANCHOR_CALL_UNSUPPORTED_INSTRUCTION] as usize;
Expand Down Expand Up @@ -584,7 +584,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
ebpf::MOV64_REG => self.emit_ins(X86Instruction::mov(OperandSize::S64, src, dst)),
ebpf::ARSH64_IMM => self.emit_shift(OperandSize::S64, 7, REGISTER_SCRATCH, dst, Some(insn.imm)),
ebpf::ARSH64_REG => self.emit_shift(OperandSize::S64, 7, src, dst, None),
ebpf::HOR64_IMM => {
ebpf::HOR64_IMM if !self.executable.get_sbpf_version().enable_lddw() => {
self.emit_sanitized_alu(OperandSize::S64, 0x09, 1, dst, (insn.imm as u64).wrapping_shl(32) as i64);
}

Expand Down
6 changes: 3 additions & 3 deletions src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ impl SBPFVersion {
self != &SBPFVersion::V1
}

/// Disable the only two slots long instruction: LD_DW_IMM
pub fn disable_lddw(&self) -> bool {
self != &SBPFVersion::V1
/// Enable the only two slots long instruction: LD_DW_IMM
pub fn enable_lddw(&self) -> bool {
self == &SBPFVersion::V1
}

/// Enable the BPF_PQR instruction class
Expand Down
4 changes: 2 additions & 2 deletions src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl Verifier for RequisiteVerifier {
}

match insn.opc {
ebpf::LD_DW_IMM if !sbpf_version.disable_lddw() => {
ebpf::LD_DW_IMM if sbpf_version.enable_lddw() => {
check_load_dw(prog, insn_ptr)?;
insn_ptr += 1;
},
Expand Down Expand Up @@ -329,7 +329,7 @@ impl Verifier for RequisiteVerifier {
ebpf::MOV64_REG => {},
ebpf::ARSH64_IMM => { check_imm_shift(&insn, insn_ptr, 64)?; },
ebpf::ARSH64_REG => {},
ebpf::HOR64_IMM if sbpf_version.disable_lddw() => {},
ebpf::HOR64_IMM if !sbpf_version.enable_lddw() => {},

// BPF_PQR class
ebpf::LMUL32_IMM if sbpf_version.enable_pqr() => {},
Expand Down

0 comments on commit 7ef666b

Please sign in to comment.