Skip to content

Commit

Permalink
ubpf_run: Refactor code to assist in adding instruction counting.
Browse files Browse the repository at this point in the history
Refactor code to add a ubpf_run function so it is easier to add
instruction counting for both the VM and JIT cases. The counting
itself will be added in subsequent patches.
  • Loading branch information
sbates130272 committed Jul 28, 2021
1 parent 8433947 commit 555e750
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
7 changes: 7 additions & 0 deletions vm/inc/ubpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ int ubpf_exec(const struct ubpf_vm *vm, void *mem, size_t mem_len, uint64_t* bpf

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

/*
* A wrapper around some of the compile, load and run commands to
* enable instruction counting. Returns the return value of the eBPF
* program being executed.
*/
uint64_t ubpf_run(struct ubpf_vm *vm, void *mem, size_t mem_len, bool jit);

/*
* Translate the eBPF byte code to x64 machine code, store in buffer, and
* write the resulting count of bytes to size.
Expand Down
13 changes: 1 addition & 12 deletions vm/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,7 @@ int main(int argc, char **argv)

uint64_t ret;

if (jit) {
ubpf_jit_fn fn = ubpf_compile(vm, &errmsg);
if (fn == NULL) {
fprintf(stderr, "Failed to compile: %s\n", errmsg);
free(errmsg);
return 1;
}
ret = fn(mem, mem_len);
} else {
if (ubpf_exec(vm, mem, mem_len, &ret) < 0)
ret = UINT64_MAX;
}
ret = ubpf_run(vm, mem, mem_len, jit);

printf("0x%"PRIx64"\n", ret);

Expand Down
21 changes: 21 additions & 0 deletions vm/ubpf_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,27 @@ ubpf_exec(const struct ubpf_vm *vm, void *mem, size_t mem_len, uint64_t* bpf_ret
}
}

uint64_t
ubpf_run(struct ubpf_vm *vm, void *mem, size_t mem_len, bool jit)
{
uint64_t ret;
char *errmsg;

if (jit) {
ubpf_jit_fn fn = ubpf_compile(vm, &errmsg);
if (fn == NULL) {
fprintf(stderr, "Failed to compile: %s\n", errmsg);
free(errmsg);
return 1;
}
ret = fn(mem, mem_len);
} else {
if (ubpf_exec(vm, mem, mem_len, &ret) < 0)
ret = UINT64_MAX;
}
return ret;
}

static bool
validate(const struct ubpf_vm *vm, const struct ebpf_inst *insts, uint32_t num_insts, char **errmsg)
{
Expand Down

0 comments on commit 555e750

Please sign in to comment.