Mercurial > pub > dyncall > dyncall
annotate test/dynload_plain/dynload_plain.c @ 382:dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
* dynload_plain test code changes to reflect this
* note in doc
- changed dynload_plain to not do inode-based tests on windows
author | Tassilo Philipp |
---|---|
date | Wed, 20 Jan 2021 13:49:43 +0100 |
parents | 3124f4c4f293 |
children | 7608e34098b0 |
rev | line source |
---|---|
214 | 1 /* |
2 | |
3 Package: dyncall | |
4 Library: test | |
5 File: test/dynload_plain/dynload_plain.c | |
281 | 6 Description: |
214 | 7 License: |
8 | |
281 | 9 Copyright (c) 2017-2018 Tassilo Philipp <tphilipp@potion-studios.com> |
214 | 10 |
11 Permission to use, copy, modify, and distribute this software for any | |
12 purpose with or without fee is hereby granted, provided that the above | |
13 copyright notice and this permission notice appear in all copies. | |
14 | |
15 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
16 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
17 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
18 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
22 | |
23 */ | |
24 | |
25 | |
26 #include "../../dynload/dynload.h" | |
27 #include "../common/platformInit.h" | |
28 | |
29 #include <string.h> | |
242 | 30 #include <sys/stat.h> |
233 | 31 #if defined(DC_WINDOWS) |
32 # include <io.h> | |
33 # define F_OK 0 | |
34 #else | |
35 # include <unistd.h> | |
36 #endif | |
318
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
37 |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
38 #if defined(DC_WINDOWS) |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
39 char* dirname(char* path) |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
40 { |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
41 static const char dot[] = "."; |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
42 char* p = strrchr(path, '\\'); |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
43 if(p) |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
44 *p = '\0'; |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
45 else |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
46 path = (char*)dot; |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
47 return path; |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
48 } |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
49 #else |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
50 # include <libgen.h> |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
51 #endif |
214 | 52 |
53 | |
308 | 54 int strlen_utf8(const char *s) |
55 { | |
56 int i=0, j=0; | |
57 while(s[i]) | |
58 j += ((s[i++] & 0xc0) != 0x80); | |
59 return j; | |
60 } | |
61 | |
62 | |
214 | 63 int main(int argc, char* argv[]) |
64 { | |
65 int r = 0, i; | |
66 void* p; | |
67 DLLib* pLib; | |
233 | 68 DLSyms* pSyms; |
214 | 69 const char* path = NULL; |
382
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
70 int cmp_inode = 1; |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
71 |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
72 /* hacky/lazy list of some clib paths per platform - more/others, like version-suffixed ones */ |
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
73 /* can be specified in Makefile; this avoids trying to write portable directory traversal stuff */ |
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
74 const char* clibs[] = { |
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
75 #if defined(DEF_C_DYLIB) |
308 | 76 DEF_C_DYLIB, |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
77 #endif |
314
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
313
diff
changeset
|
78 /* fallback guessing if not provided by Makefile */ |
214 | 79 "/lib/libc.so", |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
80 "/lib32/libc.so", |
214 | 81 "/lib64/libc.so", |
242 | 82 "/usr/lib/libc.so", |
382
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
83 "/usr/lib/system/libsystem_c.dylib", /* macos - note: not on fs w/ macos >= 11.0.1 */ |
214 | 84 "/usr/lib/libc.dylib", |
313
73b5b9e224e2
- dynload_plain test: stability fix for picking right testing lib on build
Tassilo Philipp
parents:
312
diff
changeset
|
85 "/boot/system/lib/libroot.so", /* Haiku */ |
73b5b9e224e2
- dynload_plain test: stability fix for picking right testing lib on build
Tassilo Philipp
parents:
312
diff
changeset
|
86 "\\ReactOS\\system32\\msvcrt.dll", /* ReactOS */ |
234 | 87 "C:\\ReactOS\\system32\\msvcrt.dll", |
313
73b5b9e224e2
- dynload_plain test: stability fix for picking right testing lib on build
Tassilo Philipp
parents:
312
diff
changeset
|
88 "\\Windows\\system32\\msvcrt.dll", /* Windows */ |
234 | 89 "C:\\Windows\\system32\\msvcrt.dll" |
214 | 90 }; |
91 | |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
92 /* use first matching path of hacky hardcoded list, above */ |
214 | 93 for(i=0; i<(sizeof(clibs)/sizeof(const char*)); ++i) { |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
94 if(access(clibs[i], F_OK) != -1) { |
214 | 95 path = clibs[i]; |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
96 break; |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
97 } |
382
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
98 #if defined(DC__OS_Darwin) |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
99 /* macos >= 11.0.1 (Big Sur) dylibs might not be on disk but in a sys cache, so dlopen works but not fs checks */ |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
100 else if((pLib = dlLoadLibrary(clibs[i]))) { |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
101 cmp_inode = 0; /* not dealing with files but dylib sys cache */ |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
102 dlFreeLibrary(pLib); |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
103 path = clibs[i]; |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
104 break; |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
105 } |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
106 #endif |
214 | 107 } |
108 | |
109 if(path) { | |
110 printf("using clib to test at: %s\n", path); | |
111 ++r; | |
112 | |
242 | 113 /* dl*Library tests */ |
114 /* ---------------- */ | |
115 pLib = dlLoadLibrary(path); /* check if we can load a lib */ | |
214 | 116 if(pLib) { |
242 | 117 char queriedPath[200]; /* enough for our test paths */ |
118 int bs; | |
119 | |
214 | 120 printf("pLib handle: %p\n", pLib); |
121 ++r; | |
122 | |
242 | 123 p = dlFindSymbol(pLib, "printf"); /* check if we can lookup a symbol */ |
214 | 124 printf("printf at: %p\n", p); |
125 r += (p != NULL); | |
126 | |
242 | 127 bs = dlGetLibraryPath(pLib, queriedPath, 200); |
128 if(bs && bs <= 200) { | |
308 | 129 int b, bs_; |
242 | 130 printf("path of lib looked up via handle: %s\n", queriedPath); |
382
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
131 |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
132 /*@@@ check if resolved path is absolute*/ |
242 | 133 |
382
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
134 #if defined(DC_WINDOWS) |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
135 /* on windows, inode numbers returned by stat(2) tests below are always 0, so don't use those */ |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
136 cmp_inode = 0; |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
137 #endif |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
138 |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
139 /* path based check if same lib */ |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
140 if(cmp_inode) |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
141 { |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
142 struct stat st0, st1; /* to check if same file */ |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
143 b = (stat(path, &st0) != -1) && (stat(queriedPath, &st1) != -1); |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
144 printf("lib (inode:%d) and looked up lib (inode:%d) are same: %d\n", b?(int)st0.st_ino:-1, b?(int)st1.st_ino:-1, b && (st0.st_ino == st1.st_ino)); |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
145 r += b && (st0.st_ino == st1.st_ino); /* compare if same lib using inode */ |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
146 } |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
147 else |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
148 printf("-- skipping inode based check (doesn't apply to this platform or we are dealing with macos dylib that isn't on fs) --\n"); |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
149 |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
150 /* just load lib with queried path and compare handle */ |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
151 { |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
152 DLLib* pLib_ = dlLoadLibrary(queriedPath); |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
153 b = (pLib == pLib_); /* pLib guaranteed to not be NULL, here, so no explicit !pLib_ check */ |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
154 printf("lib (handle:%p) and looked up lib (handle:%p) are same: %d\n", pLib, pLib_, b); |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
155 r += b; |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
156 dlFreeLibrary(pLib_); /* dec ref count */ |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
157 } |
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
158 |
242 | 159 /* check correct bufsize retval */ |
160 b = (bs == strlen(queriedPath) + 1); | |
308 | 161 printf("looked up path's needed buffer size (%d) computed correctly 1/2: %d\n", bs, b); |
162 r += b; | |
163 | |
164 /* check perfect fitting bufsize */ | |
165 queriedPath[0] = 0; | |
166 bs_ = dlGetLibraryPath(pLib, queriedPath, bs); | |
167 b = (bs == bs_ && bs_ == strlen(queriedPath) + 1); | |
168 printf("looked up path's needed buffer size (%d) computed correctly 2/2: %d\n", bs_, b); | |
169 r += b; | |
170 | |
171 /* check if dlGetLibraryPath returns size required if bufsize too small */ | |
172 queriedPath[0] = 0; | |
173 bs_ = dlGetLibraryPath(pLib, queriedPath, 1); /* tiny max buffer size */ | |
174 b = (bs == bs_ && strlen(queriedPath) == 0); /* nothing copied */ | |
175 printf("path lookup size requirement (%d) correctly returned: %d\n", bs_, b); | |
242 | 176 r += b; |
177 } | |
178 else | |
179 printf("failed to query lib path using lib's handle\n"); | |
180 | |
214 | 181 dlFreeLibrary(pLib); |
308 | 182 |
183 /* check if dlGetLibraryPath returns 0 when trying to lookup dummy */ | |
312
18de5758980e
- stability fix: avoid sigsegv in dynload's dlGetLibraryPath() in some cases (e.g. wrong handle given or OS specific quirk)
Tassilo Philipp
parents:
311
diff
changeset
|
184 bs = dlGetLibraryPath((DLLib*)&r/*dummy addr*/, queriedPath, 200); |
308 | 185 printf("path lookup failed as expected with bad lib handle: %d\n", bs == 0); |
186 r += (bs == 0); | |
187 | |
314
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
313
diff
changeset
|
188 /* test getting own path */ |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
313
diff
changeset
|
189 { |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
313
diff
changeset
|
190 /* get own exec's path */ |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
313
diff
changeset
|
191 bs = dlGetLibraryPath(NULL, queriedPath, 200); |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
313
diff
changeset
|
192 printf("dynload_plain's own path is: %s\n", queriedPath); |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
313
diff
changeset
|
193 r += (bs != 0 && strlen(queriedPath) > 0); |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
313
diff
changeset
|
194 |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
313
diff
changeset
|
195 /* change working dir to where our executable is, for following test */ |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
313
diff
changeset
|
196 chdir(dirname(queriedPath)); |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
313
diff
changeset
|
197 } |
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
313
diff
changeset
|
198 |
308 | 199 /* test UTF-8 path through dummy library that's created by this test's build */ |
200 { | |
201 static const char* pathU8 = "./dynload_plain_\xc3\x9f_test"; | |
314
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
313
diff
changeset
|
202 int nu8c, b; |
308 | 203 |
204 pLib = dlLoadLibrary(pathU8); /* check if we can load a lib with a UTF-8 path */ | |
314
b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
Tassilo Philipp
parents:
313
diff
changeset
|
205 printf("pLib (loaded w/ UTF-8 path %s with wd being exec's dir) handle: %p\n", pathU8, pLib); |
308 | 206 r += (p != NULL); |
207 | |
208 if(pLib) { | |
209 /* get UTF-8 path back */ | |
210 bs = dlGetLibraryPath((DLLib*)pLib, queriedPath, 200); | |
211 if(bs && bs <= 200) { | |
212 nu8c = strlen_utf8(queriedPath); /* num of UTF-8 chars is as big as ... */ | |
213 b = (bs > 0) && (nu8c == bs-2); /* ... buffer size minus 2 (b/c of one 2-byte UTF-8 char and "\0") */ | |
214 printf("UTF-8 path of lib looked up via handle: %s\n", queriedPath); | |
215 printf("looked up UTF-8 path's needed buffer size (%d) for %d UTF-8 char string computed correctly: %d\n", bs, nu8c, b); | |
216 r += b; | |
217 | |
218 dlFreeLibrary(pLib); | |
219 } | |
220 else | |
221 printf("failed to query UTF-8 lib path using lib's handle\n"); | |
222 } | |
223 } | |
214 | 224 } |
225 else | |
226 printf("unable to open library %s\n", path); | |
227 | |
228 | |
242 | 229 /* dlSyms* tests (intentionally after freeing lib above, as they work standalone) */ |
230 /* ------------- */ | |
231 pSyms = dlSymsInit(path); /* check if we can iterate over symbols - init */ | |
214 | 232 if(pSyms) { |
233 int n; | |
234 const char* name; | |
235 | |
236 printf("pSyms handle: %p\n", pSyms); | |
237 ++r; | |
238 | |
242 | 239 n = dlSymsCount(pSyms); /* check if there are some syms to iterate over */ |
214 | 240 printf("num of libc symbols: %d\n", n); |
241 r += (n > 0); | |
242 | |
243 for(i=0; i<n; ++i) { | |
244 name = dlSymsName(pSyms, i); | |
242 | 245 if(name && strcmp(name, "printf") == 0) { /* check if we find "printf" also in iterated symbols */ |
214 | 246 ++r; |
247 break; | |
248 } | |
249 } | |
250 printf("printf symbol found by iteration: %d\n", i<n); | |
251 | |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
252 name = (i<n) ? dlSymsName(pSyms, i) : NULL; |
242 | 253 r += (name && strcmp(name, "printf") == 0); /* check if we can lookup "printf" by index */ |
218 | 254 printf("printf symbol name by index: %s\n", name?name:""); |
214 | 255 |
242 | 256 pLib = dlLoadLibrary(path); /* check if we can resolve ptr -> name, */ |
257 if(pLib) { /* need to lookup by name again, first */ | |
214 | 258 p = dlFindSymbol(pLib, "printf"); |
259 name = dlSymsNameFromValue(pSyms, p); | |
218 | 260 printf("printf symbol name by its own address (%p): %s\n", p, name?name:""); |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
261 if(name) { |
308 | 262 if(strcmp(name, "printf") == 0) |
263 ++r; | |
264 else { | |
265 /* Symbol name returned might be an "alias". In that case, check address again (full lookup to be sure). */ | |
266 void* p0 = dlFindSymbol(pLib, name); | |
267 printf("lookup by address returned different name (%s), which is alias of printf: %d\n", name, (p==p0)); | |
268 r += (p == p0); | |
269 } | |
270 } | |
214 | 271 dlFreeLibrary(pLib); |
272 } | |
273 | |
274 dlSymsCleanup(pSyms); | |
275 } | |
276 else | |
277 printf("dlSymsInit failed\n"); | |
278 } | |
279 | |
242 | 280 /* Check final score of right ones to see if all worked */ |
382
dd5d03483314
- dynload changes to support macos >= 11.0.1 "built-in dynamic linker cache of all system-provided libraries" (those dylibs are no longer present on the fs)"
Tassilo Philipp
parents:
318
diff
changeset
|
281 r = (r == 16 + cmp_inode); |
214 | 282 printf("result: dynload_plain: %d\n", r); |
283 return !r; | |
284 } | |
285 |