157718be8SEnji Cooper /* $NetBSD: t_swapcontext.c,v 1.2 2014/08/25 16:31:15 bouyer 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 #ifdef __FreeBSD__ 3249a26c1fSEnji Cooper #include <sys/types.h> 33*64af3fbaSEnji Cooper #include <errno.h> 34*64af3fbaSEnji Cooper #include <string.h> 3549a26c1fSEnji Cooper #endif 3657718be8SEnji Cooper #include <pthread.h> 3757718be8SEnji Cooper #include <ucontext.h> 3857718be8SEnji Cooper #include <stdio.h> 3957718be8SEnji Cooper #include <stdlib.h> 4057718be8SEnji Cooper 4157718be8SEnji Cooper #include <atf-c.h> 4257718be8SEnji Cooper 4357718be8SEnji Cooper #include "h_common.h" 4457718be8SEnji Cooper 4557718be8SEnji Cooper #define STACKSIZE 65536 4657718be8SEnji Cooper 4757718be8SEnji Cooper char stack[STACKSIZE]; 4857718be8SEnji Cooper ucontext_t nctx; 4957718be8SEnji Cooper ucontext_t octx; 5057718be8SEnji Cooper void *oself; 5157718be8SEnji Cooper void *nself; 5257718be8SEnji Cooper int val1, val2; 5357718be8SEnji Cooper 5457718be8SEnji Cooper /* ARGSUSED0 */ 5557718be8SEnji Cooper static void 5657718be8SEnji Cooper swapfunc(void *arg) 5757718be8SEnji Cooper { 5857718be8SEnji Cooper /* 5957718be8SEnji Cooper * If the test fails, we are very likely to crash 6057718be8SEnji Cooper * without the opportunity to report 6157718be8SEnji Cooper */ 6257718be8SEnji Cooper nself = (void *)pthread_self(); 6357718be8SEnji Cooper printf("after swapcontext self = %p\n", nself); 6457718be8SEnji Cooper 6557718be8SEnji Cooper ATF_REQUIRE_EQ(oself, nself); 6657718be8SEnji Cooper printf("Test succeeded\n"); 6757718be8SEnji Cooper /* Go back in main */ 6857718be8SEnji Cooper ATF_REQUIRE(swapcontext(&nctx, &octx)); 6957718be8SEnji Cooper 7057718be8SEnji Cooper /* NOTREACHED */ 7157718be8SEnji Cooper return; 7257718be8SEnji Cooper } 7357718be8SEnji Cooper 7457718be8SEnji Cooper /* ARGSUSED0 */ 7557718be8SEnji Cooper static void * 7657718be8SEnji Cooper threadfunc(void *arg) 7757718be8SEnji Cooper { 7857718be8SEnji Cooper nctx.uc_stack.ss_sp = stack; 7957718be8SEnji Cooper nctx.uc_stack.ss_size = sizeof(stack); 8057718be8SEnji Cooper 8157718be8SEnji Cooper makecontext(&nctx, (void *)*swapfunc, 0); 8257718be8SEnji Cooper 8357718be8SEnji Cooper oself = (void *)pthread_self(); 8457718be8SEnji Cooper printf("before swapcontext self = %p\n", oself); 85*64af3fbaSEnji Cooper #ifdef __FreeBSD__ 86*64af3fbaSEnji Cooper ATF_REQUIRE_MSG(swapcontext(&octx, &nctx) != -1, "swapcontext failed: %s", 87*64af3fbaSEnji Cooper strerror(errno)); 88*64af3fbaSEnji Cooper #else 8957718be8SEnji Cooper PTHREAD_REQUIRE(swapcontext(&octx, &nctx)); 90*64af3fbaSEnji Cooper #endif 9157718be8SEnji Cooper 9257718be8SEnji Cooper /* NOTREACHED */ 9357718be8SEnji Cooper return NULL; 9457718be8SEnji Cooper } 9557718be8SEnji Cooper 9657718be8SEnji Cooper 9757718be8SEnji Cooper ATF_TC(swapcontext1); 9857718be8SEnji Cooper ATF_TC_HEAD(swapcontext1, tc) 9957718be8SEnji Cooper { 10057718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Testing if swapcontext() " 10157718be8SEnji Cooper "alters pthread_self()"); 10257718be8SEnji Cooper } 10357718be8SEnji Cooper ATF_TC_BODY(swapcontext1, tc) 10457718be8SEnji Cooper { 10557718be8SEnji Cooper pthread_t thread; 10657718be8SEnji Cooper 10721e609c4SRuslan Bukin #if defined(__FreeBSD__) && defined(__mips__) 108bf7d7eaeSRuslan Bukin /* 109bf7d7eaeSRuslan Bukin * MIPS modifies TLS pointer in set_mcontext(), so 110bf7d7eaeSRuslan Bukin * swapping contexts obtained from different threads 111bf7d7eaeSRuslan Bukin * gives us different pthread_self() return value. 112bf7d7eaeSRuslan Bukin */ 113bf7d7eaeSRuslan Bukin atf_tc_skip("Platform is not supported."); 114bf7d7eaeSRuslan Bukin #endif 115bf7d7eaeSRuslan Bukin 11657718be8SEnji Cooper oself = (void *)&val1; 11757718be8SEnji Cooper nself = (void *)&val2; 11857718be8SEnji Cooper 11957718be8SEnji Cooper printf("Testing if swapcontext() alters pthread_self()\n"); 12057718be8SEnji Cooper 121*64af3fbaSEnji Cooper #ifdef __FreeBSD__ 122*64af3fbaSEnji Cooper ATF_REQUIRE_MSG(getcontext(&nctx) != -1, "getcontext failed: %s", 123*64af3fbaSEnji Cooper strerror(errno)); 124*64af3fbaSEnji Cooper #else 12557718be8SEnji Cooper PTHREAD_REQUIRE(getcontext(&nctx)); 126*64af3fbaSEnji Cooper #endif 12757718be8SEnji Cooper PTHREAD_REQUIRE(pthread_create(&thread, NULL, threadfunc, NULL)); 12857718be8SEnji Cooper PTHREAD_REQUIRE(pthread_join(thread, NULL)); 12957718be8SEnji Cooper } 13057718be8SEnji Cooper 13157718be8SEnji Cooper ATF_TP_ADD_TCS(tp) 13257718be8SEnji Cooper { 13357718be8SEnji Cooper ATF_TP_ADD_TC(tp, swapcontext1); 13457718be8SEnji Cooper 13557718be8SEnji Cooper return atf_no_error(); 13657718be8SEnji Cooper } 137