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