1*11be35a1SLionel Sambuc /* $NetBSD: t_swapcontext.c,v 1.1 2012/09/12 02:00:55 manu Exp $ */ 2*11be35a1SLionel Sambuc 3*11be35a1SLionel Sambuc /* 4*11be35a1SLionel Sambuc * Copyright (c) 2012 Emmanuel Dreyfus. All rights reserved. 5*11be35a1SLionel Sambuc * 6*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without 7*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions 8*11be35a1SLionel Sambuc * are met: 9*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright 10*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer. 11*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 12*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the 13*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution. 14*11be35a1SLionel Sambuc * 15*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16*11be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17*11be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18*11be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19*11be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20*11be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21*11be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22*11be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23*11be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24*11be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25*11be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE. 26*11be35a1SLionel Sambuc */ 27*11be35a1SLionel Sambuc 28*11be35a1SLionel Sambuc #include <sys/cdefs.h> 29*11be35a1SLionel Sambuc __RCSID("$NetBSD"); 30*11be35a1SLionel Sambuc 31*11be35a1SLionel Sambuc #include <pthread.h> 32*11be35a1SLionel Sambuc #include <ucontext.h> 33*11be35a1SLionel Sambuc #include <stdio.h> 34*11be35a1SLionel Sambuc #include <stdlib.h> 35*11be35a1SLionel Sambuc 36*11be35a1SLionel Sambuc #include <atf-c.h> 37*11be35a1SLionel Sambuc 38*11be35a1SLionel Sambuc #include "h_common.h" 39*11be35a1SLionel Sambuc 40*11be35a1SLionel Sambuc #define STACKSIZE 65536 41*11be35a1SLionel Sambuc 42*11be35a1SLionel Sambuc char stack[STACKSIZE]; 43*11be35a1SLionel Sambuc ucontext_t nctx; 44*11be35a1SLionel Sambuc ucontext_t octx; 45*11be35a1SLionel Sambuc void *oself; 46*11be35a1SLionel Sambuc void *nself; 47*11be35a1SLionel Sambuc int val1, val2; 48*11be35a1SLionel Sambuc 49*11be35a1SLionel Sambuc /* ARGSUSED0 */ 50*11be35a1SLionel Sambuc static void 51*11be35a1SLionel Sambuc swapfunc(void *arg) 52*11be35a1SLionel Sambuc { 53*11be35a1SLionel Sambuc /* 54*11be35a1SLionel Sambuc * If the test fails, we are very likely to crash 55*11be35a1SLionel Sambuc * without the opportunity to report 56*11be35a1SLionel Sambuc */ 57*11be35a1SLionel Sambuc nself = (void *)pthread_self(); 58*11be35a1SLionel Sambuc printf("after swapcontext self = %p\n", nself); 59*11be35a1SLionel Sambuc 60*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(oself, nself); 61*11be35a1SLionel Sambuc printf("Test succeeded\n"); 62*11be35a1SLionel Sambuc 63*11be35a1SLionel Sambuc /* NOTREACHED */ 64*11be35a1SLionel Sambuc return; 65*11be35a1SLionel Sambuc } 66*11be35a1SLionel Sambuc 67*11be35a1SLionel Sambuc /* ARGSUSED0 */ 68*11be35a1SLionel Sambuc static void * 69*11be35a1SLionel Sambuc threadfunc(void *arg) 70*11be35a1SLionel Sambuc { 71*11be35a1SLionel Sambuc nctx.uc_stack.ss_sp = stack; 72*11be35a1SLionel Sambuc nctx.uc_stack.ss_size = sizeof(stack); 73*11be35a1SLionel Sambuc 74*11be35a1SLionel Sambuc makecontext(&nctx, (void *)*swapfunc, 0); 75*11be35a1SLionel Sambuc 76*11be35a1SLionel Sambuc oself = (void *)pthread_self(); 77*11be35a1SLionel Sambuc printf("before swapcontext self = %p\n", oself); 78*11be35a1SLionel Sambuc PTHREAD_REQUIRE(swapcontext(&octx, &nctx)); 79*11be35a1SLionel Sambuc 80*11be35a1SLionel Sambuc /* NOTREACHED */ 81*11be35a1SLionel Sambuc return NULL; 82*11be35a1SLionel Sambuc } 83*11be35a1SLionel Sambuc 84*11be35a1SLionel Sambuc 85*11be35a1SLionel Sambuc ATF_TC(swapcontext1); 86*11be35a1SLionel Sambuc ATF_TC_HEAD(swapcontext1, tc) 87*11be35a1SLionel Sambuc { 88*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Testing if swapcontext() " 89*11be35a1SLionel Sambuc "alters pthread_self()"); 90*11be35a1SLionel Sambuc } 91*11be35a1SLionel Sambuc ATF_TC_BODY(swapcontext1, tc) 92*11be35a1SLionel Sambuc { 93*11be35a1SLionel Sambuc pthread_t thread; 94*11be35a1SLionel Sambuc 95*11be35a1SLionel Sambuc oself = (void *)&val1; 96*11be35a1SLionel Sambuc nself = (void *)&val2; 97*11be35a1SLionel Sambuc 98*11be35a1SLionel Sambuc printf("Testing if swapcontext() alters pthread_self()\n"); 99*11be35a1SLionel Sambuc 100*11be35a1SLionel Sambuc PTHREAD_REQUIRE(getcontext(&nctx)); 101*11be35a1SLionel Sambuc PTHREAD_REQUIRE(pthread_create(&thread, NULL, threadfunc, NULL)); 102*11be35a1SLionel Sambuc 103*11be35a1SLionel Sambuc return; 104*11be35a1SLionel Sambuc } 105*11be35a1SLionel Sambuc 106*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp) 107*11be35a1SLionel Sambuc { 108*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, swapcontext1); 109*11be35a1SLionel Sambuc 110*11be35a1SLionel Sambuc return atf_no_error(); 111*11be35a1SLionel Sambuc } 112