1/* Copyright (C) 2008-2022 Free Software Foundation, Inc.
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
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#include <errno.h>
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include <ucontext.h>
23#include <tst-stack-align.h>
24
25ucontext_t ucp, ucp2;
26char st1[262144] __attribute__((aligned (16)));
27
28void
29cf (int i, int j)
30{
31 if (i != 78 || j != 274)
32 {
33 printf (format: "i %d j %d\n", i, j);
34 exit (1);
35 }
36 else if (TEST_STACK_ALIGN ())
37 {
38 puts (s: "insufficiently aligned stack");
39 exit (2);
40 }
41}
42
43int
44do_test (void)
45{
46 for (size_t j = 32; j < 64; j += sizeof (long))
47 {
48 if (getcontext (ucp: &ucp) != 0)
49 {
50 if (errno == ENOSYS)
51 {
52 puts (s: "context handling not supported");
53 return 0;
54 }
55
56 puts (s: "getcontext failed");
57 return 1;
58 }
59 ucp.uc_link = &ucp2;
60 ucp.uc_stack.ss_sp = st1;
61 ucp.uc_stack.ss_size = sizeof (st1) - j;
62 memset (&st1[sizeof (st1) - j], 0x55, j);
63 makecontext (ucp: &ucp, func: (void (*) (void)) cf, argc: 2, 78, 274);
64 if (swapcontext (oucp: &ucp2, ucp: &ucp) != 0)
65 {
66 puts (s: "setcontext failed");
67 return 1;
68 }
69
70 for (size_t i = j; i > 0; i--)
71 if (st1[sizeof (st1) - j + i - 1] != 0x55)
72 { printf (format: "fail %zd %zd\n", i, j); break; }
73 }
74
75 return 0;
76}
77
78#define TEST_FUNCTION do_test ()
79#include "../test-skeleton.c"
80

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