Skip to content

Commit

Permalink
Merge pull request iovisor#80 from Alan-Jowett/issue79
Browse files Browse the repository at this point in the history
ubpf_exec should separate errors returned from the return value
  • Loading branch information
rlane authored Jun 29, 2021
2 parents bdb5618 + f543c2a commit 8433947
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion vm/inc/ubpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ int ubpf_load(struct ubpf_vm *vm, const void *code, uint32_t code_len, char **er
*/
int ubpf_load_elf(struct ubpf_vm *vm, const void *elf, size_t elf_len, char **errmsg);

uint64_t ubpf_exec(const struct ubpf_vm *vm, void *mem, size_t mem_len);
int ubpf_exec(const struct ubpf_vm *vm, void *mem, size_t mem_len, uint64_t* bpf_return_value);

ubpf_jit_fn ubpf_compile(struct ubpf_vm *vm, char **errmsg);

Expand Down
3 changes: 2 additions & 1 deletion vm/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ int main(int argc, char **argv)
}
ret = fn(mem, mem_len);
} else {
ret = ubpf_exec(vm, mem, mem_len);
if (ubpf_exec(vm, mem, mem_len, &ret) < 0)
ret = UINT64_MAX;
}

printf("0x%"PRIx64"\n", ret);
Expand Down
21 changes: 11 additions & 10 deletions vm/ubpf_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ u32(uint64_t x)
return x;
}

uint64_t
ubpf_exec(const struct ubpf_vm *vm, void *mem, size_t mem_len)
int
ubpf_exec(const struct ubpf_vm *vm, void *mem, size_t mem_len, uint64_t* bpf_return_value)
{
uint16_t pc = 0;
const struct ebpf_inst *insts = vm->insts;
Expand All @@ -153,7 +153,7 @@ ubpf_exec(const struct ubpf_vm *vm, void *mem, size_t mem_len)

if (!insts) {
/* Code must be loaded before we can execute */
return UINT64_MAX;
return -1;
}

reg[1] = (uintptr_t)mem;
Expand Down Expand Up @@ -196,7 +196,7 @@ ubpf_exec(const struct ubpf_vm *vm, void *mem, size_t mem_len)
case EBPF_OP_DIV_REG:
if (reg[inst.src] == 0) {
vm->error_printf(stderr, "uBPF error: division by zero at PC %u\n", cur_pc);
return UINT64_MAX;
return -1;
}
reg[inst.dst] = u32(reg[inst.dst]) / u32(reg[inst.src]);
reg[inst.dst] &= UINT32_MAX;
Expand Down Expand Up @@ -244,7 +244,7 @@ ubpf_exec(const struct ubpf_vm *vm, void *mem, size_t mem_len)
case EBPF_OP_MOD_REG:
if (reg[inst.src] == 0) {
vm->error_printf(stderr, "uBPF error: division by zero at PC %u\n", cur_pc);
return UINT64_MAX;
return -1;
}
reg[inst.dst] = u32(reg[inst.dst]) % u32(reg[inst.src]);
break;
Expand Down Expand Up @@ -317,7 +317,7 @@ ubpf_exec(const struct ubpf_vm *vm, void *mem, size_t mem_len)
case EBPF_OP_DIV64_REG:
if (reg[inst.src] == 0) {
vm->error_printf(stderr, "uBPF error: division by zero at PC %u\n", cur_pc);
return UINT64_MAX;
return -1;
}
reg[inst.dst] /= reg[inst.src];
break;
Expand Down Expand Up @@ -354,7 +354,7 @@ ubpf_exec(const struct ubpf_vm *vm, void *mem, size_t mem_len)
case EBPF_OP_MOD64_REG:
if (reg[inst.src] == 0) {
vm->error_printf(stderr, "uBPF error: division by zero at PC %u\n", cur_pc);
return UINT64_MAX;
return -1;
}
reg[inst.dst] %= reg[inst.src];
break;
Expand Down Expand Up @@ -385,13 +385,13 @@ ubpf_exec(const struct ubpf_vm *vm, void *mem, size_t mem_len)
#define BOUNDS_CHECK_LOAD(size) \
do { \
if (!bounds_check(vm, (char *)reg[inst.src] + inst.offset, size, "load", cur_pc, mem, mem_len, stack)) { \
return UINT64_MAX; \
return -1; \
} \
} while (0)
#define BOUNDS_CHECK_STORE(size) \
do { \
if (!bounds_check(vm, (char *)reg[inst.dst] + inst.offset, size, "store", cur_pc, mem, mem_len, stack)) { \
return UINT64_MAX; \
return -1; \
} \
} while (0)

Expand Down Expand Up @@ -564,7 +564,8 @@ ubpf_exec(const struct ubpf_vm *vm, void *mem, size_t mem_len)
}
break;
case EBPF_OP_EXIT:
return reg[0];
*bpf_return_value = reg[0];
return 0;
case EBPF_OP_CALL:
reg[0] = vm->ext_funcs[inst.imm](reg[1], reg[2], reg[3], reg[4], reg[5]);
break;
Expand Down

0 comments on commit 8433947

Please sign in to comment.