Blame SOURCES/gcc8-rh1711346.patch

f56e54
2019-05-29  Jakub Jelinek  <jakub@redhat.com>
f56e54
f56e54
	PR fortran/90329
f56e54
	* lto-streamer.h (LTO_minor_version): Bump to 2.
f56e54
f56e54
	Backported from mainline
f56e54
	2019-05-16  Jakub Jelinek  <jakub@redhat.com>
f56e54
f56e54
	PR fortran/90329
f56e54
	* tree-core.h (struct tree_decl_common): Document
f56e54
	decl_nonshareable_flag for PARM_DECLs.
f56e54
	* tree.h (DECL_HIDDEN_STRING_LENGTH): Define.
f56e54
	* calls.c (expand_call): Don't try tail call if caller
f56e54
	has any DECL_HIDDEN_STRING_LENGTH PARM_DECLs that are or might be
f56e54
	passed on the stack and callee needs to pass any arguments on the
f56e54
	stack.
f56e54
	* tree-streamer-in.c (unpack_ts_decl_common_value_fields): Use
f56e54
	else if instead of series of mutually exclusive ifs.  Handle
f56e54
	DECL_HIDDEN_STRING_LENGTH for PARM_DECLs.
f56e54
	* tree-streamer-out.c (pack_ts_decl_common_value_fields): Likewise.
f56e54
f56e54
	* lang.opt (fbroken-callers): Remove.
f56e54
	(ftail-call-workaround, ftail-call-workaround=): New options.
f56e54
	* gfortran.h (struct gfc_namespace): Add implicit_interface_calls.
f56e54
	* interface.c (gfc_procedure_use): Set implicit_interface_calls
f56e54
	for calls to implicit interface procedures.
f56e54
	* trans-decl.c (create_function_arglist): Use flag_tail_call_workaround
f56e54
	instead of flag_broken_callers.  If it is not 2, also require
f56e54
	sym->ns->implicit_interface_calls.
f56e54
	* invoke.texi (fbroken-callers): Remove documentation.
f56e54
	(ftail-call-workaround, ftail-call-workaround=): Document.
f56e54
f56e54
	2019-05-19  Thomas Koenig  <tkoenig@gcc.gnu.org>
f56e54
f56e54
	PR fortran/90329
f56e54
	* invoke.texi: Document -fbroken-callers.
f56e54
	* lang.opt: Add -fbroken-callers.
f56e54
	* trans-decl.c (create_function_arglist): Only set
f56e54
	DECL_HIDDEN_STRING_LENGTH if flag_broken_callers is set.
f56e54
f56e54
	2019-05-16  Jakub Jelinek  <jakub@redhat.com>
f56e54
f56e54
	PR fortran/90329
f56e54
	* trans-decl.c (create_function_arglist): Set
f56e54
	DECL_HIDDEN_STRING_LENGTH on hidden string length PARM_DECLs if
f56e54
	len is constant.
f56e54
f56e54
--- gcc/calls.c
f56e54
+++ gcc/calls.c
f56e54
@@ -3754,6 +3754,28 @@ expand_call (tree exp, rtx target, int ignore)
f56e54
       || dbg_cnt (tail_call) == false)
f56e54
     try_tail_call = 0;
f56e54
 
f56e54
+  /* Workaround buggy C/C++ wrappers around Fortran routines with
f56e54
+     character(len=constant) arguments if the hidden string length arguments
f56e54
+     are passed on the stack; if the callers forget to pass those arguments,
f56e54
+     attempting to tail call in such routines leads to stack corruption.
f56e54
+     Avoid tail calls in functions where at least one such hidden string
f56e54
+     length argument is passed (partially or fully) on the stack in the
f56e54
+     caller and the callee needs to pass any arguments on the stack.
f56e54
+     See PR90329.  */
f56e54
+  if (try_tail_call && maybe_ne (args_size.constant, 0))
f56e54
+    for (tree arg = DECL_ARGUMENTS (current_function_decl);
f56e54
+	 arg; arg = DECL_CHAIN (arg))
f56e54
+      if (DECL_HIDDEN_STRING_LENGTH (arg) && DECL_INCOMING_RTL (arg))
f56e54
+	{
f56e54
+	  subrtx_iterator::array_type array;
f56e54
+	  FOR_EACH_SUBRTX (iter, array, DECL_INCOMING_RTL (arg), NONCONST)
f56e54
+	    if (MEM_P (*iter))
f56e54
+	      {
f56e54
+		try_tail_call = 0;
f56e54
+		break;
f56e54
+	      }
f56e54
+	}
f56e54
+
f56e54
   /* If the user has marked the function as requiring tail-call
f56e54
      optimization, attempt it.  */
