xref: /freebsd-src/contrib/netbsd-tests/lib/libpthread/t_swapcontext.c (revision 1a36faad54665288ed4eb839d2a4699ae2ead45e)
1*3b0a9131SEnji Cooper /* $NetBSD: t_swapcontext.c,v 1.3 2017/01/16 16:27:06 christos Exp $ */
257718be8SEnji Cooper 
357718be8SEnji Cooper /*
457718be8SEnji Cooper  * Copyright (c) 2012 Emmanuel Dreyfus. All rights reserved.
557718be8SEnji Cooper  *
657718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
757718be8SEnji Cooper  * modification, are permitted provided that the following conditions
857718be8SEnji Cooper  * are met:
957718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
1057718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
1157718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
1257718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
1357718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
1457718be8SEnji Cooper  *
1557718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1657718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1757718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1857718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1957718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2057718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2157718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2257718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2357718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2457718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2557718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
2657718be8SEnji Cooper  */
2757718be8SEnji Cooper 
2857718be8SEnji Cooper #include <sys/cdefs.h>
2957718be8SEnji Cooper __RCSID("$NetBSD");
3057718be8SEnji Cooper 
3149a26c1fSEnji Cooper #include <sys/types.h>
3264af3fbaSEnji Cooper #include <errno.h>
3357718be8SEnji Cooper #include <pthread.h>
3457718be8SEnji Cooper #include <stdio.h>
3557718be8SEnji Cooper #include <stdlib.h>
36*3b0a9131SEnji Cooper #include <string.h>
37*3b0a9131SEnji Cooper #include <ucontext.h>
3857718be8SEnji Cooper 
3957718be8SEnji Cooper #include <atf-c.h>
4057718be8SEnji Cooper 
4157718be8SEnji Cooper #include "h_common.h"
4257718be8SEnji Cooper 
4357718be8SEnji Cooper #define STACKSIZE 65536
4457718be8SEnji Cooper 
4557718be8SEnji Cooper char stack[STACKSIZE];
4657718be8SEnji Cooper ucontext_t nctx;
4757718be8SEnji Cooper ucontext_t octx;
4857718be8SEnji Cooper void *oself;
4957718be8SEnji Cooper void *nself;
5057718be8SEnji Cooper int val1, val2;
5157718be8SEnji Cooper 
5257718be8SEnji Cooper /* ARGSUSED0 */
5357718be8SEnji Cooper static void
swapfunc(void * arg)5457718be8SEnji Cooper swapfunc(void *arg)
5557718be8SEnji Cooper {
5657718be8SEnji Cooper 	/*
5757718be8SEnji Cooper 	 * If the test fails, we are very likely to crash
5857718be8SEnji Cooper 	 * without the opportunity to report
5957718be8SEnji Cooper 	 */
6057718be8SEnji Cooper 	nself = (void *)pthread_self();
6157718be8SEnji Cooper 	printf("after swapcontext self = %p\n", nself);
6257718be8SEnji Cooper 
6357718be8SEnji Cooper 	ATF_REQUIRE_EQ(oself, nself);
6457718be8SEnji Cooper 	printf("Test succeeded\n");
6557718be8SEnji Cooper 	/* Go back in main */
6657718be8SEnji Cooper 	ATF_REQUIRE(swapcontext(&nctx, &octx));
6757718be8SEnji Cooper 
6857718be8SEnji Cooper 	/* NOTREACHED */
6957718be8SEnji Cooper 	return;
7057718be8SEnji Cooper }
7157718be8SEnji Cooper 
7257718be8SEnji Cooper /* ARGSUSED0 */
7357718be8SEnji Cooper static void *
threadfunc(void * arg)7457718be8SEnji Cooper threadfunc(void *arg)
7557718be8SEnji Cooper {
7657718be8SEnji Cooper 	nctx.uc_stack.ss_sp = stack;
7757718be8SEnji Cooper 	nctx.uc_stack.ss_size = sizeof(stack);
7857718be8SEnji Cooper 
7957718be8SEnji Cooper 	makecontext(&nctx, (void *)*swapfunc, 0);
8057718be8SEnji Cooper 
8157718be8SEnji Cooper 	oself = (void *)pthread_self();
8257718be8SEnji Cooper 	printf("before swapcontext self = %p\n", oself);
8364af3fbaSEnji Cooper 	ATF_REQUIRE_MSG(swapcontext(&octx, &nctx) != -1, "swapcontext failed: %s",
8464af3fbaSEnji Cooper 	    strerror(errno));
8557718be8SEnji Cooper 
8657718be8SEnji Cooper 	/* NOTREACHED */
8757718be8SEnji Cooper 	return NULL;
8857718be8SEnji Cooper }
8957718be8SEnji Cooper 
9057718be8SEnji Cooper 
9157718be8SEnji Cooper ATF_TC(swapcontext1);
ATF_TC_HEAD(swapcontext1,tc)9257718be8SEnji Cooper ATF_TC_HEAD(swapcontext1, tc)
9357718be8SEnji Cooper {
9457718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Testing if swapcontext() "
9557718be8SEnji Cooper 	    "alters pthread_self()");
9657718be8SEnji Cooper }
ATF_TC_BODY(swapcontext1,tc)9757718be8SEnji Cooper ATF_TC_BODY(swapcontext1, tc)
9857718be8SEnji Cooper {
9957718be8SEnji Cooper 	pthread_t thread;
10057718be8SEnji Cooper 
10121e609c4SRuslan Bukin #if defined(__FreeBSD__) && defined(__mips__)
102bf7d7eaeSRuslan Bukin 	/*
103bf7d7eaeSRuslan Bukin 	 * MIPS modifies TLS pointer in set_mcontext(), so
104bf7d7eaeSRuslan Bukin 	 * swapping contexts obtained from different threads
105bf7d7eaeSRuslan Bukin 	 * gives us different pthread_self() return value.
106bf7d7eaeSRuslan Bukin 	 */
107bf7d7eaeSRuslan Bukin 	atf_tc_skip("Platform is not supported.");
108bf7d7eaeSRuslan Bukin #endif
109bf7d7eaeSRuslan Bukin 
11057718be8SEnji Cooper 	oself = (void *)&val1;
11157718be8SEnji Cooper 	nself = (void *)&val2;
11257718be8SEnji Cooper 
11357718be8SEnji Cooper 	printf("Testing if swapcontext() alters pthread_self()\n");
11457718be8SEnji Cooper 
11564af3fbaSEnji Cooper 	ATF_REQUIRE_MSG(getcontext(&nctx) != -1, "getcontext failed: %s",
11664af3fbaSEnji Cooper 	    strerror(errno));
11757718be8SEnji Cooper 	PTHREAD_REQUIRE(pthread_create(&thread, NULL, threadfunc, NULL));
11857718be8SEnji Cooper 	PTHREAD_REQUIRE(pthread_join(thread, NULL));
11957718be8SEnji Cooper }
12057718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)12157718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
12257718be8SEnji Cooper {
12357718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, swapcontext1);
12457718be8SEnji Cooper 
12557718be8SEnji Cooper 	return atf_no_error();
12657718be8SEnji Cooper }
127