fa3bfd
commit 890b7a4b33d482b5c768ab47d70758b80227e9bc
bca718
Author: Stefan Liebler <stli@linux.vnet.ibm.com>
fa3bfd
Date:   Tue Jul 7 16:11:14 2015 +0200
bca718
bca718
    S390: Fix "backtrace() returns infinitely deep stack frames with makecontext()" [BZ #18508].
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.  */