1 /* $NetBSD: t_swapcontext.c,v 1.3 2013/05/05 10:28:11 skrll Exp $ */
4 * Copyright (c) 2012 Emmanuel Dreyfus. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/cdefs.h>
38 #define STACKSIZE 65536
40 char stack
[STACKSIZE
];
52 ntls
= _lwp_getprivate();
53 printf("after swapcontext TLS pointer = %p\n", ntls
);
56 ATF_REQUIRE_EQ(ntls
, &val1
);
57 printf("TLS pointer modified by swapcontext()\n");
59 ATF_REQUIRE_EQ(ntls
, &val2
);
60 printf("TLS pointer left untouched by swapcontext()\n");
64 ATF_REQUIRE(swapcontext(&nctx
, &octx
));
73 printf("Testing if swapcontext() alters TLS pointer if _UC_TLSBASE "
74 "is %s\n", (alter_tlsbase
) ? "left set" : "cleared");
76 _lwp_setprivate(&val1
);
77 printf("before swapcontext TLS pointer = %p\n", &val1
);
79 ATF_REQUIRE(getcontext(&nctx
) == 0);
81 nctx
.uc_stack
.ss_sp
= stack
;
82 nctx
.uc_stack
.ss_size
= sizeof(stack
);
85 ATF_REQUIRE_MSG(0, "_UC_TLSBASE is not defined");
86 #else /* _UC_TLSBASE */
87 ATF_REQUIRE(nctx
.uc_flags
& _UC_TLSBASE
);
89 nctx
.uc_flags
&= ~_UC_TLSBASE
;
90 #endif /* _UC_TLSBASE */
92 makecontext(&nctx
, swapfunc
, 0);
94 _lwp_setprivate(&val2
);
95 otls
= _lwp_getprivate();
96 printf("before swapcontext TLS pointer = %p\n", otls
);
97 ATF_REQUIRE(swapcontext(&octx
, &nctx
) == 0);
99 printf("Test completed\n");
103 ATF_TC(swapcontext1
);
104 ATF_TC_HEAD(swapcontext1
, tc
)
106 atf_tc_set_md_var(tc
, "descr", "Testing if swapcontext() can let "
107 "TLS pointer untouched");
109 ATF_TC_BODY(swapcontext1
, tc
)
115 ATF_TC(swapcontext2
);
116 ATF_TC_HEAD(swapcontext2
, tc
)
118 atf_tc_set_md_var(tc
, "descr", "Testing if swapcontext() can "
119 "modify TLS pointer");
121 ATF_TC_BODY(swapcontext2
, tc
)
129 ATF_TP_ADD_TC(tp
, swapcontext1
);
130 ATF_TP_ADD_TC(tp
, swapcontext2
);
132 return atf_no_error();