bca718
commit 7cdd140cbccc8eb62a20d12a03bed83765cbc066
bca718
Author: Stefan Liebler <stli@linux.vnet.ibm.com>
bca718
Date:   Fri Jul 31 13:42:52 2015 +0200
bca718
bca718
    S390: Fix "backtrace() returns infinitely deep stack frames with makecontext()" [BZ #18508].
bca718
    
bca718
        On s390/s390x backtrace(buffer, size) returns the series of called functions until
bca718
        "makecontext_ret" and additional entries (up to "size") with "makecontext_ret".
bca718
        GDB-backtrace is also warning:
bca718
        "Backtrace stopped: previous frame identical to this frame (corrupt stack?)"
bca718
    
bca718
        To reproduce this scenario you have to setup a new context with makecontext()
bca718
        and activate it with setcontext(). See e.g. cf() function in testcase stdlib/tst-makecontext.c.
bca718
        Or see bug in libgo "Bug 66303 - runtime.Caller() returns infinitely deep stack frames
bca718
        on s390x " (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66303).
bca718
    
bca718
        This patch omits the cfi_startproc/cfi_endproc directives in ENTRY/END macro of
bca718
        __makecontext_ret. Thus no frame information is generated in .eh_frame and backtrace
bca718
        stops after __makecontext_ret. There is also no .eh_frame info for _start or
bca718
        thread_start functions.
bca718
    
bca718
        ChangeLog:
bca718
    
bca718
        	[BZ #18508]
bca718
        	* stdlib/Makefile ($(objpfx)tst-makecontext3):
bca718
        	Depend on $(libdl).
bca718
        	* stdlib/tst-makecontext.c (cf): Test if _Unwind_Backtrace
bca718
        	is not called infinitely times.
bca718
        	(backtrace_helper): New function.
bca718
        	(trace_arg): New struct.
bca718
        	(st1): Enlarge stack size.
bca718
        	* sysdeps/unix/sysv/linux/s390/s390-32/__makecontext_ret.S:
bca718
        	(__makecontext_ret): Omit cfi_startproc and cfi_endproc.
bca718
        	* sysdeps/unix/sysv/linux/s390/s390-64/__makecontext_ret.S:
bca718
        	Likewise.
bca718
bca718
diff --git a/stdlib/Makefile b/stdlib/Makefile
bca718
index 8f22c8d..c1e80d7 100644
bca718
--- a/stdlib/Makefile
bca718
+++ b/stdlib/Makefile
bca718
@@ -154,3 +154,4 @@ $(objpfx)bug-getcontext: $(link-libm)
bca718
 $(objpfx)tst-strtod-round: $(link-libm)
bca718
 $(objpfx)tst-tininess: $(link-libm)
bca718
 $(objpfx)tst-strtod-underflow: $(link-libm)
bca718
+$(objpfx)tst-makecontext: $(libdl)
bca718
diff --git a/stdlib/tst-makecontext.c b/stdlib/tst-makecontext.c
bca718
index eb6e89b..1420857 100644
bca718
--- a/stdlib/tst-makecontext.c
bca718
+++ b/stdlib/tst-makecontext.c
bca718
@@ -19,23 +19,62 @@
bca718
 #include <stdlib.h>
bca718
 #include <stdio.h>
bca718
 #include <ucontext.h>
bca718
+#include <assert.h>
bca718
+#include <unwind.h>
bca718
+#include <dlfcn.h>
bca718
+#include <gnu/lib-names.h>
bca718
 
bca718
 ucontext_t ucp;
bca718
-char st1[8192];
bca718
+char st1[16384];
bca718
 __thread int thr;
bca718
 
bca718
 int somevar = -76;
bca718
 long othervar = -78L;
bca718
 
bca718
+struct trace_arg
bca718
+{
bca718
+  int cnt, size;
bca718
+};
bca718
+
bca718
+static _Unwind_Reason_Code
bca718
+backtrace_helper (struct _Unwind_Context *ctx, void *a)
bca718
+{
bca718
+  struct trace_arg *arg = a;
bca718
+  if (++arg->cnt == arg->size)
bca718
+    return _URC_END_OF_STACK;
bca718
+  return _URC_NO_REASON;
bca718
+}
bca718
+
bca718
 void
bca718
 cf (int i)
bca718
 {
bca718
+  struct trace_arg arg = { .size = 100, .cnt = -1 };
bca718
+  void *handle;
bca718
+  _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
bca718
+
bca718
   if (i != othervar || thr != 94)
bca718
     {
bca718
       printf ("i %d thr %d\n", i, thr);
bca718
       exit (1);
bca718
     }
bca718
 
bca718
+  /* Test if callback function of _Unwind_Backtrace is not called infinitely
bca718
+     times. See Bug 18508 or gcc bug "Bug 66303 - runtime.Caller() returns
bca718
+     infinitely deep stack frames on s390x.".
bca718
+     The go runtime calls backtrace_full() in
bca718
+     <gcc-src>/libbacktrace/backtrace.c, which uses _Unwind_Backtrace().  */
bca718
+  handle = dlopen (LIBGCC_S_SO, RTLD_LAZY);
bca718
+  if (handle != NULL)
bca718
+    {
bca718
+      unwind_backtrace = dlsym (handle, "_Unwind_Backtrace");
bca718
+      if (unwind_backtrace != NULL)
bca718
+	{
bca718
+	  unwind_backtrace (backtrace_helper, &arg;;
bca718
+	  assert (arg.cnt != -1 && arg.cnt < 100);
bca718
+	}
bca718
+      dlclose (handle);
bca718
+    }
bca718
+
bca718
   /* Since uc_link below has been set to NULL, setcontext is supposed to
bca718
      terminate the process normally after this function returns.  */
bca718
 }
bca718
diff --git a/sysdeps/unix/sysv/linux/s390/s390-32/__makecontext_ret.S b/sysdeps/unix/sysv/linux/s390/s390-32/__makecontext_ret.S
bca718
index ab172bb..365c2b0 100644
bca718
--- a/sysdeps/unix/sysv/linux/s390/s390-32/__makecontext_ret.S
bca718
+++ b/sysdeps/unix/sysv/linux/s390/s390-32/__makecontext_ret.S
bca718
@@ -17,6 +17,14 @@
bca718
 
bca718
 #include <sysdep.h>
bca718
 
bca718
+/* We do not want .eh_frame info so that __makecontext_ret stops unwinding
bca718
+   if backtrace was called within a context created by makecontext. (There
bca718
+   is also no .eh_frame info for _start or thread_start.)  */
bca718
+#undef cfi_startproc
bca718
+#define cfi_startproc
bca718
+#undef cfi_endproc
bca718
+#define cfi_endproc
bca718
+
bca718
 ENTRY(__makecontext_ret)
bca718
 	basr  %r14,%r7
bca718
 	ltr   %r8,%r8			/* Check whether uc_link is 0.  */
bca718
diff --git a/sysdeps/unix/sysv/linux/s390/s390-64/__makecontext_ret.S b/sysdeps/unix/sysv/linux/s390/s390-64/__makecontext_ret.S
bca718
index cbd88e1..c4a43bd 100644
bca718
--- a/sysdeps/unix/sysv/linux/s390/s390-64/__makecontext_ret.S
bca718
+++ b/sysdeps/unix/sysv/linux/s390/s390-64/__makecontext_ret.S
bca718
@@ -17,6 +17,14 @@
bca718
 
bca718
 #include <sysdep.h>
bca718
 
bca718
+/* We do not want .eh_frame info so that __makecontext_ret stops unwinding
bca718
+   if backtrace was called within a context created by makecontext. (There
bca718
+   is also no .eh_frame info for _start or thread_start.)  */
bca718
+#undef cfi_startproc
bca718
+#define cfi_startproc
bca718
+#undef cfi_endproc
bca718
+#define cfi_endproc
bca718
+
bca718
 ENTRY(__makecontext_ret)
bca718
 	basr	%r14,%r7
bca718
 	ltgr	%r8,%r8			/* Check whether uc_link is 0.  */