Skip to content

Commit

Permalink
bpftool: Use libbpf_get_error() to check error
Browse files Browse the repository at this point in the history
Currently, LIBBPF_STRICT_ALL mode is enabled by default for
bpftool which means on error cases, some libbpf APIs would
return NULL pointers. This makes IS_ERR check failed to detect
such cases and result in segfault error. Use libbpf_get_error()
instead like we do in libbpf itself.

Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20211115012436.3143318-1-hengqi.chen@gmail.com
  • Loading branch information
Hengqi Chen authored and Andrii Nakryiko committed Nov 15, 2021
1 parent c874dff commit e504389
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
9 changes: 5 additions & 4 deletions tools/bpf/bpftool/btf.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,9 @@ static int dump_btf_c(const struct btf *btf,
int err = 0, i;

d = btf_dump__new(btf, btf_dump_printf, NULL, NULL);
if (IS_ERR(d))
return PTR_ERR(d);
err = libbpf_get_error(d);
if (err)
return err;

printf("#ifndef __VMLINUX_H__\n");
printf("#define __VMLINUX_H__\n");
Expand Down Expand Up @@ -549,8 +550,8 @@ static int do_dump(int argc, char **argv)
}

btf = btf__parse_split(*argv, base ?: base_btf);
if (IS_ERR(btf)) {
err = -PTR_ERR(btf);
err = libbpf_get_error(btf);
if (err) {
btf = NULL;
p_err("failed to load BTF from %s: %s",
*argv, strerror(err));
Expand Down
10 changes: 6 additions & 4 deletions tools/bpf/bpftool/gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,9 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)
int i, err = 0;

d = btf_dump__new(btf, codegen_btf_dump_printf, NULL, NULL);
if (IS_ERR(d))
return PTR_ERR(d);
err = libbpf_get_error(d);
if (err)
return err;

bpf_object__for_each_map(map, obj) {
/* only generate definitions for memory-mapped internal maps */
Expand Down Expand Up @@ -719,10 +720,11 @@ static int do_skeleton(int argc, char **argv)
get_obj_name(obj_name, file);
opts.object_name = obj_name;
obj = bpf_object__open_mem(obj_data, file_sz, &opts);
if (IS_ERR(obj)) {
err = libbpf_get_error(obj);
if (err) {
char err_buf[256];

libbpf_strerror(PTR_ERR(obj), err_buf, sizeof(err_buf));
libbpf_strerror(err, err_buf, sizeof(err_buf));
p_err("failed to open BPF object file: %s", err_buf);
obj = NULL;
goto out;
Expand Down
7 changes: 4 additions & 3 deletions tools/bpf/bpftool/iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ static int do_pin(int argc, char **argv)
}

obj = bpf_object__open(objfile);
if (IS_ERR(obj)) {
err = libbpf_get_error(obj);
if (err) {
p_err("can't open objfile %s", objfile);
goto close_map_fd;
}
Expand All @@ -64,8 +65,8 @@ static int do_pin(int argc, char **argv)
}

link = bpf_program__attach_iter(prog, &iter_opts);
if (IS_ERR(link)) {
err = PTR_ERR(link);
err = libbpf_get_error(link);
if (err) {
p_err("attach_iter failed for program %s",
bpf_program__name(prog));
goto close_obj;
Expand Down
10 changes: 5 additions & 5 deletions tools/bpf/bpftool/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ static struct btf *get_map_kv_btf(const struct bpf_map_info *info)
if (info->btf_vmlinux_value_type_id) {
if (!btf_vmlinux) {
btf_vmlinux = libbpf_find_kernel_btf();
if (IS_ERR(btf_vmlinux))
if (libbpf_get_error(btf_vmlinux))
p_err("failed to get kernel btf");
}
return btf_vmlinux;
Expand All @@ -832,13 +832,13 @@ static struct btf *get_map_kv_btf(const struct bpf_map_info *info)

static void free_map_kv_btf(struct btf *btf)
{
if (!IS_ERR(btf) && btf != btf_vmlinux)
if (!libbpf_get_error(btf) && btf != btf_vmlinux)
btf__free(btf);
}

static void free_btf_vmlinux(void)
{
if (!IS_ERR(btf_vmlinux))
if (!libbpf_get_error(btf_vmlinux))
btf__free(btf_vmlinux);
}

Expand All @@ -863,8 +863,8 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,

if (wtr) {
btf = get_map_kv_btf(info);
if (IS_ERR(btf)) {
err = PTR_ERR(btf);
err = libbpf_get_error(btf);
if (err) {
goto exit_free;
}

Expand Down
14 changes: 7 additions & 7 deletions tools/bpf/bpftool/struct_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static const struct btf *get_btf_vmlinux(void)
return btf_vmlinux;

btf_vmlinux = libbpf_find_kernel_btf();
if (IS_ERR(btf_vmlinux))
if (libbpf_get_error(btf_vmlinux))
p_err("struct_ops requires kernel CONFIG_DEBUG_INFO_BTF=y");

return btf_vmlinux;
Expand All @@ -45,7 +45,7 @@ static const char *get_kern_struct_ops_name(const struct bpf_map_info *info)
const char *st_ops_name;

kern_btf = get_btf_vmlinux();
if (IS_ERR(kern_btf))
if (libbpf_get_error(kern_btf))
return "<btf_vmlinux_not_found>";

t = btf__type_by_id(kern_btf, info->btf_vmlinux_value_type_id);
Expand All @@ -63,7 +63,7 @@ static __s32 get_map_info_type_id(void)
return map_info_type_id;

kern_btf = get_btf_vmlinux();
if (IS_ERR(kern_btf)) {
if (libbpf_get_error(kern_btf)) {
map_info_type_id = PTR_ERR(kern_btf);
return map_info_type_id;
}
Expand Down Expand Up @@ -415,7 +415,7 @@ static int do_dump(int argc, char **argv)
}

kern_btf = get_btf_vmlinux();
if (IS_ERR(kern_btf))
if (libbpf_get_error(kern_btf))
return -1;

if (!json_output) {
Expand Down Expand Up @@ -495,7 +495,7 @@ static int do_register(int argc, char **argv)
file = GET_ARG();

obj = bpf_object__open(file);
if (IS_ERR_OR_NULL(obj))
if (libbpf_get_error(obj))
return -1;

set_max_rlimit();
Expand All @@ -516,7 +516,7 @@ static int do_register(int argc, char **argv)
continue;

link = bpf_map__attach_struct_ops(map);
if (IS_ERR(link)) {
if (libbpf_get_error(link)) {
p_err("can't register struct_ops %s: %s",
bpf_map__name(map),
strerror(-PTR_ERR(link)));
Expand Down Expand Up @@ -596,7 +596,7 @@ int do_struct_ops(int argc, char **argv)

err = cmd_select(cmds, argc, argv, do_help);

if (!IS_ERR(btf_vmlinux))
if (!libbpf_get_error(btf_vmlinux))
btf__free(btf_vmlinux);

return err;
Expand Down

0 comments on commit e504389

Please sign in to comment.