Skip to content

Commit 6b6e6b1

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
libbpf: Accomodate DWARF/compiler bug with duplicated identical arrays
In some cases compiler seems to generate distinct DWARF types for identical arrays within the same CU. That seems like a bug, but it's already out there and breaks type graph equivalence checks, so accommodate it anyway by checking for identical arrays, regardless of their type ID. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Song Liu <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent f86524e commit 6b6e6b1

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

tools/lib/bpf/btf.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3785,6 +3785,19 @@ static inline __u16 btf_fwd_kind(struct btf_type *t)
37853785
return btf_kflag(t) ? BTF_KIND_UNION : BTF_KIND_STRUCT;
37863786
}
37873787

3788+
/* Check if given two types are identical ARRAY definitions */
3789+
static int btf_dedup_identical_arrays(struct btf_dedup *d, __u32 id1, __u32 id2)
3790+
{
3791+
struct btf_type *t1, *t2;
3792+
3793+
t1 = btf_type_by_id(d->btf, id1);
3794+
t2 = btf_type_by_id(d->btf, id2);
3795+
if (!btf_is_array(t1) || !btf_is_array(t2))
3796+
return 0;
3797+
3798+
return btf_equal_array(t1, t2);
3799+
}
3800+
37883801
/*
37893802
* Check equivalence of BTF type graph formed by candidate struct/union (we'll
37903803
* call it "candidate graph" in this description for brevity) to a type graph
@@ -3895,8 +3908,18 @@ static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
38953908
canon_id = resolve_fwd_id(d, canon_id);
38963909

38973910
hypot_type_id = d->hypot_map[canon_id];
3898-
if (hypot_type_id <= BTF_MAX_NR_TYPES)
3899-
return hypot_type_id == cand_id;
3911+
if (hypot_type_id <= BTF_MAX_NR_TYPES) {
3912+
/* In some cases compiler will generate different DWARF types
3913+
* for *identical* array type definitions and use them for
3914+
* different fields within the *same* struct. This breaks type
3915+
* equivalence check, which makes an assumption that candidate
3916+
* types sub-graph has a consistent and deduped-by-compiler
3917+
* types within a single CU. So work around that by explicitly
3918+
* allowing identical array types here.
3919+
*/
3920+
return hypot_type_id == cand_id ||
3921+
btf_dedup_identical_arrays(d, hypot_type_id, cand_id);
3922+
}
39003923

39013924
if (btf_dedup_hypot_map_add(d, canon_id, cand_id))
39023925
return -ENOMEM;

0 commit comments

Comments
 (0)