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