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

selftests/bpf: Add few corner cases to test padding handling of btf_dump

Add few hand-crafted cases and few randomized cases found using script
from [0] that tests btf_dump's padding logic.

[0] https://lore.kernel.org/bpf/85f83c333f5355c8ac026f835b18d15060725fcb.camel@ericsson.com/

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20221212211505.558851-7-andrii@kernel.org

authored by

Andrii Nakryiko and committed by
Daniel Borkmann
b148c8b9 ea2ce1ba

+164 -1
+60 -1
tools/testing/selftests/bpf/progs/btf_dump_test_case_packing.c
··· 58 58 } __attribute__((packed)); 59 59 }; 60 60 61 - /*------ END-EXPECTED-OUTPUT ------ */ 61 + /* ----- START-EXPECTED-OUTPUT ----- */ 62 + /* 63 + *struct nested_packed_but_aligned_struct { 64 + * int x1; 65 + * int x2; 66 + *}; 67 + * 68 + *struct outer_implicitly_packed_struct { 69 + * char y1; 70 + * struct nested_packed_but_aligned_struct y2; 71 + *} __attribute__((packed)); 72 + * 73 + */ 74 + /* ------ END-EXPECTED-OUTPUT ------ */ 75 + 76 + struct nested_packed_but_aligned_struct { 77 + int x1; 78 + int x2; 79 + } __attribute__((packed)); 80 + 81 + struct outer_implicitly_packed_struct { 82 + char y1; 83 + struct nested_packed_but_aligned_struct y2; 84 + }; 85 + /* ----- START-EXPECTED-OUTPUT ----- */ 86 + /* 87 + *struct usb_ss_ep_comp_descriptor { 88 + * char: 8; 89 + * char bDescriptorType; 90 + * char bMaxBurst; 91 + * short wBytesPerInterval; 92 + *}; 93 + * 94 + *struct usb_host_endpoint { 95 + * long: 64; 96 + * char: 8; 97 + * struct usb_ss_ep_comp_descriptor ss_ep_comp; 98 + * long: 0; 99 + *} __attribute__((packed)); 100 + * 101 + */ 102 + /* ------ END-EXPECTED-OUTPUT ------ */ 103 + 104 + struct usb_ss_ep_comp_descriptor { 105 + char: 8; 106 + char bDescriptorType; 107 + char bMaxBurst; 108 + int: 0; 109 + short wBytesPerInterval; 110 + } __attribute__((packed)); 111 + 112 + struct usb_host_endpoint { 113 + long: 64; 114 + char: 8; 115 + struct usb_ss_ep_comp_descriptor ss_ep_comp; 116 + long: 0; 117 + }; 118 + 62 119 63 120 int f(struct { 64 121 struct packed_trailing_space _1; ··· 126 69 union union_is_never_packed _6; 127 70 union union_does_not_need_packing _7; 128 71 union jump_code_union _8; 72 + struct outer_implicitly_packed_struct _9; 73 + struct usb_host_endpoint _10; 129 74 } *_) 130 75 { 131 76 return 0;
+104
tools/testing/selftests/bpf/progs/btf_dump_test_case_padding.c
··· 128 128 char: 8; 129 129 }; 130 130 131 + /* ----- START-EXPECTED-OUTPUT ----- */ 132 + struct exact_1byte { 133 + char x; 134 + }; 135 + 136 + struct padded_1byte { 137 + char: 8; 138 + }; 139 + 140 + struct exact_2bytes { 141 + short x; 142 + }; 143 + 144 + struct padded_2bytes { 145 + short: 16; 146 + }; 147 + 148 + struct exact_4bytes { 149 + int x; 150 + }; 151 + 152 + struct padded_4bytes { 153 + int: 32; 154 + }; 155 + 156 + struct exact_8bytes { 157 + long x; 158 + }; 159 + 160 + struct padded_8bytes { 161 + long: 64; 162 + }; 163 + 164 + struct ff_periodic_effect { 165 + int: 32; 166 + short magnitude; 167 + long: 0; 168 + short phase; 169 + long: 0; 170 + int: 32; 171 + int custom_len; 172 + short *custom_data; 173 + }; 174 + 175 + struct ib_wc { 176 + long: 64; 177 + long: 64; 178 + int: 32; 179 + int byte_len; 180 + void *qp; 181 + union {} ex; 182 + long: 64; 183 + int slid; 184 + int wc_flags; 185 + long: 64; 186 + char smac[6]; 187 + long: 0; 188 + char network_hdr_type; 189 + }; 190 + 191 + struct acpi_object_method { 192 + long: 64; 193 + char: 8; 194 + char type; 195 + short reference_count; 196 + char flags; 197 + short: 0; 198 + char: 8; 199 + char sync_level; 200 + long: 64; 201 + void *node; 202 + void *aml_start; 203 + union {} dispatch; 204 + long: 64; 205 + int aml_length; 206 + }; 207 + 208 + struct nested_unpacked { 209 + int x; 210 + }; 211 + 212 + struct nested_packed { 213 + struct nested_unpacked a; 214 + char c; 215 + } __attribute__((packed)); 216 + 217 + struct outer_mixed_but_unpacked { 218 + struct nested_packed b1; 219 + short a1; 220 + struct nested_packed b2; 221 + }; 222 + 131 223 /* ------ END-EXPECTED-OUTPUT ------ */ 132 224 133 225 int f(struct { ··· 231 139 struct padding_wo_named_members _6; 232 140 struct padding_weird_1 _7; 233 141 struct padding_weird_2 _8; 142 + struct exact_1byte _100; 143 + struct padded_1byte _101; 144 + struct exact_2bytes _102; 145 + struct padded_2bytes _103; 146 + struct exact_4bytes _104; 147 + struct padded_4bytes _105; 148 + struct exact_8bytes _106; 149 + struct padded_8bytes _107; 150 + struct ff_periodic_effect _200; 151 + struct ib_wc _201; 152 + struct acpi_object_method _202; 153 + struct outer_mixed_but_unpacked _203; 234 154 } *_) 235 155 { 236 156 return 0;