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