Blame stdlib/tst-makecontext.c

Packit 6c4009
/* Copyright (C) 2006-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <ucontext.h>
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <unwind.h>
Packit 6c4009
#include <dlfcn.h>
Packit 6c4009
#include <gnu/lib-names.h>
Packit 6c4009
Packit 6c4009
ucontext_t ucp;
Packit 6c4009
char st1[16384];
Packit 6c4009
__thread int thr;
Packit 6c4009
Packit 6c4009
int somevar = -76;
Packit 6c4009
long othervar = -78L;
Packit 6c4009
Packit 6c4009
struct trace_arg
Packit 6c4009
{
Packit 6c4009
  int cnt, size;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static _Unwind_Reason_Code
Packit 6c4009
backtrace_helper (struct _Unwind_Context *ctx, void *a)
Packit 6c4009
{
Packit 6c4009
  struct trace_arg *arg = a;
Packit 6c4009
  if (++arg->cnt == arg->size)
Packit 6c4009
    return _URC_END_OF_STACK;
Packit 6c4009
  return _URC_NO_REASON;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
cf (int i)
Packit 6c4009
{
Packit 6c4009
  struct trace_arg arg = { .size = 100, .cnt = -1 };
Packit 6c4009
  void *handle;
Packit 6c4009
  _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
Packit 6c4009
Packit 6c4009
  if (i != othervar || thr != 94)
Packit 6c4009
    {
Packit 6c4009
      printf ("i %d thr %d\n", i, thr);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Test if callback function of _Unwind_Backtrace is not called infinitely
Packit 6c4009
     times. See Bug 18508 or gcc bug "Bug 66303 - runtime.Caller() returns
Packit 6c4009
     infinitely deep stack frames on s390x.".
Packit 6c4009
     The go runtime calls backtrace_full() in
Packit 6c4009
     <gcc-src>/libbacktrace/backtrace.c, which uses _Unwind_Backtrace().  */
Packit 6c4009
  handle = dlopen (LIBGCC_S_SO, RTLD_LAZY);
Packit 6c4009
  if (handle != NULL)
Packit 6c4009
    {
Packit 6c4009
      unwind_backtrace = dlsym (handle, "_Unwind_Backtrace");
Packit 6c4009
      if (unwind_backtrace != NULL)
Packit 6c4009
	{
Packit 6c4009
	  unwind_backtrace (backtrace_helper, &arg;;
Packit 6c4009
	  assert (arg.cnt != -1 && arg.cnt < 100);
Packit 6c4009
	}
Packit 6c4009
      dlclose (handle);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Since uc_link below has been set to NULL, setcontext is supposed to
Packit 6c4009
     terminate the process normally after this function returns.  */
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  if (getcontext (&ucp) != 0)
Packit 6c4009
    {
Packit 6c4009
      if (errno == ENOSYS)
Packit 6c4009
	{
Packit 6c4009
	  puts ("context handling not supported");
Packit 6c4009
	  return 0;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      puts ("getcontext failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  thr = 94;
Packit 6c4009
  ucp.uc_link = NULL;
Packit 6c4009
  ucp.uc_stack.ss_sp = st1;
Packit 6c4009
  ucp.uc_stack.ss_size = sizeof st1;
Packit 6c4009
  makecontext (&ucp, (void (*) (void)) cf, 1, somevar - 2);
Packit 6c4009
  if (setcontext (&ucp) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("setcontext failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  return 2;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"