Mercurial > pub > dyncall > dyncall
annotate test/dynload_plain/dynload_plain.c @ 624:d28b09fa2ff9
cleanup: removal of actually unnecessary require lines
author | Tassilo Philipp |
---|---|
date | Fri, 28 Oct 2022 12:22:12 +0200 |
parents | 7608e34098b0 |
children |
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 | |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
382
diff
changeset
|
9 Copyright (c) 2017-2021 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" | |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
382
diff
changeset
|
28 #include "../common/platformInit.c" /* Impl. for functions only used in this translation unit */ |
214 | 29 |
30 #include <string.h> | |
242 | 31 #include <sys/stat.h> |
233 | 32 #if defined(DC_WINDOWS) |
33 # include <io.h> | |
34 # define F_OK 0 | |
35 #else | |
36 # include <unistd.h> | |
37 #endif | |
318
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
38 |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
39 #if defined(DC_WINDOWS) |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
40 char* dirname(char* path) |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
41 { |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
42 static const char dot[] = "."; |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
43 char* p = strrchr(path, '\\'); |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
44 if(p) |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
45 *p = '\0'; |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
46 else |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
47 path = (char*)dot; |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
48 return path; |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
49 } |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
50 #else |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
51 # include <libgen.h> |
3124f4c4f293
- dynload_plain test code build fix for windows
Tassilo Philipp
parents:
314
diff
changeset
|
52 #endif |
214 | 53 |
54 | |
308 | 55 int strlen_utf8(const char *s) |
56 { | |
57 int i=0, j=0; | |
58 while(s[i]) | |
59 j += ((s[i++] & 0xc0) != 0x80); | |
60 return j; | |
61 } | |
62 | |
63 | |
214 | 64 int main(int argc, char* argv[]) |
65 { | |
66 int r = 0, i; | |
67 void* p; | |
68 DLLib* pLib; | |
233 | 69 DLSyms* pSyms; |
214 | 70 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
|
71 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
|
72 |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
73 /* 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
|
74 /* 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
|
75 const char* clibs[] = { |
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
76 #if defined(DEF_C_DYLIB) |
308 | 77 DEF_C_DYLIB, |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
78 #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
|
79 /* fallback guessing if not provided by Makefile */ |
214 | 80 "/lib/libc.so", |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
81 "/lib32/libc.so", |
214 | 82 "/lib64/libc.so", |
242 | 83 "/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
|
84 "/usr/lib/system/libsystem_c.dylib", /* macos - note: not on fs w/ macos >= 11.0.1 */ |
214 | 85 "/usr/lib/libc.dylib", |
313
73b5b9e224e2
- dynload_plain test: stability fix for picking right testing lib on build
Tassilo Philipp
parents:
312
diff
changeset
|
86 "/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
|
87 "\\ReactOS\\system32\\msvcrt.dll", /* ReactOS */ |
234 | 88 "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
|
89 "\\Windows\\system32\\msvcrt.dll", /* Windows */ |
234 | 90 "C:\\Windows\\system32\\msvcrt.dll" |
214 | 91 }; |
92 | |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
382
diff
changeset
|
93 dcTest_initPlatform(); |
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
382
diff
changeset
|
94 |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
95 /* use first matching path of hacky hardcoded list, above */ |
214 | 96 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
|
97 if(access(clibs[i], F_OK) != -1) { |
214 | 98 path = clibs[i]; |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
99 break; |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
100 } |
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
|
101 #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
|
102 /* 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
|
103 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
|
104 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
|
105 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
|
106 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
|
107 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
|
108 } |
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
|
109 #endif |
214 | 110 } |
111 | |
112 if(path) { | |
113 printf("using clib to test at: %s\n", path); | |
114 ++r; | |
115 | |
242 | 116 /* dl*Library tests */ |
117 /* ---------------- */ | |
118 pLib = dlLoadLibrary(path); /* check if we can load a lib */ | |
214 | 119 if(pLib) { |
242 | 120 char queriedPath[200]; /* enough for our test paths */ |
121 int bs; | |
122 | |
214 | 123 printf("pLib handle: %p\n", pLib); |
124 ++r; | |
125 | |
242 | 126 p = dlFindSymbol(pLib, "printf"); /* check if we can lookup a symbol */ |
214 | 127 printf("printf at: %p\n", p); |
128 r += (p != NULL); | |
129 | |
242 | 130 bs = dlGetLibraryPath(pLib, queriedPath, 200); |
131 if(bs && bs <= 200) { | |
308 | 132 int b, bs_; |
242 | 133 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
|
134 |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
135 /*@@@ check if resolved path is absolute*/ |
242 | 136 |
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
|
137 #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
|
138 /* 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
|
139 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
|
140 #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
|
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 /* 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
|
143 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
|
144 { |
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 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
|
146 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
|
147 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
|
148 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
|
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 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
|
151 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
|
152 |
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 /* 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
|
154 { |
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 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
|
156 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
|
157 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
|
158 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
|
159 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
|
160 } |
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
|
161 |
242 | 162 /* check correct bufsize retval */ |
163 b = (bs == strlen(queriedPath) + 1); | |
308 | 164 printf("looked up path's needed buffer size (%d) computed correctly 1/2: %d\n", bs, b); |
165 r += b; | |
166 | |
167 /* check perfect fitting bufsize */ | |
168 queriedPath[0] = 0; | |
169 bs_ = dlGetLibraryPath(pLib, queriedPath, bs); | |
170 b = (bs == bs_ && bs_ == strlen(queriedPath) + 1); | |
171 printf("looked up path's needed buffer size (%d) computed correctly 2/2: %d\n", bs_, b); | |
172 r += b; | |
173 | |
174 /* check if dlGetLibraryPath returns size required if bufsize too small */ | |
175 queriedPath[0] = 0; | |
176 bs_ = dlGetLibraryPath(pLib, queriedPath, 1); /* tiny max buffer size */ | |
177 b = (bs == bs_ && strlen(queriedPath) == 0); /* nothing copied */ | |
178 printf("path lookup size requirement (%d) correctly returned: %d\n", bs_, b); | |
242 | 179 r += b; |
180 } | |
181 else | |
182 printf("failed to query lib path using lib's handle\n"); | |
183 | |
214 | 184 dlFreeLibrary(pLib); |
308 | 185 |
186 /* 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
|
187 bs = dlGetLibraryPath((DLLib*)&r/*dummy addr*/, queriedPath, 200); |
308 | 188 printf("path lookup failed as expected with bad lib handle: %d\n", bs == 0); |
189 r += (bs == 0); | |
190 | |
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
|
191 /* 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
|
192 { |
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 /* 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
|
194 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
|
195 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
|
196 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
|
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 /* 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
|
199 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
|
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
|
201 |
308 | 202 /* test UTF-8 path through dummy library that's created by this test's build */ |
203 { | |
204 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
|
205 int nu8c, b; |
308 | 206 |
207 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
|
208 printf("pLib (loaded w/ UTF-8 path %s with wd being exec's dir) handle: %p\n", pathU8, pLib); |
308 | 209 r += (p != NULL); |
210 | |
211 if(pLib) { | |
212 /* get UTF-8 path back */ | |
213 bs = dlGetLibraryPath((DLLib*)pLib, queriedPath, 200); | |
214 if(bs && bs <= 200) { | |
215 nu8c = strlen_utf8(queriedPath); /* num of UTF-8 chars is as big as ... */ | |
216 b = (bs > 0) && (nu8c == bs-2); /* ... buffer size minus 2 (b/c of one 2-byte UTF-8 char and "\0") */ | |
217 printf("UTF-8 path of lib looked up via handle: %s\n", queriedPath); | |
218 printf("looked up UTF-8 path's needed buffer size (%d) for %d UTF-8 char string computed correctly: %d\n", bs, nu8c, b); | |
219 r += b; | |
220 | |
221 dlFreeLibrary(pLib); | |
222 } | |
223 else | |
224 printf("failed to query UTF-8 lib path using lib's handle\n"); | |
225 } | |
226 } | |
214 | 227 } |
228 else | |
229 printf("unable to open library %s\n", path); | |
230 | |
231 | |
242 | 232 /* dlSyms* tests (intentionally after freeing lib above, as they work standalone) */ |
233 /* ------------- */ | |
234 pSyms = dlSymsInit(path); /* check if we can iterate over symbols - init */ | |
214 | 235 if(pSyms) { |
236 int n; | |
237 const char* name; | |
238 | |
239 printf("pSyms handle: %p\n", pSyms); | |
240 ++r; | |
241 | |
242 | 242 n = dlSymsCount(pSyms); /* check if there are some syms to iterate over */ |
214 | 243 printf("num of libc symbols: %d\n", n); |
244 r += (n > 0); | |
245 | |
246 for(i=0; i<n; ++i) { | |
247 name = dlSymsName(pSyms, i); | |
242 | 248 if(name && strcmp(name, "printf") == 0) { /* check if we find "printf" also in iterated symbols */ |
214 | 249 ++r; |
250 break; | |
251 } | |
252 } | |
253 printf("printf symbol found by iteration: %d\n", i<n); | |
254 | |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
255 name = (i<n) ? dlSymsName(pSyms, i) : NULL; |
242 | 256 r += (name && strcmp(name, "printf") == 0); /* check if we can lookup "printf" by index */ |
218 | 257 printf("printf symbol name by index: %s\n", name?name:""); |
214 | 258 |
242 | 259 pLib = dlLoadLibrary(path); /* check if we can resolve ptr -> name, */ |
260 if(pLib) { /* need to lookup by name again, first */ | |
214 | 261 p = dlFindSymbol(pLib, "printf"); |
262 name = dlSymsNameFromValue(pSyms, p); | |
218 | 263 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
|
264 if(name) { |
308 | 265 if(strcmp(name, "printf") == 0) |
266 ++r; | |
267 else { | |
268 /* Symbol name returned might be an "alias". In that case, check address again (full lookup to be sure). */ | |
269 void* p0 = dlFindSymbol(pLib, name); | |
270 printf("lookup by address returned different name (%s), which is alias of printf: %d\n", name, (p==p0)); | |
271 r += (p == p0); | |
272 } | |
273 } | |
214 | 274 dlFreeLibrary(pLib); |
275 } | |
276 | |
277 dlSymsCleanup(pSyms); | |
278 } | |
279 else | |
280 printf("dlSymsInit failed\n"); | |
281 } | |
282 | |
242 | 283 /* 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
|
284 r = (r == 16 + cmp_inode); |
214 | 285 printf("result: dynload_plain: %d\n", r); |
410
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
382
diff
changeset
|
286 |
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
382
diff
changeset
|
287 dcTest_deInitPlatform(); |
7608e34098b0
- cleanups, simplifications, some api clarification, ...
Tassilo Philipp
parents:
382
diff
changeset
|
288 |
214 | 289 return !r; |
290 } | |
291 |