public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-7794] tree-optimization/104970: Limit size computation for access attribute
@ 2022-03-24  9:40 Siddhesh Poyarekar
  0 siblings, 0 replies; only message in thread
From: Siddhesh Poyarekar @ 2022-03-24  9:40 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:c1d233e3334df07cfb4f732ace4d93d3cbc28bca

commit r12-7794-gc1d233e3334df07cfb4f732ace4d93d3cbc28bca
Author: Siddhesh Poyarekar <siddhesh@gotplt.org>
Date:   Wed Mar 23 23:09:02 2022 +0530

    tree-optimization/104970: Limit size computation for access attribute
    
    Limit object size computation only to the simple case where access
    attribute has been explicitly specified.  The object passed to
    __builtin_dynamic_object_size could either be a pointer or a VLA whose
    size has been described using access attribute.
    
    Further, return a valid size only if the object is a void * pointer or
    points to (or is a VLA of) a type that has a constant size.
    
    gcc/ChangeLog:
    
            PR tree-optimization/104970
            * tree-object-size.cc (parm_object_size): Restrict size
            computation scenarios to explicit access attributes.
    
    gcc/testsuite/ChangeLog:
    
            PR tree-optimization/104970
            * gcc.dg/builtin-dynamic-object-size-0.c (test_parmsz_simple2,
            test_parmsz_simple3, test_parmsz_extern, test_parmsz_internal,
            test_parmsz_internal2, test_parmsz_internal3): New tests.
            (main): Use them.
    
    Signed-off-by: Siddhesh Poyarekar <siddhesh@gotplt.org>

Diff:
---
 .../gcc.dg/builtin-dynamic-object-size-0.c         | 71 ++++++++++++++++++++++
 gcc/tree-object-size.cc                            | 11 +++-
 2 files changed, 79 insertions(+), 3 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c
index e5dc23a908d..b5b0b3a677c 100644
--- a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c
+++ b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c
@@ -380,6 +380,22 @@ test_parmsz_simple (void *obj, size_t sz)
   return __builtin_dynamic_object_size (obj, 0);
 }
 
+size_t
+__attribute__ ((access (__read_write__, 2, 1)))
+__attribute__ ((noinline))
+test_parmsz_simple2 (size_t sz, char obj[])
+{
+  return __builtin_dynamic_object_size (obj, 0);
+}
+
+/* Implicitly constructed access attributes not supported yet.  */
+size_t
+__attribute__ ((noinline))
+test_parmsz_simple3 (size_t sz, char obj[sz])
+{
+  return __builtin_dynamic_object_size (obj, 0);
+}
+
 size_t
 __attribute__ ((noinline))
 __attribute__ ((access (__read_write__, 1, 2)))
@@ -412,6 +428,38 @@ test_parmsz_unknown (void *obj, void *unknown, size_t sz, int cond)
   return __builtin_dynamic_object_size (cond ? obj : unknown, 0);
 }
 
+struct S;
+size_t
+__attribute__ ((access (__read_write__, 1, 2)))
+__attribute__ ((noinline))
+test_parmsz_extern (struct S *obj, size_t sz)
+{
+  return __builtin_dynamic_object_size (obj, 0);
+}
+
+/* Implicitly constructed access attributes not supported yet.  */
+size_t
+__attribute__ ((noinline))
+test_parmsz_internal (size_t sz, double obj[][sz])
+{
+  return __builtin_dynamic_object_size (obj, 0);
+}
+
+size_t
+__attribute__ ((access (__read_write__, 2, 1)))
+__attribute__ ((noinline))
+test_parmsz_internal2 (size_t sz, double obj[][sz])
+{
+  return __builtin_dynamic_object_size (obj, 0);
+}
+
+size_t
+__attribute__ ((noinline))
+test_parmsz_internal3 (size_t sz1, size_t sz2, double obj[sz1][sz2])
+{
+  return __builtin_dynamic_object_size (obj, 0);
+}
+
 /* Loops.  */
 
 size_t
@@ -532,9 +580,22 @@ main (int argc, char **argv)
   if (test_parmsz_simple (argv[0], __builtin_strlen (argv[0]) + 1)
       != __builtin_strlen (argv[0]) + 1)
     FAIL ();
+  if (test_parmsz_simple2 (__builtin_strlen (argv[0]) + 1, argv[0])
+      != __builtin_strlen (argv[0]) + 1)
+    FAIL ();
+  /* Only explicitly added access attributes are supported for now.  */
+  if (test_parmsz_simple3 (__builtin_strlen (argv[0]) + 1, argv[0]) != -1)
+    FAIL ();
   int arr[42];
   if (test_parmsz_scaled (arr, 42) != sizeof (arr))
     FAIL ();
+  if (test_parmsz_scaled (arr, 40) != 40 * sizeof (int))
+    FAIL ();
+  /* __bdos cannot see the actual size of ARR, so it will return what it was
+     passed.  Fortunately though the overflow warnings see this caller side and
+     warns of the problematic size.  */
+  if (test_parmsz_scaled (arr, 44) != 44 * sizeof (int)) /* { dg-warning "-Wstringop-overflow=" } */
+    FAIL ();
   if (test_parmsz_unknown (argv[0], argv[0], __builtin_strlen (argv[0]) + 1, 0)
       != -1)
   if (test_parmsz (argv[0], __builtin_strlen (argv[0]) + 1, -1) != 0)
@@ -550,6 +611,16 @@ main (int argc, char **argv)
     FAIL ();
   if (test_parmsz_scaled_off (arr, 42, 2) != 40 * sizeof (int))
     FAIL ();
+  struct S *s;
+  if (test_parmsz_extern (s, 42) != -1)
+    FAIL ();
+  double obj[4][4];
+  if (test_parmsz_internal (4, obj) != -1)
+    FAIL ();
+  if (test_parmsz_internal2 (4, obj) != -1)
+    FAIL ();
+  if (test_parmsz_internal3 (4, 4, obj) != -1)
+    FAIL ();
   if (test_loop (arr, 42, 0, 32, 1) != 10 * sizeof (int))
     FAIL ();
   if (test_loop (arr, 42, 32, -1, -1) != 0)
diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc
index b0b50774936..fc062b94d76 100644
--- a/gcc/tree-object-size.cc
+++ b/gcc/tree-object-size.cc
@@ -1477,14 +1477,19 @@ parm_object_size (struct object_size_info *osi, tree var)
   tree typesize = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (parm)));
   tree sz = NULL_TREE;
 
-  if (access && access->sizarg != UINT_MAX)
+  /* If we have an explicit access attribute with a usable size argument... */
+  if (access && access->sizarg != UINT_MAX && !access->internal_p
+      /* ... and either PARM is void * or has a type that is complete and has a
+	 constant size... */
+      && ((typesize && poly_int_tree_p (typesize))
+	  || (!typesize && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (parm))))))
     {
       tree fnargs = DECL_ARGUMENTS (fndecl);
       tree arg = NULL_TREE;
       unsigned argpos = 0;
 
-      /* Walk through the parameters to pick the size parameter and safely
-	 scale it by the type size.  */
+      /* ... then walk through the parameters to pick the size parameter and
+	 safely scale it by the type size if needed.  */
       for (arg = fnargs; arg; arg = TREE_CHAIN (arg), ++argpos)
 	if (argpos == access->sizarg && INTEGRAL_TYPE_P (TREE_TYPE (arg)))
 	  {


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-03-24  9:40 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-24  9:40 [gcc r12-7794] tree-optimization/104970: Limit size computation for access attribute Siddhesh Poyarekar

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).