annotate test/dynload_plain/dynload_plain.c @ 410:7608e34098b0

- cleanups, simplifications, some api clarification, ... - test cases consistency: * return status code depending on test results (for actual conformance tests, not stuff that is not an example or hack to check something, ..) * platform init helper added for some
author Tassilo Philipp
date Tue, 05 Oct 2021 21:53:04 +0200
parents dd5d03483314
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
214
Tassilo Philipp
parents:
diff changeset
1 /*
Tassilo Philipp
parents:
diff changeset
2
Tassilo Philipp
parents:
diff changeset
3 Package: dyncall
Tassilo Philipp
parents:
diff changeset
4 Library: test
Tassilo Philipp
parents:
diff changeset
5 File: test/dynload_plain/dynload_plain.c
281
f5577f6bf97a - file header cleanups for release
Tassilo Philipp
parents: 272
diff changeset
6 Description:
214
Tassilo Philipp
parents:
diff changeset
7 License:
Tassilo Philipp
parents:
diff changeset
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
Tassilo Philipp
parents:
diff changeset
10
Tassilo Philipp
parents:
diff changeset
11 Permission to use, copy, modify, and distribute this software for any
Tassilo Philipp
parents:
diff changeset
12 purpose with or without fee is hereby granted, provided that the above
Tassilo Philipp
parents:
diff changeset
13 copyright notice and this permission notice appear in all copies.
Tassilo Philipp
parents:
diff changeset
14
Tassilo Philipp
parents:
diff changeset
15 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
Tassilo Philipp
parents:
diff changeset
16 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
Tassilo Philipp
parents:
diff changeset
17 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
Tassilo Philipp
parents:
diff changeset
18 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
Tassilo Philipp
parents:
diff changeset
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
Tassilo Philipp
parents:
diff changeset
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Tassilo Philipp
parents:
diff changeset
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Tassilo Philipp
parents:
diff changeset
22
Tassilo Philipp
parents:
diff changeset
23 */
Tassilo Philipp
parents:
diff changeset
24
Tassilo Philipp
parents:
diff changeset
25
Tassilo Philipp
parents:
diff changeset
26 #include "../../dynload/dynload.h"
Tassilo Philipp
parents:
diff changeset
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
Tassilo Philipp
parents:
diff changeset
29
Tassilo Philipp
parents:
diff changeset
30 #include <string.h>
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
31 #include <sys/stat.h>
233
8216c86b4cbc - make dynload_plain test build on windows
Tassilo Philipp
parents: 223
diff changeset
32 #if defined(DC_WINDOWS)
8216c86b4cbc - make dynload_plain test build on windows
Tassilo Philipp
parents: 223
diff changeset
33 # include <io.h>
8216c86b4cbc - make dynload_plain test build on windows
Tassilo Philipp
parents: 223
diff changeset
34 # define F_OK 0
8216c86b4cbc - make dynload_plain test build on windows
Tassilo Philipp
parents: 223
diff changeset
35 #else
8216c86b4cbc - make dynload_plain test build on windows
Tassilo Philipp
parents: 223
diff changeset
36 # include <unistd.h>
8216c86b4cbc - make dynload_plain test build on windows
Tassilo Philipp
parents: 223
diff changeset
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
Tassilo Philipp
parents:
diff changeset
53
Tassilo Philipp
parents:
diff changeset
54
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
55 int strlen_utf8(const char *s)
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
56 {
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
57 int i=0, j=0;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
58 while(s[i])
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
59 j += ((s[i++] & 0xc0) != 0x80);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
60 return j;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
61 }
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
62
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
63
214
Tassilo Philipp
parents:
diff changeset
64 int main(int argc, char* argv[])
Tassilo Philipp
parents:
diff changeset
65 {
Tassilo Philipp
parents:
diff changeset
66 int r = 0, i;
Tassilo Philipp
parents:
diff changeset
67 void* p;
Tassilo Philipp
parents:
diff changeset
68 DLLib* pLib;
233
8216c86b4cbc - make dynload_plain test build on windows
Tassilo Philipp
parents: 223
diff changeset
69 DLSyms* pSyms;
214
Tassilo Philipp
parents:
diff changeset
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
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
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
Tassilo Philipp
parents:
diff changeset
80 "/lib/libc.so",
272
a94a9a83dae6 - dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents: 254
diff changeset
81 "/lib32/libc.so",
214
Tassilo Philipp
parents:
diff changeset
82 "/lib64/libc.so",
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
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
Tassilo Philipp
parents:
diff changeset
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
808045066f12 - dynload_plain test ReactOS support
Tassilo Philipp
parents: 233
diff changeset
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
808045066f12 - dynload_plain test ReactOS support
Tassilo Philipp
parents: 233
diff changeset
90 "C:\\Windows\\system32\\msvcrt.dll"
214
Tassilo Philipp
parents:
diff changeset
91 };
Tassilo Philipp
parents:
diff changeset
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
Tassilo Philipp
parents:
diff changeset
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
Tassilo Philipp
parents:
diff changeset
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
Tassilo Philipp
parents:
diff changeset
110 }
Tassilo Philipp
parents:
diff changeset
111
Tassilo Philipp
parents:
diff changeset
112 if(path) {
Tassilo Philipp
parents:
diff changeset
113 printf("using clib to test at: %s\n", path);
Tassilo Philipp
parents:
diff changeset
114 ++r;
Tassilo Philipp
parents:
diff changeset
115
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
116 /* dl*Library tests */
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
117 /* ---------------- */
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
118 pLib = dlLoadLibrary(path); /* check if we can load a lib */
214
Tassilo Philipp
parents:
diff changeset
119 if(pLib) {
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
120 char queriedPath[200]; /* enough for our test paths */
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
121 int bs;
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
122
214
Tassilo Philipp
parents:
diff changeset
123 printf("pLib handle: %p\n", pLib);
Tassilo Philipp
parents:
diff changeset
124 ++r;
Tassilo Philipp
parents:
diff changeset
125
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
126 p = dlFindSymbol(pLib, "printf"); /* check if we can lookup a symbol */
214
Tassilo Philipp
parents:
diff changeset
127 printf("printf at: %p\n", p);
Tassilo Philipp
parents:
diff changeset
128 r += (p != NULL);
Tassilo Philipp
parents:
diff changeset
129
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
130 bs = dlGetLibraryPath(pLib, queriedPath, 200);
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
131 if(bs && bs <= 200) {
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
132 int b, bs_;
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
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
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
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
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
162 /* check correct bufsize retval */
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
163 b = (bs == strlen(queriedPath) + 1);
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
164 printf("looked up path's needed buffer size (%d) computed correctly 1/2: %d\n", bs, b);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
165 r += b;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
166
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
167 /* check perfect fitting bufsize */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
168 queriedPath[0] = 0;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
169 bs_ = dlGetLibraryPath(pLib, queriedPath, bs);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
170 b = (bs == bs_ && bs_ == strlen(queriedPath) + 1);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
171 printf("looked up path's needed buffer size (%d) computed correctly 2/2: %d\n", bs_, b);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
172 r += b;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
173
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
174 /* check if dlGetLibraryPath returns size required if bufsize too small */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
175 queriedPath[0] = 0;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
176 bs_ = dlGetLibraryPath(pLib, queriedPath, 1); /* tiny max buffer size */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
177 b = (bs == bs_ && strlen(queriedPath) == 0); /* nothing copied */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
178 printf("path lookup size requirement (%d) correctly returned: %d\n", bs_, b);
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
179 r += b;
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
180 }
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
181 else
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
182 printf("failed to query lib path using lib's handle\n");
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
183
214
Tassilo Philipp
parents:
diff changeset
184 dlFreeLibrary(pLib);
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
185
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
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
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
188 printf("path lookup failed as expected with bad lib handle: %d\n", bs == 0);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
189 r += (bs == 0);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
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
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
202 /* test UTF-8 path through dummy library that's created by this test's build */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
203 {
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
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
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
206
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
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
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
209 r += (p != NULL);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
210
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
211 if(pLib) {
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
212 /* get UTF-8 path back */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
213 bs = dlGetLibraryPath((DLLib*)pLib, queriedPath, 200);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
214 if(bs && bs <= 200) {
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
215 nu8c = strlen_utf8(queriedPath); /* num of UTF-8 chars is as big as ... */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
216 b = (bs > 0) && (nu8c == bs-2); /* ... buffer size minus 2 (b/c of one 2-byte UTF-8 char and "\0") */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
217 printf("UTF-8 path of lib looked up via handle: %s\n", queriedPath);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
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);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
219 r += b;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
220
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
221 dlFreeLibrary(pLib);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
222 }
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
223 else
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
224 printf("failed to query UTF-8 lib path using lib's handle\n");
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
225 }
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
226 }
214
Tassilo Philipp
parents:
diff changeset
227 }
Tassilo Philipp
parents:
diff changeset
228 else
Tassilo Philipp
parents:
diff changeset
229 printf("unable to open library %s\n", path);
Tassilo Philipp
parents:
diff changeset
230
Tassilo Philipp
parents:
diff changeset
231
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
232 /* dlSyms* tests (intentionally after freeing lib above, as they work standalone) */
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
233 /* ------------- */
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
234 pSyms = dlSymsInit(path); /* check if we can iterate over symbols - init */
214
Tassilo Philipp
parents:
diff changeset
235 if(pSyms) {
Tassilo Philipp
parents:
diff changeset
236 int n;
Tassilo Philipp
parents:
diff changeset
237 const char* name;
Tassilo Philipp
parents:
diff changeset
238
Tassilo Philipp
parents:
diff changeset
239 printf("pSyms handle: %p\n", pSyms);
Tassilo Philipp
parents:
diff changeset
240 ++r;
Tassilo Philipp
parents:
diff changeset
241
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
242 n = dlSymsCount(pSyms); /* check if there are some syms to iterate over */
214
Tassilo Philipp
parents:
diff changeset
243 printf("num of libc symbols: %d\n", n);
Tassilo Philipp
parents:
diff changeset
244 r += (n > 0);
Tassilo Philipp
parents:
diff changeset
245
Tassilo Philipp
parents:
diff changeset
246 for(i=0; i<n; ++i) {
Tassilo Philipp
parents:
diff changeset
247 name = dlSymsName(pSyms, i);
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
248 if(name && strcmp(name, "printf") == 0) { /* check if we find "printf" also in iterated symbols */
214
Tassilo Philipp
parents:
diff changeset
249 ++r;
Tassilo Philipp
parents:
diff changeset
250 break;
Tassilo Philipp
parents:
diff changeset
251 }
Tassilo Philipp
parents:
diff changeset
252 }
Tassilo Philipp
parents:
diff changeset
253 printf("printf symbol found by iteration: %d\n", i<n);
Tassilo Philipp
parents:
diff changeset
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
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
256 r += (name && strcmp(name, "printf") == 0); /* check if we can lookup "printf" by index */
218
cb56f077fd37 - dynload_plain test fixes
Tassilo Philipp
parents: 214
diff changeset
257 printf("printf symbol name by index: %s\n", name?name:"");
214
Tassilo Philipp
parents:
diff changeset
258
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
259 pLib = dlLoadLibrary(path); /* check if we can resolve ptr -> name, */
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
260 if(pLib) { /* need to lookup by name again, first */
214
Tassilo Philipp
parents:
diff changeset
261 p = dlFindSymbol(pLib, "printf");
Tassilo Philipp
parents:
diff changeset
262 name = dlSymsNameFromValue(pSyms, p);
218
cb56f077fd37 - dynload_plain test fixes
Tassilo Philipp
parents: 214
diff changeset
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
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
265 if(strcmp(name, "printf") == 0)
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
266 ++r;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
267 else {
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
268 /* Symbol name returned might be an "alias". In that case, check address again (full lookup to be sure). */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
269 void* p0 = dlFindSymbol(pLib, name);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
270 printf("lookup by address returned different name (%s), which is alias of printf: %d\n", name, (p==p0));
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
271 r += (p == p0);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
272 }
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
273 }
214
Tassilo Philipp
parents:
diff changeset
274 dlFreeLibrary(pLib);
Tassilo Philipp
parents:
diff changeset
275 }
Tassilo Philipp
parents:
diff changeset
276
Tassilo Philipp
parents:
diff changeset
277 dlSymsCleanup(pSyms);
Tassilo Philipp
parents:
diff changeset
278 }
Tassilo Philipp
parents:
diff changeset
279 else
Tassilo Philipp
parents:
diff changeset
280 printf("dlSymsInit failed\n");
Tassilo Philipp
parents:
diff changeset
281 }
Tassilo Philipp
parents:
diff changeset
282
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
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
Tassilo Philipp
parents:
diff changeset
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
Tassilo Philipp
parents:
diff changeset
289 return !r;
Tassilo Philipp
parents:
diff changeset
290 }
Tassilo Philipp
parents:
diff changeset
291