OSDN Git Service

Merge "Fix the exception thrown by getDeclaredFields if the class is unavailable...
[android-x86/dalvik.git] / vm / interp / Stack.c
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  * Stacks and their uses (e.g. native --> interpreted method calls).
19  *
20  * See the majestic ASCII art in Stack.h.
21  */
22 #include "Dalvik.h"
23 #include "jni.h"
24
25 #include <stdlib.h>
26 #include <stdarg.h>
27
28 /*
29  * Initialize the interpreter stack in a new thread.
30  *
31  * Currently this doesn't do much, since we don't need to zero out the
32  * stack (and we really don't want to if it was created with mmap).
33  */
34 bool dvmInitInterpStack(Thread* thread, int stackSize)
35 {
36     assert(thread->interpStackStart != NULL);
37
38     assert(thread->curFrame == NULL);
39
40     return true;
41 }
42
43 /*
44  * We're calling an interpreted method from an internal VM function or
45  * via reflection.
46  *
47  * Push a frame for an interpreted method onto the stack.  This is only
48  * used when calling into interpreted code from native code.  (The
49  * interpreter does its own stack frame manipulation for interp-->interp
50  * calls.)
51  *
52  * The size we need to reserve is the sum of parameters, local variables,
53  * saved goodies, and outbound parameters.
54  *
55  * We start by inserting a "break" frame, which ensures that the interpreter
56  * hands control back to us after the function we call returns or an
57  * uncaught exception is thrown.
58  */
59 static bool dvmPushInterpFrame(Thread* self, const Method* method)
60 {
61     StackSaveArea* saveBlock;
62     StackSaveArea* breakSaveBlock;
63     int stackReq;
64     u1* stackPtr;
65
66     assert(!dvmIsNativeMethod(method));
67     assert(!dvmIsAbstractMethod(method));
68
69     stackReq = method->registersSize * 4        // params + locals
70                 + sizeof(StackSaveArea) * 2     // break frame + regular frame
71                 + method->outsSize * 4;         // args to other methods
72
73     if (self->curFrame != NULL)
74         stackPtr = (u1*) SAVEAREA_FROM_FP(self->curFrame);
75     else
76         stackPtr = self->interpStackStart;
77
78     if (stackPtr - stackReq < self->interpStackEnd) {
79         /* not enough space */
80         LOGW("Stack overflow on call to interp "
81              "(req=%d top=%p cur=%p size=%d %s.%s)\n",
82             stackReq, self->interpStackStart, self->curFrame,
83             self->interpStackSize, method->clazz->descriptor, method->name);
84         dvmHandleStackOverflow(self, method);
85         assert(dvmCheckException(self));
86         return false;
87     }
88
89     /*
90      * Shift the stack pointer down, leaving space for the function's
91      * args/registers and save area.
92      */
93     stackPtr -= sizeof(StackSaveArea);
94     breakSaveBlock = (StackSaveArea*)stackPtr;
95     stackPtr -= method->registersSize * 4 + sizeof(StackSaveArea);
96     saveBlock = (StackSaveArea*) stackPtr;
97
98 #if !defined(NDEBUG) && !defined(PAD_SAVE_AREA)
99     /* debug -- memset the new stack, unless we want valgrind's help */
100     memset(stackPtr - (method->outsSize*4), 0xaf, stackReq);
101 #endif
102 #ifdef EASY_GDB
103     breakSaveBlock->prevSave = FP_FROM_SAVEAREA(self->curFrame);
104     saveBlock->prevSave = breakSaveBlock;
105 #endif
106
107     breakSaveBlock->prevFrame = self->curFrame;
108     breakSaveBlock->savedPc = NULL;             // not required
109     breakSaveBlock->xtra.localRefCookie = 0;    // not required
110     breakSaveBlock->method = NULL;
111     saveBlock->prevFrame = FP_FROM_SAVEAREA(breakSaveBlock);
112     saveBlock->savedPc = NULL;                  // not required
113     saveBlock->xtra.currentPc = NULL;           // not required?
114     saveBlock->method = method;
115
116     LOGVV("PUSH frame: old=%p new=%p (size=%d)\n",
117         self->curFrame, FP_FROM_SAVEAREA(saveBlock),
118         (u1*)self->curFrame - (u1*)FP_FROM_SAVEAREA(saveBlock));
119
120     self->curFrame = FP_FROM_SAVEAREA(saveBlock);
121
122     return true;
123 }
124
125 /*
126  * We're calling a JNI native method from an internal VM fuction or
127  * via reflection.  This is also used to create the "fake" native-method
128  * frames at the top of the interpreted stack.
129  *
130  * This actually pushes two frames; the first is a "break" frame.
131  *
132  * The top frame has additional space for JNI local reference tracking.
133  */
134 bool dvmPushJNIFrame(Thread* self, const Method* method)
135 {
136     StackSaveArea* saveBlock;
137     StackSaveArea* breakSaveBlock;
138     int stackReq;
139     u1* stackPtr;
140
141     assert(dvmIsNativeMethod(method));
142
143     stackReq = method->registersSize * 4        // params only
144                 + sizeof(StackSaveArea) * 2;    // break frame + regular frame
145
146     if (self->curFrame != NULL)
147         stackPtr = (u1*) SAVEAREA_FROM_FP(self->curFrame);
148     else
149         stackPtr = self->interpStackStart;
150
151     if (stackPtr - stackReq < self->interpStackEnd) {
152         /* not enough space */
153         LOGW("Stack overflow on call to native "
154              "(req=%d top=%p cur=%p size=%d '%s')\n",
155             stackReq, self->interpStackStart, self->curFrame,
156             self->interpStackSize, method->name);
157         dvmHandleStackOverflow(self, method);
158         assert(dvmCheckException(self));
159         return false;
160     }
161
162     /*
163      * Shift the stack pointer down, leaving space for just the stack save
164      * area for the break frame, then shift down farther for the full frame.
165      * We leave space for the method args, which are copied in later.
166      */
167     stackPtr -= sizeof(StackSaveArea);
168     breakSaveBlock = (StackSaveArea*)stackPtr;
169     stackPtr -= method->registersSize * 4 + sizeof(StackSaveArea);
170     saveBlock = (StackSaveArea*) stackPtr;
171
172 #if !defined(NDEBUG) && !defined(PAD_SAVE_AREA)
173     /* debug -- memset the new stack */
174     memset(stackPtr, 0xaf, stackReq);
175 #endif
176 #ifdef EASY_GDB
177     if (self->curFrame == NULL)
178         breakSaveBlock->prevSave = NULL;
179     else
180         breakSaveBlock->prevSave = FP_FROM_SAVEAREA(self->curFrame);
181     saveBlock->prevSave = breakSaveBlock;
182 #endif
183
184     breakSaveBlock->prevFrame = self->curFrame;
185     breakSaveBlock->savedPc = NULL;             // not required
186     breakSaveBlock->xtra.localRefCookie = 0;    // not required
187     breakSaveBlock->method = NULL;
188     saveBlock->prevFrame = FP_FROM_SAVEAREA(breakSaveBlock);
189     saveBlock->savedPc = NULL;                  // not required
190 #ifdef USE_INDIRECT_REF
191     saveBlock->xtra.localRefCookie = self->jniLocalRefTable.segmentState.all;
192 #else
193     saveBlock->xtra.localRefCookie = self->jniLocalRefTable.nextEntry;
194 #endif
195     saveBlock->method = method;
196
197     LOGVV("PUSH JNI frame: old=%p new=%p (size=%d)\n",
198         self->curFrame, FP_FROM_SAVEAREA(saveBlock),
199         (u1*)self->curFrame - (u1*)FP_FROM_SAVEAREA(saveBlock));
200
201     self->curFrame = FP_FROM_SAVEAREA(saveBlock);
202
203     return true;
204 }
205
206 /*
207  * This is used by the JNI PushLocalFrame call.  We push a new frame onto
208  * the stack that has no ins, outs, or locals, and no break frame above it.
209  * It's strictly used for tracking JNI local refs, and will be popped off
210  * by dvmPopFrame if it's not removed explicitly.
211  */
212 bool dvmPushLocalFrame(Thread* self, const Method* method)
213 {
214     StackSaveArea* saveBlock;
215     int stackReq;
216     u1* stackPtr;
217
218     assert(dvmIsNativeMethod(method));
219
220     stackReq = sizeof(StackSaveArea);       // regular frame
221
222     assert(self->curFrame != NULL);
223     stackPtr = (u1*) SAVEAREA_FROM_FP(self->curFrame);
224
225     if (stackPtr - stackReq < self->interpStackEnd) {
226         /* not enough space; let JNI throw the exception */
227         LOGW("Stack overflow on PushLocal "
228              "(req=%d top=%p cur=%p size=%d '%s')\n",
229             stackReq, self->interpStackStart, self->curFrame,
230             self->interpStackSize, method->name);
231         dvmHandleStackOverflow(self, method);
232         assert(dvmCheckException(self));
233         return false;
234     }
235
236     /*
237      * Shift the stack pointer down, leaving space for just the stack save
238      * area for the break frame, then shift down farther for the full frame.
239      */
240     stackPtr -= sizeof(StackSaveArea);
241     saveBlock = (StackSaveArea*) stackPtr;
242
243 #if !defined(NDEBUG) && !defined(PAD_SAVE_AREA)
244     /* debug -- memset the new stack */
245     memset(stackPtr, 0xaf, stackReq);
246 #endif
247 #ifdef EASY_GDB
248     saveBlock->prevSave = FP_FROM_SAVEAREA(self->curFrame);
249 #endif
250
251     saveBlock->prevFrame = self->curFrame;
252     saveBlock->savedPc = NULL;                  // not required
253 #ifdef USE_INDIRECT_REF
254     saveBlock->xtra.localRefCookie = self->jniLocalRefTable.segmentState.all;
255 #else
256     saveBlock->xtra.localRefCookie = self->jniLocalRefTable.nextEntry;
257 #endif
258     saveBlock->method = method;
259
260     LOGVV("PUSH JNI local frame: old=%p new=%p (size=%d)\n",
261         self->curFrame, FP_FROM_SAVEAREA(saveBlock),
262         (u1*)self->curFrame - (u1*)FP_FROM_SAVEAREA(saveBlock));
263
264     self->curFrame = FP_FROM_SAVEAREA(saveBlock);
265
266     return true;
267 }
268
269 /*
270  * Pop one frame pushed on by JNI PushLocalFrame.
271  *
272  * If we've gone too far, the previous frame is either a break frame or
273  * an interpreted frame.  Either way, the method pointer won't match.
274  */
275 bool dvmPopLocalFrame(Thread* self)
276 {
277     StackSaveArea* saveBlock = SAVEAREA_FROM_FP(self->curFrame);
278
279     assert(!dvmIsBreakFrame(self->curFrame));
280     if (saveBlock->method != SAVEAREA_FROM_FP(saveBlock->prevFrame)->method) {
281         /*
282          * The previous frame doesn't have the same method pointer -- we've
283          * been asked to pop too much.
284          */
285         assert(dvmIsBreakFrame(saveBlock->prevFrame) ||
286                !dvmIsNativeMethod(
287                        SAVEAREA_FROM_FP(saveBlock->prevFrame)->method));
288         return false;
289     }
290
291     LOGVV("POP JNI local frame: removing %s, now %s\n",
292         saveBlock->method->name,
293         SAVEAREA_FROM_FP(saveBlock->prevFrame)->method->name);
294     dvmPopJniLocals(self, saveBlock);
295     self->curFrame = saveBlock->prevFrame;
296
297     return true;
298 }
299
300 /*
301  * Pop a frame we added.  There should be one method frame and one break
302  * frame.
303  *
304  * If JNI Push/PopLocalFrame calls were mismatched, we might end up
305  * popping multiple method frames before we find the break.
306  *
307  * Returns "false" if there was no frame to pop.
308  */
309 static bool dvmPopFrame(Thread* self)
310 {
311     StackSaveArea* saveBlock;
312
313     if (self->curFrame == NULL)
314         return false;
315
316     saveBlock = SAVEAREA_FROM_FP(self->curFrame);
317     assert(!dvmIsBreakFrame(self->curFrame));
318
319     /*
320      * Remove everything up to the break frame.  If this was a call into
321      * native code, pop the JNI local references table.
322      */
323     while (saveBlock->prevFrame != NULL && saveBlock->method != NULL) {
324         /* probably a native->native JNI call */
325
326         if (dvmIsNativeMethod(saveBlock->method)) {
327             LOGVV("Popping JNI stack frame for %s.%s%s\n",
328                 saveBlock->method->clazz->descriptor,
329                 saveBlock->method->name,
330                 (SAVEAREA_FROM_FP(saveBlock->prevFrame)->method == NULL) ?
331                 "" : " (JNI local)");
332             assert(saveBlock->xtra.localRefCookie != 0);
333             //assert(saveBlock->xtra.localRefCookie >= self->jniLocalRefTable.table &&
334             //    saveBlock->xtra.localRefCookie <=self->jniLocalRefTable.nextEntry);
335
336             dvmPopJniLocals(self, saveBlock);
337         }
338
339         saveBlock = SAVEAREA_FROM_FP(saveBlock->prevFrame);
340     }
341     if (saveBlock->method != NULL) {
342         LOGE("PopFrame missed the break\n");
343         assert(false);
344         dvmAbort();     // stack trashed -- nowhere to go in this thread
345     }
346
347     LOGVV("POP frame: cur=%p new=%p\n",
348         self->curFrame, saveBlock->prevFrame);
349
350     self->curFrame = saveBlock->prevFrame;
351     return true;
352 }
353
354 /*
355  * Common code for dvmCallMethodV/A and dvmInvokeMethod.
356  *
357  * Pushes a call frame on, advancing self->curFrame.
358  */
359 static ClassObject* callPrep(Thread* self, const Method* method, Object* obj,
360     bool checkAccess)
361 {
362     ClassObject* clazz;
363
364 #ifndef NDEBUG
365     if (self->status != THREAD_RUNNING) {
366         LOGW("threadid=%d: status=%d on call to %s.%s -\n",
367             self->threadId, self->status,
368             method->clazz->descriptor, method->name);
369     }
370 #endif
371
372     assert(self != NULL);
373     assert(method != NULL);
374
375     if (obj != NULL)
376         clazz = obj->clazz;
377     else
378         clazz = method->clazz;
379
380     IF_LOGVV() {
381         char* desc = dexProtoCopyMethodDescriptor(&method->prototype);
382         LOGVV("thread=%d native code calling %s.%s %s\n", self->threadId,
383             clazz->descriptor, method->name, desc);
384         free(desc);
385     }
386
387     if (checkAccess) {
388         /* needed for java.lang.reflect.Method.invoke */
389         if (!dvmCheckMethodAccess(dvmGetCaller2Class(self->curFrame),
390                 method))
391         {
392             /* note this throws IAException, not IAError */
393             dvmThrowException("Ljava/lang/IllegalAccessException;",
394                 "access to method denied");
395             return NULL;
396         }
397     }
398
399     /*
400      * Push a call frame on.  If there isn't enough room for ins, locals,
401      * outs, and the saved state, it will throw an exception.
402      *
403      * This updates self->curFrame.
404      */
405     if (dvmIsNativeMethod(method)) {
406         /* native code calling native code the hard way */
407         if (!dvmPushJNIFrame(self, method)) {
408             assert(dvmCheckException(self));
409             return NULL;
410         }
411     } else {
412         /* native code calling interpreted code */
413         if (!dvmPushInterpFrame(self, method)) {
414             assert(dvmCheckException(self));
415             return NULL;
416         }
417     }
418
419     return clazz;
420 }
421
422 /*
423  * Issue a method call.
424  *
425  * Pass in NULL for "obj" on calls to static methods.
426  *
427  * (Note this can't be inlined because it takes a variable number of args.)
428  */
429 void dvmCallMethod(Thread* self, const Method* method, Object* obj,
430     JValue* pResult, ...)
431 {
432     va_list args;
433     va_start(args, pResult);
434     dvmCallMethodV(self, method, obj, false, pResult, args);
435     va_end(args);
436 }
437
438 /*
439  * Issue a method call with a variable number of arguments.  We process
440  * the contents of "args" by scanning the method signature.
441  *
442  * Pass in NULL for "obj" on calls to static methods.
443  *
444  * We don't need to take the class as an argument because, in Dalvik,
445  * we don't need to worry about static synchronized methods.
446  */
447 void dvmCallMethodV(Thread* self, const Method* method, Object* obj,
448     bool fromJni, JValue* pResult, va_list args)
449 {
450     const char* desc = &(method->shorty[1]); // [0] is the return type.
451     int verifyCount = 0;
452     ClassObject* clazz;
453     u4* ins;
454
455     clazz = callPrep(self, method, obj, false);
456     if (clazz == NULL)
457         return;
458
459     /* "ins" for new frame start at frame pointer plus locals */
460     ins = ((u4*)self->curFrame) + (method->registersSize - method->insSize);
461
462     //LOGD("  FP is %p, INs live at >= %p\n", self->curFrame, ins);
463
464     /* put "this" pointer into in0 if appropriate */
465     if (!dvmIsStaticMethod(method)) {
466 #ifdef WITH_EXTRA_OBJECT_VALIDATION
467         assert(obj != NULL && dvmIsValidObject(obj));
468 #endif
469         *ins++ = (u4) obj;
470         verifyCount++;
471     }
472
473     JNIEnv* env = self->jniEnv;
474     while (*desc != '\0') {
475         switch (*(desc++)) {
476             case 'D': case 'J': {
477                 u8 val = va_arg(args, u8);
478                 memcpy(ins, &val, 8);       // EABI prevents direct store
479                 ins += 2;
480                 verifyCount += 2;
481                 break;
482             }
483             case 'F': {
484                 /* floats were normalized to doubles; convert back */
485                 float f = (float) va_arg(args, double);
486                 *ins++ = dvmFloatToU4(f);
487                 verifyCount++;
488                 break;
489             }
490             case 'L': {     /* 'shorty' descr uses L for all refs, incl array */
491                 void* argObj = va_arg(args, void*);
492                 assert(obj == NULL || dvmIsValidObject(obj));
493                 if (fromJni)
494                     *ins++ = (u4) dvmDecodeIndirectRef(env, argObj);
495                 else
496                     *ins++ = (u4) argObj;
497                 verifyCount++;
498                 break;
499             }
500             default: {
501                 /* Z B C S I -- all passed as 32-bit integers */
502                 *ins++ = va_arg(args, u4);
503                 verifyCount++;
504                 break;
505             }
506         }
507     }
508
509 #ifndef NDEBUG
510     if (verifyCount != method->insSize) {
511         LOGE("Got vfycount=%d insSize=%d for %s.%s\n", verifyCount,
512             method->insSize, clazz->descriptor, method->name);
513         assert(false);
514         goto bail;
515     }
516 #endif
517
518     //dvmDumpThreadStack(dvmThreadSelf());
519
520     if (dvmIsNativeMethod(method)) {
521         TRACE_METHOD_ENTER(self, method);
522         /*
523          * Because we leave no space for local variables, "curFrame" points
524          * directly at the method arguments.
525          */
526         (*method->nativeFunc)(self->curFrame, pResult, method, self);
527         TRACE_METHOD_EXIT(self, method);
528     } else {
529         dvmInterpret(self, method, pResult);
530     }
531
532 #ifndef NDEBUG
533 bail:
534 #endif
535     dvmPopFrame(self);
536 }
537
538 /*
539  * Issue a method call with arguments provided in an array.  We process
540  * the contents of "args" by scanning the method signature.
541  *
542  * The values were likely placed into an uninitialized jvalue array using
543  * the field specifiers, which means that sub-32-bit fields (e.g. short,
544  * boolean) may not have 32 or 64 bits of valid data.  This is different
545  * from the varargs invocation where the C compiler does a widening
546  * conversion when calling a function.  As a result, we have to be a
547  * little more precise when pulling stuff out.
548  *
549  * "args" may be NULL if the method has no arguments.
550  */
551 void dvmCallMethodA(Thread* self, const Method* method, Object* obj,
552     bool fromJni, JValue* pResult, const jvalue* args)
553 {
554     const char* desc = &(method->shorty[1]); // [0] is the return type.
555     int verifyCount = 0;
556     ClassObject* clazz;
557     u4* ins;
558
559     clazz = callPrep(self, method, obj, false);
560     if (clazz == NULL)
561         return;
562
563     /* "ins" for new frame start at frame pointer plus locals */
564     ins = ((u4*)self->curFrame) + (method->registersSize - method->insSize);
565
566     /* put "this" pointer into in0 if appropriate */
567     if (!dvmIsStaticMethod(method)) {
568         assert(obj != NULL);
569         *ins++ = (u4) obj;              /* obj is a "real" ref */
570         verifyCount++;
571     }
572
573     JNIEnv* env = self->jniEnv;
574     while (*desc != '\0') {
575         switch (*desc++) {
576         case 'D':                       /* 64-bit quantity; have to use */
577         case 'J':                       /*  memcpy() in case of mis-alignment */
578             memcpy(ins, &args->j, 8);
579             ins += 2;
580             verifyCount++;              /* this needs an extra push */
581             break;
582         case 'L':                       /* includes array refs */
583             if (fromJni)
584                 *ins++ = (u4) dvmDecodeIndirectRef(env, args->l);
585             else
586                 *ins++ = (u4) args->l;
587             break;
588         case 'F':
589         case 'I':
590             *ins++ = args->i;           /* full 32 bits */
591             break;
592         case 'S':
593             *ins++ = args->s;           /* 16 bits, sign-extended */
594             break;
595         case 'C':
596             *ins++ = args->c;           /* 16 bits, unsigned */
597             break;
598         case 'B':
599             *ins++ = args->b;           /* 8 bits, sign-extended */
600             break;
601         case 'Z':
602             *ins++ = args->z;           /* 8 bits, zero or non-zero */
603             break;
604         default:
605             LOGE("Invalid char %c in short signature of %s.%s\n",
606                 *(desc-1), clazz->descriptor, method->name);
607             assert(false);
608             goto bail;
609         }
610
611         verifyCount++;
612         args++;
613     }
614
615 #ifndef NDEBUG
616     if (verifyCount != method->insSize) {
617         LOGE("Got vfycount=%d insSize=%d for %s.%s\n", verifyCount,
618             method->insSize, clazz->descriptor, method->name);
619         assert(false);
620         goto bail;
621     }
622 #endif
623
624     if (dvmIsNativeMethod(method)) {
625         TRACE_METHOD_ENTER(self, method);
626         /*
627          * Because we leave no space for local variables, "curFrame" points
628          * directly at the method arguments.
629          */
630         (*method->nativeFunc)(self->curFrame, pResult, method, self);
631         TRACE_METHOD_EXIT(self, method);
632     } else {
633         dvmInterpret(self, method, pResult);
634     }
635
636 bail:
637     dvmPopFrame(self);
638 }
639
640 /*
641  * Invoke a method, using the specified arguments and return type, through
642  * one of the reflection interfaces.  Could be a virtual or direct method
643  * (including constructors).  Used for reflection.
644  *
645  * Deals with boxing/unboxing primitives and performs widening conversions.
646  *
647  * "invokeObj" will be null for a static method.
648  *
649  * If the invocation returns with an exception raised, we have to wrap it.
650  */
651 Object* dvmInvokeMethod(Object* obj, const Method* method,
652     ArrayObject* argList, ArrayObject* params, ClassObject* returnType,
653     bool noAccessCheck)
654 {
655     ClassObject* clazz;
656     Object* retObj = NULL;
657     Thread* self = dvmThreadSelf();
658     s4* ins;
659     int verifyCount, argListLength;
660     JValue retval;
661
662     /* verify arg count */
663     if (argList != NULL)
664         argListLength = argList->length;
665     else
666         argListLength = 0;
667     if (argListLength != (int) params->length) {
668         LOGI("invoke: expected %d args, received %d args\n",
669             params->length, argListLength);
670         dvmThrowException("Ljava/lang/IllegalArgumentException;",
671             "wrong number of arguments");
672         return NULL;
673     }
674
675     clazz = callPrep(self, method, obj, !noAccessCheck);
676     if (clazz == NULL)
677         return NULL;
678
679     /* "ins" for new frame start at frame pointer plus locals */
680     ins = ((s4*)self->curFrame) + (method->registersSize - method->insSize);
681     verifyCount = 0;
682
683     //LOGD("  FP is %p, INs live at >= %p\n", self->curFrame, ins);
684
685     /* put "this" pointer into in0 if appropriate */
686     if (!dvmIsStaticMethod(method)) {
687         assert(obj != NULL);
688         *ins++ = (s4) obj;
689         verifyCount++;
690     }
691
692     /*
693      * Copy the args onto the stack.  Primitive types are converted when
694      * necessary, and object types are verified.
695      */
696     DataObject** args;
697     ClassObject** types;
698     int i;
699
700     args = (DataObject**) argList->contents;
701     types = (ClassObject**) params->contents;
702     for (i = 0; i < argListLength; i++) {
703         int width;
704
705         width = dvmConvertArgument(*args++, *types++, ins);
706         if (width < 0) {
707             if (*(args-1) != NULL) {
708                 LOGV("invoke: type mismatch on arg %d ('%s' '%s')\n",
709                     i, (*(args-1))->obj.clazz->descriptor,
710                     (*(types-1))->descriptor);
711             }
712             dvmPopFrame(self);      // throw wants to pull PC out of stack
713             dvmThrowException("Ljava/lang/IllegalArgumentException;",
714                 "argument type mismatch");
715             goto bail_popped;
716         }
717
718         ins += width;
719         verifyCount += width;
720     }
721
722     if (verifyCount != method->insSize) {
723         LOGE("Got vfycount=%d insSize=%d for %s.%s\n", verifyCount,
724             method->insSize, clazz->descriptor, method->name);
725         assert(false);
726         goto bail;
727     }
728     //dvmDumpThreadStack(dvmThreadSelf());
729
730     if (dvmIsNativeMethod(method)) {
731         TRACE_METHOD_ENTER(self, method);
732         /*
733          * Because we leave no space for local variables, "curFrame" points
734          * directly at the method arguments.
735          */
736         (*method->nativeFunc)(self->curFrame, &retval, method, self);
737         TRACE_METHOD_EXIT(self, method);
738     } else {
739         dvmInterpret(self, method, &retval);
740     }
741
742     /*
743      * If an exception is raised, wrap and replace.  This is necessary
744      * because the invoked method could have thrown a checked exception
745      * that the caller wasn't prepared for.
746      *
747      * We might be able to do this up in the interpreted code, but that will
748      * leave us with a shortened stack trace in the top-level exception.
749      */
750     if (dvmCheckException(self)) {
751         dvmWrapException("Ljava/lang/reflect/InvocationTargetException;");
752     } else {
753         /*
754          * If this isn't a void method or constructor, convert the return type
755          * to an appropriate object.
756          *
757          * We don't do this when an exception is raised because the value
758          * in "retval" is undefined.
759          */
760         if (returnType != NULL) {
761             retObj = (Object*)dvmWrapPrimitive(retval, returnType);
762             dvmReleaseTrackedAlloc(retObj, NULL);
763         }
764     }
765
766 bail:
767     dvmPopFrame(self);
768 bail_popped:
769     return retObj;
770 }
771
772 typedef struct LineNumFromPcContext {
773     u4 address;
774     u4 lineNum;
775 } LineNumFromPcContext;
776
777 static int lineNumForPcCb(void *cnxt, u4 address, u4 lineNum)
778 {
779     LineNumFromPcContext *pContext = (LineNumFromPcContext *)cnxt;
780
781     // We know that this callback will be called in
782     // ascending address order, so keep going until we find
783     // a match or we've just gone past it.
784
785     if (address > pContext->address) {
786         // The line number from the previous positions callback
787         // wil be the final result.
788         return 1;
789     }
790
791     pContext->lineNum = lineNum;
792
793     return (address == pContext->address) ? 1 : 0;
794 }
795
796 /*
797  * Determine the source file line number based on the program counter.
798  * "pc" is an offset, in 16-bit units, from the start of the method's code.
799  *
800  * Returns -1 if no match was found (possibly because the source files were
801  * compiled without "-g", so no line number information is present).
802  * Returns -2 for native methods (as expected in exception traces).
803  */
804 int dvmLineNumFromPC(const Method* method, u4 relPc)
805 {
806     const DexCode* pDexCode = dvmGetMethodCode(method);
807
808     if (pDexCode == NULL) {
809         if (dvmIsNativeMethod(method) && !dvmIsAbstractMethod(method))
810             return -2;
811         return -1;      /* can happen for abstract method stub */
812     }
813
814     LineNumFromPcContext context;
815     memset(&context, 0, sizeof(context));
816     context.address = relPc;
817     // A method with no line number info should return -1
818     context.lineNum = -1;
819
820     dexDecodeDebugInfo(method->clazz->pDvmDex->pDexFile, pDexCode,
821             method->clazz->descriptor,
822             method->prototype.protoIdx,
823             method->accessFlags,
824             lineNumForPcCb, NULL, &context);
825
826     return context.lineNum;
827 }
828
829 /*
830  * Compute the frame depth.
831  *
832  * Excludes "break" frames.
833  */
834 int dvmComputeExactFrameDepth(const void* fp)
835 {
836     int count = 0;
837
838     for ( ; fp != NULL; fp = SAVEAREA_FROM_FP(fp)->prevFrame) {
839         if (!dvmIsBreakFrame(fp))
840             count++;
841     }
842
843     return count;
844 }
845
846 /*
847  * Compute the "vague" frame depth, which is just a pointer subtraction.
848  * The result is NOT an overly generous assessment of the number of
849  * frames; the only meaningful use is to compare against the result of
850  * an earlier invocation.
851  *
852  * Useful for implementing single-step debugger modes, which may need to
853  * call this for every instruction.
854  */
855 int dvmComputeVagueFrameDepth(Thread* thread, const void* fp)
856 {
857     const u1* interpStackStart = thread->interpStackStart;
858
859     assert((u1*) fp >= interpStackStart - thread->interpStackSize);
860     assert((u1*) fp < interpStackStart);
861     return interpStackStart - (u1*) fp;
862 }
863
864 /*
865  * Get the calling frame.  Pass in the current fp.
866  *
867  * Skip "break" frames and reflection invoke frames.
868  */
869 void* dvmGetCallerFP(const void* curFrame)
870 {
871     void* caller = SAVEAREA_FROM_FP(curFrame)->prevFrame;
872     StackSaveArea* saveArea;
873
874 retry:
875     if (dvmIsBreakFrame(caller)) {
876         /* pop up one more */
877         caller = SAVEAREA_FROM_FP(caller)->prevFrame;
878         if (caller == NULL)
879             return NULL;        /* hit the top */
880
881         /*
882          * If we got here by java.lang.reflect.Method.invoke(), we don't
883          * want to return Method's class loader.  Shift up one and try
884          * again.
885          */
886         saveArea = SAVEAREA_FROM_FP(caller);
887         if (dvmIsReflectionMethod(saveArea->method)) {
888             caller = saveArea->prevFrame;
889             assert(caller != NULL);
890             goto retry;
891         }
892     }
893
894     return caller;
895 }
896
897 /*
898  * Get the caller's class.  Pass in the current fp.
899  *
900  * This is used by e.g. java.lang.Class.
901  */
902 ClassObject* dvmGetCallerClass(const void* curFrame)
903 {
904     void* caller;
905
906     caller = dvmGetCallerFP(curFrame);
907     if (caller == NULL)
908         return NULL;
909
910     return SAVEAREA_FROM_FP(caller)->method->clazz;
911 }
912
913 /*
914  * Get the caller's caller's class.  Pass in the current fp.
915  *
916  * This is used by e.g. java.lang.Class, which wants to know about the
917  * class loader of the method that called it.
918  */
919 ClassObject* dvmGetCaller2Class(const void* curFrame)
920 {
921     void* caller = SAVEAREA_FROM_FP(curFrame)->prevFrame;
922     void* callerCaller;
923
924     /* at the top? */
925     if (dvmIsBreakFrame(caller) && SAVEAREA_FROM_FP(caller)->prevFrame == NULL)
926         return NULL;
927
928     /* go one more */
929     callerCaller = dvmGetCallerFP(caller);
930     if (callerCaller == NULL)
931         return NULL;
932
933     return SAVEAREA_FROM_FP(callerCaller)->method->clazz;
934 }
935
936 /*
937  * Get the caller's caller's caller's class.  Pass in the current fp.
938  *
939  * This is used by e.g. java.lang.Class, which wants to know about the
940  * class loader of the method that called it.
941  */
942 ClassObject* dvmGetCaller3Class(const void* curFrame)
943 {
944     void* caller = SAVEAREA_FROM_FP(curFrame)->prevFrame;
945     int i;
946
947     /* at the top? */
948     if (dvmIsBreakFrame(caller) && SAVEAREA_FROM_FP(caller)->prevFrame == NULL)
949         return NULL;
950
951     /* Walk up two frames if possible. */
952     for (i = 0; i < 2; i++) {
953         caller = dvmGetCallerFP(caller);
954         if (caller == NULL)
955             return NULL;
956     }
957
958     return SAVEAREA_FROM_FP(caller)->method->clazz;
959 }
960
961 /*
962  * Create a flat array of methods that comprise the current interpreter
963  * stack trace.  Pass in the current frame ptr.
964  *
965  * Allocates a new array and fills it with method pointers.  Break frames
966  * are skipped, but reflection invocations are not.  The caller must free
967  * "*pArray".
968  *
969  * The current frame will be in element 0.
970  *
971  * Returns "true" on success, "false" on failure (e.g. malloc failed).
972  */
973 bool dvmCreateStackTraceArray(const void* fp, const Method*** pArray,
974     int* pLength)
975 {
976     const Method** array;
977     int idx, depth;
978
979     depth = dvmComputeExactFrameDepth(fp);
980     array = (const Method**) malloc(depth * sizeof(Method*));
981     if (array == NULL)
982         return false;
983
984     for (idx = 0; fp != NULL; fp = SAVEAREA_FROM_FP(fp)->prevFrame) {
985         if (!dvmIsBreakFrame(fp))
986             array[idx++] = SAVEAREA_FROM_FP(fp)->method;
987     }
988     assert(idx == depth);
989
990     *pArray = array;
991     *pLength = depth;
992     return true;
993 }
994
995 /*
996  * Open up the reserved area and throw an exception.  The reserved area
997  * should only be needed to create and initialize the exception itself.
998  *
999  * If we already opened it and we're continuing to overflow, abort the VM.
1000  *
1001  * We have to leave the "reserved" area open until the "catch" handler has
1002  * finished doing its processing.  This is because the catch handler may
1003  * need to resolve classes, which requires calling into the class loader if
1004  * the classes aren't already in the "initiating loader" list.
1005  */
1006 void dvmHandleStackOverflow(Thread* self, const Method* method)
1007 {
1008     /*
1009      * Can we make the reserved area available?
1010      */
1011     if (self->stackOverflowed) {
1012         /*
1013          * Already did, nothing to do but bail.
1014          */
1015         LOGE("DalvikVM: double-overflow of stack in threadid=%d; aborting\n",
1016             self->threadId);
1017         dvmDumpThread(self, false);
1018         dvmAbort();
1019     }
1020
1021     /* open it up to the full range */
1022     LOGI("threadid=%d: stack overflow on call to %s.%s:%s\n",
1023         self->threadId,
1024         method->clazz->descriptor, method->name, method->shorty);
1025     StackSaveArea* saveArea = SAVEAREA_FROM_FP(self->curFrame);
1026     LOGI("  method requires %d+%d+%d=%d bytes, fp is %p (%d left)\n",
1027         method->registersSize * 4, sizeof(StackSaveArea), method->outsSize * 4,
1028         (method->registersSize + method->outsSize) * 4 + sizeof(StackSaveArea),
1029         saveArea, (u1*) saveArea - self->interpStackEnd);
1030     LOGI("  expanding stack end (%p to %p)\n", self->interpStackEnd,
1031         self->interpStackStart - self->interpStackSize);
1032     //dvmDumpThread(self, false);
1033     self->interpStackEnd = self->interpStackStart - self->interpStackSize;
1034     self->stackOverflowed = true;
1035
1036     /*
1037      * If we were trying to throw an exception when the stack overflowed,
1038      * we will blow up when doing the class lookup on StackOverflowError
1039      * because of the pending exception.  So, we clear it and make it
1040      * the cause of the SOE.
1041      */
1042     Object* excep = dvmGetException(self);
1043     if (excep != NULL) {
1044         LOGW("Stack overflow while throwing exception\n");
1045         dvmClearException(self);
1046     }
1047     dvmThrowChainedExceptionByClass(gDvm.classJavaLangStackOverflowError,
1048         NULL, excep);
1049 }
1050
1051 /*
1052  * Reduce the available stack size.  By this point we should have finished
1053  * our overflow processing.
1054  */
1055 void dvmCleanupStackOverflow(Thread* self, const Object* exception)
1056 {
1057     const u1* newStackEnd;
1058
1059     assert(self->stackOverflowed);
1060
1061     if (exception->clazz != gDvm.classJavaLangStackOverflowError) {
1062         /* exception caused during SOE, not the SOE itself */
1063         return;
1064     }
1065
1066     newStackEnd = (self->interpStackStart - self->interpStackSize)
1067         + STACK_OVERFLOW_RESERVE;
1068     if ((u1*)self->curFrame <= newStackEnd) {
1069         LOGE("Can't shrink stack: curFrame is in reserved area (%p %p)\n",
1070             self->interpStackEnd, self->curFrame);
1071         dvmDumpThread(self, false);
1072         dvmAbort();
1073     }
1074
1075     self->interpStackEnd = newStackEnd;
1076     self->stackOverflowed = false;
1077
1078     LOGI("Shrank stack (to %p, curFrame is %p)\n", self->interpStackEnd,
1079         self->curFrame);
1080 }
1081
1082
1083 /*
1084  * Extract the object that is the target of a monitor-enter instruction
1085  * in the top stack frame of "thread".
1086  *
1087  * The other thread might be alive, so this has to work carefully.
1088  *
1089  * We assume the thread list lock is currently held.
1090  *
1091  * Returns "true" if we successfully recover the object.  "*pOwner" will
1092  * be NULL if we can't determine the owner for some reason (e.g. race
1093  * condition on ownership transfer).
1094  */
1095 static bool extractMonitorEnterObject(Thread* thread, Object** pLockObj,
1096     Thread** pOwner)
1097 {
1098     void* framePtr = thread->curFrame;
1099
1100     if (framePtr == NULL || dvmIsBreakFrame(framePtr))
1101         return false;
1102
1103     const StackSaveArea* saveArea = SAVEAREA_FROM_FP(framePtr);
1104     const Method* method = saveArea->method;
1105     const u2* currentPc = saveArea->xtra.currentPc;
1106
1107     /* check Method* */
1108     if (!dvmLinearAllocContains(method, sizeof(Method))) {
1109         LOGD("ExtrMon: method %p not valid\n", method);
1110         return false;
1111     }
1112
1113     /* check currentPc */
1114     u4 insnsSize = dvmGetMethodInsnsSize(method);
1115     if (currentPc < method->insns ||
1116         currentPc >= method->insns + insnsSize)
1117     {
1118         LOGD("ExtrMon: insns %p not valid (%p - %p)\n",
1119             currentPc, method->insns, method->insns + insnsSize);
1120         return false;
1121     }
1122
1123     /* check the instruction */
1124     if ((*currentPc & 0xff) != OP_MONITOR_ENTER) {
1125         LOGD("ExtrMon: insn at %p is not monitor-enter (0x%02x)\n",
1126             currentPc, *currentPc & 0xff);
1127         return false;
1128     }
1129
1130     /* get and check the register index */
1131     unsigned int reg = *currentPc >> 8;
1132     if (reg >= method->registersSize) {
1133         LOGD("ExtrMon: invalid register %d (max %d)\n",
1134             reg, method->registersSize);
1135         return false;
1136     }
1137
1138     /* get and check the object in that register */
1139     u4* fp = (u4*) framePtr;
1140     Object* obj = (Object*) fp[reg];
1141     if (!dvmIsValidObject(obj)) {
1142         LOGD("ExtrMon: invalid object %p at %p[%d]\n", obj, fp, reg);
1143         return false;
1144     }
1145     *pLockObj = obj;
1146
1147     /*
1148      * Try to determine the object's lock holder; it's okay if this fails.
1149      *
1150      * We're assuming the thread list lock is already held by this thread.
1151      * If it's not, we may be living dangerously if we have to scan through
1152      * the thread list to find a match.  (The VM will generally be in a
1153      * suspended state when executing here, so this is a minor concern
1154      * unless we're dumping while threads are running, in which case there's
1155      * a good chance of stuff blowing up anyway.)
1156      */
1157     *pOwner = dvmGetObjectLockHolder(obj);
1158
1159     return true;
1160 }
1161
1162 /*
1163  * Dump stack frames, starting from the specified frame and moving down.
1164  *
1165  * Each frame holds a pointer to the currently executing method, and the
1166  * saved program counter from the caller ("previous" frame).  This means
1167  * we don't have the PC for the current method on the stack, which is
1168  * pretty reasonable since it's in the "PC register" for the VM.  Because
1169  * exceptions need to show the correct line number we actually *do* have
1170  * an updated version in the fame's "xtra.currentPc", but it's unreliable.
1171  *
1172  * Note "framePtr" could be NULL in rare circumstances.
1173  */
1174 static void dumpFrames(const DebugOutputTarget* target, void* framePtr,
1175     Thread* thread)
1176 {
1177     const StackSaveArea* saveArea;
1178     const Method* method;
1179     int checkCount = 0;
1180     const u2* currentPc = NULL;
1181     bool first = true;
1182
1183     /*
1184      * The "currentPc" is updated whenever we execute an instruction that
1185      * might throw an exception.  Show it here.
1186      */
1187     if (framePtr != NULL && !dvmIsBreakFrame(framePtr)) {
1188         saveArea = SAVEAREA_FROM_FP(framePtr);
1189
1190         if (saveArea->xtra.currentPc != NULL)
1191             currentPc = saveArea->xtra.currentPc;
1192     }
1193
1194     while (framePtr != NULL) {
1195         saveArea = SAVEAREA_FROM_FP(framePtr);
1196         method = saveArea->method;
1197
1198         if (dvmIsBreakFrame(framePtr)) {
1199             //dvmPrintDebugMessage(target, "  (break frame)\n");
1200         } else {
1201             int relPc;
1202
1203             if (currentPc != NULL)
1204                 relPc = currentPc - saveArea->method->insns;
1205             else
1206                 relPc = -1;
1207
1208             char* className = dvmDescriptorToDot(method->clazz->descriptor);
1209             if (dvmIsNativeMethod(method))
1210                 dvmPrintDebugMessage(target,
1211                     "  at %s.%s(Native Method)\n", className, method->name);
1212             else {
1213                 dvmPrintDebugMessage(target,
1214                     "  at %s.%s(%s:%s%d)\n",
1215                     className, method->name, dvmGetMethodSourceFile(method),
1216                     (relPc >= 0 && first) ? "~" : "",
1217                     relPc < 0 ? -1 : dvmLineNumFromPC(method, relPc));
1218             }
1219             free(className);
1220
1221             if (first) {
1222                 /*
1223                  * Decorate WAIT and MONITOR threads with some detail on
1224                  * the first frame.
1225                  *
1226                  * warning: wait status not stable, even in suspend
1227                  */
1228                 if (thread->status == THREAD_WAIT ||
1229                     thread->status == THREAD_TIMED_WAIT)
1230                 {
1231                     Monitor* mon = thread->waitMonitor;
1232                     Object* obj = dvmGetMonitorObject(mon);
1233                     if (obj != NULL) {
1234                         className = dvmDescriptorToDot(obj->clazz->descriptor);
1235                         dvmPrintDebugMessage(target,
1236                             "  - waiting on <%p> (a %s)\n", obj, className);
1237                         free(className);
1238                     }
1239                 } else if (thread->status == THREAD_MONITOR) {
1240                     Object* obj;
1241                     Thread* owner;
1242                     if (extractMonitorEnterObject(thread, &obj, &owner)) {
1243                         className = dvmDescriptorToDot(obj->clazz->descriptor);
1244                         if (owner != NULL) {
1245                             char* threadName = dvmGetThreadName(owner);
1246                             dvmPrintDebugMessage(target,
1247                                 "  - waiting to lock <%p> (a %s) held by threadid=%d (%s)\n",
1248                                 obj, className, owner->threadId, threadName);
1249                             free(threadName);
1250                         } else {
1251                             dvmPrintDebugMessage(target,
1252                                 "  - waiting to lock <%p> (a %s) held by ???\n",
1253                                 obj, className);
1254                         }
1255                         free(className);
1256                     }
1257                 }
1258             }
1259         }
1260
1261         /*
1262          * Get saved PC for previous frame.  There's no savedPc in a "break"
1263          * frame, because that represents native or interpreted code
1264          * invoked by the VM.  The saved PC is sitting in the "PC register",
1265          * a local variable on the native stack.
1266          */
1267         currentPc = saveArea->savedPc;
1268
1269         first = false;
1270
1271         if (saveArea->prevFrame != NULL && saveArea->prevFrame <= framePtr) {
1272             LOGW("Warning: loop in stack trace at frame %d (%p -> %p)\n",
1273                 checkCount, framePtr, saveArea->prevFrame);
1274             break;
1275         }
1276         framePtr = saveArea->prevFrame;
1277
1278         checkCount++;
1279         if (checkCount > 300) {
1280             dvmPrintDebugMessage(target,
1281                 "  ***** printed %d frames, not showing any more\n",
1282                 checkCount);
1283             break;
1284         }
1285     }
1286     dvmPrintDebugMessage(target, "\n");
1287 }
1288
1289
1290 /*
1291  * Dump the stack for the specified thread.
1292  */
1293 void dvmDumpThreadStack(const DebugOutputTarget* target, Thread* thread)
1294 {
1295     dumpFrames(target, thread->curFrame, thread);
1296 }
1297
1298 /*
1299  * Dump the stack for the specified thread, which is still running.
1300  *
1301  * This is very dangerous, because stack frames are being pushed on and
1302  * popped off, and if the thread exits we'll be looking at freed memory.
1303  * The plan here is to take a snapshot of the stack and then dump that
1304  * to try to minimize the chances of catching it mid-update.  This should
1305  * work reasonably well on a single-CPU system.
1306  *
1307  * There is a small chance that calling here will crash the VM.
1308  */
1309 void dvmDumpRunningThreadStack(const DebugOutputTarget* target, Thread* thread)
1310 {
1311     StackSaveArea* saveArea;
1312     const u1* origStack;
1313     u1* stackCopy = NULL;
1314     int origSize, fpOffset;
1315     void* fp;
1316     int depthLimit = 200;
1317
1318     if (thread == NULL || thread->curFrame == NULL) {
1319         dvmPrintDebugMessage(target,
1320             "DumpRunning: Thread at %p has no curFrame (threadid=%d)\n",
1321             thread, (thread != NULL) ? thread->threadId : 0);
1322         return;
1323     }
1324
1325     /* wait for a full quantum */
1326     sched_yield();
1327
1328     /* copy the info we need, then the stack itself */
1329     origSize = thread->interpStackSize;
1330     origStack = (const u1*) thread->interpStackStart - origSize;
1331     stackCopy = (u1*) malloc(origSize);
1332     fpOffset = (u1*) thread->curFrame - origStack;
1333     memcpy(stackCopy, origStack, origSize);
1334
1335     /*
1336      * Run through the stack and rewrite the "prev" pointers.
1337      */
1338     //LOGI("DR: fpOff=%d (from %p %p)\n",fpOffset, origStack, thread->curFrame);
1339     fp = stackCopy + fpOffset;
1340     while (true) {
1341         int prevOffset;
1342
1343         if (depthLimit-- < 0) {
1344             /* we're probably screwed */
1345             dvmPrintDebugMessage(target, "DumpRunning: depth limit hit\n");
1346             dvmAbort();
1347         }
1348         saveArea = SAVEAREA_FROM_FP(fp);
1349         if (saveArea->prevFrame == NULL)
1350             break;
1351
1352         prevOffset = (u1*) saveArea->prevFrame - origStack;
1353         if (prevOffset < 0 || prevOffset > origSize) {
1354             dvmPrintDebugMessage(target,
1355                 "DumpRunning: bad offset found: %d (from %p %p)\n",
1356                 prevOffset, origStack, saveArea->prevFrame);
1357             saveArea->prevFrame = NULL;
1358             break;
1359         }
1360
1361         saveArea->prevFrame = stackCopy + prevOffset;
1362         fp = saveArea->prevFrame;
1363     }
1364
1365     /*
1366      * We still need to pass the Thread for some monitor wait stuff.
1367      */
1368     dumpFrames(target, stackCopy + fpOffset, thread);
1369     free(stackCopy);
1370 }