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