1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_swapcontext.c,v 1.2 2014/08/25 16:31:15 bouyer Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*
411be35a1SLionel Sambuc * Copyright (c) 2012 Emmanuel Dreyfus. All rights reserved.
511be35a1SLionel Sambuc *
611be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
711be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
811be35a1SLionel Sambuc * are met:
911be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1011be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1111be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1211be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1311be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1411be35a1SLionel Sambuc *
1511be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1611be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1711be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1811be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1911be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2011be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2111be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2211be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2311be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2411be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2511be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
2611be35a1SLionel Sambuc */
2711be35a1SLionel Sambuc
2811be35a1SLionel Sambuc #include <sys/cdefs.h>
2911be35a1SLionel Sambuc __RCSID("$NetBSD");
3011be35a1SLionel Sambuc
3111be35a1SLionel Sambuc #include <pthread.h>
3211be35a1SLionel Sambuc #include <ucontext.h>
3311be35a1SLionel Sambuc #include <stdio.h>
3411be35a1SLionel Sambuc #include <stdlib.h>
3511be35a1SLionel Sambuc
3611be35a1SLionel Sambuc #include <atf-c.h>
3711be35a1SLionel Sambuc
3811be35a1SLionel Sambuc #include "h_common.h"
3911be35a1SLionel Sambuc
4011be35a1SLionel Sambuc #define STACKSIZE 65536
4111be35a1SLionel Sambuc
4211be35a1SLionel Sambuc char stack[STACKSIZE];
4311be35a1SLionel Sambuc ucontext_t nctx;
4411be35a1SLionel Sambuc ucontext_t octx;
4511be35a1SLionel Sambuc void *oself;
4611be35a1SLionel Sambuc void *nself;
4711be35a1SLionel Sambuc int val1, val2;
4811be35a1SLionel Sambuc
4911be35a1SLionel Sambuc /* ARGSUSED0 */
5011be35a1SLionel Sambuc static void
swapfunc(void * arg)5111be35a1SLionel Sambuc swapfunc(void *arg)
5211be35a1SLionel Sambuc {
5311be35a1SLionel Sambuc /*
5411be35a1SLionel Sambuc * If the test fails, we are very likely to crash
5511be35a1SLionel Sambuc * without the opportunity to report
5611be35a1SLionel Sambuc */
5711be35a1SLionel Sambuc nself = (void *)pthread_self();
5811be35a1SLionel Sambuc printf("after swapcontext self = %p\n", nself);
5911be35a1SLionel Sambuc
6011be35a1SLionel Sambuc ATF_REQUIRE_EQ(oself, nself);
6111be35a1SLionel Sambuc printf("Test succeeded\n");
62*0a6a1f1dSLionel Sambuc /* Go back in main */
63*0a6a1f1dSLionel Sambuc ATF_REQUIRE(swapcontext(&nctx, &octx));
6411be35a1SLionel Sambuc
6511be35a1SLionel Sambuc /* NOTREACHED */
6611be35a1SLionel Sambuc return;
6711be35a1SLionel Sambuc }
6811be35a1SLionel Sambuc
6911be35a1SLionel Sambuc /* ARGSUSED0 */
7011be35a1SLionel Sambuc static void *
threadfunc(void * arg)7111be35a1SLionel Sambuc threadfunc(void *arg)
7211be35a1SLionel Sambuc {
7311be35a1SLionel Sambuc nctx.uc_stack.ss_sp = stack;
7411be35a1SLionel Sambuc nctx.uc_stack.ss_size = sizeof(stack);
7511be35a1SLionel Sambuc
7611be35a1SLionel Sambuc makecontext(&nctx, (void *)*swapfunc, 0);
7711be35a1SLionel Sambuc
7811be35a1SLionel Sambuc oself = (void *)pthread_self();
7911be35a1SLionel Sambuc printf("before swapcontext self = %p\n", oself);
8011be35a1SLionel Sambuc PTHREAD_REQUIRE(swapcontext(&octx, &nctx));
8111be35a1SLionel Sambuc
8211be35a1SLionel Sambuc /* NOTREACHED */
8311be35a1SLionel Sambuc return NULL;
8411be35a1SLionel Sambuc }
8511be35a1SLionel Sambuc
8611be35a1SLionel Sambuc
8711be35a1SLionel Sambuc ATF_TC(swapcontext1);
ATF_TC_HEAD(swapcontext1,tc)8811be35a1SLionel Sambuc ATF_TC_HEAD(swapcontext1, tc)
8911be35a1SLionel Sambuc {
9011be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Testing if swapcontext() "
9111be35a1SLionel Sambuc "alters pthread_self()");
9211be35a1SLionel Sambuc }
ATF_TC_BODY(swapcontext1,tc)9311be35a1SLionel Sambuc ATF_TC_BODY(swapcontext1, tc)
9411be35a1SLionel Sambuc {
9511be35a1SLionel Sambuc pthread_t thread;
9611be35a1SLionel Sambuc
9711be35a1SLionel Sambuc oself = (void *)&val1;
9811be35a1SLionel Sambuc nself = (void *)&val2;
9911be35a1SLionel Sambuc
10011be35a1SLionel Sambuc printf("Testing if swapcontext() alters pthread_self()\n");
10111be35a1SLionel Sambuc
10211be35a1SLionel Sambuc PTHREAD_REQUIRE(getcontext(&nctx));
10311be35a1SLionel Sambuc PTHREAD_REQUIRE(pthread_create(&thread, NULL, threadfunc, NULL));
104*0a6a1f1dSLionel Sambuc PTHREAD_REQUIRE(pthread_join(thread, NULL));
10511be35a1SLionel Sambuc }
10611be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)10711be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
10811be35a1SLionel Sambuc {
10911be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, swapcontext1);
11011be35a1SLionel Sambuc
11111be35a1SLionel Sambuc return atf_no_error();
11211be35a1SLionel Sambuc }
113