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