public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Add warn_unused_result for malloc-like functions (PR tree-optimization/78902).
@ 2019-06-06  8:02 Martin Liška
  2019-06-06 15:17 ` Martin Sebor
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Martin Liška @ 2019-06-06  8:02 UTC (permalink / raw)
  To: gcc-patches; +Cc: Martin Sebor

[-- Attachment #1: Type: text/plain, Size: 3134 bytes --]

Hi.

The patch is about addition of warn_unused_attribute for malloc-like function.

Patch can bootstrap on x86_64-linux-gnu and survives regression tests.

Ready to be installed?
Thanks,
Martin

gcc/ChangeLog:

2019-06-06  Martin Liska  <mliska@suse.cz>

	PR tree-optimization/78902
	* builtin-attrs.def (ATTR_WARN_UNUSED_RESULT): New.
	(ATTR_MALLOC_NOTHROW_LEAF_LIST): Remove.
	(ATTR_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
	(ATTR_ALLOC_SIZE_2_NOTHROW_LIST): Remove.
	(ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST): Remove.
	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LIST): New.
	(ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LIST): New.
	(ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
	(ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST): Remove.
	(ATTR_ALLOCA_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
	(ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST):  Remove.
	(ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_2_NOTHROW_LEAF_LIST):
	New.
	(ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST): Remove.
	(ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LEAF_LIST): New.
	(ATTR_MALLOC_NOTHROW_NONNULL): Remove.
	(ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
	(ATTR_MALLOC_NOTHROW_NONNULL_LEAF): Remove.
	(ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
	* builtins.def (BUILT_IN_ALIGNED_ALLOC): Change to use
	warn_unused_result attribute.
	(BUILT_IN_STRDUP): Likewise.
	(BUILT_IN_STRNDUP): Likewise.
	(BUILT_IN_ALLOCA): Likewise.
	(BUILT_IN_CALLOC): Likewise.
	(BUILT_IN_MALLOC): Likewise.
	(BUILT_IN_REALLOC): Likewise.

gcc/testsuite/ChangeLog:

2019-06-06  Martin Liska  <mliska@suse.cz>

	PR tree-optimization/78902
	* c-c++-common/asan/alloca_loop_unpoisoning.c: Use result
	of __builtin_alloca.
	* c-c++-common/asan/pr88619.c: Likewise.
	* g++.dg/overload/using2.C: Likewise for malloc.
	* gcc.dg/attr-alloc_size-5.c: Add new dg-warning.
	* gcc.dg/nonnull-3.c: Use result of __builtin_strdup.
	* gcc.dg/pr43643.c: Likewise.
	* gcc.dg/pr59717.c: Likewise for calloc.
	* gcc.dg/torture/pr71816.c: Likewise.
	* gcc.dg/tree-ssa/pr78886.c: Likewise.
	* gcc.dg/tree-ssa/pr79697.c: Likewise.
	* gcc.dg/pr78902.c: New test.
---
 gcc/builtin-attrs.def                         | 37 ++++++++++++-------
 gcc/builtins.def                              | 14 +++----
 .../asan/alloca_loop_unpoisoning.c            |  2 +-
 gcc/testsuite/c-c++-common/asan/pr88619.c     |  2 +-
 gcc/testsuite/g++.dg/overload/using2.C        |  2 +-
 gcc/testsuite/gcc.dg/attr-alloc_size-5.c      |  2 +-
 gcc/testsuite/gcc.dg/nonnull-3.c              |  4 +-
 gcc/testsuite/gcc.dg/pr43643.c                |  6 +--
 gcc/testsuite/gcc.dg/pr59717.c                |  8 ++--
 gcc/testsuite/gcc.dg/pr78902.c                | 14 +++++++
 gcc/testsuite/gcc.dg/torture/pr71816.c        |  2 +-
 gcc/testsuite/gcc.dg/tree-ssa/pr78886.c       |  2 +-
 gcc/testsuite/gcc.dg/tree-ssa/pr79697.c       |  6 +--
 13 files changed, 62 insertions(+), 39 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr78902.c



[-- Attachment #2: 0001-Add-warn_unused_result-for-malloc-like-functions-PR-.patch --]
[-- Type: text/x-patch, Size: 18988 bytes --]

diff --git a/gcc/builtin-attrs.def b/gcc/builtin-attrs.def
index 204141f6c53..39d1395f42a 100644
--- a/gcc/builtin-attrs.def
+++ b/gcc/builtin-attrs.def
@@ -118,6 +118,7 @@ DEF_ATTR_IDENT (ATTR_TM_REGPARM, "*tm regparm")
 DEF_ATTR_IDENT (ATTR_TM_TMPURE, "transaction_pure")
 DEF_ATTR_IDENT (ATTR_RETURNS_TWICE, "returns_twice")
 DEF_ATTR_IDENT (ATTR_RETURNS_NONNULL, "returns_nonnull")
+DEF_ATTR_IDENT (ATTR_WARN_UNUSED_RESULT, "warn_unused_result")
 
 DEF_ATTR_TREE_LIST (ATTR_NOVOPS_LIST, ATTR_NOVOPS, ATTR_NULL, ATTR_NULL)
 
@@ -157,8 +158,10 @@ DEF_ATTR_TREE_LIST (ATTR_CONST_NORETURN_NOTHROW_LEAF_COLD_LIST, ATTR_COLD,\
 			ATTR_NULL, ATTR_CONST_NORETURN_NOTHROW_LEAF_LIST)
 DEF_ATTR_TREE_LIST (ATTR_MALLOC_NOTHROW_LIST, ATTR_MALLOC,	\
 			ATTR_NULL, ATTR_NOTHROW_LIST)
-DEF_ATTR_TREE_LIST (ATTR_MALLOC_NOTHROW_LEAF_LIST, ATTR_MALLOC,	\
+DEF_ATTR_TREE_LIST (ATTR_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST, ATTR_WARN_UNUSED_RESULT,	\
 			ATTR_NULL, ATTR_NOTHROW_LEAF_LIST)
+DEF_ATTR_TREE_LIST (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST, ATTR_MALLOC,	\
+			ATTR_NULL, ATTR_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST)
 DEF_ATTR_TREE_LIST (ATTR_SENTINEL_NOTHROW_LIST, ATTR_SENTINEL,	\
 			ATTR_NULL, ATTR_NOTHROW_LIST)
 DEF_ATTR_TREE_LIST (ATTR_SENTINEL_NOTHROW_LEAF_LIST, ATTR_SENTINEL,	\
@@ -170,24 +173,26 @@ DEF_ATTR_TREE_LIST (ATTR_COLD_CONST_NORETURN_NOTHROW_LEAF_LIST, ATTR_CONST,\
    with _SIZE_1, or second argument with _SIZE_2, 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_ALLOC_SIZE_2_NOTHROW_LIST, ATTR_ALLOC_SIZE,	\
-			ATTR_LIST_2, 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)
+		    ATTR_LIST_1, ATTR_MALLOC_NOTHROW_LIST)
+DEF_ATTR_TREE_LIST (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LIST, ATTR_WARN_UNUSED_RESULT,	\
+		    ATTR_NULL, ATTR_MALLOC_NOTHROW_LIST)
+DEF_ATTR_TREE_LIST (ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LIST, ATTR_ALLOC_SIZE,	\
+		    ATTR_LIST_2, ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LIST)
+DEF_ATTR_TREE_LIST (ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST, ATTR_ALLOC_SIZE, \
+		    ATTR_LIST_1, ATTR_MALLOC_WARN_UNUSED_RESULT_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)
+DEF_ATTR_TREE_LIST (ATTR_ALLOCA_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST, ATTR_RETURNS_NONNULL,
+		    ATTR_NULL, ATTR_MALLOC_WARN_UNUSED_RESULT_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)
+DEF_ATTR_TREE_LIST (ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_2_NOTHROW_LEAF_LIST, ATTR_ALLOC_SIZE, \
+		    ATTR_LIST_1_2, ATTR_MALLOC_WARN_UNUSED_RESULT_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)
+DEF_ATTR_TREE_LIST (ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LEAF_LIST, ATTR_ALLOC_SIZE, \
+		    ATTR_LIST_2, ATTR_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST)
 
 /* Functions whose pointer parameter(s) are all nonnull.  */
 DEF_ATTR_TREE_LIST (ATTR_NONNULL_LIST, ATTR_NONNULL, ATTR_NULL, ATTR_NULL)
@@ -290,11 +295,15 @@ DEF_ATTR_TREE_LIST (ATTR_PURE_NOTHROW_NONNULL, ATTR_PURE, ATTR_NULL, \
 DEF_ATTR_TREE_LIST (ATTR_PURE_NOTHROW_NONNULL_LEAF, ATTR_PURE, ATTR_NULL, \
 			ATTR_NOTHROW_NONNULL_LEAF)
 /* Nothrow malloc functions whose pointer parameter(s) are all nonnull.  */
-DEF_ATTR_TREE_LIST (ATTR_MALLOC_NOTHROW_NONNULL, ATTR_MALLOC, ATTR_NULL, \
+DEF_ATTR_TREE_LIST (ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL, ATTR_WARN_UNUSED_RESULT, ATTR_NULL, \
 			ATTR_NOTHROW_NONNULL)
+DEF_ATTR_TREE_LIST (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL, ATTR_MALLOC, ATTR_NULL, \
+			ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL)
 /* Nothrow malloc leaf functions whose pointer parameter(s) are all nonnull.  */
-DEF_ATTR_TREE_LIST (ATTR_MALLOC_NOTHROW_NONNULL_LEAF, ATTR_MALLOC, ATTR_NULL, \
+DEF_ATTR_TREE_LIST (ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF, ATTR_WARN_UNUSED_RESULT, ATTR_NULL, \
 			ATTR_NOTHROW_NONNULL_LEAF)
+DEF_ATTR_TREE_LIST (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF, ATTR_MALLOC, ATTR_NULL, \
+			ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF)
 
 /* Construct a tree for the format attribute (and implicitly nonnull).  */
 #define DEF_FORMAT_ATTRIBUTE(TYPE, FA, VALUES)				 \
diff --git a/gcc/builtins.def b/gcc/builtins.def
index c92292aeab9..6d41bdb4f44 100644
--- a/gcc/builtins.def
+++ b/gcc/builtins.def
@@ -288,7 +288,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_ALLOC_SIZE_2_NOTHROW_LIST)
+DEF_C11_BUILTIN        (BUILT_IN_ALIGNED_ALLOC, "aligned_alloc", BT_FN_PTR_SIZE_SIZE, ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_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)
@@ -702,8 +702,8 @@ DEF_LIB_BUILTIN        (BUILT_IN_STRCHR, "strchr", BT_FN_STRING_CONST_STRING_INT
 DEF_LIB_BUILTIN        (BUILT_IN_STRCMP, "strcmp", BT_FN_INT_CONST_STRING_CONST_STRING, ATTR_PURE_NOTHROW_NONNULL_LEAF)
 DEF_LIB_BUILTIN        (BUILT_IN_STRCPY, "strcpy", BT_FN_STRING_STRING_CONST_STRING, ATTR_RET1_NOTHROW_NONNULL_LEAF)
 DEF_LIB_BUILTIN        (BUILT_IN_STRCSPN, "strcspn", BT_FN_SIZE_CONST_STRING_CONST_STRING, ATTR_PURE_NOTHROW_NONNULL_LEAF)
-DEF_EXT_LIB_BUILTIN    (BUILT_IN_STRDUP, "strdup", BT_FN_STRING_CONST_STRING, ATTR_MALLOC_NOTHROW_NONNULL_LEAF)
-DEF_EXT_LIB_BUILTIN    (BUILT_IN_STRNDUP, "strndup", BT_FN_STRING_CONST_STRING_SIZE, ATTR_MALLOC_NOTHROW_NONNULL_LEAF)
+DEF_EXT_LIB_BUILTIN    (BUILT_IN_STRDUP, "strdup", BT_FN_STRING_CONST_STRING, ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF)
+DEF_EXT_LIB_BUILTIN    (BUILT_IN_STRNDUP, "strndup", BT_FN_STRING_CONST_STRING_SIZE, ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF)
 DEF_LIB_BUILTIN        (BUILT_IN_STRLEN, "strlen", BT_FN_SIZE_CONST_STRING, ATTR_PURE_NOTHROW_NONNULL_LEAF)
 DEF_EXT_LIB_BUILTIN    (BUILT_IN_STRNCASECMP, "strncasecmp", BT_FN_INT_CONST_STRING_CONST_STRING_SIZE, ATTR_PURE_NOTHROW_NONNULL_LEAF)
 DEF_LIB_BUILTIN        (BUILT_IN_STRNCAT, "strncat", BT_FN_STRING_STRING_CONST_STRING_SIZE, ATTR_RET1_NOTHROW_NONNULL_LEAF)
@@ -811,7 +811,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_COLD_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_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST)
+DEF_EXT_LIB_BUILTIN    (BUILT_IN_ALLOCA, "alloca", BT_FN_PTR_SIZE, ATTR_ALLOCA_WARN_UNUSED_RESULT_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)
@@ -819,7 +819,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_SIZE_1_2_NOTHROW_LEAF_LIST)
+DEF_LIB_BUILTIN        (BUILT_IN_CALLOC, "calloc", BT_FN_PTR_SIZE_SIZE, ATTR_MALLOC_WARN_UNUSED_RESULT_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)
@@ -896,7 +896,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_SIZE_1_NOTHROW_LEAF_LIST)
+DEF_LIB_BUILTIN        (BUILT_IN_MALLOC, "malloc", BT_FN_PTR_SIZE, ATTR_MALLOC_WARN_UNUSED_RESULT_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)
@@ -908,7 +908,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_ALLOC_SIZE_2_NOTHROW_LEAF_LIST)
+DEF_LIB_BUILTIN        (BUILT_IN_REALLOC, "realloc", BT_FN_PTR_PTR_SIZE, ATTR_ALLOC_WARN_UNUSED_RESULT_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)
diff --git a/gcc/testsuite/c-c++-common/asan/alloca_loop_unpoisoning.c b/gcc/testsuite/c-c++-common/asan/alloca_loop_unpoisoning.c
index 55345fdb3a5..3050d3ba94b 100644
--- a/gcc/testsuite/c-c++-common/asan/alloca_loop_unpoisoning.c
+++ b/gcc/testsuite/c-c++-common/asan/alloca_loop_unpoisoning.c
@@ -17,7 +17,7 @@ __attribute__((noinline)) void foo(int len) {
   top = &x;
   volatile char array[len];
   assert(!((uintptr_t) array & 31L));
-  __builtin_alloca(len);
+  void *p = __builtin_alloca(len);
   for (int i = 0; i < thirty_two; ++i) {
     char array[i];
     bot = array;
diff --git a/gcc/testsuite/c-c++-common/asan/pr88619.c b/gcc/testsuite/c-c++-common/asan/pr88619.c
index aab8a4c4c9c..fd3fbf1a73a 100644
--- a/gcc/testsuite/c-c++-common/asan/pr88619.c
+++ b/gcc/testsuite/c-c++-common/asan/pr88619.c
@@ -10,5 +10,5 @@ main ()
   A b;
   int *p = &b;
   *(p - 1) = 123;
-  __builtin_alloca (b);
+  void *p2 = __builtin_alloca (b);
 }
diff --git a/gcc/testsuite/g++.dg/overload/using2.C b/gcc/testsuite/g++.dg/overload/using2.C
index b1d52f7cf43..edfd1a10ac4 100644
--- a/gcc/testsuite/g++.dg/overload/using2.C
+++ b/gcc/testsuite/g++.dg/overload/using2.C
@@ -69,7 +69,7 @@ using std::C3; using other::C3;
   long C3 (long) throw ();
 
 int main () {
-  malloc (0);
+  void *p = malloc (0);
   exit (0);
 
   _exit (0); // { dg-error "ambiguous" }
diff --git a/gcc/testsuite/gcc.dg/attr-alloc_size-5.c b/gcc/testsuite/gcc.dg/attr-alloc_size-5.c
index 26ee43f87de..7aa7cbf0c72 100644
--- a/gcc/testsuite/gcc.dg/attr-alloc_size-5.c
+++ b/gcc/testsuite/gcc.dg/attr-alloc_size-5.c
@@ -230,5 +230,5 @@ test_alloca (size_t n)
 {
   extern void* alloca (size_t);
 
-  alloca (0);
+  alloca (0); /* { dg-warning "ignoring return value of '.*' declared with attribute 'warn_unused_result'" } */
 }
diff --git a/gcc/testsuite/gcc.dg/nonnull-3.c b/gcc/testsuite/gcc.dg/nonnull-3.c
index 040248c53aa..6f7bc4f4295 100644
--- a/gcc/testsuite/gcc.dg/nonnull-3.c
+++ b/gcc/testsuite/gcc.dg/nonnull-3.c
@@ -56,8 +56,8 @@ foo (void *p, char *s)
   __builtin_strspn (s, NULL);  /* { dg-warning "null" "null pointer check" } */
   __builtin_strchr (NULL, 16);  /* { dg-warning "null" "null pointer check" } */
   __builtin_strrchr (NULL, 16);  /* { dg-warning "null" "null pointer check" } */
-  __builtin_strdup (NULL);  /* { dg-warning "null" "null pointer check" } */
-  __builtin_strndup (NULL, 16);  /* { dg-warning "null" "null pointer check" } */
+  void *p1 = __builtin_strdup (NULL);  /* { dg-warning "null" "null pointer check" } */
+  void *p2 = __builtin_strndup (NULL, 16);  /* { dg-warning "null" "null pointer check" } */
 
   __builtin_nan (NULL);  /* { dg-warning "null" "null pointer check" } */
   __builtin_nanf (NULL);  /* { dg-warning "null" "null pointer check" } */
diff --git a/gcc/testsuite/gcc.dg/pr43643.c b/gcc/testsuite/gcc.dg/pr43643.c
index b0c57c0a6dc..43896abd85a 100644
--- a/gcc/testsuite/gcc.dg/pr43643.c
+++ b/gcc/testsuite/gcc.dg/pr43643.c
@@ -10,9 +10,9 @@ extern char *strdup (const char *);
 void
 func(char *a, char *b, char *c)
 {
-  strdup(a);
-  strdup(b);
-  strdup(c);
+  void *p = strdup(a);
+  p = strdup(b);
+  p = strdup(c);
 }
 
 int
diff --git a/gcc/testsuite/gcc.dg/pr59717.c b/gcc/testsuite/gcc.dg/pr59717.c
index e49020aa063..64e4f32197f 100644
--- a/gcc/testsuite/gcc.dg/pr59717.c
+++ b/gcc/testsuite/gcc.dg/pr59717.c
@@ -242,7 +242,7 @@ stdlib (void *p, void *q, __SIZE_TYPE__ sz)
 {
   abort (); /* { dg-warning "incompatible implicit" } */
   /* { dg-message "include ..stdlib.h.." "" { target *-*-* } .-1 } */
-  calloc (sz, 1); /* { dg-warning "incompatible implicit" } */
+  void *p1 = calloc (sz, 1); /* { dg-warning "incompatible implicit" } */
   /* { dg-message "include ..stdlib.h.." "" { target *-*-* } .-1 } */
   exit (1); /* { dg-warning "incompatible implicit" } */
   /* { dg-message "include ..stdlib.h.." "" { target *-*-* } .-1 } */
@@ -252,11 +252,11 @@ stdlib (void *p, void *q, __SIZE_TYPE__ sz)
   /* { dg-message "include ..stdlib.h.." "" { target *-*-* } .-1 } */
   llabs (1LL); /* { dg-warning "incompatible implicit" } */
   /* { dg-message "include ..stdlib.h.." "" { target *-*-* } .-1 } */
-  malloc (sz); /* { dg-warning "incompatible implicit" } */
+  void *p2 = malloc (sz); /* { dg-warning "incompatible implicit" } */
   /* { dg-message "include ..stdlib.h.." "" { target *-*-* } .-1 } */
-  realloc (p, sz); /* { dg-warning "incompatible implicit" } */
+  void *p3 = realloc (p, sz); /* { dg-warning "incompatible implicit" } */
   /* { dg-message "include ..stdlib.h.." "" { target *-*-* } .-1 } */
-  aligned_alloc (sz, sz); /* { dg-warning "incompatible implicit" } */
+  void *p4 = aligned_alloc (sz, sz); /* { dg-warning "incompatible implicit" } */
   /* { dg-message "include ..stdlib.h.." "" { target *-*-* } .-1 } */
 }
 
diff --git a/gcc/testsuite/gcc.dg/pr78902.c b/gcc/testsuite/gcc.dg/pr78902.c
new file mode 100644
index 00000000000..49efc970475
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr78902.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+
+void *ptr;
+
+void foo(void)
+{
+ __builtin_malloc (1); /* { dg-warning "ignoring return value of '__builtin_malloc' declared with attribute 'warn_unused_result'" } */
+ __builtin_calloc (10, 20); /* { dg-warning "ignoring return value of '__builtin_calloc' declared with attribute 'warn_unused_result'" } */
+ __builtin_alloca (10); /* { dg-warning "ignoring return value of '__builtin_alloca' declared with attribute 'warn_unused_result'" } */
+ __builtin_realloc (ptr, 100); /* { dg-warning "ignoring return value of '__builtin_realloc' declared with attribute 'warn_unused_result'" } */
+ __builtin_aligned_alloc (10, 16); /* { dg-warning "ignoring return value of '__builtin_aligned_alloc' declared with attribute 'warn_unused_result'" } */
+ __builtin_strdup ("pes"); /* { dg-warning "ignoring return value of '__builtin_strdup' declared with attribute 'warn_unused_result'" } */
+ __builtin_strndup ("pes", 10); /* { dg-warning "ignoring return value of '__builtin_strndup' declared with attribute 'warn_unused_result'" } */
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr71816.c b/gcc/testsuite/gcc.dg/torture/pr71816.c
index 179fad85cd1..be37ad921bd 100644
--- a/gcc/testsuite/gcc.dg/torture/pr71816.c
+++ b/gcc/testsuite/gcc.dg/torture/pr71816.c
@@ -11,7 +11,7 @@ void *realloc();
 int ext2fs_resize_mem(void *p1) {
     int size = 0;
     memcpy(&ext2fs_resize_mem_p, p1, sizeof(ext2fs_resize_mem_p));
-    realloc(&ext2fs_resize_mem_p, size);
+    void *p = realloc(&ext2fs_resize_mem_p, size);
     return 0;
 }
 struct ext2_icount_el *insert_icount_el() {
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr78886.c b/gcc/testsuite/gcc.dg/tree-ssa/pr78886.c
index de22b83ad25..7f62891b77b 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/pr78886.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr78886.c
@@ -7,6 +7,6 @@ void *malloc(size_t x);
 void foo(void)
 {
  volatile int i;
- malloc(1);
+ void *p = malloc(1);
  i;
 }
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr79697.c b/gcc/testsuite/gcc.dg/tree-ssa/pr79697.c
index 973ec0dc193..e62efd5de61 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/pr79697.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr79697.c
@@ -3,17 +3,17 @@
 
 void f(void)
 {
-  __builtin_strdup ("abc");
+  __builtin_strdup ("abc"); /* { dg-warning "ignoring return value of '__builtin_strdup' declared with attribute 'warn_unused_result'" } */
 }
 
 void g(void)
 {
-  __builtin_strndup ("abc", 3);
+  __builtin_strndup ("abc", 3); /* { dg-warning "ignoring return value of '__builtin_strndup' declared with attribute 'warn_unused_result'" } */
 }
 
 void h(void)
 {
-  __builtin_realloc (0, 10);
+  __builtin_realloc (0, 10); /* { dg-warning "ignoring return value of '__builtin_realloc' declared with attribute 'warn_unused_result'" } */
 }
 
 /* { dg-final { scan-tree-dump "Deleting : __builtin_strdup" "cddce1" } } */


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Add warn_unused_result for malloc-like functions (PR tree-optimization/78902).
  2019-06-06  8:02 [PATCH] Add warn_unused_result for malloc-like functions (PR tree-optimization/78902) Martin Liška
@ 2019-06-06 15:17 ` Martin Sebor
  2019-06-06 16:58   ` Jeff Law
  2019-06-07  5:27   ` Martin Liška
  2019-06-07  0:02 ` Jeff Law
  2019-06-07  7:07 ` [PATCH 1/2] Add alloc_size for libiberty memory allocation functions Martin Liška
  2 siblings, 2 replies; 13+ messages in thread
