1/* Check setjmp/longjmp between user contexts.
2 Copyright (C) 2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <setjmp.h>
22#include <ucontext.h>
23#include <stackinfo.h>
24
25static ucontext_t uctx_main, uctx_func1, uctx_func2;
26const char *str1 = "\e[31mswapcontext(&uctx_func1, &uctx_main)\e[0m";
27const char *str2 = "\e[34mswapcontext(&uctx_func2, &uctx_main)\e[0m";
28const char *fmt1 = "\e[31m";
29const char *fmt2 = "\e[34m";
30static jmp_buf jmpbuf;
31
32#define handle_error(msg) \
33 do { perror(msg); exit(EXIT_FAILURE); } while (0)
34
35__attribute__((noinline, noclone))
36static void
37func4(ucontext_t *uocp, ucontext_t *ucp, const char *str, const char *fmt)
38{
39 printf(format: " %sfunc4: %s\e[0m\n", fmt, str);
40 if (swapcontext(oucp: uocp, ucp: ucp) == -1)
41 handle_error("swapcontext");
42 printf(format: " %sfunc4: returning\e[0m\n", fmt);
43}
44
45__attribute__((noinline, noclone))
46static void
47func3(ucontext_t *uocp, ucontext_t *ucp, const char *str, const char *fmt)
48{
49 printf(format: " %sfunc3: func4(uocp, ucp, str)\e[0m\n", fmt);
50 func4(uocp, ucp, str, fmt);
51 printf(format: " %sfunc3: returning\e[0m\n", fmt);
52}
53
54__attribute__((noinline, noclone))
55static void
56func1(void)
57{
58 while ( 1 )
59 {
60 printf(format: " \e[31mfunc1: func3(&uctx_func1, &uctx_main, str1)\e[0m\n");
61 if (setjmp (jmpbuf) != 0)
62 {
63 printf(format: " %sfunc1: from longjmp\e[0m\n", fmt1);
64 exit (EXIT_SUCCESS);
65 }
66 func3( uocp: &uctx_func1, ucp: &uctx_main, str: str1, fmt: fmt1);
67 }
68}
69
70__attribute__((noinline, noclone))
71static void
72func2(void)
73{
74 while ( 1 )
75 {
76 printf(format: " \e[34mfunc2: func3(&uctx_func2, &uctx_main, str2)\e[0m\n");
77 func3(uocp: &uctx_func2, ucp: &uctx_main, str: str2, fmt: fmt2);
78 printf(format: " %sfunc2: calling longjmp\e[0m\n", fmt2);
79 longjmp (jmpbuf, 1);
80 }
81}
82
83static int
84do_test (void)
85{
86 /* ____longjmp_chk has */
87#if 0
88#ifdef _STACK_GROWS_DOWN
89#define called_from(this, saved) ((this) < (saved))
90#else
91#define called_from(this, saved) ((this) > (saved))
92#endif
93
94 /* If "env" is from a frame that called us, we're all set. */
95 if (called_from(this_frame, saved_frame))
96 __longjmp (env, val);
97#endif
98 /* Arrange stacks for uctx_func1 and uctx_func2 so that called_from
99 is true when setjmp is called from uctx_func1 and longjmp is called
100 from uctx_func2. */
101#ifdef _STACK_GROWS_DOWN
102# define UCTX_FUNC1_STACK 1
103# define UCTX_FUNC2_STACK 0
104#else
105# define UCTX_FUNC1_STACK 0
106# define UCTX_FUNC2_STACK 1
107#endif
108 char func_stack[2][16384];
109 int i;
110
111 if (getcontext(ucp: &uctx_func1) == -1)
112 handle_error("getcontext");
113 uctx_func1.uc_stack.ss_sp = func_stack[UCTX_FUNC1_STACK];
114 uctx_func1.uc_stack.ss_size = sizeof (func_stack[UCTX_FUNC1_STACK]);
115 uctx_func1.uc_link = &uctx_main;
116 makecontext(ucp: &uctx_func1, func: func1, argc: 0);
117
118 if (getcontext(ucp: &uctx_func2) == -1)
119 handle_error("getcontext");
120 uctx_func2.uc_stack.ss_sp = func_stack[UCTX_FUNC2_STACK];
121 uctx_func2.uc_stack.ss_size = sizeof (func_stack[UCTX_FUNC2_STACK]);
122 uctx_func2.uc_link = &uctx_func1;
123 makecontext(ucp: &uctx_func2, func: func2, argc: 0);
124
125 for ( i = 0; i < 2; i++ )
126 {
127 if (swapcontext(oucp: &uctx_main, ucp: &uctx_func1) == -1)
128 handle_error("swapcontext");
129 printf(format: " \e[35mmain: swapcontext(&uctx_main, &uctx_func2)\n\e[0m");
130 if (swapcontext(oucp: &uctx_main, ucp: &uctx_func2) == -1)
131 handle_error("swapcontext");
132 printf(format: " \e[35mmain: swapcontext(&uctx_main, &uctx_func1)\n\e[0m");
133 }
134
135 exit(EXIT_FAILURE);
136}
137
138#include <support/test-driver.c>
139

source code of glibc/stdlib/tst-swapcontext2.c