xref: /netbsd-src/tests/lib/libpthread/t_swapcontext.c (revision ba65fde2d7fefa7d39838fa5fa855e62bd606b5e)
1 /* $NetBSD: t_swapcontext.c,v 1.1 2012/09/12 02:00:55 manu 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 <pthread.h>
32 #include <ucontext.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 
36 #include <atf-c.h>
37 
38 #include "h_common.h"
39 
40 #define STACKSIZE 65536
41 
42 char stack[STACKSIZE];
43 ucontext_t nctx;
44 ucontext_t octx;
45 void *oself;
46 void *nself;
47 int val1, val2;
48 
49 /* ARGSUSED0 */
50 static void
51 swapfunc(void *arg)
52 {
53 	/*
54 	 * If the test fails, we are very likely to crash
55 	 * without the opportunity to report
56 	 */
57 	nself = (void *)pthread_self();
58 	printf("after swapcontext self = %p\n", nself);
59 
60 	ATF_REQUIRE_EQ(oself, nself);
61 	printf("Test succeeded\n");
62 
63 	/* NOTREACHED */
64 	return;
65 }
66 
67 /* ARGSUSED0 */
68 static void *
69 threadfunc(void *arg)
70 {
71 	nctx.uc_stack.ss_sp = stack;
72 	nctx.uc_stack.ss_size = sizeof(stack);
73 
74 	makecontext(&nctx, (void *)*swapfunc, 0);
75 
76 	oself = (void *)pthread_self();
77 	printf("before swapcontext self = %p\n", oself);
78 	PTHREAD_REQUIRE(swapcontext(&octx, &nctx));
79 
80 	/* NOTREACHED */
81 	return NULL;
82 }
83 
84 
85 ATF_TC(swapcontext1);
86 ATF_TC_HEAD(swapcontext1, tc)
87 {
88 	atf_tc_set_md_var(tc, "descr", "Testing if swapcontext() "
89 	    "alters pthread_self()");
90 }
91 ATF_TC_BODY(swapcontext1, tc)
92 {
93 	pthread_t thread;
94 
95 	oself = (void *)&val1;
96 	nself = (void *)&val2;
97 
98 	printf("Testing if swapcontext() alters pthread_self()\n");
99 
100 	PTHREAD_REQUIRE(getcontext(&nctx));
101 	PTHREAD_REQUIRE(pthread_create(&thread, NULL, threadfunc, NULL));
102 
103 	return;
104 }
105 
106 ATF_TP_ADD_TCS(tp)
107 {
108 	ATF_TP_ADD_TC(tp, swapcontext1);
109 
110 	return atf_no_error();
111 }
112