From: Martin Sebor @ 2019-06-06 15:17 UTC (permalink / raw)
  To: Martin Liška, gcc-patches

On 6/6/19 2:01 AM, Martin Liška wrote:
> Hi.
> 
> The patch is about addition of warn_unused_attribute for malloc-like function.
> 
> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.

I like this change (as you know :)  Just one question: should
all allocation functions be also annotated, including the two
variants of __builtin_alloca_with_align and
__builtin_posix_memalign?

As a separate comment, to get the benefit of the attribute in
GCC we might want to also annotate the wrappers in libiberty.h
and perhaps also some (many?) of the functions in tree.h.
(This is just a suggestion to think about independent of your
change.)

Martin

> 
> Ready to be installed?
> Thanks,
> Martin
> 
> gcc/ChangeLog:
> 
> 2019-06-06  Martin Liska  <mliska@suse.cz>
> 
> 	PR tree-optimization/78902
> 	* builtin-attrs.def (ATTR_WARN_UNUSED_RESULT): New.
> 	(ATTR_MALLOC_NOTHROW_LEAF_LIST): Remove.
> 	(ATTR_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
> 	(ATTR_ALLOC_SIZE_2_NOTHROW_LIST): Remove.
> 	(ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST): Remove.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LIST): New.
> 	(ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LIST): New.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
> 	(ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST): Remove.
> 	(ATTR_ALLOCA_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
> 	(ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST):  Remove.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_2_NOTHROW_LEAF_LIST):
> 	New.
> 	(ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST): Remove.
> 	(ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LEAF_LIST): New.
> 	(ATTR_MALLOC_NOTHROW_NONNULL): Remove.
> 	(ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
> 	(ATTR_MALLOC_NOTHROW_NONNULL_LEAF): Remove.
> 	(ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
> 	* builtins.def (BUILT_IN_ALIGNED_ALLOC): Change to use
> 	warn_unused_result attribute.
> 	(BUILT_IN_STRDUP): Likewise.
> 	(BUILT_IN_STRNDUP): Likewise.
> 	(BUILT_IN_ALLOCA): Likewise.
> 	(BUILT_IN_CALLOC): Likewise.
> 	(BUILT_IN_MALLOC): Likewise.
> 	(BUILT_IN_REALLOC): Likewise.
> 
> gcc/testsuite/ChangeLog:
> 
> 2019-06-06  Martin Liska  <mliska@suse.cz>
> 
> 	PR tree-optimization/78902
> 	* c-c++-common/asan/alloca_loop_unpoisoning.c: Use result
> 	of __builtin_alloca.
> 	* c-c++-common/asan/pr88619.c: Likewise.
> 	* g++.dg/overload/using2.C: Likewise for malloc.
> 	* gcc.dg/attr-alloc_size-5.c: Add new dg-warning.
> 	* gcc.dg/nonnull-3.c: Use result of __builtin_strdup.
> 	* gcc.dg/pr43643.c: Likewise.
> 	* gcc.dg/pr59717.c: Likewise for calloc.
> 	* gcc.dg/torture/pr71816.c: Likewise.
> 	* gcc.dg/tree-ssa/pr78886.c: Likewise.
> 	* gcc.dg/tree-ssa/pr79697.c: Likewise.
> 	* gcc.dg/pr78902.c: New test.
> ---
>   gcc/builtin-attrs.def                         | 37 ++++++++++++-------
>   gcc/builtins.def                              | 14 +++----
>   .../asan/alloca_loop_unpoisoning.c            |  2 +-
>   gcc/testsuite/c-c++-common/asan/pr88619.c     |  2 +-
>   gcc/testsuite/g++.dg/overload/using2.C        |  2 +-
>   gcc/testsuite/gcc.dg/attr-alloc_size-5.c      |  2 +-
>   gcc/testsuite/gcc.dg/nonnull-3.c              |  4 +-
>   gcc/testsuite/gcc.dg/pr43643.c                |  6 +--
>   gcc/testsuite/gcc.dg/pr59717.c                |  8 ++--
>   gcc/testsuite/gcc.dg/pr78902.c                | 14 +++++++
>   gcc/testsuite/gcc.dg/torture/pr71816.c        |  2 +-
>   gcc/testsuite/gcc.dg/tree-ssa/pr78886.c       |  2 +-
>   gcc/testsuite/gcc.dg/tree-ssa/pr79697.c       |  6 +--
>   13 files changed, 62 insertions(+), 39 deletions(-)
>   create mode 100644 gcc/testsuite/gcc.dg/pr78902.c
> 
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Add warn_unused_result for malloc-like functions (PR tree-optimization/78902).
  2019-06-06 15:17 ` Martin Sebor
@ 2019-06-06 16:58   ` Jeff Law
  2019-06-07  5:27   ` Martin Liška
  1 sibling, 0 replies; 13+ messages in thread
From: Jeff Law @ 2019-06-06 16:58 UTC (permalink / raw)
  To: Martin Sebor, Martin Liška, gcc-patches

On 6/6/19 9:17 AM, Martin Sebor wrote:
> On 6/6/19 2:01 AM, Martin Liška wrote:
>> Hi.
>>
>> The patch is about addition of warn_unused_attribute for malloc-like
>> function.
>>
>> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
> 
> I like this change (as you know :)  Just one question: should
> all allocation functions be also annotated, including the two
> variants of __builtin_alloca_with_align and
> __builtin_posix_memalign?
> 
> As a separate comment, to get the benefit of the attribute in
> GCC we might want to also annotate the wrappers in libiberty.h
> and perhaps also some (many?) of the functions in tree.h.
> (This is just a suggestion to think about independent of your
> change.)
I like it conceptually as well -- I think the glibc guys had pretty good
results exposing latent issues when they went through and did the
necessary markup for WUR warnings.

Jeff

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Add warn_unused_result for malloc-like functions (PR tree-optimization/78902).
  2019-06-06  8:02 [PATCH] Add warn_unused_result for malloc-like functions (PR tree-optimization/78902) Martin Liška
  2019-06-06 15:17 ` Martin Sebor
@ 2019-06-07  0:02 ` Jeff Law
  2019-06-07  6:27   ` Martin Liška
  2019-06-07  7:07 ` [PATCH 1/2] Add alloc_size for libiberty memory allocation functions Martin Liška
  2 siblings, 1 reply; 13+ messages in thread
From: Jeff Law @ 2019-06-07  0:02 UTC (permalink / raw)
  To: Martin Liška, gcc-patches; +Cc: Martin Sebor

On 6/6/19 2:01 AM, Martin Liška wrote:
> Hi.
> 
> The patch is about addition of warn_unused_attribute for malloc-like function.
> 
> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
> 
> Ready to be installed?
> Thanks,
> Martin
> 
> gcc/ChangeLog:
> 
> 2019-06-06  Martin Liska  <mliska@suse.cz>
> 
> 	PR tree-optimization/78902
> 	* builtin-attrs.def (ATTR_WARN_UNUSED_RESULT): New.
> 	(ATTR_MALLOC_NOTHROW_LEAF_LIST): Remove.
> 	(ATTR_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
> 	(ATTR_ALLOC_SIZE_2_NOTHROW_LIST): Remove.
> 	(ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST): Remove.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LIST): New.
> 	(ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LIST): New.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
> 	(ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST): Remove.
> 	(ATTR_ALLOCA_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
> 	(ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST):  Remove.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_2_NOTHROW_LEAF_LIST):
> 	New.
> 	(ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST): Remove.
> 	(ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LEAF_LIST): New.
> 	(ATTR_MALLOC_NOTHROW_NONNULL): Remove.
> 	(ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
> 	(ATTR_MALLOC_NOTHROW_NONNULL_LEAF): Remove.
> 	(ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
> 	(ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
> 	* builtins.def (BUILT_IN_ALIGNED_ALLOC): Change to use
> 	warn_unused_result attribute.
> 	(BUILT_IN_STRDUP): Likewise.
> 	(BUILT_IN_STRNDUP): Likewise.
> 	(BUILT_IN_ALLOCA): Likewise.
> 	(BUILT_IN_CALLOC): Likewise.
> 	(BUILT_IN_MALLOC): Likewise.
> 	(BUILT_IN_REALLOC): Likewise.
> 
> gcc/testsuite/ChangeLog:
> 
> 2019-06-06  Martin Liska  <mliska@suse.cz>
> 
> 	PR tree-optimization/78902
> 	* c-c++-common/asan/alloca_loop_unpoisoning.c: Use result
> 	of __builtin_alloca.
> 	* c-c++-common/asan/pr88619.c: Likewise.
> 	* g++.dg/overload/using2.C: Likewise for malloc.
> 	* gcc.dg/attr-alloc_size-5.c: Add new dg-warning.
> 	* gcc.dg/nonnull-3.c: Use result of __builtin_strdup.
> 	* gcc.dg/pr43643.c: Likewise.
> 	* gcc.dg/pr59717.c: Likewise for calloc.
> 	* gcc.dg/torture/pr71816.c: Likewise.
> 	* gcc.dg/tree-ssa/pr78886.c: Likewise.
> 	* gcc.dg/tree-ssa/pr79697.c: Likewise.
> 	* gcc.dg/pr78902.c: New test.
OK.

Any thoughts on whether or not we'd want to IPA propagate this attribute
like we do for the malloc attribute?

jeff
> ---

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Add warn_unused_result for malloc-like functions (PR tree-optimization/78902).
  2019-06-06 15:17 ` Martin Sebor
  2019-06-06 16:58   ` Jeff Law
@ 2019-06-07  5:27   ` Martin Liška
  2019-06-07 15:09     ` Martin Sebor
  1 sibling, 1 reply; 13+ messages in thread
From: Martin Liška @ 2019-06-07  5:27 UTC (permalink / raw)
  To: Martin Sebor, gcc-patches

On 6/6/19 5:17 PM, Martin Sebor wrote:
> On 6/6/19 2:01 AM, Martin Liška wrote:
>> Hi.
>>
>> The patch is about addition of warn_unused_attribute for malloc-like function.
>>
>> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
> 
> I like this change (as you know :)  Just one question: should
> all allocation functions be also annotated, including the two
> variants of __builtin_alloca_with_align and
> __builtin_posix_memalign?

Hi, we define the first one (and __builtin_alloca_with_align_and_max) in builtins.def
as: /* Implementing variable sized local variables.  */
and define just a stub: DEF_BUILTIN_STUB

Do you know why is that a variable sized function?
The later one is a bit different as it does not return memory, but return code.
Glibc does not mark it with warn_unused_result attr.

> 
> As a separate comment, to get the benefit of the attribute in
> GCC we might want to also annotate the wrappers in libiberty.h
> and perhaps also some (many?) of the functions in tree.h.
> (This is just a suggestion to think about independent of your
> change.)

Good idea, I can prepare a patch for it as well.

Martin

> 
> Martin
> 
>>
>> Ready to be installed?
>> Thanks,
>> Martin
>>
>> gcc/ChangeLog:
>>
>> 2019-06-06  Martin Liska  <mliska@suse.cz>
>>
>>     PR tree-optimization/78902
>>     * builtin-attrs.def (ATTR_WARN_UNUSED_RESULT): New.
>>     (ATTR_MALLOC_NOTHROW_LEAF_LIST): Remove.
>>     (ATTR_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
>>     (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
>>     (ATTR_ALLOC_SIZE_2_NOTHROW_LIST): Remove.
>>     (ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST): Remove.
>>     (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LIST): New.
>>     (ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LIST): New.
>>     (ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
>>     (ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST): Remove.
>>     (ATTR_ALLOCA_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
>>     (ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST):  Remove.
>>     (ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_2_NOTHROW_LEAF_LIST):
>>     New.
>>     (ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST): Remove.
>>     (ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LEAF_LIST): New.
>>     (ATTR_MALLOC_NOTHROW_NONNULL): Remove.
>>     (ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
>>     (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
>>     (ATTR_MALLOC_NOTHROW_NONNULL_LEAF): Remove.
>>     (ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
>>     (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
>>     * builtins.def (BUILT_IN_ALIGNED_ALLOC): Change to use
>>     warn_unused_result attribute.
>>     (BUILT_IN_STRDUP): Likewise.
>>     (BUILT_IN_STRNDUP): Likewise.
>>     (BUILT_IN_ALLOCA): Likewise.
>>     (BUILT_IN_CALLOC): Likewise.
>>     (BUILT_IN_MALLOC): Likewise.
>>     (BUILT_IN_REALLOC): Likewise.
>>
>> gcc/testsuite/ChangeLog:
>>
>> 2019-06-06  Martin Liska  <mliska@suse.cz>
>>
>>     PR tree-optimization/78902
>>     * c-c++-common/asan/alloca_loop_unpoisoning.c: Use result
>>     of __builtin_alloca.
>>     * c-c++-common/asan/pr88619.c: Likewise.
>>     * g++.dg/overload/using2.C: Likewise for malloc.
>>     * gcc.dg/attr-alloc_size-5.c: Add new dg-warning.
>>     * gcc.dg/nonnull-3.c: Use result of __builtin_strdup.
>>     * gcc.dg/pr43643.c: Likewise.
>>     * gcc.dg/pr59717.c: Likewise for calloc.
>>     * gcc.dg/torture/pr71816.c: Likewise.
>>     * gcc.dg/tree-ssa/pr78886.c: Likewise.
>>     * gcc.dg/tree-ssa/pr79697.c: Likewise.
>>     * gcc.dg/pr78902.c: New test.
>> ---
>>   gcc/builtin-attrs.def                         | 37 ++++++++++++-------
>>   gcc/builtins.def                              | 14 +++----
>>   .../asan/alloca_loop_unpoisoning.c            |  2 +-
>>   gcc/testsuite/c-c++-common/asan/pr88619.c     |  2 +-
>>   gcc/testsuite/g++.dg/overload/using2.C        |  2 +-
>>   gcc/testsuite/gcc.dg/attr-alloc_size-5.c      |  2 +-
>>   gcc/testsuite/gcc.dg/nonnull-3.c              |  4 +-
>>   gcc/testsuite/gcc.dg/pr43643.c                |  6 +--
>>   gcc/testsuite/gcc.dg/pr59717.c                |  8 ++--
>>   gcc/testsuite/gcc.dg/pr78902.c                | 14 +++++++
>>   gcc/testsuite/gcc.dg/torture/pr71816.c        |  2 +-
>>   gcc/testsuite/gcc.dg/tree-ssa/pr78886.c       |  2 +-
>>   gcc/testsuite/gcc.dg/tree-ssa/pr79697.c       |  6 +--
>>   13 files changed, 62 insertions(+), 39 deletions(-)
>>   create mode 100644 gcc/testsuite/gcc.dg/pr78902.c
>>
>>
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Add warn_unused_result for malloc-like functions (PR tree-optimization/78902).
  2019-06-07  0:02 ` Jeff Law
@ 2019-06-07  6:27   ` Martin Liška
  0 siblings, 0 replies; 13+ messages in thread
From: Martin Liška @ 2019-06-07  6:27 UTC (permalink / raw)
  To: Jeff Law, gcc-patches; +Cc: Martin Sebor

On 6/7/19 2:02 AM, Jeff Law wrote:
> Any thoughts on whether or not we'd want to IPA propagate this attribute
> like we do for the malloc attribute?

If I see correctly, the pass_warn_unused_result is run very early in the early
tree optimizations. Thus we can't propagate that right now.

Martin

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH 1/2] Add alloc_size for libiberty memory allocation functions.
  2019-06-06  8:02 [PATCH] Add warn_unused_result for malloc-like functions (PR tree-optimization/78902) Martin Liška
  2019-06-06 15:17 ` Martin Sebor
  2019-06-07  0:02 ` Jeff Law
@ 2019-06-07  7:07 ` Martin Liška
  2019-06-07  7:08   ` [PATCH 2/2] Add warn_unused_result attribute for memory-related, functions in libiberty Martin Liška
                     ` (2 more replies)
  2 siblings, 3 replies; 13+ messages in thread
From: Martin Liška @ 2019-06-07  7:07 UTC (permalink / raw)
  To: gcc-patches; +Cc: Martin Sebor

[-- Attachment #1: Type: text/plain, Size: 198 bytes --]

Hi.

I'm sending 2 follow up patches that touch libiberty memory allocation
functions.

Patch can bootstrap on x86_64-linux-gnu and survives regression tests.

Ready to be installed?
Thanks,
Martin

[-- Attachment #2: 0001-Add-alloc_size-for-libiberty-memory-allocation-funct.patch --]
[-- Type: text/x-patch, Size: 2941 bytes --]

From bd430388cffe4b72d6be52cbaeda9eb9c8f8a248 Mon Sep 17 00:00:00 2001
From: Martin Liska <mliska@suse.cz>
Date: Fri, 7 Jun 2019 08:09:36 +0200
Subject: [PATCH 1/2] Add alloc_size for libiberty memory allocation functions.

include/ChangeLog:

2019-06-07  Martin Liska  <mliska@suse.cz>

	* ansidecl.h:
	(ATTRIBUTE_RESULT_SIZE_1): Define new macro.
	(ATTRIBUTE_RESULT_SIZE_2): Likewise.
	(ATTRIBUTE_RESULT_SIZE_1_2): Likewise.
	* libiberty.h (xmalloc): Add RESULT_SIZE attribute.
	(xrealloc): Likewise.
	(xcalloc): Likewise.
---
 include/ansidecl.h  | 25 +++++++++++++++++++++++++
 include/libiberty.h |  6 +++---
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/include/ansidecl.h b/include/ansidecl.h
index 0395c4d0e19..3035c33b8aa 100644
--- a/include/ansidecl.h
+++ b/include/ansidecl.h
@@ -292,6 +292,31 @@ So instead we use the macro below and test it against specific values.  */
 # endif
 #endif
 
+/* Attribute `alloc_size' was valid as of gcc 4.3.  */
+#ifndef ATTRIBUTE_RESULT_SIZE_1
+# if (GCC_VERSION >= 4003)
+#  define ATTRIBUTE_RESULT_SIZE_1 __attribute__ ((alloc_size (1)))
+# else
+#  define ATTRIBUTE_RESULT_SIZE_1
+#endif
+#endif
+
+#ifndef ATTRIBUTE_RESULT_SIZE_2
+# if (GCC_VERSION >= 4003)
+#  define ATTRIBUTE_RESULT_SIZE_2 __attribute__ ((alloc_size (2)))
+# else
+#  define ATTRIBUTE_RESULT_SIZE_2
+#endif
+#endif
+
+#ifndef ATTRIBUTE_RESULT_SIZE_1_2
+# if (GCC_VERSION >= 4003)
+#  define ATTRIBUTE_RESULT_SIZE_1_2 __attribute__ ((alloc_size (1, 2)))
+# else
+#  define ATTRIBUTE_RESULT_SIZE_1_2
+#endif
+#endif
+
 /* We use __extension__ in some places to suppress -pedantic warnings
    about GCC extensions.  This feature didn't work properly before
    gcc 2.8.  */
diff --git a/include/libiberty.h b/include/libiberty.h
index e11aa97f0ba..0870163d945 100644
--- a/include/libiberty.h
+++ b/include/libiberty.h
@@ -310,18 +310,18 @@ extern void xmalloc_failed (size_t) ATTRIBUTE_NORETURN;
    message to stderr (using the name set by xmalloc_set_program_name,
    if any) and then call xexit.  */
 
-extern void *xmalloc (size_t) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL;
+extern void *xmalloc (size_t) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_RESULT_SIZE_1;
 
 /* Reallocate memory without fail.  This works like xmalloc.  Note,
    realloc type functions are not suitable for attribute malloc since
    they may return the same address across multiple calls. */
 
-extern void *xrealloc (void *, size_t) ATTRIBUTE_RETURNS_NONNULL;
+extern void *xrealloc (void *, size_t) ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_RESULT_SIZE_2;
 
 /* Allocate memory without fail and set it to zero.  This works like
    xmalloc.  */
 
-extern void *xcalloc (size_t, size_t) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL;
+extern void *xcalloc (size_t, size_t) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_RESULT_SIZE_1_2;
 
 /* Copy a string into a memory buffer without fail.  */
 
-- 
2.21.0


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH 2/2] Add warn_unused_result attribute for memory-related, functions in libiberty.
  2019-06-07  7:07 ` [PATCH 1/2] Add alloc_size for libiberty memory allocation functions Martin Liška
@ 2019-06-07  7:08   ` Martin Liška
  2019-06-07 17:13     ` Jeff Law
  2019-06-07 17:13   ` [PATCH 1/2] Add alloc_size for libiberty memory allocation functions Jeff Law
  2019-06-10 22:52   ` [PATCH] Fix i686-linux bootstrap (was Re: [PATCH 1/2] Add alloc_size for libiberty memory allocation functions.) Jakub Jelinek
  2 siblings, 1 reply; 13+ messages in thread
From: Martin Liška @ 2019-06-07  7:08 UTC (permalink / raw)
  To: gcc-patches; +Cc: Martin Sebor

[-- Attachment #1: Type: text/plain, Size: 8 bytes --]

Part 2.

[-- Attachment #2: 0002-Add-warn_unused_result-attribute-for-memory-related-.patch --]
[-- Type: text/x-patch, Size: 3596 bytes --]

From 52f1ae0aa86b11fc3ae17e3e224b58aa8b8519a6 Mon Sep 17 00:00:00 2001
From: Martin Liska <mliska@suse.cz>
Date: Fri, 7 Jun 2019 08:14:17 +0200
Subject: [PATCH 2/2] Add warn_unused_result attribute for memory-related
 functions in libiberty.

include/ChangeLog:

2019-06-07  Martin Liska  <mliska@suse.cz>

	* ansidecl.h (ATTRIBUTE_WARN_UNUSED_RESULT): New macro.
	* libiberty.h (xmalloc): Use it.
	(xrealloc): Likewise.
	(xcalloc): Likewise.
	(xstrdup): Likewise.
	(xstrndup): Likewise.
	(xmemdup): Likewise.
---
 include/ansidecl.h  |  9 +++++++++
 include/libiberty.h | 12 ++++++------
 2 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/include/ansidecl.h b/include/ansidecl.h
index 3035c33b8aa..445c8c3fb58 100644
--- a/include/ansidecl.h
+++ b/include/ansidecl.h
@@ -317,6 +317,15 @@ So instead we use the macro below and test it against specific values.  */
 #endif
 #endif
 
+/* Attribute `warn_unused_result' was valid as of gcc 3.3.  */
+#ifndef ATTRIBUTE_WARN_UNUSED_RESULT
+# if GCC_VERSION >= 3003
+#  define ATTRIBUTE_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
+# else
+#  define ATTRIBUTE_WARN_UNUSED_RESULT
+# endif
+#endif
+
 /* We use __extension__ in some places to suppress -pedantic warnings
    about GCC extensions.  This feature didn't work properly before
    gcc 2.8.  */
diff --git a/include/libiberty.h b/include/libiberty.h
index 0870163d945..635519e088a 100644
--- a/include/libiberty.h
+++ b/include/libiberty.h
@@ -310,30 +310,30 @@ extern void xmalloc_failed (size_t) ATTRIBUTE_NORETURN;
    message to stderr (using the name set by xmalloc_set_program_name,
    if any) and then call xexit.  */
 
-extern void *xmalloc (size_t) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_RESULT_SIZE_1;
+extern void *xmalloc (size_t) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_RESULT_SIZE_1 ATTRIBUTE_WARN_UNUSED_RESULT;
 
 /* Reallocate memory without fail.  This works like xmalloc.  Note,
    realloc type functions are not suitable for attribute malloc since
    they may return the same address across multiple calls. */
 
-extern void *xrealloc (void *, size_t) ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_RESULT_SIZE_2;
+extern void *xrealloc (void *, size_t) ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_RESULT_SIZE_2 ATTRIBUTE_WARN_UNUSED_RESULT;
 
 /* Allocate memory without fail and set it to zero.  This works like
    xmalloc.  */
 
-extern void *xcalloc (size_t, size_t) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_RESULT_SIZE_1_2;
+extern void *xcalloc (size_t, size_t) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_RESULT_SIZE_1_2 ATTRIBUTE_WARN_UNUSED_RESULT;
 
 /* Copy a string into a memory buffer without fail.  */
 
-extern char *xstrdup (const char *) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL;
+extern char *xstrdup (const char *) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_WARN_UNUSED_RESULT;
 
 /* Copy at most N characters from string into a buffer without fail.  */
 
-extern char *xstrndup (const char *, size_t) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL;
+extern char *xstrndup (const char *, size_t) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_WARN_UNUSED_RESULT;
 
 /* Copy an existing memory buffer to a new memory buffer without fail.  */
 
-extern void *xmemdup (const void *, size_t, size_t) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL;
+extern void *xmemdup (const void *, size_t, size_t) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_WARN_UNUSED_RESULT;
 
 /* Physical memory routines.  Return values are in BYTES.  */
 extern double physmem_total (void);
-- 
2.21.0


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Add warn_unused_result for malloc-like functions (PR tree-optimization/78902).
  2019-06-07  5:27   ` Martin Liška
@ 2019-06-07 15:09     ` Martin Sebor
  0 siblings, 0 replies; 13+ messages in thread
From: Martin Sebor @ 2019-06-07 15:09 UTC (permalink / raw)
  To: Martin Liška, gcc-patches

On 6/6/19 11:27 PM, Martin Liška wrote:
> On 6/6/19 5:17 PM, Martin Sebor wrote:
>> On 6/6/19 2:01 AM, Martin Liška wrote:
>>> Hi.
>>>
>>> The patch is about addition of warn_unused_attribute for malloc-like function.
>>>
>>> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
>>
>> I like this change (as you know :)  Just one question: should
>> all allocation functions be also annotated, including the two
>> variants of __builtin_alloca_with_align and
>> __builtin_posix_memalign?
> 
> Hi, we define the first one (and __builtin_alloca_with_align_and_max) in builtins.def
> as: /* Implementing variable sized local variables.  */
> and define just a stub: DEF_BUILTIN_STUB
> 
> Do you know why is that a variable sized function?

__builtin_alloca_with_align implements VLAs.  I think the stubs
are used for intrnisics that were originally meant to be called
exclusively by GCC alone and not directly by programs.

> The later one is a bit different as it does not return memory, but return code.
> Glibc does not mark it with warn_unused_result attr.

My mistake.  I confused posix_memalign with aligned_alloc.

Martin


> 
>>
>> As a separate comment, to get the benefit of the attribute in
>> GCC we might want to also annotate the wrappers in libiberty.h
>> and perhaps also some (many?) of the functions in tree.h.
>> (This is just a suggestion to think about independent of your
>> change.)
> 
> Good idea, I can prepare a patch for it as well.
> 
> Martin
> 
>>
>> Martin
>>
>>>
>>> Ready to be installed?
>>> Thanks,
>>> Martin
>>>
>>> gcc/ChangeLog:
>>>
>>> 2019-06-06  Martin Liska  <mliska@suse.cz>
>>>
>>>      PR tree-optimization/78902
>>>      * builtin-attrs.def (ATTR_WARN_UNUSED_RESULT): New.
>>>      (ATTR_MALLOC_NOTHROW_LEAF_LIST): Remove.
>>>      (ATTR_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
>>>      (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LEAF_LIST): New.
>>>      (ATTR_ALLOC_SIZE_2_NOTHROW_LIST): Remove.
>>>      (ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST): Remove.
>>>      (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_LIST): New.
>>>      (ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LIST): New.
>>>      (ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
>>>      (ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST): Remove.
>>>      (ATTR_ALLOCA_WARN_UNUSED_RESULT_SIZE_1_NOTHROW_LEAF_LIST): New.
>>>      (ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST):  Remove.
>>>      (ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_2_NOTHROW_LEAF_LIST):
>>>      New.
>>>      (ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST): Remove.
>>>      (ATTR_ALLOC_WARN_UNUSED_RESULT_SIZE_2_NOTHROW_LEAF_LIST): New.
>>>      (ATTR_MALLOC_NOTHROW_NONNULL): Remove.
>>>      (ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
>>>      (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL): New.
>>>      (ATTR_MALLOC_NOTHROW_NONNULL_LEAF): Remove.
>>>      (ATTR_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
>>>      (ATTR_MALLOC_WARN_UNUSED_RESULT_NOTHROW_NONNULL_LEAF): New.
>>>      * builtins.def (BUILT_IN_ALIGNED_ALLOC): Change to use
>>>      warn_unused_result attribute.
>>>      (BUILT_IN_STRDUP): Likewise.
>>>      (BUILT_IN_STRNDUP): Likewise.
>>>      (BUILT_IN_ALLOCA): Likewise.
>>>      (BUILT_IN_CALLOC): Likewise.
>>>      (BUILT_IN_MALLOC): Likewise.
>>>      (BUILT_IN_REALLOC): Likewise.
>>>
>>> gcc/testsuite/ChangeLog:
>>>
>>> 2019-06-06  Martin Liska  <mliska@suse.cz>
>>>
>>>      PR tree-optimization/78902
>>>      * c-c++-common/asan/alloca_loop_unpoisoning.c: Use result
>>>      of __builtin_alloca.
>>>      * c-c++-common/asan/pr88619.c: Likewise.
>>>      * g++.dg/overload/using2.C: Likewise for malloc.
>>>      * gcc.dg/attr-alloc_size-5.c: Add new dg-warning.
>>>      * gcc.dg/nonnull-3.c: Use result of __builtin_strdup.
>>>      * gcc.dg/pr43643.c: Likewise.
>>>      * gcc.dg/pr59717.c: Likewise for calloc.
>>>      * gcc.dg/torture/pr71816.c: Likewise.
>>>      * gcc.dg/tree-ssa/pr78886.c: Likewise.
>>>      * gcc.dg/tree-ssa/pr79697.c: Likewise.
>>>      * gcc.dg/pr78902.c: New test.
>>> ---
>>>    gcc/builtin-attrs.def                         | 37 ++++++++++++-------
>>>    gcc/builtins.def                              | 14 +++----
>>>    .../asan/alloca_loop_unpoisoning.c            |  2 +-
>>>    gcc/testsuite/c-c++-common/asan/pr88619.c     |  2 +-
>>>    gcc/testsuite/g++.dg/overload/using2.C        |  2 +-
>>>    gcc/testsuite/gcc.dg/attr-alloc_size-5.c      |  2 +-
>>>    gcc/testsuite/gcc.dg/nonnull-3.c              |  4 +-
>>>    gcc/testsuite/gcc.dg/pr43643.c                |  6 +--
>>>    gcc/testsuite/gcc.dg/pr59717.c                |  8 ++--
>>>    gcc/testsuite/gcc.dg/pr78902.c                | 14 +++++++
>>>    gcc/testsuite/gcc.dg/torture/pr71816.c        |  2 +-
>>>    gcc/testsuite/gcc.dg/tree-ssa/pr78886.c       |  2 +-
>>>    gcc/testsuite/gcc.dg/tree-ssa/pr79697.c       |  6 +--
>>>    13 files changed, 62 insertions(+), 39 deletions(-)
>>>    create mode 100644 gcc/testsuite/gcc.dg/pr78902.c
>>>
>>>
>>
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 1/2] Add alloc_size for libiberty memory allocation functions.
  2019-06-07  7:07 ` [PATCH 1/2] Add alloc_size for libiberty memory allocation functions Martin Liška
  2019-06-07  7:08   ` [PATCH 2/2] Add warn_unused_result attribute for memory-related, functions in libiberty Martin Liška
@ 2019-06-07 17:13   ` Jeff Law
  2019-06-10 22:52   ` [PATCH] Fix i686-linux bootstrap (was Re: [PATCH 1/2] Add alloc_size for libiberty memory allocation functions.) Jakub Jelinek
  2 siblings, 0 replies; 13+ messages in thread
From: Jeff Law @ 2019-06-07 17:13 UTC (permalink / raw)
  To: Martin Liška, gcc-patches; +Cc: Martin Sebor

On 6/7/19 1:07 AM, Martin Liška wrote:
> Hi.
> 
> I'm sending 2 follow up patches that touch libiberty memory allocation
> functions.
> 
> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
> 
> Ready to be installed?
> Thanks,
> Martin
> 
> 
> 0001-Add-alloc_size-for-libiberty-memory-allocation-funct.patch
> 
> From bd430388cffe4b72d6be52cbaeda9eb9c8f8a248 Mon Sep 17 00:00:00 2001
> From: Martin Liska <mliska@suse.cz>
> Date: Fri, 7 Jun 2019 08:09:36 +0200
> Subject: [PATCH 1/2] Add alloc_size for libiberty memory allocation functions.
> 
> include/ChangeLog:
> 
> 2019-06-07  Martin Liska  <mliska@suse.cz>
> 
> 	* ansidecl.h:
> 	(ATTRIBUTE_RESULT_SIZE_1): Define new macro.
> 	(ATTRIBUTE_RESULT_SIZE_2): Likewise.
> 	(ATTRIBUTE_RESULT_SIZE_1_2): Likewise.
> 	* libiberty.h (xmalloc): Add RESULT_SIZE attribute.
> 	(xrealloc): Likewise.
> 	(xcalloc): Likewise.
OK.
jeff

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 2/2] Add warn_unused_result attribute for memory-related, functions in libiberty.
  2019-06-07  7:08   ` [PATCH 2/2] Add warn_unused_result attribute for memory-related, functions in libiberty Martin Liška
@ 2019-06-07 17:13     ` Jeff Law
  0 siblings, 0 replies; 13+ messages in thread
From: Jeff Law @ 2019-06-07 17:13 UTC (permalink / raw)
  To: Martin Liška, gcc-patches; +Cc: Martin Sebor

On 6/7/19 1:08 AM, Martin Liška wrote:
> Part 2.
> 
> 
> 0002-Add-warn_unused_result-attribute-for-memory-related-.patch
> 
> From 52f1ae0aa86b11fc3ae17e3e224b58aa8b8519a6 Mon Sep 17 00:00:00 2001
> From: Martin Liska <mliska@suse.cz>
> Date: Fri, 7 Jun 2019 08:14:17 +0200
> Subject: [PATCH 2/2] Add warn_unused_result attribute for memory-related
>  functions in libiberty.
> 
> include/ChangeLog:
> 
> 2019-06-07  Martin Liska  <mliska@suse.cz>
> 
> 	* ansidecl.h (ATTRIBUTE_WARN_UNUSED_RESULT): New macro.
> 	* libiberty.h (xmalloc): Use it.
> 	(xrealloc): Likewise.
> 	(xcalloc): Likewise.
> 	(xstrdup): Likewise.
> 	(xstrndup): Likewise.
> 	(xmemdup): Likewise.
OK
jeff

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH] Fix i686-linux bootstrap (was Re: [PATCH 1/2] Add alloc_size for libiberty memory allocation functions.)
  2019-06-07  7:07 ` [PATCH 1/2] Add alloc_size for libiberty memory allocation functions Martin Liška
  2019-06-07  7:08   ` [PATCH 2/2] Add warn_unused_result attribute for memory-related, functions in libiberty Martin Liška
  2019-06-07 17:13   ` [PATCH 1/2] Add alloc_size for libiberty memory allocation functions Jeff Law
@ 2019-06-10 22:52   ` Jakub Jelinek
  2019-06-11  8:18     ` Richard Biener
  2 siblings, 1 reply; 13+ messages in thread
From: Jakub Jelinek @ 2019-06-10 22:52 UTC (permalink / raw)
  To: Martin Liška, Richard Biener, Jeff Law; +Cc: gcc-patches, Martin Sebor

On Fri, Jun 07, 2019 at 09:07:39AM +0200, Martin Liška wrote:
> 2019-06-07  Martin Liska  <mliska@suse.cz>
> 
> 	* ansidecl.h:
> 	(ATTRIBUTE_RESULT_SIZE_1): Define new macro.
> 	(ATTRIBUTE_RESULT_SIZE_2): Likewise.
> 	(ATTRIBUTE_RESULT_SIZE_1_2): Likewise.
> 	* libiberty.h (xmalloc): Add RESULT_SIZE attribute.
> 	(xrealloc): Likewise.
> 	(xcalloc): Likewise.

This change broke i686-linux bootstrap, we get:
error: argument 1 value '4294967292' exceeds maximum object size 2147483647 [-Werror=alloc-size-larger-than=]
error, false positive warning.
Because blocks.length () is
  unsigned length (void) const
  { return m_vec ? m_vec->length () : 0; }
we decide to jump-thread that and think the 0 value is somehow special and
the warning decides to warn, but the fact is that we don't really know
anything about it (and for such case don't warn).

The caller actually guarantees that blocks.length () is > 0 as it pushes one
basic block to the vector unconditionally, so we can work around this
warning by adding an assertion.  Not entirely happy about that, on the other
side nobody uses trans-mem and so the extra comparison and cold check
isn't that big a deal.

Bootstrapped/regtested on i686-linux, ok for trunk?

2019-06-10  Jakub Jelinek  <jakub@redhat.com>

	PR bootstrap/90819
	* trans-mem.c (tm_memopt_compute_available): Add assertion
	that blocks is not empty.  Formatting fix.

--- gcc/trans-mem.c.jj	2019-05-20 21:59:20.081717030 +0200
+++ gcc/trans-mem.c	2019-06-10 23:34:31.725056038 +0200
@@ -3708,9 +3708,9 @@ tm_memopt_compute_available (struct tm_r
   /* Allocate a worklist array/queue.  Entries are only added to the
      list if they were not already on the list.  So the size is
      bounded by the number of basic blocks in the region.  */
+  gcc_assert (!blocks.is_empty ());
   qlen = blocks.length () - 1;
-  qin = qout = worklist =
-    XNEWVEC (basic_block, qlen);
+  qin = qout = worklist = XNEWVEC (basic_block, qlen);
 
   /* Put every block in the region on the worklist.  */
   for (i = 0; blocks.iterate (i, &bb); ++i)


	Jakub

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Fix i686-linux bootstrap (was Re: [PATCH 1/2] Add alloc_size for libiberty memory allocation functions.)
  2019-06-10 22:52   ` [PATCH] Fix i686-linux bootstrap (was Re: [PATCH 1/2] Add alloc_size for libiberty memory allocation functions.) Jakub Jelinek
@ 2019-06-11  8:18     ` Richard Biener
  0 siblings, 0 replies; 13+ messages in thread
From: Richard Biener @ 2019-06-11  8:18 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: Martin Liška, Jeff Law, gcc-patches, Martin Sebor

[-- Attachment #1: Type: text/plain, Size: 2413 bytes --]

On Tue, 11 Jun 2019, Jakub Jelinek wrote:

> On Fri, Jun 07, 2019 at 09:07:39AM +0200, Martin Liška wrote:
> > 2019-06-07  Martin Liska  <mliska@suse.cz>
> > 
> > 	* ansidecl.h:
> > 	(ATTRIBUTE_RESULT_SIZE_1): Define new macro.
> > 	(ATTRIBUTE_RESULT_SIZE_2): Likewise.
> > 	(ATTRIBUTE_RESULT_SIZE_1_2): Likewise.
> > 	* libiberty.h (xmalloc): Add RESULT_SIZE attribute.
> > 	(xrealloc): Likewise.
> > 	(xcalloc): Likewise.
> 
> This change broke i686-linux bootstrap, we get:
> error: argument 1 value '4294967292' exceeds maximum object size 2147483647 [-Werror=alloc-size-larger-than=]
> error, false positive warning.
> Because blocks.length () is
>   unsigned length (void) const
>   { return m_vec ? m_vec->length () : 0; }
> we decide to jump-thread that and think the 0 value is somehow special and
> the warning decides to warn, but the fact is that we don't really know
> anything about it (and for such case don't warn).
> 
> The caller actually guarantees that blocks.length () is > 0 as it pushes one
> basic block to the vector unconditionally, so we can work around this
> warning by adding an assertion.  Not entirely happy about that, on the other
> side nobody uses trans-mem and so the extra comparison and cold check
> isn't that big a deal.
> 
> Bootstrapped/regtested on i686-linux, ok for trunk?

OK.

Richard.

> 2019-06-10  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR bootstrap/90819
> 	* trans-mem.c (tm_memopt_compute_available): Add assertion
> 	that blocks is not empty.  Formatting fix.
> 
> --- gcc/trans-mem.c.jj	2019-05-20 21:59:20.081717030 +0200
> +++ gcc/trans-mem.c	2019-06-10 23:34:31.725056038 +0200
> @@ -3708,9 +3708,9 @@ tm_memopt_compute_available (struct tm_r
>    /* Allocate a worklist array/queue.  Entries are only added to the
>       list if they were not already on the list.  So the size is
>       bounded by the number of basic blocks in the region.  */
> +  gcc_assert (!blocks.is_empty ());
>    qlen = blocks.length () - 1;
> -  qin = qout = worklist =
> -    XNEWVEC (basic_block, qlen);
> +  qin = qout = worklist = XNEWVEC (basic_block, qlen);
>  
>    /* Put every block in the region on the worklist.  */
>    for (i = 0; blocks.iterate (i, &bb); ++i)
> 
> 
> 	Jakub
> 

-- 
Richard Biener <rguenther@suse.de>
SUSE Linux GmbH, Maxfeldstrasse 5, 90409 Nuernberg, Germany;
GF: Felix Imendörffer, Mary Higgins, Sri Rasiah; HRB 21284 (AG Nürnberg)

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2019-06-11  8:18 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-06  8:02 [PATCH] Add warn_unused_result for malloc-like functions (PR tree-optimization/78902) Martin Liška
2019-06-06 15:17 ` Martin Sebor
2019-06-06 16:58   ` Jeff Law
2019-06-07  5:27   ` Martin Liška
2019-06-07 15:09     ` Martin Sebor
2019-06-07  0:02 ` Jeff Law
2019-06-07  6:27   ` Martin Liška
2019-06-07  7:07 ` [PATCH 1/2] Add alloc_size for libiberty memory allocation functions Martin Liška
2019-06-07  7:08   ` [PATCH 2/2] Add warn_unused_result attribute for memory-related, functions in libiberty Martin Liška
2019-06-07 17:13     ` Jeff Law
2019-06-07 17:13   ` [PATCH 1/2] Add alloc_size for libiberty memory allocation functions Jeff Law
2019-06-10 22:52   ` [PATCH] Fix i686-linux bootstrap (was Re: [PATCH 1/2] Add alloc_size for libiberty memory allocation functions.) Jakub Jelinek
2019-06-11  8:18     ` Richard Biener

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