11.9Suwe/* $NetBSD: t_swapcontext.c,v 1.9 2018/02/28 21:36:50 uwe Exp $ */
21.1Smanu
31.1Smanu/*
41.1Smanu * Copyright (c) 2012 Emmanuel Dreyfus. All rights reserved.
51.1Smanu *
61.1Smanu * Redistribution and use in source and binary forms, with or without
71.1Smanu * modification, are permitted provided that the following conditions
81.1Smanu * are met:
91.1Smanu * 1. Redistributions of source code must retain the above copyright
101.1Smanu *    notice, this list of conditions and the following disclaimer.
111.1Smanu * 2. Redistributions in binary form must reproduce the above copyright
121.1Smanu *    notice, this list of conditions and the following disclaimer in the
131.1Smanu *    documentation and/or other materials provided with the distribution.
141.1Smanu *
151.1Smanu * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
161.1Smanu * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
171.1Smanu * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
181.1Smanu * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
191.1Smanu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
201.1Smanu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
211.1Smanu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
221.1Smanu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
231.1Smanu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
241.1Smanu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
251.1Smanu * POSSIBILITY OF SUCH DAMAGE.
261.1Smanu */
271.1Smanu
281.1Smanu#include <sys/cdefs.h>
291.9Suwe__RCSID("$NetBSD: t_swapcontext.c,v 1.9 2018/02/28 21:36:50 uwe Exp $");
301.1Smanu
311.3Schristos#include <sys/types.h>
321.3Schristos#include <errno.h>
331.1Smanu#include <pthread.h>
341.1Smanu#include <stdio.h>
351.1Smanu#include <stdlib.h>
361.3Schristos#include <string.h>
371.3Schristos#include <ucontext.h>
381.1Smanu
391.1Smanu#include <atf-c.h>
401.1Smanu
411.1Smanu#include "h_common.h"
421.1Smanu
431.1Smanu#define STACKSIZE 65536
441.1Smanu
451.1Smanuchar stack[STACKSIZE];
461.1Smanuucontext_t nctx;
471.1Smanuucontext_t octx;
481.1Smanuvoid *oself;
491.1Smanuvoid *nself;
501.1Smanuint val1, val2;
511.1Smanu
521.1Smanustatic void
531.6Suweswapfunc(void)
541.1Smanu{
551.1Smanu	/*
561.4Suwe	 * If the test fails, we are very likely to crash
571.1Smanu	 * without the opportunity to report
581.4Suwe	 */
591.1Smanu	nself = (void *)pthread_self();
601.1Smanu	printf("after swapcontext self = %p\n", nself);
611.1Smanu
621.1Smanu	ATF_REQUIRE_EQ(oself, nself);
631.1Smanu	printf("Test succeeded\n");
641.1Smanu}
651.1Smanu
661.1Smanu/* ARGSUSED0 */
671.1Smanustatic void *
681.1Smanuthreadfunc(void *arg)
691.1Smanu{
701.1Smanu	nctx.uc_stack.ss_sp = stack;
711.1Smanu	nctx.uc_stack.ss_size = sizeof(stack);
721.9Suwe	nctx.uc_link = &octx;
731.4Suwe
741.5Suwe	makecontext(&nctx, swapfunc, 0);
751.4Suwe
761.1Smanu	oself = (void *)pthread_self();
771.1Smanu	printf("before swapcontext self = %p\n", oself);
781.3Schristos	ATF_REQUIRE_MSG(swapcontext(&octx, &nctx) != -1, "swapcontext failed: %s",
791.3Schristos	    strerror(errno));
801.1Smanu
811.1Smanu	return NULL;
821.1Smanu}
831.1Smanu
841.1Smanu
851.1SmanuATF_TC(swapcontext1);
861.1SmanuATF_TC_HEAD(swapcontext1, tc)
871.1Smanu{
881.1Smanu	atf_tc_set_md_var(tc, "descr", "Testing if swapcontext() "
891.1Smanu	    "alters pthread_self()");
901.1Smanu}
911.1SmanuATF_TC_BODY(swapcontext1, tc)
921.1Smanu{
931.1Smanu	pthread_t thread;
941.1Smanu
951.1Smanu	oself = (void *)&val1;
961.1Smanu	nself = (void *)&val2;
971.1Smanu
981.1Smanu	printf("Testing if swapcontext() alters pthread_self()\n");
991.1Smanu
1001.3Schristos	ATF_REQUIRE_MSG(getcontext(&nctx) != -1, "getcontext failed: %s",
1011.3Schristos	    strerror(errno));
1021.1Smanu	PTHREAD_REQUIRE(pthread_create(&thread, NULL, threadfunc, NULL));
1031.2Sbouyer	PTHREAD_REQUIRE(pthread_join(thread, NULL));
1041.1Smanu}
1051.1Smanu
1061.1SmanuATF_TP_ADD_TCS(tp)
1071.1Smanu{
1081.1Smanu	ATF_TP_ADD_TC(tp, swapcontext1);
1091.1Smanu
1101.1Smanu	return atf_no_error();
1111.1Smanu}
112