PR c/78668 - aligned_alloc, realloc, et al. missing attribute alloc_size
authorMartin Sebor <msebor@redhat.com>
Sun, 4 Dec 2016 17:48:44 +0000 (17:48 +0000)
committerMartin Sebor <msebor@gcc.gnu.org>
Sun, 4 Dec 2016 17:48:44 +0000 (10:48 -0700)
gcc/ChangeLog:

PR c/78668
        * builtin-attrs.def (ATTR_ALLOC_SIZE, ATTR_RETURNS_NONNULL): New
        identifier tree nodes.
        (ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST): New attribute list.
        (ATTR_MALLOC_SIZE_1_NOTHROW_LIST): Same.
        (ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST): Same.
        (ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST): Same.
        (ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST): Same.
        * builtins.def (aligned_alloc, calloc, malloc, realloc):
        Add attribute alloc_size.
        (alloca): Add attribute alloc_size and returns_nonnull.

gcc/testsuite/ChangeLog:

PR c/78668
* gcc.dg/builtin-alloc-size.c: New test.

From-SVN: r243231

gcc/ChangeLog
gcc/builtin-attrs.def
gcc/builtins.def
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/builtin-alloc-size.c [new file with mode: 0644]

index e888c036fe1567006eced2b6be68e2a4fbb28b83..c33ad2f149c3dc6bde3202c88d6b0b38f14477e4 100644 (file)
@@ -1,3 +1,17 @@
+2016-12-04  Martin Sebor  <msebor@redhat.com>
+
+       PR c/78668
+        * builtin-attrs.def (ATTR_ALLOC_SIZE, ATTR_RETURNS_NONNULL): New
+        identifier tree nodes.
+        (ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST): New attribute list.
+        (ATTR_MALLOC_SIZE_1_NOTHROW_LIST): Same.
+        (ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST): Same.
+        (ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST): Same.
+        (ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST): Same.
+        * builtins.def (aligned_alloc, calloc, malloc, realloc):
+        Add attribute alloc_size.
+        (alloca): Add attribute alloc_size and returns_nonnull.
+
 2016-12-04  Uros Bizjak  <ubizjak@gmail.com>
 
        PR target/70322
index 88c9bd1f495a865435794f0bc4bb27bede6d6f86..1520d151007f6be15c2069a257556721c0edb5cf 100644 (file)
@@ -83,6 +83,7 @@ DEF_LIST_INT_INT (5,6)
 #undef DEF_LIST_INT_INT
 
 /* Construct trees for identifiers.  */
+DEF_ATTR_IDENT (ATTR_ALLOC_SIZE, "alloc_size")
 DEF_ATTR_IDENT (ATTR_COLD, "cold")
 DEF_ATTR_IDENT (ATTR_CONST, "const")
 DEF_ATTR_IDENT (ATTR_FORMAT, "format")
