t_getcontext.c revision 288444
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);
54275033Sjhb#if defined(__FreeBSD__) && defined(__amd64__)
55275033Sjhb	for (i = 0; i < 5; i++) {
56288444Sandrew#elif defined(__FreeBSD__) && defined(__aarch64__)
57288444Sandrew	for (i = 0; i < 7; i++) {
58275033Sjhb#else
59272343Sngie	for (i = 0; i < 9; i++) {
60275033Sjhb#endif
61272343Sngie		ia = va_arg(va, int);
62272343Sngie		ATF_REQUIRE_EQ(i, ia);
63272343Sngie	}
64272343Sngie	va_end(va);
65272343Sngie
66272343Sngie	calls++;
67272343Sngie}
68272343Sngie
69272343SngieATF_TC(getcontext_err);
70272343SngieATF_TC_HEAD(getcontext_err, tc)
71272343Sngie{
72272343Sngie	atf_tc_set_md_var(tc, "descr", "Test errors from getcontext(2)");
73272343Sngie}
74272343Sngie
75272343SngieATF_TC_BODY(getcontext_err, tc)
76272343Sngie{
77272343Sngie
78272343Sngie	errno = 0;
79272343Sngie	ATF_REQUIRE_ERRNO(EFAULT, getcontext((void *)-1) == -1);
80272343Sngie}
81272343Sngie
82272343SngieATF_TC(setcontext_err);
83272343SngieATF_TC_HEAD(setcontext_err, tc)
84272343Sngie{
85272343Sngie	atf_tc_set_md_var(tc, "descr", "Test errors from setcontext(2)");
86272343Sngie}
87272343Sngie
88272343SngieATF_TC_BODY(setcontext_err, tc)
89272343Sngie{
90272343Sngie
91272343Sngie	errno = 0;
92272343Sngie	ATF_REQUIRE_ERRNO(EFAULT, setcontext((void *)-1) == -1);
93272343Sngie}
94272343Sngie
95272343SngieATF_TC(setcontext_link);
96272343SngieATF_TC_HEAD(setcontext_link, tc)
97272343Sngie{
98272343Sngie
99272343Sngie	atf_tc_set_md_var(tc, "descr",
100272343Sngie	"Checks get/make/setcontext(), context linking via uc_link(), "
101272343Sngie	    "and argument passing to the new context");
102272343Sngie}
103272343Sngie
104272343SngieATF_TC_BODY(setcontext_link, tc)
105272343Sngie{
106272343Sngie	ucontext_t uc[DEPTH];
107272343Sngie	ucontext_t save;
108272343Sngie	volatile int i = 0; /* avoid longjmp clobbering */
109272343Sngie
110272343Sngie	for (i = 0; i < DEPTH; ++i) {
111272343Sngie		ATF_REQUIRE_EQ(getcontext(&uc[i]), 0);
112272343Sngie
113272343Sngie		uc[i].uc_stack.ss_sp = malloc(STACKSZ);
114272343Sngie		uc[i].uc_stack.ss_size = STACKSZ;
115272343Sngie		uc[i].uc_link = (i > 0) ? &uc[i - 1] : &save;
116272343Sngie
117275033Sjhb#if defined(__FreeBSD__) && defined(__amd64__)
118275033Sjhb		/* FreeBSD/amd64 only permits up to 6 arguments. */
119275033Sjhb		makecontext(&uc[i], (void *)run, 6, i,
120275033Sjhb			0, 1, 2, 3, 4);
121288444Sandrew#elif defined(__FreeBSD__) && defined(__aarch64__)
122288444Sandrew		/* FreeBSD/arm64 only permits up to 8 arguments. */
123288444Sandrew		makecontext(&uc[i], (void *)run, 8, i,
124288444Sandrew			0, 1, 2, 3, 4, 5, 6);
125275033Sjhb#else
126272343Sngie		makecontext(&uc[i], (void *)run, 10, i,
127272343Sngie			0, 1, 2, 3, 4, 5, 6, 7, 8);
128275033Sjhb#endif
129272343Sngie	}
130272343Sngie
131272343Sngie	ATF_REQUIRE_EQ(getcontext(&save), 0);
132272343Sngie
133272343Sngie	if (calls == 0)
134272343Sngie		ATF_REQUIRE_EQ(setcontext(&uc[DEPTH-1]), 0);
135272343Sngie}
136272343Sngie
137272343SngieATF_TP_ADD_TCS(tp)
138272343Sngie{
139272343Sngie
140272343Sngie	ATF_TP_ADD_TC(tp, getcontext_err);
141272343Sngie	ATF_TP_ADD_TC(tp, setcontext_err);
142272343Sngie	ATF_TP_ADD_TC(tp, setcontext_link);
143272343Sngie
144272343Sngie	return atf_no_error();
145272343Sngie}
146