OSDN Git Service

Merge "Change NewDirectByteBuffer to allow NULL if capacity == 0."
[android-x86/dalvik.git] / vm / Jni.cpp
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * Dalvik implementation of JNI interfaces.
19  */
20 #include "Dalvik.h"
21 #include "JniInternal.h"
22 #include "ScopedPthreadMutexLock.h"
23 #include "UniquePtr.h"
24
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <limits.h>
28
29 /*
30 Native methods and interaction with the GC
31
32 All JNI methods must start by changing their thread status to
33 THREAD_RUNNING, and finish by changing it back to THREAD_NATIVE before
34 returning to native code.  The switch to "running" triggers a thread
35 suspension check.
36
37 With a rudimentary GC we should be able to skip the status change for
38 simple functions, e.g.  IsSameObject, GetJavaVM, GetStringLength, maybe
39 even access to fields with primitive types.  Our options are more limited
40 with a compacting GC.
41
42 For performance reasons we do as little error-checking as possible here.
43 For example, we don't check to make sure the correct type of Object is
44 passed in when setting a field, and we don't prevent you from storing
45 new values in a "final" field.  Such things are best handled in the
46 "check" version.  For actions that are common, dangerous, and must be
47 checked at runtime, such as array bounds checks, we do the tests here.
48
49
50 General notes on local/global reference tracking
51
52 JNI provides explicit control over natively-held references that the GC
53 needs to know about.  These can be local, in which case they're released
54 when the native method returns into the VM, or global, which are held
55 until explicitly released.  (There are also weak-global references,
56 which have the lifespan and visibility of global references, but the
57 object they refer to may be collected.)
58
59 The references can be created with explicit JNI NewLocalRef / NewGlobalRef
60 calls.  The former is very unusual, the latter is reasonably common
61 (e.g. for caching references to class objects).
62
63 Local references are most often created as a side-effect of JNI functions.
64 For example, the AllocObject/NewObject functions must create local
65 references to the objects returned, because nothing else in the GC root
66 set has a reference to the new objects.
67
68 The most common mode of operation is for a method to create zero or
69 more local references and return.  Explicit "local delete" operations
70 are expected to be exceedingly rare, except when walking through an
71 object array, and the Push/PopLocalFrame calls are expected to be used
72 infrequently.  For efficient operation, we want to add new local refs
73 with a simple store/increment operation; to avoid infinite growth in
74 pathological situations, we need to reclaim the space used by deleted
75 entries.
76
77 If we just want to maintain a list for the GC root set, we can use an
78 expanding append-only array that compacts when objects are deleted.
79 In typical situations, e.g. running through an array of objects, we will
80 be deleting one of the most recently added entries, so we can minimize
81 the number of elements moved (or avoid having to move any).
82
83 If we want to conceal the pointer values from native code, which is
84 necessary to allow the GC to move JNI-referenced objects around, then we
85 have to use a more complicated indirection mechanism.
86
87 The spec says, "Local references are only valid in the thread in which
88 they are created.  The native code must not pass local references from
89 one thread to another."
90
91
92 Pinned objects
93
94 For some large chunks of data, notably primitive arrays and String data,
95 JNI allows the VM to choose whether it wants to pin the array object or
96 make a copy.  We currently pin the memory for better execution performance.
97
98 TODO: we're using simple root set references to pin primitive array data,
99 because they have the property we need (i.e. the pointer we return is
100 guaranteed valid until we explicitly release it).  However, if we have a
101 compacting GC and don't want to pin all memory held by all global refs,
102 we need to treat these differently.
103
104
105 Global reference tracking
106
107 There should be a small "active" set centered around the most-recently
108 added items.
109
110 Because it's global, access to it has to be synchronized.  Additions and
111 removals require grabbing a mutex.  If the table serves as an indirection
112 mechanism (i.e. it's not just a list for the benefit of the garbage
113 collector), reference lookups may also require grabbing a mutex.
114
115 The JNI spec does not define any sort of limit, so the list must be able
116 to expand to a reasonable size.  It may be useful to log significant
117 increases in usage to help identify resource leaks.
118
119
120 Weak-global reference tracking
121
122 [TBD]
123
124
125 Local reference tracking
126
127 Each Thread/JNIEnv points to an IndirectRefTable.
128
129 We implement Push/PopLocalFrame with actual stack frames.  Before a JNI
130 frame gets popped, we set "nextEntry" to the "top" pointer of the current
131 frame, effectively releasing the references.
132
133 The GC will scan all references in the table.
134
135 */
136
137 static void ReportJniError() {
138     dvmDumpThread(dvmThreadSelf(), false);
139     dvmAbort();
140 }
141
142 #ifdef WITH_JNI_STACK_CHECK
143 # define COMPUTE_STACK_SUM(_self)   computeStackSum(_self);
144 # define CHECK_STACK_SUM(_self)     checkStackSum(_self);
145
146 /*
147  * Compute a CRC on the entire interpreted stack.
148  *
149  * Would be nice to compute it on "self" as well, but there are parts of
150  * the Thread that can be altered by other threads (e.g. prev/next pointers).
151  */
152 static void computeStackSum(Thread* self) {
153     const u1* low = (const u1*)SAVEAREA_FROM_FP(self->interpSave.curFrame);
154     u4 crc = dvmInitCrc32();
155     self->stackCrc = 0;
156     crc = dvmComputeCrc32(crc, low, self->interpStackStart - low);
157     self->stackCrc = crc;
158 }
159
160 /*
161  * Compute a CRC on the entire interpreted stack, and compare it to what
162  * we previously computed.
163  *
164  * We can execute JNI directly from native code without calling in from
165  * interpreted code during VM initialization and immediately after JNI
166  * thread attachment.  Another opportunity exists during JNI_OnLoad.  Rather
167  * than catching these cases we just ignore them here, which is marginally
168  * less accurate but reduces the amount of code we have to touch with #ifdefs.
169  */
170 static void checkStackSum(Thread* self) {
171     const u1* low = (const u1*)SAVEAREA_FROM_FP(self->interpSave.curFrame);
172     u4 stackCrc = self->stackCrc;
173     self->stackCrc = 0;
174     u4 crc = dvmInitCrc32();
175     crc = dvmComputeCrc32(crc, low, self->interpStackStart - low);
176     if (crc != stackCrc) {
177         const Method* meth = dvmGetCurrentJNIMethod();
178         if (dvmComputeExactFrameDepth(self->interpSave.curFrame) == 1) {
179             ALOGD("JNI: bad stack CRC (0x%08x) -- okay during init", stackCrc);
180         } else if (strcmp(meth->name, "nativeLoad") == 0 &&
181                 (strcmp(meth->clazz->descriptor, "Ljava/lang/Runtime;") == 0)) {
182             ALOGD("JNI: bad stack CRC (0x%08x) -- okay during JNI_OnLoad", stackCrc);
183         } else {
184             ALOGW("JNI: bad stack CRC (%08x vs %08x)", crc, stackCrc);
185             ReportJniError();
186         }
187     }
188     self->stackCrc = (u4) -1;       /* make logic errors more noticeable */
189 }
190
191 #else
192 # define COMPUTE_STACK_SUM(_self)   ((void)0)
193 # define CHECK_STACK_SUM(_self)     ((void)0)
194 #endif
195
196
197 /*
198  * ===========================================================================
199  *      Utility functions
200  * ===========================================================================
201  */
202
203 /*
204  * Entry/exit processing for all JNI calls.
205  *
206  * We skip the (curiously expensive) thread-local storage lookup on our Thread*.
207  * If the caller has passed the wrong JNIEnv in, we're going to be accessing unsynchronized
208  * structures from more than one thread, and things are going to fail
209  * in bizarre ways.  This is only sensible if the native code has been
210  * fully exercised with CheckJNI enabled.
211  */
212 class ScopedJniThreadState {
213 public:
214     explicit ScopedJniThreadState(JNIEnv* env) {
215         mSelf = ((JNIEnvExt*) env)->self;
216
217         if (UNLIKELY(gDvmJni.workAroundAppJniBugs)) {
218             // When emulating direct pointers with indirect references, it's critical
219             // that we use the correct per-thread indirect reference table.
220             Thread* self = gDvmJni.workAroundAppJniBugs ? dvmThreadSelf() : mSelf;
221             if (self != mSelf) {
222                 ALOGE("JNI ERROR: env->self != thread-self (%p vs. %p); auto-correcting", mSelf, self);
223                 mSelf = self;
224             }
225         }
226
227         CHECK_STACK_SUM(mSelf);
228         dvmChangeStatus(mSelf, THREAD_RUNNING);
229     }
230
231     ~ScopedJniThreadState() {
232         dvmChangeStatus(mSelf, THREAD_NATIVE);
233         COMPUTE_STACK_SUM(mSelf);
234     }
235
236     inline Thread* self() {
237         return mSelf;
238     }
239
240 private:
241     Thread* mSelf;
242
243     // Disallow copy and assignment.
244     ScopedJniThreadState(const ScopedJniThreadState&);
245     void operator=(const ScopedJniThreadState&);
246 };
247
248 #define kGlobalRefsTableInitialSize 512
249 #define kGlobalRefsTableMaxSize     51200       /* arbitrary, must be < 64K */
250 #define kGrefWaterInterval          100
251 #define kTrackGrefUsage             true
252
253 #define kWeakGlobalRefsTableInitialSize 16
254
255 #define kPinTableInitialSize        16
256 #define kPinTableMaxSize            1024
257 #define kPinComplainThreshold       10
258
259 bool dvmJniStartup() {
260     if (!gDvm.jniGlobalRefTable.init(kGlobalRefsTableInitialSize,
261                                  kGlobalRefsTableMaxSize,
262                                  kIndirectKindGlobal)) {
263         return false;
264     }
265     if (!gDvm.jniWeakGlobalRefTable.init(kWeakGlobalRefsTableInitialSize,
266                                  kGlobalRefsTableMaxSize,
267                                  kIndirectKindWeakGlobal)) {
268         return false;
269     }
270
271     dvmInitMutex(&gDvm.jniGlobalRefLock);
272     dvmInitMutex(&gDvm.jniWeakGlobalRefLock);
273     gDvm.jniGlobalRefLoMark = 0;
274     gDvm.jniGlobalRefHiMark = kGrefWaterInterval * 2;
275
276     if (!dvmInitReferenceTable(&gDvm.jniPinRefTable, kPinTableInitialSize, kPinTableMaxSize)) {
277         return false;
278     }
279
280     dvmInitMutex(&gDvm.jniPinRefLock);
281
282     return true;
283 }
284
285 void dvmJniShutdown() {
286     gDvm.jniGlobalRefTable.destroy();
287     gDvm.jniWeakGlobalRefTable.destroy();
288     dvmClearReferenceTable(&gDvm.jniPinRefTable);
289 }
290
291 /*
292  * Find the JNIEnv associated with the current thread.
293  *
294  * Currently stored in the Thread struct.  Could also just drop this into
295  * thread-local storage.
296  */
297 JNIEnvExt* dvmGetJNIEnvForThread() {
298     Thread* self = dvmThreadSelf();
299     if (self == NULL) {
300         return NULL;
301     }
302     return (JNIEnvExt*) dvmGetThreadJNIEnv(self);
303 }
304
305 /*
306  * Convert an indirect reference to an Object reference.  The indirect
307  * reference may be local, global, or weak-global.
308  *
309  * If "jobj" is NULL, or is a weak global reference whose reference has
310  * been cleared, this returns NULL.  If jobj is an invalid indirect
311  * reference, kInvalidIndirectRefObject is returned.
312  *
313  * Note "env" may be NULL when decoding global references.
314  */
315 Object* dvmDecodeIndirectRef(Thread* self, jobject jobj) {
316     if (jobj == NULL) {
317         return NULL;
318     }
319
320     switch (indirectRefKind(jobj)) {
321     case kIndirectKindLocal:
322         {
323             Object* result = self->jniLocalRefTable.get(jobj);
324             if (UNLIKELY(result == NULL)) {
325                 ALOGE("JNI ERROR (app bug): use of deleted local reference (%p)", jobj);
326                 ReportJniError();
327             }
328             return result;
329         }
330     case kIndirectKindGlobal:
331         {
332             // TODO: find a way to avoid the mutex activity here
333             IndirectRefTable* pRefTable = &gDvm.jniGlobalRefTable;
334             ScopedPthreadMutexLock lock(&gDvm.jniGlobalRefLock);
335             Object* result = pRefTable->get(jobj);
336             if (UNLIKELY(result == NULL)) {
337                 ALOGE("JNI ERROR (app bug): use of deleted global reference (%p)", jobj);
338                 ReportJniError();
339             }
340             return result;
341         }
342     case kIndirectKindWeakGlobal:
343         {
344             // TODO: find a way to avoid the mutex activity here
345             IndirectRefTable* pRefTable = &gDvm.jniWeakGlobalRefTable;
346             ScopedPthreadMutexLock lock(&gDvm.jniWeakGlobalRefLock);
347             Object* result = pRefTable->get(jobj);
348             if (result == kClearedJniWeakGlobal) {
349                 result = NULL;
350             } else if (UNLIKELY(result == NULL)) {
351                 ALOGE("JNI ERROR (app bug): use of deleted weak global reference (%p)", jobj);
352                 ReportJniError();
353             }
354             return result;
355         }
356     case kIndirectKindInvalid:
357     default:
358         if (UNLIKELY(gDvmJni.workAroundAppJniBugs)) {
359             // Assume an invalid local reference is actually a direct pointer.
360             return reinterpret_cast<Object*>(jobj);
361         }
362         ALOGW("Invalid indirect reference %p in decodeIndirectRef", jobj);
363         ReportJniError();
364         return kInvalidIndirectRefObject;
365     }
366 }
367
368 static void AddLocalReferenceFailure(IndirectRefTable* pRefTable) {
369     pRefTable->dump("JNI local");
370     ALOGE("Failed adding to JNI local ref table (has %zd entries)", pRefTable->capacity());
371     ReportJniError(); // spec says call FatalError; this is equivalent
372 }
373
374 /*
375  * Add a local reference for an object to the current stack frame.  When
376  * the native function returns, the reference will be discarded.
377  *
378  * We need to allow the same reference to be added multiple times.
379  *
380  * This will be called on otherwise unreferenced objects.  We cannot do
381  * GC allocations here, and it's best if we don't grab a mutex.
382  */
383 static inline jobject addLocalReference(Thread* self, Object* obj) {
384     if (obj == NULL) {
385         return NULL;
386     }
387
388     IndirectRefTable* pRefTable = &self->jniLocalRefTable;
389     void* curFrame = self->interpSave.curFrame;
390     u4 cookie = SAVEAREA_FROM_FP(curFrame)->xtra.localRefCookie;
391     jobject jobj = (jobject) pRefTable->add(cookie, obj);
392     if (UNLIKELY(jobj == NULL)) {
393         AddLocalReferenceFailure(pRefTable);
394     }
395
396     if (UNLIKELY(gDvmJni.workAroundAppJniBugs)) {
397         // Hand out direct pointers to support broken old apps.
398         return reinterpret_cast<jobject>(obj);
399     }
400     return jobj;
401 }
402
403 /*
404  * Ensure that at least "capacity" references can be held in the local
405  * refs table of the current thread.
406  */
407 static bool ensureLocalCapacity(Thread* self, int capacity) {
408     int numEntries = self->jniLocalRefTable.capacity();
409     // TODO: this isn't quite right, since "numEntries" includes holes
410     return ((kJniLocalRefMax - numEntries) >= capacity);
411 }
412
413 /*
414  * Explicitly delete a reference from the local list.
415  */
416 static void deleteLocalReference(Thread* self, jobject jobj) {
417     if (jobj == NULL) {
418         return;
419     }
420
421     IndirectRefTable* pRefTable = &self->jniLocalRefTable;
422     void* curFrame = self->interpSave.curFrame;
423     u4 cookie = SAVEAREA_FROM_FP(curFrame)->xtra.localRefCookie;
424     if (!pRefTable->remove(cookie, jobj)) {
425         /*
426          * Attempting to delete a local reference that is not in the
427          * topmost local reference frame is a no-op.  DeleteLocalRef returns
428          * void and doesn't throw any exceptions, but we should probably
429          * complain about it so the user will notice that things aren't
430          * going quite the way they expect.
431          */
432         ALOGW("JNI WARNING: DeleteLocalRef(%p) failed to find entry", jobj);
433     }
434 }
435
436 /*
437  * Add a global reference for an object.
438  *
439  * We may add the same object more than once.  Add/remove calls are paired,
440  * so it needs to appear on the list multiple times.
441  */
442 static jobject addGlobalReference(Object* obj) {
443     if (obj == NULL) {
444         return NULL;
445     }
446
447     //ALOGI("adding obj=%p", obj);
448     //dvmDumpThread(dvmThreadSelf(), false);
449
450     if (false && dvmIsClassObject((Object*)obj)) {
451         ClassObject* clazz = (ClassObject*) obj;
452         ALOGI("-------");
453         ALOGI("Adding global ref on class %s", clazz->descriptor);
454         dvmDumpThread(dvmThreadSelf(), false);
455     }
456     if (false && ((Object*)obj)->clazz == gDvm.classJavaLangString) {
457         StringObject* strObj = (StringObject*) obj;
458         char* str = dvmCreateCstrFromString(strObj);
459         if (strcmp(str, "sync-response") == 0) {
460             ALOGI("-------");
461             ALOGI("Adding global ref on string '%s'", str);
462             dvmDumpThread(dvmThreadSelf(), false);
463             //dvmAbort();
464         }
465         free(str);
466     }
467     if (false && ((Object*)obj)->clazz == gDvm.classArrayByte) {
468         ArrayObject* arrayObj = (ArrayObject*) obj;
469         if (arrayObj->length == 8192 /*&&
470             dvmReferenceTableEntries(&gDvm.jniGlobalRefTable) > 400*/)
471         {
472             ALOGI("Adding global ref on byte array %p (len=%d)",
473                 arrayObj, arrayObj->length);
474             dvmDumpThread(dvmThreadSelf(), false);
475         }
476     }
477
478     ScopedPthreadMutexLock lock(&gDvm.jniGlobalRefLock);
479
480     /*
481      * Throwing an exception on failure is problematic, because JNI code
482      * may not be expecting an exception, and things sort of cascade.  We
483      * want to have a hard limit to catch leaks during debugging, but this
484      * otherwise needs to expand until memory is consumed.  As a practical
485      * matter, if we have many thousands of global references, chances are
486      * we're either leaking global ref table entries or we're going to
487      * run out of space in the GC heap.
488      */
489     jobject jobj = (jobject) gDvm.jniGlobalRefTable.add(IRT_FIRST_SEGMENT, obj);
490     if (jobj == NULL) {
491         gDvm.jniGlobalRefTable.dump("JNI global");
492         ALOGE("Failed adding to JNI global ref table (%zd entries)",
493                 gDvm.jniGlobalRefTable.capacity());
494         ReportJniError();
495     }
496
497     LOGVV("GREF add %p  (%s.%s)", obj,
498         dvmGetCurrentJNIMethod()->clazz->descriptor,
499         dvmGetCurrentJNIMethod()->name);
500
501     /* GREF usage tracking; should probably be disabled for production env */
502     if (kTrackGrefUsage && gDvm.jniGrefLimit != 0) {
503         int count = gDvm.jniGlobalRefTable.capacity();
504         // TODO: adjust for "holes"
505         if (count > gDvm.jniGlobalRefHiMark) {
506             ALOGD("GREF has increased to %d", count);
507             gDvm.jniGlobalRefHiMark += kGrefWaterInterval;
508             gDvm.jniGlobalRefLoMark += kGrefWaterInterval;
509
510             /* watch for "excessive" use; not generally appropriate */
511             if (count >= gDvm.jniGrefLimit) {
512                 if (gDvmJni.warnOnly) {
513                     ALOGW("Excessive JNI global references (%d)", count);
514                 } else {
515                     gDvm.jniGlobalRefTable.dump("JNI global");
516                     ALOGE("Excessive JNI global references (%d)", count);
517                     ReportJniError();
518                 }
519             }
520         }
521     }
522     return jobj;
523 }
524
525 static jobject addWeakGlobalReference(Object* obj) {
526     if (obj == NULL) {
527         return NULL;
528     }
529
530     ScopedPthreadMutexLock lock(&gDvm.jniWeakGlobalRefLock);
531     IndirectRefTable *table = &gDvm.jniWeakGlobalRefTable;
532     jobject jobj = (jobject) table->add(IRT_FIRST_SEGMENT, obj);
533     if (jobj == NULL) {
534         gDvm.jniWeakGlobalRefTable.dump("JNI weak global");
535         ALOGE("Failed adding to JNI weak global ref table (%zd entries)", table->capacity());
536         ReportJniError();
537     }
538     return jobj;
539 }
540
541 static void deleteWeakGlobalReference(jobject jobj) {
542     if (jobj == NULL) {
543         return;
544     }
545
546     ScopedPthreadMutexLock lock(&gDvm.jniWeakGlobalRefLock);
547     IndirectRefTable *table = &gDvm.jniWeakGlobalRefTable;
548     if (!table->remove(IRT_FIRST_SEGMENT, jobj)) {
549         ALOGW("JNI: DeleteWeakGlobalRef(%p) failed to find entry", jobj);
550     }
551 }
552
553 /*
554  * Remove a global reference.  In most cases it's the entry most recently
555  * added, which makes this pretty quick.
556  *
557  * Thought: if it's not the most recent entry, just null it out.  When we
558  * fill up, do a compaction pass before we expand the list.
559  */
560 static void deleteGlobalReference(jobject jobj) {
561     if (jobj == NULL) {
562         return;
563     }
564
565     ScopedPthreadMutexLock lock(&gDvm.jniGlobalRefLock);
566     if (!gDvm.jniGlobalRefTable.remove(IRT_FIRST_SEGMENT, jobj)) {
567         ALOGW("JNI: DeleteGlobalRef(%p) failed to find entry", jobj);
568         return;
569     }
570
571     if (kTrackGrefUsage && gDvm.jniGrefLimit != 0) {
572         int count = gDvm.jniGlobalRefTable.capacity();
573         // TODO: not quite right, need to subtract holes
574         if (count < gDvm.jniGlobalRefLoMark) {
575             ALOGD("GREF has decreased to %d", count);
576             gDvm.jniGlobalRefHiMark -= kGrefWaterInterval;
577             gDvm.jniGlobalRefLoMark -= kGrefWaterInterval;
578         }
579     }
580 }
581
582 /*
583  * Objects don't currently move, so we just need to create a reference
584  * that will ensure the array object isn't collected.
585  *
586  * We use a separate reference table, which is part of the GC root set.
587  */
588 static void pinPrimitiveArray(ArrayObject* arrayObj) {
589     if (arrayObj == NULL) {
590         return;
591     }
592
593     ScopedPthreadMutexLock lock(&gDvm.jniPinRefLock);
594
595     if (!dvmAddToReferenceTable(&gDvm.jniPinRefTable, (Object*)arrayObj)) {
596         dvmDumpReferenceTable(&gDvm.jniPinRefTable, "JNI pinned array");
597         ALOGE("Failed adding to JNI pinned array ref table (%d entries)",
598            (int) dvmReferenceTableEntries(&gDvm.jniPinRefTable));
599         ReportJniError();
600     }
601
602     /*
603      * If we're watching global ref usage, also keep an eye on these.
604      *
605      * The total number of pinned primitive arrays should be pretty small.
606      * A single array should not be pinned more than once or twice; any
607      * more than that is a strong indicator that a Release function is
608      * not being called.
609      */
610     if (kTrackGrefUsage && gDvm.jniGrefLimit != 0) {
611         int count = 0;
612         Object** ppObj = gDvm.jniPinRefTable.table;
613         while (ppObj < gDvm.jniPinRefTable.nextEntry) {
614             if (*ppObj++ == (Object*) arrayObj)
615                 count++;
616         }
617
618         if (count > kPinComplainThreshold) {
619             ALOGW("JNI: pin count on array %p (%s) is now %d",
620                 arrayObj, arrayObj->clazz->descriptor, count);
621             /* keep going */
622         }
623     }
624 }
625
626 /*
627  * Un-pin the array object.  If an object was pinned twice, it must be
628  * unpinned twice before it's free to move.
629  */
630 static void unpinPrimitiveArray(ArrayObject* arrayObj) {
631     if (arrayObj == NULL) {
632         return;
633     }
634
635     ScopedPthreadMutexLock lock(&gDvm.jniPinRefLock);
636     if (!dvmRemoveFromReferenceTable(&gDvm.jniPinRefTable,
637             gDvm.jniPinRefTable.table, (Object*) arrayObj))
638     {
639         ALOGW("JNI: unpinPrimitiveArray(%p) failed to find entry (valid=%d)",
640             arrayObj, dvmIsHeapAddress((Object*) arrayObj));
641         return;
642     }
643 }
644
645 /*
646  * Dump the contents of the JNI reference tables to the log file.
647  *
648  * We only dump the local refs associated with the current thread.
649  */
650 void dvmDumpJniReferenceTables() {
651     Thread* self = dvmThreadSelf();
652     self->jniLocalRefTable.dump("JNI local");
653     gDvm.jniGlobalRefTable.dump("JNI global");
654     dvmDumpReferenceTable(&gDvm.jniPinRefTable, "JNI pinned array");
655 }
656
657 /*
658  * Verify that a reference passed in from native code is one that the
659  * code is allowed to have.
660  *
661  * It's okay for native code to pass us a reference that:
662  *  - was passed in as an argument when invoked by native code (and hence
663  *    is in the JNI local refs table)
664  *  - was returned to it from JNI (and is now in the local refs table)
665  *  - is present in the JNI global refs table
666  *
667  * Used by -Xcheck:jni and GetObjectRefType.
668  */
669 jobjectRefType dvmGetJNIRefType(Thread* self, jobject jobj) {
670     /*
671      * IndirectRefKind is currently defined as an exact match of
672      * jobjectRefType, so this is easy.  We have to decode it to determine
673      * if it's a valid reference and not merely valid-looking.
674      */
675     assert(jobj != NULL);
676
677     Object* obj = dvmDecodeIndirectRef(self, jobj);
678     if (obj == reinterpret_cast<Object*>(jobj) && gDvmJni.workAroundAppJniBugs) {
679         // If we're handing out direct pointers, check whether 'jobj' is a direct reference
680         // to a local reference.
681         return self->jniLocalRefTable.contains(obj) ? JNILocalRefType : JNIInvalidRefType;
682     } else if (obj == kInvalidIndirectRefObject) {
683         return JNIInvalidRefType;
684     } else {
685         return (jobjectRefType) indirectRefKind(jobj);
686     }
687 }
688
689 static void dumpMethods(Method* methods, size_t methodCount, const char* name) {
690     size_t i;
691     for (i = 0; i < methodCount; ++i) {
692         Method* method = &methods[i];
693         if (strcmp(name, method->name) == 0) {
694             char* desc = dexProtoCopyMethodDescriptor(&method->prototype);
695             ALOGE("Candidate: %s.%s:%s", method->clazz->descriptor, name, desc);
696             free(desc);
697         }
698     }
699 }
700
701 static void dumpCandidateMethods(ClassObject* clazz, const char* methodName, const char* signature) {
702     ALOGE("ERROR: couldn't find native method");
703     ALOGE("Requested: %s.%s:%s", clazz->descriptor, methodName, signature);
704     dumpMethods(clazz->virtualMethods, clazz->virtualMethodCount, methodName);
705     dumpMethods(clazz->directMethods, clazz->directMethodCount, methodName);
706 }
707
708 /*
709  * Register a method that uses JNI calling conventions.
710  */
711 static bool dvmRegisterJNIMethod(ClassObject* clazz, const char* methodName,
712     const char* signature, void* fnPtr)
713 {
714     if (fnPtr == NULL) {
715         return false;
716     }
717
718     // If a signature starts with a '!', we take that as a sign that the native code doesn't
719     // need the extra JNI arguments (the JNIEnv* and the jclass).
720     bool fastJni = false;
721     if (*signature == '!') {
722         fastJni = true;
723         ++signature;
724         ALOGV("fast JNI method %s.%s:%s detected", clazz->descriptor, methodName, signature);
725     }
726
727     Method* method = dvmFindDirectMethodByDescriptor(clazz, methodName, signature);
728     if (method == NULL) {
729         method = dvmFindVirtualMethodByDescriptor(clazz, methodName, signature);
730     }
731     if (method == NULL) {
732         dumpCandidateMethods(clazz, methodName, signature);
733         return false;
734     }
735
736     if (!dvmIsNativeMethod(method)) {
737         ALOGW("Unable to register: not native: %s.%s:%s", clazz->descriptor, methodName, signature);
738         return false;
739     }
740
741     if (fastJni) {
742         // In this case, we have extra constraints to check...
743         if (dvmIsSynchronizedMethod(method)) {
744             // Synchronization is usually provided by the JNI bridge,
745             // but we won't have one.
746             ALOGE("fast JNI method %s.%s:%s cannot be synchronized",
747                     clazz->descriptor, methodName, signature);
748             return false;
749         }
750         if (!dvmIsStaticMethod(method)) {
751             // There's no real reason for this constraint, but since we won't
752             // be supplying a JNIEnv* or a jobject 'this', you're effectively
753             // static anyway, so it seems clearer to say so.
754             ALOGE("fast JNI method %s.%s:%s cannot be non-static",
755                     clazz->descriptor, methodName, signature);
756             return false;
757         }
758     }
759
760     if (method->nativeFunc != dvmResolveNativeMethod) {
761         /* this is allowed, but unusual */
762         ALOGV("Note: %s.%s:%s was already registered", clazz->descriptor, methodName, signature);
763     }
764
765     method->fastJni = fastJni;
766     dvmUseJNIBridge(method, fnPtr);
767
768     ALOGV("JNI-registered %s.%s:%s", clazz->descriptor, methodName, signature);
769     return true;
770 }
771
772 static const char* builtInPrefixes[] = {
773     "Landroid/",
774     "Lcom/android/",
775     "Lcom/google/android/",
776     "Ldalvik/",
777     "Ljava/",
778     "Ljavax/",
779     "Llibcore/",
780     "Lorg/apache/harmony/",
781 };
782
783 static bool shouldTrace(Method* method) {
784     const char* className = method->clazz->descriptor;
785     // Return true if the -Xjnitrace setting implies we should trace 'method'.
786     if (gDvm.jniTrace && strstr(className, gDvm.jniTrace)) {
787         return true;
788     }
789     // Return true if we're trying to log all third-party JNI activity and 'method' doesn't look
790     // like part of Android.
791     if (gDvmJni.logThirdPartyJni) {
792         for (size_t i = 0; i < NELEM(builtInPrefixes); ++i) {
793             if (strstr(className, builtInPrefixes[i]) == className) {
794                 return false;
795             }
796         }
797         return true;
798     }
799     return false;
800 }
801
802 /*
803  * Point "method->nativeFunc" at the JNI bridge, and overload "method->insns"
804  * to point at the actual function.
805  */
806 void dvmUseJNIBridge(Method* method, void* func) {
807     method->shouldTrace = shouldTrace(method);
808
809     // Does the method take any reference arguments?
810     method->noRef = true;
811     const char* cp = method->shorty;
812     while (*++cp != '\0') { // Pre-increment to skip return type.
813         if (*cp == 'L') {
814             method->noRef = false;
815             break;
816         }
817     }
818
819     DalvikBridgeFunc bridge = gDvmJni.useCheckJni ? dvmCheckCallJNIMethod : dvmCallJNIMethod;
820     dvmSetNativeFunc(method, bridge, (const u2*) func);
821 }
822
823 // TODO: rewrite this to share code with CheckJNI's tracing...
824 static void appendValue(char type, const JValue value, char* buf, size_t n, bool appendComma)
825 {
826     size_t len = strlen(buf);
827     if (len >= n - 32) { // 32 should be longer than anything we could append.
828         buf[len - 1] = '.';
829         buf[len - 2] = '.';
830         buf[len - 3] = '.';
831         return;
832     }
833     char* p = buf + len;
834     switch (type) {
835     case 'B':
836         if (value.b >= 0 && value.b < 10) {
837             sprintf(p, "%d", value.b);
838         } else {
839             sprintf(p, "%#x (%d)", value.b, value.b);
840         }
841         break;
842     case 'C':
843         if (value.c < 0x7f && value.c >= ' ') {
844             sprintf(p, "U+%x ('%c')", value.c, value.c);
845         } else {
846             sprintf(p, "U+%x", value.c);
847         }
848         break;
849     case 'D':
850         sprintf(p, "%g", value.d);
851         break;
852     case 'F':
853         sprintf(p, "%g", value.f);
854         break;
855     case 'I':
856         sprintf(p, "%d", value.i);
857         break;
858     case 'L':
859         sprintf(p, "%#x", value.i);
860         break;
861     case 'J':
862         sprintf(p, "%lld", value.j);
863         break;
864     case 'S':
865         sprintf(p, "%d", value.s);
866         break;
867     case 'V':
868         strcpy(p, "void");
869         break;
870     case 'Z':
871         strcpy(p, value.z ? "true" : "false");
872         break;
873     default:
874         sprintf(p, "unknown type '%c'", type);
875         break;
876     }
877
878     if (appendComma) {
879         strcat(p, ", ");
880     }
881 }
882
883 static void logNativeMethodEntry(const Method* method, const u4* args)
884 {
885     char thisString[32] = { 0 };
886     const u4* sp = args;
887     if (!dvmIsStaticMethod(method)) {
888         sprintf(thisString, "this=0x%08x ", *sp++);
889     }
890
891     char argsString[128]= { 0 };
892     const char* desc = &method->shorty[1];
893     while (*desc != '\0') {
894         char argType = *desc++;
895         JValue value;
896         if (argType == 'D' || argType == 'J') {
897             value.j = dvmGetArgLong(sp, 0);
898             sp += 2;
899         } else {
900             value.i = *sp++;
901         }
902         appendValue(argType, value, argsString, sizeof(argsString),
903         *desc != '\0');
904     }
905
906     std::string className(dvmHumanReadableDescriptor(method->clazz->descriptor));
907     char* signature = dexProtoCopyMethodDescriptor(&method->prototype);
908     ALOGI("-> %s %s%s %s(%s)", className.c_str(), method->name, signature, thisString, argsString);
909     free(signature);
910 }
911
912 static void logNativeMethodExit(const Method* method, Thread* self, const JValue returnValue)
913 {
914     std::string className(dvmHumanReadableDescriptor(method->clazz->descriptor));
915     char* signature = dexProtoCopyMethodDescriptor(&method->prototype);
916     if (dvmCheckException(self)) {
917         Object* exception = dvmGetException(self);
918         std::string exceptionClassName(dvmHumanReadableDescriptor(exception->clazz->descriptor));
919         ALOGI("<- %s %s%s threw %s", className.c_str(),
920                 method->name, signature, exceptionClassName.c_str());
921     } else {
922         char returnValueString[128] = { 0 };
923         char returnType = method->shorty[0];
924         appendValue(returnType, returnValue, returnValueString, sizeof(returnValueString), false);
925         ALOGI("<- %s %s%s returned %s", className.c_str(),
926                 method->name, signature, returnValueString);
927     }
928     free(signature);
929 }
930
931 /*
932  * Get the method currently being executed by examining the interp stack.
933  */
934 const Method* dvmGetCurrentJNIMethod() {
935     assert(dvmThreadSelf() != NULL);
936
937     void* fp = dvmThreadSelf()->interpSave.curFrame;
938     const Method* meth = SAVEAREA_FROM_FP(fp)->method;
939
940     assert(meth != NULL);
941     assert(dvmIsNativeMethod(meth));
942     return meth;
943 }
944
945 /*
946  * Track a JNI MonitorEnter in the current thread.
947  *
948  * The goal is to be able to "implicitly" release all JNI-held monitors
949  * when the thread detaches.
950  *
951  * Monitors may be entered multiple times, so we add a new entry for each
952  * enter call.  It would be more efficient to keep a counter.  At present
953  * there's no real motivation to improve this however.
954  */
955 static void trackMonitorEnter(Thread* self, Object* obj) {
956     static const int kInitialSize = 16;
957     ReferenceTable* refTable = &self->jniMonitorRefTable;
958
959     /* init table on first use */
960     if (refTable->table == NULL) {
961         assert(refTable->maxEntries == 0);
962
963         if (!dvmInitReferenceTable(refTable, kInitialSize, INT_MAX)) {
964             ALOGE("Unable to initialize monitor tracking table");
965             ReportJniError();
966         }
967     }
968
969     if (!dvmAddToReferenceTable(refTable, obj)) {
970         /* ran out of memory? could throw exception instead */
971         ALOGE("Unable to add entry to monitor tracking table");
972         ReportJniError();
973     } else {
974         LOGVV("--- added monitor %p", obj);
975     }
976 }
977
978 /*
979  * Track a JNI MonitorExit in the current thread.
980  */
981 static void trackMonitorExit(Thread* self, Object* obj) {
982     ReferenceTable* pRefTable = &self->jniMonitorRefTable;
983
984     if (!dvmRemoveFromReferenceTable(pRefTable, pRefTable->table, obj)) {
985         ALOGE("JNI monitor %p not found in tracking list", obj);
986         /* keep going? */
987     } else {
988         LOGVV("--- removed monitor %p", obj);
989     }
990 }
991
992 /*
993  * Release all monitors held by the jniMonitorRefTable list.
994  */
995 void dvmReleaseJniMonitors(Thread* self) {
996     ReferenceTable* pRefTable = &self->jniMonitorRefTable;
997     Object** top = pRefTable->table;
998
999     if (top == NULL) {
1000         return;
1001     }
1002     Object** ptr = pRefTable->nextEntry;
1003     while (--ptr >= top) {
1004         if (!dvmUnlockObject(self, *ptr)) {
1005             ALOGW("Unable to unlock monitor %p at thread detach", *ptr);
1006         } else {
1007             LOGVV("--- detach-releasing monitor %p", *ptr);
1008         }
1009     }
1010
1011     /* zap it */
1012     pRefTable->nextEntry = pRefTable->table;
1013 }
1014
1015 /*
1016  * Determine if the specified class can be instantiated from JNI.  This
1017  * is used by AllocObject / NewObject, which are documented as throwing
1018  * an exception for abstract and interface classes, and not accepting
1019  * array classes.  We also want to reject attempts to create new Class
1020  * objects, since only DefineClass should do that.
1021  */
1022 static bool canAllocClass(ClassObject* clazz) {
1023     if (dvmIsAbstractClass(clazz) || dvmIsInterfaceClass(clazz)) {
1024         /* JNI spec defines what this throws */
1025         dvmThrowInstantiationException(clazz, "abstract class or interface");
1026         return false;
1027     } else if (dvmIsArrayClass(clazz) || dvmIsTheClassClass(clazz)) {
1028         /* spec says "must not" for arrays, ignores Class */
1029         dvmThrowInstantiationException(clazz, "wrong JNI function");
1030         return false;
1031     }
1032     return true;
1033 }
1034
1035
1036 /*
1037  * ===========================================================================
1038  *      JNI call bridge
1039  * ===========================================================================
1040  */
1041
1042 /*
1043  * The functions here form a bridge between interpreted code and JNI native
1044  * functions.  The basic task is to convert an array of primitives and
1045  * references into C-style function arguments.  This is architecture-specific
1046  * and usually requires help from assembly code.
1047  *
1048  * The bridge takes four arguments: the array of parameters, a place to
1049  * store the function result (if any), the method to call, and a pointer
1050  * to the current thread.
1051  *
1052  * These functions aren't called directly from elsewhere in the VM.
1053  * A pointer in the Method struct points to one of these, and when a native
1054  * method is invoked the interpreter jumps to it.
1055  *
1056  * (The "internal native" methods are invoked the same way, but instead
1057  * of calling through a bridge, the target method is called directly.)
1058  *
1059  * The "args" array should not be modified, but we do so anyway for
1060  * performance reasons.  We know that it points to the "outs" area on
1061  * the current method's interpreted stack.  This area is ignored by the
1062  * precise GC, because there is no register map for a native method (for
1063  * an interpreted method the args would be listed in the argument set).
1064  * We know all of the values exist elsewhere on the interpreted stack,
1065  * because the method call setup copies them right before making the call,
1066  * so we don't have to worry about concealing stuff from the GC.
1067  *
1068  * If we don't want to modify "args", we either have to create a local
1069  * copy and modify it before calling dvmPlatformInvoke, or we have to do
1070  * the local reference replacement within dvmPlatformInvoke.  The latter
1071  * has some performance advantages, though if we can inline the local
1072  * reference adds we may win when there's a lot of reference args (unless
1073  * we want to code up some local ref table manipulation in assembly.
1074  */
1075
1076 /*
1077  * If necessary, convert the value in pResult from a local/global reference
1078  * to an object pointer.
1079  *
1080  * If the returned reference is invalid, kInvalidIndirectRefObject will
1081  * be returned in pResult.
1082  */
1083 static inline void convertReferenceResult(JNIEnv* env, JValue* pResult,
1084     const Method* method, Thread* self)
1085 {
1086     if (method->shorty[0] == 'L' && !dvmCheckException(self) && pResult->l != NULL) {
1087         pResult->l = dvmDecodeIndirectRef(self, (jobject) pResult->l);
1088     }
1089 }
1090
1091 /*
1092  * General form, handles all cases.
1093  */
1094 void dvmCallJNIMethod(const u4* args, JValue* pResult, const Method* method, Thread* self) {
1095     u4* modArgs = (u4*) args;
1096     jclass staticMethodClass = NULL;
1097
1098     u4 accessFlags = method->accessFlags;
1099     bool isSynchronized = (accessFlags & ACC_SYNCHRONIZED) != 0;
1100
1101     //ALOGI("JNI calling %p (%s.%s:%s):", method->insns,
1102     //    method->clazz->descriptor, method->name, method->shorty);
1103
1104     /*
1105      * Walk the argument list, creating local references for appropriate
1106      * arguments.
1107      */
1108     int idx = 0;
1109     Object* lockObj;
1110     if ((accessFlags & ACC_STATIC) != 0) {
1111         lockObj = (Object*) method->clazz;
1112         /* add the class object we pass in */
1113         staticMethodClass = (jclass) addLocalReference(self, (Object*) method->clazz);
1114     } else {
1115         lockObj = (Object*) args[0];
1116         /* add "this" */
1117         modArgs[idx++] = (u4) addLocalReference(self, (Object*) modArgs[0]);
1118     }
1119
1120     if (!method->noRef) {
1121         const char* shorty = &method->shorty[1];        /* skip return type */
1122         while (*shorty != '\0') {
1123             switch (*shorty++) {
1124             case 'L':
1125                 //ALOGI("  local %d: 0x%08x", idx, modArgs[idx]);
1126                 if (modArgs[idx] != 0) {
1127                     modArgs[idx] = (u4) addLocalReference(self, (Object*) modArgs[idx]);
1128                 }
1129                 break;
1130             case 'D':
1131             case 'J':
1132                 idx++;
1133                 break;
1134             default:
1135                 /* Z B C S I -- do nothing */
1136                 break;
1137             }
1138             idx++;
1139         }
1140     }
1141
1142     if (UNLIKELY(method->shouldTrace)) {
1143         logNativeMethodEntry(method, args);
1144     }
1145     if (UNLIKELY(isSynchronized)) {
1146         dvmLockObject(self, lockObj);
1147     }
1148
1149     ThreadStatus oldStatus = dvmChangeStatus(self, THREAD_NATIVE);
1150
1151     ANDROID_MEMBAR_FULL();      /* guarantee ordering on method->insns */
1152     assert(method->insns != NULL);
1153
1154     JNIEnv* env = self->jniEnv;
1155     COMPUTE_STACK_SUM(self);
1156     dvmPlatformInvoke(env,
1157             (ClassObject*) staticMethodClass,
1158             method->jniArgInfo, method->insSize, modArgs, method->shorty,
1159             (void*) method->insns, pResult);
1160     CHECK_STACK_SUM(self);
1161
1162     dvmChangeStatus(self, oldStatus);
1163
1164     convertReferenceResult(env, pResult, method, self);
1165
1166     if (UNLIKELY(isSynchronized)) {
1167         dvmUnlockObject(self, lockObj);
1168     }
1169     if (UNLIKELY(method->shouldTrace)) {
1170         logNativeMethodExit(method, self, *pResult);
1171     }
1172 }
1173
1174 /*
1175  * ===========================================================================
1176  *      JNI implementation
1177  * ===========================================================================
1178  */
1179
1180 /*
1181  * Return the version of the native method interface.
1182  */
1183 static jint GetVersion(JNIEnv* env) {
1184     /*
1185      * There is absolutely no need to toggle the mode for correct behavior.
1186      * However, it does provide native code with a simple "suspend self
1187      * if necessary" call.
1188      */
1189     ScopedJniThreadState ts(env);
1190     return JNI_VERSION_1_6;
1191 }
1192
1193 /*
1194  * Create a new class from a bag of bytes.
1195  *
1196  * This is not currently supported within Dalvik.
1197  */
1198 static jclass DefineClass(JNIEnv* env, const char *name, jobject loader,
1199     const jbyte* buf, jsize bufLen)
1200 {
1201     UNUSED_PARAMETER(name);
1202     UNUSED_PARAMETER(loader);
1203     UNUSED_PARAMETER(buf);
1204     UNUSED_PARAMETER(bufLen);
1205
1206     ScopedJniThreadState ts(env);
1207     ALOGW("JNI DefineClass is not supported");
1208     return NULL;
1209 }
1210
1211 /*
1212  * Find a class by name.
1213  *
1214  * We have to use the "no init" version of FindClass here, because we might
1215  * be getting the class prior to registering native methods that will be
1216  * used in <clinit>.
1217  *
1218  * We need to get the class loader associated with the current native
1219  * method.  If there is no native method, e.g. we're calling this from native
1220  * code right after creating the VM, the spec says we need to use the class
1221  * loader returned by "ClassLoader.getBaseClassLoader".  There is no such
1222  * method, but it's likely they meant ClassLoader.getSystemClassLoader.
1223  * We can't get that until after the VM has initialized though.
1224  */
1225 static jclass FindClass(JNIEnv* env, const char* name) {
1226     ScopedJniThreadState ts(env);
1227
1228     const Method* thisMethod = dvmGetCurrentJNIMethod();
1229     assert(thisMethod != NULL);
1230
1231     Object* loader;
1232     Object* trackedLoader = NULL;
1233     if (ts.self()->classLoaderOverride != NULL) {
1234         /* hack for JNI_OnLoad */
1235         assert(strcmp(thisMethod->name, "nativeLoad") == 0);
1236         loader = ts.self()->classLoaderOverride;
1237     } else if (thisMethod == gDvm.methDalvikSystemNativeStart_main ||
1238                thisMethod == gDvm.methDalvikSystemNativeStart_run) {
1239         /* start point of invocation interface */
1240         if (!gDvm.initializing) {
1241             loader = trackedLoader = dvmGetSystemClassLoader();
1242         } else {
1243             loader = NULL;
1244         }
1245     } else {
1246         loader = thisMethod->clazz->classLoader;
1247     }
1248
1249     char* descriptor = dvmNameToDescriptor(name);
1250     if (descriptor == NULL) {
1251         return NULL;
1252     }
1253     ClassObject* clazz = dvmFindClassNoInit(descriptor, loader);
1254     free(descriptor);
1255
1256     jclass jclazz = (jclass) addLocalReference(ts.self(), (Object*) clazz);
1257     dvmReleaseTrackedAlloc(trackedLoader, ts.self());
1258     return jclazz;
1259 }
1260
1261 /*
1262  * Return the superclass of a class.
1263  */
1264 static jclass GetSuperclass(JNIEnv* env, jclass jclazz) {
1265     ScopedJniThreadState ts(env);
1266     ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz);
1267     return (jclass) addLocalReference(ts.self(), (Object*)clazz->super);
1268 }
1269
1270 /*
1271  * Determine whether an object of clazz1 can be safely cast to clazz2.
1272  *
1273  * Like IsInstanceOf, but with a pair of class objects instead of obj+class.
1274  */
1275 static jboolean IsAssignableFrom(JNIEnv* env, jclass jclazz1, jclass jclazz2) {
1276     ScopedJniThreadState ts(env);
1277     ClassObject* clazz1 = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz1);
1278     ClassObject* clazz2 = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz2);
1279     return dvmInstanceof(clazz1, clazz2);
1280 }
1281
1282 /*
1283  * Given a java.lang.reflect.Method or .Constructor, return a methodID.
1284  */
1285 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jmethod) {
1286     ScopedJniThreadState ts(env);
1287     Object* method = dvmDecodeIndirectRef(ts.self(), jmethod);
1288     return (jmethodID) dvmGetMethodFromReflectObj(method);
1289 }
1290
1291 /*
1292  * Given a java.lang.reflect.Field, return a fieldID.
1293  */
1294 static jfieldID FromReflectedField(JNIEnv* env, jobject jfield) {
1295     ScopedJniThreadState ts(env);
1296     Object* field = dvmDecodeIndirectRef(ts.self(), jfield);
1297     return (jfieldID) dvmGetFieldFromReflectObj(field);
1298 }
1299
1300 /*
1301  * Convert a methodID to a java.lang.reflect.Method or .Constructor.
1302  *
1303  * (The "isStatic" field does not appear in the spec.)
1304  *
1305  * Throws OutOfMemory and returns NULL on failure.
1306  */
1307 static jobject ToReflectedMethod(JNIEnv* env, jclass jcls, jmethodID methodID, jboolean isStatic) {
1308     ScopedJniThreadState ts(env);
1309     ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jcls);
1310     Object* obj = dvmCreateReflectObjForMethod(clazz, (Method*) methodID);
1311     dvmReleaseTrackedAlloc(obj, NULL);
1312     return addLocalReference(ts.self(), obj);
1313 }
1314
1315 /*
1316  * Convert a fieldID to a java.lang.reflect.Field.
1317  *
1318  * (The "isStatic" field does not appear in the spec.)
1319  *
1320  * Throws OutOfMemory and returns NULL on failure.
1321  */
1322 static jobject ToReflectedField(JNIEnv* env, jclass jcls, jfieldID fieldID, jboolean isStatic) {
1323     ScopedJniThreadState ts(env);
1324     ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jcls);
1325     Object* obj = dvmCreateReflectObjForField(clazz, (Field*) fieldID);
1326     dvmReleaseTrackedAlloc(obj, NULL);
1327     return addLocalReference(ts.self(), obj);
1328 }
1329
1330 /*
1331  * Take this exception and throw it.
1332  */
1333 static jint Throw(JNIEnv* env, jthrowable jobj) {
1334     ScopedJniThreadState ts(env);
1335     if (jobj != NULL) {
1336         Object* obj = dvmDecodeIndirectRef(ts.self(), jobj);
1337         dvmSetException(ts.self(), obj);
1338         return JNI_OK;
1339     }
1340     return JNI_ERR;
1341 }
1342
1343 /*
1344  * Constructs an exception object from the specified class with the message
1345  * specified by "message", and throws it.
1346  */
1347 static jint ThrowNew(JNIEnv* env, jclass jclazz, const char* message) {
1348     ScopedJniThreadState ts(env);
1349     ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz);
1350     dvmThrowException(clazz, message);
1351     // TODO: should return failure if this didn't work (e.g. OOM)
1352     return JNI_OK;
1353 }
1354
1355 /*
1356  * If an exception is being thrown, return the exception object.  Otherwise,
1357  * return NULL.
1358  *
1359  * TODO: if there is no pending exception, we should be able to skip the
1360  * enter/exit checks.  If we find one, we need to enter and then re-fetch
1361  * the exception (in case it got moved by a compacting GC).
1362  */
1363 static jthrowable ExceptionOccurred(JNIEnv* env) {
1364     ScopedJniThreadState ts(env);
1365     Object* exception = dvmGetException(ts.self());
1366     jthrowable localException = (jthrowable) addLocalReference(ts.self(), exception);
1367     if (localException == NULL && exception != NULL) {
1368         /*
1369          * We were unable to add a new local reference, and threw a new
1370          * exception.  We can't return "exception", because it's not a
1371          * local reference.  So we have to return NULL, indicating that
1372          * there was no exception, even though it's pretty much raining
1373          * exceptions in here.
1374          */
1375         ALOGW("JNI WARNING: addLocal/exception combo");
1376     }
1377     return localException;
1378 }
1379
1380 /*
1381  * Print an exception and stack trace to stderr.
1382  */
1383 static void ExceptionDescribe(JNIEnv* env) {
1384     ScopedJniThreadState ts(env);
1385     Object* exception = dvmGetException(ts.self());
1386     if (exception != NULL) {
1387         dvmPrintExceptionStackTrace();
1388     } else {
1389         ALOGI("Odd: ExceptionDescribe called, but no exception pending");
1390     }
1391 }
1392
1393 /*
1394  * Clear the exception currently being thrown.
1395  *
1396  * TODO: we should be able to skip the enter/exit stuff.
1397  */
1398 static void ExceptionClear(JNIEnv* env) {
1399     ScopedJniThreadState ts(env);
1400     dvmClearException(ts.self());
1401 }
1402
1403 /*
1404  * Kill the VM.  This function does not return.
1405  */
1406 static void FatalError(JNIEnv* env, const char* msg) {
1407     //dvmChangeStatus(NULL, THREAD_RUNNING);
1408     ALOGE("JNI posting fatal error: %s", msg);
1409     ReportJniError();
1410 }
1411
1412 /*
1413  * Push a new JNI frame on the stack, with a new set of locals.
1414  *
1415  * The new frame must have the same method pointer.  (If for no other
1416  * reason than FindClass needs it to get the appropriate class loader.)
1417  */
1418 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
1419     ScopedJniThreadState ts(env);
1420     if (!ensureLocalCapacity(ts.self(), capacity) ||
1421             !dvmPushLocalFrame(ts.self(), dvmGetCurrentJNIMethod()))
1422     {
1423         /* yes, OutOfMemoryError, not StackOverflowError */
1424         dvmClearException(ts.self());
1425         dvmThrowOutOfMemoryError("out of stack in JNI PushLocalFrame");
1426         return JNI_ERR;
1427     }
1428     return JNI_OK;
1429 }
1430
1431 /*
1432  * Pop the local frame off.  If "jresult" is not null, add it as a
1433  * local reference on the now-current frame.
1434  */
1435 static jobject PopLocalFrame(JNIEnv* env, jobject jresult) {
1436     ScopedJniThreadState ts(env);
1437     Object* result = dvmDecodeIndirectRef(ts.self(), jresult);
1438     if (!dvmPopLocalFrame(ts.self())) {
1439         ALOGW("JNI WARNING: too many PopLocalFrame calls");
1440         dvmClearException(ts.self());
1441         dvmThrowRuntimeException("too many PopLocalFrame calls");
1442     }
1443     return addLocalReference(ts.self(), result);
1444 }
1445
1446 /*
1447  * Add a reference to the global list.
1448  */
1449 static jobject NewGlobalRef(JNIEnv* env, jobject jobj) {
1450     ScopedJniThreadState ts(env);
1451     Object* obj = dvmDecodeIndirectRef(ts.self(), jobj);
1452     return addGlobalReference(obj);
1453 }
1454
1455 /*
1456  * Delete a reference from the global list.
1457  */
1458 static void DeleteGlobalRef(JNIEnv* env, jobject jglobalRef) {
1459     ScopedJniThreadState ts(env);
1460     deleteGlobalReference(jglobalRef);
1461 }
1462
1463
1464 /*
1465  * Add a reference to the local list.
1466  */
1467 static jobject NewLocalRef(JNIEnv* env, jobject jobj) {
1468     ScopedJniThreadState ts(env);
1469     Object* obj = dvmDecodeIndirectRef(ts.self(), jobj);
1470     return addLocalReference(ts.self(), obj);
1471 }
1472
1473 /*
1474  * Delete a reference from the local list.
1475  */
1476 static void DeleteLocalRef(JNIEnv* env, jobject jlocalRef) {
1477     ScopedJniThreadState ts(env);
1478     deleteLocalReference(ts.self(), jlocalRef);
1479 }
1480
1481 /*
1482  * Ensure that the local references table can hold at least this many
1483  * references.
1484  */
1485 static jint EnsureLocalCapacity(JNIEnv* env, jint capacity) {
1486     ScopedJniThreadState ts(env);
1487     bool okay = ensureLocalCapacity(ts.self(), capacity);
1488     if (!okay) {
1489         dvmThrowOutOfMemoryError("can't ensure local reference capacity");
1490     }
1491     return okay ? 0 : -1;
1492 }
1493
1494
1495 /*
1496  * Determine whether two Object references refer to the same underlying object.
1497  */
1498 static jboolean IsSameObject(JNIEnv* env, jobject jref1, jobject jref2) {
1499     ScopedJniThreadState ts(env);
1500     Object* obj1 = dvmDecodeIndirectRef(ts.self(), jref1);
1501     Object* obj2 = dvmDecodeIndirectRef(ts.self(), jref2);
1502     return (obj1 == obj2);
1503 }
1504
1505 /*
1506  * Allocate a new object without invoking any constructors.
1507  */
1508 static jobject AllocObject(JNIEnv* env, jclass jclazz) {
1509     ScopedJniThreadState ts(env);
1510
1511     ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz);
1512     if (!canAllocClass(clazz) ||
1513         (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz)))
1514     {
1515         assert(dvmCheckException(ts.self()));
1516         return NULL;
1517     }
1518
1519     Object* newObj = dvmAllocObject(clazz, ALLOC_DONT_TRACK);
1520     return addLocalReference(ts.self(), newObj);
1521 }
1522
1523 /*
1524  * Allocate a new object and invoke the supplied constructor.
1525  */
1526 static jobject NewObject(JNIEnv* env, jclass jclazz, jmethodID methodID, ...) {
1527     ScopedJniThreadState ts(env);
1528     ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz);
1529
1530     if (!canAllocClass(clazz) || (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz))) {
1531         assert(dvmCheckException(ts.self()));
1532         return NULL;
1533     }
1534
1535     Object* newObj = dvmAllocObject(clazz, ALLOC_DONT_TRACK);
1536     jobject result = addLocalReference(ts.self(), newObj);
1537     if (newObj != NULL) {
1538         JValue unused;
1539         va_list args;
1540         va_start(args, methodID);
1541         dvmCallMethodV(ts.self(), (Method*) methodID, newObj, true, &unused, args);
1542         va_end(args);
1543     }
1544     return result;
1545 }
1546
1547 static jobject NewObjectV(JNIEnv* env, jclass jclazz, jmethodID methodID, va_list args) {
1548     ScopedJniThreadState ts(env);
1549     ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz);
1550
1551     if (!canAllocClass(clazz) || (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz))) {
1552         assert(dvmCheckException(ts.self()));
1553         return NULL;
1554     }
1555
1556     Object* newObj = dvmAllocObject(clazz, ALLOC_DONT_TRACK);
1557     jobject result = addLocalReference(ts.self(), newObj);
1558     if (newObj != NULL) {
1559         JValue unused;
1560         dvmCallMethodV(ts.self(), (Method*) methodID, newObj, true, &unused, args);
1561     }
1562     return result;
1563 }
1564
1565 static jobject NewObjectA(JNIEnv* env, jclass jclazz, jmethodID methodID, jvalue* args) {
1566     ScopedJniThreadState ts(env);
1567     ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz);
1568
1569     if (!canAllocClass(clazz) || (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz))) {
1570         assert(dvmCheckException(ts.self()));
1571         return NULL;
1572     }
1573
1574     Object* newObj = dvmAllocObject(clazz, ALLOC_DONT_TRACK);
1575     jobject result = addLocalReference(ts.self(), newObj);
1576     if (newObj != NULL) {
1577         JValue unused;
1578         dvmCallMethodA(ts.self(), (Method*) methodID, newObj, true, &unused, args);
1579     }
1580     return result;
1581 }
1582
1583 /*
1584  * Returns the class of an object.
1585  *
1586  * JNI spec says: obj must not be NULL.
1587  */
1588 static jclass GetObjectClass(JNIEnv* env, jobject jobj) {
1589     ScopedJniThreadState ts(env);
1590
1591     assert(jobj != NULL);
1592
1593     Object* obj = dvmDecodeIndirectRef(ts.self(), jobj);
1594     return (jclass) addLocalReference(ts.self(), (Object*) obj->clazz);
1595 }
1596
1597 /*
1598  * Determine whether "obj" is an instance of "clazz".
1599  */
1600 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass jclazz) {
1601     ScopedJniThreadState ts(env);
1602
1603     assert(jclazz != NULL);
1604     if (jobj == NULL) {
1605         return true;
1606     }
1607
1608     Object* obj = dvmDecodeIndirectRef(ts.self(), jobj);
1609     ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz);
1610     return dvmInstanceof(obj->clazz, clazz);
1611 }
1612
1613 /*
1614  * Get a method ID for an instance method.
1615  *
1616  * While Dalvik bytecode has distinct instructions for virtual, super,
1617  * static, direct, and interface method invocation, JNI only provides
1618  * two functions for acquiring a method ID.  This call handles everything
1619  * but static methods.
1620  *
1621  * JNI defines <init> as an instance method, but Dalvik considers it a
1622  * "direct" method, so we have to special-case it here.
1623  *
1624  * Dalvik also puts all private methods into the "direct" list, so we
1625  * really need to just search both lists.
1626  */
1627 static jmethodID GetMethodID(JNIEnv* env, jclass jclazz, const char* name, const char* sig) {
1628     ScopedJniThreadState ts(env);
1629
1630     ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz);
1631     if (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz)) {
1632         assert(dvmCheckException(ts.self()));
1633     } else if (dvmIsInterfaceClass(clazz)) {
1634         Method* meth = dvmFindInterfaceMethodHierByDescriptor(clazz, name, sig);
1635         if (meth == NULL) {
1636             dvmThrowExceptionFmt(gDvm.exNoSuchMethodError,
1637                 "no method with name='%s' signature='%s' in interface %s",
1638                 name, sig, clazz->descriptor);
1639         }
1640         return (jmethodID) meth;
1641     }
1642     Method* meth = dvmFindVirtualMethodHierByDescriptor(clazz, name, sig);
1643     if (meth == NULL) {
1644         /* search private methods and constructors; non-hierarchical */
1645         meth = dvmFindDirectMethodByDescriptor(clazz, name, sig);
1646     }
1647     if (meth != NULL && dvmIsStaticMethod(meth)) {
1648         IF_ALOGD() {
1649             char* desc = dexProtoCopyMethodDescriptor(&meth->prototype);
1650             ALOGD("GetMethodID: not returning static method %s.%s %s",
1651                     clazz->descriptor, meth->name, desc);
1652             free(desc);
1653         }
1654         meth = NULL;
1655     }
1656     if (meth == NULL) {
1657         dvmThrowExceptionFmt(gDvm.exNoSuchMethodError,
1658                 "no method with name='%s' signature='%s' in class %s",
1659                 name, sig, clazz->descriptor);
1660     } else {
1661         /*
1662          * The method's class may not be the same as clazz, but if
1663          * it isn't this must be a virtual method and the class must
1664          * be a superclass (and, hence, already initialized).
1665          */
1666         assert(dvmIsClassInitialized(meth->clazz) || dvmIsClassInitializing(meth->clazz));
1667     }
1668     return (jmethodID) meth;
1669 }
1670
1671 /*
1672  * Get a field ID (instance fields).
1673  */
1674 static jfieldID GetFieldID(JNIEnv* env, jclass jclazz, const char* name, const char* sig) {
1675     ScopedJniThreadState ts(env);
1676
1677     ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz);
1678
1679     if (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz)) {
1680         assert(dvmCheckException(ts.self()));
1681         return NULL;
1682     }
1683
1684     jfieldID id = (jfieldID) dvmFindInstanceFieldHier(clazz, name, sig);
1685     if (id == NULL) {
1686         dvmThrowExceptionFmt(gDvm.exNoSuchFieldError,
1687                 "no field with name='%s' signature='%s' in class %s",
1688                 name, sig, clazz->descriptor);
1689     }
1690     return id;
1691 }
1692
1693 /*
1694  * Get the method ID for a static method in a class.
1695  */
1696 static jmethodID GetStaticMethodID(JNIEnv* env, jclass jclazz, const char* name, const char* sig) {
1697     ScopedJniThreadState ts(env);
1698
1699     ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz);
1700     if (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz)) {
1701         assert(dvmCheckException(ts.self()));
1702         return NULL;
1703     }
1704
1705     Method* meth = dvmFindDirectMethodHierByDescriptor(clazz, name, sig);
1706
1707     /* make sure it's static, not virtual+private */
1708     if (meth != NULL && !dvmIsStaticMethod(meth)) {
1709         IF_ALOGD() {
1710             char* desc = dexProtoCopyMethodDescriptor(&meth->prototype);
1711             ALOGD("GetStaticMethodID: not returning nonstatic method %s.%s %s",
1712                     clazz->descriptor, meth->name, desc);
1713             free(desc);
1714         }
1715         meth = NULL;
1716     }
1717
1718     jmethodID id = (jmethodID) meth;
1719     if (id == NULL) {
1720         dvmThrowExceptionFmt(gDvm.exNoSuchMethodError,
1721                 "no static method with name='%s' signature='%s' in class %s",
1722                 name, sig, clazz->descriptor);
1723     }
1724     return id;
1725 }
1726
1727 /*
1728  * Get a field ID (static fields).
1729  */
1730 static jfieldID GetStaticFieldID(JNIEnv* env, jclass jclazz, const char* name, const char* sig) {
1731     ScopedJniThreadState ts(env);
1732
1733     ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz);
1734     if (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz)) {
1735         assert(dvmCheckException(ts.self()));
1736         return NULL;
1737     }
1738
1739     jfieldID id = (jfieldID) dvmFindStaticFieldHier(clazz, name, sig);
1740     if (id == NULL) {
1741         dvmThrowExceptionFmt(gDvm.exNoSuchFieldError,
1742                 "no static field with name='%s' signature='%s' in class %s",
1743                 name, sig, clazz->descriptor);
1744     }
1745     return id;
1746 }
1747
1748 /*
1749  * Get a static field.
1750  *
1751  * If we get an object reference, add it to the local refs list.
1752  */
1753 #define GET_STATIC_TYPE_FIELD(_ctype, _jname, _isref)                       \
1754     static _ctype GetStatic##_jname##Field(JNIEnv* env, jclass jclazz,      \
1755         jfieldID fieldID)                                                   \
1756     {                                                                       \
1757         UNUSED_PARAMETER(jclazz);                                           \
1758         ScopedJniThreadState ts(env);                                       \
1759         StaticField* sfield = (StaticField*) fieldID;                       \
1760         _ctype value;                                                       \
1761         if (dvmIsVolatileField(sfield)) {                                   \
1762             if (_isref) {   /* only when _ctype==jobject */                 \
1763                 Object* obj = dvmGetStaticFieldObjectVolatile(sfield);      \
1764                 value = (_ctype)(u4)addLocalReference(ts.self(), obj);            \
1765             } else {                                                        \
1766                 value = (_ctype) dvmGetStaticField##_jname##Volatile(sfield);\
1767             }                                                               \
1768         } else {                                                            \
1769             if (_isref) {                                                   \
1770                 Object* obj = dvmGetStaticFieldObject(sfield);              \
1771                 value = (_ctype)(u4)addLocalReference(ts.self(), obj);            \
1772             } else {                                                        \
1773                 value = (_ctype) dvmGetStaticField##_jname(sfield);         \
1774             }                                                               \
1775         }                                                                   \
1776         return value;                                                       \
1777     }
1778 GET_STATIC_TYPE_FIELD(jobject, Object, true);
1779 GET_STATIC_TYPE_FIELD(jboolean, Boolean, false);
1780 GET_STATIC_TYPE_FIELD(jbyte, Byte, false);
1781 GET_STATIC_TYPE_FIELD(jchar, Char, false);
1782 GET_STATIC_TYPE_FIELD(jshort, Short, false);
1783 GET_STATIC_TYPE_FIELD(jint, Int, false);
1784 GET_STATIC_TYPE_FIELD(jlong, Long, false);
1785 GET_STATIC_TYPE_FIELD(jfloat, Float, false);
1786 GET_STATIC_TYPE_FIELD(jdouble, Double, false);
1787
1788 /*
1789  * Set a static field.
1790  */
1791 #define SET_STATIC_TYPE_FIELD(_ctype, _ctype2, _jname, _isref)              \
1792     static void SetStatic##_jname##Field(JNIEnv* env, jclass jclazz,        \
1793         jfieldID fieldID, _ctype value)                                     \
1794     {                                                                       \
1795         UNUSED_PARAMETER(jclazz);                                           \
1796         ScopedJniThreadState ts(env);                                       \
1797         StaticField* sfield = (StaticField*) fieldID;                       \
1798         if (dvmIsVolatileField(sfield)) {                                   \
1799             if (_isref) {   /* only when _ctype==jobject */                 \
1800                 Object* valObj = dvmDecodeIndirectRef(ts.self(), (jobject)(u4)value); \
1801                 dvmSetStaticFieldObjectVolatile(sfield, valObj);            \
1802             } else {                                                        \
1803                 dvmSetStaticField##_jname##Volatile(sfield, (_ctype2)value);\
1804             }                                                               \
1805         } else {                                                            \
1806             if (_isref) {                                                   \
1807                 Object* valObj = dvmDecodeIndirectRef(ts.self(), (jobject)(u4)value); \
1808                 dvmSetStaticFieldObject(sfield, valObj);                    \
1809             } else {                                                        \
1810                 dvmSetStaticField##_jname(sfield, (_ctype2)value);          \
1811             }                                                               \
1812         }                                                                   \
1813     }
1814 SET_STATIC_TYPE_FIELD(jobject, Object*, Object, true);
1815 SET_STATIC_TYPE_FIELD(jboolean, bool, Boolean, false);
1816 SET_STATIC_TYPE_FIELD(jbyte, s1, Byte, false);
1817 SET_STATIC_TYPE_FIELD(jchar, u2, Char, false);
1818 SET_STATIC_TYPE_FIELD(jshort, s2, Short, false);
1819 SET_STATIC_TYPE_FIELD(jint, s4, Int, false);
1820 SET_STATIC_TYPE_FIELD(jlong, s8, Long, false);
1821 SET_STATIC_TYPE_FIELD(jfloat, float, Float, false);
1822 SET_STATIC_TYPE_FIELD(jdouble, double, Double, false);
1823
1824 /*
1825  * Get an instance field.
1826  *
1827  * If we get an object reference, add it to the local refs list.
1828  */
1829 #define GET_TYPE_FIELD(_ctype, _jname, _isref)                              \
1830     static _ctype Get##_jname##Field(JNIEnv* env, jobject jobj,             \
1831         jfieldID fieldID)                                                   \
1832     {                                                                       \
1833         ScopedJniThreadState ts(env);                                       \
1834         Object* obj = dvmDecodeIndirectRef(ts.self(), jobj);                      \
1835         InstField* field = (InstField*) fieldID;                            \
1836         _ctype value;                                                       \
1837         if (dvmIsVolatileField(field)) {                            \
1838             if (_isref) {   /* only when _ctype==jobject */                 \
1839                 Object* valObj =                                            \
1840                     dvmGetFieldObjectVolatile(obj, field->byteOffset);      \
1841                 value = (_ctype)(u4)addLocalReference(ts.self(), valObj);         \
1842             } else {                                                        \
1843                 value = (_ctype)                                            \
1844                     dvmGetField##_jname##Volatile(obj, field->byteOffset);  \
1845             }                                                               \
1846         } else {                                                            \
1847             if (_isref) {                                                   \
1848                 Object* valObj = dvmGetFieldObject(obj, field->byteOffset); \
1849                 value = (_ctype)(u4)addLocalReference(ts.self(), valObj);         \
1850             } else {                                                        \
1851                 value = (_ctype) dvmGetField##_jname(obj, field->byteOffset);\
1852             }                                                               \
1853         }                                                                   \
1854         return value;                                                       \
1855     }
1856 GET_TYPE_FIELD(jobject, Object, true);
1857 GET_TYPE_FIELD(jboolean, Boolean, false);
1858 GET_TYPE_FIELD(jbyte, Byte, false);
1859 GET_TYPE_FIELD(jchar, Char, false);
1860 GET_TYPE_FIELD(jshort, Short, false);
1861 GET_TYPE_FIELD(jint, Int, false);
1862 GET_TYPE_FIELD(jlong, Long, false);
1863 GET_TYPE_FIELD(jfloat, Float, false);
1864 GET_TYPE_FIELD(jdouble, Double, false);
1865
1866 /*
1867  * Set an instance field.
1868  */
1869 #define SET_TYPE_FIELD(_ctype, _ctype2, _jname, _isref)                     \
1870     static void Set##_jname##Field(JNIEnv* env, jobject jobj,               \
1871         jfieldID fieldID, _ctype value)                                     \
1872     {                                                                       \
1873         ScopedJniThreadState ts(env);                                       \
1874         Object* obj = dvmDecodeIndirectRef(ts.self(), jobj); \
1875         InstField* field = (InstField*) fieldID;                            \
1876         if (dvmIsVolatileField(field)) {                                    \
1877             if (_isref) {   /* only when _ctype==jobject */                 \
1878                 Object* valObj = dvmDecodeIndirectRef(ts.self(), (jobject)(u4)value); \
1879                 dvmSetFieldObjectVolatile(obj, field->byteOffset, valObj);  \
1880             } else {                                                        \
1881                 dvmSetField##_jname##Volatile(obj,                          \
1882                     field->byteOffset, (_ctype2)value);                     \
1883             }                                                               \
1884         } else {                                                            \
1885             if (_isref) {                                                   \
1886                 Object* valObj = dvmDecodeIndirectRef(ts.self(), (jobject)(u4)value); \
1887                 dvmSetFieldObject(obj, field->byteOffset, valObj);          \
1888             } else {                                                        \
1889                 dvmSetField##_jname(obj,                                    \
1890                     field->byteOffset, (_ctype2)value);                     \
1891             }                                                               \
1892         }                                                                   \
1893     }
1894 SET_TYPE_FIELD(jobject, Object*, Object, true);
1895 SET_TYPE_FIELD(jboolean, bool, Boolean, false);
1896 SET_TYPE_FIELD(jbyte, s1, Byte, false);
1897 SET_TYPE_FIELD(jchar, u2, Char, false);
1898 SET_TYPE_FIELD(jshort, s2, Short, false);
1899 SET_TYPE_FIELD(jint, s4, Int, false);
1900 SET_TYPE_FIELD(jlong, s8, Long, false);
1901 SET_TYPE_FIELD(jfloat, float, Float, false);
1902 SET_TYPE_FIELD(jdouble, double, Double, false);
1903
1904 /*
1905  * Make a virtual method call.
1906  *
1907  * Three versions (..., va_list, jvalue[]) for each return type.  If we're
1908  * returning an Object, we have to add it to the local references table.
1909  */
1910 #define CALL_VIRTUAL(_ctype, _jname, _retfail, _retok, _isref)              \
1911     static _ctype Call##_jname##Method(JNIEnv* env, jobject jobj,           \
1912         jmethodID methodID, ...)                                            \
1913     {                                                                       \
1914         ScopedJniThreadState ts(env);                                       \
1915         Object* obj = dvmDecodeIndirectRef(ts.self(), jobj);                      \
1916         const Method* meth;                                                 \
1917         va_list args;                                                       \
1918         JValue result;                                                      \
1919         meth = dvmGetVirtualizedMethod(obj->clazz, (Method*)methodID);      \
1920         if (meth == NULL) {                                                 \
1921             return _retfail;                                                \
1922         }                                                                   \
1923         va_start(args, methodID);                                           \
1924         dvmCallMethodV(ts.self(), meth, obj, true, &result, args);          \
1925         va_end(args);                                                       \
1926         if (_isref && !dvmCheckException(ts.self()))                        \
1927             result.l = (Object*)addLocalReference(ts.self(), result.l);           \
1928         return _retok;                                                      \
1929     }                                                                       \
1930     static _ctype Call##_jname##MethodV(JNIEnv* env, jobject jobj,          \
1931         jmethodID methodID, va_list args)                                   \
1932     {                                                                       \
1933         ScopedJniThreadState ts(env);                                       \
1934         Object* obj = dvmDecodeIndirectRef(ts.self(), jobj);                      \
1935         const Method* meth;                                                 \
1936         JValue result;                                                      \
1937         meth = dvmGetVirtualizedMethod(obj->clazz, (Method*)methodID);      \
1938         if (meth == NULL) {                                                 \
1939             return _retfail;                                                \
1940         }                                                                   \
1941         dvmCallMethodV(ts.self(), meth, obj, true, &result, args);          \
1942         if (_isref && !dvmCheckException(ts.self()))                        \
1943             result.l = (Object*)addLocalReference(ts.self(), result.l);           \
1944         return _retok;                                                      \
1945     }                                                                       \
1946     static _ctype Call##_jname##MethodA(JNIEnv* env, jobject jobj,          \
1947         jmethodID methodID, jvalue* args)                                   \
1948     {                                                                       \
1949         ScopedJniThreadState ts(env);                                       \
1950         Object* obj = dvmDecodeIndirectRef(ts.self(), jobj);                      \
1951         const Method* meth;                                                 \
1952         JValue result;                                                      \
1953         meth = dvmGetVirtualizedMethod(obj->clazz, (Method*)methodID);      \
1954         if (meth == NULL) {                                                 \
1955             return _retfail;                                                \
1956         }                                                                   \
1957         dvmCallMethodA(ts.self(), meth, obj, true, &result, args);          \
1958         if (_isref && !dvmCheckException(ts.self()))                        \
1959             result.l = (Object*)addLocalReference(ts.self(), result.l);           \
1960         return _retok;                                                      \
1961     }
1962 CALL_VIRTUAL(jobject, Object, NULL, (jobject) result.l, true);
1963 CALL_VIRTUAL(jboolean, Boolean, 0, result.z, false);
1964 CALL_VIRTUAL(jbyte, Byte, 0, result.b, false);
1965 CALL_VIRTUAL(jchar, Char, 0, result.c, false);
1966 CALL_VIRTUAL(jshort, Short, 0, result.s, false);
1967 CALL_VIRTUAL(jint, Int, 0, result.i, false);
1968 CALL_VIRTUAL(jlong, Long, 0, result.j, false);
1969 CALL_VIRTUAL(jfloat, Float, 0.0f, result.f, false);
1970 CALL_VIRTUAL(jdouble, Double, 0.0, result.d, false);
1971 CALL_VIRTUAL(void, Void, , , false);
1972
1973 /*
1974  * Make a "non-virtual" method call.  We're still calling a virtual method,
1975  * but this time we're not doing an indirection through the object's vtable.
1976  * The "clazz" parameter defines which implementation of a method we want.
1977  *
1978  * Three versions (..., va_list, jvalue[]) for each return type.
1979  */
1980 #define CALL_NONVIRTUAL(_ctype, _jname, _retfail, _retok, _isref)           \
1981     static _ctype CallNonvirtual##_jname##Method(JNIEnv* env, jobject jobj, \
1982         jclass jclazz, jmethodID methodID, ...)                             \
1983     {                                                                       \
1984         ScopedJniThreadState ts(env);                                       \
1985         Object* obj = dvmDecodeIndirectRef(ts.self(), jobj);                      \
1986         ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz); \
1987         const Method* meth;                                                 \
1988         va_list args;                                                       \
1989         JValue result;                                                      \
1990         meth = dvmGetVirtualizedMethod(clazz, (Method*)methodID);           \
1991         if (meth == NULL) {                                                 \
1992             return _retfail;                                                \
1993         }                                                                   \
1994         va_start(args, methodID);                                           \
1995         dvmCallMethodV(ts.self(), meth, obj, true, &result, args);          \
1996         if (_isref && !dvmCheckException(ts.self()))                        \
1997             result.l = (Object*)addLocalReference(ts.self(), result.l);           \
1998         va_end(args);                                                       \
1999         return _retok;                                                      \
2000     }                                                                       \
2001     static _ctype CallNonvirtual##_jname##MethodV(JNIEnv* env, jobject jobj,\
2002         jclass jclazz, jmethodID methodID, va_list args)                    \
2003     {                                                                       \
2004         ScopedJniThreadState ts(env);                                       \
2005         Object* obj = dvmDecodeIndirectRef(ts.self(), jobj);                      \
2006         ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz); \
2007         const Method* meth;                                                 \
2008         JValue result;                                                      \
2009         meth = dvmGetVirtualizedMethod(clazz, (Method*)methodID);           \
2010         if (meth == NULL) {                                                 \
2011             return _retfail;                                                \
2012         }                                                                   \
2013         dvmCallMethodV(ts.self(), meth, obj, true, &result, args);          \
2014         if (_isref && !dvmCheckException(ts.self()))                        \
2015             result.l = (Object*)addLocalReference(ts.self(), result.l);           \
2016         return _retok;                                                      \
2017     }                                                                       \
2018     static _ctype CallNonvirtual##_jname##MethodA(JNIEnv* env, jobject jobj,\
2019         jclass jclazz, jmethodID methodID, jvalue* args)                    \
2020     {                                                                       \
2021         ScopedJniThreadState ts(env);                                       \
2022         Object* obj = dvmDecodeIndirectRef(ts.self(), jobj); \
2023         ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz); \
2024         const Method* meth;                                                 \
2025         JValue result;                                                      \
2026         meth = dvmGetVirtualizedMethod(clazz, (Method*)methodID);           \
2027         if (meth == NULL) {                                                 \
2028             return _retfail;                                                \
2029         }                                                                   \
2030         dvmCallMethodA(ts.self(), meth, obj, true, &result, args);          \
2031         if (_isref && !dvmCheckException(ts.self()))                        \
2032             result.l = (Object*)addLocalReference(ts.self(), result.l);           \
2033         return _retok;                                                      \
2034     }
2035 CALL_NONVIRTUAL(jobject, Object, NULL, (jobject) result.l, true);
2036 CALL_NONVIRTUAL(jboolean, Boolean, 0, result.z, false);
2037 CALL_NONVIRTUAL(jbyte, Byte, 0, result.b, false);
2038 CALL_NONVIRTUAL(jchar, Char, 0, result.c, false);
2039 CALL_NONVIRTUAL(jshort, Short, 0, result.s, false);
2040 CALL_NONVIRTUAL(jint, Int, 0, result.i, false);
2041 CALL_NONVIRTUAL(jlong, Long, 0, result.j, false);
2042 CALL_NONVIRTUAL(jfloat, Float, 0.0f, result.f, false);
2043 CALL_NONVIRTUAL(jdouble, Double, 0.0, result.d, false);
2044 CALL_NONVIRTUAL(void, Void, , , false);
2045
2046
2047 /*
2048  * Call a static method.
2049  */
2050 #define CALL_STATIC(_ctype, _jname, _retfail, _retok, _isref)               \
2051     static _ctype CallStatic##_jname##Method(JNIEnv* env, jclass jclazz,    \
2052         jmethodID methodID, ...)                                            \
2053     {                                                                       \
2054         UNUSED_PARAMETER(jclazz);                                           \
2055         ScopedJniThreadState ts(env);                                       \
2056         JValue result;                                                      \
2057         va_list args;                                                       \
2058         va_start(args, methodID);                                           \
2059         dvmCallMethodV(ts.self(), (Method*)methodID, NULL, true, &result, args);\
2060         va_end(args);                                                       \
2061         if (_isref && !dvmCheckException(ts.self()))                        \
2062             result.l = (Object*)addLocalReference(ts.self(), result.l);           \
2063         return _retok;                                                      \
2064     }                                                                       \
2065     static _ctype CallStatic##_jname##MethodV(JNIEnv* env, jclass jclazz,   \
2066         jmethodID methodID, va_list args)                                   \
2067     {                                                                       \
2068         UNUSED_PARAMETER(jclazz);                                           \
2069         ScopedJniThreadState ts(env);                                       \
2070         JValue result;                                                      \
2071         dvmCallMethodV(ts.self(), (Method*)methodID, NULL, true, &result, args);\
2072         if (_isref && !dvmCheckException(ts.self()))                        \
2073             result.l = (Object*)addLocalReference(ts.self(), result.l);           \
2074         return _retok;                                                      \
2075     }                                                                       \
2076     static _ctype CallStatic##_jname##MethodA(JNIEnv* env, jclass jclazz,   \
2077         jmethodID methodID, jvalue* args)                                   \
2078     {                                                                       \
2079         UNUSED_PARAMETER(jclazz);                                           \
2080         ScopedJniThreadState ts(env);                                       \
2081         JValue result;                                                      \
2082         dvmCallMethodA(ts.self(), (Method*)methodID, NULL, true, &result, args);\
2083         if (_isref && !dvmCheckException(ts.self()))                        \
2084             result.l = (Object*)addLocalReference(ts.self(), result.l);           \
2085         return _retok;                                                      \
2086     }
2087 CALL_STATIC(jobject, Object, NULL, (jobject) result.l, true);
2088 CALL_STATIC(jboolean, Boolean, 0, result.z, false);
2089 CALL_STATIC(jbyte, Byte, 0, result.b, false);
2090 CALL_STATIC(jchar, Char, 0, result.c, false);
2091 CALL_STATIC(jshort, Short, 0, result.s, false);
2092 CALL_STATIC(jint, Int, 0, result.i, false);
2093 CALL_STATIC(jlong, Long, 0, result.j, false);
2094 CALL_STATIC(jfloat, Float, 0.0f, result.f, false);
2095 CALL_STATIC(jdouble, Double, 0.0, result.d, false);
2096 CALL_STATIC(void, Void, , , false);
2097
2098 /*
2099  * Create a new String from Unicode data.
2100  *
2101  * If "len" is zero, we will return an empty string even if "unicodeChars"
2102  * is NULL.  (The JNI spec is vague here.)
2103  */
2104 static jstring NewString(JNIEnv* env, const jchar* unicodeChars, jsize len) {
2105     ScopedJniThreadState ts(env);
2106     StringObject* jstr = dvmCreateStringFromUnicode(unicodeChars, len);
2107     if (jstr == NULL) {
2108         return NULL;
2109     }
2110     dvmReleaseTrackedAlloc((Object*) jstr, NULL);
2111     return (jstring) addLocalReference(ts.self(), (Object*) jstr);
2112 }
2113
2114 /*
2115  * Return the length of a String in Unicode character units.
2116  */
2117 static jsize GetStringLength(JNIEnv* env, jstring jstr) {
2118     ScopedJniThreadState ts(env);
2119     StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(ts.self(), jstr);
2120     return strObj->length();
2121 }
2122
2123
2124 /*
2125  * Get a string's character data.
2126  *
2127  * The result is guaranteed to be valid until ReleaseStringChars is
2128  * called, which means we have to pin it or return a copy.
2129  */
2130 static const jchar* GetStringChars(JNIEnv* env, jstring jstr, jboolean* isCopy) {
2131     ScopedJniThreadState ts(env);
2132
2133     StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(ts.self(), jstr);
2134     ArrayObject* strChars = strObj->array();
2135
2136     pinPrimitiveArray(strChars);
2137
2138     const u2* data = strObj->chars();
2139     if (isCopy != NULL) {
2140         *isCopy = JNI_FALSE;
2141     }
2142     return (jchar*) data;
2143 }
2144
2145 /*
2146  * Release our grip on some characters from a string.
2147  */
2148 static void ReleaseStringChars(JNIEnv* env, jstring jstr, const jchar* chars) {
2149     ScopedJniThreadState ts(env);
2150     StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(ts.self(), jstr);
2151     ArrayObject* strChars = strObj->array();
2152     unpinPrimitiveArray(strChars);
2153 }
2154
2155 /*
2156  * Create a new java.lang.String object from chars in modified UTF-8 form.
2157  *
2158  * The spec doesn't say how to handle a NULL string.  Popular desktop VMs
2159  * accept it and return a NULL pointer in response.
2160  */
2161 static jstring NewStringUTF(JNIEnv* env, const char* bytes) {
2162     ScopedJniThreadState ts(env);
2163     if (bytes == NULL) {
2164         return NULL;
2165     }
2166     /* note newStr could come back NULL on OOM */
2167     StringObject* newStr = dvmCreateStringFromCstr(bytes);
2168     jstring result = (jstring) addLocalReference(ts.self(), (Object*) newStr);
2169     dvmReleaseTrackedAlloc((Object*)newStr, NULL);
2170     return result;
2171 }
2172
2173 /*
2174  * Return the length in bytes of the modified UTF-8 form of the string.
2175  */
2176 static jsize GetStringUTFLength(JNIEnv* env, jstring jstr) {
2177     ScopedJniThreadState ts(env);
2178     StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(ts.self(), jstr);
2179     if (strObj == NULL) {
2180         return 0; // Should we throw something or assert?
2181     }
2182     return strObj->utfLength();
2183 }
2184
2185 /*
2186  * Convert "string" to modified UTF-8 and return a pointer.  The returned
2187  * value must be released with ReleaseStringUTFChars.
2188  *
2189  * According to the JNI reference, "Returns a pointer to a UTF-8 string,
2190  * or NULL if the operation fails. Returns NULL if and only if an invocation
2191  * of this function has thrown an exception."
2192  *
2193  * The behavior here currently follows that of other open-source VMs, which
2194  * quietly return NULL if "string" is NULL.  We should consider throwing an
2195  * NPE.  (The CheckJNI code blows up if you try to pass in a NULL string,
2196  * which should catch this sort of thing during development.)  Certain other
2197  * VMs will crash with a segmentation fault.
2198  */
2199 static const char* GetStringUTFChars(JNIEnv* env, jstring jstr, jboolean* isCopy) {
2200     ScopedJniThreadState ts(env);
2201     if (jstr == NULL) {
2202         /* this shouldn't happen; throw NPE? */
2203         return NULL;
2204     }
2205     if (isCopy != NULL) {
2206         *isCopy = JNI_TRUE;
2207     }
2208     StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(ts.self(), jstr);
2209     char* newStr = dvmCreateCstrFromString(strObj);
2210     if (newStr == NULL) {
2211         /* assume memory failure */
2212         dvmThrowOutOfMemoryError("native heap string alloc failed");
2213     }
2214     return newStr;
2215 }
2216
2217 /*
2218  * Release a string created by GetStringUTFChars().
2219  */
2220 static void ReleaseStringUTFChars(JNIEnv* env, jstring jstr, const char* utf) {
2221     ScopedJniThreadState ts(env);
2222     free((char*) utf);
2223 }
2224
2225 /*
2226  * Return the capacity of the array.
2227  */
2228 static jsize GetArrayLength(JNIEnv* env, jarray jarr) {
2229     ScopedJniThreadState ts(env);
2230     ArrayObject* arrObj = (ArrayObject*) dvmDecodeIndirectRef(ts.self(), jarr);
2231     return arrObj->length;
2232 }
2233
2234 /*
2235  * Construct a new array that holds objects from class "elementClass".
2236  */
2237 static jobjectArray NewObjectArray(JNIEnv* env, jsize length,
2238     jclass jelementClass, jobject jinitialElement)
2239 {
2240     ScopedJniThreadState ts(env);
2241
2242     if (jelementClass == NULL) {
2243         dvmThrowNullPointerException("JNI NewObjectArray elementClass == NULL");
2244         return NULL;
2245     }
2246
2247     ClassObject* elemClassObj = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jelementClass);
2248     ClassObject* arrayClass = dvmFindArrayClassForElement(elemClassObj);
2249     ArrayObject* newObj = dvmAllocArrayByClass(arrayClass, length, ALLOC_DEFAULT);
2250     if (newObj == NULL) {
2251         assert(dvmCheckException(ts.self()));
2252         return NULL;
2253     }
2254     jobjectArray newArray = (jobjectArray) addLocalReference(ts.self(), (Object*) newObj);
2255     dvmReleaseTrackedAlloc((Object*) newObj, NULL);
2256
2257     /*
2258      * Initialize the array.
2259      */
2260     if (jinitialElement != NULL) {
2261         Object* initialElement = dvmDecodeIndirectRef(ts.self(), jinitialElement);
2262         Object** arrayData = (Object**) (void*) newObj->contents;
2263         for (jsize i = 0; i < length; ++i) {
2264             arrayData[i] = initialElement;
2265         }
2266     }
2267
2268     return newArray;
2269 }
2270
2271 static bool checkArrayElementBounds(ArrayObject* arrayObj, jsize index) {
2272     assert(arrayObj != NULL);
2273     if (index < 0 || index >= (int) arrayObj->length) {
2274         dvmThrowArrayIndexOutOfBoundsException(arrayObj->length, index);
2275         return false;
2276     }
2277     return true;
2278 }
2279
2280 /*
2281  * Get one element of an Object array.
2282  *
2283  * Add the object to the local references table in case the array goes away.
2284  */
2285 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray jarr, jsize index) {
2286     ScopedJniThreadState ts(env);
2287
2288     ArrayObject* arrayObj = (ArrayObject*) dvmDecodeIndirectRef(ts.self(), jarr);
2289     if (!checkArrayElementBounds(arrayObj, index)) {
2290         return NULL;
2291     }
2292
2293     Object* value = ((Object**) (void*) arrayObj->contents)[index];
2294     return addLocalReference(ts.self(), value);
2295 }
2296
2297 /*
2298  * Set one element of an Object array.
2299  */
2300 static void SetObjectArrayElement(JNIEnv* env, jobjectArray jarr, jsize index, jobject jobj) {
2301     ScopedJniThreadState ts(env);
2302
2303     ArrayObject* arrayObj = (ArrayObject*) dvmDecodeIndirectRef(ts.self(), jarr);
2304     if (!checkArrayElementBounds(arrayObj, index)) {
2305         return;
2306     }
2307
2308     Object* obj = dvmDecodeIndirectRef(ts.self(), jobj);
2309
2310     if (obj != NULL && !dvmCanPutArrayElement(obj->clazz, arrayObj->clazz)) {
2311       ALOGV("Can't put a '%s'(%p) into array type='%s'(%p)",
2312             obj->clazz->descriptor, obj,
2313             arrayObj->clazz->descriptor, arrayObj);
2314       dvmThrowArrayStoreExceptionIncompatibleElement(obj->clazz, arrayObj->clazz);
2315       return;
2316     }
2317
2318     //ALOGV("JNI: set element %d in array %p to %p", index, array, value);
2319
2320     dvmSetObjectArrayElement(arrayObj, index, obj);
2321 }
2322
2323 /*
2324  * Create a new array of primitive elements.
2325  */
2326 #define NEW_PRIMITIVE_ARRAY(_artype, _jname, _typechar) \
2327     static _artype New##_jname##Array(JNIEnv* env, jsize length) { \
2328         ScopedJniThreadState ts(env); \
2329         ArrayObject* arrayObj = dvmAllocPrimitiveArray(_typechar, length, ALLOC_DEFAULT); \
2330         if (arrayObj == NULL) { \
2331             return NULL; \
2332         } \
2333         _artype result = (_artype) addLocalReference(ts.self(), (Object*) arrayObj); \
2334         dvmReleaseTrackedAlloc((Object*) arrayObj, NULL); \
2335         return result; \
2336     }
2337 NEW_PRIMITIVE_ARRAY(jbooleanArray, Boolean, 'Z');
2338 NEW_PRIMITIVE_ARRAY(jbyteArray, Byte, 'B');
2339 NEW_PRIMITIVE_ARRAY(jcharArray, Char, 'C');
2340 NEW_PRIMITIVE_ARRAY(jshortArray, Short, 'S');
2341 NEW_PRIMITIVE_ARRAY(jintArray, Int, 'I');
2342 NEW_PRIMITIVE_ARRAY(jlongArray, Long, 'J');
2343 NEW_PRIMITIVE_ARRAY(jfloatArray, Float, 'F');
2344 NEW_PRIMITIVE_ARRAY(jdoubleArray, Double, 'D');
2345
2346 /*
2347  * Get a pointer to a C array of primitive elements from an array object
2348  * of the matching type.
2349  *
2350  * In a compacting GC, we either need to return a copy of the elements or
2351  * "pin" the memory.  Otherwise we run the risk of native code using the
2352  * buffer as the destination of e.g. a blocking read() call that wakes up
2353  * during a GC.
2354  */
2355 #define GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
2356     static _ctype* Get##_jname##ArrayElements(JNIEnv* env, \
2357         _ctype##Array jarr, jboolean* isCopy) \
2358     { \
2359         ScopedJniThreadState ts(env); \
2360         ArrayObject* arrayObj = (ArrayObject*) dvmDecodeIndirectRef(ts.self(), jarr); \
2361         pinPrimitiveArray(arrayObj); \
2362         _ctype* data = (_ctype*) (void*) arrayObj->contents; \
2363         if (isCopy != NULL) { \
2364             *isCopy = JNI_FALSE; \
2365         } \
2366         return data; \
2367     }
2368
2369 /*
2370  * Release the storage locked down by the "get" function.
2371  *
2372  * The spec says, "'mode' has no effect if 'elems' is not a copy of the
2373  * elements in 'array'."  They apparently did not anticipate the need to
2374  * un-pin memory.
2375  */
2376 #define RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname)                    \
2377     static void Release##_jname##ArrayElements(JNIEnv* env,                 \
2378         _ctype##Array jarr, _ctype* elems, jint mode)                       \
2379     {                                                                       \
2380         UNUSED_PARAMETER(elems);                                            \
2381         if (mode != JNI_COMMIT) {                                           \
2382             ScopedJniThreadState ts(env);                                   \
2383             ArrayObject* arrayObj = (ArrayObject*) dvmDecodeIndirectRef(ts.self(), jarr); \
2384             unpinPrimitiveArray(arrayObj);                                  \
2385         }                                                                   \
2386     }
2387
2388 static void throwArrayRegionOutOfBounds(ArrayObject* arrayObj, jsize start,
2389     jsize len, const char* arrayIdentifier)
2390 {
2391     dvmThrowExceptionFmt(gDvm.exArrayIndexOutOfBoundsException,
2392         "%s offset=%d length=%d %s.length=%d",
2393         arrayObj->clazz->descriptor, start, len, arrayIdentifier,
2394         arrayObj->length);
2395 }
2396
2397 /*
2398  * Copy a section of a primitive array to a buffer.
2399  */
2400 #define GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
2401     static void Get##_jname##ArrayRegion(JNIEnv* env, \
2402         _ctype##Array jarr, jsize start, jsize len, _ctype* buf) \
2403     { \
2404         ScopedJniThreadState ts(env); \
2405         ArrayObject* arrayObj = (ArrayObject*) dvmDecodeIndirectRef(ts.self(), jarr); \
2406         _ctype* data = (_ctype*) (void*) arrayObj->contents; \
2407         if (start < 0 || len < 0 || start + len > (int) arrayObj->length) { \
2408             throwArrayRegionOutOfBounds(arrayObj, start, len, "src"); \
2409         } else { \
2410             memcpy(buf, data + start, len * sizeof(_ctype)); \
2411         } \
2412     }
2413
2414 /*
2415  * Copy a section of a primitive array from a buffer.
2416  */
2417 #define SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
2418     static void Set##_jname##ArrayRegion(JNIEnv* env, \
2419         _ctype##Array jarr, jsize start, jsize len, const _ctype* buf) \
2420     { \
2421         ScopedJniThreadState ts(env); \
2422         ArrayObject* arrayObj = (ArrayObject*) dvmDecodeIndirectRef(ts.self(), jarr); \
2423         _ctype* data = (_ctype*) (void*) arrayObj->contents; \
2424         if (start < 0 || len < 0 || start + len > (int) arrayObj->length) { \
2425             throwArrayRegionOutOfBounds(arrayObj, start, len, "dst"); \
2426         } else { \
2427             memcpy(data + start, buf, len * sizeof(_ctype)); \
2428         } \
2429     }
2430
2431 /*
2432  * 4-in-1:
2433  *  Get<Type>ArrayElements
2434  *  Release<Type>ArrayElements
2435  *  Get<Type>ArrayRegion
2436  *  Set<Type>ArrayRegion
2437  */
2438 #define PRIMITIVE_ARRAY_FUNCTIONS(_ctype, _jname)                           \
2439     GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname);                           \
2440     RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname);                       \
2441     GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname);                             \
2442     SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname);
2443
2444 PRIMITIVE_ARRAY_FUNCTIONS(jboolean, Boolean);
2445 PRIMITIVE_ARRAY_FUNCTIONS(jbyte, Byte);
2446 PRIMITIVE_ARRAY_FUNCTIONS(jchar, Char);
2447 PRIMITIVE_ARRAY_FUNCTIONS(jshort, Short);
2448 PRIMITIVE_ARRAY_FUNCTIONS(jint, Int);
2449 PRIMITIVE_ARRAY_FUNCTIONS(jlong, Long);
2450 PRIMITIVE_ARRAY_FUNCTIONS(jfloat, Float);
2451 PRIMITIVE_ARRAY_FUNCTIONS(jdouble, Double);
2452
2453 /*
2454  * Register one or more native functions in one class.
2455  *
2456  * This can be called multiple times on the same method, allowing the
2457  * caller to redefine the method implementation at will.
2458  */
2459 static jint RegisterNatives(JNIEnv* env, jclass jclazz,
2460     const JNINativeMethod* methods, jint nMethods)
2461 {
2462     ScopedJniThreadState ts(env);
2463
2464     ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz);
2465
2466     if (gDvm.verboseJni) {
2467         ALOGI("[Registering JNI native methods for class %s]",
2468             clazz->descriptor);
2469     }
2470
2471     for (int i = 0; i < nMethods; i++) {
2472         if (!dvmRegisterJNIMethod(clazz, methods[i].name,
2473                 methods[i].signature, methods[i].fnPtr))
2474         {
2475             return JNI_ERR;
2476         }
2477     }
2478     return JNI_OK;
2479 }
2480
2481 /*
2482  * Un-register all native methods associated with the class.
2483  *
2484  * The JNI docs refer to this as a way to reload/relink native libraries,
2485  * and say it "should not be used in normal native code".  In particular,
2486  * there is no need to do this during shutdown, and you do not need to do
2487  * this before redefining a method implementation with RegisterNatives.
2488  *
2489  * It's chiefly useful for a native "plugin"-style library that wasn't
2490  * loaded with System.loadLibrary() (since there's no way to unload those).
2491  * For example, the library could upgrade itself by:
2492  *
2493  *  1. call UnregisterNatives to unbind the old methods
2494  *  2. ensure that no code is still executing inside it (somehow)
2495  *  3. dlclose() the library
2496  *  4. dlopen() the new library
2497  *  5. use RegisterNatives to bind the methods from the new library
2498  *
2499  * The above can work correctly without the UnregisterNatives call, but
2500  * creates a window of opportunity in which somebody might try to call a
2501  * method that is pointing at unmapped memory, crashing the VM.  In theory
2502  * the same guards that prevent dlclose() from unmapping executing code could
2503  * prevent that anyway, but with this we can be more thorough and also deal
2504  * with methods that only exist in the old or new form of the library (maybe
2505  * the lib wants to try the call and catch the UnsatisfiedLinkError).
2506  */
2507 static jint UnregisterNatives(JNIEnv* env, jclass jclazz) {
2508     ScopedJniThreadState ts(env);
2509
2510     ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(ts.self(), jclazz);
2511     if (gDvm.verboseJni) {
2512         ALOGI("[Unregistering JNI native methods for class %s]",
2513             clazz->descriptor);
2514     }
2515     dvmUnregisterJNINativeMethods(clazz);
2516     return JNI_OK;
2517 }
2518
2519 /*
2520  * Lock the monitor.
2521  *
2522  * We have to track all monitor enters and exits, so that we can undo any
2523  * outstanding synchronization before the thread exits.
2524  */
2525 static jint MonitorEnter(JNIEnv* env, jobject jobj) {
2526     ScopedJniThreadState ts(env);
2527     Object* obj = dvmDecodeIndirectRef(ts.self(), jobj);
2528     dvmLockObject(ts.self(), obj);
2529     trackMonitorEnter(ts.self(), obj);
2530     return JNI_OK;
2531 }
2532
2533 /*
2534  * Unlock the monitor.
2535  *
2536  * Throws an IllegalMonitorStateException if the current thread
2537  * doesn't own the monitor.  (dvmUnlockObject() takes care of the throw.)
2538  *
2539  * According to the 1.6 spec, it's legal to call here with an exception
2540  * pending.  If this fails, we'll stomp the original exception.
2541  */
2542 static jint MonitorExit(JNIEnv* env, jobject jobj) {
2543     ScopedJniThreadState ts(env);
2544     Object* obj = dvmDecodeIndirectRef(ts.self(), jobj);
2545     bool success = dvmUnlockObject(ts.self(), obj);
2546     if (success) {
2547         trackMonitorExit(ts.self(), obj);
2548     }
2549     return success ? JNI_OK : JNI_ERR;
2550 }
2551
2552 /*
2553  * Return the JavaVM interface associated with the current thread.
2554  */
2555 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2556     ScopedJniThreadState ts(env);
2557     *vm = gDvmJni.jniVm;
2558     return (*vm == NULL) ? JNI_ERR : JNI_OK;
2559 }
2560
2561 /*
2562  * Copies "len" Unicode characters, from offset "start".
2563  */
2564 static void GetStringRegion(JNIEnv* env, jstring jstr, jsize start, jsize len, jchar* buf) {
2565     ScopedJniThreadState ts(env);
2566     StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(ts.self(), jstr);
2567     int strLen = strObj->length();
2568     if (((start|len) < 0) || (start + len > strLen)) {
2569         dvmThrowStringIndexOutOfBoundsExceptionWithRegion(strLen, start, len);
2570         return;
2571     }
2572     memcpy(buf, strObj->chars() + start, len * sizeof(u2));
2573 }
2574
2575 /*
2576  * Translates "len" Unicode characters, from offset "start", into
2577  * modified UTF-8 encoding.
2578  */
2579 static void GetStringUTFRegion(JNIEnv* env, jstring jstr, jsize start, jsize len, char* buf) {
2580     ScopedJniThreadState ts(env);
2581     StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(ts.self(), jstr);
2582     int strLen = strObj->length();
2583     if (((start|len) < 0) || (start + len > strLen)) {
2584         dvmThrowStringIndexOutOfBoundsExceptionWithRegion(strLen, start, len);
2585         return;
2586     }
2587     dvmGetStringUtfRegion(strObj, start, len, buf);
2588 }
2589
2590 /*
2591  * Get a raw pointer to array data.
2592  *
2593  * The caller is expected to call "release" before doing any JNI calls
2594  * or blocking I/O operations.
2595  *
2596  * We need to pin the memory or block GC.
2597  */
2598 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray jarr, jboolean* isCopy) {
2599     ScopedJniThreadState ts(env);
2600     ArrayObject* arrayObj = (ArrayObject*) dvmDecodeIndirectRef(ts.self(), jarr);
2601     pinPrimitiveArray(arrayObj);
2602     void* data = arrayObj->contents;
2603     if (UNLIKELY(isCopy != NULL)) {
2604         *isCopy = JNI_FALSE;
2605     }
2606     return data;
2607 }
2608
2609 /*
2610  * Release an array obtained with GetPrimitiveArrayCritical.
2611  */
2612 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray jarr, void* carray, jint mode) {
2613     if (mode != JNI_COMMIT) {
2614         ScopedJniThreadState ts(env);
2615         ArrayObject* arrayObj = (ArrayObject*) dvmDecodeIndirectRef(ts.self(), jarr);
2616         unpinPrimitiveArray(arrayObj);
2617     }
2618 }
2619
2620 /*
2621  * Like GetStringChars, but with restricted use.
2622  */
2623 static const jchar* GetStringCritical(JNIEnv* env, jstring jstr, jboolean* isCopy) {
2624     ScopedJniThreadState ts(env);
2625
2626     StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(ts.self(), jstr);
2627     ArrayObject* strChars = strObj->array();
2628
2629     pinPrimitiveArray(strChars);
2630
2631     const u2* data = strObj->chars();
2632     if (isCopy != NULL) {
2633         *isCopy = JNI_FALSE;
2634     }
2635     return (jchar*) data;
2636 }
2637
2638 /*
2639  * Like ReleaseStringChars, but with restricted use.
2640  */
2641 static void ReleaseStringCritical(JNIEnv* env, jstring jstr, const jchar* carray) {
2642     ScopedJniThreadState ts(env);
2643     StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(ts.self(), jstr);
2644     ArrayObject* strChars = strObj->array();
2645     unpinPrimitiveArray(strChars);
2646 }
2647
2648 /*
2649  * Create a new weak global reference.
2650  */
2651 static jweak NewWeakGlobalRef(JNIEnv* env, jobject jobj) {
2652     ScopedJniThreadState ts(env);
2653     Object *obj = dvmDecodeIndirectRef(ts.self(), jobj);
2654     return (jweak) addWeakGlobalReference(obj);
2655 }
2656
2657 /*
2658  * Delete the specified weak global reference.
2659  */
2660 static void DeleteWeakGlobalRef(JNIEnv* env, jweak wref) {
2661     ScopedJniThreadState ts(env);
2662     deleteWeakGlobalReference(wref);
2663 }
2664
2665 /*
2666  * Quick check for pending exceptions.
2667  *
2668  * TODO: we should be able to skip the enter/exit macros here.
2669  */
2670 static jboolean ExceptionCheck(JNIEnv* env) {
2671     ScopedJniThreadState ts(env);
2672     return dvmCheckException(ts.self());
2673 }
2674
2675 /*
2676  * Returns the type of the object referred to by "obj".  It can be local,
2677  * global, or weak global.
2678  *
2679  * In the current implementation, references can be global and local at
2680  * the same time, so while the return value is accurate it may not tell
2681  * the whole story.
2682  */
2683 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject jobj) {
2684     ScopedJniThreadState ts(env);
2685     return dvmGetJNIRefType(ts.self(), jobj);
2686 }
2687
2688 /*
2689  * Allocate and return a new java.nio.ByteBuffer for this block of memory.
2690  */
2691 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2692     ScopedJniThreadState ts(env);
2693
2694     if (capacity < 0) {
2695         ALOGE("JNI ERROR (app bug): negative buffer capacity: %lld", capacity);
2696         ReportJniError();
2697     }
2698     if (address == NULL && capacity != 0) {
2699         ALOGE("JNI ERROR (app bug): non-zero capacity for NULL pointer: %lld", capacity);
2700         ReportJniError();
2701     }
2702
2703     /* create an instance of java.nio.ReadWriteDirectByteBuffer */
2704     ClassObject* bufferClazz = gDvm.classJavaNioReadWriteDirectByteBuffer;
2705     if (!dvmIsClassInitialized(bufferClazz) && !dvmInitClass(bufferClazz)) {
2706         return NULL;
2707     }
2708     Object* newObj = dvmAllocObject(bufferClazz, ALLOC_DONT_TRACK);
2709     if (newObj == NULL) {
2710         return NULL;
2711     }
2712     /* call the constructor */
2713     jobject result = addLocalReference(ts.self(), newObj);
2714     JValue unused;
2715     dvmCallMethod(ts.self(), gDvm.methJavaNioReadWriteDirectByteBuffer_init,
2716             newObj, &unused, (jint) address, (jint) capacity);
2717     if (dvmGetException(ts.self()) != NULL) {
2718         deleteLocalReference(ts.self(), result);
2719         return NULL;
2720     }
2721     return result;
2722 }
2723
2724 /*
2725  * Get the starting address of the buffer for the specified java.nio.Buffer.
2726  *
2727  * If this is not a "direct" buffer, we return NULL.
2728  */
2729 static void* GetDirectBufferAddress(JNIEnv* env, jobject jbuf) {
2730     ScopedJniThreadState ts(env);
2731
2732     // All Buffer objects have an effectiveDirectAddress field.
2733     Object* bufObj = dvmDecodeIndirectRef(ts.self(), jbuf);
2734     return (void*) dvmGetFieldInt(bufObj, gDvm.offJavaNioBuffer_effectiveDirectAddress);
2735 }
2736
2737 /*
2738  * Get the capacity of the buffer for the specified java.nio.Buffer.
2739  *
2740  * Returns -1 if the object is not a direct buffer.  (We actually skip
2741  * this check, since it's expensive to determine, and just return the
2742  * capacity regardless.)
2743  */
2744 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject jbuf) {
2745     ScopedJniThreadState ts(env);
2746
2747     /*
2748      * The capacity is always in the Buffer.capacity field.
2749      *
2750      * (The "check" version should verify that this is actually a Buffer,
2751      * but we're not required to do so here.)
2752      */
2753     Object* buf = dvmDecodeIndirectRef(ts.self(), jbuf);
2754     return dvmGetFieldInt(buf, gDvm.offJavaNioBuffer_capacity);
2755 }
2756
2757
2758 /*
2759  * ===========================================================================
2760  *      JNI invocation functions
2761  * ===========================================================================
2762  */
2763
2764 /*
2765  * Handle AttachCurrentThread{AsDaemon}.
2766  *
2767  * We need to make sure the VM is actually running.  For example, if we start
2768  * up, issue an Attach, and the VM exits almost immediately, by the time the
2769  * attaching happens the VM could already be shutting down.
2770  *
2771  * It's hard to avoid a race condition here because we don't want to hold
2772  * a lock across the entire operation.  What we can do is temporarily
2773  * increment the thread count to prevent a VM exit.
2774  *
2775  * This could potentially still have problems if a daemon thread calls here
2776  * while the VM is shutting down.  dvmThreadSelf() will work, since it just
2777  * uses pthread TLS, but dereferencing "vm" could fail.  Such is life when
2778  * you shut down a VM while threads are still running inside it.
2779  *
2780  * Remember that some code may call this as a way to find the per-thread
2781  * JNIEnv pointer.  Don't do excess work for that case.
2782  */
2783 static jint attachThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool isDaemon) {
2784     JavaVMAttachArgs* args = (JavaVMAttachArgs*) thr_args;
2785
2786     /*
2787      * Return immediately if we're already one with the VM.
2788      */
2789     Thread* self = dvmThreadSelf();
2790     if (self != NULL) {
2791         *p_env = self->jniEnv;
2792         return JNI_OK;
2793     }
2794
2795     /*
2796      * No threads allowed in zygote mode.
2797      */
2798     if (gDvm.zygote) {
2799         return JNI_ERR;
2800     }
2801
2802     /* increment the count to keep the VM from bailing while we run */
2803     dvmLockThreadList(NULL);
2804     if (gDvm.nonDaemonThreadCount == 0) {
2805         // dead or dying
2806         ALOGV("Refusing to attach thread '%s' -- VM is shutting down",
2807             (thr_args == NULL) ? "(unknown)" : args->name);
2808         dvmUnlockThreadList();
2809         return JNI_ERR;
2810     }
2811     gDvm.nonDaemonThreadCount++;
2812     dvmUnlockThreadList();
2813
2814     /* tweak the JavaVMAttachArgs as needed */
2815     JavaVMAttachArgs argsCopy;
2816     if (args == NULL) {
2817         /* allow the v1.1 calling convention */
2818         argsCopy.version = JNI_VERSION_1_2;
2819         argsCopy.name = NULL;
2820         argsCopy.group = (jobject) dvmGetMainThreadGroup();
2821     } else {
2822         assert(args->version >= JNI_VERSION_1_2);
2823
2824         argsCopy.version = args->version;
2825         argsCopy.name = args->name;
2826         if (args->group != NULL) {
2827             argsCopy.group = (jobject) dvmDecodeIndirectRef(NULL, args->group);
2828         } else {
2829             argsCopy.group = (jobject) dvmGetMainThreadGroup();
2830         }
2831     }
2832
2833     bool result = dvmAttachCurrentThread(&argsCopy, isDaemon);
2834
2835     /* restore the count */
2836     dvmLockThreadList(NULL);
2837     gDvm.nonDaemonThreadCount--;
2838     dvmUnlockThreadList();
2839
2840     /*
2841      * Change the status to indicate that we're out in native code.  This
2842      * call is not guarded with state-change macros, so we have to do it
2843      * by hand.
2844      */
2845     if (result) {
2846         self = dvmThreadSelf();
2847         assert(self != NULL);
2848         dvmChangeStatus(self, THREAD_NATIVE);
2849         *p_env = self->jniEnv;
2850         return JNI_OK;
2851     } else {
2852         return JNI_ERR;
2853     }
2854 }
2855
2856 /*
2857  * Attach the current thread to the VM.  If the thread is already attached,
2858  * this is a no-op.
2859  */
2860 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
2861     return attachThread(vm, p_env, thr_args, false);
2862 }
2863
2864 /*
2865  * Like AttachCurrentThread, but set the "daemon" flag.
2866  */
2867 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args)
2868 {
2869     return attachThread(vm, p_env, thr_args, true);
2870 }
2871
2872 /*
2873  * Dissociate the current thread from the VM.
2874  */
2875 static jint DetachCurrentThread(JavaVM* vm) {
2876     Thread* self = dvmThreadSelf();
2877     if (self == NULL) {
2878         /* not attached, can't do anything */
2879         return JNI_ERR;
2880     }
2881
2882     /* switch to "running" to check for suspension */
2883     dvmChangeStatus(self, THREAD_RUNNING);
2884
2885     /* detach the thread */
2886     dvmDetachCurrentThread();
2887
2888     /* (no need to change status back -- we have no status) */
2889     return JNI_OK;
2890 }
2891
2892 /*
2893  * If current thread is attached to VM, return the associated JNIEnv.
2894  * Otherwise, stuff NULL in and return JNI_EDETACHED.
2895  *
2896  * JVMTI overloads this by specifying a magic value for "version", so we
2897  * do want to check that here.
2898  */
2899 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2900     Thread* self = dvmThreadSelf();
2901
2902     if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2903         return JNI_EVERSION;
2904     }
2905
2906     if (self == NULL) {
2907         *env = NULL;
2908     } else {
2909         /* TODO: status change is probably unnecessary */
2910         dvmChangeStatus(self, THREAD_RUNNING);
2911         *env = (void*) dvmGetThreadJNIEnv(self);
2912         dvmChangeStatus(self, THREAD_NATIVE);
2913     }
2914     return (*env != NULL) ? JNI_OK : JNI_EDETACHED;
2915 }
2916
2917 /*
2918  * Destroy the VM.  This may be called from any thread.
2919  *
2920  * If the current thread is attached, wait until the current thread is
2921  * the only non-daemon user-level thread.  If the current thread is not
2922  * attached, we attach it and do the processing as usual.  (If the attach
2923  * fails, it's probably because all the non-daemon threads have already
2924  * exited and the VM doesn't want to let us back in.)
2925  *
2926  * TODO: we don't really deal with the situation where more than one thread
2927  * has called here.  One thread wins, the other stays trapped waiting on
2928  * the condition variable forever.  Not sure this situation is interesting
2929  * in real life.
2930  */
2931 static jint DestroyJavaVM(JavaVM* vm) {
2932     JavaVMExt* ext = (JavaVMExt*) vm;
2933     if (ext == NULL) {
2934         return JNI_ERR;
2935     }
2936
2937     if (gDvm.verboseShutdown) {
2938         ALOGD("DestroyJavaVM waiting for non-daemon threads to exit");
2939     }
2940
2941     /*
2942      * Sleep on a condition variable until it's okay to exit.
2943      */
2944     Thread* self = dvmThreadSelf();
2945     if (self == NULL) {
2946         JNIEnv* tmpEnv;
2947         if (AttachCurrentThread(vm, &tmpEnv, NULL) != JNI_OK) {
2948             ALOGV("Unable to reattach main for Destroy; assuming VM is shutting down (count=%d)",
2949                 gDvm.nonDaemonThreadCount);
2950             goto shutdown;
2951         } else {
2952             ALOGV("Attached to wait for shutdown in Destroy");
2953         }
2954     }
2955     dvmChangeStatus(self, THREAD_VMWAIT);
2956
2957     dvmLockThreadList(self);
2958     gDvm.nonDaemonThreadCount--;    // remove current thread from count
2959
2960     while (gDvm.nonDaemonThreadCount > 0) {
2961         pthread_cond_wait(&gDvm.vmExitCond, &gDvm.threadListLock);
2962     }
2963
2964     dvmUnlockThreadList();
2965     self = NULL;
2966
2967 shutdown:
2968     // TODO: call System.exit() to run any registered shutdown hooks
2969     // (this may not return -- figure out how this should work)
2970
2971     if (gDvm.verboseShutdown) {
2972         ALOGD("DestroyJavaVM shutting VM down");
2973     }
2974     dvmShutdown();
2975
2976     // TODO - free resources associated with JNI-attached daemon threads
2977     free(ext->envList);
2978     free(ext);
2979
2980     return JNI_OK;
2981 }
2982
2983
2984 /*
2985  * ===========================================================================
2986  *      Function tables
2987  * ===========================================================================
2988  */
2989
2990 static const struct JNINativeInterface gNativeInterface = {
2991     NULL,
2992     NULL,
2993     NULL,
2994     NULL,
2995
2996     GetVersion,
2997
2998     DefineClass,
2999     FindClass,
3000
3001     FromReflectedMethod,
3002     FromReflectedField,
3003     ToReflectedMethod,
3004
3005     GetSuperclass,
3006     IsAssignableFrom,
3007
3008     ToReflectedField,
3009
3010     Throw,
3011     ThrowNew,
3012     ExceptionOccurred,
3013     ExceptionDescribe,
3014     ExceptionClear,
3015     FatalError,
3016
3017     PushLocalFrame,
3018     PopLocalFrame,
3019
3020     NewGlobalRef,
3021     DeleteGlobalRef,
3022     DeleteLocalRef,
3023     IsSameObject,
3024     NewLocalRef,
3025     EnsureLocalCapacity,
3026
3027     AllocObject,
3028     NewObject,
3029     NewObjectV,
3030     NewObjectA,
3031
3032     GetObjectClass,
3033     IsInstanceOf,
3034
3035     GetMethodID,
3036
3037     CallObjectMethod,
3038     CallObjectMethodV,
3039     CallObjectMethodA,
3040     CallBooleanMethod,
3041     CallBooleanMethodV,
3042     CallBooleanMethodA,
3043     CallByteMethod,
3044     CallByteMethodV,
3045     CallByteMethodA,
3046     CallCharMethod,
3047     CallCharMethodV,
3048     CallCharMethodA,
3049     CallShortMethod,
3050     CallShortMethodV,
3051     CallShortMethodA,
3052     CallIntMethod,
3053     CallIntMethodV,
3054     CallIntMethodA,
3055     CallLongMethod,
3056     CallLongMethodV,
3057     CallLongMethodA,
3058     CallFloatMethod,
3059     CallFloatMethodV,
3060     CallFloatMethodA,
3061     CallDoubleMethod,
3062     CallDoubleMethodV,
3063     CallDoubleMethodA,
3064     CallVoidMethod,
3065     CallVoidMethodV,
3066     CallVoidMethodA,
3067
3068     CallNonvirtualObjectMethod,
3069     CallNonvirtualObjectMethodV,
3070     CallNonvirtualObjectMethodA,
3071     CallNonvirtualBooleanMethod,
3072     CallNonvirtualBooleanMethodV,
3073     CallNonvirtualBooleanMethodA,
3074     CallNonvirtualByteMethod,
3075     CallNonvirtualByteMethodV,
3076     CallNonvirtualByteMethodA,
3077     CallNonvirtualCharMethod,
3078     CallNonvirtualCharMethodV,
3079     CallNonvirtualCharMethodA,
3080     CallNonvirtualShortMethod,
3081     CallNonvirtualShortMethodV,
3082     CallNonvirtualShortMethodA,
3083     CallNonvirtualIntMethod,
3084     CallNonvirtualIntMethodV,
3085     CallNonvirtualIntMethodA,
3086     CallNonvirtualLongMethod,
3087     CallNonvirtualLongMethodV,
3088     CallNonvirtualLongMethodA,
3089     CallNonvirtualFloatMethod,
3090     CallNonvirtualFloatMethodV,
3091     CallNonvirtualFloatMethodA,
3092     CallNonvirtualDoubleMethod,
3093     CallNonvirtualDoubleMethodV,
3094     CallNonvirtualDoubleMethodA,
3095     CallNonvirtualVoidMethod,
3096     CallNonvirtualVoidMethodV,
3097     CallNonvirtualVoidMethodA,
3098
3099     GetFieldID,
3100
3101     GetObjectField,
3102     GetBooleanField,
3103     GetByteField,
3104     GetCharField,
3105     GetShortField,
3106     GetIntField,
3107     GetLongField,
3108     GetFloatField,
3109     GetDoubleField,
3110     SetObjectField,
3111     SetBooleanField,
3112     SetByteField,
3113     SetCharField,
3114     SetShortField,
3115     SetIntField,
3116     SetLongField,
3117     SetFloatField,
3118     SetDoubleField,
3119
3120     GetStaticMethodID,
3121
3122     CallStaticObjectMethod,
3123     CallStaticObjectMethodV,
3124     CallStaticObjectMethodA,
3125     CallStaticBooleanMethod,
3126     CallStaticBooleanMethodV,
3127     CallStaticBooleanMethodA,
3128     CallStaticByteMethod,
3129     CallStaticByteMethodV,
3130     CallStaticByteMethodA,
3131     CallStaticCharMethod,
3132     CallStaticCharMethodV,
3133     CallStaticCharMethodA,
3134     CallStaticShortMethod,
3135     CallStaticShortMethodV,
3136     CallStaticShortMethodA,
3137     CallStaticIntMethod,
3138     CallStaticIntMethodV,
3139     CallStaticIntMethodA,
3140     CallStaticLongMethod,
3141     CallStaticLongMethodV,
3142     CallStaticLongMethodA,
3143     CallStaticFloatMethod,
3144     CallStaticFloatMethodV,
3145     CallStaticFloatMethodA,
3146     CallStaticDoubleMethod,
3147     CallStaticDoubleMethodV,
3148     CallStaticDoubleMethodA,
3149     CallStaticVoidMethod,
3150     CallStaticVoidMethodV,
3151     CallStaticVoidMethodA,
3152
3153     GetStaticFieldID,
3154
3155     GetStaticObjectField,
3156     GetStaticBooleanField,
3157     GetStaticByteField,
3158     GetStaticCharField,
3159     GetStaticShortField,
3160     GetStaticIntField,
3161     GetStaticLongField,
3162     GetStaticFloatField,
3163     GetStaticDoubleField,
3164
3165     SetStaticObjectField,
3166     SetStaticBooleanField,
3167     SetStaticByteField,
3168     SetStaticCharField,
3169     SetStaticShortField,
3170     SetStaticIntField,
3171     SetStaticLongField,
3172     SetStaticFloatField,
3173     SetStaticDoubleField,
3174
3175     NewString,
3176
3177     GetStringLength,
3178     GetStringChars,
3179     ReleaseStringChars,
3180
3181     NewStringUTF,
3182     GetStringUTFLength,
3183     GetStringUTFChars,
3184     ReleaseStringUTFChars,
3185
3186     GetArrayLength,
3187     NewObjectArray,
3188     GetObjectArrayElement,
3189     SetObjectArrayElement,
3190
3191     NewBooleanArray,
3192     NewByteArray,
3193     NewCharArray,
3194     NewShortArray,
3195     NewIntArray,
3196     NewLongArray,
3197     NewFloatArray,
3198     NewDoubleArray,
3199
3200     GetBooleanArrayElements,
3201     GetByteArrayElements,
3202     GetCharArrayElements,
3203     GetShortArrayElements,
3204     GetIntArrayElements,
3205     GetLongArrayElements,
3206     GetFloatArrayElements,
3207     GetDoubleArrayElements,
3208
3209     ReleaseBooleanArrayElements,
3210     ReleaseByteArrayElements,
3211     ReleaseCharArrayElements,
3212     ReleaseShortArrayElements,
3213     ReleaseIntArrayElements,
3214     ReleaseLongArrayElements,
3215     ReleaseFloatArrayElements,
3216     ReleaseDoubleArrayElements,
3217
3218     GetBooleanArrayRegion,
3219     GetByteArrayRegion,
3220     GetCharArrayRegion,
3221     GetShortArrayRegion,
3222     GetIntArrayRegion,
3223     GetLongArrayRegion,
3224     GetFloatArrayRegion,
3225     GetDoubleArrayRegion,
3226     SetBooleanArrayRegion,
3227     SetByteArrayRegion,
3228     SetCharArrayRegion,
3229     SetShortArrayRegion,
3230     SetIntArrayRegion,
3231     SetLongArrayRegion,
3232     SetFloatArrayRegion,
3233     SetDoubleArrayRegion,
3234
3235     RegisterNatives,
3236     UnregisterNatives,
3237
3238     MonitorEnter,
3239     MonitorExit,
3240
3241     GetJavaVM,
3242
3243     GetStringRegion,
3244     GetStringUTFRegion,
3245
3246     GetPrimitiveArrayCritical,
3247     ReleasePrimitiveArrayCritical,
3248
3249     GetStringCritical,
3250     ReleaseStringCritical,
3251
3252     NewWeakGlobalRef,
3253     DeleteWeakGlobalRef,
3254
3255     ExceptionCheck,
3256
3257     NewDirectByteBuffer,
3258     GetDirectBufferAddress,
3259     GetDirectBufferCapacity,
3260
3261     GetObjectRefType
3262 };
3263
3264 static const struct JNIInvokeInterface gInvokeInterface = {
3265     NULL,
3266     NULL,
3267     NULL,
3268
3269     DestroyJavaVM,
3270     AttachCurrentThread,
3271     DetachCurrentThread,
3272
3273     GetEnv,
3274
3275     AttachCurrentThreadAsDaemon,
3276 };
3277
3278 /*
3279  * ===========================================================================
3280  *      VM/Env creation
3281  * ===========================================================================
3282  */
3283
3284 /*
3285  * Create a new JNIEnv struct and add it to the VM's list.
3286  *
3287  * "self" will be NULL for the main thread, since the VM hasn't started
3288  * yet; the value will be filled in later.
3289  */
3290 JNIEnv* dvmCreateJNIEnv(Thread* self) {
3291     JavaVMExt* vm = (JavaVMExt*) gDvmJni.jniVm;
3292
3293     //if (self != NULL)
3294     //    ALOGI("Ent CreateJNIEnv: threadid=%d %p", self->threadId, self);
3295
3296     assert(vm != NULL);
3297
3298     JNIEnvExt* newEnv = (JNIEnvExt*) calloc(1, sizeof(JNIEnvExt));
3299     newEnv->funcTable = &gNativeInterface;
3300     if (self != NULL) {
3301         dvmSetJniEnvThreadId((JNIEnv*) newEnv, self);
3302         assert(newEnv->envThreadId != 0);
3303     } else {
3304         /* make it obvious if we fail to initialize these later */
3305         newEnv->envThreadId = 0x77777775;
3306         newEnv->self = (Thread*) 0x77777779;
3307     }
3308     if (gDvmJni.useCheckJni) {
3309         dvmUseCheckedJniEnv(newEnv);
3310     }
3311
3312     ScopedPthreadMutexLock lock(&vm->envListLock);
3313
3314     /* insert at head of list */
3315     newEnv->next = vm->envList;
3316     assert(newEnv->prev == NULL);
3317     if (vm->envList == NULL) {
3318         // rare, but possible
3319         vm->envList = newEnv;
3320     } else {
3321         vm->envList->prev = newEnv;
3322     }
3323     vm->envList = newEnv;
3324
3325     //if (self != NULL)
3326     //    ALOGI("Xit CreateJNIEnv: threadid=%d %p", self->threadId, self);
3327     return (JNIEnv*) newEnv;
3328 }
3329
3330 /*
3331  * Remove a JNIEnv struct from the list and free it.
3332  */
3333 void dvmDestroyJNIEnv(JNIEnv* env) {
3334     if (env == NULL) {
3335         return;
3336     }
3337
3338     //ALOGI("Ent DestroyJNIEnv: threadid=%d %p", self->threadId, self);
3339
3340     JNIEnvExt* extEnv = (JNIEnvExt*) env;
3341     JavaVMExt* vm = (JavaVMExt*) gDvmJni.jniVm;
3342
3343     ScopedPthreadMutexLock lock(&vm->envListLock);
3344
3345     if (extEnv == vm->envList) {
3346         assert(extEnv->prev == NULL);
3347         vm->envList = extEnv->next;
3348     } else {
3349         assert(extEnv->prev != NULL);
3350         extEnv->prev->next = extEnv->next;
3351     }
3352     if (extEnv->next != NULL) {
3353         extEnv->next->prev = extEnv->prev;
3354     }
3355
3356     free(env);
3357     //ALOGI("Xit DestroyJNIEnv: threadid=%d %p", self->threadId, self);
3358 }
3359
3360 /*
3361  * Enable "checked JNI" after the VM has partially started.  This must
3362  * only be called in "zygote" mode, when we have one thread running.
3363  *
3364  * This doesn't attempt to rewrite the JNI call bridge associated with
3365  * native methods, so we won't get those checks for any methods that have
3366  * already been resolved.
3367  */
3368 void dvmLateEnableCheckedJni() {
3369     JNIEnvExt* extEnv = dvmGetJNIEnvForThread();
3370     if (extEnv == NULL) {
3371         ALOGE("dvmLateEnableCheckedJni: thread has no JNIEnv");
3372         return;
3373     }
3374     JavaVMExt* extVm = (JavaVMExt*) gDvmJni.jniVm;
3375     assert(extVm != NULL);
3376
3377     if (!gDvmJni.useCheckJni) {
3378         ALOGD("Late-enabling CheckJNI");
3379         dvmUseCheckedJniVm(extVm);
3380         dvmUseCheckedJniEnv(extEnv);
3381     } else {
3382         ALOGD("Not late-enabling CheckJNI (already on)");
3383     }
3384 }
3385
3386 /*
3387  * Not supported.
3388  */
3389 jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
3390     return JNI_ERR;
3391 }
3392
3393 /*
3394  * Return a buffer full of created VMs.
3395  *
3396  * We always have zero or one.
3397  */
3398 jint JNI_GetCreatedJavaVMs(JavaVM** vmBuf, jsize bufLen, jsize* nVMs) {
3399     if (gDvmJni.jniVm != NULL) {
3400         *nVMs = 1;
3401         if (bufLen > 0) {
3402             *vmBuf++ = gDvmJni.jniVm;
3403         }
3404     } else {
3405         *nVMs = 0;
3406     }
3407     return JNI_OK;
3408 }
3409
3410 /*
3411  * Create a new VM instance.
3412  *
3413  * The current thread becomes the main VM thread.  We return immediately,
3414  * which effectively means the caller is executing in a native method.
3415  */
3416 jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
3417     const JavaVMInitArgs* args = (JavaVMInitArgs*) vm_args;
3418     if (args->version < JNI_VERSION_1_2) {
3419         return JNI_EVERSION;
3420     }
3421
3422     // TODO: don't allow creation of multiple VMs -- one per customer for now
3423
3424     /* zero globals; not strictly necessary the first time a VM is started */
3425     memset(&gDvm, 0, sizeof(gDvm));
3426
3427     /*
3428      * Set up structures for JNIEnv and VM.
3429      */
3430     JavaVMExt* pVM = (JavaVMExt*) calloc(1, sizeof(JavaVMExt));
3431     pVM->funcTable = &gInvokeInterface;
3432     pVM->envList = NULL;
3433     dvmInitMutex(&pVM->envListLock);
3434
3435     UniquePtr<const char*[]> argv(new const char*[args->nOptions]);
3436     memset(argv.get(), 0, sizeof(char*) * (args->nOptions));
3437
3438     /*
3439      * Convert JNI args to argv.
3440      *
3441      * We have to pull out vfprintf/exit/abort, because they use the
3442      * "extraInfo" field to pass function pointer "hooks" in.  We also
3443      * look for the -Xcheck:jni stuff here.
3444      */
3445     int argc = 0;
3446     for (int i = 0; i < args->nOptions; i++) {
3447         const char* optStr = args->options[i].optionString;
3448         if (optStr == NULL) {
3449             dvmFprintf(stderr, "ERROR: CreateJavaVM failed: argument %d was NULL\n", i);
3450             return JNI_ERR;
3451         } else if (strcmp(optStr, "vfprintf") == 0) {
3452             gDvm.vfprintfHook = (int (*)(FILE *, const char*, va_list))args->options[i].extraInfo;
3453         } else if (strcmp(optStr, "exit") == 0) {
3454             gDvm.exitHook = (void (*)(int)) args->options[i].extraInfo;
3455         } else if (strcmp(optStr, "abort") == 0) {
3456             gDvm.abortHook = (void (*)(void))args->options[i].extraInfo;
3457         } else if (strcmp(optStr, "sensitiveThread") == 0) {
3458             gDvm.isSensitiveThreadHook = (bool (*)(void))args->options[i].extraInfo;
3459         } else if (strcmp(optStr, "-Xcheck:jni") == 0) {
3460             gDvmJni.useCheckJni = true;
3461         } else if (strncmp(optStr, "-Xjniopts:", 10) == 0) {
3462             char* jniOpts = strdup(optStr + 10);
3463             size_t jniOptCount = 1;
3464             for (char* p = jniOpts; *p != 0; ++p) {
3465                 if (*p == ',') {
3466                     ++jniOptCount;
3467                     *p = 0;
3468                 }
3469             }
3470             char* jniOpt = jniOpts;
3471             for (size_t i = 0; i < jniOptCount; ++i) {
3472                 if (strcmp(jniOpt, "warnonly") == 0) {
3473                     gDvmJni.warnOnly = true;
3474                 } else if (strcmp(jniOpt, "forcecopy") == 0) {
3475                     gDvmJni.forceCopy = true;
3476                 } else if (strcmp(jniOpt, "logThirdPartyJni") == 0) {
3477                     gDvmJni.logThirdPartyJni = true;
3478                 } else {
3479                     dvmFprintf(stderr, "ERROR: CreateJavaVM failed: unknown -Xjniopts option '%s'\n",
3480                             jniOpt);
3481                     return JNI_ERR;
3482                 }
3483                 jniOpt += strlen(jniOpt) + 1;
3484             }
3485             free(jniOpts);
3486         } else {
3487             /* regular option */
3488             argv[argc++] = optStr;
3489         }
3490     }
3491
3492     if (gDvmJni.useCheckJni) {
3493         dvmUseCheckedJniVm(pVM);
3494     }
3495
3496     if (gDvmJni.jniVm != NULL) {
3497         dvmFprintf(stderr, "ERROR: Dalvik only supports one VM per process\n");
3498         return JNI_ERR;
3499     }
3500     gDvmJni.jniVm = (JavaVM*) pVM;
3501
3502     /*
3503      * Create a JNIEnv for the main thread.  We need to have something set up
3504      * here because some of the class initialization we do when starting
3505      * up the VM will call into native code.
3506      */
3507     JNIEnvExt* pEnv = (JNIEnvExt*) dvmCreateJNIEnv(NULL);
3508
3509     /* Initialize VM. */
3510     gDvm.initializing = true;
3511     std::string status =
3512             dvmStartup(argc, argv.get(), args->ignoreUnrecognized, (JNIEnv*)pEnv);
3513     gDvm.initializing = false;
3514
3515     if (!status.empty()) {
3516         free(pEnv);
3517         free(pVM);
3518         ALOGW("CreateJavaVM failed: %s", status.c_str());
3519         return JNI_ERR;
3520     }
3521
3522     /*
3523      * Success!  Return stuff to caller.
3524      */
3525     dvmChangeStatus(NULL, THREAD_NATIVE);
3526     *p_env = (JNIEnv*) pEnv;
3527     *p_vm = (JavaVM*) pVM;
3528     ALOGV("CreateJavaVM succeeded");
3529     return JNI_OK;
3530 }