Home | History | Annotate | Line # | Download | only in sys
t_swapcontext.c revision 1.1.2.2
      1  1.1.2.2   tls /* $NetBSD: t_swapcontext.c,v 1.1.2.2 2013/06/23 06:28:56 tls Exp $ */
      2      1.1  manu 
      3      1.1  manu /*
      4      1.1  manu  * Copyright (c) 2012 Emmanuel Dreyfus. All rights reserved.
      5      1.1  manu  *
      6      1.1  manu  * Redistribution and use in source and binary forms, with or without
      7      1.1  manu  * modification, are permitted provided that the following conditions
      8      1.1  manu  * are met:
      9      1.1  manu  * 1. Redistributions of source code must retain the above copyright
     10      1.1  manu  *    notice, this list of conditions and the following disclaimer.
     11      1.1  manu  * 2. Redistributions in binary form must reproduce the above copyright
     12      1.1  manu  *    notice, this list of conditions and the following disclaimer in the
     13      1.1  manu  *    documentation and/or other materials provided with the distribution.
     14      1.1  manu  *
     15      1.1  manu  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16      1.1  manu  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17      1.1  manu  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18      1.1  manu  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19      1.1  manu  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20      1.1  manu  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21      1.1  manu  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22      1.1  manu  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23      1.1  manu  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24      1.1  manu  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25      1.1  manu  * POSSIBILITY OF SUCH DAMAGE.
     26      1.1  manu  */
     27      1.1  manu 
     28      1.1  manu #include <sys/cdefs.h>
     29      1.1  manu __RCSID("$NetBSD");
     30      1.1  manu 
     31      1.1  manu #include <ucontext.h>
     32      1.1  manu #include <stdio.h>
     33      1.1  manu #include <stdlib.h>
     34      1.1  manu #include <lwp.h>
     35      1.1  manu 
     36      1.1  manu #include <atf-c.h>
     37      1.1  manu 
     38      1.1  manu #define STACKSIZE 65536
     39      1.1  manu 
     40      1.1  manu char stack[STACKSIZE];
     41      1.1  manu ucontext_t nctx;
     42      1.1  manu ucontext_t octx;
     43      1.1  manu void *otls;
     44      1.1  manu void *ntls;
     45      1.1  manu int val1, val2;
     46      1.1  manu int alter_tlsbase;
     47      1.1  manu 
     48      1.1  manu /* ARGSUSED0 */
     49      1.1  manu static void
     50      1.1  manu swapfunc(void *arg)
     51      1.1  manu {
     52      1.1  manu 	ntls = _lwp_getprivate();
     53      1.1  manu 	printf("after swapcontext TLS pointer = %p\n", ntls);
     54      1.1  manu 
     55      1.1  manu 	if (alter_tlsbase) {
     56      1.1  manu 		ATF_REQUIRE_EQ(ntls, &val1);
     57      1.1  manu 		printf("TLS pointer modified by swapcontext()\n");
     58      1.1  manu 	} else {
     59      1.1  manu 		ATF_REQUIRE_EQ(ntls, &val2);
     60      1.1  manu 		printf("TLS pointer left untouched by swapcontext()\n");
     61      1.1  manu 	}
     62      1.1  manu 
     63      1.1  manu 	/* Go back in main */
     64      1.1  manu 	ATF_REQUIRE(swapcontext(&nctx, &octx));
     65      1.1  manu 
     66      1.1  manu 	/* NOTREACHED */
     67      1.1  manu 	return;
     68      1.1  manu }
     69      1.1  manu 
     70      1.1  manu static void
     71      1.1  manu mainfunc(void)
     72      1.1  manu {
     73      1.1  manu 	printf("Testing if swapcontext() alters TLS pointer if _UC_TLSBASE "
     74      1.1  manu 	       "is %s\n", (alter_tlsbase) ? "left set" : "cleared");
     75      1.1  manu 
     76      1.1  manu 	_lwp_setprivate(&val1);
     77      1.1  manu 	printf("before swapcontext TLS pointer = %p\n", &val1);
     78      1.1  manu 
     79      1.1  manu 	ATF_REQUIRE(getcontext(&nctx) == 0);
     80      1.1  manu 
     81      1.1  manu 	nctx.uc_stack.ss_sp = stack;
     82      1.1  manu 	nctx.uc_stack.ss_size = sizeof(stack);
     83      1.1  manu 
     84      1.1  manu #ifndef _UC_TLSBASE
     85      1.1  manu 	ATF_REQUIRE_MSG(0, "_UC_TLSBASE is not defined");
     86      1.1  manu #else /* _UC_TLSBASE */
     87      1.1  manu 	ATF_REQUIRE(nctx.uc_flags & _UC_TLSBASE);
     88      1.1  manu 	if (!alter_tlsbase)
     89      1.1  manu 		nctx.uc_flags &= ~_UC_TLSBASE;
     90      1.1  manu #endif /* _UC_TLSBASE */
     91  1.1.2.2   tls 
     92  1.1.2.1   tls 	makecontext(&nctx, swapfunc, 0);
     93  1.1.2.2   tls 
     94      1.1  manu 	_lwp_setprivate(&val2);
     95      1.1  manu 	otls = _lwp_getprivate();
     96      1.1  manu 	printf("before swapcontext TLS pointer = %p\n", otls);
     97      1.1  manu 	ATF_REQUIRE(swapcontext(&octx, &nctx) == 0);
     98      1.1  manu 
     99      1.1  manu 	printf("Test completed\n");
    100      1.1  manu }
    101      1.1  manu 
    102      1.1  manu 
    103      1.1  manu ATF_TC(swapcontext1);
    104      1.1  manu ATF_TC_HEAD(swapcontext1, tc)
    105      1.1  manu {
    106      1.1  manu 	atf_tc_set_md_var(tc, "descr", "Testing if swapcontext() can let "
    107      1.1  manu 	    "TLS pointer untouched");
    108      1.1  manu }
    109      1.1  manu ATF_TC_BODY(swapcontext1, tc)
    110      1.1  manu {
    111      1.1  manu 	alter_tlsbase = 0;
    112      1.1  manu 	mainfunc();
    113      1.1  manu }
    114      1.1  manu 
    115      1.1  manu ATF_TC(swapcontext2);
    116      1.1  manu ATF_TC_HEAD(swapcontext2, tc)
    117      1.1  manu {
    118      1.1  manu 	atf_tc_set_md_var(tc, "descr", "Testing if swapcontext() can "
    119      1.1  manu 	    "modify TLS pointer");
    120      1.1  manu }
    121      1.1  manu ATF_TC_BODY(swapcontext2, tc)
    122      1.1  manu {
    123      1.1  manu 	alter_tlsbase = 1;
    124      1.1  manu 	mainfunc();
    125      1.1  manu }
    126      1.1  manu 
    127      1.1  manu ATF_TP_ADD_TCS(tp)
    128      1.1  manu {
    129      1.1  manu 	ATF_TP_ADD_TC(tp, swapcontext1);
    130      1.1  manu 	ATF_TP_ADD_TC(tp, swapcontext2);
    131      1.1  manu 
    132      1.1  manu 	return atf_no_error();
    133      1.1  manu }
    134