Skip to content

Commit 80b1e77

Browse files
Ze Gaoyonghong-song
authored andcommitted
libbpf-tools: fix fentry_try_attach bug
the bpf verifier would complain on program exit without initing R0 but the buggy implementation makes up one BPF_EXIT instruction only, which would be rejected by the verifier, and what's worse is that it does not log any err about the try-to-load failure, which makes all tools use this api would silently fall back to kprobe unexpectedly. this patch fixs it and also prints verbose message about the result of the try. Signed-off-by: Ze Gao <[email protected]>
1 parent 5657c7d commit 80b1e77

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

libbpf-tools/trace_helpers.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -993,16 +993,25 @@ bool is_kernel_module(const char *name)
993993

994994
static bool fentry_try_attach(int id)
995995
{
996-
struct bpf_insn insns[] = { { .code = BPF_JMP | BPF_EXIT } };
997-
LIBBPF_OPTS(bpf_prog_load_opts, opts);
998996
int prog_fd, attach_fd;
999-
1000-
opts.expected_attach_type = BPF_TRACE_FENTRY;
1001-
opts.attach_btf_id = id,
1002-
1003-
prog_fd = bpf_prog_load(BPF_PROG_TYPE_TRACING, "test", NULL, insns, 1, &opts);
1004-
if (prog_fd < 0)
997+
char error[4096];
998+
struct bpf_insn insns[] = {
999+
{ .code = BPF_ALU64 | BPF_MOV | BPF_K, .dst_reg = BPF_REG_0, .imm = 0 },
1000+
{ .code = BPF_JMP | BPF_EXIT },
1001+
};
1002+
LIBBPF_OPTS(bpf_prog_load_opts, opts,
1003+
.expected_attach_type = BPF_TRACE_FENTRY,
1004+
.attach_btf_id = id,
1005+
.log_buf = error,
1006+
.log_size = sizeof(error),
1007+
);
1008+
1009+
prog_fd = bpf_prog_load(BPF_PROG_TYPE_TRACING, "test", "GPL", insns,
1010+
sizeof(insns) / sizeof(struct bpf_insn), &opts);
1011+
if (prog_fd < 0) {
1012+
fprintf(stderr, "failed to try attaching to fentry: %s\n", error);
10051013
return false;
1014+
}
10061015

10071016
attach_fd = bpf_raw_tracepoint_open(NULL, prog_fd);
10081017
if (attach_fd >= 0)

0 commit comments

Comments
 (0)