From: Jakub Jelinek <jakub@redhat.com>
To: linux-toolchains@vger.kernel.org
Subject: GCC 15 -fzero-init-padding-bits= option and redzone clobber
Date: Thu, 28 Nov 2024 12:19:10 +0100 [thread overview]
Message-ID: <Z0hRrrNU3Q+ro2T7@tucnak> (raw)
Hi!
This is just a FYI, since today GCC 15 no longer zero initializes padding
bits in unions where the standard doesn't require it.
So e.g.
void foo (void)
{
union U { int a; long b[64]; };
/* This clears everything including padding bits,
required by at least C23 (note, GCC 15 defaults to -std=gnu23) */
union U u = {};
/* This used to clear everything, but only clears
v.a in GCC 15 by default. */
union U v = {0};
}
If you want to keep the old behavior e.g. for security purposes (the whole
union can be copied to user etc.), one can use
-fzero-init-padding-bits=unions to restore the GCC 14 and older behavior.
And, -fzero-init-padding-bits=all can be used to clear padding bits even
in cases where the standard doesn't require them even in structures, e.g.
void bar (void)
{
struct S { char a; int b; };
/* C23 requires padding bits to be cleared here. */
struct S s = {};
/* But not here. -fzero-init-padding-bits=all does that anyway. */
struct S t = { 1, 2 };
}
Note, there is also __builtin_clear_padding builtin to clear padding bits
already since GCC 11, though it doesn't clear bits in unions unless they
are padding bits for all possible members, as it doesn't know which union
member is current.
Another new feature since today that might be relevant to kernel is
the "redzone" inline asm clobber.
It can/should be used on inline asm which does or could clobber memory
below the stack pointer and so its presence must disable use of redzone
(currently on x86_64 and powerpc*), whether because say pushf/pop pair
or because the inline asm performs calls without taking into account
the red zone (e.g. on x86_64 that would be something like subtracting
128 from %rsp at the start and restoring at the end).
In the past I think kernel used some hacks like clobbering rsp, that is
something that really shouldn't be used even if it happened to work,
inline asm is of course allowed to change the stack pointer temporarily,
but before returning (if it returns at all) it needs to restore it back,
and clobbers are not about temporary changes during the execution of inline
asm, but about changes from the start to the end of inline asm.
So
asm ("call something" : ... : ... : "redzone");
(of course it likely needs tons of other clobbers for call clobbered
registers unless it saves them and restores them in the inline asm or
in whatever it calls).
Jakub
next reply other threads:[~2024-11-28 11:19 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-28 11:19 Jakub Jelinek [this message]
2024-11-29 12:52 ` GCC 15 -fzero-init-padding-bits= option and redzone clobber Peter Zijlstra
2024-11-29 13:23 ` Jakub Jelinek
2024-11-29 17:55 ` Linus Torvalds
2024-11-29 18:21 ` Linus Torvalds
2024-11-30 11:10 ` Segher Boessenkool
2024-11-30 17:43 ` Linus Torvalds
2024-11-30 22:19 ` Segher Boessenkool
2024-11-30 22:43 ` Linus Torvalds
2024-11-30 22:45 ` Linus Torvalds
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=Z0hRrrNU3Q+ro2T7@tucnak \
--to=jakub@redhat.com \
--cc=linux-toolchains@vger.kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).