Skip to content

Commit

Permalink
Make sign extension explicit
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>
  • Loading branch information
Alan Jowett committed May 7, 2024
1 parent 93e06df commit 0dcf196
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions vm/ubpf_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ i32(uint64_t x)
return x;
}

/**
* @brief Sign extend immediate value to a signed 64-bit value.
*
* @param[in] immediate The signed 32-bit immediate value to sign extend.
* @return The sign extended 64-bit value.
*/
static int64_t sign_extend_immediate(int32_t immediate) {
return (int64_t)immediate;

}

#define IS_ALIGNED(x, a) (((uintptr_t)(x) & ((a)-1)) == 0)

Expand Down Expand Up @@ -686,7 +696,7 @@ ubpf_exec(const struct ubpf_vm* vm, void* mem, size_t mem_len, uint64_t* bpf_ret
pc += inst.offset;
break;
case EBPF_OP_JEQ_IMM:
if (reg[inst.dst] == inst.imm) {
if (reg[inst.dst] == (uint64_t)sign_extend_immediate(inst.imm)) {
pc += inst.offset;
}
break;
Expand All @@ -706,7 +716,7 @@ ubpf_exec(const struct ubpf_vm* vm, void* mem, size_t mem_len, uint64_t* bpf_ret
}
break;
case EBPF_OP_JGT_IMM:
if (reg[inst.dst] > inst.imm) {
if (reg[inst.dst] > (uint64_t)sign_extend_immediate(inst.imm)) {
pc += inst.offset;
}
break;
Expand All @@ -726,7 +736,7 @@ ubpf_exec(const struct ubpf_vm* vm, void* mem, size_t mem_len, uint64_t* bpf_ret
}
break;
case EBPF_OP_JGE_IMM:
if (reg[inst.dst] >= inst.imm) {
if (reg[inst.dst] >= (uint64_t)sign_extend_immediate(inst.imm)) {
pc += inst.offset;
}
break;
Expand All @@ -746,7 +756,7 @@ ubpf_exec(const struct ubpf_vm* vm, void* mem, size_t mem_len, uint64_t* bpf_ret
}
break;
case EBPF_OP_JLT_IMM:
if (reg[inst.dst] < inst.imm) {
if (reg[inst.dst] < (uint64_t)sign_extend_immediate(inst.imm)) {
pc += inst.offset;
}
break;
Expand All @@ -766,7 +776,7 @@ ubpf_exec(const struct ubpf_vm* vm, void* mem, size_t mem_len, uint64_t* bpf_ret
}
break;
case EBPF_OP_JLE_IMM:
if (reg[inst.dst] <= inst.imm) {
if (reg[inst.dst] <= (uint64_t)sign_extend_immediate(inst.imm)) {
pc += inst.offset;
}
break;
Expand All @@ -786,7 +796,7 @@ ubpf_exec(const struct ubpf_vm* vm, void* mem, size_t mem_len, uint64_t* bpf_ret
}
break;
case EBPF_OP_JSET_IMM:
if (reg[inst.dst] & inst.imm) {
if (reg[inst.dst] & (uint64_t)sign_extend_immediate(inst.imm)) {
pc += inst.offset;
}
break;
Expand All @@ -806,7 +816,7 @@ ubpf_exec(const struct ubpf_vm* vm, void* mem, size_t mem_len, uint64_t* bpf_ret
}
break;
case EBPF_OP_JNE_IMM:
if (reg[inst.dst] != inst.imm) {
if (reg[inst.dst] != (uint64_t)sign_extend_immediate(inst.imm)) {
pc += inst.offset;
}
break;
Expand All @@ -826,7 +836,7 @@ ubpf_exec(const struct ubpf_vm* vm, void* mem, size_t mem_len, uint64_t* bpf_ret
}
break;
case EBPF_OP_JSGT_IMM:
if ((int64_t)reg[inst.dst] > inst.imm) {
if ((int64_t)reg[inst.dst] > sign_extend_immediate(inst.imm)) {
pc += inst.offset;
}
break;
Expand All @@ -846,7 +856,7 @@ ubpf_exec(const struct ubpf_vm* vm, void* mem, size_t mem_len, uint64_t* bpf_ret
}
break;
case EBPF_OP_JSGE_IMM:
if ((int64_t)reg[inst.dst] >= inst.imm) {
if ((int64_t)reg[inst.dst] >= sign_extend_immediate(inst.imm)) {
pc += inst.offset;
}
break;
Expand All @@ -866,7 +876,7 @@ ubpf_exec(const struct ubpf_vm* vm, void* mem, size_t mem_len, uint64_t* bpf_ret
}
break;
case EBPF_OP_JSLT_IMM:
if ((int64_t)reg[inst.dst] < inst.imm) {
if ((int64_t)reg[inst.dst] < sign_extend_immediate(inst.imm)) {
pc += inst.offset;
}
break;
Expand All @@ -886,7 +896,7 @@ ubpf_exec(const struct ubpf_vm* vm, void* mem, size_t mem_len, uint64_t* bpf_ret
}
break;
case EBPF_OP_JSLE_IMM:
if ((int64_t)reg[inst.dst] <= inst.imm) {
if ((int64_t)reg[inst.dst] <= sign_extend_immediate(inst.imm)) {
pc += inst.offset;
}
break;
Expand Down

0 comments on commit 0dcf196

Please sign in to comment.