diff mbox series

[bpf-next,1/2] libbpf: Skip BTF_KIND_FWD when counting duplicated type names

Message ID 20220222074524.1027060-2-xukuohai@huawei.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series libbpf: Fix btf dump error for BTF_KIND_FWD | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 3 maintainers not CCed: kpsingh@kernel.org netdev@vger.kernel.org john.fastabend@gmail.com
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning WARNING: line length of 84 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next success VM_Test

Commit Message

Xu Kuohai Feb. 22, 2022, 7:45 a.m. UTC
If a FWD appears in the BTF before a STRUCT with the same name, the
STRUCT is dumped with a conflicted name:

    $ bpftool btf dump file vmlinux format raw | grep "'unix_sock'"
    [81287] FWD 'unix_sock' fwd_kind=struct
    [89336] STRUCT 'unix_sock' size=1024 vlen=14

    $ bpftool btf dump file vmlinux format c | grep "struct unix_sock"
    struct unix_sock;
    struct unix_sock___2 {	<--- conflict, the "___2" is unexpected
		    struct unix_sock___2 *unix_sk;

Fixes: 351131b51c7a ("libbpf: add btf_dump API for BTF-to-C conversion")
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
---
 tools/lib/bpf/btf_dump.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

Comments

Song Liu Feb. 23, 2022, 5:38 a.m. UTC | #1
On Mon, Feb 21, 2022 at 11:41 PM Xu Kuohai <xukuohai@huawei.com> wrote:
>
> If a FWD appears in the BTF before a STRUCT with the same name, the
> STRUCT is dumped with a conflicted name:
>
>     $ bpftool btf dump file vmlinux format raw | grep "'unix_sock'"
>     [81287] FWD 'unix_sock' fwd_kind=struct
>     [89336] STRUCT 'unix_sock' size=1024 vlen=14
>
>     $ bpftool btf dump file vmlinux format c | grep "struct unix_sock"
>     struct unix_sock;
>     struct unix_sock___2 {      <--- conflict, the "___2" is unexpected
>                     struct unix_sock___2 *unix_sk;
>
> Fixes: 351131b51c7a ("libbpf: add btf_dump API for BTF-to-C conversion")
> Signed-off-by: Xu Kuohai <xukuohai@huawei.com>

Acked-by: Song Liu <songliubraving@fb.com>
Andrii Nakryiko Feb. 23, 2022, 10:42 p.m. UTC | #2
On Mon, Feb 21, 2022 at 11:34 PM Xu Kuohai <xukuohai@huawei.com> wrote:
>
> If a FWD appears in the BTF before a STRUCT with the same name, the
> STRUCT is dumped with a conflicted name:
>
>     $ bpftool btf dump file vmlinux format raw | grep "'unix_sock'"
>     [81287] FWD 'unix_sock' fwd_kind=struct
>     [89336] STRUCT 'unix_sock' size=1024 vlen=14
>
>     $ bpftool btf dump file vmlinux format c | grep "struct unix_sock"
>     struct unix_sock;
>     struct unix_sock___2 {      <--- conflict, the "___2" is unexpected
>                     struct unix_sock___2 *unix_sk;
>
> Fixes: 351131b51c7a ("libbpf: add btf_dump API for BTF-to-C conversion")
> Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
> ---
>  tools/lib/bpf/btf_dump.c | 16 +++++++++-------
>  1 file changed, 9 insertions(+), 7 deletions(-)
>
> diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
> index 07ebe70d3a30..55079efbd8f1 100644
> --- a/tools/lib/bpf/btf_dump.c
> +++ b/tools/lib/bpf/btf_dump.c
> @@ -1505,13 +1505,15 @@ static const char *btf_dump_resolve_name(struct btf_dump *d, __u32 id,
>         if (s->name_resolved)
>                 return *cached_name ? *cached_name : orig_name;
>
> -       dup_cnt = btf_dump_name_dups(d, name_map, orig_name);
> -       if (dup_cnt > 1) {
> -               const size_t max_len = 256;
> -               char new_name[max_len];
> -
> -               snprintf(new_name, max_len, "%s___%zu", orig_name, dup_cnt);
> -               *cached_name = strdup(new_name);
> +       if (!btf_is_fwd(t)) {

let's handle FWD as a special case and not touch existing logic. Maybe
something like this?

if (btf_is_fwd(t) || (btf_is_enum(t) && btf_vlen(t) == 0)) {
    s->name_resolved = 1;
    return orig_name;

}


btf_is_enum() part should handle enum forward declarations (they are
represented as enums with no values defined)


> +               dup_cnt = btf_dump_name_dups(d, name_map, orig_name);
> +               if (dup_cnt > 1) {
> +                       const size_t max_len = 256;
> +                       char new_name[max_len];
> +
> +                       snprintf(new_name, max_len, "%s___%zu", orig_name, dup_cnt);
> +                       *cached_name = strdup(new_name);
> +               }
>         }
>
>         s->name_resolved = 1;
> --
> 2.30.2
>
diff mbox series

Patch

diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index 07ebe70d3a30..55079efbd8f1 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -1505,13 +1505,15 @@  static const char *btf_dump_resolve_name(struct btf_dump *d, __u32 id,
 	if (s->name_resolved)
 		return *cached_name ? *cached_name : orig_name;
 
-	dup_cnt = btf_dump_name_dups(d, name_map, orig_name);
-	if (dup_cnt > 1) {
-		const size_t max_len = 256;
-		char new_name[max_len];
-
-		snprintf(new_name, max_len, "%s___%zu", orig_name, dup_cnt);
-		*cached_name = strdup(new_name);
+	if (!btf_is_fwd(t)) {
+		dup_cnt = btf_dump_name_dups(d, name_map, orig_name);
+		if (dup_cnt > 1) {
+			const size_t max_len = 256;
+			char new_name[max_len];
+
+			snprintf(new_name, max_len, "%s___%zu", orig_name, dup_cnt);
+			*cached_name = strdup(new_name);
+		}
 	}
 
 	s->name_resolved = 1;