Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm-sdk: fix setting rodata and bss section variables #17

Merged
merged 3 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The wasm-bpf runtime require two parts: `the host side`(Outside the Wasm runtime
- see [src](src) and [include](include) directories, which would be a sample runtime built on the top of [libbpf](https://github.com/libbpf/libbpf) and [WAMR](https://github.com/bytecodealliance/wasm-micro-runtime).
- You can easily build your own Wasm-eBPF runtime in `any` languages, `any` eBPF libraries and `any` Wasm runtimes with the same System interface.
- wasm side: toolchains and libraries
- a [`libbpf-wasm`](wasm-include/libbpf-wasm.h) header only library to provide libbpf APIs for Wasm guest `C/C++` code.
- a [`libbpf-wasm`](wasm-sdk/libbpf-wasm.h) header only library to provide libbpf APIs for Wasm guest `C/C++` code.
- a [`bpftool`](https://github.com/eunomia-bpf/bpftool/tree/wasm-bpftool) tool to generate the Wasm-eBPF `skeleton` headers, and `C struct definitions` for passing data between the host and Wasm guest without serialization.
- More languages support(`Rust`, `Go`, etc) is on the way.

Expand Down
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
- host 侧: 见 [src](src) 以及 [include](include) 文件夹。 主机侧是一个构建在 [libbpf](https://github.com/libbpf/libbpf) 和 [WAMR](https://github.com/bytecodealliance/wasm-micro-runtime) 之上的运行时。
- 使用同一套工具链,任何人用任何 wasm 运行时或者任何 ebpf 用户态库,以及任何语言,都可以在两三百行三四百行内轻松实现一套 wasm+ebpf 运行时平台,运行几乎所有的 ebpf 应用场景。
- wasm 侧:
- 一个用于给 Wasm 客户侧 `C/C++` 代码提供 libbpf API的头文件库([`libbpf-wasm`](wasm-include/libbpf-wasm.h))。
- 一个用于给 Wasm 客户侧 `C/C++` 代码提供 libbpf API的头文件库([`libbpf-wasm`](wasm-sdk/libbpf-wasm.h))。
- 一个用来生成 Wasm-eBPF `skeleton` 头文件以及生成用于在主机侧和 Wasm 客户侧传递数据的 C 结构体定义的 [`bpftool`](https://github.com/eunomia-bpf/bpftool/tree/wasm-bpftool)。
- 更多编程语言支持(比如 `Rust`、 `Go` 等)还在开发中。

Expand Down
2 changes: 1 addition & 1 deletion examples/bootstrap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ This would invoke the following steps:
>
> We have create a specical POC tool outside of `bpftool` for generate C structs serialization-free bindings between eBPF/host side and Wasm, you can find it in [c-struct-bindgen](https://github.com/eunomia-bpf/c-struct-bindgen). More details about how to deal with the struct layout issue can be found in the README of the c-struct-bindgen tool.

The libbpf API for wasm program is provided as an header only library, you can find it in `libbpf-wasm.h` (wasm-include/libbpf-wasm.h). The wasm program can use the libbpf API and syscall to operate the BPF object, for example:
The libbpf API for wasm program is provided as an header only library, you can find it in [`libbpf-wasm.h`](../../wasm-sdk/libbpf-wasm.h). The wasm program can use the libbpf API and syscall to operate the BPF object, for example:

```c
/* Load and verify BPF application */
Expand Down
2 changes: 1 addition & 1 deletion examples/bootstrap/README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ make
>
>我们创建了一个特殊的 POC 工具,它不属于 `bpftool`,可以生成 eBPF/主机端和 Wasm 之间的不需要序列化的 C 结构体绑定,您可以在 [c-struct-bindgen](https://github.com/eunomia-bpf/c-struct-bindgen) 中找到它。关于如何处理结构体布局问题的更多详细信息,可以在 c-struct-bindgen 工具的 README 中找到。

libbpf API 为 wasm 程序提供了一个仅包含头文件的库,您可以在 libbpf-wasm.h(wasm-include/libbpf-wasm.h中找到它。wasm 程序可以使用 libbpf API 和 syscall 操作 BPF 对象,例如:
libbpf API 为 wasm 程序提供了一个仅包含头文件的库,您可以在 [libbpf-wasm](../../wasm-sdk/libbpf-wasm.h) 中找到它。wasm 程序可以使用 libbpf API 和 syscall 操作 BPF 对象,例如:

```c
/* Load and verify BPF application */
Expand Down
2 changes: 2 additions & 0 deletions examples/bootstrap/bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ main(int argc, char **argv)
struct bpf_buffer *rb = NULL;
struct bootstrap_bpf *skel;
int err;

// parse the args manually for demo purpose
if (argc > 3 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
print_usage();
return 0;
Expand Down
11 changes: 6 additions & 5 deletions examples/runqlat/runqlat.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct env {
char *cgroupspath;
bool cg;
} env = {
.interval = 99999999,
.interval = 1,
.times = 99999999,
};

Expand All @@ -45,7 +45,6 @@ const char argp_program_doc[] =
"EXAMPLES:\n"
" runqlat # summarize run queue latency as a histogram\n"
" runqlat 1 10 # print 1 second summaries, 10 times\n"
" runqlat -mT 1 # 1s summaries, milliseconds, and timestamps\n"
" runqlat -P # show each PID separately\n"
" runqlat -p 185 # trace PID 185 only\n"
" runqlat -c CG # Trace process under cgroupsPath CG\n";
Expand Down Expand Up @@ -108,12 +107,14 @@ int main(int argc, char **argv)
int err;
int idx, cg_map_fd;
int cgfd = -1;


// parse the args manually for demo purpose
if (argc > 3 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
print_usage();
return 0;
}
if (argc == 3) {
} else if (argc == 2 && strcmp(argv[1], "-P")) {
env.per_process = true;
} else if (argc == 3) {
env.interval = atoi(argv[1]);
env.times = atoi(argv[2]);
} else if (argc == 2) {
Expand Down
2 changes: 1 addition & 1 deletion third_party/bpftool
Submodule bpftool updated 1 files
+89 −1 src/gen.c
21 changes: 20 additions & 1 deletion wasm-sdk/libbpf-wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ struct bpf_prog_skeleton {
struct bpf_object_skeleton {
size_t sz; /* size of this struct, for forward/backward compatibility */
const char *name;
const void *data;
void *data;
size_t data_sz;
size_t rodata_offset; /* offset of rodata section */
size_t bss_offset; /* offset of bss section */

bpf_object_skel obj;

Expand Down Expand Up @@ -92,6 +94,15 @@ static inline int libbpf_err_errno(int ret) {
static int bpf_map__fd(const struct bpf_map *map) {
return wasm_bpf_map_fd_by_name(map->obj_ptr, map->name);
}

static bool
str_has_surfix(const char *str, const char *surfix) {
size_t str_len = strlen(str);
size_t surfix_len = strlen(surfix);
if (str_len < surfix_len) return false;
return strcmp(str + str_len - surfix_len, surfix) == 0;
}

struct bpf_object_open_opts;
static int bpf_object__open_skeleton(struct bpf_object_skeleton *s,
const struct bpf_object_open_opts *opts) {
Expand All @@ -105,6 +116,14 @@ static int bpf_object__open_skeleton(struct bpf_object_skeleton *s,
if (!*map_skel->map) return -1;
strncpy((*map_skel->map)->name, map_skel->name,
sizeof((*map_skel->map)->name));
if (str_has_surfix(map_skel->name, "rodata") && map_skel->mmaped) {
// set the address to mmaped rodata variable
*map_skel->mmaped = s->data + s->rodata_offset;
}
// it will not work for bss, because bss is not mmaped
// if (str_has_surfix(map_skel->name, "bss") && map_skel->mmaped) {
// *map_skel->mmaped = s->data + s->bss_offset;
// }
}

for (int i = 0; i < s->prog_cnt; i++) {
Expand Down