f56e54
   if (must_tail_call)
f56e54
--- gcc/fortran/gfortran.h
f56e54
+++ gcc/fortran/gfortran.h
f56e54
@@ -1857,6 +1857,9 @@ typedef struct gfc_namespace
f56e54
 
f56e54
   /* Set to 1 for !$ACC ROUTINE namespaces.  */
f56e54
   unsigned oacc_routine:1;
f56e54
+
f56e54
+  /* Set to 1 if there are any calls to procedures with implicit interface.  */
f56e54
+  unsigned implicit_interface_calls:1;
f56e54
 }
f56e54
 gfc_namespace;
f56e54
 
f56e54
--- gcc/fortran/interface.c
f56e54
+++ gcc/fortran/interface.c
f56e54
@@ -3657,6 +3657,7 @@ gfc_procedure_use (gfc_symbol *sym, gfc_actual_arglist **ap, locus *where)
f56e54
 	gfc_warning (OPT_Wimplicit_procedure,
f56e54
 		     "Procedure %qs called at %L is not explicitly declared",
f56e54
 		     sym->name, where);
f56e54
+      gfc_find_proc_namespace (sym->ns)->implicit_interface_calls = 1;
f56e54
     }
f56e54
 
f56e54
   if (sym->attr.if_source == IFSRC_UNKNOWN)
f56e54
--- gcc/fortran/invoke.texi
f56e54
+++ gcc/fortran/invoke.texi
f56e54
@@ -181,7 +181,8 @@ and warnings}.
f56e54
 @item Code Generation Options
f56e54
 @xref{Code Gen Options,,Options for code generation conventions}.
