comparison test/dynload_plain/dynload_plain.c @ 242:85b61e8facfe

dynload: - added new function dlGetLibraryPath to get path of already loaded lib - covered new function in dynload_plain test - cleanups/cosmetics for consistency
author Tassilo Philipp
date Thu, 04 May 2017 13:42:17 +0200
parents 808045066f12
children 0ba6189a51dd
comparison
equal deleted inserted replaced
241:cde7b1f3b8f2 242:85b61e8facfe
25 25
26 #include "../../dynload/dynload.h" 26 #include "../../dynload/dynload.h"
27 #include "../common/platformInit.h" 27 #include "../common/platformInit.h"
28 28
29 #include <string.h> 29 #include <string.h>
30 #include <sys/stat.h>
30 #if defined(DC_WINDOWS) 31 #if defined(DC_WINDOWS)
31 # include <io.h> 32 # include <io.h>
32 # define F_OK 0 33 # define F_OK 0
33 #else 34 #else
34 # include <unistd.h> 35 # include <unistd.h>
40 int r = 0, i; 41 int r = 0, i;
41 void* p; 42 void* p;
42 DLLib* pLib; 43 DLLib* pLib;
43 DLSyms* pSyms; 44 DLSyms* pSyms;
44 const char* path = NULL; 45 const char* path = NULL;
45 const char* clibs[] = { // hacky/lazy list of some clib paths per platform 46 const char* clibs[] = { /* hacky/lazy list of some clib paths per platform */
46 "/lib/libc.so", 47 "/lib/libc.so",
47 "/lib/libc.so.6", 48 "/lib/libc.so.6",
48 "/lib/libc.so.7", 49 "/lib/libc.so.7",
50 "/lib/libc.so.39.3", /* hack: for OpenBSD used in dyncall test env */
49 "/lib64/libc.so", 51 "/lib64/libc.so",
50 "/lib64/libc.so.6", 52 "/lib64/libc.so.6",
51 "/lib64/libc.so.7", 53 "/lib64/libc.so.7",
52 "/lib32/libc.so", 54 "/lib32/libc.so",
53 "/lib32/libc.so.6", 55 "/lib32/libc.so.6",
54 "/lib32/libc.so.7", 56 "/lib32/libc.so.7",
55 "/usr/lib/system/libsystem_c.dylib", 57 "/usr/lib/libc.so",
58 "/usr/lib/libc.so.6",
59 "/usr/lib/libc.so.7",
60 "/usr/lib/system/libsystem_c.dylib",
56 "/usr/lib/libc.dylib", 61 "/usr/lib/libc.dylib",
57 "\\ReactOS\\system32\\msvcrt.dll", 62 "\\ReactOS\\system32\\msvcrt.dll",
58 "C:\\ReactOS\\system32\\msvcrt.dll", 63 "C:\\ReactOS\\system32\\msvcrt.dll",
59 "\\Windows\\system32\\msvcrt.dll", 64 "\\Windows\\system32\\msvcrt.dll",
60 "C:\\Windows\\system32\\msvcrt.dll" 65 "C:\\Windows\\system32\\msvcrt.dll"
70 75
71 if(path) { 76 if(path) {
72 printf("using clib to test at: %s\n", path); 77 printf("using clib to test at: %s\n", path);
73 ++r; 78 ++r;
74 79
75 // dl*Library tests 80 /* dl*Library tests */
76 // -------- 81 /* ---------------- */
77 pLib = dlLoadLibrary(path); // check if we can load a lib 82 pLib = dlLoadLibrary(path); /* check if we can load a lib */
78 if(pLib) { 83 if(pLib) {
84 char queriedPath[200]; /* enough for our test paths */
85 int bs;
86
79 printf("pLib handle: %p\n", pLib); 87 printf("pLib handle: %p\n", pLib);
80 ++r; 88 ++r;
81 89
82 p = dlFindSymbol(pLib, "printf"); // check if we can lookup a symbol 90 p = dlFindSymbol(pLib, "printf"); /* check if we can lookup a symbol */
83 printf("printf at: %p\n", p); 91 printf("printf at: %p\n", p);
84 r += (p != NULL); 92 r += (p != NULL);
93
94 bs = dlGetLibraryPath(pLib, queriedPath, 200);
95 if(bs && bs <= 200) {
96 struct stat st0, st1; /* to check if same file */
97 int b;
98 printf("path of lib looked up via handle: %s\n", queriedPath);
99 b = (stat(path, &st0) != -1) && (stat(queriedPath, &st1) != -1);
100 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));
101 r += b && (st0.st_ino == st1.st_ino); /* compare if same lib using inode */
102 /*@@@ check of resolved path is absolute*/
103
104 /* check correct bufsize retval */
105 b = (bs == strlen(queriedPath) + 1);
106 printf("looked up path's needed buffer size (%d) computed correctly: %d\n", bs, b);
107 r += b;
108 }
109 else
110 printf("failed to query lib path using lib's handle\n");
85 111
86 dlFreeLibrary(pLib); 112 dlFreeLibrary(pLib);
87 } 113 }
88 else 114 else
89 printf("unable to open library %s\n", path); 115 printf("unable to open library %s\n", path);
90 116
91 117
92 // dlSyms* tests (intentionally after freeing lib above, as they work standalone) 118 /* dlSyms* tests (intentionally after freeing lib above, as they work standalone) */
93 // -------- 119 /* ------------- */
94 pSyms = dlSymsInit(path); // check if we can iterate over symbols - init 120 pSyms = dlSymsInit(path); /* check if we can iterate over symbols - init */
95 if(pSyms) { 121 if(pSyms) {
96 int n; 122 int n;
97 const char* name; 123 const char* name;
98 124
99 printf("pSyms handle: %p\n", pSyms); 125 printf("pSyms handle: %p\n", pSyms);
100 ++r; 126 ++r;
101 127
102 n = dlSymsCount(pSyms); // check if there are some syms to iterate over 128 n = dlSymsCount(pSyms); /* check if there are some syms to iterate over */
103 printf("num of libc symbols: %d\n", n); 129 printf("num of libc symbols: %d\n", n);
104 r += (n > 0); 130 r += (n > 0);
105 131
106 for(i=0; i<n; ++i) { 132 for(i=0; i<n; ++i) {
107 name = dlSymsName(pSyms, i); 133 name = dlSymsName(pSyms, i);
108 if(name && strcmp(name, "printf") == 0) { // check if we find "printf" also in iterated symbols 134 if(name && strcmp(name, "printf") == 0) { /* check if we find "printf" also in iterated symbols */
109 ++r; 135 ++r;
110 break; 136 break;
111 } 137 }
112 } 138 }
113 printf("printf symbol found by iteration: %d\n", i<n); 139 printf("printf symbol found by iteration: %d\n", i<n);
114 140
115 name = (i<n) ? dlSymsName(pSyms, i) : NULL; 141 name = (i<n) ? dlSymsName(pSyms, i) : NULL;
116 r += (name && strcmp(name, "printf") == 0); // check if we can lookup "printf" by index 142 r += (name && strcmp(name, "printf") == 0); /* check if we can lookup "printf" by index */
117 printf("printf symbol name by index: %s\n", name?name:""); 143 printf("printf symbol name by index: %s\n", name?name:"");
118 144
119 pLib = dlLoadLibrary(path); // check if we can resolve ptr -> name, 145 pLib = dlLoadLibrary(path); /* check if we can resolve ptr -> name, */
120 if(pLib) { // need to lookup by name again, first 146 if(pLib) { /* need to lookup by name again, first */
121 p = dlFindSymbol(pLib, "printf"); 147 p = dlFindSymbol(pLib, "printf");
122 name = dlSymsNameFromValue(pSyms, p); 148 name = dlSymsNameFromValue(pSyms, p);
123 printf("printf symbol name by its own address (%p): %s\n", p, name?name:""); 149 printf("printf symbol name by its own address (%p): %s\n", p, name?name:"");
124 r += (name && strcmp(name, "printf") == 0); 150 r += (name && strcmp(name, "printf") == 0);
125 dlFreeLibrary(pLib); 151 dlFreeLibrary(pLib);
129 } 155 }
130 else 156 else
131 printf("dlSymsInit failed\n"); 157 printf("dlSymsInit failed\n");
132 } 158 }
133 159
134 // All worked if we got a score of 6 right ones 160 /* Check final score of right ones to see if all worked */
135 r = (r == 8); 161 r = (r == 10);
136 printf("result: dynload_plain: %d\n", r); 162 printf("result: dynload_plain: %d\n", r);
137 return !r; 163 return !r;
138 } 164 }
139 165