11.3Sjoerg/*	$NetBSD: t_tls_dlopen.c,v 1.3 2012/01/17 20:34:57 joerg Exp $	*/
21.1Sjoerg/*-
31.1Sjoerg * Copyright (c) 2011 The NetBSD Foundation, Inc.
41.1Sjoerg * All rights reserved.
51.1Sjoerg *
61.1Sjoerg * This code is derived from software contributed to The NetBSD Foundation
71.1Sjoerg * by Joerg Sonnenberger.
81.1Sjoerg *
91.1Sjoerg * Redistribution and use in source and binary forms, with or without
101.1Sjoerg * modification, are permitted provided that the following conditions
111.1Sjoerg * are met:
121.1Sjoerg *
131.1Sjoerg * 1. Redistributions of source code must retain the above copyright
141.1Sjoerg *    notice, this list of conditions and the following disclaimer.
151.1Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
161.1Sjoerg *    notice, this list of conditions and the following disclaimer in
171.1Sjoerg *    the documentation and/or other materials provided with the
181.1Sjoerg *    distribution.
191.1Sjoerg *
201.1Sjoerg * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
211.1Sjoerg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221.1Sjoerg * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231.1Sjoerg * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
241.1Sjoerg * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251.1Sjoerg * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
261.1Sjoerg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
271.1Sjoerg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281.1Sjoerg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291.1Sjoerg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
301.1Sjoerg * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311.1Sjoerg * SUCH DAMAGE.
321.1Sjoerg */
331.1Sjoerg
341.1Sjoerg#include <sys/cdefs.h>
351.3Sjoerg__RCSID("$NetBSD: t_tls_dlopen.c,v 1.3 2012/01/17 20:34:57 joerg Exp $");
361.1Sjoerg
371.1Sjoerg#include <atf-c.h>
381.1Sjoerg#include <dlfcn.h>
391.1Sjoerg#include <pthread.h>
401.2Sjoerg#include <unistd.h>
411.1Sjoerg
421.1Sjoerg#include <sys/tls.h>
431.1Sjoerg
441.3Sjoerg#ifdef __HAVE_NO___THREAD
451.1Sjoerg#define	__thread
461.1Sjoerg#endif
471.1Sjoerg
481.1SjoergATF_TC(t_tls_dlopen);
491.1Sjoerg
501.1SjoergATF_TC_HEAD(t_tls_dlopen, tc)
511.1Sjoerg{
521.1Sjoerg	atf_tc_set_md_var(tc, "descr",
531.1Sjoerg	    "Test (un)initialized TLS variables and dlopen");
541.1Sjoerg}
551.1Sjoerg
561.1Sjoergvoid (*testf_helper)(int, int);
571.1Sjoerg
581.1Sjoerg__thread int var1 = 1;
591.1Sjoerg__thread int var2;
601.2Sjoerg__thread int *var3 = &optind;
611.2Sjoergint var4_helper;
621.2Sjoerg__thread int *var4 = &var4_helper;
631.1Sjoerg
641.1Sjoergstatic void *
651.1Sjoergtestf(void *dummy)
661.1Sjoerg{
671.1Sjoerg	ATF_CHECK_EQ(var1, 1);
681.1Sjoerg	ATF_CHECK_EQ(var2, 0);
691.2Sjoerg	ATF_CHECK_EQ(var3, &optind);
701.2Sjoerg	ATF_CHECK_EQ(var4, &var4_helper);
711.1Sjoerg	testf_helper(2, 2);
721.1Sjoerg	ATF_CHECK_EQ(var1, 2);
731.1Sjoerg	ATF_CHECK_EQ(var2, 2);
741.1Sjoerg	testf_helper(3, 3);
751.1Sjoerg	ATF_CHECK_EQ(var1, 3);
761.1Sjoerg	ATF_CHECK_EQ(var2, 3);
771.2Sjoerg	ATF_CHECK_EQ(var3, &optind);
781.1Sjoerg
791.1Sjoerg	return NULL;
801.1Sjoerg}
811.1Sjoerg
821.1SjoergATF_TC_BODY(t_tls_dlopen, tc)
831.1Sjoerg{
841.1Sjoerg	void *handle;
851.1Sjoerg	pthread_t t;
861.1Sjoerg
871.3Sjoerg#ifdef __HAVE_NO___THREAD
881.1Sjoerg	atf_tc_skip("no TLS support on this platform");
891.1Sjoerg#endif
901.1Sjoerg
911.1Sjoerg	handle = dlopen("h_tls_dlopen.so", RTLD_NOW | RTLD_LOCAL);
921.1Sjoerg	ATF_REQUIRE(handle != NULL);
931.1Sjoerg
941.1Sjoerg	testf_helper = dlsym(handle, "testf_dso_helper");
951.1Sjoerg	ATF_REQUIRE(testf_helper != NULL);
961.1Sjoerg
971.1Sjoerg	testf(NULL);
981.1Sjoerg
991.1Sjoerg	pthread_create(&t, 0, testf, 0);
1001.1Sjoerg	pthread_join(t, NULL);
1011.1Sjoerg
1021.1Sjoerg	pthread_create(&t, 0, testf, 0);
1031.1Sjoerg	pthread_join(t, NULL);
1041.1Sjoerg
1051.1Sjoerg	dlclose(handle);
1061.1Sjoerg}
1071.1Sjoerg
1081.1SjoergATF_TP_ADD_TCS(tp)
1091.1Sjoerg{
1101.1Sjoerg	ATF_TP_ADD_TC(tp, t_tls_dlopen);
1111.1Sjoerg
1121.1Sjoerg	return atf_no_error();
1131.1Sjoerg}
114