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