Mercurial > pub > dyncall > dyncall
annotate test/dynload_plain/dynload_plain.c @ 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
- dynload_plain test
* now testing getting exec's path
* workdir independent
author | Tassilo Philipp |
---|---|
date | Wed, 06 Nov 2019 12:32:53 +0100 |
parents | 73b5b9e224e2 |
children | 3124f4c4f293 |
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 | |
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
|
37 #include <libgen.h> |
214 | 38 |
39 | |
308 | 40 int strlen_utf8(const char *s) |
41 { | |
42 int i=0, j=0; | |
43 while(s[i]) | |
44 j += ((s[i++] & 0xc0) != 0x80); | |
45 return j; | |
46 } | |
47 | |
48 | |
214 | 49 int main(int argc, char* argv[]) |
50 { | |
51 int r = 0, i; | |
52 void* p; | |
53 DLLib* pLib; | |
233 | 54 DLSyms* pSyms; |
214 | 55 const char* path = NULL; |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
56 /* 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
|
57 /* 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
|
58 const char* clibs[] = { |
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
59 #if defined(DEF_C_DYLIB) |
308 | 60 DEF_C_DYLIB, |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
61 #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
|
62 /* fallback guessing if not provided by Makefile */ |
214 | 63 "/lib/libc.so", |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
64 "/lib32/libc.so", |
214 | 65 "/lib64/libc.so", |
242 | 66 "/usr/lib/libc.so", |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
67 "/usr/lib/system/libsystem_c.dylib", /* macos */ |
214 | 68 "/usr/lib/libc.dylib", |
313
73b5b9e224e2
- dynload_plain test: stability fix for picking right testing lib on build
Tassilo Philipp
parents:
312
diff
changeset
|
69 "/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
|
70 "\\ReactOS\\system32\\msvcrt.dll", /* ReactOS */ |
234 | 71 "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
|
72 "\\Windows\\system32\\msvcrt.dll", /* Windows */ |
234 | 73 "C:\\Windows\\system32\\msvcrt.dll" |
214 | 74 }; |
75 | |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
76 /* use first matching path of hacky hardcoded list, above */ |
214 | 77 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
|
78 if(access(clibs[i], F_OK) != -1) { |
214 | 79 path = clibs[i]; |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
80 break; |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
81 } |
214 | 82 } |
83 | |
84 if(path) { | |
85 printf("using clib to test at: %s\n", path); | |
86 ++r; | |
87 | |
242 | 88 /* dl*Library tests */ |
89 /* ---------------- */ | |
90 pLib = dlLoadLibrary(path); /* check if we can load a lib */ | |
214 | 91 if(pLib) { |
242 | 92 char queriedPath[200]; /* enough for our test paths */ |
93 int bs; | |
94 | |
214 | 95 printf("pLib handle: %p\n", pLib); |
96 ++r; | |
97 | |
242 | 98 p = dlFindSymbol(pLib, "printf"); /* check if we can lookup a symbol */ |
214 | 99 printf("printf at: %p\n", p); |
100 r += (p != NULL); | |
101 | |
242 | 102 bs = dlGetLibraryPath(pLib, queriedPath, 200); |
103 if(bs && bs <= 200) { | |
308 | 104 struct stat st0, st1; /* to check if same file */ |
105 int b, bs_; | |
242 | 106 printf("path of lib looked up via handle: %s\n", queriedPath); |
107 b = (stat(path, &st0) != -1) && (stat(queriedPath, &st1) != -1); | |
245
0ba6189a51dd
- dynload dlGetLibraryPath simplifications:
Tassilo Philipp
parents:
242
diff
changeset
|
108 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 | 109 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
|
110 /*@@@ check if resolved path is absolute*/ |
242 | 111 |
112 /* check correct bufsize retval */ | |
113 b = (bs == strlen(queriedPath) + 1); | |
308 | 114 printf("looked up path's needed buffer size (%d) computed correctly 1/2: %d\n", bs, b); |
115 r += b; | |
116 | |
117 /* check perfect fitting bufsize */ | |
118 queriedPath[0] = 0; | |
119 bs_ = dlGetLibraryPath(pLib, queriedPath, bs); | |
120 b = (bs == bs_ && bs_ == strlen(queriedPath) + 1); | |
121 printf("looked up path's needed buffer size (%d) computed correctly 2/2: %d\n", bs_, b); | |
122 r += b; | |
123 | |
124 /* check if dlGetLibraryPath returns size required if bufsize too small */ | |
125 queriedPath[0] = 0; | |
126 bs_ = dlGetLibraryPath(pLib, queriedPath, 1); /* tiny max buffer size */ | |
127 b = (bs == bs_ && strlen(queriedPath) == 0); /* nothing copied */ | |
128 printf("path lookup size requirement (%d) correctly returned: %d\n", bs_, b); | |
242 | 129 r += b; |
130 } | |
131 else | |
132 printf("failed to query lib path using lib's handle\n"); | |
133 | |
214 | 134 dlFreeLibrary(pLib); |
308 | 135 |
136 /* 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
|
137 bs = dlGetLibraryPath((DLLib*)&r/*dummy addr*/, queriedPath, 200); |
308 | 138 printf("path lookup failed as expected with bad lib handle: %d\n", bs == 0); |
139 r += (bs == 0); | |
140 | |
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
|
141 /* 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
|
142 { |
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
|
143 /* 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
|
144 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
|
145 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
|
146 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
|
147 |
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
|
148 /* 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
|
149 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
|
150 } |
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
|
151 |
308 | 152 /* test UTF-8 path through dummy library that's created by this test's build */ |
153 { | |
154 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
|
155 int nu8c, b; |
308 | 156 |
157 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
|
158 printf("pLib (loaded w/ UTF-8 path %s with wd being exec's dir) handle: %p\n", pathU8, pLib); |
308 | 159 r += (p != NULL); |
160 | |
161 if(pLib) { | |
162 /* get UTF-8 path back */ | |
163 bs = dlGetLibraryPath((DLLib*)pLib, queriedPath, 200); | |
164 if(bs && bs <= 200) { | |
165 nu8c = strlen_utf8(queriedPath); /* num of UTF-8 chars is as big as ... */ | |
166 b = (bs > 0) && (nu8c == bs-2); /* ... buffer size minus 2 (b/c of one 2-byte UTF-8 char and "\0") */ | |
167 printf("UTF-8 path of lib looked up via handle: %s\n", queriedPath); | |
168 printf("looked up UTF-8 path's needed buffer size (%d) for %d UTF-8 char string computed correctly: %d\n", bs, nu8c, b); | |
169 r += b; | |
170 | |
171 dlFreeLibrary(pLib); | |
172 } | |
173 else | |
174 printf("failed to query UTF-8 lib path using lib's handle\n"); | |
175 } | |
176 } | |
214 | 177 } |
178 else | |
179 printf("unable to open library %s\n", path); | |
180 | |
181 | |
242 | 182 /* dlSyms* tests (intentionally after freeing lib above, as they work standalone) */ |
183 /* ------------- */ | |
184 pSyms = dlSymsInit(path); /* check if we can iterate over symbols - init */ | |
214 | 185 if(pSyms) { |
186 int n; | |
187 const char* name; | |
188 | |
189 printf("pSyms handle: %p\n", pSyms); | |
190 ++r; | |
191 | |
242 | 192 n = dlSymsCount(pSyms); /* check if there are some syms to iterate over */ |
214 | 193 printf("num of libc symbols: %d\n", n); |
194 r += (n > 0); | |
195 | |
196 for(i=0; i<n; ++i) { | |
197 name = dlSymsName(pSyms, i); | |
242 | 198 if(name && strcmp(name, "printf") == 0) { /* check if we find "printf" also in iterated symbols */ |
214 | 199 ++r; |
200 break; | |
201 } | |
202 } | |
203 printf("printf symbol found by iteration: %d\n", i<n); | |
204 | |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
205 name = (i<n) ? dlSymsName(pSyms, i) : NULL; |
242 | 206 r += (name && strcmp(name, "printf") == 0); /* check if we can lookup "printf" by index */ |
218 | 207 printf("printf symbol name by index: %s\n", name?name:""); |
214 | 208 |
242 | 209 pLib = dlLoadLibrary(path); /* check if we can resolve ptr -> name, */ |
210 if(pLib) { /* need to lookup by name again, first */ | |
214 | 211 p = dlFindSymbol(pLib, "printf"); |
212 name = dlSymsNameFromValue(pSyms, p); | |
218 | 213 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
|
214 if(name) { |
308 | 215 if(strcmp(name, "printf") == 0) |
216 ++r; | |
217 else { | |
218 /* Symbol name returned might be an "alias". In that case, check address again (full lookup to be sure). */ | |
219 void* p0 = dlFindSymbol(pLib, name); | |
220 printf("lookup by address returned different name (%s), which is alias of printf: %d\n", name, (p==p0)); | |
221 r += (p == p0); | |
222 } | |
223 } | |
214 | 224 dlFreeLibrary(pLib); |
225 } | |
226 | |
227 dlSymsCleanup(pSyms); | |
228 } | |
229 else | |
230 printf("dlSymsInit failed\n"); | |
231 } | |
232 | |
242 | 233 /* 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
|
234 r = (r == 16); |
214 | 235 printf("result: dynload_plain: %d\n", r); |
236 return !r; | |
237 } | |
238 |