1 | /*
|
---|
2 | * Copyright (c) 2016 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup dltest
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief Test dynamic linking
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <dlfcn.h>
|
---|
39 | #include <libdltest.h>
|
---|
40 | #include <stdbool.h>
|
---|
41 | #include <stdio.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 |
|
---|
44 | /** libdltest library handle */
|
---|
45 | void *handle;
|
---|
46 |
|
---|
47 |
|
---|
48 | /** Test dlsym() function */
|
---|
49 | static bool test_dlsym(void)
|
---|
50 | {
|
---|
51 | int (*p_dl_get_constant)(void);
|
---|
52 |
|
---|
53 | printf("dlsym()... ");
|
---|
54 | p_dl_get_constant = dlsym(handle, "dl_get_constant");
|
---|
55 | if (p_dl_get_constant == NULL) {
|
---|
56 | printf("FAILED\n");
|
---|
57 | return false;
|
---|
58 | }
|
---|
59 |
|
---|
60 | printf("Passed\n");
|
---|
61 | return true;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /** Test calling function that returns a constant */
|
---|
65 | static bool test_dlfcn_dl_get_constant(void)
|
---|
66 | {
|
---|
67 | int (*p_dl_get_constant)(void);
|
---|
68 | int val;
|
---|
69 |
|
---|
70 | printf("Call dlsym/dl_get_constant...\n");
|
---|
71 |
|
---|
72 | p_dl_get_constant = dlsym(handle, "dl_get_constant");
|
---|
73 | if (p_dl_get_constant == NULL) {
|
---|
74 | printf("FAILED\n");
|
---|
75 | return false;
|
---|
76 | }
|
---|
77 |
|
---|
78 | val = p_dl_get_constant();
|
---|
79 |
|
---|
80 | printf("Got %d, expected %d... ", val, dl_constant);
|
---|
81 | if (val != dl_constant) {
|
---|
82 | printf("FAILED\n");
|
---|
83 | return false;
|
---|
84 | }
|
---|
85 |
|
---|
86 | printf("Passed\n");
|
---|
87 | return true;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /** Test calling a function that returns contents of a private initialized
|
---|
91 | * variable.
|
---|
92 | */
|
---|
93 | static bool test_dlfcn_dl_get_private_var(void)
|
---|
94 | {
|
---|
95 | int (*p_dl_get_private_var)(void);
|
---|
96 | int val;
|
---|
97 |
|
---|
98 | printf("Call dlsym/dl_get_private_var...\n");
|
---|
99 |
|
---|
100 | p_dl_get_private_var = dlsym(handle, "dl_get_private_var");
|
---|
101 | if (p_dl_get_private_var == NULL) {
|
---|
102 | printf("FAILED\n");
|
---|
103 | return false;
|
---|
104 | }
|
---|
105 |
|
---|
106 | val = p_dl_get_private_var();
|
---|
107 |
|
---|
108 | printf("Got %d, expected %d... ", val, dl_private_var_val);
|
---|
109 | if (val != dl_private_var_val) {
|
---|
110 | printf("FAILED\n");
|
---|
111 | return false;
|
---|
112 | }
|
---|
113 |
|
---|
114 | printf("Passed\n");
|
---|
115 | return true;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /** Test calling a function that returns contents of a private uninitialized
|
---|
119 | * variable.
|
---|
120 | */
|
---|
121 | static bool test_dlfcn_dl_get_private_uvar(void)
|
---|
122 | {
|
---|
123 | int (*p_dl_get_private_uvar)(void);
|
---|
124 | int val;
|
---|
125 |
|
---|
126 | printf("Call dlsym/dl_get_private_uvar...\n");
|
---|
127 |
|
---|
128 | p_dl_get_private_uvar = dlsym(handle, "dl_get_private_uvar");
|
---|
129 | if (p_dl_get_private_uvar == NULL) {
|
---|
130 | printf("FAILED\n");
|
---|
131 | return false;
|
---|
132 | }
|
---|
133 |
|
---|
134 | val = p_dl_get_private_uvar();
|
---|
135 |
|
---|
136 | printf("Got %d, expected %d... ", val, 0);
|
---|
137 | if (val != 0) {
|
---|
138 | printf("FAILED\n");
|
---|
139 | return false;
|
---|
140 | }
|
---|
141 |
|
---|
142 | printf("Passed\n");
|
---|
143 | return true;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /** Test calling a function that returns the contents of a public initialized
|
---|
147 | * variable.
|
---|
148 | */
|
---|
149 | static bool test_dlfcn_dl_get_public_var(void)
|
---|
150 | {
|
---|
151 | int (*p_dl_get_public_var)(void);
|
---|
152 | int val;
|
---|
153 |
|
---|
154 | printf("Call dlsym/dl_get_public_var...\n");
|
---|
155 |
|
---|
156 | p_dl_get_public_var = dlsym(handle, "dl_get_public_var");
|
---|
157 | if (p_dl_get_public_var == NULL) {
|
---|
158 | printf("FAILED\n");
|
---|
159 | return false;
|
---|
160 | }
|
---|
161 |
|
---|
162 | val = p_dl_get_public_var();
|
---|
163 |
|
---|
164 | printf("Got %d, expected %d... ", val, dl_public_var_val);
|
---|
165 | if (val != dl_public_var_val) {
|
---|
166 | printf("FAILED\n");
|
---|
167 | return false;
|
---|
168 | }
|
---|
169 |
|
---|
170 | printf("Passed\n");
|
---|
171 | return true;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /** Test calling a function that returns the contents of a public uninitialized
|
---|
175 | * variable.
|
---|
176 | */
|
---|
177 | static bool test_dlfcn_dl_get_public_uvar(void)
|
---|
178 | {
|
---|
179 | int (*p_dl_get_public_uvar)(void);
|
---|
180 | int val;
|
---|
181 |
|
---|
182 | printf("Call dlsym/dl_get_public_uvar...\n");
|
---|
183 |
|
---|
184 | p_dl_get_public_uvar = dlsym(handle, "dl_get_public_uvar");
|
---|
185 | if (p_dl_get_public_uvar == NULL) {
|
---|
186 | printf("FAILED\n");
|
---|
187 | return false;
|
---|
188 | }
|
---|
189 |
|
---|
190 | val = p_dl_get_public_uvar();
|
---|
191 |
|
---|
192 | printf("Got %d, expected %d... ", val, 0);
|
---|
193 | if (val != 0) {
|
---|
194 | printf("FAILED\n");
|
---|
195 | return false;
|
---|
196 | }
|
---|
197 |
|
---|
198 | printf("Passed\n");
|
---|
199 | return true;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /** Test directly reading a public initialized variable whose address was
|
---|
203 | * obtained using dlsym.
|
---|
204 | */
|
---|
205 | static bool test_dlfcn_read_public_var(void)
|
---|
206 | {
|
---|
207 | int *p_dl_public_var;
|
---|
208 | int val;
|
---|
209 |
|
---|
210 | printf("Read dlsym/dl_public_var...\n");
|
---|
211 |
|
---|
212 | p_dl_public_var = dlsym(handle, "dl_public_var");
|
---|
213 | if (p_dl_public_var == NULL) {
|
---|
214 | printf("FAILED\n");
|
---|
215 | return false;
|
---|
216 | }
|
---|
217 |
|
---|
218 | val = *p_dl_public_var;
|
---|
219 |
|
---|
220 | printf("Got %d, expected %d... ", val, dl_public_var_val);
|
---|
221 | if (val != dl_public_var_val) {
|
---|
222 | printf("FAILED\n");
|
---|
223 | return false;
|
---|
224 | }
|
---|
225 |
|
---|
226 | printf("Passed\n");
|
---|
227 | return true;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /** Test directly reading a public uninitialized variable whose address was
|
---|
231 | * obtained using dlsym.
|
---|
232 | */
|
---|
233 | static bool test_dlfcn_read_public_uvar(void)
|
---|
234 | {
|
---|
235 | int *p_dl_public_uvar;
|
---|
236 | int val;
|
---|
237 |
|
---|
238 | printf("Read dlsym/dl_public_uvar...\n");
|
---|
239 |
|
---|
240 | p_dl_public_uvar = dlsym(handle, "dl_public_uvar");
|
---|
241 | if (p_dl_public_uvar == NULL) {
|
---|
242 | printf("FAILED\n");
|
---|
243 | return false;
|
---|
244 | }
|
---|
245 |
|
---|
246 | val = *p_dl_public_uvar;
|
---|
247 |
|
---|
248 | printf("Got %d, expected %d... ", val, 0);
|
---|
249 | if (val != 0) {
|
---|
250 | printf("FAILED\n");
|
---|
251 | return false;
|
---|
252 | }
|
---|
253 |
|
---|
254 | printf("Passed\n");
|
---|
255 | return true;
|
---|
256 | }
|
---|
257 |
|
---|
258 | #ifdef DLTEST_LINKED
|
---|
259 |
|
---|
260 | /** Test directly calling function that returns a constant */
|
---|
261 | static bool test_lnk_dl_get_constant(void)
|
---|
262 | {
|
---|
263 | int val;
|
---|
264 |
|
---|
265 | printf("Call linked dl_get_constant...\n");
|
---|
266 |
|
---|
267 | val = dl_get_constant();
|
---|
268 |
|
---|
269 | printf("Got %d, expected %d... ", val, dl_constant);
|
---|
270 | if (val != dl_constant) {
|
---|
271 | printf("FAILED\n");
|
---|
272 | return false;
|
---|
273 | }
|
---|
274 |
|
---|
275 | printf("Passed\n");
|
---|
276 | return true;
|
---|
277 | }
|
---|
278 |
|
---|
279 | /** Test dircetly calling a function that returns contents of a private
|
---|
280 | * initialized variable.
|
---|
281 | */
|
---|
282 | static bool test_lnk_dl_get_private_var(void)
|
---|
283 | {
|
---|
284 | int val;
|
---|
285 |
|
---|
286 | printf("Call linked dl_get_private_var...\n");
|
---|
287 |
|
---|
288 | val = dl_get_private_var();
|
---|
289 |
|
---|
290 | printf("Got %d, expected %d... ", val, dl_private_var_val);
|
---|
291 | if (val != dl_private_var_val) {
|
---|
292 | printf("FAILED\n");
|
---|
293 | return false;
|
---|
294 | }
|
---|
295 |
|
---|
296 | printf("Passed\n");
|
---|
297 | return true;
|
---|
298 | }
|
---|
299 |
|
---|
300 | /** Test dircetly calling a function that returns contents of a private
|
---|
301 | * uninitialized variable.
|
---|
302 | */
|
---|
303 | static bool test_lnk_dl_get_private_uvar(void)
|
---|
304 | {
|
---|
305 | int val;
|
---|
306 |
|
---|
307 | printf("Call linked dl_get_private_uvar...\n");
|
---|
308 |
|
---|
309 | val = dl_get_private_uvar();
|
---|
310 |
|
---|
311 | printf("Got %d, expected %d... ", val, 0);
|
---|
312 | if (val != 0) {
|
---|
313 | printf("FAILED\n");
|
---|
314 | return false;
|
---|
315 | }
|
---|
316 |
|
---|
317 | printf("Passed\n");
|
---|
318 | return true;
|
---|
319 | }
|
---|
320 |
|
---|
321 | /** Test directly calling a function that returns the contents of a public
|
---|
322 | * initialized variable.
|
---|
323 | */
|
---|
324 | static bool test_lnk_dl_get_public_var(void)
|
---|
325 | {
|
---|
326 | int val;
|
---|
327 |
|
---|
328 | printf("Call linked dl_get_public_var...\n");
|
---|
329 |
|
---|
330 | val = dl_get_public_var();
|
---|
331 |
|
---|
332 | printf("Got %d, expected %d... ", val, dl_public_var_val);
|
---|
333 | if (val != dl_public_var_val) {
|
---|
334 | printf("FAILED\n");
|
---|
335 | return false;
|
---|
336 | }
|
---|
337 |
|
---|
338 | printf("Passed\n");
|
---|
339 | return true;
|
---|
340 | }
|
---|
341 |
|
---|
342 | /** Test directly calling a function that returns the contents of a public
|
---|
343 | * uninitialized variable.
|
---|
344 | */
|
---|
345 | static bool test_lnk_dl_get_public_uvar(void)
|
---|
346 | {
|
---|
347 | int val;
|
---|
348 |
|
---|
349 | printf("Call linked dl_get_public_uvar...\n");
|
---|
350 |
|
---|
351 | val = dl_get_public_uvar();
|
---|
352 |
|
---|
353 | printf("Got %d, expected %d... ", val, 0);
|
---|
354 | if (val != 0) {
|
---|
355 | printf("FAILED\n");
|
---|
356 | return false;
|
---|
357 | }
|
---|
358 |
|
---|
359 | printf("Passed\n");
|
---|
360 | return true;
|
---|
361 | }
|
---|
362 |
|
---|
363 | /** Test directly reading a public initialized variable. */
|
---|
364 | static bool test_lnk_read_public_var(void)
|
---|
365 | {
|
---|
366 | int val;
|
---|
367 |
|
---|
368 | printf("Read linked dl_public_var...\n");
|
---|
369 |
|
---|
370 | val = dl_public_var;
|
---|
371 |
|
---|
372 | printf("Got %d, expected %d... ", val, dl_public_var_val);
|
---|
373 | if (val != dl_public_var_val) {
|
---|
374 | printf("FAILED\n");
|
---|
375 | return false;
|
---|
376 | }
|
---|
377 |
|
---|
378 | printf("Passed\n");
|
---|
379 | return true;
|
---|
380 | }
|
---|
381 |
|
---|
382 | /** Test directly reading a public uninitialized variable. */
|
---|
383 | static bool test_lnk_read_public_uvar(void)
|
---|
384 | {
|
---|
385 | int val;
|
---|
386 |
|
---|
387 | printf("Read linked dl_public_uvar...\n");
|
---|
388 |
|
---|
389 | val = dl_public_uvar;
|
---|
390 |
|
---|
391 | printf("Got %d, expected %d... ", val, 0);
|
---|
392 | if (val != 0) {
|
---|
393 | printf("FAILED\n");
|
---|
394 | return false;
|
---|
395 | }
|
---|
396 |
|
---|
397 | printf("Passed\n");
|
---|
398 | return true;
|
---|
399 | }
|
---|
400 |
|
---|
401 | #endif
|
---|
402 |
|
---|
403 | int main(int argc, char *argv[])
|
---|
404 | {
|
---|
405 |
|
---|
406 | printf("Dynamic linking test\n");
|
---|
407 |
|
---|
408 | printf("dlopen()... ");
|
---|
409 | handle = dlopen("libdltest.so.0", 0);
|
---|
410 | if (handle == NULL) {
|
---|
411 | printf("FAILED\n");
|
---|
412 | return 1;
|
---|
413 | }
|
---|
414 |
|
---|
415 | printf("Passed\n");
|
---|
416 |
|
---|
417 | if (!test_dlsym())
|
---|
418 | return 1;
|
---|
419 |
|
---|
420 | if (!test_dlfcn_dl_get_constant())
|
---|
421 | return 1;
|
---|
422 |
|
---|
423 | if (!test_dlfcn_dl_get_private_var())
|
---|
424 | return 1;
|
---|
425 |
|
---|
426 | if (!test_dlfcn_dl_get_private_uvar())
|
---|
427 | return 1;
|
---|
428 |
|
---|
429 | if (!test_dlfcn_dl_get_public_var())
|
---|
430 | return 1;
|
---|
431 |
|
---|
432 | if (!test_dlfcn_dl_get_public_uvar())
|
---|
433 | return 1;
|
---|
434 |
|
---|
435 | if (!test_dlfcn_read_public_var())
|
---|
436 | return 1;
|
---|
437 |
|
---|
438 | if (!test_dlfcn_read_public_uvar())
|
---|
439 | return 1;
|
---|
440 |
|
---|
441 | #ifdef DLTEST_LINKED
|
---|
442 | if (!test_lnk_dl_get_constant())
|
---|
443 | return 1;
|
---|
444 |
|
---|
445 | if (!test_lnk_dl_get_private_var())
|
---|
446 | return 1;
|
---|
447 |
|
---|
448 | if (!test_lnk_dl_get_private_uvar())
|
---|
449 | return 1;
|
---|
450 |
|
---|
451 | if (!test_lnk_dl_get_public_var())
|
---|
452 | return 1;
|
---|
453 |
|
---|
454 | if (!test_lnk_dl_get_public_uvar())
|
---|
455 | return 1;
|
---|
456 |
|
---|
457 | if (!test_lnk_read_public_var())
|
---|
458 | return 1;
|
---|
459 |
|
---|
460 | if (!test_lnk_read_public_uvar())
|
---|
461 | return 1;
|
---|
462 | #endif
|
---|
463 | // printf("dlclose()... ");
|
---|
464 | // dlclose(handle);
|
---|
465 | // printf("Passed\n");
|
---|
466 |
|
---|
467 | printf("All passed.\n");
|
---|
468 | return 0;
|
---|
469 | }
|
---|
470 |
|
---|
471 | /**
|
---|
472 | * @}
|
---|
473 | */
|
---|