t_tls_dynamic.c revision 276478
1185573Srwatson/*	$NetBSD: t_tls_dynamic.c,v 1.3 2012/01/17 20:34:57 joerg Exp $	*/
2181053Srwatson/*-
3146759Srwatson * Copyright (c) 2011 The NetBSD Foundation, Inc.
4146759Srwatson * All rights reserved.
5146759Srwatson *
6146759Srwatson * This code is derived from software contributed to The NetBSD Foundation
7146759Srwatson * by Joerg Sonnenberger.
8146759Srwatson *
9155191Srwatson * Redistribution and use in source and binary forms, with or without
10155191Srwatson * modification, are permitted provided that the following conditions
11155191Srwatson * are met:
12155191Srwatson *
13155191Srwatson * 1. Redistributions of source code must retain the above copyright
14181053Srwatson *    notice, this list of conditions and the following disclaimer.
15155191Srwatson * 2. Redistributions in binary form must reproduce the above copyright
16155191Srwatson *    notice, this list of conditions and the following disclaimer in
17146759Srwatson *    the documentation and/or other materials provided with the
18155191Srwatson *    distribution.
19155191Srwatson *
20155191Srwatson * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21155191Srwatson * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22155191Srwatson * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23155191Srwatson * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24155191Srwatson * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25155191Srwatson * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26155191Srwatson * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27155191Srwatson * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28155191Srwatson * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29189279Srwatson * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30146759Srwatson * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31146759Srwatson * SUCH DAMAGE.
32146759Srwatson */
33155191Srwatson
34155191Srwatson#include <sys/cdefs.h>
35146759Srwatson__RCSID("$NetBSD: t_tls_dynamic.c,v 1.3 2012/01/17 20:34:57 joerg Exp $");
36186647Srwatson
37186647Srwatson#include <atf-c.h>
38186647Srwatson#include <pthread.h>
39155191Srwatson#include <unistd.h>
40186647Srwatson
41155191Srwatson#ifdef __NetBSD__
42155191Srwatson#include <sys/tls.h>
43186647Srwatson#endif
44155191Srwatson
45155191Srwatson#ifdef __HAVE_NO___THREAD
46155191Srwatson#define	__thread
47161635Srwatson#endif
48161635Srwatson
49155191SrwatsonATF_TC(t_tls_dynamic);
50155191Srwatson
51146759SrwatsonATF_TC_HEAD(t_tls_dynamic, tc)
52155191Srwatson{
53155191Srwatson	atf_tc_set_md_var(tc, "descr",
54155191Srwatson	    "Test (un)initialized TLS variables in dynamic binaries");
55155191Srwatson}
56146759Srwatson
57155191Srwatsonvoid testf_dso_helper(int, int);
58155191Srwatson
59155191Srwatsonextern __thread int var1;
60161635Srwatsonextern __thread int var2;
61155191Srwatsonextern __thread pid_t (*dso_var1)(void);
62155191Srwatson
63162508Srwatson__thread int *var3 = &optind;
64162508Srwatsonint var4_helper;
65162508Srwatson__thread int *var4 = &var4_helper;
66162508Srwatson
67162508Srwatsonstatic void *
68186647Srwatsontestf(void *dummy)
69189279Srwatson{
70189279Srwatson	ATF_CHECK_EQ(var1, 1);
71189279Srwatson	ATF_CHECK_EQ(var2, 0);
72155191Srwatson	testf_dso_helper(2, 2);
73155191Srwatson	ATF_CHECK_EQ(var1, 2);
74162508Srwatson	ATF_CHECK_EQ(var2, 2);
75155191Srwatson	testf_dso_helper(3, 3);
76155191Srwatson	ATF_CHECK_EQ(var1, 3);
77162508Srwatson	ATF_CHECK_EQ(var2, 3);
78155191Srwatson	ATF_CHECK_EQ(var3, &optind);
79155191Srwatson	ATF_CHECK_EQ(var4, &var4_helper);
80155191Srwatson	ATF_CHECK_EQ(dso_var1, getpid);
81155191Srwatson
82186647Srwatson	return NULL;
83186647Srwatson}
84186647Srwatson
85155191SrwatsonATF_TC_BODY(t_tls_dynamic, tc)
86155191Srwatson{
87161635Srwatson	pthread_t t;
88155191Srwatson
89155191Srwatson#ifdef __HAVE_NO___THREAD
90155191Srwatson	atf_tc_skip("no TLS support on this platform");
91155191Srwatson#endif
92155191Srwatson
93155191Srwatson	testf(NULL);
94155191Srwatson
95155191Srwatson	pthread_create(&t, 0, testf, 0);
96155191Srwatson	pthread_join(t, NULL);
97155191Srwatson
98155191Srwatson	pthread_create(&t, 0, testf, 0);
99155191Srwatson	pthread_join(t, NULL);
100155191Srwatson}
101155191Srwatson
102155191SrwatsonATF_TP_ADD_TCS(tp)
103155191Srwatson{
104155191Srwatson	ATF_TP_ADD_TC(tp, t_tls_dynamic);
105155191Srwatson
106155191Srwatson	return atf_no_error();
107155191Srwatson}
108155191Srwatson