Skip to content

Commit

Permalink
libbpf: temorary WA for invalid BTF info generated by Zig
Browse files Browse the repository at this point in the history
close #3

Signed-off-by: Tw <weii.tan>
  • Loading branch information
Tw committed May 9, 2023
1 parent 85a07eb commit bad505e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v2
- run: sudo apt-get install -y libelf-dev libz-dev
- run: uname -a
- run: zig build test
- run: sudo ./zig-out/bin/test
lint:
Expand Down
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ fn create_libbpf(b: *Builder, target: std.zig.CrossTarget, optimize: std.builtin
const libbpfFlags = [_][]const u8{
"-D_LARGEFILE64_SOURCE",
"-D_FILE_OFFSET_BITS=64",
"-DZIG_BTF_WA",
};
libbpf.addCSourceFiles(&libbpfSources, &libbpfFlags);
libbpf.addIncludePath("external/libbpf/include");
Expand Down
18 changes: 18 additions & 0 deletions external/libbpf/src/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -2780,6 +2780,20 @@ static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
m->offset = 0;
}
}

#ifdef ZIG_BTF_WA
// https://github.com/tw4452852/zbpf/issues/3
else if (btf_is_ptr(t)) {
t->name_off = 0;
} else if (btf_is_fwd(t) || btf_is_struct(t)) {
char *name = (char *)btf__name_by_offset(btf, t->name_off);
while (*name) {
if (*name == '.')
*name = '_';
name++;
}
}
#endif
}

return 0;
Expand Down Expand Up @@ -3121,7 +3135,11 @@ static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
}

sanitize = btf_needs_sanitization(obj);
#ifdef ZIG_BTF_WA
if (true) {
#else
if (sanitize) {
#endif
const void *raw_data;
__u32 sz;

Expand Down
2 changes: 1 addition & 1 deletion samples/ringbuf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var my_pid = bpf.Map.ArrayMap("my_pid", u32, 1, 0).init();

var n: u32 = 0;

export fn test_ringbuf(_: *opaque {}) linksection("kprobe/do_nanosleep") c_int {
export fn test_ringbuf() linksection("kprobe/do_nanosleep") c_int {
const pid = my_pid.lookup(0) orelse return 1;

const cur_pid = @truncate(u32, helpers.get_current_pid_tgid());
Expand Down

0 comments on commit bad505e

Please sign in to comment.