1 | /* Test that --glibc-hwcaps-prepend works, using dlopen and /etc/ld.so.cache. |
2 | Copyright (C) 2020-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <dlfcn.h> |
20 | #include <stddef.h> |
21 | #include <stdio.h> |
22 | #include <stdlib.h> |
23 | #include <string.h> |
24 | #include <support/check.h> |
25 | #include <support/support.h> |
26 | #include <support/xdlfcn.h> |
27 | #include <support/xunistd.h> |
28 | |
29 | /* Invoke /sbin/ldconfig with some error checking. */ |
30 | static void |
31 | run_ldconfig (void) |
32 | { |
33 | char *command = xasprintf (format: "%s/ldconfig" , support_install_rootsbindir); |
34 | TEST_COMPARE (system (command), 0); |
35 | free (ptr: command); |
36 | } |
37 | |
38 | /* The library under test. */ |
39 | #define SONAME "libmarkermod1.so" |
40 | |
41 | static int |
42 | do_test (void) |
43 | { |
44 | if (dlopen (SONAME, RTLD_NOW) != NULL) |
45 | FAIL_EXIT1 (SONAME " is already on the search path" ); |
46 | |
47 | /* Install the default implementation of libmarkermod1.so. */ |
48 | xmkdirp ("/etc" , 0777); |
49 | support_write_file_string (path: "/etc/ld.so.conf" , contents: "/glibc-test/lib\n" ); |
50 | xmkdirp ("/glibc-test/lib/glibc-hwcaps/prepend2" , 0777); |
51 | xmkdirp ("/glibc-test/lib/glibc-hwcaps/prepend3" , 0777); |
52 | { |
53 | char *src = xasprintf (format: "%s/elf/libmarkermod1-1.so" , support_objdir_root); |
54 | support_copy_file (from: src, to: "/glibc-test/lib/" SONAME); |
55 | free (ptr: src); |
56 | } |
57 | run_ldconfig (); |
58 | { |
59 | /* The default implementation can now be loaded. */ |
60 | void *handle = xdlopen (SONAME, RTLD_NOW); |
61 | int (*marker1) (void) = xdlsym (handle, symbol: "marker1" ); |
62 | TEST_COMPARE (marker1 (), 1); |
63 | xdlclose (handle); |
64 | } |
65 | |
66 | /* Add the first override to the directory that is searched last. */ |
67 | { |
68 | char *src = xasprintf (format: "%s/elf/libmarkermod1-2.so" , support_objdir_root); |
69 | support_copy_file (from: src, to: "/glibc-test/lib/glibc-hwcaps/prepend2/" |
70 | SONAME); |
71 | free (ptr: src); |
72 | } |
73 | { |
74 | /* This is still the first implementation. The cache has not been |
75 | updated. */ |
76 | void *handle = xdlopen (SONAME, RTLD_NOW); |
77 | int (*marker1) (void) = xdlsym (handle, symbol: "marker1" ); |
78 | TEST_COMPARE (marker1 (), 1); |
79 | xdlclose (handle); |
80 | } |
81 | run_ldconfig (); |
82 | { |
83 | /* After running ldconfig, it is the second implementation. */ |
84 | void *handle = xdlopen (SONAME, RTLD_NOW); |
85 | int (*marker1) (void) = xdlsym (handle, symbol: "marker1" ); |
86 | TEST_COMPARE (marker1 (), 2); |
87 | xdlclose (handle); |
88 | } |
89 | |
90 | /* Add the second override to the directory that is searched first. */ |
91 | { |
92 | char *src = xasprintf (format: "%s/elf/libmarkermod1-3.so" , support_objdir_root); |
93 | support_copy_file (from: src, to: "/glibc-test/lib/glibc-hwcaps/prepend3/" |
94 | SONAME); |
95 | free (ptr: src); |
96 | } |
97 | { |
98 | /* This is still the second implementation. */ |
99 | void *handle = xdlopen (SONAME, RTLD_NOW); |
100 | int (*marker1) (void) = xdlsym (handle, symbol: "marker1" ); |
101 | TEST_COMPARE (marker1 (), 2); |
102 | xdlclose (handle); |
103 | } |
104 | run_ldconfig (); |
105 | { |
106 | /* After running ldconfig, it is the third implementation. */ |
107 | void *handle = xdlopen (SONAME, RTLD_NOW); |
108 | int (*marker1) (void) = xdlsym (handle, symbol: "marker1" ); |
109 | TEST_COMPARE (marker1 (), 3); |
110 | xdlclose (handle); |
111 | } |
112 | |
113 | /* Remove the second override again, without running ldconfig. |
114 | Ideally, this would revert to implementation 2. However, in the |
115 | current implementation, the cache returns exactly one file name |
116 | which does not exist after unlinking, so the dlopen fails. */ |
117 | xunlink (path: "/glibc-test/lib/glibc-hwcaps/prepend3/" SONAME); |
118 | TEST_VERIFY (dlopen (SONAME, RTLD_NOW) == NULL); |
119 | run_ldconfig (); |
120 | { |
121 | /* After running ldconfig, the second implementation is available |
122 | once more. */ |
123 | void *handle = xdlopen (SONAME, RTLD_NOW); |
124 | int (*marker1) (void) = xdlsym (handle, symbol: "marker1" ); |
125 | TEST_COMPARE (marker1 (), 2); |
126 | xdlclose (handle); |
127 | } |
128 | |
129 | return 0; |
130 | } |
131 | |
132 | static void |
133 | prepare (int argc, char **argv) |
134 | { |
135 | const char *no_restart = "no-restart" ; |
136 | if (argc == 2 && strcmp (s1: argv[1], s2: no_restart) == 0) |
137 | return; |
138 | /* Re-execute the test with an explicit loader invocation. */ |
139 | execl (path: support_objdir_elf_ldso, |
140 | arg: support_objdir_elf_ldso, |
141 | "--glibc-hwcaps-prepend" , "prepend3:prepend2" , |
142 | argv[0], no_restart, |
143 | NULL); |
144 | printf (format: "error: execv of %s failed: %m\n" , argv[0]); |
145 | _exit (status: 1); |
146 | } |
147 | |
148 | #define PREPARE prepare |
149 | #include <support/test-driver.c> |
150 | |