selftests/bpf: validate skeleton gen handles skipped fields
Subsystems (6)
Andrii Nakryiko
maintainerPATCH 1/2 selftests/bpf: validate skeleton gen handles skipped fields
Adjust static_linked selftests to test a mix of global and static variables and their handling of bpftool's skeleton generation code.
Signed-off-by Andrii Nakryiko <[email protected]>
--- tools/testing/selftests/bpf/prog_tests/static_linked.c | 4 ++-- tools/testing/selftests/bpf/progs/linked_maps1.c | 2 +- tools/testing/selftests/bpf/progs/test_static_linked1.c | 2 +- tools/testing/selftests/bpf/progs/test_static_linked2.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/prog_tests/static_linked.c +2 -2
@@ -27,8 +27,8 @@ void test_static_linked(void)/* trigger */ usleep (1 ); -ASSERT_EQ (skel->bss->var1,1 *2 +2 +3 ,"var1" ); -ASSERT_EQ (skel->bss->var2,4 *3 +5 +6 ,"var2" ); +ASSERT_EQ (skel->data->var1,1 *2 +2 +3 ,"var1" ); +ASSERT_EQ (skel->data->var2,4 *3 +5 +6 ,"var2" ); cleanup:test_static_linked__destroy (skel);
tools/testing/selftests/bpf/progs/linked_maps1.c +1 -1
@@ -75,7 +75,7 @@ int BPF_PROG(handler_exit1) val =bpf_map_lookup_elem (&map_weak, &key);if (val) output_weak1 = *val; - +return 0 ; }
tools/testing/selftests/bpf/progs/test_static_linked1.c +1 -1
@@ -7,7 +7,7 @@/* 8-byte aligned .data */ static volatile long static_var1 =2 ;static volatile int static_var2 =3 ; -int var1 =0 ; +int var1 =-1 ;/* 4-byte aligned .rodata */ const volatile int rovar1;
tools/testing/selftests/bpf/progs/test_static_linked2.c +1 -1
@@ -7,7 +7,7 @@/* 4-byte aligned .data */ static volatile int static_var1 =5 ;static volatile int static_var2 =6 ; -int var2 =0 ; +int var2 =-1 ;/* 8-byte aligned .rodata */ const volatile long rovar2;
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH 2/2 libbpf: reject static maps
Static maps never really worked with libbpf, because all such maps were always silently resolved to the very first map. Detect static maps (both legacy and BTF-defined) and report user-friendly error.
Tested locally by switching few maps (legacy and BTF-defined) in selftests to static ones and verifying that now libbpf rejects them loudly.
Signed-off-by Andrii Nakryiko <[email protected]>
--- tools/lib/bpf/libbpf.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)
tools/lib/bpf/libbpf.c +20 -5
@@ -1795,7 +1795,6 @@ static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)if (!symbols)return -EINVAL ; - scn =elf_sec_by_idx (obj, obj->efile.maps_shndx); data =elf_sec_data (obj, scn);if (!scn || !data) {
@@ -1848,6 +1847,7 @@ static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)if (IS_ERR (map))return PTR_ERR (map); + map_name =elf_sym_str (obj, sym.st_name);if (!map_name) { pr_warn("failed to get map #%d name sym string for obj %s\n",
@@ -1855,6 +1855,12 @@ static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)return -LIBBPF_ERRNO__FORMAT ; } +if (GELF_ST_TYPE (sym.st_info) ==STT_SECTION + ||GELF_ST_BIND (sym.st_info) ==STB_LOCAL ) { +pr_warn ("map '%s' (legacy): static maps are not supported\n" , map_name); +return -ENOTSUP ; + } + map->libbpf_type =LIBBPF_MAP_UNSPEC ; map->sec_idx = sym.st_shndx; map->sec_offset = sym.st_value;
@@ -2262,6 +2268,16 @@ static void fill_map_from_def(struct bpf_map *map, const struct btf_map_def *defpr_debug ("map '%s': found inner map definition.\n" , map->name); } +static const char *btf_var_linkage_str (__u32 linkage) +{ +switch (linkage) { +case BTF_VAR_STATIC :return "static" ; +case BTF_VAR_GLOBAL_ALLOCATED :return "global" ; +case BTF_VAR_GLOBAL_EXTERN :return "extern" ; +default :return "unknown" ; + } +} + static int bpf_object__init_user_btf_map(struct bpf_object *obj, const struct btf_type *sec, int var_idx, int sec_idx,
@@ -2294,10 +2310,9 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj, map_name, btf_kind_str(var));return -EINVAL ; } -if (var_extra->linkage !=BTF_VAR_GLOBAL_ALLOCATED && - var_extra->linkage !=BTF_VAR_STATIC ) { -pr_warn ("map '%s': unsupported var linkage %u.\n" , - map_name, var_extra->linkage); +if (var_extra->linkage !=BTF_VAR_GLOBAL_ALLOCATED ) { +pr_warn ("map '%s': unsupported map linkage %s.\n" , + map_name,btf_var_linkage_str (var_extra->linkage));return -EOPNOTSUPP ; }
Signature · 1 line
2.30.2