Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libbpf: Fix btf_dump's packed struct determination

Fix bug in btf_dump's logic of determining if a given struct type is
packed or not. The notion of "natural alignment" is not needed and is
even harmful in this case, so drop it altogether. The biggest difference
in btf_is_struct_packed() compared to its original implementation is
that we don't really use btf__align_of() to determine overall alignment
of a struct type (because it could be 1 for both packed and non-packed
struct, depending on specifci field definitions), and just use field's
actual alignment to calculate whether any field is requiring packing or
struct's size overall necessitates packing.

Add two simple test cases that demonstrate the difference this change
would make.

Fixes: ea2ce1ba99aa ("libbpf: Fix BTF-to-C converter's padding logic")
Reported-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/bpf/20221215183605.4149488-1-andrii@kernel.org

authored by

Andrii Nakryiko and committed by
Daniel Borkmann
4fb877aa b148c8b9

+25 -27
+6 -27
tools/lib/bpf/btf_dump.c
··· 830 830 } 831 831 } 832 832 833 - static int btf_natural_align_of(const struct btf *btf, __u32 id) 834 - { 835 - const struct btf_type *t = btf__type_by_id(btf, id); 836 - int i, align, vlen; 837 - const struct btf_member *m; 838 - 839 - if (!btf_is_composite(t)) 840 - return btf__align_of(btf, id); 841 - 842 - align = 1; 843 - m = btf_members(t); 844 - vlen = btf_vlen(t); 845 - for (i = 0; i < vlen; i++, m++) { 846 - align = max(align, btf__align_of(btf, m->type)); 847 - } 848 - 849 - return align; 850 - } 851 - 852 833 static bool btf_is_struct_packed(const struct btf *btf, __u32 id, 853 834 const struct btf_type *t) 854 835 { 855 836 const struct btf_member *m; 856 - int align, i, bit_sz; 837 + int max_align = 1, align, i, bit_sz; 857 838 __u16 vlen; 858 - 859 - align = btf_natural_align_of(btf, id); 860 - /* size of a non-packed struct has to be a multiple of its alignment */ 861 - if (align && (t->size % align) != 0) 862 - return true; 863 839 864 840 m = btf_members(t); 865 841 vlen = btf_vlen(t); 866 842 /* all non-bitfield fields have to be naturally aligned */ 867 843 for (i = 0; i < vlen; i++, m++) { 868 - align = btf_natural_align_of(btf, m->type); 844 + align = btf__align_of(btf, m->type); 869 845 bit_sz = btf_member_bitfield_size(t, i); 870 846 if (align && bit_sz == 0 && m->offset % (8 * align) != 0) 871 847 return true; 848 + max_align = max(align, max_align); 872 849 } 873 - 850 + /* size of a non-packed struct has to be a multiple of its alignment */ 851 + if (t->size % max_align != 0) 852 + return true; 874 853 /* 875 854 * if original struct was marked as packed, but its layout is 876 855 * naturally aligned, we'll detect that it's not packed
+19
tools/testing/selftests/bpf/progs/btf_dump_test_case_packing.c
··· 116 116 long: 0; 117 117 }; 118 118 119 + /* ----- START-EXPECTED-OUTPUT ----- */ 120 + struct nested_packed_struct { 121 + int a; 122 + char b; 123 + } __attribute__((packed)); 124 + 125 + struct outer_nonpacked_struct { 126 + short a; 127 + struct nested_packed_struct b; 128 + }; 129 + 130 + struct outer_packed_struct { 131 + short a; 132 + struct nested_packed_struct b; 133 + } __attribute__((packed)); 134 + 135 + /* ------ END-EXPECTED-OUTPUT ------ */ 119 136 120 137 int f(struct { 121 138 struct packed_trailing_space _1; ··· 145 128 union jump_code_union _8; 146 129 struct outer_implicitly_packed_struct _9; 147 130 struct usb_host_endpoint _10; 131 + struct outer_nonpacked_struct _11; 132 + struct outer_packed_struct _12; 148 133 } *_) 149 134 { 150 135 return 0;