etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libc / sys / t_swapcontext.c
blobde36c8807dce83c702fc8f9257c6867335fdefe9
1 /* $NetBSD: t_swapcontext.c,v 1.3 2013/05/05 10:28:11 skrll Exp $ */
3 /*
4 * Copyright (c) 2012 Emmanuel Dreyfus. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __RCSID("$NetBSD");
31 #include <ucontext.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <lwp.h>
36 #include <atf-c.h>
38 #define STACKSIZE 65536
40 char stack[STACKSIZE];
41 ucontext_t nctx;
42 ucontext_t octx;
43 void *otls;
44 void *ntls;
45 int val1, val2;
46 int alter_tlsbase;
48 /* ARGSUSED0 */
49 static void
50 swapfunc(void *arg)
52 ntls = _lwp_getprivate();
53 printf("after swapcontext TLS pointer = %p\n", ntls);
55 if (alter_tlsbase) {
56 ATF_REQUIRE_EQ(ntls, &val1);
57 printf("TLS pointer modified by swapcontext()\n");
58 } else {
59 ATF_REQUIRE_EQ(ntls, &val2);
60 printf("TLS pointer left untouched by swapcontext()\n");
63 /* Go back in main */
64 ATF_REQUIRE(swapcontext(&nctx, &octx));
66 /* NOTREACHED */
67 return;
70 static void
71 mainfunc(void)
73 printf("Testing if swapcontext() alters TLS pointer if _UC_TLSBASE "
74 "is %s\n", (alter_tlsbase) ? "left set" : "cleared");
76 _lwp_setprivate(&val1);
77 printf("before swapcontext TLS pointer = %p\n", &val1);
79 ATF_REQUIRE(getcontext(&nctx) == 0);
81 nctx.uc_stack.ss_sp = stack;
82 nctx.uc_stack.ss_size = sizeof(stack);
84 #ifndef _UC_TLSBASE
85 ATF_REQUIRE_MSG(0, "_UC_TLSBASE is not defined");
86 #else /* _UC_TLSBASE */
87 ATF_REQUIRE(nctx.uc_flags & _UC_TLSBASE);
88 if (!alter_tlsbase)
89 nctx.uc_flags &= ~_UC_TLSBASE;
90 #endif /* _UC_TLSBASE */
92 makecontext(&nctx, swapfunc, 0);
94 _lwp_setprivate(&val2);
95 otls = _lwp_getprivate();
96 printf("before swapcontext TLS pointer = %p\n", otls);
97 ATF_REQUIRE(swapcontext(&octx, &nctx) == 0);
99 printf("Test completed\n");
103 ATF_TC(swapcontext1);
104 ATF_TC_HEAD(swapcontext1, tc)
106 atf_tc_set_md_var(tc, "descr", "Testing if swapcontext() can let "
107 "TLS pointer untouched");
109 ATF_TC_BODY(swapcontext1, tc)
111 alter_tlsbase = 0;
112 mainfunc();
115 ATF_TC(swapcontext2);
116 ATF_TC_HEAD(swapcontext2, tc)
118 atf_tc_set_md_var(tc, "descr", "Testing if swapcontext() can "
119 "modify TLS pointer");
121 ATF_TC_BODY(swapcontext2, tc)
123 alter_tlsbase = 1;
124 mainfunc();
127 ATF_TP_ADD_TCS(tp)
129 ATF_TP_ADD_TC(tp, swapcontext1);
130 ATF_TP_ADD_TC(tp, swapcontext2);
132 return atf_no_error();