Mercurial > pub > dyncall > dyncall
annotate test/dynload_plain/dynload_plain.c @ 281:f5577f6bf97a
- file header cleanups for release
author | Tassilo Philipp |
---|---|
date | Thu, 19 Apr 2018 20:10:26 +0200 |
parents | a94a9a83dae6 |
children | 7c6f19d42b31 |
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 | |
39 int main(int argc, char* argv[]) | |
40 { | |
41 int r = 0, i; | |
42 void* p; | |
43 DLLib* pLib; | |
233 | 44 DLSyms* pSyms; |
214 | 45 const char* path = NULL; |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
46 /* 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
|
47 /* 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
|
48 const char* clibs[] = { |
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
49 #if defined(DEF_C_DYLIB) |
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
50 DEF_C_DYLIB, |
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
51 #endif |
214 | 52 "/lib/libc.so", |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
53 "/lib32/libc.so", |
214 | 54 "/lib64/libc.so", |
242 | 55 "/usr/lib/libc.so", |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
56 "/usr/lib/system/libsystem_c.dylib", /* macos */ |
214 | 57 "/usr/lib/libc.dylib", |
248 | 58 "/boot/system/lib/libroot.so", /* Haiku */ |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
59 "\\ReactOS\\system32\\msvcrt.dll", /* ReactOS */ |
234 | 60 "C:\\ReactOS\\system32\\msvcrt.dll", |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
61 "\\Windows\\system32\\msvcrt.dll", /* Windows */ |
234 | 62 "C:\\Windows\\system32\\msvcrt.dll" |
214 | 63 }; |
64 | |
272
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
65 /* use first matching path of hacky hardcoded list, above */ |
214 | 66 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
|
67 if(access(clibs[i], F_OK) != -1) { |
214 | 68 path = clibs[i]; |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
69 break; |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
70 } |
214 | 71 } |
72 | |
73 if(path) { | |
74 printf("using clib to test at: %s\n", path); | |
75 ++r; | |
76 | |
242 | 77 /* dl*Library tests */ |
78 /* ---------------- */ | |
79 pLib = dlLoadLibrary(path); /* check if we can load a lib */ | |
214 | 80 if(pLib) { |
242 | 81 char queriedPath[200]; /* enough for our test paths */ |
82 int bs; | |
83 | |
214 | 84 printf("pLib handle: %p\n", pLib); |
85 ++r; | |
86 | |
242 | 87 p = dlFindSymbol(pLib, "printf"); /* check if we can lookup a symbol */ |
214 | 88 printf("printf at: %p\n", p); |
89 r += (p != NULL); | |
90 | |
242 | 91 bs = dlGetLibraryPath(pLib, queriedPath, 200); |
92 if(bs && bs <= 200) { | |
93 struct stat st0, st1; /* to check if same file */ | |
94 int b; | |
95 printf("path of lib looked up via handle: %s\n", queriedPath); | |
96 b = (stat(path, &st0) != -1) && (stat(queriedPath, &st1) != -1); | |
245
0ba6189a51dd
- dynload dlGetLibraryPath simplifications:
Tassilo Philipp
parents:
242
diff
changeset
|
97 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 | 98 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
|
99 /*@@@ check if resolved path is absolute*/ |
242 | 100 |
101 /* check correct bufsize retval */ | |
102 b = (bs == strlen(queriedPath) + 1); | |
103 printf("looked up path's needed buffer size (%d) computed correctly: %d\n", bs, b); | |
104 r += b; | |
105 } | |
106 else | |
107 printf("failed to query lib path using lib's handle\n"); | |
108 | |
214 | 109 dlFreeLibrary(pLib); |
110 } | |
111 else | |
112 printf("unable to open library %s\n", path); | |
113 | |
114 | |
242 | 115 /* dlSyms* tests (intentionally after freeing lib above, as they work standalone) */ |
116 /* ------------- */ | |
117 pSyms = dlSymsInit(path); /* check if we can iterate over symbols - init */ | |
214 | 118 if(pSyms) { |
119 int n; | |
120 const char* name; | |
121 | |
122 printf("pSyms handle: %p\n", pSyms); | |
123 ++r; | |
124 | |
242 | 125 n = dlSymsCount(pSyms); /* check if there are some syms to iterate over */ |
214 | 126 printf("num of libc symbols: %d\n", n); |
127 r += (n > 0); | |
128 | |
129 for(i=0; i<n; ++i) { | |
130 name = dlSymsName(pSyms, i); | |
242 | 131 if(name && strcmp(name, "printf") == 0) { /* check if we find "printf" also in iterated symbols */ |
214 | 132 ++r; |
133 break; | |
134 } | |
135 } | |
136 printf("printf symbol found by iteration: %d\n", i<n); | |
137 | |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
138 name = (i<n) ? dlSymsName(pSyms, i) : NULL; |
242 | 139 r += (name && strcmp(name, "printf") == 0); /* check if we can lookup "printf" by index */ |
218 | 140 printf("printf symbol name by index: %s\n", name?name:""); |
214 | 141 |
242 | 142 pLib = dlLoadLibrary(path); /* check if we can resolve ptr -> name, */ |
143 if(pLib) { /* need to lookup by name again, first */ | |
214 | 144 p = dlFindSymbol(pLib, "printf"); |
145 name = dlSymsNameFromValue(pSyms, p); | |
218 | 146 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
|
147 if(name) { |
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
148 if(strcmp(name, "printf") == 0) |
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
149 ++r; |
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
150 else { |
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
151 /* Symbol name returned might be an "alias". In that case, check address again (full lookup to be sure). */ |
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
152 void* p0 = dlFindSymbol(pLib, name); |
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
153 printf("lookup by address returned different name (%s), which is alias of printf: %d\n", name, (p==p0)); |
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
154 r += (p == p0); |
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
155 } |
a94a9a83dae6
- dynload_plain test handling symbol aliases, now
Tassilo Philipp
parents:
254
diff
changeset
|
156 } |
214 | 157 dlFreeLibrary(pLib); |
158 } | |
159 | |
160 dlSymsCleanup(pSyms); | |
161 } | |
162 else | |
163 printf("dlSymsInit failed\n"); | |
164 } | |
165 | |
242 | 166 /* Check final score of right ones to see if all worked */ |
167 r = (r == 10); | |
214 | 168 printf("result: dynload_plain: %d\n", r); |
169 return !r; | |
170 } | |
171 |