From: Michael Matz <matz@suse.de>
To: Qing Zhao <qing.zhao@oracle.com>
Cc: Martin Uecker <uecker@tugraz.at>,
Kees Cook <keescook@chromium.org>,
Joseph Myers <joseph@codesourcery.com>,
Richard Biener <richard.guenther@gmail.com>,
jakub Jelinek <jakub@redhat.com>,
Qing Zhao via Gcc-patches <gcc-patches@gcc.gnu.org>,
Siddhesh Poyarekar <siddhesh@gotplt.org>,
"isanbard@gmail.com" <isanbard@gmail.com>
Subject: Re: [V2][PATCH 0/3] New attribute "counted_by" to annotate bounds for C99 FAM(PR108896)
Date: Wed, 9 Aug 2023 16:21:11 +0000 (UTC) [thread overview]
Message-ID: <alpine.LSU.2.20.2308091606510.25429@wotan.suse.de> (raw)
In-Reply-To: <9E6E0BBA-A97F-4C94-B188-8E4A620B36DB@oracle.com>
[-- Attachment #1: Type: text/plain, Size: 2779 bytes --]
Hello,
On Wed, 9 Aug 2023, Qing Zhao wrote:
> Although this is an old FAM related issue that does not relate to my current patch
> (and might need to be resolved in a separate patch). I think that it’s necessary to have
> more discussion on this old issue and resolve it.
>
> The first thing that I’d like to confirm is:
>
> What the exact memory layout for the following structure x?
>
> struct foo { int a; short b; char t[]; } x = { .t = { 1, 2, 3 } };
>
> And the key that is confusing me is, where should the field “t” start?
>
> A. Starting at offset 8 as the following:
>
> a 4-bytes
> b 2-bytes
> padding 2-bytes
> t 3-bytes
Why should there be padding before 't'? It's a char array (FAM or not),
so it can be (and should be) placed directly after 'b'. So ...
> B. Starting at offset 6 as the following:
>
> a 4-bytes
> b 2-bytes
> t 3-bytes
... this is the correct layout, when seen in isolation. The discussion
revolves around what should come after 't': if it's a non-FAM struct (with
t[3]), then it's clear that there needs to be padding after it, so to pad
out the whole struct to be 12 bytes long (for sizeof() purpose), as
required by its alignment (due to the int field 'a').
So, should the equivalent FAM struct also have this sizeof()? If no:
there should be a good argument why it shouldn't be similar to the non-FAM
one.
Then there is an argument that the compiler would be fine, when allocating
a single object of such type (not as part of an array!), to only reserve 9
bytes of space for the FAM-struct. Then the question is: should it also
do that for a non-FAM struct (based on the argument that the padding
behind 't' isn't accessible, and hence doesn't need to be alloced). I
think it would be quite unexpected for the compiler to actually reserve
less space than sizeof() implies, so I personally don't buy that argument.
For FAM or non-FAM structs.
Note that if one choses to allocate less space than sizeof implies that
this will have quite some consequences for code generation, in that
sometimes the instruction sequences (e.g. for copying) need to be careful
to never access tail padding that should be there in array context, but
isn't there in single-object context. I think this alone should make it
clear that it's advisable that sizeof() and allocated size agree.
As in: I think sizeof for both structs should return 12, and 12 bytes
should be reserved for objects of such types.
And then the next question is what __builtin_object_size should do with
these: should it return the size with or without padding at end (i.e.
could/should it return 9 even if sizeof is 12). I can see arguments for
both.
Ciao,
Michael.
next prev parent reply other threads:[~2023-08-09 16:21 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-04 19:44 Qing Zhao
2023-08-04 19:44 ` [V2][PATCH 1/3] Provide counted_by attribute to flexible array member field (PR108896) Qing Zhao
2023-08-04 19:44 ` [V2][PATCH 2/3] Use the counted_by atribute info in builtin object size [PR108896] Qing Zhao
2023-08-04 19:44 ` [V2][PATCH 3/3] Use the counted_by attribute information in bound sanitizer[PR108896] Qing Zhao
2023-08-07 16:16 ` [V2][PATCH 0/3] New attribute "counted_by" to annotate bounds for C99 FAM(PR108896) Kees Cook
2023-08-07 16:33 ` Qing Zhao
2023-08-09 19:17 ` Kees Cook
2023-08-08 14:54 ` Martin Uecker
2023-08-08 16:18 ` Michael Matz
2023-08-08 19:58 ` Kees Cook
2023-08-09 16:05 ` Qing Zhao
2023-08-09 16:21 ` Michael Matz [this message]
2023-08-09 20:10 ` Qing Zhao
2023-08-10 6:58 ` Martin Uecker
2023-08-10 13:59 ` Qing Zhao
2023-08-10 14:38 ` Martin Uecker
2023-08-10 14:42 ` Jakub Jelinek
2023-08-10 14:47 ` Martin Uecker
2023-08-10 14:58 ` Siddhesh Poyarekar
2023-08-10 15:18 ` Martin Uecker
2023-08-10 16:28 ` Qing Zhao
2023-08-10 16:30 ` Siddhesh Poyarekar
2023-08-10 16:39 ` Jakub Jelinek
2023-08-10 17:06 ` Siddhesh Poyarekar
2023-08-16 21:42 ` Qing Zhao
2023-08-10 18:18 ` Qing Zhao
2023-08-10 14:02 ` Michael Matz
2023-08-10 13:54 ` Michael Matz
2023-08-09 20:34 ` Qing Zhao
2023-08-17 5:31 ` Kees Cook
2023-08-17 6:38 ` Kees Cook
2023-08-17 13:44 ` Qing Zhao
2023-08-17 16:54 ` Kees Cook
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=alpine.LSU.2.20.2308091606510.25429@wotan.suse.de \
--to=matz@suse.de \
--cc=gcc-patches@gcc.gnu.org \
--cc=isanbard@gmail.com \
--cc=jakub@redhat.com \
--cc=joseph@codesourcery.com \
--cc=keescook@chromium.org \
--cc=qing.zhao@oracle.com \
--cc=richard.guenther@gmail.com \
--cc=siddhesh@gotplt.org \
--cc=uecker@tugraz.at \
/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 read-only IMAP folder(s) and NNTP newsgroup(s).