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