1*11be35a1SLionel Sambuc /* $NetBSD: t_tls_dlopen.c,v 1.3 2012/01/17 20:34:57 joerg Exp $ */
2*11be35a1SLionel Sambuc /*-
3*11be35a1SLionel Sambuc * Copyright (c) 2011 The NetBSD Foundation, Inc.
4*11be35a1SLionel Sambuc * All rights reserved.
5*11be35a1SLionel Sambuc *
6*11be35a1SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
7*11be35a1SLionel Sambuc * by Joerg Sonnenberger.
8*11be35a1SLionel Sambuc *
9*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
10*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
11*11be35a1SLionel Sambuc * are met:
12*11be35a1SLionel Sambuc *
13*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in
17*11be35a1SLionel Sambuc * the documentation and/or other materials provided with the
18*11be35a1SLionel Sambuc * distribution.
19*11be35a1SLionel Sambuc *
20*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*11be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*11be35a1SLionel Sambuc * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*11be35a1SLionel Sambuc * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*11be35a1SLionel Sambuc * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*11be35a1SLionel Sambuc * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*11be35a1SLionel Sambuc * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27*11be35a1SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*11be35a1SLionel Sambuc * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*11be35a1SLionel Sambuc * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30*11be35a1SLionel Sambuc * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*11be35a1SLionel Sambuc * SUCH DAMAGE.
32*11be35a1SLionel Sambuc */
33*11be35a1SLionel Sambuc
34*11be35a1SLionel Sambuc #include <sys/cdefs.h>
35*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_tls_dlopen.c,v 1.3 2012/01/17 20:34:57 joerg Exp $");
36*11be35a1SLionel Sambuc
37*11be35a1SLionel Sambuc #include <atf-c.h>
38*11be35a1SLionel Sambuc #include <dlfcn.h>
39*11be35a1SLionel Sambuc #include <pthread.h>
40*11be35a1SLionel Sambuc #include <unistd.h>
41*11be35a1SLionel Sambuc
42*11be35a1SLionel Sambuc #include <sys/tls.h>
43*11be35a1SLionel Sambuc
44*11be35a1SLionel Sambuc #ifdef __HAVE_NO___THREAD
45*11be35a1SLionel Sambuc #define __thread
46*11be35a1SLionel Sambuc #endif
47*11be35a1SLionel Sambuc
48*11be35a1SLionel Sambuc ATF_TC(t_tls_dlopen);
49*11be35a1SLionel Sambuc
ATF_TC_HEAD(t_tls_dlopen,tc)50*11be35a1SLionel Sambuc ATF_TC_HEAD(t_tls_dlopen, tc)
51*11be35a1SLionel Sambuc {
52*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
53*11be35a1SLionel Sambuc "Test (un)initialized TLS variables and dlopen");
54*11be35a1SLionel Sambuc }
55*11be35a1SLionel Sambuc
56*11be35a1SLionel Sambuc void (*testf_helper)(int, int);
57*11be35a1SLionel Sambuc
58*11be35a1SLionel Sambuc __thread int var1 = 1;
59*11be35a1SLionel Sambuc __thread int var2;
60*11be35a1SLionel Sambuc __thread int *var3 = &optind;
61*11be35a1SLionel Sambuc int var4_helper;
62*11be35a1SLionel Sambuc __thread int *var4 = &var4_helper;
63*11be35a1SLionel Sambuc
64*11be35a1SLionel Sambuc static void *
testf(void * dummy)65*11be35a1SLionel Sambuc testf(void *dummy)
66*11be35a1SLionel Sambuc {
67*11be35a1SLionel Sambuc ATF_CHECK_EQ(var1, 1);
68*11be35a1SLionel Sambuc ATF_CHECK_EQ(var2, 0);
69*11be35a1SLionel Sambuc ATF_CHECK_EQ(var3, &optind);
70*11be35a1SLionel Sambuc ATF_CHECK_EQ(var4, &var4_helper);
71*11be35a1SLionel Sambuc testf_helper(2, 2);
72*11be35a1SLionel Sambuc ATF_CHECK_EQ(var1, 2);
73*11be35a1SLionel Sambuc ATF_CHECK_EQ(var2, 2);
74*11be35a1SLionel Sambuc testf_helper(3, 3);
75*11be35a1SLionel Sambuc ATF_CHECK_EQ(var1, 3);
76*11be35a1SLionel Sambuc ATF_CHECK_EQ(var2, 3);
77*11be35a1SLionel Sambuc ATF_CHECK_EQ(var3, &optind);
78*11be35a1SLionel Sambuc
79*11be35a1SLionel Sambuc return NULL;
80*11be35a1SLionel Sambuc }
81*11be35a1SLionel Sambuc
ATF_TC_BODY(t_tls_dlopen,tc)82*11be35a1SLionel Sambuc ATF_TC_BODY(t_tls_dlopen, tc)
83*11be35a1SLionel Sambuc {
84*11be35a1SLionel Sambuc void *handle;
85*11be35a1SLionel Sambuc pthread_t t;
86*11be35a1SLionel Sambuc
87*11be35a1SLionel Sambuc #ifdef __HAVE_NO___THREAD
88*11be35a1SLionel Sambuc atf_tc_skip("no TLS support on this platform");
89*11be35a1SLionel Sambuc #endif
90*11be35a1SLionel Sambuc
91*11be35a1SLionel Sambuc handle = dlopen("h_tls_dlopen.so", RTLD_NOW | RTLD_LOCAL);
92*11be35a1SLionel Sambuc ATF_REQUIRE(handle != NULL);
93*11be35a1SLionel Sambuc
94*11be35a1SLionel Sambuc testf_helper = dlsym(handle, "testf_dso_helper");
95*11be35a1SLionel Sambuc ATF_REQUIRE(testf_helper != NULL);
96*11be35a1SLionel Sambuc
97*11be35a1SLionel Sambuc testf(NULL);
98*11be35a1SLionel Sambuc
99*11be35a1SLionel Sambuc pthread_create(&t, 0, testf, 0);
100*11be35a1SLionel Sambuc pthread_join(t, NULL);
101*11be35a1SLionel Sambuc
102*11be35a1SLionel Sambuc pthread_create(&t, 0, testf, 0);
103*11be35a1SLionel Sambuc pthread_join(t, NULL);
104*11be35a1SLionel Sambuc
105*11be35a1SLionel Sambuc dlclose(handle);
106*11be35a1SLionel Sambuc }
107*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)108*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
109*11be35a1SLionel Sambuc {
110*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, t_tls_dlopen);
111*11be35a1SLionel Sambuc
112*11be35a1SLionel Sambuc return atf_no_error();
113*11be35a1SLionel Sambuc }
114