]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdlib/tst-makecontext.c
Add a test for longjmp from user context
[thirdparty/glibc.git] / stdlib / tst-makecontext.c
CommitLineData
6d7e8eda 1/* Copyright (C) 2006-2023 Free Software Foundation, Inc.
a12dcecc
UD
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
a12dcecc 17
d6220e9e 18#include <errno.h>
a12dcecc
UD
19#include <stdlib.h>
20#include <stdio.h>
21#include <ucontext.h>
890b7a4b
SL
22#include <assert.h>
23#include <unwind.h>
24#include <dlfcn.h>
25#include <gnu/lib-names.h>
a12dcecc
UD
26
27ucontext_t ucp;
890b7a4b 28char st1[16384];
a12dcecc
UD
29__thread int thr;
30
3b32d8a2 31int somevar = -76;
117df5d9 32long othervar = -78L;
3b32d8a2 33
890b7a4b
SL
34struct trace_arg
35{
36 int cnt, size;
37};
38
39static _Unwind_Reason_Code
40backtrace_helper (struct _Unwind_Context *ctx, void *a)
41{
42 struct trace_arg *arg = a;
43 if (++arg->cnt == arg->size)
44 return _URC_END_OF_STACK;
45 return _URC_NO_REASON;
46}
47
a12dcecc
UD
48void
49cf (int i)
50{
890b7a4b
SL
51 struct trace_arg arg = { .size = 100, .cnt = -1 };
52 void *handle;
53 _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
54
117df5d9 55 if (i != othervar || thr != 94)
a12dcecc
UD
56 {
57 printf ("i %d thr %d\n", i, thr);
58 exit (1);
59 }
dc97c227 60
890b7a4b
SL
61 /* Test if callback function of _Unwind_Backtrace is not called infinitely
62 times. See Bug 18508 or gcc bug "Bug 66303 - runtime.Caller() returns
63 infinitely deep stack frames on s390x.".
64 The go runtime calls backtrace_full() in
65 <gcc-src>/libbacktrace/backtrace.c, which uses _Unwind_Backtrace(). */
66 handle = dlopen (LIBGCC_S_SO, RTLD_LAZY);
67 if (handle != NULL)
68 {
69 unwind_backtrace = dlsym (handle, "_Unwind_Backtrace");
70 if (unwind_backtrace != NULL)
71 {
72 unwind_backtrace (backtrace_helper, &arg);
73 assert (arg.cnt != -1 && arg.cnt < 100);
74 }
75 dlclose (handle);
76 }
77
dc97c227
TS
78 /* Since uc_link below has been set to NULL, setcontext is supposed to
79 terminate the process normally after this function returns. */
a12dcecc
UD
80}
81
82int
d6220e9e 83do_test (void)
a12dcecc
UD
84{
85 if (getcontext (&ucp) != 0)
86 {
d6220e9e
UD
87 if (errno == ENOSYS)
88 {
89 puts ("context handling not supported");
90 return 0;
91 }
92
a12dcecc
UD
93 puts ("getcontext failed");
94 return 1;
95 }
96 thr = 94;
97 ucp.uc_link = NULL;
98 ucp.uc_stack.ss_sp = st1;
99 ucp.uc_stack.ss_size = sizeof st1;
3b32d8a2 100 makecontext (&ucp, (void (*) (void)) cf, 1, somevar - 2);
a12dcecc
UD
101 if (setcontext (&ucp) != 0)
102 {
103 puts ("setcontext failed");
104 return 1;
105 }
106 return 2;
107}
d6220e9e
UD
108
109#define TEST_FUNCTION do_test ()
110#include "../test-skeleton.c"