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