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