t_ifunc.c revision 1.11 1 1.11 skrll /* $NetBSD: t_ifunc.c,v 1.11 2022/06/21 06:47:38 skrll Exp $ */
2 1.1 joerg
3 1.1 joerg /*
4 1.1 joerg * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 1.1 joerg * All rights reserved.
6 1.1 joerg *
7 1.1 joerg * Redistribution and use in source and binary forms, with or without
8 1.1 joerg * modification, are permitted provided that the following conditions
9 1.1 joerg * are met:
10 1.1 joerg * 1. Redistributions of source code must retain the above copyright
11 1.1 joerg * notice, this list of conditions and the following disclaimer.
12 1.1 joerg * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 joerg * notice, this list of conditions and the following disclaimer in the
14 1.1 joerg * documentation and/or other materials provided with the distribution.
15 1.1 joerg *
16 1.1 joerg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 1.1 joerg * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 1.1 joerg * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 1.1 joerg * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 joerg * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 1.1 joerg * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 joerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 1.1 joerg * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 joerg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 1.1 joerg * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 1.1 joerg * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 1.1 joerg * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 joerg */
29 1.1 joerg
30 1.1 joerg #include <sys/types.h>
31 1.1 joerg
32 1.1 joerg #include <atf-c.h>
33 1.1 joerg #include <dlfcn.h>
34 1.1 joerg #include <util.h>
35 1.1 joerg
36 1.2 christos #include "h_macros.h"
37 1.1 joerg
38 1.11 skrll #if \
39 1.11 skrll defined(__arm__) || \
40 1.11 skrll defined(__i386__) || \
41 1.11 skrll defined(__sparc__) || \
42 1.11 skrll defined(__powerpc__) || \
43 1.11 skrll defined(__x86_64__)
44 1.7 maya #define LINKER_SUPPORT 1
45 1.6 martin #else
46 1.7 maya #define LINKER_SUPPORT 0
47 1.6 martin #endif
48 1.6 martin
49 1.1 joerg ATF_TC(rtld_ifunc);
50 1.1 joerg
51 1.1 joerg ATF_TC_HEAD(rtld_ifunc, tc)
52 1.1 joerg {
53 1.1 joerg atf_tc_set_md_var(tc, "descr", "ifunc functions are resolved");
54 1.1 joerg }
55 1.1 joerg
56 1.1 joerg ATF_TC_BODY(rtld_ifunc, tc)
57 1.1 joerg {
58 1.1 joerg const char *envstr[] = {
59 1.1 joerg "0", "1"
60 1.1 joerg };
61 1.8 joerg long long expected_result[] = {
62 1.8 joerg 0xdeadbeefll, 0xbeefdeadll
63 1.1 joerg };
64 1.1 joerg void *handle;
65 1.8 joerg long long (*sym)(void);
66 1.8 joerg long long result;
67 1.1 joerg const char *error;
68 1.1 joerg size_t i;
69 1.1 joerg
70 1.7 maya if (!LINKER_SUPPORT)
71 1.7 maya atf_tc_skip("Missing linker support for ifunc relocations");
72 1.3 joerg
73 1.1 joerg for (i = 0; i < __arraycount(envstr); ++i) {
74 1.1 joerg setenv("USE_IFUNC2", envstr[i], 1);
75 1.1 joerg
76 1.1 joerg handle = dlopen("libh_helper_ifunc_dso.so", RTLD_LAZY);
77 1.1 joerg error = dlerror();
78 1.1 joerg ATF_CHECK(error == NULL);
79 1.1 joerg ATF_CHECK(handle != NULL);
80 1.1 joerg
81 1.1 joerg sym = dlsym(handle, "ifunc");
82 1.1 joerg error = dlerror();
83 1.1 joerg ATF_CHECK(error == NULL);
84 1.1 joerg ATF_CHECK(sym != NULL);
85 1.1 joerg
86 1.1 joerg result = (*sym)();
87 1.1 joerg ATF_CHECK(result == expected_result[i]);
88 1.1 joerg
89 1.1 joerg dlclose(handle);
90 1.1 joerg error = dlerror();
91 1.1 joerg ATF_CHECK(error == NULL);
92 1.1 joerg
93 1.1 joerg char *command;
94 1.8 joerg easprintf(&command, "%s/h_ifunc %lld",
95 1.1 joerg atf_tc_get_config_var(tc, "srcdir"), expected_result[i]);
96 1.1 joerg if (system(command) != EXIT_SUCCESS)
97 1.1 joerg atf_tc_fail("Test failed; see output for details");
98 1.1 joerg free(command);
99 1.1 joerg }
100 1.1 joerg }
101 1.1 joerg
102 1.3 joerg ATF_TC(rtld_hidden_ifunc);
103 1.3 joerg
104 1.3 joerg ATF_TC_HEAD(rtld_hidden_ifunc, tc)
105 1.3 joerg {
106 1.3 joerg atf_tc_set_md_var(tc, "descr", "hidden ifunc functions are resolved");
107 1.3 joerg }
108 1.3 joerg
109 1.3 joerg ATF_TC_BODY(rtld_hidden_ifunc, tc)
110 1.3 joerg {
111 1.3 joerg const char *envstr[] = {
112 1.3 joerg "0", "1"
113 1.3 joerg };
114 1.8 joerg long long expected_result[] = {
115 1.8 joerg 0xdeadbeefll, 0xbeefdeadll
116 1.3 joerg };
117 1.3 joerg void *handle;
118 1.8 joerg long long (*sym)(void);
119 1.8 joerg long long (*(*sym2)(void))(void);
120 1.8 joerg long long result;
121 1.3 joerg const char *error;
122 1.3 joerg size_t i;
123 1.3 joerg
124 1.7 maya if (!LINKER_SUPPORT)
125 1.7 maya atf_tc_skip("Missing linker support for ifunc relocations");
126 1.6 martin
127 1.3 joerg for (i = 0; i < __arraycount(envstr); ++i) {
128 1.3 joerg setenv("USE_IFUNC2", envstr[i], 1);
129 1.3 joerg
130 1.3 joerg handle = dlopen("libh_helper_ifunc_dso.so", RTLD_LAZY);
131 1.3 joerg error = dlerror();
132 1.3 joerg ATF_CHECK(error == NULL);
133 1.3 joerg ATF_CHECK(handle != NULL);
134 1.3 joerg
135 1.3 joerg sym = dlsym(handle, "ifunc_plt");
136 1.3 joerg error = dlerror();
137 1.3 joerg ATF_CHECK(error == NULL);
138 1.3 joerg ATF_CHECK(sym != NULL);
139 1.3 joerg
140 1.3 joerg result = (*sym)();
141 1.3 joerg ATF_CHECK(result == expected_result[!i]);
142 1.3 joerg
143 1.4 joerg sym2 = dlsym(handle, "ifunc_indirect");
144 1.4 joerg error = dlerror();
145 1.4 joerg ATF_CHECK(error == NULL);
146 1.4 joerg ATF_CHECK(sym2 != NULL);
147 1.4 joerg
148 1.4 joerg sym = (*sym2)();
149 1.4 joerg result = (*sym)();
150 1.4 joerg ATF_CHECK(result == expected_result[!i]);
151 1.4 joerg
152 1.3 joerg dlclose(handle);
153 1.3 joerg error = dlerror();
154 1.3 joerg ATF_CHECK(error == NULL);
155 1.3 joerg
156 1.3 joerg char *command;
157 1.8 joerg easprintf(&command, "%s/h_ifunc %lld",
158 1.3 joerg atf_tc_get_config_var(tc, "srcdir"), expected_result[i]);
159 1.3 joerg if (system(command) != EXIT_SUCCESS)
160 1.3 joerg atf_tc_fail("Test failed; see output for details");
161 1.3 joerg free(command);
162 1.3 joerg }
163 1.3 joerg }
164 1.3 joerg
165 1.5 joerg ATF_TC(rtld_main_ifunc);
166 1.5 joerg ATF_TC_HEAD(rtld_main_ifunc, tc)
167 1.5 joerg {
168 1.5 joerg atf_tc_set_md_var(tc, "descr",
169 1.5 joerg "ifunc functions are resolved in the executable");
170 1.5 joerg }
171 1.5 joerg
172 1.7 maya #if LINKER_SUPPORT
173 1.8 joerg static long long
174 1.5 joerg ifunc_helper(void)
175 1.5 joerg {
176 1.8 joerg return 0xdeadbeefll;
177 1.5 joerg }
178 1.5 joerg
179 1.5 joerg static __attribute__((used))
180 1.8 joerg long long (*resolve_ifunc(void))(void)
181 1.5 joerg {
182 1.5 joerg return ifunc_helper;
183 1.5 joerg }
184 1.5 joerg __hidden_ifunc(ifunc, resolve_ifunc);
185 1.7 maya #endif
186 1.8 joerg long long ifunc(void);
187 1.5 joerg
188 1.5 joerg ATF_TC_BODY(rtld_main_ifunc, tc)
189 1.5 joerg {
190 1.7 maya if (!LINKER_SUPPORT)
191 1.7 maya atf_tc_skip("Missing linker support for ifunc relocations");
192 1.8 joerg ATF_CHECK(ifunc() == 0xdeadbeefll);
193 1.5 joerg }
194 1.5 joerg
195 1.1 joerg ATF_TP_ADD_TCS(tp)
196 1.1 joerg {
197 1.1 joerg ATF_TP_ADD_TC(tp, rtld_ifunc);
198 1.3 joerg ATF_TP_ADD_TC(tp, rtld_hidden_ifunc);
199 1.5 joerg ATF_TP_ADD_TC(tp, rtld_main_ifunc);
200 1.9 maya return atf_no_error();
201 1.1 joerg }
202