libbpf: Fix btf__align_of() by taking into account field offsets
authorAndrii Nakryiko <andrii@kernel.org>
Mon, 12 Dec 2022 21:15:03 +0000 (13:15 -0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 10 Mar 2023 08:32:49 +0000 (09:32 +0100)
commit9e93b377dccf5a84b7eaf9633bbff1fd3d7977ab
tree908268d0d45e5b85f57978ecdf55384cb899893f
parentf1fe40120de6ad4ffa8299fde035a5feba10d4fb
libbpf: Fix btf__align_of() by taking into account field offsets

[ Upstream commit 25a4481b4136af7794e1df2d6c90ed2f354d60ce ]

btf__align_of() is supposed to be return alignment requirement of
a requested BTF type. For STRUCT/UNION it doesn't always return correct
value, because it calculates alignment only based on field types. But
for packed structs this is not enough, we need to also check field
offsets and struct size. If field offset isn't aligned according to
field type's natural alignment, then struct must be packed. Similarly,
if struct size is not a multiple of struct's natural alignment, then
struct must be packed as well.

This patch fixes this issue precisely by additionally checking these
conditions.

Fixes: 3d208f4ca111 ("libbpf: Expose btf__align_of() API")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20221212211505.558851-5-andrii@kernel.org
Signed-off-by: Sasha Levin <sashal@kernel.org>
tools/lib/bpf/btf.c