libbpf: Fix btf_dump's packed struct determination

[ Upstream commit 4fb877aaa179dcdb1676d55216482febaada457e ]

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
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
Andrii Nakryiko
2022-12-15 10:36:05 -08:00
committed by Greg Kroah-Hartman
parent 7ad30ad8c6
commit b4781477f3
2 changed files with 25 additions and 27 deletions

View File

@@ -116,6 +116,23 @@ struct usb_host_endpoint {
long: 0;
};
/* ----- START-EXPECTED-OUTPUT ----- */
struct nested_packed_struct {
int a;
char b;
} __attribute__((packed));
struct outer_nonpacked_struct {
short a;
struct nested_packed_struct b;
};
struct outer_packed_struct {
short a;
struct nested_packed_struct b;
} __attribute__((packed));
/* ------ END-EXPECTED-OUTPUT ------ */
int f(struct {
struct packed_trailing_space _1;
@@ -128,6 +145,8 @@ int f(struct {
union jump_code_union _8;
struct outer_implicitly_packed_struct _9;
struct usb_host_endpoint _10;
struct outer_nonpacked_struct _11;
struct outer_packed_struct _12;
} *_)
{
return 0;