[BZ,#18508] S390: Fix "backtrace() returns infinitely deep stack frames with makecontext()".

Message ID mleq97$5v2$1@ger.gmane.org
State Superseded
Headers

Commit Message

Stefan Liebler June 12, 2015, 2:27 p.m. UTC
  On 06/12/2015 11:14 AM, Florian Weimer wrote:
> On 06/09/2015 05:42 PM, Stefan Liebler wrote:
>
>> On s390/s390x backtrace(buffer, size) returns the series of called
>> functions until "makecontext_ret" and additional entries (up to "size")
>> with "makecontext_ret".
>> GDB-backtrace is also warning: "Backtrace stopped: previous frame
>> identical to this frame (corrupt stack?)"
>
> Is it possible to extract a test case from the Go program, by calling
> makecontext and backtrace in the same way to Go run time does?
>
> I think this would be a useful addition to the glibc test suite.
>
Yes, i can make a testcase like stdlib/tst-makecontext.c or extend it - 
see below.

Can i call _Unwind_Backtrace directly on all architectures? Or does it 
exist on all architectures?
The diff below uses dlopen to load libgcc, but e.g. s390 needs 
libgcc_s.so.1 and others like m68k needs libgcc_s.so.2.
Is there a way to load the correct libgcc_s.so on all architectures?

As an alternative i could use the glibc backtrace() method. But at least 
for the x86_64 version and all others who simply includes it, this 
testcase would not fail, because the backtrace_helper function has a 
check, if the address has changed in two consecutively calls.
The go runtime hasn't such a check and would fail like reported by the 
mentioned go-bug.

Any ideas?

Bye Stefan

       terminate the process normally after this function returns.  */
  }
  

Comments

Andreas Schwab June 12, 2015, 3:40 p.m. UTC | #1
Stefan Liebler <stli@linux.vnet.ibm.com> writes:

> Is there a way to load the correct libgcc_s.so on all architectures?

#include <gnu/lib-names.h>
dlopen(LIBGCC_S_SO, ...)

Andreas.
  

Patch

diff --git a/stdlib/tst-makecontext.c b/stdlib/tst-makecontext.c
index 29a588e..33f9d9e 100644
--- a/stdlib/tst-makecontext.c
+++ b/stdlib/tst-makecontext.c
@@ -19,23 +19,62 @@ 
  #include <stdlib.h>
  #include <stdio.h>
  #include <ucontext.h>
+#include <assert.h>
+#include <unwind.h>
+#include <dlfcn.h>

  ucontext_t ucp;
-char st1[8192];
+char st1[16384];
  __thread int thr;

+struct trace_arg
+{
+  int cnt, size;
+};
+
  int somevar = -76;
  long othervar = -78L;

+static _Unwind_Reason_Code
+backtrace_helper (struct _Unwind_Context *ctx, void *a)
+{
+  struct trace_arg *arg = a;
+
+  if (++arg->cnt == arg->size)
+    return _URC_END_OF_STACK;
+  return _URC_NO_REASON;
+}
+
  void
  cf (int i)
  {
+  struct trace_arg arg = { .size = 100, .cnt = -1 };
+  void *handle;
+  _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
+
    if (i != othervar || thr != 94)
      {
        printf ("i %d thr %d\n", i, thr);
        exit (1);
      }

+  /* Test if callback function of _Unwind_Backtrace is not called 
infinitely
+     times. See Bug 18508 or gcc bug "Bug 66303 - runtime.Caller() returns
+     infinitely deep stack frames on s390x.".
+     The go runtime calls backtrace_full() in
+     <gcc-src>/libbacktrace/backtrace.c, which uses 
_Unwind_Backtrace().  */
+  handle = __libc_dlopen ("libgcc_s.so.1");
+  if (handle != NULL)
+    {
+      unwind_backtrace = __libc_dlsym (handle, "_Unwind_Backtrace");
+      if (unwind_backtrace != NULL)
+       {
+         unwind_backtrace (backtrace_helper, &arg);
+         assert (arg.cnt != -1 && arg.cnt < 100);
+       }
+      __libc_dlclose (handle);
+    }
+
    /* Since uc_link below has been set to NULL, setcontext is supposed to