51f0aa
commit 890b7a4b33d482b5c768ab47d70758b80227e9bc
ce426f
Author: Stefan Liebler <stli@linux.vnet.ibm.com>
51f0aa
Date:   Tue Jul 7 16:11:14 2015 +0200
ce426f
ce426f
    S390: Fix "backtrace() returns infinitely deep stack frames with makecontext()" [BZ #18508].
ce426f
ce426f
diff --git a/stdlib/Makefile b/stdlib/Makefile
ce426f
index 8f22c8d..c1e80d7 100644
ce426f
--- a/stdlib/Makefile
ce426f
+++ b/stdlib/Makefile
ce426f
@@ -154,3 +154,4 @@ $(objpfx)bug-getcontext: $(link-libm)
ce426f
 $(objpfx)tst-strtod-round: $(link-libm)
ce426f
 $(objpfx)tst-tininess: $(link-libm)
ce426f
 $(objpfx)tst-strtod-underflow: $(link-libm)
ce426f
+$(objpfx)tst-makecontext: $(libdl)
ce426f
diff --git a/stdlib/tst-makecontext.c b/stdlib/tst-makecontext.c
ce426f
index eb6e89b..1420857 100644
ce426f
--- a/stdlib/tst-makecontext.c
ce426f
+++ b/stdlib/tst-makecontext.c
ce426f
@@ -19,23 +19,62 @@
ce426f
 #include <stdlib.h>
ce426f
 #include <stdio.h>
ce426f
 #include <ucontext.h>
ce426f
+#include <assert.h>
ce426f
+#include <unwind.h>
ce426f
+#include <dlfcn.h>
ce426f
+#include <gnu/lib-names.h>
ce426f
 
ce426f
 ucontext_t ucp;
ce426f
-char st1[8192];
ce426f
+char st1[16384];
ce426f
 __thread int thr;
ce426f
 
ce426f
 int somevar = -76;
ce426f
 long othervar = -78L;
ce426f
 
ce426f
+struct trace_arg
ce426f
+{
ce426f
+  int cnt, size;
ce426f
+};
ce426f
+
ce426f
+static _Unwind_Reason_Code
ce426f
+backtrace_helper (struct _Unwind_Context *ctx, void *a)
ce426f
+{
ce426f
+  struct trace_arg *arg = a;
ce426f
+  if (++arg->cnt == arg->size)
ce426f
+    return _URC_END_OF_STACK;
ce426f
+  return _URC_NO_REASON;
ce426f
+}
ce426f
+
ce426f
 void
ce426f
 cf (int i)
ce426f
 {
ce426f
+  struct trace_arg arg = { .size = 100, .cnt = -1 };
ce426f
+  void *handle;
ce426f
+  _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
ce426f
+
ce426f
   if (i != othervar || thr != 94)
ce426f
     {
ce426f
       printf ("i %d thr %d\n", i, thr);
ce426f
       exit (1);
ce426f
     }
ce426f
 
ce426f
+  /* Test if callback function of _Unwind_Backtrace is not called infinitely
ce426f
+     times. See Bug 18508 or gcc bug "Bug 66303 - runtime.Caller() returns
ce426f
+     infinitely deep stack frames on s390x.".
ce426f
+     The go runtime calls backtrace_full() in
ce426f
+     <gcc-src>/libbacktrace/backtrace.c, which uses _Unwind_Backtrace().  */
ce426f
+  handle = dlopen (LIBGCC_S_SO, RTLD_LAZY);
ce426f
+  if (handle != NULL)
ce426f
+    {
ce426f
+      unwind_backtrace = dlsym (handle, "_Unwind_Backtrace");
ce426f
+      if (unwind_backtrace != NULL)
ce426f
+	{
ce426f
+	  unwind_backtrace (backtrace_helper, &arg;;
ce426f
+	  assert (arg.cnt != -1 && arg.cnt < 100);
ce426f
+	}
ce426f
+      dlclose (handle);
ce426f
+    }
ce426f
+
ce426f
   /* Since uc_link below has been set to NULL, setcontext is supposed to
ce426f
      terminate the process normally after this function returns.  */
ce426f
 }
ce426f
diff --git a/sysdeps/unix/sysv/linux/s390/s390-32/__makecontext_ret.S b/sysdeps/unix/sysv/linux/s390/s390-32/__makecontext_ret.S
ce426f
index ab172bb..365c2b0 100644
ce426f
--- a/sysdeps/unix/sysv/linux/s390/s390-32/__makecontext_ret.S
ce426f
+++ b/sysdeps/unix/sysv/linux/s390/s390-32/__makecontext_ret.S
ce426f
@@ -17,6 +17,14 @@
ce426f
 
ce426f
 #include <sysdep.h>
ce426f
 
ce426f
+/* We do not want .eh_frame info so that __makecontext_ret stops unwinding
ce426f
+   if backtrace was called within a context created by makecontext. (There
ce426f
+   is also no .eh_frame info for _start or thread_start.)  */
ce426f
+#undef cfi_startproc
ce426f
+#define cfi_startproc
ce426f
+#undef cfi_endproc
ce426f
+#define cfi_endproc
ce426f
+
ce426f
 ENTRY(__makecontext_ret)
ce426f
 	basr  %r14,%r7
ce426f
 	ltr   %r8,%r8			/* Check whether uc_link is 0.  */
ce426f
diff --git a/sysdeps/unix/sysv/linux/s390/s390-64/__makecontext_ret.S b/sysdeps/unix/sysv/linux/s390/s390-64/__makecontext_ret.S
ce426f
index cbd88e1..c4a43bd 100644
ce426f
--- a/sysdeps/unix/sysv/linux/s390/s390-64/__makecontext_ret.S
ce426f
+++ b/sysdeps/unix/sysv/linux/s390/s390-64/__makecontext_ret.S
ce426f
@@ -17,6 +17,14 @@
ce426f
 
ce426f
 #include <sysdep.h>
ce426f
 
ce426f
+/* We do not want .eh_frame info so that __makecontext_ret stops unwinding
ce426f
+   if backtrace was called within a context created by makecontext. (There
ce426f
+   is also no .eh_frame info for _start or thread_start.)  */
ce426f
+#undef cfi_startproc
ce426f
+#define cfi_startproc
ce426f
+#undef cfi_endproc
ce426f
+#define cfi_endproc
ce426f
+
ce426f
 ENTRY(__makecontext_ret)
ce426f
 	basr	%r14,%r7
ce426f
 	ltgr	%r8,%r8			/* Check whether uc_link is 0.  */