-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdll.cpp
More file actions
419 lines (322 loc) · 11.3 KB
/
dll.cpp
File metadata and controls
419 lines (322 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include "std.h"
#include "rope.h"
#ifndef NO_JNI
#include "hash.h"
#include "jni.h"
#include "natives.h"
#include "symbol.h"
#include "excep.h"
/* Set by call to initialise -- if true, prints out
results of dynamic method resolution */
static int verbose;
extern int nativeExtraArg(MethodBlock *mb);
extern uintptr_t *callJNIMethod(void *env, Class *classobj, char *sig, int extra,
uintptr_t *ostack, unsigned char *native_func, int args);
extern struct _JNINativeInterface Jam_JNINativeInterface;
extern JavaVM invokeIntf;
#define HASHTABSZE 1<<4
static HashTable hash_table;
void *lookupLoadedDlls(MethodBlock *mb);
#endif
/* Trace library loading and method lookup */
#ifdef TRACEDLL
#define TRACE(fmt, ...) jam_printf(fmt, ## __VA_ARGS__)
#else
#define TRACE(fmt, ...)
#endif
char *mangleString(char *utf8) {
int len = utf8Len(utf8);
unsigned short *unicode = (unsigned short*) sysMalloc(len * 2);
char *mangled, *mngldPtr;
int i, mangledLen = 0;
convertUtf8(utf8, unicode);
/* Work out the length of the mangled string */
for(i = 0; i < len; i++) {
unsigned short c = unicode[i];
switch(c) {
case '_':
case ';':
case '[':
mangledLen += 2;
break;
default:
mangledLen += isalnum(c) ? 1 : 6;
break;
}
}
mangled = mngldPtr = (char*) sysMalloc(mangledLen + 1);
/* Construct the mangled string */
for(i = 0; i < len; i++) {
unsigned short c = unicode[i];
switch(c) {
case '_':
*mngldPtr++ = '_';
*mngldPtr++ = '1';
break;
case ';':
*mngldPtr++ = '_';
*mngldPtr++ = '2';
break;
case '[':
*mngldPtr++ = '_';
*mngldPtr++ = '3';
break;
case '/':
*mngldPtr++ = '_';
break;
default:
if(isalnum(c))
*mngldPtr++ = c;
else
mngldPtr += sprintf(mngldPtr, "_0%04x", c);
break;
}
}
*mngldPtr = '\0';
sysFree(unicode);
return mangled;
}
char *mangleClassAndMethodName(MethodBlock *mb) {
const char *classname = CLASS_CB(mb->classobj)->name;
char *methodname = mb->name;
char *nonMangled = (char*) sysMalloc(strlen(classname) + strlen(methodname) + 7);
char *mangled;
sprintf(nonMangled, "Java/%s/%s", classname, methodname);
mangled = mangleString(nonMangled);
sysFree(nonMangled);
return mangled;
}
char *mangleSignature(MethodBlock *mb) {
char *type = mb->type;
char *nonMangled;
char *mangled;
int i;
/* find ending ) */
for(i = strlen(type) - 1; type[i] != ')'; i--);
nonMangled = (char *) sysMalloc(i);
strncpy(nonMangled, type + 1, i - 1);
nonMangled[i - 1] = '\0';
mangled = mangleString(nonMangled);
sysFree(nonMangled);
return mangled;
}
void *lookupInternal(MethodBlock *mb) {
ClassBlock *cb = CLASS_CB(mb->classobj);
int i;
TRACE("<DLL: Looking up %s internally>\n", mb->name);
/* First try to locate the class */
for(i = 0; native_methods[i].classname &&
(strcmp(cb->name, native_methods[i].classname) != 0); i++);
if(native_methods[i].classname) {
VMMethod *methods = native_methods[i].methods;
/* Found the class -- now try to locate the method */
for(i = 0; methods[i].methodname &&
(strcmp(mb->name, methods[i].methodname) != 0); i++);
if(methods[i].methodname) {
if(verbose)
jam_printf("internal");
/* Found it -- set the invoker to the native method */
return mb->native_invoker = (void*)methods[i].method;
}
}
return NULL;
}
void *resolveNativeMethod(MethodBlock *mb) {
void *func;
if(verbose) {
char *classname = slash2dots(CLASS_CB(mb->classobj)->name);
jam_printf("[Dynamic-linking native method %s.%s ... ", classname, mb->name);
sysFree(classname);
}
/* First see if it's an internal native method */
func = lookupInternal(mb);
#ifndef NO_JNI
if(func == NULL)
func = lookupLoadedDlls(mb);
#endif
if(verbose)
jam_printf("]\n");
return func;
}
uintptr_t *resolveNativeWrapper(Class *classobj, MethodBlock *mb, uintptr_t *ostack) {
void *func = resolveNativeMethod(mb);
if(func == NULL) {
signalException(java_lang_UnsatisfiedLinkError, mb->name);
return ostack;
}
return (*(uintptr_t *(*)(Class*, MethodBlock*, uintptr_t*))func)(classobj, mb, ostack);
}
void initialiseDll(InitArgs *args) {
#ifndef NO_JNI
/* Init hash table, and create lock */
initHashTable(hash_table, HASHTABSZE, TRUE);
#endif
verbose = args->verbosedll;
}
#ifndef NO_JNI
typedef struct {
char *name;
void *handle;
Object *loader;
} DllEntry;
int dllNameHash(char *name) {
int hash = 0;
while(*name)
hash = hash * 37 + *name++;
return hash;
}
int resolveDll(char *name, Object *loader) {
DllEntry *dll;
TRACE("<DLL: Attempting to resolve library %s>\n", name);
#define HASH(ptr) dllNameHash(ptr)
#define COMPARE(ptr1, ptr2, hash1, hash2) \
((hash1 == hash2) && (strcmp(ptr1, ptr2->name) == 0))
#define PREPARE(ptr) ptr
#define SCAVENGE(ptr) FALSE
#define FOUND(ptr) ptr
/* Do not add if absent, no scavenge, locked */
findHashEntry(hash_table, name, dll, FALSE, FALSE, TRUE, DllEntry*);
if(dll == NULL) {
DllEntry *dll2;
void *onload, *handle = nativeLibOpen(name);
if(handle == NULL)
return FALSE;
TRACE("<DLL: Successfully opened library %s>\n", name);
if((onload = nativeLibSym(handle, "JNI_OnLoad")) != NULL) {
int ver;
initJNILrefs();
ver = (*(jint (*)(JavaVM*, void*))onload)(&invokeIntf, NULL);
if(ver != JNI_VERSION_1_2 && ver != JNI_VERSION_1_4) {
TRACE("<DLL: JNI_OnLoad returned unsupported version %d.\n>", ver);
return FALSE;
}
}
dll = (DllEntry*)sysMalloc(sizeof(DllEntry));
dll->name = strcpy((char*)sysMalloc(strlen(name)+1), name);
dll->handle = handle;
dll->loader = loader;
#undef HASH
#undef COMPARE
#define HASH(ptr) dllNameHash(ptr->name)
#define COMPARE(ptr1, ptr2, hash1, hash2) \
((hash1 == hash2) && (strcmp(ptr1->name, ptr2->name) == 0))
/* Add if absent, no scavenge, locked */
findHashEntry(hash_table, dll, dll2, TRUE, FALSE, TRUE, DllEntry*);
/* If the library has an OnUnload function it must be
called from a running Java thread (i.e. not within
the GC!). Create an unloader object which will be
finalised when the class loader is collected */
if(nativeLibSym(dll->handle, "JNI_OnUnload") != NULL)
newLibraryUnloader(loader, dll);
} else
if(dll->loader != loader)
return FALSE;
return TRUE;
}
char *getDllPath() {
char *env = nativeLibPath();
return env ? env : (char*)"";
}
const char *getBootDllPath() {
return CLASSPATH_INSTALL_DIR"/lib/classpath";
}
char *getDllName(char *name) {
return nativeLibMapName(name);
}
void *lookupLoadedDlls0(char *name, Object *loader) {
TRACE("<DLL: Looking up %s loader %p in loaded DLL's>\n", name, loader);
#define ITERATE(ptr) \
{ \
DllEntry *dll = (DllEntry*)ptr; \
if(dll->loader == loader) { \
void *sym = nativeLibSym(dll->handle, name); \
if(sym != NULL) \
return sym; \
} \
}
hashIterate(hash_table);
return NULL;
}
void unloadDll(DllEntry *dll, int unloader) {
void *on_unload = nativeLibSym(dll->handle, "JNI_OnUnload");
if(unloader || on_unload == NULL) {
TRACE("<DLL: Unloading DLL %s\n", dll->name);
if(on_unload != NULL) {
initJNILrefs();
(*(void (*)(JavaVM*, void*))on_unload)(&invokeIntf, NULL);
}
nativeLibClose(dll->handle);
sysFree(dll->name);
sysFree(dll);
}
}
void unloaderUnloadDll(uintptr_t entry) {
unloadDll((DllEntry*)entry, TRUE);
}
#undef ITERATE
#define ITERATE(ptr) \
{ \
DllEntry *dll = (DllEntry*)ptr; \
if(isMarked(dll->loader)) \
threadReference(&dll->loader); \
}
void threadLiveClassLoaderDlls() {
hashIterate(hash_table);
}
void unloadClassLoaderDlls(Object *loader) {
int unloaded = 0;
TRACE("<DLL: Unloading DLLs for loader %p\n", loader);
#undef ITERATE
#define ITERATE(ptr) \
{ \
DllEntry *dll = (DllEntry*)*ptr; \
if(dll->loader == loader) { \
unloadDll(dll, FALSE); \
*ptr = NULL; \
unloaded++; \
} \
}
hashIterateP(hash_table);
if(unloaded) {
int size;
/* Update count to remaining number of DLLs */
hash_table.hash_count -= unloaded;
/* Calculate nearest multiple of 2 larger than count */
for(size = 1; size < hash_table.hash_count; size <<= 1);
/* Ensure new table is less than 2/3 full */
size = hash_table.hash_count*3 > size*2 ? size<< 1 : size;
resizeHash(&hash_table, size);
}
}
static void *env = &Jam_JNINativeInterface;
uintptr_t *callJNIWrapper(Class *classobj, MethodBlock *mb, uintptr_t *ostack) {
TRACE("<DLL: Calling JNI method %s.%s%s>\n", CLASS_CB(classobj)->name, mb->name, mb->type);
if(!initJNILrefs())
return NULL;
return callJNIMethod(&env, (mb->access_flags & ACC_STATIC) ? classobj : NULL,
mb->type, mb->native_extra_arg, ostack, (unsigned char*)mb->code, mb->args_count);
}
void *lookupLoadedDlls(MethodBlock *mb) {
Object *loader = (CLASS_CB(mb->classobj))->class_loader;
char *mangled = mangleClassAndMethodName(mb);
void *func;
func = lookupLoadedDlls0(mangled, loader);
if(func == NULL) {
char *mangledSig = mangleSignature(mb);
char *fullyMangled = (char*)sysMalloc(strlen(mangled)+strlen(mangledSig)+3);
sprintf(fullyMangled, "%s__%s", mangled, mangledSig);
func = lookupLoadedDlls0(fullyMangled, loader);
sysFree(fullyMangled);
sysFree(mangledSig);
}
sysFree(mangled);
if(func) {
if(verbose)
jam_printf("JNI");
mb->code = (unsigned char *) func;
mb->native_extra_arg = nativeExtraArg(mb);
return mb->native_invoker = (void*) callJNIWrapper;
}
return NULL;
}
#endif