Skip to content

Commit

Permalink
jit: sign-extend the quotient register on sdiv32 (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrod committed Apr 29, 2022
1 parent 792fb1e commit e61e045
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ fn emit_muldivmod<E: UserDefinedError>(jit: &mut JitCompiler, opc: u8, src: u8,
X86Instruction::pop(RAX).emit(jit)?;
}

if size == OperandSize::S32 && opc & ebpf::BPF_ALU_OP_MASK == ebpf::BPF_MUL {
if size == OperandSize::S32 && (mul || sdiv) {
X86Instruction::sign_extend_i32_to_i64(dst, dst).emit(jit)?;
}
Ok(())
Expand Down
31 changes: 31 additions & 0 deletions tests/ubpf_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,21 @@ fn test_sdiv32_imm() {
);
}

#[test]
fn test_sdiv32_neg_imm() {
test_interpreter_and_jit_asm!(
"
lddw r0, 0x10000000c
sdiv32 r0, -4
exit",
[],
(),
0,
{ |_vm, res: Result| { res.unwrap() as i64 == -3 } },
3
);
}

#[test]
fn test_sdiv32_reg() {
test_interpreter_and_jit_asm!(
Expand All @@ -854,6 +869,22 @@ fn test_sdiv32_reg() {
);
}

#[test]
fn test_sdiv32_neg_reg() {
test_interpreter_and_jit_asm!(
"
lddw r0, 0x10000000c
mov r1, -4
sdiv32 r0, r1
exit",
[],
(),
0,
{ |_vm, res: Result| { res.unwrap() as i64 == -0x3 } },
4
);
}

#[test]
fn test_div64_imm() {
test_interpreter_and_jit_asm!(
Expand Down

0 comments on commit e61e045

Please sign in to comment.