f56e54
 @gccoptlist{-faggressive-function-elimination -fblas-matmul-limit=@var{n} @gol
f56e54
--fbounds-check -fcheck-array-temporaries @gol
f56e54
+-fbounds-check -ftail-call-workaround -ftail-call-workaround=@var{n} @gol
f56e54
+-fcheck-array-temporaries @gol
f56e54
 -fcheck=@var{<all|array-temps|bounds|do|mem|pointer|recursion>} @gol
f56e54
 -fcoarray=@var{<none|single|lib>} -fexternal-blas -ff2c
f56e54
 -ffrontend-loop-interchange @gol
f56e54
@@ -1580,6 +1581,39 @@ warnings for generated array temporaries.
f56e54
 @c Note: This option is also referred in gcc's manpage
f56e54
 Deprecated alias for @option{-fcheck=bounds}.
f56e54
 
f56e54
+@item -ftail-call-workaround
f56e54
+@itemx -ftail-call-workaround=@var{n}
f56e54
+@opindex @code{tail-call-workaround}
f56e54
+Some C interfaces to Fortran codes violate the gfortran ABI by
f56e54
+omitting the hidden character length arguments as described in
f56e54
+@xref{Argument passing conventions}.  This can lead to crashes
f56e54
+because pushing arguments for tail calls can overflow the stack.
f56e54
+
f56e54
+To provide a workaround for existing binary packages, this option
f56e54
+disables tail call optimization for gfortran procedures with character
f56e54
+arguments.  With @option{-ftail-call-workaround=2} tail call optimization
f56e54
+is disabled in all gfortran procedures with character arguments,
f56e54
+with @option{-ftail-call-workaround=1} or equivalent
f56e54
+@option{-ftail-call-workaround} only in gfortran procedures with character
f56e54
+arguments that call implicitly prototyped procedures.
f56e54
+
f56e54
+Using this option can lead to problems including crashes due to
f56e54
+insufficient stack space.
f56e54
+
f56e54
+It is @emph{very strongly} recommended to fix the code in question.
f56e54
+The @option{-fc-prototypes-external} option can be used to generate
f56e54
+prototypes which conform to gfortran's ABI, for inclusion in the
f56e54
+source code.
f56e54
+
f56e54
+Support for this option will likely be withdrawn in a future release
f56e54
+of gfortran.
f56e54
+
f56e54
+The negative form, @option{-fno-tail-call-workaround} or equivalent
f56e54
+@option{-ftail-call-workaround=0}, can be used to disable this option.
f56e54
+
f56e54
+Default is currently @option{-ftail-call-workaround}, this will change
f56e54
+in future releases.
f56e54
+
f56e54
 @item -fcheck-array-temporaries
f56e54
 @opindex @code{fcheck-array-temporaries}
f56e54
 Deprecated alias for @option{-fcheck=array-temps}.
f56e54
--- gcc/fortran/lang.opt
f56e54
+++ gcc/fortran/lang.opt
f56e54
@@ -742,6 +742,13 @@ fsign-zero
f56e54
 Fortran Var(flag_sign_zero) Init(1)
f56e54
 Apply negative sign to zero values.
f56e54
 
f56e54
+ftail-call-workaround
f56e54
+Fortran Alias(ftail-call-workaround=,1,0)
f56e54
+
f56e54
+ftail-call-workaround=
f56e54
+Fortran RejectNegative Joined UInteger IntegerRange(0, 2) Var(flag_tail_call_workaround) Init(1)
f56e54
+Disallow tail call optimization when a calling routine may have omitted character lengths.
f56e54
+
f56e54
 funderscoring
f56e54
 Fortran Var(flag_underscoring) Init(1)
f56e54
 Append underscores to externally visible names.
f56e54
--- gcc/fortran/trans-decl.c
f56e54
+++ gcc/fortran/trans-decl.c
f56e54
@@ -2513,6 +2513,17 @@ create_function_arglist (gfc_symbol * sym)
f56e54
 	  TREE_READONLY (length) = 1;
f56e54
 	  gfc_finish_decl (length);
f56e54
 
f56e54
+	  /* Marking the length DECL_HIDDEN_STRING_LENGTH will lead
f56e54
+	     to tail calls being disabled.  Only do that if we
f56e54
+	     potentially have broken callers.  */
f56e54
+	  if (flag_tail_call_workaround
f56e54
+	      && f->sym->ts.u.cl
f56e54
+	      && f->sym->ts.u.cl->length
f56e54
+	      && f->sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
f56e54
+	      && (flag_tail_call_workaround == 2
f56e54
+		  || f->sym->ns->implicit_interface_calls))
f56e54
+	    DECL_HIDDEN_STRING_LENGTH (length) = 1;
f56e54
+
f56e54
 	  /* Remember the passed value.  */
f56e54
           if (!f->sym->ts.u.cl ||  f->sym->ts.u.cl->passed_length)
f56e54
             {
f56e54
--- gcc/lto-streamer.h
f56e54
+++ gcc/lto-streamer.h
f56e54
@@ -121,7 +121,7 @@ along with GCC; see the file COPYING3.  If not see
f56e54
      form followed by the data for the string.  */
f56e54
 
f56e54
 #define LTO_major_version 7
f56e54
-#define LTO_minor_version 1
f56e54
+#define LTO_minor_version 2
f56e54
 
f56e54
 typedef unsigned char	lto_decl_flags_t;
f56e54
 
f56e54
--- gcc/tree-core.h
f56e54
+++ gcc/tree-core.h
f56e54
@@ -1644,6 +1644,7 @@ struct GTY(()) tree_decl_common {
f56e54
   /* In a VAR_DECL and PARM_DECL, this is DECL_READ_P.  */
f56e54
   unsigned decl_read_flag : 1;
f56e54
   /* In a VAR_DECL or RESULT_DECL, this is DECL_NONSHAREABLE.  */
f56e54
+  /* In a PARM_DECL, this is DECL_HIDDEN_STRING_LENGTH.  */
f56e54
   unsigned decl_nonshareable_flag : 1;
f56e54
 
f56e54
   /* DECL_OFFSET_ALIGN, used only for FIELD_DECLs.  */
f56e54
--- gcc/tree-streamer-in.c
f56e54
+++ gcc/tree-streamer-in.c
f56e54
@@ -252,7 +252,7 @@ unpack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
f56e54
       LABEL_DECL_UID (expr) = -1;
f56e54
     }
f56e54
 
f56e54
-  if (TREE_CODE (expr) == FIELD_DECL)
f56e54
+  else if (TREE_CODE (expr) == FIELD_DECL)
f56e54
     {
f56e54
       DECL_PACKED (expr) = (unsigned) bp_unpack_value (bp, 1);
f56e54
       DECL_NONADDRESSABLE_P (expr) = (unsigned) bp_unpack_value (bp, 1);
f56e54
@@ -260,12 +260,15 @@ unpack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
f56e54
       expr->decl_common.off_align = bp_unpack_value (bp, 8);
f56e54
     }
f56e54
 
f56e54
-  if (VAR_P (expr))
f56e54
+  else if (VAR_P (expr))
f56e54
     {
f56e54
       DECL_HAS_DEBUG_EXPR_P (expr) = (unsigned) bp_unpack_value (bp, 1);
f56e54
       DECL_NONLOCAL_FRAME (expr) = (unsigned) bp_unpack_value (bp, 1);
f56e54
     }
f56e54
 
f56e54
+  else if (TREE_CODE (expr) == PARM_DECL)
f56e54
+    DECL_HIDDEN_STRING_LENGTH (expr) = (unsigned) bp_unpack_value (bp, 1);
f56e54
+
f56e54
   if (TREE_CODE (expr) == RESULT_DECL
f56e54
       || TREE_CODE (expr) == PARM_DECL
f56e54
       || VAR_P (expr))
f56e54
--- gcc/tree-streamer-out.c
f56e54
+++ gcc/tree-streamer-out.c
f56e54
@@ -212,7 +212,7 @@ pack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
f56e54
       bp_pack_var_len_unsigned (bp, EH_LANDING_PAD_NR (expr));
f56e54
     }
f56e54
 
f56e54
-  if (TREE_CODE (expr) == FIELD_DECL)
f56e54
+  else if (TREE_CODE (expr) == FIELD_DECL)
f56e54
     {
f56e54
       bp_pack_value (bp, DECL_PACKED (expr), 1);
f56e54
       bp_pack_value (bp, DECL_NONADDRESSABLE_P (expr), 1);
f56e54
@@ -220,12 +220,15 @@ pack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
f56e54
       bp_pack_value (bp, expr->decl_common.off_align, 8);
f56e54
     }
f56e54
 
f56e54
-  if (VAR_P (expr))
f56e54
+  else if (VAR_P (expr))
f56e54
     {
f56e54
       bp_pack_value (bp, DECL_HAS_DEBUG_EXPR_P (expr), 1);
f56e54
       bp_pack_value (bp, DECL_NONLOCAL_FRAME (expr), 1);
f56e54
     }
f56e54
 
f56e54
+  else if (TREE_CODE (expr) == PARM_DECL)
f56e54
+    bp_pack_value (bp, DECL_HIDDEN_STRING_LENGTH (expr), 1);
f56e54
+
f56e54
   if (TREE_CODE (expr) == RESULT_DECL
f56e54
       || TREE_CODE (expr) == PARM_DECL
f56e54
       || VAR_P (expr))
f56e54
--- gcc/tree.h
f56e54
+++ gcc/tree.h
f56e54
@@ -909,6 +909,11 @@ extern void omp_clause_range_check_failed (const_tree, const char *, int,
f56e54
   (TREE_CHECK2 (NODE, VAR_DECL, \
f56e54
 		RESULT_DECL)->decl_common.decl_nonshareable_flag)
f56e54
 
f56e54
+/* In a PARM_DECL, set for Fortran hidden string length arguments that some
f56e54
+   buggy callers don't pass to the callee.  */
f56e54
+#define DECL_HIDDEN_STRING_LENGTH(NODE) \
f56e54
+  (TREE_CHECK (NODE, PARM_DECL)->decl_common.decl_nonshareable_flag)
f56e54
+
f56e54
 /* In a CALL_EXPR, means that the call is the jump from a thunk to the
f56e54
    thunked-to function.  */
f56e54
 #define CALL_FROM_THUNK_P(NODE) (CALL_EXPR_CHECK (NODE)->base.protected_flag)