[2/2] c/100547 - reject overly large vector_size attributes

Message ID rn439r3q-p38o-s07r-n3o9-r488s698p479@fhfr.qr
State New
Headers show
Series
  • [1/2] middle-end/100547 - check rtvec_alloc size
Related show

Commit Message

Richard Biener May 12, 2021, 8:47 a.m.
This rejects a number of vector components that does not fit an 'int'
which is an internal limitation of RTVEC.

This regresses gcc.dg/attr-vector_size.c which checks for much larger
supported vectors.  Not sure what to do about this - I'll also note
the RTVEC limitation is a host specific limitation (unless we change
this 'int' to int32_t).

Bootstrapped and tested on x86_64-unknown-linux-gnu with the above
mentioned regression.

Any comments?

Thanks,
Richard.

2021-05-12  Richard Biener  <rguenther@suse.de>

	PR c/100547
gcc/c-family/
	* c-attribs.c (type_valid_for_vector_size): Reject too large nunits.

	* gcc.dg/pr100547.c: New testcase.
---
 gcc/c-family/c-attribs.c        | 10 ++++++++++
 gcc/testsuite/gcc.dg/pr100547.c | 35 +++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/pr100547.c

-- 
2.26.2

Comments

H.J. Lu via Gcc-patches May 12, 2021, 3:11 p.m. | #1
On 5/12/21 2:47 AM, Richard Biener wrote:
> This rejects a number of vector components that does not fit an 'int'

> which is an internal limitation of RTVEC.

> 

> This regresses gcc.dg/attr-vector_size.c which checks for much larger

> supported vectors.  Not sure what to do about this - I'll also note

> the RTVEC limitation is a host specific limitation (unless we change

> this 'int' to int32_t).

> 

> Bootstrapped and tested on x86_64-unknown-linux-gnu with the above

> mentioned regression.

> 

> Any comments?


Just a few minor ones.

> 

> Thanks,

> Richard.

> 

> 2021-05-12  Richard Biener  <rguenther@suse.de>

> 

> 	PR c/100547

> gcc/c-family/

> 	* c-attribs.c (type_valid_for_vector_size): Reject too large nunits.

> 

> 	* gcc.dg/pr100547.c: New testcase.

> ---

>   gcc/c-family/c-attribs.c        | 10 ++++++++++

>   gcc/testsuite/gcc.dg/pr100547.c | 35 +++++++++++++++++++++++++++++++++

>   2 files changed, 45 insertions(+)

>   create mode 100644 gcc/testsuite/gcc.dg/pr100547.c

> 

> diff --git a/gcc/c-family/c-attribs.c b/gcc/c-family/c-attribs.c

> index c1f652d1dc9..60bcfa22df1 100644

> --- a/gcc/c-family/c-attribs.c

> +++ b/gcc/c-family/c-attribs.c

> @@ -4252,6 +4252,16 @@ type_valid_for_vector_size (tree type, tree atname, tree args,

>         return NULL_TREE;

>       }

>   

> +  if (nunits >= (unsigned HOST_WIDE_INT)INT_MAX)


The inequality includes INT_MAX, so the phrasing below doesn't exactly
match the condition.  Since nunits must be a power of 2 the condition
might as well be greater than.


> +    {

> +      if (error_p)

> +	error ("number of components of the vector exceeds %d", INT_MAX);


"number of vector components" sounds better (though there are other
messages that use this phrasing already).

Especially since it's computed, I would suggest to mention the value
of nunits in the message (it should also be mentioned in the "power
of two" message).

Martin


> +      else

> +	warning (OPT_Wattributes,

> +		 "number of components of the vector exceeds %d", INT_MAX);

> +      return NULL_TREE;

> +    }

> +

>     if (ptrnunits)

>       *ptrnunits = nunits;

>   

> diff --git a/gcc/testsuite/gcc.dg/pr100547.c b/gcc/testsuite/gcc.dg/pr100547.c

> new file mode 100644

> index 00000000000..92f34bf974f

> --- /dev/null

> +++ b/gcc/testsuite/gcc.dg/pr100547.c

> @@ -0,0 +1,35 @@

> +/* { dg-do compile } */

> +/* { dg-options "-O -g" } */

> +

> +typedef int __attribute__((vector_size(

> +    ((((((((((((((((((((((((((((((8 * sizeof(short)) * sizeof(short)) *

> +                                sizeof(short)) *

> +                               sizeof(short)) *

> +                              sizeof(short)) *

> +                             sizeof(short)) *

> +                            sizeof(short)) *

> +                           sizeof(short)) *

> +                          sizeof(short)) *

> +                         sizeof(short)) *

> +                        sizeof(short)) *

> +                       sizeof(short)) *

> +                      sizeof(short)) *

> +                     sizeof(short)) *

> +                    sizeof(short)) *

> +                   sizeof(short)) *

> +                  sizeof(short)) *

> +                 sizeof(short)) *

> +                sizeof(short)) *

> +               sizeof(short)) *

> +              sizeof(short)) *

> +             sizeof(short)) *

> +            sizeof(short)) *

> +           sizeof(short)) *

> +          sizeof(short)) *

> +         sizeof(short)) *

> +        sizeof(short)) *

> +       sizeof(short)) *

> +      sizeof(short)) *

> +     sizeof(short)) *

> +    sizeof(short)))) V; /* { dg-error "number of components" } */

> +void k() { V w = { 0 }; }

>

Patch

diff --git a/gcc/c-family/c-attribs.c b/gcc/c-family/c-attribs.c
index c1f652d1dc9..60bcfa22df1 100644
--- a/gcc/c-family/c-attribs.c
+++ b/gcc/c-family/c-attribs.c
@@ -4252,6 +4252,16 @@  type_valid_for_vector_size (tree type, tree atname, tree args,
       return NULL_TREE;
     }
 
+  if (nunits >= (unsigned HOST_WIDE_INT)INT_MAX)
+    {
+      if (error_p)
+	error ("number of components of the vector exceeds %d", INT_MAX);
+      else
+	warning (OPT_Wattributes,
+		 "number of components of the vector exceeds %d", INT_MAX);
+      return NULL_TREE;
+    }
+
   if (ptrnunits)
     *ptrnunits = nunits;
 
diff --git a/gcc/testsuite/gcc.dg/pr100547.c b/gcc/testsuite/gcc.dg/pr100547.c
new file mode 100644
index 00000000000..92f34bf974f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr100547.c
@@ -0,0 +1,35 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O -g" } */
+
+typedef int __attribute__((vector_size(
+    ((((((((((((((((((((((((((((((8 * sizeof(short)) * sizeof(short)) *
+                                sizeof(short)) *
+                               sizeof(short)) *
+                              sizeof(short)) *
+                             sizeof(short)) *
+                            sizeof(short)) *
+                           sizeof(short)) *
+                          sizeof(short)) *
+                         sizeof(short)) *
+                        sizeof(short)) *
+                       sizeof(short)) *
+                      sizeof(short)) *
+                     sizeof(short)) *
+                    sizeof(short)) *
+                   sizeof(short)) *
+                  sizeof(short)) *
+                 sizeof(short)) *
+                sizeof(short)) *
+               sizeof(short)) *
+              sizeof(short)) *
+             sizeof(short)) *
+            sizeof(short)) *
+           sizeof(short)) *
+          sizeof(short)) *
+         sizeof(short)) *
+        sizeof(short)) *
+       sizeof(short)) *
+      sizeof(short)) *
+     sizeof(short)) *
+    sizeof(short)))) V; /* { dg-error "number of components" } */
+void k() { V w = { 0 }; }