From: Antoine Tenart <atenart@kernel.org>
To: dwarves@vger.kernel.org
Cc: Antoine Tenart <atenart@kernel.org>, pvalerio@redhat.com
Subject: [PATCH 1/2] btf_loader: infer alignment for zero-length arrays
Date: Wed, 25 Jun 2025 17:23:19 +0200 [thread overview]
Message-ID: <20250625152329.28601-2-atenart@kernel.org> (raw)
In-Reply-To: <20250625152329.28601-1-atenart@kernel.org>
Zero sized arrays do not go through the bitfield fixup logic, which in
turn makes them to skip the alignment infer logic. The result is
zero-length arrays being incorrectly aligned when an explicit statement
is needed. E.g. this happens for variable length arrays at end of
structures, or for 0-length ones.
Before this patch we can see:
struct skb_ext {
refcount_t refcnt; /* 0 4 */
u8 offset[4]; /* 4 4 */
u8 chunks; /* 8 1 */
/* XXX 7 bytes hole, try to pack */
char data[]; /* 16 0 */
/* size: 16, cachelines: 1, members: 4 */
/* sum members: 9, holes: 1, sum holes: 7 */
/* last cacheline: 16 bytes */
} __attribute__((__aligned__(8)));
offsetof(struct skb_ext, data) returns 9.
After this patch we get:
struct skb_ext {
refcount_t refcnt; /* 0 4 */
u8 offset[4]; /* 4 4 */
u8 chunks; /* 8 1 */
/* XXX 7 bytes hole, try to pack */
char data[] __attribute__((__aligned__(8))); /* 16 0 */
/* size: 16, cachelines: 1, members: 4 */
/* sum members: 9, holes: 1, sum holes: 7 */
/* forced alignments: 1, forced holes: 1, sum forced holes: 7 */
/* last cacheline: 16 bytes */
} __attribute__((__aligned__(8)));
offsetof(struct skb_ext, data) returns 16.
Signed-off-by: Antoine Tenart <atenart@kernel.org>
---
btf_loader.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/btf_loader.c b/btf_loader.c
index f4f9f65289b5..76771afedd95 100644
--- a/btf_loader.c
+++ b/btf_loader.c
@@ -648,6 +648,10 @@ static int class__fixup_btf_bitfields(const struct conf_load *conf, struct tag *
/* if BTF data is incorrect and has size == 0, skip field,
* instead of crashing */
if (pos->byte_size == 0) {
+ pos->alignment = class__infer_alignment(conf,
+ pos->byte_offset,
+ tag__natural_alignment(type, cu),
+ smallest_offset);
continue;
}
--
2.49.0
next prev parent reply other threads:[~2025-06-25 15:23 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-25 15:23 [PATCH 0/2] pahole: BTF alignment inference improvements Antoine Tenart
2025-06-25 15:23 ` Antoine Tenart [this message]
2025-07-04 16:34 ` [PATCH 1/2] btf_loader: infer alignment for zero-length arrays Alan Maguire
2025-07-08 13:55 ` Antoine Tenart
2025-06-25 15:23 ` [PATCH 2/2] btf_loader: fix smallest offset in case of bitfields Antoine Tenart
2025-07-04 16:40 ` Alan Maguire
2025-07-08 14:01 ` Antoine Tenart
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250625152329.28601-2-atenart@kernel.org \
--to=atenart@kernel.org \
--cc=dwarves@vger.kernel.org \
--cc=pvalerio@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox