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
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
281
f5577f6bf97a - file header cleanups for release
Tassilo Philipp
parents: 272
diff changeset
9 Copyright (c) 2017-2018 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"
Tassilo Philipp
parents:
diff changeset
28
Tassilo Philipp
parents:
diff changeset
29 #include <string.h>
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
30 #include <sys/stat.h>
233
8216c86b4cbc - make dynload_plain test build on windows
Tassilo Philipp
parents: 223
diff changeset
31 #if defined(DC_WINDOWS)
8216c86b4cbc - make dynload_plain test build on windows
Tassilo Philipp
parents: 223
diff changeset
32 # include <io.h>
8216c86b4cbc - make dynload_plain test build on windows
Tassilo Philipp
parents: 223
diff changeset
33 # define F_OK 0
8216c86b4cbc - make dynload_plain test build on windows
Tassilo Philipp
parents: 223
diff changeset
34 #else
8216c86b4cbc - make dynload_plain test build on windows
Tassilo Philipp
parents: 223
diff changeset
35 # include <unistd.h>
8216c86b4cbc - make dynload_plain test build on windows
Tassilo Philipp
parents: 223
diff changeset
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
Tassilo Philipp
parents:
diff changeset
52
Tassilo Philipp
parents:
diff changeset
53
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
54 int strlen_utf8(const char *s)
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
55 {
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
56 int i=0, j=0;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
57 while(s[i])
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
58 j += ((s[i++] & 0xc0) != 0x80);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
59 return j;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
60 }
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
214
Tassilo Philipp
parents:
diff changeset
63 int main(int argc, char* argv[])
Tassilo Philipp
parents:
diff changeset
64 {
Tassilo Philipp
parents:
diff changeset
65 int r = 0, i;
Tassilo Philipp
parents:
diff changeset
66 void* p;
Tassilo Philipp
parents:
diff changeset
67 DLLib* pLib;
233
8216c86b4cbc - make dynload_plain test build on windows
Tassilo Philipp
parents: 223
diff changeset
68 DLSyms* pSyms;
214
Tassilo Philipp
parents:
diff changeset
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
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
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
Tassilo Philipp
parents:
diff changeset
79 "/lib/libc.so",
272
a94a9a83dae6 - dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents: 254
diff changeset
80 "/lib32/libc.so",
214
Tassilo Philipp
parents:
diff changeset
81 "/lib64/libc.so",
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
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
Tassilo Philipp
parents:
diff changeset
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
808045066f12 - dynload_plain test ReactOS support
Tassilo Philipp
parents: 233
diff changeset
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
808045066f12 - dynload_plain test ReactOS support
Tassilo Philipp
parents: 233
diff changeset
89 "C:\\Windows\\system32\\msvcrt.dll"
214
Tassilo Philipp
parents:
diff changeset
90 };
Tassilo Philipp
parents:
diff changeset
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
Tassilo Philipp
parents:
diff changeset
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
Tassilo Philipp
parents:
diff changeset
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
Tassilo Philipp
parents:
diff changeset
107 }
Tassilo Philipp
parents:
diff changeset
108
Tassilo Philipp
parents:
diff changeset
109 if(path) {
Tassilo Philipp
parents:
diff changeset
110 printf("using clib to test at: %s\n", path);
Tassilo Philipp
parents:
diff changeset
111 ++r;
Tassilo Philipp
parents:
diff changeset
112
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
113 /* dl*Library tests */
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
114 /* ---------------- */
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
115 pLib = dlLoadLibrary(path); /* check if we can load a lib */
214
Tassilo Philipp
parents:
diff changeset
116 if(pLib) {
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
117 char queriedPath[200]; /* enough for our test paths */
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
118 int bs;
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
119
214
Tassilo Philipp
parents:
diff changeset
120 printf("pLib handle: %p\n", pLib);
Tassilo Philipp
parents:
diff changeset
121 ++r;
Tassilo Philipp
parents:
diff changeset
122
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
123 p = dlFindSymbol(pLib, "printf"); /* check if we can lookup a symbol */
214
Tassilo Philipp
parents:
diff changeset
124 printf("printf at: %p\n", p);
Tassilo Philipp
parents:
diff changeset
125 r += (p != NULL);
Tassilo Philipp
parents:
diff changeset
126
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
127 bs = dlGetLibraryPath(pLib, queriedPath, 200);
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
128 if(bs && bs <= 200) {
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
129 int b, bs_;
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
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
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
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
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
159 /* check correct bufsize retval */
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
160 b = (bs == strlen(queriedPath) + 1);
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
161 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
162 r += b;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
163
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
164 /* check perfect fitting bufsize */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
165 queriedPath[0] = 0;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
166 bs_ = dlGetLibraryPath(pLib, queriedPath, bs);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
167 b = (bs == bs_ && bs_ == strlen(queriedPath) + 1);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
168 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
169 r += b;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
170
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
171 /* check if dlGetLibraryPath returns size required if bufsize too small */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
172 queriedPath[0] = 0;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
173 bs_ = dlGetLibraryPath(pLib, queriedPath, 1); /* tiny max buffer size */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
174 b = (bs == bs_ && strlen(queriedPath) == 0); /* nothing copied */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
175 printf("path lookup size requirement (%d) correctly returned: %d\n", bs_, b);
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
176 r += b;
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
177 }
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
178 else
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
179 printf("failed to query lib path using lib's handle\n");
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
180
214
Tassilo Philipp
parents:
diff changeset
181 dlFreeLibrary(pLib);
308
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
182
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
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
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
185 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
186 r += (bs == 0);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
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
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
199 /* 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
200 {
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
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
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 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
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
206 r += (p != NULL);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
207
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
208 if(pLib) {
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
209 /* get UTF-8 path back */
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
210 bs = dlGetLibraryPath((DLLib*)pLib, queriedPath, 200);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
211 if(bs && bs <= 200) {
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
212 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
213 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
214 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
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);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
216 r += b;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
217
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
218 dlFreeLibrary(pLib);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
219 }
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
220 else
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
221 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
222 }
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
223 }
214
Tassilo Philipp
parents:
diff changeset
224 }
Tassilo Philipp
parents:
diff changeset
225 else
Tassilo Philipp
parents:
diff changeset
226 printf("unable to open library %s\n", path);
Tassilo Philipp
parents:
diff changeset
227
Tassilo Philipp
parents:
diff changeset
228
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
229 /* dlSyms* tests (intentionally after freeing lib above, as they work standalone) */
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
230 /* ------------- */
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
231 pSyms = dlSymsInit(path); /* check if we can iterate over symbols - init */
214
Tassilo Philipp
parents:
diff changeset
232 if(pSyms) {
Tassilo Philipp
parents:
diff changeset
233 int n;
Tassilo Philipp
parents:
diff changeset
234 const char* name;
Tassilo Philipp
parents:
diff changeset
235
Tassilo Philipp
parents:
diff changeset
236 printf("pSyms handle: %p\n", pSyms);
Tassilo Philipp
parents:
diff changeset
237 ++r;
Tassilo Philipp
parents:
diff changeset
238
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
239 n = dlSymsCount(pSyms); /* check if there are some syms to iterate over */
214
Tassilo Philipp
parents:
diff changeset
240 printf("num of libc symbols: %d\n", n);
Tassilo Philipp
parents:
diff changeset
241 r += (n > 0);
Tassilo Philipp
parents:
diff changeset
242
Tassilo Philipp
parents:
diff changeset
243 for(i=0; i<n; ++i) {
Tassilo Philipp
parents:
diff changeset
244 name = dlSymsName(pSyms, i);
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
245 if(name && strcmp(name, "printf") == 0) { /* check if we find "printf" also in iterated symbols */
214
Tassilo Philipp
parents:
diff changeset
246 ++r;
Tassilo Philipp
parents:
diff changeset
247 break;
Tassilo Philipp
parents:
diff changeset
248 }
Tassilo Philipp
parents:
diff changeset
249 }
Tassilo Philipp
parents:
diff changeset
250 printf("printf symbol found by iteration: %d\n", i<n);
Tassilo Philipp
parents:
diff changeset
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
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
253 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
254 printf("printf symbol name by index: %s\n", name?name:"");
214
Tassilo Philipp
parents:
diff changeset
255
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
256 pLib = dlLoadLibrary(path); /* check if we can resolve ptr -> name, */
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
257 if(pLib) { /* need to lookup by name again, first */
214
Tassilo Philipp
parents:
diff changeset
258 p = dlFindSymbol(pLib, "printf");
Tassilo Philipp
parents:
diff changeset
259 name = dlSymsNameFromValue(pSyms, p);
218
cb56f077fd37 - dynload_plain test fixes
Tassilo Philipp
parents: 214
diff changeset
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
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
262 if(strcmp(name, "printf") == 0)
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
263 ++r;
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
264 else {
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
265 /* 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
266 void* p0 = dlFindSymbol(pLib, name);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
267 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
268 r += (p == p0);
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
269 }
7c6f19d42b31 dynload UTF-8 support for library paths:
Tassilo Philipp
parents: 281
diff changeset
270 }
214
Tassilo Philipp
parents:
diff changeset
271 dlFreeLibrary(pLib);
Tassilo Philipp
parents:
diff changeset
272 }
Tassilo Philipp
parents:
diff changeset
273
Tassilo Philipp
parents:
diff changeset
274 dlSymsCleanup(pSyms);
Tassilo Philipp
parents:
diff changeset
275 }
Tassilo Philipp
parents:
diff changeset
276 else
Tassilo Philipp
parents:
diff changeset
277 printf("dlSymsInit failed\n");
Tassilo Philipp
parents:
diff changeset
278 }
Tassilo Philipp
parents:
diff changeset
279
242
85b61e8facfe dynload:
Tassilo Philipp
parents: 234
diff changeset
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
Tassilo Philipp
parents:
diff changeset
282 printf("result: dynload_plain: %d\n", r);
Tassilo Philipp
parents:
diff changeset
283 return !r;
Tassilo Philipp
parents:
diff changeset
284 }
Tassilo Philipp
parents:
diff changeset
285