This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
mirrored from https://android.googlesource.com/platform/dalvik.git
/
Native.c
807 lines (705 loc) · 23.7 KB
/
Native.c
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Native method resolution.
*
* Currently the "Dalvik native" methods are only used for internal methods.
* Someday we may want to export the interface as a faster but riskier
* alternative to JNI.
*/
#include "Dalvik.h"
#include <stdlib.h>
#include <dlfcn.h>
static void freeSharedLibEntry(void* ptr);
static void* lookupSharedLibMethod(const Method* method);
/*
* Initialize the native code loader.
*/
bool dvmNativeStartup(void)
{
gDvm.nativeLibs = dvmHashTableCreate(4, freeSharedLibEntry);
if (gDvm.nativeLibs == NULL)
return false;
return true;
}
/*
* Free up our tables.
*/
void dvmNativeShutdown(void)
{
dvmHashTableFree(gDvm.nativeLibs);
gDvm.nativeLibs = NULL;
}
/*
* Resolve a native method and invoke it.
*
* This is executed as if it were a native bridge or function. If the
* resolution succeeds, method->insns is replaced, and we don't go through
* here again.
*
* Initializes method's class if necessary.
*
* An exception is thrown on resolution failure.
*/
void dvmResolveNativeMethod(const u4* args, JValue* pResult,
const Method* method, Thread* self)
{
ClassObject* clazz = method->clazz;
void* func;
/*
* If this is a static method, it could be called before the class
* has been initialized.
*/
if (dvmIsStaticMethod(method)) {
if (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz)) {
assert(dvmCheckException(dvmThreadSelf()));
return;
}
} else {
assert(dvmIsClassInitialized(clazz) ||
dvmIsClassInitializing(clazz));
}
/* start with our internal-native methods */
func = dvmLookupInternalNativeMethod(method);
if (func != NULL) {
/* resolution always gets the same answer, so no race here */
IF_LOGVV() {
char* desc = dexProtoCopyMethodDescriptor(&method->prototype);
LOGVV("+++ resolved native %s.%s %s, invoking\n",
clazz->descriptor, method->name, desc);
free(desc);
}
if (dvmIsSynchronizedMethod(method)) {
LOGE("ERROR: internal-native can't be declared 'synchronized'\n");
LOGE("Failing on %s.%s\n", method->clazz->descriptor, method->name);
dvmAbort(); // harsh, but this is VM-internal problem
}
DalvikBridgeFunc dfunc = (DalvikBridgeFunc) func;
dvmSetNativeFunc(method, dfunc, NULL);
assert(method->insns == NULL);
dfunc(args, pResult, method, self);
return;
}
/* now scan any DLLs we have loaded for JNI signatures */
func = lookupSharedLibMethod(method);
if (func != NULL) {
if (dvmIsSynchronizedMethod(method))
dvmSetNativeFunc(method, dvmCallSynchronizedJNIMethod, func);
else
dvmSetNativeFunc(method, dvmCallJNIMethod, func);
dvmCallJNIMethod(args, pResult, method, self);
return;
}
IF_LOGW() {
char* desc = dexProtoCopyMethodDescriptor(&method->prototype);
LOGW("No implementation found for native %s.%s %s\n",
clazz->descriptor, method->name, desc);
free(desc);
}
dvmThrowException("Ljava/lang/UnsatisfiedLinkError;", method->name);
}
/*
* ===========================================================================
* Native shared library support
* ===========================================================================
*/
// TODO? if a ClassLoader is unloaded, we need to unload all DLLs that
// are associated with it. (Or not -- can't determine if native code
// is still using parts of it.)
typedef enum OnLoadState {
kOnLoadPending = 0, /* initial state, must be zero */
kOnLoadFailed,
kOnLoadOkay,
} OnLoadState;
/*
* We add one of these to the hash table for every library we load. The
* hash is on the "pathName" field.
*/
typedef struct SharedLib {
char* pathName; /* absolute path to library */
void* handle; /* from dlopen */
Object* classLoader; /* ClassLoader we are associated with */
pthread_mutex_t onLoadLock; /* guards remaining items */
pthread_cond_t onLoadCond; /* wait for JNI_OnLoad in other thread */
u4 onLoadThreadId; /* recursive invocation guard */
OnLoadState onLoadResult; /* result of earlier JNI_OnLoad */
} SharedLib;
/*
* (This is a dvmHashTableLookup callback.)
*
* Find an entry that matches the string.
*/
static int hashcmpNameStr(const void* ventry, const void* vname)
{
const SharedLib* pLib = (const SharedLib*) ventry;
const char* name = (const char*) vname;
return strcmp(pLib->pathName, name);
}
/*
* (This is a dvmHashTableLookup callback.)
*
* Find an entry that matches the new entry.
*
* We don't compare the class loader here, because you're not allowed to
* have the same shared library associated with more than one CL.
*/
static int hashcmpSharedLib(const void* ventry, const void* vnewEntry)
{
const SharedLib* pLib = (const SharedLib*) ventry;
const SharedLib* pNewLib = (const SharedLib*) vnewEntry;
LOGD("--- comparing %p '%s' %p '%s'\n",
pLib, pLib->pathName, pNewLib, pNewLib->pathName);
return strcmp(pLib->pathName, pNewLib->pathName);
}
/*
* Check to see if an entry with the same pathname already exists.
*/
static SharedLib* findSharedLibEntry(const char* pathName)
{
u4 hash = dvmComputeUtf8Hash(pathName);
void* ent;
ent = dvmHashTableLookup(gDvm.nativeLibs, hash, (void*)pathName,
hashcmpNameStr, false);
return ent;
}
/*
* Add the new entry to the table.
*
* Returns the table entry, which will not be the same as "pLib" if the
* entry already exists.
*/
static SharedLib* addSharedLibEntry(SharedLib* pLib)
{
u4 hash = dvmComputeUtf8Hash(pLib->pathName);
void* ent;
/*
* Do the lookup with the "add" flag set. If we add it, we will get
* our own pointer back. If somebody beat us to the punch, we'll get
* their pointer back instead.
*/
return dvmHashTableLookup(gDvm.nativeLibs, hash, pLib, hashcmpSharedLib,
true);
}
/*
* Free up an entry. (This is a dvmHashTableFree callback.)
*/
static void freeSharedLibEntry(void* ptr)
{
SharedLib* pLib = (SharedLib*) ptr;
/*
* Calling dlclose() here is somewhat dangerous, because it's possible
* that a thread outside the VM is still accessing the code we loaded.
*/
if (false)
dlclose(pLib->handle);
free(pLib->pathName);
free(pLib);
}
/*
* Convert library name to system-dependent form, e.g. "jpeg" becomes
* "libjpeg.so".
*
* (Should we have this take buffer+len and avoid the alloc? It gets
* called very rarely.)
*/
char* dvmCreateSystemLibraryName(char* libName)
{
char buf[256];
int len;
len = snprintf(buf, sizeof(buf), OS_SHARED_LIB_FORMAT_STR, libName);
if (len >= (int) sizeof(buf))
return NULL;
else
return strdup(buf);
}
#if 0
/*
* Find a library, given the lib's system-dependent name (e.g. "libjpeg.so").
*
* We need to search through the path defined by the java.library.path
* property.
*
* Returns NULL if the library was not found.
*/
static char* findLibrary(const char* libSysName)
{
char* javaLibraryPath = NULL;
char* testName = NULL;
char* start;
char* cp;
bool done;
javaLibraryPath = dvmGetProperty("java.library.path");
if (javaLibraryPath == NULL)
goto bail;
LOGVV("+++ path is '%s'\n", javaLibraryPath);
start = cp = javaLibraryPath;
while (cp != NULL) {
char pathBuf[256];
int len;
cp = strchr(start, ':');
if (cp != NULL)
*cp = '\0';
len = snprintf(pathBuf, sizeof(pathBuf), "%s/%s", start, libSysName);
if (len >= (int) sizeof(pathBuf)) {
LOGW("Path overflowed %d bytes: '%s' / '%s'\n",
len, start, libSysName);
/* keep going, next one might fit */
} else {
LOGVV("+++ trying '%s'\n", pathBuf);
if (access(pathBuf, R_OK) == 0) {
testName = strdup(pathBuf);
break;
}
}
start = cp +1;
}
bail:
free(javaLibraryPath);
return testName;
}
/*
* Load a native shared library, given the system-independent piece of
* the library name.
*
* Throws an exception on failure.
*/
void dvmLoadNativeLibrary(StringObject* libNameObj, Object* classLoader)
{
char* libName = NULL;
char* libSysName = NULL;
char* libPath = NULL;
/*
* If "classLoader" isn't NULL, call the class loader's "findLibrary"
* method with the lib name. If it returns a non-NULL result, we use
* that as the pathname.
*/
if (classLoader != NULL) {
Method* findLibrary;
Object* findLibResult;
findLibrary = dvmFindVirtualMethodByDescriptor(classLoader->clazz,
"findLibrary", "(Ljava/lang/String;)Ljava/lang/String;");
if (findLibrary == NULL) {
LOGW("Could not find findLibrary() in %s\n",
classLoader->clazz->name);
dvmThrowException("Ljava/lang/UnsatisfiedLinkError;",
"findLibrary");
goto bail;
}
findLibResult = (Object*)(u4) dvmCallMethod(findLibrary, classLoader,
libNameObj);
if (dvmCheckException()) {
LOGV("returning early on exception\n");
goto bail;
}
if (findLibResult != NULL) {
/* success! */
libPath = dvmCreateCstrFromString(libNameObj);
LOGI("Found library through CL: '%s'\n", libPath);
dvmLoadNativeCode(libPath, classLoader);
goto bail;
}
}
libName = dvmCreateCstrFromString(libNameObj);
if (libName == NULL)
goto bail;
libSysName = dvmCreateSystemLibraryName(libName);
if (libSysName == NULL)
goto bail;
libPath = findLibrary(libSysName);
if (libPath != NULL) {
LOGD("Found library through path: '%s'\n", libPath);
dvmLoadNativeCode(libPath, classLoader);
} else {
LOGW("Unable to locate shared lib matching '%s'\n", libSysName);
dvmThrowException("Ljava/lang/UnsatisfiedLinkError;", libName);
}
bail:
free(libName);
free(libSysName);
free(libPath);
}
#endif
/*
* Check the result of an earlier call to JNI_OnLoad on this library. If
* the call has not yet finished in another thread, wait for it.
*/
static bool checkOnLoadResult(SharedLib* pEntry)
{
Thread* self = dvmThreadSelf();
if (pEntry->onLoadThreadId == self->threadId) {
/*
* Check this so we don't end up waiting for ourselves. We need
* to return "true" so the caller can continue.
*/
LOGI("threadid=%d: recursive native library load attempt (%s)\n",
self->threadId, pEntry->pathName);
return true;
}
LOGV("+++ retrieving %s OnLoad status\n", pEntry->pathName);
bool result;
dvmLockMutex(&pEntry->onLoadLock);
while (pEntry->onLoadResult == kOnLoadPending) {
LOGD("threadid=%d: waiting for %s OnLoad status\n",
self->threadId, pEntry->pathName);
int oldStatus = dvmChangeStatus(self, THREAD_VMWAIT);
pthread_cond_wait(&pEntry->onLoadCond, &pEntry->onLoadLock);
dvmChangeStatus(self, oldStatus);
}
if (pEntry->onLoadResult == kOnLoadOkay) {
LOGV("+++ earlier OnLoad(%s) okay\n", pEntry->pathName);
result = true;
} else {
LOGV("+++ earlier OnLoad(%s) failed\n", pEntry->pathName);
result = false;
}
dvmUnlockMutex(&pEntry->onLoadLock);
return result;
}
typedef int (*OnLoadFunc)(JavaVM*, void*);
/*
* Load native code from the specified absolute pathname. Per the spec,
* if we've already loaded a library with the specified pathname, we
* return without doing anything.
*
* TODO? for better results we should absolutify the pathname. For fully
* correct results we should stat to get the inode and compare that. The
* existing implementation is fine so long as everybody is using
* System.loadLibrary.
*
* The library will be associated with the specified class loader. The JNI
* spec says we can't load the same library into more than one class loader.
*
* Returns "true" on success.
*/
bool dvmLoadNativeCode(const char* pathName, Object* classLoader)
{
SharedLib* pEntry;
void* handle;
LOGD("Trying to load lib %s %p\n", pathName, classLoader);
/*
* See if we've already loaded it. If we have, and the class loader
* matches, return successfully without doing anything.
*/
pEntry = findSharedLibEntry(pathName);
if (pEntry != NULL) {
if (pEntry->classLoader != classLoader) {
LOGW("Shared lib '%s' already opened by CL %p; can't open in %p\n",
pathName, pEntry->classLoader, classLoader);
return false;
}
LOGD("Shared lib '%s' already loaded in same CL %p\n",
pathName, classLoader);
if (!checkOnLoadResult(pEntry))
return false;
return true;
}
/*
* Open the shared library. Because we're using a full path, the system
* doesn't have to search through LD_LIBRARY_PATH. (It may do so to
* resolve this library's dependencies though.)
*
* Failures here are expected when java.library.path has several entries.
*
* The current android-arm dynamic linker implementation tends to
* return "Cannot find library" from dlerror() regardless of the actual
* problem. A more useful diagnostic may be sent to stdout/stderr,
* but often that's not visible. Some things to try:
* - make sure the library exists on the device
* - verify that the right path is being opened (the debug log message
* above can help with that)
* - check to see if the library is valid
* - check config/prelink-linux-arm.map to ensure that the library
* is listed and is not being overrun by the previous entry (if
* loading suddenly stops working on a prelinked library, this is
* a good one to check)
* - write a trivial app that calls sleep() then dlopen(), attach
* to it with "strace -p <pid>" while it sleeps, and watch for
* attempts to open nonexistent dependent shared libs
*
* This can execute slowly for a large library on a busy system, so we
* want to switch from RUNNING to VMWAIT while it executes. This allows
* the GC to ignore us.
*/
Thread* self = dvmThreadSelf();
int oldStatus = dvmChangeStatus(self, THREAD_VMWAIT);
handle = dlopen(pathName, RTLD_LAZY);
dvmChangeStatus(self, oldStatus);
if (handle == NULL) {
LOGI("Unable to dlopen(%s): %s\n", pathName, dlerror());
return false;
}
/* create a new entry */
SharedLib* pNewEntry;
pNewEntry = (SharedLib*) calloc(1, sizeof(SharedLib));
pNewEntry->pathName = strdup(pathName);
pNewEntry->handle = handle;
pNewEntry->classLoader = classLoader;
dvmInitMutex(&pNewEntry->onLoadLock);
pthread_cond_init(&pNewEntry->onLoadCond, NULL);
pNewEntry->onLoadThreadId = self->threadId;
/* try to add it to the list */
SharedLib* pActualEntry = addSharedLibEntry(pNewEntry);
if (pNewEntry != pActualEntry) {
LOGI("WOW: we lost a race to add a shared lib (%s CL=%p)\n",
pathName, classLoader);
freeSharedLibEntry(pNewEntry);
return checkOnLoadResult(pActualEntry);
} else {
LOGD("Added shared lib %s %p\n", pathName, classLoader);
bool result = true;
void* vonLoad;
int version;
vonLoad = dlsym(handle, "JNI_OnLoad");
if (vonLoad == NULL) {
LOGD("No JNI_OnLoad found in %s %p\n", pathName, classLoader);
} else {
/*
* Call JNI_OnLoad. We have to override the current class
* loader, which will always be "null" since the stuff at the
* top of the stack is around Runtime.loadLibrary(). (See
* the comments in the JNI FindClass function.)
*/
OnLoadFunc func = vonLoad;
Object* prevOverride = self->classLoaderOverride;
self->classLoaderOverride = classLoader;
oldStatus = dvmChangeStatus(self, THREAD_NATIVE);
LOGV("+++ calling JNI_OnLoad(%s)\n", pathName);
version = (*func)(gDvm.vmList, NULL);
dvmChangeStatus(self, oldStatus);
self->classLoaderOverride = prevOverride;
if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 &&
version != JNI_VERSION_1_6)
{
LOGW("JNI_OnLoad returned bad version (%d) in %s %p\n",
version, pathName, classLoader);
/*
* It's unwise to call dlclose() here, but we can mark it
* as bad and ensure that future load attempts will fail.
*
* We don't know how far JNI_OnLoad got, so there could
* be some partially-initialized stuff accessible through
* newly-registered native method calls. We could try to
* unregister them, but that doesn't seem worthwhile.
*/
result = false;
} else {
LOGV("+++ finished JNI_OnLoad %s\n", pathName);
}
}
if (result)
pNewEntry->onLoadResult = kOnLoadOkay;
else
pNewEntry->onLoadResult = kOnLoadFailed;
pNewEntry->onLoadThreadId = 0;
/*
* Broadcast a wakeup to anybody sleeping on the condition variable.
*/
dvmLockMutex(&pNewEntry->onLoadLock);
pthread_cond_broadcast(&pNewEntry->onLoadCond);
dvmUnlockMutex(&pNewEntry->onLoadLock);
return result;
}
}
/*
* ===========================================================================
* Signature-based method lookup
* ===========================================================================
*/
/*
* Create the pre-mangled form of the class+method string.
*
* Returns a newly-allocated string, and sets "*pLen" to the length.
*/
static char* createJniNameString(const char* classDescriptor,
const char* methodName, int* pLen)
{
char* result;
size_t descriptorLength = strlen(classDescriptor);
*pLen = 4 + descriptorLength + strlen(methodName);
result = malloc(*pLen +1);
if (result == NULL)
return NULL;
/*
* Add one to classDescriptor to skip the "L", and then replace
* the final ";" with a "/" after the sprintf() call.
*/
sprintf(result, "Java/%s%s", classDescriptor + 1, methodName);
result[5 + (descriptorLength - 2)] = '/';
return result;
}
/*
* Returns a newly-allocated, mangled copy of "str".
*
* "str" is a "modified UTF-8" string. We convert it to UTF-16 first to
* make life simpler.
*/
static char* mangleString(const char* str, int len)
{
u2* utf16 = NULL;
char* mangle = NULL;
int charLen;
//LOGI("mangling '%s' %d\n", str, len);
assert(str[len] == '\0');
charLen = dvmUtf8Len(str);
utf16 = (u2*) malloc(sizeof(u2) * charLen);
if (utf16 == NULL)
goto bail;
dvmConvertUtf8ToUtf16(utf16, str);
/*
* Compute the length of the mangled string.
*/
int i, mangleLen = 0;
for (i = 0; i < charLen; i++) {
u2 ch = utf16[i];
if (ch > 127) {
mangleLen += 6;
} else {
switch (ch) {
case '_':
case ';':
case '[':
mangleLen += 2;
break;
default:
mangleLen++;
break;
}
}
}
char* cp;
mangle = (char*) malloc(mangleLen +1);
if (mangle == NULL)
goto bail;
for (i = 0, cp = mangle; i < charLen; i++) {
u2 ch = utf16[i];
if (ch > 127) {
sprintf(cp, "_0%04x", ch);
cp += 6;
} else {
switch (ch) {
case '_':
*cp++ = '_';
*cp++ = '1';
break;
case ';':
*cp++ = '_';
*cp++ = '2';
break;
case '[':
*cp++ = '_';
*cp++ = '3';
break;
case '/':
*cp++ = '_';
break;
default:
*cp++ = (char) ch;
break;
}
}
}
*cp = '\0';
bail:
free(utf16);
return mangle;
}
/*
* Create the mangled form of the parameter types.
*/
static char* createMangledSignature(const DexProto* proto)
{
DexStringCache sigCache;
const char* interim;
char* result;
dexStringCacheInit(&sigCache);
interim = dexProtoGetParameterDescriptors(proto, &sigCache);
result = mangleString(interim, strlen(interim));
dexStringCacheRelease(&sigCache);
return result;
}
/*
* (This is a dvmHashForeach callback.)
*
* Search for a matching method in this shared library.
*
* TODO: we may want to skip libraries for which JNI_OnLoad failed.
*/
static int findMethodInLib(void* vlib, void* vmethod)
{
const SharedLib* pLib = (const SharedLib*) vlib;
const Method* meth = (const Method*) vmethod;
char* preMangleCM = NULL;
char* mangleCM = NULL;
char* mangleSig = NULL;
char* mangleCMSig = NULL;
void* func = NULL;
int len;
if (meth->clazz->classLoader != pLib->classLoader) {
LOGD("+++ not scanning '%s' for '%s' (wrong CL)\n",
pLib->pathName, meth->name);
return 0;
} else
LOGV("+++ scanning '%s' for '%s'\n", pLib->pathName, meth->name);
/*
* First, we try it without the signature.
*/
preMangleCM =
createJniNameString(meth->clazz->descriptor, meth->name, &len);
if (preMangleCM == NULL)
goto bail;
mangleCM = mangleString(preMangleCM, len);
if (mangleCM == NULL)
goto bail;
LOGV("+++ calling dlsym(%s)\n", mangleCM);
func = dlsym(pLib->handle, mangleCM);
if (func == NULL) {
mangleSig =
createMangledSignature(&meth->prototype);
if (mangleSig == NULL)
goto bail;
mangleCMSig = (char*) malloc(strlen(mangleCM) + strlen(mangleSig) +3);
if (mangleCMSig == NULL)
goto bail;
sprintf(mangleCMSig, "%s__%s", mangleCM, mangleSig);
LOGV("+++ calling dlsym(%s)\n", mangleCMSig);
func = dlsym(pLib->handle, mangleCMSig);
if (func != NULL) {
LOGV("Found '%s' with dlsym\n", mangleCMSig);
}
} else {
LOGV("Found '%s' with dlsym\n", mangleCM);
}
bail:
free(preMangleCM);
free(mangleCM);
free(mangleSig);
free(mangleCMSig);
return (int) func;
}
/*
* See if the requested method lives in any of the currently-loaded
* shared libraries. We do this by checking each of them for the expected
* method signature.
*/
static void* lookupSharedLibMethod(const Method* method)
{
if (gDvm.nativeLibs == NULL) {
LOGE("Unexpected init state: nativeLibs not ready\n");
dvmAbort();
}
return (void*) dvmHashForeach(gDvm.nativeLibs, findMethodInLib,
(void*) method);
}