@@ -151,6 +152,26 @@ DEF_ATTR_TREE_LIST (ATTR_SENTINEL_NOTHROW_LEAF_LIST, ATTR_SENTINEL,        \
 DEF_ATTR_TREE_LIST (ATTR_COLD_CONST_NORETURN_NOTHROW_LEAF_LIST, ATTR_CONST,\
                        ATTR_NULL, ATTR_COLD_NORETURN_NOTHROW_LEAF_LIST)
 
+/* Allocation functions like malloc and realloc whose first argument
+   specifies the size of the allocated object.  */
+DEF_ATTR_TREE_LIST (ATTR_MALLOC_SIZE_1_NOTHROW_LIST, ATTR_ALLOC_SIZE,  \
+                       ATTR_LIST_1, ATTR_MALLOC_NOTHROW_LIST)
+DEF_ATTR_TREE_LIST (ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST, ATTR_ALLOC_SIZE, \
+                       ATTR_LIST_1, ATTR_MALLOC_NOTHROW_LEAF_LIST)
+/* Alloca is just like malloc except that it never returns null.  */
+DEF_ATTR_TREE_LIST (ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST, ATTR_RETURNS_NONNULL,
+                   ATTR_NULL, ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST)
+
+/* Allocation functions like calloc the product of whose first two arguments
+   specifies the size of the allocated object.  */
+DEF_ATTR_TREE_LIST (ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST, ATTR_ALLOC_SIZE, \
+                       ATTR_LIST_1_2, ATTR_MALLOC_NOTHROW_LEAF_LIST)
+
+/* Allocation functions like realloc whose second argument specifies
+   the size of the allocated object.  */
+DEF_ATTR_TREE_LIST (ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST, ATTR_ALLOC_SIZE, \
+                       ATTR_LIST_2, ATTR_NOTHROW_LEAF_LIST)
+
 /* Functions whose pointer parameter(s) are all nonnull.  */
 DEF_ATTR_TREE_LIST (ATTR_NONNULL_LIST, ATTR_NONNULL, ATTR_NULL, ATTR_NULL)
 /* Functions whose first parameter is a nonnull pointer.  */
index 6766975fdf922508c61e26c0ebdaed066b23f8aa..9cd24e8a89ce48dd9e299f529d7176fb10ec0257 100644 (file)
@@ -297,7 +297,7 @@ DEF_C99_BUILTIN        (BUILT_IN_ACOSH, "acosh", BT_FN_DOUBLE_DOUBLE, ATTR_MATHF
 DEF_C99_BUILTIN        (BUILT_IN_ACOSHF, "acoshf", BT_FN_FLOAT_FLOAT, ATTR_MATHFN_FPROUNDING_ERRNO)
 DEF_C99_BUILTIN        (BUILT_IN_ACOSHL, "acoshl", BT_FN_LONGDOUBLE_LONGDOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
 DEF_C99_C90RES_BUILTIN (BUILT_IN_ACOSL, "acosl", BT_FN_LONGDOUBLE_LONGDOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
-DEF_C11_BUILTIN        (BUILT_IN_ALIGNED_ALLOC, "aligned_alloc", BT_FN_PTR_SIZE_SIZE, ATTR_MALLOC_NOTHROW_LIST)
+DEF_C11_BUILTIN        (BUILT_IN_ALIGNED_ALLOC, "aligned_alloc", BT_FN_PTR_SIZE_SIZE, ATTR_MALLOC_SIZE_1_NOTHROW_LIST)
 DEF_LIB_BUILTIN        (BUILT_IN_ASIN, "asin", BT_FN_DOUBLE_DOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
 DEF_C99_C90RES_BUILTIN (BUILT_IN_ASINF, "asinf", BT_FN_FLOAT_FLOAT, ATTR_MATHFN_FPROUNDING_ERRNO)
 DEF_C99_BUILTIN        (BUILT_IN_ASINH, "asinh", BT_FN_DOUBLE_DOUBLE, ATTR_MATHFN_FPROUNDING)
@@ -777,7 +777,7 @@ DEF_GCC_BUILTIN        (BUILT_IN_UMULLL_OVERFLOW, "umulll_overflow", BT_FN_BOOL_
 DEF_LIB_BUILTIN        (BUILT_IN_ABORT, "abort", BT_FN_VOID, ATTR_TMPURE_NORETURN_NOTHROW_LEAF_LIST)
 DEF_LIB_BUILTIN        (BUILT_IN_ABS, "abs", BT_FN_INT_INT, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_AGGREGATE_INCOMING_ADDRESS, "aggregate_incoming_address", BT_FN_PTR_VAR, ATTR_LEAF_LIST)
-DEF_EXT_LIB_BUILTIN    (BUILT_IN_ALLOCA, "alloca", BT_FN_PTR_SIZE, ATTR_MALLOC_NOTHROW_LEAF_LIST)
+DEF_EXT_LIB_BUILTIN    (BUILT_IN_ALLOCA, "alloca", BT_FN_PTR_SIZE, ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_APPLY, "apply", BT_FN_PTR_PTR_FN_VOID_VAR_PTR_SIZE, ATTR_NULL)
 DEF_GCC_BUILTIN        (BUILT_IN_APPLY_ARGS, "apply_args", BT_FN_PTR_VAR, ATTR_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_BSWAP16, "bswap16", BT_FN_UINT16_UINT16, ATTR_CONST_NOTHROW_LEAF_LIST)
@@ -785,7 +785,7 @@ DEF_GCC_BUILTIN        (BUILT_IN_BSWAP32, "bswap32", BT_FN_UINT32_UINT32, ATTR_C
 DEF_GCC_BUILTIN        (BUILT_IN_BSWAP64, "bswap64", BT_FN_UINT64_UINT64, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_EXT_LIB_BUILTIN    (BUILT_IN_CLEAR_CACHE, "__clear_cache", BT_FN_VOID_PTR_PTR, ATTR_NOTHROW_LEAF_LIST)
 /* [trans-mem]: Adjust BUILT_IN_TM_CALLOC if BUILT_IN_CALLOC is changed.  */
-DEF_LIB_BUILTIN        (BUILT_IN_CALLOC, "calloc", BT_FN_PTR_SIZE_SIZE, ATTR_MALLOC_NOTHROW_LEAF_LIST)
+DEF_LIB_BUILTIN        (BUILT_IN_CALLOC, "calloc", BT_FN_PTR_SIZE_SIZE, ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_CLASSIFY_TYPE, "classify_type", BT_FN_INT_VAR, ATTR_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_CLZ, "clz", BT_FN_INT_UINT, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_CLZIMAX, "clzimax", BT_FN_INT_UINTMAX, ATTR_CONST_NOTHROW_LEAF_LIST)
@@ -861,7 +861,7 @@ DEF_LIB_BUILTIN        (BUILT_IN_LABS, "labs", BT_FN_LONG_LONG, ATTR_CONST_NOTHR
 DEF_C99_BUILTIN        (BUILT_IN_LLABS, "llabs", BT_FN_LONGLONG_LONGLONG, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_LONGJMP, "longjmp", BT_FN_VOID_PTR_INT, ATTR_NORETURN_NOTHROW_LIST)
 /* [trans-mem]: Adjust BUILT_IN_TM_MALLOC if BUILT_IN_MALLOC is changed.  */
-DEF_LIB_BUILTIN        (BUILT_IN_MALLOC, "malloc", BT_FN_PTR_SIZE, ATTR_MALLOC_NOTHROW_LEAF_LIST)
+DEF_LIB_BUILTIN        (BUILT_IN_MALLOC, "malloc", BT_FN_PTR_SIZE, ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_NEXT_ARG, "next_arg", BT_FN_PTR_VAR, ATTR_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_PARITY, "parity", BT_FN_INT_UINT, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_PARITYIMAX, "parityimax", BT_FN_INT_UINTMAX, ATTR_CONST_NOTHROW_LEAF_LIST)
@@ -873,7 +873,7 @@ DEF_GCC_BUILTIN        (BUILT_IN_POPCOUNTL, "popcountl", BT_FN_INT_ULONG, ATTR_C
 DEF_GCC_BUILTIN        (BUILT_IN_POPCOUNTLL, "popcountll", BT_FN_INT_ULONGLONG, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_EXT_LIB_BUILTIN    (BUILT_IN_POSIX_MEMALIGN, "posix_memalign", BT_FN_INT_PTRPTR_SIZE_SIZE, ATTR_NOTHROW_NONNULL_LEAF)
 DEF_GCC_BUILTIN        (BUILT_IN_PREFETCH, "prefetch", BT_FN_VOID_CONST_PTR_VAR, ATTR_NOVOPS_LEAF_LIST)
-DEF_LIB_BUILTIN        (BUILT_IN_REALLOC, "realloc", BT_FN_PTR_PTR_SIZE, ATTR_NOTHROW_LEAF_LIST)
+DEF_LIB_BUILTIN        (BUILT_IN_REALLOC, "realloc", BT_FN_PTR_PTR_SIZE, ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_RETURN, "return", BT_FN_VOID_PTR, ATTR_NORETURN_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_RETURN_ADDRESS, "return_address", BT_FN_PTR_UINT, ATTR_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_SAVEREGS, "saveregs", BT_FN_PTR_VAR, ATTR_NULL)
index a323678ff7f9a328be12134caadf32f5a1ee94c0..838fca5f1bc1a4d70e3f5810960213f7c6ef6f8a 100644 (file)
@@ -1,3 +1,8 @@
+2016-12-04  Martin Sebor  <msebor@redhat.com>
+
+       PR c/78668
+       * gcc.dg/builtin-alloc-size.c: New test.
+
 2016-12-04  Uros Bizjak  <ubizjak@gmail.com>
 
        PR target/70322
diff --git a/gcc/testsuite/gcc.dg/builtin-alloc-size.c b/gcc/testsuite/gcc.dg/builtin-alloc-size.c
new file mode 100644 (file)
index 0000000..5a40862
--- /dev/null
@@ -0,0 +1,72 @@
+/* PR c/78668 - aligned_alloc, realloc, et al. missing attribute alloc_size
+   Test to verify that memory allocation built-ins are decorated with
+   attribute alloc_size that __builtin_object_size can make use of (or
+   are treated as if they were for that purpose)..
+   { dg-do compile }
+   { dg-additional-options "-O2 -fdump-tree-optimized" } */
+
+void sink (void*);
+
+unsigned size (unsigned n)
+{
+  return n;
+}
+
+void test_aligned_alloc (unsigned a)
+{
+  unsigned n = size (7);
+
+  void *p = __builtin_aligned_alloc (n, a);
+  if (__builtin_object_size (p, 0) != n)
+    __builtin_abort ();
+  sink (p);
+}
+
+void test_alloca (void)
+{
+  unsigned n = size (13);
+
+  void *p = __builtin_alloca (n);
+
+  /* Also verify that alloca is declared with attribute returns_nonnull
+     (or treated as it were as the case may be).  */
+  if (!p)
+    __builtin_abort ();
+
+  if (__builtin_object_size (p, 0) != n)
+    __builtin_abort ();
+  sink (p);
+}
+
+void test_calloc (void)
+{
+  unsigned m = size (19);
+  unsigned n = size (23);
+
+  void *p = __builtin_calloc (m, n);
+  if (__builtin_object_size (p, 0) != m * n)
+    __builtin_abort ();
+  sink (p);
+}
+
+void test_malloc (void)
+{
+  unsigned n = size (17);
+
+  void *p = __builtin_malloc (n);
+  if (__builtin_object_size (p, 0) != n)
+    __builtin_abort ();
+  sink (p);
+}
+
+void test_realloc (void *p)
+{
+  unsigned n = size (31);
+
+  p = __builtin_realloc (p, n);
+  if (__builtin_object_size (p, 0) != n)
+    __builtin_abort ();
+  sink (p);
+}
+
+/* { dg-final { scan-tree-dump-not "abort" "optimized" } } */