Home | History | Annotate | Line # | Download | only in sys
      1  1.5  thorpej /* $NetBSD: t_swapcontext.c,v 1.5 2024/05/27 22:03:21 thorpej 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.5  thorpej __RCSID("$NetBSD: t_swapcontext.c,v 1.5 2024/05/27 22:03:21 thorpej Exp $");
     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 	ATF_REQUIRE(nctx.uc_flags & _UC_TLSBASE);
     85  1.1     manu 	if (!alter_tlsbase)
     86  1.1     manu 		nctx.uc_flags &= ~_UC_TLSBASE;
     87  1.3    skrll 
     88  1.2    skrll 	makecontext(&nctx, swapfunc, 0);
     89  1.3    skrll 
     90  1.1     manu 	_lwp_setprivate(&val2);
     91  1.1     manu 	otls = _lwp_getprivate();
     92  1.1     manu 	printf("before swapcontext TLS pointer = %p\n", otls);
     93  1.1     manu 	ATF_REQUIRE(swapcontext(&octx, &nctx) == 0);
     94  1.1     manu 
     95  1.1     manu 	printf("Test completed\n");
     96  1.1     manu }
     97  1.1     manu 
     98  1.1     manu 
     99  1.1     manu ATF_TC(swapcontext1);
    100  1.1     manu ATF_TC_HEAD(swapcontext1, tc)
    101  1.1     manu {
    102  1.1     manu 	atf_tc_set_md_var(tc, "descr", "Testing if swapcontext() can let "
    103  1.1     manu 	    "TLS pointer untouched");
    104  1.1     manu }
    105  1.1     manu ATF_TC_BODY(swapcontext1, tc)
    106  1.1     manu {
    107  1.5  thorpej #ifdef __vax__
    108  1.5  thorpej 	atf_tc_expect_fail("PR port-vax/58290");
    109  1.5  thorpej #endif
    110  1.1     manu 	alter_tlsbase = 0;
    111  1.1     manu 	mainfunc();
    112  1.1     manu }
    113  1.1     manu 
    114  1.1     manu ATF_TC(swapcontext2);
    115  1.1     manu ATF_TC_HEAD(swapcontext2, tc)
    116  1.1     manu {
    117  1.1     manu 	atf_tc_set_md_var(tc, "descr", "Testing if swapcontext() can "
    118  1.1     manu 	    "modify TLS pointer");
    119  1.1     manu }
    120  1.1     manu ATF_TC_BODY(swapcontext2, tc)
    121  1.1     manu {
    122  1.5  thorpej #ifdef __vax__
    123  1.5  thorpej 	atf_tc_expect_fail("PR port-vax/58290");
    124  1.5  thorpej #endif
    125  1.1     manu 	alter_tlsbase = 1;
    126  1.1     manu 	mainfunc();
    127  1.1     manu }
    128  1.1     manu 
    129  1.1     manu ATF_TP_ADD_TCS(tp)
    130  1.1     manu {
    131  1.1     manu 	ATF_TP_ADD_TC(tp, swapcontext1);
    132  1.1     manu 	ATF_TP_ADD_TC(tp, swapcontext2);
    133  1.1     manu 
    134  1.1     manu 	return atf_no_error();
    135  1.1     manu }
    136