Tests for minimal signal handler functionality in MINSIGSTKSZ space.
[glibc.git] / stdlib / tst-swapcontext1.c
blobf56eb542c206dce8bbbc2333594d910a14410d84
1 /* Check multiple makecontext calls.
2 Copyright (C) 2018-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <ucontext.h>
23 static ucontext_t uctx_main, uctx_func1, uctx_func2;
24 const char *str1 = "\e[31mswapcontext(&uctx_func1, &uctx_main)\e[0m";
25 const char *str2 = "\e[34mswapcontext(&uctx_func2, &uctx_main)\e[0m";
26 const char *fmt1 = "\e[31m";
27 const char *fmt2 = "\e[34m";
29 #define handle_error(msg) \
30 do { perror(msg); exit(EXIT_FAILURE); } while (0)
32 __attribute__((noinline, noclone))
33 static void
34 func4(ucontext_t *uocp, ucontext_t *ucp, const char *str, const char *fmt)
36 printf(" %sfunc4: %s\e[0m\n", fmt, str);
37 if (swapcontext(uocp, ucp) == -1)
38 handle_error("swapcontext");
39 printf(" %sfunc4: returning\e[0m\n", fmt);
42 __attribute__((noinline, noclone))
43 static void
44 func3(ucontext_t *uocp, ucontext_t *ucp, const char *str, const char *fmt)
46 printf(" %sfunc3: func4(uocp, ucp, str)\e[0m\n", fmt);
47 func4(uocp, ucp, str, fmt);
48 printf(" %sfunc3: returning\e[0m\n", fmt);
51 __attribute__((noinline, noclone))
52 static void
53 func1(void)
55 while ( 1 )
57 printf(" \e[31mfunc1: func3(&uctx_func1, &uctx_main, str1)\e[0m\n");
58 func3( &uctx_func1, &uctx_main, str1, fmt1);
62 __attribute__((noinline, noclone))
63 static void
64 func2(void)
66 while ( 1 )
68 printf(" \e[34mfunc2: func3(&uctx_func2, &uctx_main, str2)\e[0m\n");
69 func3(&uctx_func2, &uctx_main, str2, fmt2);
73 static int
74 do_test (void)
76 char func1_stack[16384];
77 char func2_stack[16384];
78 int i;
80 if (getcontext(&uctx_func1) == -1)
81 handle_error("getcontext");
82 uctx_func1.uc_stack.ss_sp = func1_stack;
83 uctx_func1.uc_stack.ss_size = sizeof(func1_stack);
84 uctx_func1.uc_link = &uctx_main;
85 makecontext(&uctx_func1, func1, 0);
87 if (getcontext(&uctx_func2) == -1)
88 handle_error("getcontext");
89 uctx_func2.uc_stack.ss_sp = func2_stack;
90 uctx_func2.uc_stack.ss_size = sizeof(func2_stack);
91 uctx_func2.uc_link = &uctx_func1;
92 makecontext(&uctx_func2, func2, 0);
94 for ( i = 0; i < 4; i++ )
96 if (swapcontext(&uctx_main, &uctx_func1) == -1)
97 handle_error("swapcontext");
98 printf(" \e[35mmain: swapcontext(&uctx_main, &uctx_func2)\n\e[0m");
99 if (swapcontext(&uctx_main, &uctx_func2) == -1)
100 handle_error("swapcontext");
101 printf(" \e[35mmain: swapcontext(&uctx_main, &uctx_func1)\n\e[0m");
104 printf("main: exiting\n");
105 exit(EXIT_SUCCESS);
108 #include <support/test-driver.c>