t_swapcontext.c revision 1.1
11.1Smanu/* $NetBSD: t_swapcontext.c,v 1.1 2012/09/12 02:00:55 manu 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.1Smanu__RCSID("$NetBSD");
301.1Smanu
311.1Smanu#include <pthread.h>
321.1Smanu#include <ucontext.h>
331.1Smanu#include <stdio.h>
341.1Smanu#include <stdlib.h>
351.1Smanu
361.1Smanu#include <atf-c.h>
371.1Smanu
381.1Smanu#include "h_common.h"
391.1Smanu
401.1Smanu#define STACKSIZE 65536
411.1Smanu
421.1Smanuchar stack[STACKSIZE];
431.1Smanuucontext_t nctx;
441.1Smanuucontext_t octx;
451.1Smanuvoid *oself;
461.1Smanuvoid *nself;
471.1Smanuint val1, val2;
481.1Smanu
491.1Smanu/* ARGSUSED0 */
501.1Smanustatic void
511.1Smanuswapfunc(void *arg)
521.1Smanu{
531.1Smanu	/*
541.1Smanu	 * If the test fails, we are very likely to crash
551.1Smanu	 * without the opportunity to report
561.1Smanu	 */
571.1Smanu	nself = (void *)pthread_self();
581.1Smanu	printf("after swapcontext self = %p\n", nself);
591.1Smanu
601.1Smanu	ATF_REQUIRE_EQ(oself, nself);
611.1Smanu	printf("Test succeeded\n");
621.1Smanu
631.1Smanu	/* NOTREACHED */
641.1Smanu	return;
651.1Smanu}
661.1Smanu
671.1Smanu/* ARGSUSED0 */
681.1Smanustatic void *
691.1Smanuthreadfunc(void *arg)
701.1Smanu{
711.1Smanu	nctx.uc_stack.ss_sp = stack;
721.1Smanu	nctx.uc_stack.ss_size = sizeof(stack);
731.1Smanu
741.1Smanu	makecontext(&nctx, (void *)*swapfunc, 0);
751.1Smanu
761.1Smanu	oself = (void *)pthread_self();
771.1Smanu	printf("before swapcontext self = %p\n", oself);
781.1Smanu	PTHREAD_REQUIRE(swapcontext(&octx, &nctx));
791.1Smanu
801.1Smanu	/* NOTREACHED */
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.1Smanu	PTHREAD_REQUIRE(getcontext(&nctx));
1011.1Smanu	PTHREAD_REQUIRE(pthread_create(&thread, NULL, threadfunc, NULL));
1021.1Smanu
1031.1Smanu	return;
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