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

Commit 48f271f

Browse files
authored
Replaces "sub r11, imm" with "add r11, -imm". (#488)
1 parent 4c68373 commit 48f271f

File tree

7 files changed

+14
-33
lines changed

7 files changed

+14
-33
lines changed

benches/vm_execution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ fn bench_jit_vs_interpreter_call_depth_dynamic(bencher: &mut Bencher) {
259259
jlt r6, 1024, -4
260260
exit
261261
function_foo:
262-
sub r11, 4
262+
add r11, -4
263263
stw [r10-4], 0x11223344
264264
mov r6, r1
265265
jeq r6, 0, +3

src/interpreter.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,21 +187,14 @@ impl<'a, 'b, V: Verifier, C: ContextObject> Interpreter<'a, 'b, V, C> {
187187
}
188188

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

207200
ebpf::LD_DW_IMM => {

src/jit.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -392,16 +392,9 @@ impl<'a, V: Verifier, C: ContextObject> JitCompiler<'a, V, C> {
392392
let target_pc = (self.pc as isize + insn.off as isize + 1) as usize;
393393

394394
match insn.opc {
395-
_ if insn.dst == STACK_PTR_REG as u8 && self.executable.get_sbpf_version().dynamic_stack_frames() => {
395+
ebpf::ADD64_IMM if insn.dst == STACK_PTR_REG as u8 && self.executable.get_sbpf_version().dynamic_stack_frames() => {
396396
let stack_ptr_access = X86IndirectAccess::Offset(self.slot_on_environment_stack(RuntimeEnvironmentSlot::StackPointer));
397-
match insn.opc {
398-
ebpf::SUB64_IMM => self.emit_ins(X86Instruction::alu(OperandSize::S64, 0x81, 5, RBP, insn.imm, Some(stack_ptr_access))),
399-
ebpf::ADD64_IMM => self.emit_ins(X86Instruction::alu(OperandSize::S64, 0x81, 0, RBP, insn.imm, Some(stack_ptr_access))),
400-
_ => {
401-
#[cfg(debug_assertions)]
402-
unreachable!("unexpected insn on r11")
403-
}
404-
}
397+
self.emit_ins(X86Instruction::alu(OperandSize::S64, 0x81, 0, RBP, insn.imm, Some(stack_ptr_access)));
405398
}
406399

407400
ebpf::LD_DW_IMM => {

src/verifier.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,7 @@ fn check_registers(
182182

183183
match (insn.dst, store) {
184184
(0..=9, _) | (10, true) => Ok(()),
185-
(11, _)
186-
if sbpf_version.dynamic_stack_frames()
187-
&& (insn.opc == ebpf::SUB64_IMM || insn.opc == ebpf::ADD64_IMM) =>
188-
{
189-
Ok(())
190-
}
185+
(11, _) if sbpf_version.dynamic_stack_frames() && insn.opc == ebpf::ADD64_IMM => Ok(()),
191186
(10, false) => Err(VerifierError::CannotWriteR10(adj_insn_ptr(insn_ptr))),
192187
(_, _) => Err(VerifierError::InvalidDestinationRegister(adj_insn_ptr(
193188
insn_ptr,

tests/elfs/relative_call.so

0 Bytes
Binary file not shown.

tests/execution.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,11 +2383,11 @@ fn test_err_dynamic_stack_ptr_overflow() {
23832383
// stack_ptr -= stack_ptr + 1
23842384
test_interpreter_and_jit_asm!(
23852385
"
2386-
sub r11, 0x7FFFFFFF
2387-
sub r11, 0x7FFFFFFF
2388-
sub r11, 0x7FFFFFFF
2389-
sub r11, 0x7FFFFFFF
2390-
sub r11, 0x14005
2386+
add r11, -0x7FFFFFFF
2387+
add r11, -0x7FFFFFFF
2388+
add r11, -0x7FFFFFFF
2389+
add r11, -0x7FFFFFFF
2390+
add r11, -0x14005
23912391
call function_foo
23922392
exit
23932393
function_foo:
@@ -2435,7 +2435,7 @@ fn test_dynamic_frame_ptr() {
24352435
// to the top of the stack
24362436
test_interpreter_and_jit_asm!(
24372437
"
2438-
sub r11, 8
2438+
add r11, -8
24392439
call function_foo
24402440
exit
24412441
function_foo:
@@ -2452,7 +2452,7 @@ fn test_dynamic_frame_ptr() {
24522452
// is restored
24532453
test_interpreter_and_jit_asm!(
24542454
"
2455-
sub r11, 8
2455+
add r11, -8
24562456
call function_foo
24572457
mov r0, r10
24582458
exit

tests/verifier.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ fn test_verifier_err_invalid_reg_src() {
201201
fn test_verifier_resize_stack_ptr_success() {
202202
let executable = assemble::<TestContextObject>(
203203
"
204-
sub r11, 1
204+
add r11, -1
205205
add r11, 1
206206
exit",
207207
Arc::new(BuiltinProgram::new_loader(

0 commit comments

Comments
 (0)