xref: /freebsd/contrib/netbsd-tests/lib/libpthread/dlopen/t_dlopen.c (revision cd0d51baaa4509a1db83251a601d34404d20c990)
1 /*	$NetBSD: t_dlopen.c,v 1.1 2013/03/21 16:50:21 christos Exp $ */
2 /*-
3  * Copyright (c) 2013 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Emmanuel Dreyfus
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: t_dlopen.c,v 1.1 2013/03/21 16:50:21 christos Exp $");
36 
37 #include <atf-c.h>
38 #include <dlfcn.h>
39 #include <pthread.h>
40 #include <unistd.h>
41 
42 ATF_TC(dlopen);
43 
44 ATF_TC_HEAD(dlopen, tc)
45 {
46 	atf_tc_set_md_var(tc, "descr",
47 	    "Test if dlopen can load -lpthread DSO");
48 }
49 
50 #define DSO TESTDIR "/h_pthread_dlopen.so"
51 
52 ATF_TC_BODY(dlopen, tc)
53 {
54 	void *handle;
55 	int (*testf_dso_null)(void);
56 	handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL);
57 	ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror());
58 
59 	testf_dso_null = dlsym(handle, "testf_dso_null");
60 	ATF_REQUIRE_MSG(testf_dso_null != NULL, "dlsym fails: %s", dlerror());
61 
62 	ATF_REQUIRE(testf_dso_null() == 0xcafe);
63 
64 	ATF_REQUIRE(dlclose(handle) == 0);
65 }
66 
67 ATF_TC(dlopen_mutex);
68 
69 ATF_TC_HEAD(dlopen_mutex, tc)
70 {
71 	atf_tc_set_md_var(tc, "descr",
72 	    "Test if dlopen can load -lpthread DSO without breaking mutex");
73 }
74 
75 ATF_TC_BODY(dlopen_mutex, tc)
76 {
77 	pthread_mutex_t mtx;
78 	void *handle;
79 	int (*testf_dso_null)(void);
80 
81 	ATF_REQUIRE(pthread_mutex_init(&mtx, NULL) == 0);
82 	ATF_REQUIRE(pthread_mutex_lock(&mtx) == 0);
83 
84 	handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL);
85 	ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror());
86 
87 	testf_dso_null = dlsym(handle, "testf_dso_null");
88 	ATF_REQUIRE_MSG(testf_dso_null != NULL, "dlsym fails: %s", dlerror());
89 
90 	ATF_REQUIRE(testf_dso_null() == 0xcafe);
91 
92 	ATF_REQUIRE(pthread_mutex_unlock(&mtx) == 0);
93 
94 	ATF_REQUIRE(dlclose(handle) == 0);
95 
96 	pthread_mutex_destroy(&mtx);
97 }
98 
99 ATF_TC(dlopen_mutex_libc);
100 
101 ATF_TC_HEAD(dlopen_mutex_libc, tc)
102 {
103 	atf_tc_set_md_var(tc, "descr",
104 	    "Test if dlopen can load -lpthread DSO and use libc locked mutex");
105 }
106 
107 ATF_TC_BODY(dlopen_mutex_libc, tc)
108 {
109 	pthread_mutex_t mtx;
110 	void *handle;
111 	int (*testf_dso_mutex_unlock)(pthread_mutex_t *);
112 
113 	ATF_REQUIRE(pthread_mutex_init(&mtx, NULL) == 0);
114 	ATF_REQUIRE(pthread_mutex_lock(&mtx) == 0);
115 
116 	handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL);
117 	ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror());
118 
119 	testf_dso_mutex_unlock = dlsym(handle, "testf_dso_mutex_unlock");
120 	ATF_REQUIRE_MSG(testf_dso_mutex_unlock != NULL,
121 			"dlsym fails: %s", dlerror());
122 
123 	ATF_REQUIRE(testf_dso_mutex_unlock(&mtx) == 0xcafe);
124 
125 	dlclose(handle);
126 
127 	pthread_mutex_destroy(&mtx);
128 }
129 
130 ATF_TC(dlopen_mutex_libpthread);
131 
132 ATF_TC_HEAD(dlopen_mutex_libpthread, tc)
133 {
134 	atf_tc_set_md_var(tc, "descr",
135 	    "Test if dlopen can load -lpthread DSO and use "
136 	    "libpthread locked mutex");
137 }
138 
139 ATF_TC_BODY(dlopen_mutex_libpthread, tc)
140 {
141 	pthread_mutex_t mtx;
142 	void *handle;
143 	int (*testf_dso_mutex_lock)(pthread_mutex_t *);
144 
145 	ATF_REQUIRE(pthread_mutex_init(&mtx, NULL) == 0);
146 
147 	handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL);
148 	ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror());
149 
150 	testf_dso_mutex_lock = dlsym(handle, "testf_dso_mutex_lock");
151 	ATF_REQUIRE_MSG(testf_dso_mutex_lock != NULL,
152 			"dlsym fails: %s", dlerror());
153 
154 	ATF_REQUIRE(testf_dso_mutex_lock(&mtx) == 0xcafe);
155 
156 	ATF_REQUIRE(pthread_mutex_unlock(&mtx) == 0);
157 
158 	dlclose(handle);
159 
160 	pthread_mutex_destroy(&mtx);
161 }
162 
163 ATF_TP_ADD_TCS(tp)
164 {
165 	ATF_TP_ADD_TC(tp, dlopen);
166 	ATF_TP_ADD_TC(tp, dlopen_mutex);
167 	ATF_TP_ADD_TC(tp, dlopen_mutex_libc);
168 	ATF_TP_ADD_TC(tp, dlopen_mutex_libpthread);
169 
170 	return atf_no_error();
171 }
172