xref: /freebsd/lib/libc/tests/gen/makecontext_test.c (revision e796cc77c586c2955b2f3940dbf4991b31e8d289)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018 John H. Baldwin <jhb@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <atf-c.h>
33 #include <ucontext.h>
34 
35 static char uc_stack[16 * 1024];
36 
37 static void
38 check_1(int arg1)
39 {
40 
41 	ATF_REQUIRE_EQ(arg1, 1);
42 }
43 
44 ATF_TC_WITHOUT_HEAD(makecontext_arg1);
45 ATF_TC_BODY(makecontext_arg1, tc)
46 {
47 	ucontext_t ctx[2];
48 
49 	ATF_REQUIRE_EQ(getcontext(&ctx[1]), 0);
50 	ctx[1].uc_stack.ss_sp = uc_stack;
51 	ctx[1].uc_stack.ss_size = sizeof(uc_stack);
52 	ctx[1].uc_link = &ctx[0];
53 	makecontext(&ctx[1], (void (*)(void))check_1, 1, 1);
54 
55 	ATF_REQUIRE_EQ(swapcontext(&ctx[0], &ctx[1]), 0);
56 }
57 
58 static void
59 check_2(int arg1, int arg2)
60 {
61 
62 	ATF_REQUIRE_EQ(arg1, 1);
63 	ATF_REQUIRE_EQ(arg2, 2);
64 }
65 
66 ATF_TC_WITHOUT_HEAD(makecontext_arg2);
67 ATF_TC_BODY(makecontext_arg2, tc)
68 {
69 	ucontext_t ctx[2];
70 
71 	ATF_REQUIRE_EQ(getcontext(&ctx[1]), 0);
72 	ctx[1].uc_stack.ss_sp = uc_stack;
73 	ctx[1].uc_stack.ss_size = sizeof(uc_stack);
74 	ctx[1].uc_link = &ctx[0];
75 	makecontext(&ctx[1], (void (*)(void))check_2, 2, 1, 2);
76 
77 	ATF_REQUIRE_EQ(swapcontext(&ctx[0], &ctx[1]), 0);
78 }
79 
80 static void
81 check_3(int arg1, int arg2, int arg3)
82 {
83 
84 	ATF_REQUIRE_EQ(arg1, 1);
85 	ATF_REQUIRE_EQ(arg2, 2);
86 	ATF_REQUIRE_EQ(arg3, 3);
87 }
88 
89 ATF_TC_WITHOUT_HEAD(makecontext_arg3);
90 ATF_TC_BODY(makecontext_arg3, tc)
91 {
92 	ucontext_t ctx[2];
93 
94 	ATF_REQUIRE_EQ(getcontext(&ctx[1]), 0);
95 	ctx[1].uc_stack.ss_sp = uc_stack;
96 	ctx[1].uc_stack.ss_size = sizeof(uc_stack);
97 	ctx[1].uc_link = &ctx[0];
98 	makecontext(&ctx[1], (void (*)(void))check_3, 3, 1, 2, 3);
99 
100 	ATF_REQUIRE_EQ(swapcontext(&ctx[0], &ctx[1]), 0);
101 }
102 
103 static void
104 check_4(int arg1, int arg2, int arg3, int arg4)
105 {
106 
107 	ATF_REQUIRE_EQ(arg1, 1);
108 	ATF_REQUIRE_EQ(arg2, 2);
109 	ATF_REQUIRE_EQ(arg3, 3);
110 	ATF_REQUIRE_EQ(arg4, 4);
111 }
112 
113 ATF_TC_WITHOUT_HEAD(makecontext_arg4);
114 ATF_TC_BODY(makecontext_arg4, tc)
115 {
116 	ucontext_t ctx[2];
117 
118 	ATF_REQUIRE_EQ(getcontext(&ctx[1]), 0);
119 	ctx[1].uc_stack.ss_sp = uc_stack;
120 	ctx[1].uc_stack.ss_size = sizeof(uc_stack);
121 	ctx[1].uc_link = &ctx[0];
122 	makecontext(&ctx[1], (void (*)(void))check_4, 4, 1, 2, 3, 4);
123 
124 	ATF_REQUIRE_EQ(swapcontext(&ctx[0], &ctx[1]), 0);
125 }
126 
127 static void
128 check_5(int arg1, int arg2, int arg3, int arg4, int arg5)
129 {
130 
131 	ATF_REQUIRE_EQ(arg1, 1);
132 	ATF_REQUIRE_EQ(arg2, 2);
133 	ATF_REQUIRE_EQ(arg3, 3);
134 	ATF_REQUIRE_EQ(arg4, 4);
135 	ATF_REQUIRE_EQ(arg5, 5);
136 }
137 
138 ATF_TC_WITHOUT_HEAD(makecontext_arg5);
139 ATF_TC_BODY(makecontext_arg5, tc)
140 {
141 	ucontext_t ctx[2];
142 
143 	ATF_REQUIRE_EQ(getcontext(&ctx[1]), 0);
144 	ctx[1].uc_stack.ss_sp = uc_stack;
145 	ctx[1].uc_stack.ss_size = sizeof(uc_stack);
146 	ctx[1].uc_link = &ctx[0];
147 	makecontext(&ctx[1], (void (*)(void))check_5, 5, 1, 2, 3, 4, 5);
148 
149 	ATF_REQUIRE_EQ(swapcontext(&ctx[0], &ctx[1]), 0);
150 }
151 
152 static void
153 check_6(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6)
154 {
155 
156 	ATF_REQUIRE_EQ(arg1, 1);
157 	ATF_REQUIRE_EQ(arg2, 2);
158 	ATF_REQUIRE_EQ(arg3, 3);
159 	ATF_REQUIRE_EQ(arg4, 4);
160 	ATF_REQUIRE_EQ(arg5, 5);
161 	ATF_REQUIRE_EQ(arg6, 6);
162 }
163 
164 ATF_TC_WITHOUT_HEAD(makecontext_arg6);
165 ATF_TC_BODY(makecontext_arg6, tc)
166 {
167 	ucontext_t ctx[2];
168 
169 	ATF_REQUIRE_EQ(getcontext(&ctx[1]), 0);
170 	ctx[1].uc_stack.ss_sp = uc_stack;
171 	ctx[1].uc_stack.ss_size = sizeof(uc_stack);
172 	ctx[1].uc_link = &ctx[0];
173 	makecontext(&ctx[1], (void (*)(void))check_6, 6, 1, 2, 3, 4, 5, 6);
174 
175 	ATF_REQUIRE_EQ(swapcontext(&ctx[0], &ctx[1]), 0);
176 }
177 
178 ATF_TP_ADD_TCS(tp)
179 {
180 
181 	ATF_TP_ADD_TC(tp, makecontext_arg1);
182 	ATF_TP_ADD_TC(tp, makecontext_arg2);
183 	ATF_TP_ADD_TC(tp, makecontext_arg3);
184 	ATF_TP_ADD_TC(tp, makecontext_arg4);
185 	ATF_TP_ADD_TC(tp, makecontext_arg5);
186 	ATF_TP_ADD_TC(tp, makecontext_arg6);
187 
188 	return (atf_no_error());
189 }
190