1 | /* Test that --glibc-hwcaps-prepend works, using dlopen and /etc/ld.so.cache. |
2 | Copyright (C) 2020-2024 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 | { |
48 | /* Install the default implementation of libmarkermod1.so. */ |
49 | char *conf_path = xasprintf (format: "%s/ld.so.conf" , support_sysconfdir_prefix); |
50 | xmkdirp (support_sysconfdir_prefix, 0777); |
51 | support_write_file_string (path: conf_path, contents: "/glibc-test/lib\n" ); |
52 | free (ptr: conf_path); |
53 | } |
54 | xmkdirp ("/glibc-test/lib/glibc-hwcaps/prepend2" , 0777); |
55 | xmkdirp ("/glibc-test/lib/glibc-hwcaps/prepend3" , 0777); |
56 | { |
57 | char *src = xasprintf (format: "%s/elf/libmarkermod1-1.so" , support_objdir_root); |
58 | support_copy_file (from: src, to: "/glibc-test/lib/" SONAME); |
59 | free (ptr: src); |
60 | } |
61 | run_ldconfig (); |
62 | { |
63 | /* The default implementation can now be loaded. */ |
64 | void *handle = xdlopen (SONAME, RTLD_NOW); |
65 | int (*marker1) (void) = xdlsym (handle, symbol: "marker1" ); |
66 | TEST_COMPARE (marker1 (), 1); |
67 | xdlclose (handle); |
68 | } |
69 | |
70 | /* Add the first override to the directory that is searched last. */ |
71 | { |
72 | char *src = xasprintf (format: "%s/elf/libmarkermod1-2.so" , support_objdir_root); |
73 | support_copy_file (from: src, to: "/glibc-test/lib/glibc-hwcaps/prepend2/" |
74 | SONAME); |
75 | free (ptr: src); |
76 | } |
77 | { |
78 | /* This is still the first implementation. The cache has not been |
79 | updated. */ |
80 | void *handle = xdlopen (SONAME, RTLD_NOW); |
81 | int (*marker1) (void) = xdlsym (handle, symbol: "marker1" ); |
82 | TEST_COMPARE (marker1 (), 1); |
83 | xdlclose (handle); |
84 | } |
85 | run_ldconfig (); |
86 | { |
87 | /* After running ldconfig, it is the second implementation. */ |
88 | void *handle = xdlopen (SONAME, RTLD_NOW); |
89 | int (*marker1) (void) = xdlsym (handle, symbol: "marker1" ); |
90 | TEST_COMPARE (marker1 (), 2); |
91 | xdlclose (handle); |
92 | } |
93 | |
94 | /* Add the second override to the directory that is searched first. */ |
95 | { |
96 | char *src = xasprintf (format: "%s/elf/libmarkermod1-3.so" , support_objdir_root); |
97 | support_copy_file (from: src, to: "/glibc-test/lib/glibc-hwcaps/prepend3/" |
98 | SONAME); |
99 | free (ptr: src); |
100 | } |
101 | { |
102 | /* This is still the second implementation. */ |
103 | void *handle = xdlopen (SONAME, RTLD_NOW); |
104 | int (*marker1) (void) = xdlsym (handle, symbol: "marker1" ); |
105 | TEST_COMPARE (marker1 (), 2); |
106 | xdlclose (handle); |
107 | } |
108 | run_ldconfig (); |
109 | { |
110 | /* After running ldconfig, it is the third implementation. */ |
111 | void *handle = xdlopen (SONAME, RTLD_NOW); |
112 | int (*marker1) (void) = xdlsym (handle, symbol: "marker1" ); |
113 | TEST_COMPARE (marker1 (), 3); |
114 | xdlclose (handle); |
115 | } |
116 | |
117 | /* Remove the second override again, without running ldconfig. |
118 | Ideally, this would revert to implementation 2. However, in the |
119 | current implementation, the cache returns exactly one file name |
120 | which does not exist after unlinking, so the dlopen fails. */ |
121 | xunlink (path: "/glibc-test/lib/glibc-hwcaps/prepend3/" SONAME); |
122 | TEST_VERIFY (dlopen (SONAME, RTLD_NOW) == NULL); |
123 | run_ldconfig (); |
124 | { |
125 | /* After running ldconfig, the second implementation is available |
126 | once more. */ |
127 | void *handle = xdlopen (SONAME, RTLD_NOW); |
128 | int (*marker1) (void) = xdlsym (handle, symbol: "marker1" ); |
129 | TEST_COMPARE (marker1 (), 2); |
130 | xdlclose (handle); |
131 | } |
132 | |
133 | return 0; |
134 | } |
135 | |
136 | static void |
137 | prepare (int argc, char **argv) |
138 | { |
139 | const char *no_restart = "no-restart" ; |
140 | if (argc == 2 && strcmp (s1: argv[1], s2: no_restart) == 0) |
141 | return; |
142 | /* Re-execute the test with an explicit loader invocation. */ |
143 | execl (path: support_objdir_elf_ldso, |
144 | arg: support_objdir_elf_ldso, |
145 | "--glibc-hwcaps-prepend" , "prepend3:prepend2" , |
146 | argv[0], no_restart, |
147 | NULL); |
148 | printf (format: "error: execv of %s failed: %m\n" , argv[0]); |
149 | _exit (status: 1); |
150 | } |
151 | |
152 | #define PREPARE prepare |
153 | #include <support/test-driver.c> |
154 | |