Skip to content

Commit

Permalink
add error injection demo
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwei37 committed Nov 22, 2023
1 parent 227c0ba commit 1e96b3a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ More bcc/libbpf-tools examples can be found in [example/libbpf-tools](https://gi

You can also run bpftime with `bpftrace`, we've test it on [this commit](https://github.com/iovisor/bpftrace/commit/75aca47dd8e1d642ff31c9d3ce330e0c616e5b96). More details about how to run bpftrace in usespace, can be found in [example/bpftrace](https://github.com/eunomia-bpf/bpftime/tree/master/example/bpftrace).

### Error injections

- [`error-injection`](https://github.com/eunomia-bpf/bpftime/tree/master/example/error-inject) Inject errors into a userspace function to test its error handling capabilities.

> ⚠️ **Note**: `bpftime` is actively under development, and it's not yet recommended for production use. See our [roadmap](#roadmap) for details. We'd love to hear your feedback and suggestions! Please feel free to open an issue or [Contact us](#contact-and-citations).
## In-Depth
Expand Down
3 changes: 2 additions & 1 deletion example/error-inject/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
error_inject
.output/
.output/
victim
19 changes: 19 additions & 0 deletions example/error-inject/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# error-inject

bpftime can allow you override the execution of a function in userspace, and return a value you specify, or error injection in the system call.

It's useful for testing the error handling of your program.

## Run for override userspace function

server

```sh
LD_PRELOAD=~/.bpftime/libbpftime-syscall-server.so ./error_inject
```

client

```sh
LD_PRELOAD=~/.bpftime/libbpftime-agent.so ./victim
```
9 changes: 7 additions & 2 deletions example/error-inject/error_inject.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
SEC("uprobe")
int do_error_inject_patch(struct pt_regs *ctx)
{
bpf_printk("target_func called is overrided.\n");
bpf_override_return(ctx, 0);
int rand = bpf_get_prandom_u32();
if (rand % 2 == 0) {
bpf_printk("bpf: Inject error. Target func will not exec.\n");
bpf_override_return(ctx, -1);
return 0;
}
bpf_printk("bpf: Continue.\n");
return 0;
}

Expand Down
23 changes: 23 additions & 0 deletions example/error-inject/victim.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int target_func()
{
int res = open("/dev/null");
printf("target_func is running\n");
close(res);
return 0;
}

int main(int argc, char *argv[])
{
while (1) {
sleep(1);
int res = target_func();
if (res != 0) {
printf("got error %d\n", res);
}
}
return 0;
}
13 changes: 12 additions & 1 deletion runtime/src/bpf_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ uint64_t bpftime_probe_read(uint64_t dst, uint64_t size, uint64_t ptr, uint64_t,
return 0;
}

uint64_t bpftime_get_prandom_u32()
{
return (uint32_t)rand();
}

uint64_t bpftime_ktime_get_coarse_ns(uint64_t, uint64_t, uint64_t, uint64_t,
uint64_t)
{
Expand Down Expand Up @@ -613,6 +618,12 @@ const bpftime_helper_group kernel_helper_group = {
.name = "bpf_trace_printk",
.fn = (void *)bpftime_trace_printk,
} },
{ BPF_FUNC_get_prandom_u32,
bpftime_helper_info{
.index = BPF_FUNC_get_prandom_u32,
.name = "bpf_get_prandom_u32",
.fn = (void *)bpftime_get_prandom_u32,
} },
{ BPF_FUNC_get_current_pid_tgid,
bpftime_helper_info{
.index = BPF_FUNC_get_current_pid_tgid,
Expand All @@ -629,7 +640,7 @@ const bpftime_helper_group kernel_helper_group = {
.name = "bpf_get_current_comm",
.fn = (void *)bpftime_get_current_comm,
} },
{ BPF_FUNC_override_return,
{ BPF_FUNC_override_return,
bpftime_helper_info{
.index = BPF_FUNC_override_return,
.name = "bpf_override_return",
Expand Down

0 comments on commit 1e96b3a

Please sign in to comment.