t_lwp_create.c revision 1.3 1 1.3 thorpej /* $NetBSD: t_lwp_create.c,v 1.3 2020/06/06 18:11:21 thorpej Exp $ */
2 1.1 martin
3 1.1 martin /*-
4 1.1 martin * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 1.1 martin * All rights reserved.
6 1.1 martin *
7 1.1 martin * Redistribution and use in source and binary forms, with or without
8 1.1 martin * modification, are permitted provided that the following conditions
9 1.1 martin * are met:
10 1.1 martin * 1. Redistributions of source code must retain the above copyright
11 1.1 martin * notice, this list of conditions and the following disclaimer.
12 1.1 martin * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 martin * notice, this list of conditions and the following disclaimer in the
14 1.1 martin * documentation and/or other materials provided with the distribution.
15 1.1 martin *
16 1.1 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 martin * POSSIBILITY OF SUCH DAMAGE.
27 1.1 martin */
28 1.1 martin
29 1.1 martin /*
30 1.1 martin * This code is partly based on code by Joel Sing <joel at sing.id.au>
31 1.1 martin */
32 1.1 martin
33 1.1 martin #include <atf-c.h>
34 1.1 martin #include <lwp.h>
35 1.1 martin #include <stdio.h>
36 1.1 martin #include <stdlib.h>
37 1.1 martin #include <ucontext.h>
38 1.1 martin #include <inttypes.h>
39 1.1 martin #include <errno.h>
40 1.1 martin
41 1.1 martin #ifdef __alpha__
42 1.1 martin #include <machine/alpha_cpu.h>
43 1.1 martin #endif
44 1.1 martin #ifdef __amd64__
45 1.1 martin #include <machine/vmparam.h>
46 1.1 martin #include <machine/psl.h>
47 1.1 martin #endif
48 1.1 martin #ifdef __hppa__
49 1.1 martin #include <machine/psl.h>
50 1.1 martin #endif
51 1.1 martin #ifdef __i386__
52 1.1 martin #include <machine/segments.h>
53 1.1 martin #include <machine/psl.h>
54 1.1 martin #endif
55 1.1 martin #if defined(__m68k__) || defined(__sh3__) || defined __vax__
56 1.1 martin #include <machine/psl.h>
57 1.1 martin #endif
58 1.1 martin
59 1.1 martin volatile lwpid_t the_lwp_id = 0;
60 1.1 martin
61 1.1 martin static void lwp_main_func(void* arg)
62 1.1 martin {
63 1.1 martin the_lwp_id = _lwp_self();
64 1.1 martin _lwp_exit();
65 1.1 martin }
66 1.1 martin
67 1.1 martin /*
68 1.2 martin * Hard to document - see usage examples below.
69 1.1 martin */
70 1.1 martin #define INVALID_UCONTEXT(ARCH,NAME,DESC) \
71 1.1 martin static void ARCH##_##NAME(ucontext_t *); \
72 1.1 martin ATF_TC(lwp_create_##ARCH##_fail_##NAME); \
73 1.1 martin ATF_TC_HEAD(lwp_create_##ARCH##_fail_##NAME, tc) \
74 1.1 martin { \
75 1.1 martin atf_tc_set_md_var(tc, "descr", "verify rejection of invalid ucontext " \
76 1.1 martin "on " #ARCH " due to " DESC); \
77 1.1 martin } \
78 1.1 martin \
79 1.1 martin ATF_TC_BODY(lwp_create_##ARCH##_fail_##NAME, tc) \
80 1.1 martin { \
81 1.1 martin ucontext_t uc; \
82 1.1 martin lwpid_t lid; \
83 1.1 martin int error; \
84 1.1 martin \
85 1.1 martin getcontext(&uc); \
86 1.1 martin uc.uc_flags = _UC_CPU; \
87 1.1 martin ARCH##_##NAME(&uc); \
88 1.1 martin \
89 1.1 martin error = _lwp_create(&uc, 0, &lid); \
90 1.1 martin ATF_REQUIRE(error != 0 && errno == EINVAL); \
91 1.1 martin } \
92 1.1 martin static void ARCH##_##NAME(ucontext_t *uc) \
93 1.1 martin {
94 1.1 martin
95 1.1 martin
96 1.1 martin ATF_TC(lwp_create_works);
97 1.1 martin ATF_TC_HEAD(lwp_create_works, tc)
98 1.1 martin {
99 1.1 martin atf_tc_set_md_var(tc, "descr", "Verify creation of a lwp and waiting"
100 1.1 martin " for it to finish");
101 1.1 martin }
102 1.1 martin
103 1.1 martin ATF_TC_BODY(lwp_create_works, tc)
104 1.1 martin {
105 1.1 martin ucontext_t uc;
106 1.1 martin lwpid_t lid;
107 1.1 martin int error;
108 1.1 martin void *stack;
109 1.1 martin static const size_t ssize = 16*1024;
110 1.1 martin
111 1.1 martin stack = malloc(ssize);
112 1.1 martin _lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
113 1.1 martin
114 1.1 martin error = _lwp_create(&uc, 0, &lid);
115 1.1 martin ATF_REQUIRE(error == 0);
116 1.1 martin
117 1.1 martin error = _lwp_wait(lid, NULL);
118 1.1 martin ATF_REQUIRE(error == 0);
119 1.1 martin ATF_REQUIRE(lid == the_lwp_id);
120 1.1 martin }
121 1.1 martin
122 1.3 thorpej ATF_TC(lwp_create_bad_lid_ptr);
123 1.3 thorpej ATF_TC_HEAD(lwp_create_bad_lid_ptr, tc)
124 1.3 thorpej {
125 1.3 thorpej atf_tc_set_md_var(tc, "descr",
126 1.3 thorpej "Verify _lwp_create() fails as expected with bad lid pointer");
127 1.3 thorpej }
128 1.3 thorpej
129 1.3 thorpej ATF_TC_BODY(lwp_create_bad_lid_ptr, tc)
130 1.3 thorpej {
131 1.3 thorpej ucontext_t uc;
132 1.3 thorpej int error;
133 1.3 thorpej int serrno;
134 1.3 thorpej void *stack;
135 1.3 thorpej static const size_t ssize = 16*1024;
136 1.3 thorpej
137 1.3 thorpej stack = malloc(ssize);
138 1.3 thorpej _lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
139 1.3 thorpej
140 1.3 thorpej error = _lwp_create(&uc, 0, NULL);
141 1.3 thorpej serrno = errno;
142 1.3 thorpej ATF_REQUIRE(error == -1);
143 1.3 thorpej ATF_REQUIRE(serrno == EFAULT);
144 1.3 thorpej }
145 1.3 thorpej
146 1.1 martin INVALID_UCONTEXT(generic, no_uc_cpu, "not setting cpu registers")
147 1.1 martin uc->uc_flags &= ~_UC_CPU;
148 1.1 martin }
149 1.1 martin
150 1.1 martin #ifdef __alpha__
151 1.1 martin INVALID_UCONTEXT(alpha, pslset, "trying to clear the USERMODE flag")
152 1.1 martin uc->uc_mcontext.__gregs[_REG_PS] &= ~ALPHA_PSL_USERMODE;
153 1.1 martin }
154 1.1 martin INVALID_UCONTEXT(alpha, pslclr, "trying to set a 'must be zero' flag")
155 1.1 martin uc->uc_mcontext.__gregs[_REG_PS] |= ALPHA_PSL_IPL_HIGH;
156 1.1 martin }
157 1.1 martin #endif
158 1.1 martin #ifdef __amd64__
159 1.1 martin INVALID_UCONTEXT(amd64, untouchable_rflags, "forbidden rflags changed")
160 1.1 martin uc->uc_mcontext.__gregs[_REG_RFLAGS] |= PSL_MBZ;
161 1.1 martin }
162 1.1 martin /*
163 1.1 martin * XXX: add invalid GS/DS selector tests
164 1.1 martin */
165 1.1 martin INVALID_UCONTEXT(amd64, pc_too_high,
166 1.1 martin "instruction pointer outside userland address space")
167 1.1 martin uc->uc_mcontext.__gregs[_REG_RIP] = VM_MAXUSER_ADDRESS;
168 1.1 martin }
169 1.1 martin #endif
170 1.1 martin #ifdef __arm__
171 1.1 martin INVALID_UCONTEXT(arm, invalid_mode, "psr or r15 set to non-user-mode")
172 1.1 martin uc->uc_mcontext.__gregs[_REG_PC] |= 0x1f /*PSR_SYS32_MODE*/;
173 1.1 martin uc->uc_mcontext.__gregs[_REG_CPSR] |= 0x03 /*R15_MODE_SVC*/;
174 1.1 martin }
175 1.1 martin #endif
176 1.1 martin #ifdef __hppa__
177 1.1 martin INVALID_UCONTEXT(hppa, invalid_1, "set illegal bits in psw")
178 1.1 martin uc->uc_mcontext.__gregs[_REG_PSW] |= PSW_MBZ;
179 1.1 martin }
180 1.1 martin INVALID_UCONTEXT(hppa, invalid_0, "clear illegal bits in psw")
181 1.1 martin uc->uc_mcontext.__gregs[_REG_PSW] &= ~PSW_MBS;
182 1.1 martin }
183 1.1 martin #endif
184 1.1 martin #ifdef __i386__
185 1.1 martin INVALID_UCONTEXT(i386, untouchable_eflags, "changing forbidden eflags")
186 1.1 martin uc->uc_mcontext.__gregs[_REG_EFL] |= PSL_IOPL;
187 1.1 martin }
188 1.1 martin INVALID_UCONTEXT(i386, priv_escalation, "modifying priviledge level")
189 1.1 martin uc->uc_mcontext.__gregs[_REG_CS] &= ~SEL_RPL;
190 1.1 martin }
191 1.1 martin #endif
192 1.1 martin #ifdef __m68k__
193 1.1 martin INVALID_UCONTEXT(m68k, invalid_ps_bits,
194 1.1 martin "setting forbidden bits in the ps register")
195 1.1 martin uc->uc_mcontext.__gregs[_REG_PS] |= (PSL_MBZ|PSL_IPL|PSL_S);
196 1.1 martin }
197 1.1 martin #endif
198 1.1 martin #ifdef __sh3__
199 1.1 martin INVALID_UCONTEXT(sh3, modify_userstatic,
200 1.1 martin "modifying illegal bits in the status register")
201 1.1 martin uc->uc_mcontext.__gregs[_REG_SR] |= PSL_MD;
202 1.1 martin }
203 1.1 martin #endif
204 1.1 martin #ifdef __sparc__
205 1.1 martin INVALID_UCONTEXT(sparc, pc_odd, "mis-aligned instruction pointer")
206 1.1 martin uc->uc_mcontext.__gregs[_REG_PC] = 0x100002;
207 1.1 martin }
208 1.1 martin INVALID_UCONTEXT(sparc, npc_odd, "mis-aligned next instruction pointer")
209 1.1 martin uc->uc_mcontext.__gregs[_REG_nPC] = 0x100002;
210 1.1 martin }
211 1.1 martin INVALID_UCONTEXT(sparc, pc_null, "NULL instruction pointer")
212 1.1 martin uc->uc_mcontext.__gregs[_REG_PC] = 0;
213 1.1 martin }
214 1.1 martin INVALID_UCONTEXT(sparc, npc_null, "NULL next instruction pointer")
215 1.1 martin uc->uc_mcontext.__gregs[_REG_nPC] = 0;
216 1.1 martin }
217 1.1 martin #endif
218 1.1 martin #ifdef __vax__
219 1.1 martin INVALID_UCONTEXT(vax, psl_0, "clearing forbidden bits in psl")
220 1.1 martin uc->uc_mcontext.__gregs[_REG_PSL] &= ~(PSL_U | PSL_PREVU);
221 1.1 martin }
222 1.1 martin INVALID_UCONTEXT(vax, psl_1, "setting forbidden bits in psl")
223 1.1 martin uc->uc_mcontext.__gregs[_REG_PSL] |= PSL_IPL | PSL_IS;
224 1.1 martin }
225 1.1 martin INVALID_UCONTEXT(vax, psl_cm, "setting CM bit in psl")
226 1.1 martin uc->uc_mcontext.__gregs[_REG_PSL] |= PSL_CM;
227 1.1 martin }
228 1.1 martin #endif
229 1.1 martin
230 1.1 martin ATF_TP_ADD_TCS(tp)
231 1.1 martin {
232 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_works);
233 1.3 thorpej ATF_TP_ADD_TC(tp, lwp_create_bad_lid_ptr);
234 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_generic_fail_no_uc_cpu);
235 1.1 martin #ifdef __alpha__
236 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_alpha_fail_pslset);
237 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_alpha_fail_pslclr);
238 1.1 martin #endif
239 1.1 martin #ifdef __amd64__
240 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_amd64_fail_untouchable_rflags);
241 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_amd64_fail_pc_too_high);
242 1.1 martin #endif
243 1.1 martin #ifdef __arm__
244 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_arm_fail_invalid_mode);
245 1.1 martin #endif
246 1.1 martin #ifdef __hppa__
247 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_hppa_fail_invalid_1);
248 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_hppa_fail_invalid_0);
249 1.1 martin #endif
250 1.1 martin #ifdef __i386__
251 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_i386_fail_untouchable_eflags);
252 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_i386_fail_priv_escalation);
253 1.1 martin #endif
254 1.1 martin #ifdef __m68k__
255 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_m68k_fail_invalid_ps_bits);
256 1.1 martin #endif
257 1.1 martin #ifdef __sh3__
258 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_sh3_fail_modify_userstatic);
259 1.1 martin #endif
260 1.1 martin #ifdef __sparc__
261 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_sparc_fail_pc_odd);
262 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_sparc_fail_npc_odd);
263 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_sparc_fail_pc_null);
264 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_sparc_fail_npc_null);
265 1.1 martin #endif
266 1.1 martin #ifdef __vax__
267 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_vax_fail_psl_0);
268 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_vax_fail_psl_1);
269 1.1 martin ATF_TP_ADD_TC(tp, lwp_create_vax_fail_psl_cm);
270 1.1 martin #endif
271 1.1 martin return atf_no_error();
272 1.1 martin }
273