1 /* $NetBSD: t_swapcontext.c,v 1.3 2017/01/16 16:27:06 christos 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 <sys/types.h>
32 #include <errno.h>
33 #include <pthread.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ucontext.h>
38
39 #include <atf-c.h>
40
41 #include "h_common.h"
42
43 #define STACKSIZE 65536
44
45 char stack[STACKSIZE];
46 ucontext_t nctx;
47 ucontext_t octx;
48 void *oself;
49 void *nself;
50 int val1, val2;
51
52 /* ARGSUSED0 */
53 static void
swapfunc(void * arg)54 swapfunc(void *arg)
55 {
56 /*
57 * If the test fails, we are very likely to crash
58 * without the opportunity to report
59 */
60 nself = (void *)pthread_self();
61 printf("after swapcontext self = %p\n", nself);
62
63 ATF_REQUIRE_EQ(oself, nself);
64 printf("Test succeeded\n");
65 /* Go back in main */
66 ATF_REQUIRE(swapcontext(&nctx, &octx));
67
68 /* NOTREACHED */
69 return;
70 }
71
72 /* ARGSUSED0 */
73 static void *
threadfunc(void * arg)74 threadfunc(void *arg)
75 {
76 nctx.uc_stack.ss_sp = stack;
77 nctx.uc_stack.ss_size = sizeof(stack);
78
79 makecontext(&nctx, (void *)*swapfunc, 0);
80
81 oself = (void *)pthread_self();
82 printf("before swapcontext self = %p\n", oself);
83 ATF_REQUIRE_MSG(swapcontext(&octx, &nctx) != -1, "swapcontext failed: %s",
84 strerror(errno));
85
86 /* NOTREACHED */
87 return NULL;
88 }
89
90
91 ATF_TC(swapcontext1);
ATF_TC_HEAD(swapcontext1,tc)92 ATF_TC_HEAD(swapcontext1, tc)
93 {
94 atf_tc_set_md_var(tc, "descr", "Testing if swapcontext() "
95 "alters pthread_self()");
96 }
ATF_TC_BODY(swapcontext1,tc)97 ATF_TC_BODY(swapcontext1, tc)
98 {
99 pthread_t thread;
100
101 #if defined(__FreeBSD__) && defined(__mips__)
102 /*
103 * MIPS modifies TLS pointer in set_mcontext(), so
104 * swapping contexts obtained from different threads
105 * gives us different pthread_self() return value.
106 */
107 atf_tc_skip("Platform is not supported.");
108 #endif
109
110 oself = (void *)&val1;
111 nself = (void *)&val2;
112
113 printf("Testing if swapcontext() alters pthread_self()\n");
114
115 ATF_REQUIRE_MSG(getcontext(&nctx) != -1, "getcontext failed: %s",
116 strerror(errno));
117 PTHREAD_REQUIRE(pthread_create(&thread, NULL, threadfunc, NULL));
118 PTHREAD_REQUIRE(pthread_join(thread, NULL));
119 }
120
ATF_TP_ADD_TCS(tp)121 ATF_TP_ADD_TCS(tp)
122 {
123 ATF_TP_ADD_TC(tp, swapcontext1);
124
125 return atf_no_error();
126 }
127