]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/netbsd-tests/lib/libc/sys/t_swapcontext.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / netbsd-tests / lib / libc / sys / t_swapcontext.c
1 /* $NetBSD: t_swapcontext.c,v 1.3 2013/05/05 10:28:11 skrll Exp $ */
2
3 /*
4  * Copyright (c) 2012 Emmanuel Dreyfus. All rights reserved.
5  *
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.
14  *
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.
26  */
27
28 #include <sys/cdefs.h>
29 __RCSID("$NetBSD");
30
31 #include <ucontext.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <lwp.h>
35
36 #include <atf-c.h>
37
38 #define STACKSIZE 65536
39
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;
47
48 /* ARGSUSED0 */
49 static void
50 swapfunc(void *arg)
51 {
52         ntls = _lwp_getprivate();
53         printf("after swapcontext TLS pointer = %p\n", ntls);
54
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");
61         }
62
63         /* Go back in main */
64         ATF_REQUIRE(swapcontext(&nctx, &octx));
65
66         /* NOTREACHED */
67         return;
68 }
69
70 static void
71 mainfunc(void)
72 {
73         printf("Testing if swapcontext() alters TLS pointer if _UC_TLSBASE "
74                "is %s\n", (alter_tlsbase) ? "left set" : "cleared");
75
76         _lwp_setprivate(&val1);
77         printf("before swapcontext TLS pointer = %p\n", &val1);
78
79         ATF_REQUIRE(getcontext(&nctx) == 0);
80
81         nctx.uc_stack.ss_sp = stack;
82         nctx.uc_stack.ss_size = sizeof(stack);
83
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 */
91
92         makecontext(&nctx, swapfunc, 0);
93
94         _lwp_setprivate(&val2);
95         otls = _lwp_getprivate();
96         printf("before swapcontext TLS pointer = %p\n", otls);
97         ATF_REQUIRE(swapcontext(&octx, &nctx) == 0);
98
99         printf("Test completed\n");
100 }
101
102
103 ATF_TC(swapcontext1);
104 ATF_TC_HEAD(swapcontext1, tc)
105 {
106         atf_tc_set_md_var(tc, "descr", "Testing if swapcontext() can let "
107             "TLS pointer untouched");
108 }
109 ATF_TC_BODY(swapcontext1, tc)
110 {
111         alter_tlsbase = 0;
112         mainfunc();
113 }
114
115 ATF_TC(swapcontext2);
116 ATF_TC_HEAD(swapcontext2, tc)
117 {
118         atf_tc_set_md_var(tc, "descr", "Testing if swapcontext() can "
119             "modify TLS pointer");
120 }
121 ATF_TC_BODY(swapcontext2, tc)
122 {
123         alter_tlsbase = 1;
124         mainfunc();
125 }
126
127 ATF_TP_ADD_TCS(tp)
128 {
129         ATF_TP_ADD_TC(tp, swapcontext1);
130         ATF_TP_ADD_TC(tp, swapcontext2);
131
132         return atf_no_error();
133 }