1272343Sngie/* $NetBSD: t_getcontext.c,v 1.3 2011/07/14 04:59:14 jruoho Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2008 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26272343Sngie * POSSIBILITY OF SUCH DAMAGE.
27272343Sngie */
28272343Sngie
29272343Sngie#include <sys/cdefs.h>
30272343Sngie__COPYRIGHT("@(#) Copyright (c) 2008\
31272343Sngie The NetBSD Foundation, inc. All rights reserved.");
32272343Sngie__RCSID("$NetBSD: t_getcontext.c,v 1.3 2011/07/14 04:59:14 jruoho Exp $");
33272343Sngie
34272343Sngie#include <atf-c.h>
35272343Sngie#include <errno.h>
36272343Sngie#include <stdarg.h>
37272343Sngie#include <stdlib.h>
38272343Sngie#include <ucontext.h>
39272343Sngie
40272343Sngie#define STACKSZ (10*1024)
41272343Sngie#define DEPTH 3
42272343Sngie
43272343Sngiestatic int calls;
44272343Sngie
45272343Sngiestatic void
46272343Sngierun(int n, ...)
47272343Sngie{
48272343Sngie	va_list va;
49272343Sngie	int i, ia;
50272343Sngie
51272343Sngie	ATF_REQUIRE_EQ(n, DEPTH - calls - 1);
52272343Sngie
53272343Sngie	va_start(va, n);
54314818Sngie#ifdef __FreeBSD__
55314818Sngie#if defined(__amd64__)
56275033Sjhb	for (i = 0; i < 5; i++) {
57314818Sngie#elif defined(__aarch64__)
58288444Sandrew	for (i = 0; i < 7; i++) {
59314818Sngie#elif defined(__mips__)
60314818Sngie	for (i = 0; i < 5; i++) {
61275033Sjhb#else
62272343Sngie	for (i = 0; i < 9; i++) {
63275033Sjhb#endif
64314818Sngie#else
65314818Sngie	for (i = 0; i < 9; i++) {
66314818Sngie#endif
67272343Sngie		ia = va_arg(va, int);
68272343Sngie		ATF_REQUIRE_EQ(i, ia);
69272343Sngie	}
70272343Sngie	va_end(va);
71272343Sngie
72272343Sngie	calls++;
73272343Sngie}
74272343Sngie
75272343SngieATF_TC(getcontext_err);
76272343SngieATF_TC_HEAD(getcontext_err, tc)
77272343Sngie{
78272343Sngie	atf_tc_set_md_var(tc, "descr", "Test errors from getcontext(2)");
79272343Sngie}
80272343Sngie
81272343SngieATF_TC_BODY(getcontext_err, tc)
82272343Sngie{
83272343Sngie
84272343Sngie	errno = 0;
85272343Sngie	ATF_REQUIRE_ERRNO(EFAULT, getcontext((void *)-1) == -1);
86272343Sngie}
87272343Sngie
88272343SngieATF_TC(setcontext_err);
89272343SngieATF_TC_HEAD(setcontext_err, tc)
90272343Sngie{
91272343Sngie	atf_tc_set_md_var(tc, "descr", "Test errors from setcontext(2)");
92272343Sngie}
93272343Sngie
94272343SngieATF_TC_BODY(setcontext_err, tc)
95272343Sngie{
96272343Sngie
97272343Sngie	errno = 0;
98272343Sngie	ATF_REQUIRE_ERRNO(EFAULT, setcontext((void *)-1) == -1);
99272343Sngie}
100272343Sngie
101272343SngieATF_TC(setcontext_link);
102272343SngieATF_TC_HEAD(setcontext_link, tc)
103272343Sngie{
104272343Sngie
105272343Sngie	atf_tc_set_md_var(tc, "descr",
106272343Sngie	"Checks get/make/setcontext(), context linking via uc_link(), "
107272343Sngie	    "and argument passing to the new context");
108272343Sngie}
109272343Sngie
110272343SngieATF_TC_BODY(setcontext_link, tc)
111272343Sngie{
112272343Sngie	ucontext_t uc[DEPTH];
113272343Sngie	ucontext_t save;
114272343Sngie	volatile int i = 0; /* avoid longjmp clobbering */
115272343Sngie
116272343Sngie	for (i = 0; i < DEPTH; ++i) {
117272343Sngie		ATF_REQUIRE_EQ(getcontext(&uc[i]), 0);
118272343Sngie
119272343Sngie		uc[i].uc_stack.ss_sp = malloc(STACKSZ);
120272343Sngie		uc[i].uc_stack.ss_size = STACKSZ;
121272343Sngie		uc[i].uc_link = (i > 0) ? &uc[i - 1] : &save;
122272343Sngie
123314818Sngie#ifdef __FreeBSD__
124314818Sngie#if defined(__amd64__)
125275033Sjhb		/* FreeBSD/amd64 only permits up to 6 arguments. */
126275033Sjhb		makecontext(&uc[i], (void *)run, 6, i,
127275033Sjhb			0, 1, 2, 3, 4);
128314818Sngie#elif defined(__aarch64__)
129288444Sandrew		/* FreeBSD/arm64 only permits up to 8 arguments. */
130288444Sandrew		makecontext(&uc[i], (void *)run, 8, i,
131288444Sandrew			0, 1, 2, 3, 4, 5, 6);
132314818Sngie#elif defined(__mips__)
133314818Sngie		/* FreeBSD/mips only permits up to 6 arguments. */
134314818Sngie		makecontext(&uc[i], (void *)run, 6, i,
135314818Sngie			0, 1, 2, 3, 4);
136275033Sjhb#else
137272343Sngie		makecontext(&uc[i], (void *)run, 10, i,
138272343Sngie			0, 1, 2, 3, 4, 5, 6, 7, 8);
139275033Sjhb#endif
140314818Sngie#else
141314818Sngie		makecontext(&uc[i], (void *)run, 10, i,
142314818Sngie			0, 1, 2, 3, 4, 5, 6, 7, 8);
143314818Sngie#endif
144272343Sngie	}
145272343Sngie
146272343Sngie	ATF_REQUIRE_EQ(getcontext(&save), 0);
147272343Sngie
148272343Sngie	if (calls == 0)
149272343Sngie		ATF_REQUIRE_EQ(setcontext(&uc[DEPTH-1]), 0);
150272343Sngie}
151272343Sngie
152272343SngieATF_TP_ADD_TCS(tp)
153272343Sngie{
154272343Sngie
155272343Sngie	ATF_TP_ADD_TC(tp, getcontext_err);
156272343Sngie	ATF_TP_ADD_TC(tp, setcontext_err);
157272343Sngie	ATF_TP_ADD_TC(tp, setcontext_link);
158272343Sngie
159272343Sngie	return atf_no_error();
160272343Sngie}
161