Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Replaces "sub r11, imm" with "add r11, -imm". (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso authored Jul 25, 2023
1 parent 4c68373 commit 48f271f
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 33 deletions.
2 changes: 1 addition & 1 deletion benches/vm_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ fn bench_jit_vs_interpreter_call_depth_dynamic(bencher: &mut Bencher) {
jlt r6, 1024, -4
exit
function_foo:
sub r11, 4
add r11, -4
stw [r10-4], 0x11223344
mov r6, r1
jeq r6, 0, +3
Expand Down
11 changes: 2 additions & 9 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,14 @@ impl<'a, 'b, V: Verifier, C: ContextObject> Interpreter<'a, 'b, V, C> {
}

match insn.opc {
_ if dst == STACK_PTR_REG && self.executable.get_sbpf_version().dynamic_stack_frames() => {
ebpf::ADD64_IMM if dst == STACK_PTR_REG && self.executable.get_sbpf_version().dynamic_stack_frames() => {
// Let the stack overflow. For legitimate programs, this is a nearly
// impossible condition to hit since programs are metered and we already
// enforce a maximum call depth. For programs that intentionally mess
// around with the stack pointer, MemoryRegion::map will return
// InvalidVirtualAddress(stack_ptr) once an invalid stack address is
// accessed.
match insn.opc {
ebpf::SUB64_IMM => { self.vm.stack_pointer = self.vm.stack_pointer.overflowing_add(-insn.imm as u64).0; }
ebpf::ADD64_IMM => { self.vm.stack_pointer = self.vm.stack_pointer.overflowing_add(insn.imm as u64).0; }
_ => {
#[cfg(debug_assertions)]
unreachable!("unexpected insn on r11")
}
}
self.vm.stack_pointer = self.vm.stack_pointer.overflowing_add(insn.imm as u64).0;
}

ebpf::LD_DW_IMM => {
Expand Down
11 changes: 2 additions & 9 deletions src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,16 +392,9 @@ impl<'a, V: Verifier, C: ContextObject> JitCompiler<'a, V, C> {
let target_pc = (self.pc as isize + insn.off as isize + 1) as usize;

match insn.opc {
_ if insn.dst == STACK_PTR_REG as u8 && self.executable.get_sbpf_version().dynamic_stack_frames() => {
ebpf::ADD64_IMM if insn.dst == STACK_PTR_REG as u8 && self.executable.get_sbpf_version().dynamic_stack_frames() => {
let stack_ptr_access = X86IndirectAccess::Offset(self.slot_on_environment_stack(RuntimeEnvironmentSlot::StackPointer));
match insn.opc {
ebpf::SUB64_IMM => self.emit_ins(X86Instruction::alu(OperandSize::S64, 0x81, 5, RBP, insn.imm, Some(stack_ptr_access))),
ebpf::ADD64_IMM => self.emit_ins(X86Instruction::alu(OperandSize::S64, 0x81, 0, RBP, insn.imm, Some(stack_ptr_access))),
_ => {
#[cfg(debug_assertions)]
unreachable!("unexpected insn on r11")
}
}
self.emit_ins(X86Instruction::alu(OperandSize::S64, 0x81, 0, RBP, insn.imm, Some(stack_ptr_access)));
}

ebpf::LD_DW_IMM => {
Expand Down
7 changes: 1 addition & 6 deletions src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,7 @@ fn check_registers(

match (insn.dst, store) {
(0..=9, _) | (10, true) => Ok(()),
(11, _)
if sbpf_version.dynamic_stack_frames()
&& (insn.opc == ebpf::SUB64_IMM || insn.opc == ebpf::ADD64_IMM) =>
{
Ok(())
}
(11, _) if sbpf_version.dynamic_stack_frames() && insn.opc == ebpf::ADD64_IMM => Ok(()),
(10, false) => Err(VerifierError::CannotWriteR10(adj_insn_ptr(insn_ptr))),
(_, _) => Err(VerifierError::InvalidDestinationRegister(adj_insn_ptr(
insn_ptr,
Expand Down
Binary file modified tests/elfs/relative_call.so
Binary file not shown.
14 changes: 7 additions & 7 deletions tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2383,11 +2383,11 @@ fn test_err_dynamic_stack_ptr_overflow() {
// stack_ptr -= stack_ptr + 1
test_interpreter_and_jit_asm!(
"
sub r11, 0x7FFFFFFF
sub r11, 0x7FFFFFFF
sub r11, 0x7FFFFFFF
sub r11, 0x7FFFFFFF
sub r11, 0x14005
add r11, -0x7FFFFFFF
add r11, -0x7FFFFFFF
add r11, -0x7FFFFFFF
add r11, -0x7FFFFFFF
add r11, -0x14005
call function_foo
exit
function_foo:
Expand Down Expand Up @@ -2435,7 +2435,7 @@ fn test_dynamic_frame_ptr() {
// to the top of the stack
test_interpreter_and_jit_asm!(
"
sub r11, 8
add r11, -8
call function_foo
exit
function_foo:
Expand All @@ -2452,7 +2452,7 @@ fn test_dynamic_frame_ptr() {
// is restored
test_interpreter_and_jit_asm!(
"
sub r11, 8
add r11, -8
call function_foo
mov r0, r10
exit
Expand Down
2 changes: 1 addition & 1 deletion tests/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ fn test_verifier_err_invalid_reg_src() {
fn test_verifier_resize_stack_ptr_success() {
let executable = assemble::<TestContextObject>(
"
sub r11, 1
add r11, -1
add r11, 1
exit",
Arc::new(BuiltinProgram::new_loader(
Expand Down

0 comments on commit 48f271f

Please sign in to comment.