From: Martin Uecker <uecker@tugraz.at>
To: Qing Zhao <qing.zhao@oracle.com>,
joseph@codesourcery.com, richard.guenther@gmail.com,
jakub@redhat.com, gcc-patches@gcc.gnu.org
Cc: keescook@chromium.org, siddhesh@gotplt.org, isanbard@gmail.com
Subject: Re: [V1][PATCH 2/3] Use the element_count atribute info in builtin object size [PR108896].
Date: Sat, 27 May 2023 12:20:41 +0200 [thread overview]
Message-ID: <f8c0ce8b77ef4b82d6d3c6ce2139fbc15025b243.camel@tugraz.at> (raw)
In-Reply-To: <20230525161450.3704901-3-qing.zhao@oracle.com>
Thank you for working on this!
Here are a couple of comments:
How is the size for an object with FAM defined?
There are at least three possible choices:
offset(..) + N * sizeof
sizeof(..) + N * sizeof
or the size of a struct with the replacement array.
Or is this not relevant here?
I would personally prefer an attribute which does
not use a string, but uses C expressions, so that
one could write something like this (although I would
limit it initially to the most simple case)
struct {
struct bar { int n; }* ptr;
int buf[] [[element_count(.ptr->n + 3)]];
};
Of course, we could still support this later even
if we use a string now.
Martin
Am Donnerstag, dem 25.05.2023 um 16:14 +0000 schrieb Qing Zhao:
> 2023-05-17 Qing Zhao <qing.zhao@oracle.com>
>
> gcc/ChangeLog:
>
> PR C/108896
> * tree-object-size.cc (addr_object_size): Use the element_count
> attribute info.
> * tree.cc (component_ref_has_element_count_p): New function.
> (component_ref_get_element_count): New function.
> * tree.h (component_ref_has_element_count_p): New prototype.
> (component_ref_get_element_count): New prototype.
>
> gcc/testsuite/ChangeLog:
>
> PR C/108896
> * gcc.dg/flex-array-element-count-2.c: New test.
> ---
> .../gcc.dg/flex-array-element-count-2.c | 56 +++++++++++
> gcc/tree-object-size.cc | 37 ++++++--
> gcc/tree.cc | 93 +++++++++++++++++++
> gcc/tree.h | 10 ++
> 4 files changed, 189 insertions(+), 7 deletions(-)
> create mode 100644 gcc/testsuite/gcc.dg/flex-array-element-count-2.c
>
> diff --git a/gcc/testsuite/gcc.dg/flex-array-element-count-2.c b/gcc/testsuite/gcc.dg/flex-array-element-count-2.c
> new file mode 100644
> index 00000000000..5a280e8c731
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/flex-array-element-count-2.c
> @@ -0,0 +1,56 @@
> +/* test the attribute element_count and its usage in
> + * __builtin_dynamic_object_size. */
> +/* { dg-do run } */
> +/* { dg-options "-O2" } */
> +
> +#include "builtin-object-size-common.h"
> +
> +#define expect(p, _v) do { \
> + size_t v = _v; \
> + if (p == v) \
> + __builtin_printf ("ok: %s == %zd\n", #p, p); \
> + else \
> + { \
> + __builtin_printf ("WAT: %s == %zd (expected %zd)\n", #p, p, v); \
> + FAIL (); \
> + } \
> +} while (0);
> +
> +struct flex {
> + int b;
> + int c[];
> +} *array_flex;
> +
> +struct annotated {
> + int b;
> + int c[] __attribute__ ((element_count ("b")));
> +} *array_annotated;
> +
> +void __attribute__((__noinline__)) setup (int normal_count, int attr_count)
> +{
> + array_flex
> + = (struct flex *)malloc (sizeof (struct flex)
> + + normal_count * sizeof (int));
> + array_flex->b = normal_count;
> +
> + array_annotated
> + = (struct annotated *)malloc (sizeof (struct annotated)
> + + attr_count * sizeof (int));
> + array_annotated->b = attr_count;
> +
> + return;
> +}
> +
> +void __attribute__((__noinline__)) test ()
> +{
> + expect(__builtin_dynamic_object_size(array_flex->c, 1), -1);
> + expect(__builtin_dynamic_object_size(array_annotated->c, 1),
> + array_annotated->b * sizeof (int));
> +}
> +
> +int main(int argc, char *argv[])
> +{
> + setup (10,10);
> + test ();
> + DONE ();
> +}
> diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc
> index 9a936a91983..f9aadd59054 100644
> --- a/gcc/tree-object-size.cc
> +++ b/gcc/tree-object-size.cc
> @@ -585,6 +585,7 @@ addr_object_size (struct object_size_info *osi, const_tree ptr,
> if (pt_var != TREE_OPERAND (ptr, 0))
> {
> tree var;
> + tree element_count_ref = NULL_TREE;
>
>
> if (object_size_type & OST_SUBOBJECT)
> {
> @@ -600,11 +601,12 @@ addr_object_size (struct object_size_info *osi, const_tree ptr,
> var = TREE_OPERAND (var, 0);
> if (var != pt_var && TREE_CODE (var) == ARRAY_REF)
> var = TREE_OPERAND (var, 0);
> - if (! TYPE_SIZE_UNIT (TREE_TYPE (var))
> + if (! component_ref_has_element_count_p (var)
> + && ((! TYPE_SIZE_UNIT (TREE_TYPE (var))
> || ! tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (var)))
> || (pt_var_size && TREE_CODE (pt_var_size) == INTEGER_CST
> && tree_int_cst_lt (pt_var_size,
> - TYPE_SIZE_UNIT (TREE_TYPE (var)))))
> + TYPE_SIZE_UNIT (TREE_TYPE (var)))))))
> var = pt_var;
> else if (var != pt_var && TREE_CODE (pt_var) == MEM_REF)
> {
> @@ -612,6 +614,7 @@ addr_object_size (struct object_size_info *osi, const_tree ptr,
> /* For &X->fld, compute object size if fld isn't a flexible array
> member. */
> bool is_flexible_array_mem_ref = false;
> +
> while (v && v != pt_var)
> switch (TREE_CODE (v))
> {
> @@ -639,6 +642,8 @@ addr_object_size (struct object_size_info *osi, const_tree ptr,
> break;
> }
> is_flexible_array_mem_ref = array_ref_flexible_size_p (v);
> + element_count_ref = component_ref_get_element_count (v);
> +
> while (v != pt_var && TREE_CODE (v) == COMPONENT_REF)
> if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
> != UNION_TYPE
> @@ -652,8 +657,11 @@ addr_object_size (struct object_size_info *osi, const_tree ptr,
> == RECORD_TYPE)
> {
> /* compute object size only if v is not a
> - flexible array member. */
> - if (!is_flexible_array_mem_ref)
> + flexible array member or the flexible array member
> + has a known element count indicated by the user
> + through attribute element_count. */
> + if (!is_flexible_array_mem_ref
> + || element_count_ref)
> {
> v = NULL_TREE;
> break;
> @@ -686,9 +694,24 @@ addr_object_size (struct object_size_info *osi, const_tree ptr,
>
>
> if (var != pt_var)
> {
> - var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
> - if (!TREE_CONSTANT (var_size))
> - var_size = get_or_create_ssa_default_def (cfun, var_size);
> + if (!element_count_ref)
> + {
> + var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
> + if (!TREE_CONSTANT (var_size))
> + var_size = get_or_create_ssa_default_def (cfun, var_size);
> + }
> + else
> + {
> + gcc_assert (TREE_CODE (var) == COMPONENT_REF
> + && TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE);
> + tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (var)));
> + var_size
> + = size_binop (MULT_EXPR,
> + fold_convert (sizetype, element_count_ref),
> + fold_convert (sizetype, element_size));
> + if (!todo)
> + todo = TODO_update_ssa_only_virtuals;
> + }
> if (!var_size)
> return false;
> }
> diff --git a/gcc/tree.cc b/gcc/tree.cc
> index 7e6de288886..48753b94f2c 100644
> --- a/gcc/tree.cc
> +++ b/gcc/tree.cc
> @@ -12745,6 +12745,99 @@ array_ref_element_size (tree exp)
> return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
> }
>
>
> +/* For a component_ref that has an array type ARRAY_REF, return TRUE when
> + an element_count attribute attached to the corresponding FIELD_DECL.
> + return FALSE otherwise. */
> +bool
> +component_ref_has_element_count_p (tree array_ref)
> +{
> + if (TREE_CODE (array_ref) != COMPONENT_REF)
> + return false;
> +
> + if (TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE)
> + return false;
> +
> + tree struct_object = TREE_OPERAND (array_ref, 0);
> + tree struct_type = TREE_TYPE (struct_object);
> +
> + if (!RECORD_OR_UNION_TYPE_P (struct_type))
> + return false;
> + tree field_decl = TREE_OPERAND (array_ref, 1);
> + tree attr_element_count = lookup_attribute ("element_count",
> + DECL_ATTRIBUTES (field_decl));
> +
> + if (!attr_element_count)
> + return false;
> + return true;
> +}
> +
> +
> +/* For a component_ref that has an array type ARRAY_REF, get the object that
> + represents its element_count per the attribute element_count attached to
> + the corresponding FIELD_DECL. return NULL_TREE when cannot find such
> + object.
> + For example, if:
> +
> + struct P {
> + int k;
> + int x[] __attribute__ ((element_count ("k")));
> + } *p;
> +
> + for the following reference:
> +
> + p->x[b]
> +
> + the object that represents its element count will be:
> +
> + p->k
> +
> + So, when component_ref_get_element_count (p->x[b]) is called, p->k should be
> + returned.
> +*/
> +
> +tree
> +component_ref_get_element_count (tree array_ref)
> +{
> + if (! component_ref_has_element_count_p (array_ref))
> + return NULL_TREE;
> +
> + tree struct_object = TREE_OPERAND (array_ref, 0);
> + tree struct_type = TREE_TYPE (struct_object);
> + tree field_decl = TREE_OPERAND (array_ref, 1);
> + tree attr_element_count = lookup_attribute ("element_count",
> + DECL_ATTRIBUTES (field_decl));
> + gcc_assert (attr_element_count);
> +
> + /* If there is an element_count attribute attached to the field,
> + get the field that maps to the element_count. */
> +
> + const char *fieldname
> + = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr_element_count)));
> +
> + tree element_count_field = NULL_TREE;
> + for (tree field = TYPE_FIELDS (struct_type); field;
> + field = DECL_CHAIN (field))
> + if (TREE_CODE (field) == FIELD_DECL
> + && DECL_NAME (field) != NULL
> + && strcmp (IDENTIFIER_POINTER (DECL_NAME (field)), fieldname) == 0)
> + {
> + element_count_field = field;
> + break;
> + }
> +
> + gcc_assert (element_count_field);
> +
> + /* generate the tree node that represent the element_count of this array
> + ref. This is a COMPONENT_REF to the element_count_field of the
> + containing structure. */
> +
> + tree element_count_ref = build3 (COMPONENT_REF,
> + TREE_TYPE (element_count_field),
> + struct_object, element_count_field,
> + NULL_TREE);
> + return element_count_ref;
> +}
> +
> /* Return a tree representing the lower bound of the array mentioned in
> EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
>
>
> diff --git a/gcc/tree.h b/gcc/tree.h
> index 0b72663e6a1..8f36370a02d 100644
> --- a/gcc/tree.h
> +++ b/gcc/tree.h
> @@ -5582,6 +5582,16 @@ extern tree get_base_address (tree t);
> of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
> extern tree array_ref_element_size (tree);
>
>
> +/* Give a component_ref that has an array type, return true when an
> + attribute element_count attached to the corresponding FIELD_DECL. */
> +extern bool component_ref_has_element_count_p (tree);
> +
> +/* Give a component_ref that has an array type, return the object that
> + represents its element_count per the attribute element_count attached to
> + the corresponding FIELD_DECL. return NULL_TREE when cannot find such
> + object. */
> +extern tree component_ref_get_element_count (tree);
> +
> /* Return a typenode for the "standard" C type with a given name. */
> extern tree get_typenode_from_name (const char *);
>
>
next prev parent reply other threads:[~2023-05-27 10:20 UTC|newest]
Thread overview: 52+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-25 16:14 [V1][PATCH 0/3] New attribute "element_count" to annotate bounds for C99 FAM(PR108896) Qing Zhao
2023-05-25 16:14 ` [V1][PATCH 1/3] Provide element_count attribute to flexible array member field (PR108896) Qing Zhao
2023-05-25 21:02 ` Joseph Myers
2023-05-26 13:32 ` Qing Zhao
2023-05-26 18:15 ` Joseph Myers
2023-05-26 19:09 ` Qing Zhao
2023-06-07 19:59 ` Qing Zhao
2023-06-07 20:53 ` Joseph Myers
2023-06-07 21:32 ` Qing Zhao
2023-06-07 22:05 ` Joseph Myers
2023-06-08 13:06 ` Qing Zhao
2023-06-15 15:09 ` Qing Zhao
2023-06-15 16:55 ` Joseph Myers
2023-06-15 19:54 ` Qing Zhao
2023-06-15 22:48 ` Joseph Myers
2023-06-16 15:01 ` Qing Zhao
2023-06-16 7:21 ` Martin Uecker
2023-06-16 15:14 ` Qing Zhao
2023-06-16 16:21 ` Joseph Myers
2023-06-16 17:07 ` Martin Uecker
2023-06-16 20:20 ` Qing Zhao
2023-06-16 21:35 ` Joseph Myers
2023-06-20 19:40 ` Qing Zhao
2023-06-27 15:44 ` Qing Zhao
2023-05-25 16:14 ` [V1][PATCH 2/3] Use the element_count atribute info in builtin object size [PR108896] Qing Zhao
2023-05-27 10:20 ` Martin Uecker [this message]
2023-05-30 16:08 ` Qing Zhao
2023-05-25 16:14 ` [V1][PATCH 3/3] Use the element_count attribute information in bound sanitizer[PR108896] Qing Zhao
2023-05-26 16:12 ` [V1][PATCH 0/3] New attribute "element_count" to annotate bounds for C99 FAM(PR108896) Kees Cook
2023-05-30 21:44 ` Qing Zhao
2023-05-26 20:40 ` Kees Cook
2023-05-30 15:43 ` Qing Zhao
2023-07-06 18:56 ` Qing Zhao
2023-07-06 21:10 ` Martin Uecker
2023-07-07 15:47 ` Qing Zhao
2023-07-07 20:21 ` Qing Zhao
2023-07-13 20:31 ` Kees Cook
2023-07-17 21:17 ` Qing Zhao
2023-07-17 23:40 ` Kees Cook
2023-07-18 15:37 ` Qing Zhao
2023-07-18 16:03 ` Martin Uecker
2023-07-18 16:25 ` Qing Zhao
2023-07-18 16:50 ` Martin Uecker
2023-07-18 18:53 ` Qing Zhao
2023-07-19 8:41 ` Martin Uecker
2023-07-19 16:16 ` Qing Zhao
2023-07-19 18:52 ` Qing Zhao
2023-07-31 20:14 ` Qing Zhao
2023-08-01 22:45 ` Kees Cook
2023-08-02 6:25 ` Martin Uecker
2023-08-02 15:02 ` Qing Zhao
2023-08-02 15:09 ` Qing Zhao
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=f8c0ce8b77ef4b82d6d3c6ce2139fbc15025b243.camel@tugraz.at \
--to=uecker@tugraz.at \
--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 \
/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).