| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | */ |
| The Android Open Source Project | 89c1feb | 2008-12-17 18:03:55 -0800 | [diff] [blame] | 16 | |
| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 17 | /* |
| 18 | * Main interpreter entry point and support functions. |
| 19 | * |
| 20 | * The entry point selects the "standard" or "debug" interpreter and |
| 21 | * facilitates switching between them. The standard interpreter may |
| 22 | * use the "fast" or "portable" implementation. |
| 23 | * |
| 24 | * Some debugger support functions are included here. Ideally their |
| 25 | * entire existence would be "#ifdef WITH_DEBUGGER", but we're not that |
| 26 | * aggressive in other parts of the code yet. |
| 27 | */ |
| 28 | #include "Dalvik.h" |
| 29 | #include "interp/InterpDefs.h" |
| 30 | |
| 31 | |
| 32 | /* |
| 33 | * =========================================================================== |
| 34 | * Debugger support |
| 35 | * =========================================================================== |
| 36 | */ |
| 37 | |
| 38 | /* |
| 39 | * Initialize the breakpoint address lookup table when the debugger attaches. |
| 40 | * |
| 41 | * This shouldn't be necessary -- the global area is initially zeroed out, |
| 42 | * and the events should be cleaning up after themselves. |
| 43 | */ |
| 44 | void dvmInitBreakpoints(void) |
| 45 | { |
| 46 | #ifdef WITH_DEBUGGER |
| 47 | memset(gDvm.debugBreakAddr, 0, sizeof(gDvm.debugBreakAddr)); |
| 48 | #else |
| 49 | assert(false); |
| 50 | #endif |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Add an address to the list, putting it in the first non-empty slot. |
| 55 | * |
| 56 | * Sometimes the debugger likes to add two entries for one breakpoint. |
| 57 | * We add two entries here, so that we get the right behavior when it's |
| 58 | * removed twice. |
| 59 | * |
| 60 | * This will only be run from the JDWP thread, and it will happen while |
| 61 | * we are updating the event list, which is synchronized. We're guaranteed |
| 62 | * to be the only one adding entries, and the lock ensures that nobody |
| 63 | * will be trying to remove them while we're in here. |
| 64 | * |
| 65 | * "addr" is the absolute address of the breakpoint bytecode. |
| 66 | */ |
| 67 | void dvmAddBreakAddr(Method* method, int instrOffset) |
| 68 | { |
| 69 | #ifdef WITH_DEBUGGER |
| 70 | const u2* addr = method->insns + instrOffset; |
| 71 | const u2** ptr = gDvm.debugBreakAddr; |
| 72 | int i; |
| 73 | |
| 74 | LOGV("BKP: add %p %s.%s (%s:%d)\n", |
| 75 | addr, method->clazz->descriptor, method->name, |
| 76 | dvmGetMethodSourceFile(method), dvmLineNumFromPC(method, instrOffset)); |
| 77 | |
| 78 | method->debugBreakpointCount++; |
| 79 | for (i = 0; i < MAX_BREAKPOINTS; i++, ptr++) { |
| 80 | if (*ptr == NULL) { |
| 81 | *ptr = addr; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | if (i == MAX_BREAKPOINTS) { |
| 86 | /* no room; size is too small or we're not cleaning up properly */ |
| 87 | LOGE("ERROR: max breakpoints exceeded\n"); |
| 88 | assert(false); |
| 89 | } |
| 90 | #else |
| 91 | assert(false); |
| 92 | #endif |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Remove an address from the list by setting the entry to NULL. |
| 97 | * |
| 98 | * This can be called from the JDWP thread (because the debugger has |
| 99 | * cancelled the breakpoint) or from an event thread (because it's a |
| 100 | * single-shot breakpoint, e.g. "run to line"). We only get here as |
| 101 | * the result of removing an entry from the event list, which is |
| 102 | * synchronized, so it should not be possible for two threads to be |
| 103 | * updating breakpoints at the same time. |
| 104 | */ |
| 105 | void dvmClearBreakAddr(Method* method, int instrOffset) |
| 106 | { |
| 107 | #ifdef WITH_DEBUGGER |
| 108 | const u2* addr = method->insns + instrOffset; |
| 109 | const u2** ptr = gDvm.debugBreakAddr; |
| 110 | int i; |
| 111 | |
| 112 | LOGV("BKP: clear %p %s.%s (%s:%d)\n", |
| 113 | addr, method->clazz->descriptor, method->name, |
| 114 | dvmGetMethodSourceFile(method), dvmLineNumFromPC(method, instrOffset)); |
| 115 | |
| 116 | method->debugBreakpointCount--; |
| 117 | assert(method->debugBreakpointCount >= 0); |
| 118 | for (i = 0; i < MAX_BREAKPOINTS; i++, ptr++) { |
| 119 | if (*ptr == addr) { |
| 120 | *ptr = NULL; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | if (i == MAX_BREAKPOINTS) { |
| 125 | /* didn't find it */ |
| 126 | LOGE("ERROR: breakpoint on %p not found\n", addr); |
| 127 | assert(false); |
| 128 | } |
| 129 | #else |
| 130 | assert(false); |
| 131 | #endif |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Add a single step event. Currently this is a global item. |
| 136 | * |
| 137 | * We set up some initial values based on the thread's current state. This |
| 138 | * won't work well if the thread is running, so it's up to the caller to |
| 139 | * verify that it's suspended. |
| 140 | * |
| 141 | * This is only called from the JDWP thread. |
| 142 | */ |
| 143 | bool dvmAddSingleStep(Thread* thread, int size, int depth) |
| 144 | { |
| 145 | #ifdef WITH_DEBUGGER |
| 146 | StepControl* pCtrl = &gDvm.stepControl; |
| 147 | |
| 148 | if (pCtrl->active && thread != pCtrl->thread) { |
| 149 | LOGW("WARNING: single-step active for %p; adding %p\n", |
| 150 | pCtrl->thread, thread); |
| 151 | |
| 152 | /* |
| 153 | * Keep going, overwriting previous. This can happen if you |
| 154 | * suspend a thread in Object.wait, hit the single-step key, then |
| 155 | * switch to another thread and do the same thing again. |
| 156 | * The first thread's step is still pending. |
| 157 | * |
| 158 | * TODO: consider making single-step per-thread. Adds to the |
| 159 | * overhead, but could be useful in rare situations. |
| 160 | */ |
| 161 | } |
| 162 | |
| 163 | pCtrl->size = size; |
| 164 | pCtrl->depth = depth; |
| 165 | pCtrl->thread = thread; |
| 166 | |
| 167 | /* |
| 168 | * We may be stepping into or over method calls, or running until we |
| 169 | * return from the current method. To make this work we need to track |
| 170 | * the current line, current method, and current stack depth. We need |
| 171 | * to be checking these after most instructions, notably those that |
| 172 | * call methods, return from methods, or are on a different line from the |
| 173 | * previous instruction. |
| 174 | * |
| 175 | * We have to start with a snapshot of the current state. If we're in |
| 176 | * an interpreted method, everything we need is in the current frame. If |
| 177 | * we're in a native method, possibly with some extra JNI frames pushed |
| 178 | * on by PushLocalFrame, we want to use the topmost native method. |
| 179 | */ |
| 180 | const StackSaveArea* saveArea; |
| 181 | void* fp; |
| 182 | void* prevFp = NULL; |
| 183 | |
| 184 | for (fp = thread->curFrame; fp != NULL; fp = saveArea->prevFrame) { |
| 185 | const Method* method; |
| 186 | |
| 187 | saveArea = SAVEAREA_FROM_FP(fp); |
| 188 | method = saveArea->method; |
| 189 | |
| 190 | if (!dvmIsBreakFrame(fp) && !dvmIsNativeMethod(method)) |
| 191 | break; |
| 192 | prevFp = fp; |
| 193 | } |
| 194 | if (fp == NULL) { |
| 195 | LOGW("Unexpected: step req in native-only threadid=%d\n", |
| 196 | thread->threadId); |
| 197 | return false; |
| 198 | } |
| 199 | if (prevFp != NULL) { |
| 200 | /* |
| 201 | * First interpreted frame wasn't the one at the bottom. Break |
| 202 | * frames are only inserted when calling from native->interp, so we |
| 203 | * don't need to worry about one being here. |
| 204 | */ |
| 205 | LOGV("##### init step while in native method\n"); |
| 206 | fp = prevFp; |
| 207 | assert(!dvmIsBreakFrame(fp)); |
| 208 | assert(dvmIsNativeMethod(SAVEAREA_FROM_FP(fp)->method)); |
| 209 | saveArea = SAVEAREA_FROM_FP(fp); |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Pull the goodies out. "xtra.currentPc" should be accurate since |
| 214 | * we update it on every instruction while the debugger is connected. |
| 215 | */ |
| 216 | pCtrl->method = saveArea->method; |
| 217 | // Clear out any old address set |
| 218 | if (pCtrl->pAddressSet != NULL) { |
| 219 | // (discard const) |
| 220 | free((void *)pCtrl->pAddressSet); |
| 221 | pCtrl->pAddressSet = NULL; |
| 222 | } |
| 223 | if (dvmIsNativeMethod(pCtrl->method)) { |
| 224 | pCtrl->line = -1; |
| 225 | } else { |
| 226 | pCtrl->line = dvmLineNumFromPC(saveArea->method, |
| 227 | saveArea->xtra.currentPc - saveArea->method->insns); |
| 228 | pCtrl->pAddressSet |
| 229 | = dvmAddressSetForLine(saveArea->method, pCtrl->line); |
| 230 | } |
| 231 | pCtrl->frameDepth = dvmComputeVagueFrameDepth(thread, thread->curFrame); |
| 232 | pCtrl->active = true; |
| 233 | |
| 234 | LOGV("##### step init: thread=%p meth=%p '%s' line=%d frameDepth=%d depth=%s size=%s\n", |
| 235 | pCtrl->thread, pCtrl->method, pCtrl->method->name, |
| 236 | pCtrl->line, pCtrl->frameDepth, |
| 237 | dvmJdwpStepDepthStr(pCtrl->depth), |
| 238 | dvmJdwpStepSizeStr(pCtrl->size)); |
| 239 | |
| 240 | return true; |
| 241 | #else |
| 242 | assert(false); |
| 243 | return false; |
| 244 | #endif |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Disable a single step event. |
| 249 | */ |
| 250 | void dvmClearSingleStep(Thread* thread) |
| 251 | { |
| 252 | #ifdef WITH_DEBUGGER |
| 253 | UNUSED_PARAMETER(thread); |
| 254 | |
| 255 | gDvm.stepControl.active = false; |
| 256 | #else |
| 257 | assert(false); |
| 258 | #endif |
| 259 | } |
| 260 | |
| 261 | |
| 262 | /* |
| 263 | * Recover the "this" pointer from the current interpreted method. "this" |
| 264 | * is always in "in0" for non-static methods. |
| 265 | * |
| 266 | * The "ins" start at (#of registers - #of ins). Note in0 != v0. |
| 267 | * |
| 268 | * This works because "dx" guarantees that it will work. It's probably |
| 269 | * fairly common to have a virtual method that doesn't use its "this" |
| 270 | * pointer, in which case we're potentially wasting a register. However, |
| 271 | * the debugger doesn't treat "this" as just another argument. For |
| 272 | * example, events (such as breakpoints) can be enabled for specific |
| 273 | * values of "this". There is also a separate StackFrame.ThisObject call |
| 274 | * in JDWP that is expected to work for any non-native non-static method. |
| 275 | * |
| 276 | * Because we need it when setting up debugger event filters, we want to |
| 277 | * be able to do this quickly. |
| 278 | */ |
| 279 | Object* dvmGetThisPtr(const Method* method, const u4* fp) |
| 280 | { |
| 281 | if (dvmIsStaticMethod(method)) |
| 282 | return NULL; |
| 283 | return (Object*)fp[method->registersSize - method->insSize]; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | #if defined(WITH_TRACKREF_CHECKS) |
| 288 | /* |
| 289 | * Verify that all internally-tracked references have been released. If |
| 290 | * they haven't, print them and abort the VM. |
| 291 | * |
| 292 | * "debugTrackedRefStart" indicates how many refs were on the list when |
| 293 | * we were first invoked. |
| 294 | */ |
| 295 | void dvmInterpCheckTrackedRefs(Thread* self, const Method* method, |
| 296 | int debugTrackedRefStart) |
| 297 | { |
| 298 | if (dvmReferenceTableEntries(&self->internalLocalRefTable) |
| 299 | != (size_t) debugTrackedRefStart) |
| 300 | { |
| 301 | char* desc; |
| 302 | Object** top; |
| 303 | int count; |
| 304 | |
| 305 | count = dvmReferenceTableEntries(&self->internalLocalRefTable); |
| 306 | |
| 307 | LOGE("TRACK: unreleased internal reference (prev=%d total=%d)\n", |
| 308 | debugTrackedRefStart, count); |
| 309 | desc = dexProtoCopyMethodDescriptor(&method->prototype); |
| 310 | LOGE(" current method is %s.%s %s\n", method->clazz->descriptor, |
| 311 | method->name, desc); |
| 312 | free(desc); |
| 313 | top = self->internalLocalRefTable.table + debugTrackedRefStart; |
| 314 | while (top < self->internalLocalRefTable.nextEntry) { |
| 315 | LOGE(" %p (%s)\n", |
| 316 | *top, |
| 317 | ((*top)->clazz != NULL) ? (*top)->clazz->descriptor : ""); |
| 318 | top++; |
| 319 | } |
| 320 | dvmDumpThread(self, false); |
| 321 | |
| 322 | dvmAbort(); |
| 323 | } |
| 324 | //LOGI("TRACK OK\n"); |
| 325 | } |
| 326 | #endif |
| 327 | |
| 328 | |
| 329 | #ifdef LOG_INSTR |
| 330 | /* |
| 331 | * Dump the v-registers. Sent to the ILOG log tag. |
| 332 | */ |
| 333 | void dvmDumpRegs(const Method* method, const u4* framePtr, bool inOnly) |
| 334 | { |
| 335 | int i, localCount; |
| 336 | |
| 337 | localCount = method->registersSize - method->insSize; |
| 338 | |
| 339 | LOG(LOG_VERBOSE, LOG_TAG"i", "Registers (fp=%p):\n", framePtr); |
| 340 | for (i = method->registersSize-1; i >= 0; i--) { |
| 341 | if (i >= localCount) { |
| 342 | LOG(LOG_VERBOSE, LOG_TAG"i", " v%-2d in%-2d : 0x%08x\n", |
| 343 | i, i-localCount, framePtr[i]); |
| 344 | } else { |
| 345 | if (inOnly) { |
| 346 | LOG(LOG_VERBOSE, LOG_TAG"i", " [...]\n"); |
| 347 | break; |
| 348 | } |
| 349 | const char* name = ""; |
| 350 | int j; |
| 351 | #if 0 // "locals" structure has changed -- need to rewrite this |
| 352 | DexFile* pDexFile = method->clazz->pDexFile; |
| 353 | const DexCode* pDexCode = dvmGetMethodCode(method); |
| 354 | int localsSize = dexGetLocalsSize(pDexFile, pDexCode); |
| 355 | const DexLocal* locals = dvmDexGetLocals(pDexFile, pDexCode); |
| 356 | for (j = 0; j < localsSize, j++) { |
| 357 | if (locals[j].registerNum == (u4) i) { |
| 358 | name = dvmDexStringStr(locals[j].pName); |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | #endif |
| 363 | LOG(LOG_VERBOSE, LOG_TAG"i", " v%-2d : 0x%08x %s\n", |
| 364 | i, framePtr[i], name); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | #endif |
| 369 | |
| 370 | |
| 371 | /* |
| 372 | * =========================================================================== |
| 373 | * Entry point and general support functions |
| 374 | * =========================================================================== |
| 375 | */ |
| 376 | |
| 377 | /* |
| 378 | * Construct an s4 from two consecutive half-words of switch data. |
| 379 | * This needs to check endianness because the DEX optimizer only swaps |
| 380 | * half-words in instruction stream. |
| 381 | * |
| 382 | * "switchData" must be 32-bit aligned. |
| 383 | */ |
| 384 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 385 | static inline s4 s4FromSwitchData(const void* switchData) { |
| 386 | return *(s4*) switchData; |
| 387 | } |
| 388 | #else |
| 389 | static inline s4 s4FromSwitchData(const void* switchData) { |
| 390 | u2* data = switchData; |
| 391 | return data[0] | (((s4) data[1]) << 16); |
| Jay Freeman (saurik) | ffa5c29 | 2008-11-16 13:51:51 +0000 | [diff] [blame] | 392 | } |
| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 393 | #endif |
| 394 | |
| 395 | /* |
| 396 | * Find the matching case. Returns the offset to the handler instructions. |
| 397 | * |
| 398 | * Returns 3 if we don't find a match (it's the size of the packed-switch |
| 399 | * instruction). |
| 400 | */ |
| 401 | s4 dvmInterpHandlePackedSwitch(const u2* switchData, s4 testVal) |
| 402 | { |
| 403 | const int kInstrLen = 3; |
| 404 | u2 size; |
| 405 | s4 firstKey; |
| 406 | const s4* entries; |
| 407 | |
| 408 | /* |
| 409 | * Packed switch data format: |
| 410 | * ushort ident = 0x0100 magic value |
| 411 | * ushort size number of entries in the table |
| 412 | * int first_key first (and lowest) switch case value |
| 413 | * int targets[size] branch targets, relative to switch opcode |
| 414 | * |
| 415 | * Total size is (4+size*2) 16-bit code units. |
| 416 | */ |
| 417 | if (*switchData++ != kPackedSwitchSignature) { |
| 418 | /* should have been caught by verifier */ |
| 419 | dvmThrowException("Ljava/lang/InternalError;", |
| 420 | "bad packed switch magic"); |
| 421 | return kInstrLen; |
| 422 | } |
| 423 | |
| 424 | size = *switchData++; |
| 425 | assert(size > 0); |
| 426 | |
| 427 | firstKey = *switchData++; |
| 428 | firstKey |= (*switchData++) << 16; |
| 429 | |
| 430 | if (testVal < firstKey || testVal >= firstKey + size) { |
| 431 | LOGVV("Value %d not found in switch (%d-%d)\n", |
| 432 | testVal, firstKey, firstKey+size-1); |
| 433 | return kInstrLen; |
| 434 | } |
| 435 | |
| 436 | /* The entries are guaranteed to be aligned on a 32-bit boundary; |
| 437 | * we can treat them as a native int array. |
| 438 | */ |
| 439 | entries = (const s4*) switchData; |
| 440 | assert(((u4)entries & 0x3) == 0); |
| 441 | |
| 442 | assert(testVal - firstKey >= 0 && testVal - firstKey < size); |
| 443 | LOGVV("Value %d found in slot %d (goto 0x%02x)\n", |
| 444 | testVal, testVal - firstKey, |
| 445 | s4FromSwitchData(&entries[testVal - firstKey])); |
| 446 | return s4FromSwitchData(&entries[testVal - firstKey]); |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * Find the matching case. Returns the offset to the handler instructions. |
| 451 | * |
| 452 | * Returns 3 if we don't find a match (it's the size of the sparse-switch |
| 453 | * instruction). |
| 454 | */ |
| 455 | s4 dvmInterpHandleSparseSwitch(const u2* switchData, s4 testVal) |
| 456 | { |
| 457 | const int kInstrLen = 3; |
| 458 | u2 ident, size; |
| 459 | const s4* keys; |
| 460 | const s4* entries; |
| 461 | int i; |
| 462 | |
| 463 | /* |
| 464 | * Sparse switch data format: |
| 465 | * ushort ident = 0x0200 magic value |
| 466 | * ushort size number of entries in the table; > 0 |
| 467 | * int keys[size] keys, sorted low-to-high; 32-bit aligned |
| 468 | * int targets[size] branch targets, relative to switch opcode |
| 469 | * |
| 470 | * Total size is (2+size*4) 16-bit code units. |
| 471 | */ |
| 472 | |
| 473 | if (*switchData++ != kSparseSwitchSignature) { |
| 474 | /* should have been caught by verifier */ |
| 475 | dvmThrowException("Ljava/lang/InternalError;", |
| 476 | "bad sparse switch magic"); |
| 477 | return kInstrLen; |
| 478 | } |
| 479 | |
| 480 | size = *switchData++; |
| 481 | assert(size > 0); |
| 482 | |
| 483 | /* The keys are guaranteed to be aligned on a 32-bit boundary; |
| 484 | * we can treat them as a native int array. |
| 485 | */ |
| 486 | keys = (const s4*) switchData; |
| 487 | assert(((u4)keys & 0x3) == 0); |
| 488 | |
| 489 | /* The entries are guaranteed to be aligned on a 32-bit boundary; |
| 490 | * we can treat them as a native int array. |
| 491 | */ |
| 492 | entries = keys + size; |
| 493 | assert(((u4)entries & 0x3) == 0); |
| 494 | |
| 495 | /* |
| 496 | * Run through the list of keys, which are guaranteed to |
| 497 | * be sorted low-to-high. |
| 498 | * |
| 499 | * Most tables have 3-4 entries. Few have more than 10. A binary |
| 500 | * search here is probably not useful. |
| 501 | */ |
| 502 | for (i = 0; i < size; i++) { |
| 503 | s4 k = s4FromSwitchData(&keys[i]); |
| 504 | if (k == testVal) { |
| 505 | LOGVV("Value %d found in entry %d (goto 0x%02x)\n", |
| 506 | testVal, i, s4FromSwitchData(&entries[i])); |
| 507 | return s4FromSwitchData(&entries[i]); |
| 508 | } else if (k > testVal) { |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | LOGVV("Value %d not found in switch\n", testVal); |
| 514 | return kInstrLen; |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * Fill the array with predefined constant values. |
| 519 | * |
| 520 | * Returns true if job is completed, otherwise false to indicate that |
| 521 | * an exception has been thrown. |
| 522 | */ |
| The Android Open Source Project | 89c1feb | 2008-12-17 18:03:55 -0800 | [diff] [blame] | 523 | bool dvmInterpHandleFillArrayData(ArrayObject* arrayObj, const u2* arrayData) |
| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 524 | { |
| 525 | u2 width; |
| 526 | u4 size; |
| 527 | |
| The Android Open Source Project | 89c1feb | 2008-12-17 18:03:55 -0800 | [diff] [blame] | 528 | if (arrayObj == NULL) { |
| 529 | dvmThrowException("Ljava/lang/NullPointerException;", NULL); |
| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 530 | return false; |
| 531 | } |
| The Android Open Source Project | 89c1feb | 2008-12-17 18:03:55 -0800 | [diff] [blame] | 532 | |
| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 533 | /* |
| 534 | * Array data table format: |
| 535 | * ushort ident = 0x0300 magic value |
| 536 | * ushort width width of each element in the table |
| 537 | * uint size number of elements in the table |
| 538 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 539 | * padding at the end) |
| 540 | * |
| 541 | * Total size is 4+(width * size + 1)/2 16-bit code units. |
| 542 | */ |
| 543 | if (arrayData[0] != kArrayDataSignature) { |
| 544 | dvmThrowException("Ljava/lang/InternalError;", "bad array data magic"); |
| 545 | return false; |
| 546 | } |
| 547 | |
| 548 | width = arrayData[1]; |
| 549 | size = arrayData[2] | (((u4)arrayData[3]) << 16); |
| 550 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 551 | if (size > arrayObj->length) { |
| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 552 | dvmThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", NULL); |
| 553 | return false; |
| 554 | } |
| 555 | memcpy(arrayObj->contents, &arrayData[4], size*width); |
| 556 | return true; |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Find the concrete method that corresponds to "methodIdx". The code in |
| 561 | * "method" is executing invoke-method with "thisClass" as its first argument. |
| 562 | * |
| 563 | * Returns NULL with an exception raised on failure. |
| 564 | */ |
| 565 | Method* dvmInterpFindInterfaceMethod(ClassObject* thisClass, u4 methodIdx, |
| 566 | const Method* method, DvmDex* methodClassDex) |
| 567 | { |
| 568 | Method* absMethod; |
| 569 | Method* methodToCall; |
| 570 | int i, vtableIndex; |
| 571 | |
| 572 | /* |
| 573 | * Resolve the method. This gives us the abstract method from the |
| 574 | * interface class declaration. |
| 575 | */ |
| 576 | absMethod = dvmDexGetResolvedMethod(methodClassDex, methodIdx); |
| 577 | if (absMethod == NULL) { |
| 578 | absMethod = dvmResolveInterfaceMethod(method->clazz, methodIdx); |
| 579 | if (absMethod == NULL) { |
| 580 | LOGV("+ unknown method\n"); |
| 581 | return NULL; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | /* make sure absMethod->methodIndex means what we think it means */ |
| 586 | assert(dvmIsAbstractMethod(absMethod)); |
| 587 | |
| 588 | /* |
| 589 | * Run through the "this" object's iftable. Find the entry for |
| 590 | * absMethod's class, then use absMethod->methodIndex to find |
| 591 | * the method's entry. The value there is the offset into our |
| 592 | * vtable of the actual method to execute. |
| 593 | * |
| 594 | * The verifier does not guarantee that objects stored into |
| 595 | * interface references actually implement the interface, so this |
| 596 | * check cannot be eliminated. |
| 597 | */ |
| 598 | for (i = 0; i < thisClass->iftableCount; i++) { |
| 599 | if (thisClass->iftable[i].clazz == absMethod->clazz) |
| 600 | break; |
| 601 | } |
| 602 | if (i == thisClass->iftableCount) { |
| 603 | /* impossible in verified DEX, need to check for it in unverified */ |
| 604 | dvmThrowException("Ljava/lang/IncompatibleClassChangeError;", |
| 605 | "interface not implemented"); |
| 606 | return NULL; |
| 607 | } |
| 608 | |
| 609 | assert(absMethod->methodIndex < |
| 610 | thisClass->iftable[i].clazz->virtualMethodCount); |
| 611 | |
| 612 | vtableIndex = |
| 613 | thisClass->iftable[i].methodIndexArray[absMethod->methodIndex]; |
| 614 | assert(vtableIndex >= 0 && vtableIndex < thisClass->vtableCount); |
| 615 | methodToCall = thisClass->vtable[vtableIndex]; |
| 616 | |
| 617 | #if 0 |
| 618 | /* this can happen when there's a stale class file */ |
| 619 | if (dvmIsAbstractMethod(methodToCall)) { |
| 620 | dvmThrowException("Ljava/lang/AbstractMethodError;", |
| 621 | "interface method not implemented"); |
| 622 | return NULL; |
| 623 | } |
| 624 | #else |
| 625 | assert(!dvmIsAbstractMethod(methodToCall) || |
| 626 | methodToCall->nativeFunc != NULL); |
| 627 | #endif |
| 628 | |
| 629 | LOGVV("+++ interface=%s.%s concrete=%s.%s\n", |
| 630 | absMethod->clazz->descriptor, absMethod->name, |
| 631 | methodToCall->clazz->descriptor, methodToCall->name); |
| 632 | assert(methodToCall != NULL); |
| 633 | |
| 634 | return methodToCall; |
| 635 | } |
| 636 | |
| 637 | |
| 638 | /* |
| Andy McFadden | 3a1aedb | 2009-05-07 13:30:23 -0700 | [diff] [blame^] | 639 | * Throw an exception for a problem identified by the verifier. |
| 640 | * |
| 641 | * This is used by the invoke-verification-error instruction. It always |
| 642 | * throws an exception. |
| 643 | * |
| 644 | * "kind" indicates the kind of failure encountered by the verifier. The |
| 645 | * meaning of "ref" is kind-specific; it's usually an index to a |
| 646 | * class, field, or method reference. |
| 647 | */ |
| 648 | void dvmThrowVerificationError(const DvmDex* pDvmDex, int kind, int ref) |
| 649 | { |
| 650 | // TODO |
| 651 | dvmThrowException("Ljava/lang/VerifyError;", NULL); |
| 652 | } |
| 653 | |
| 654 | |
| 655 | /* |
| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 656 | * Main interpreter loop entry point. Select "standard" or "debug" |
| 657 | * interpreter and switch between them as required. |
| 658 | * |
| 659 | * This begins executing code at the start of "method". On exit, "pResult" |
| 660 | * holds the return value of the method (or, if "method" returns NULL, it |
| 661 | * holds an undefined value). |
| 662 | * |
| 663 | * The interpreted stack frame, which holds the method arguments, has |
| 664 | * already been set up. |
| 665 | */ |
| 666 | void dvmInterpret(Thread* self, const Method* method, JValue* pResult) |
| 667 | { |
| 668 | InterpState interpState; |
| 669 | bool change; |
| 670 | |
| 671 | #if defined(WITH_TRACKREF_CHECKS) |
| 672 | interpState.debugTrackedRefStart = |
| 673 | dvmReferenceTableEntries(&self->internalLocalRefTable); |
| 674 | #endif |
| 675 | #if defined(WITH_PROFILER) || defined(WITH_DEBUGGER) |
| 676 | interpState.debugIsMethodEntry = true; |
| 677 | #endif |
| 678 | |
| 679 | /* |
| 680 | * Initialize working state. |
| 681 | * |
| 682 | * No need to initialize "retval". |
| 683 | */ |
| 684 | interpState.method = method; |
| 685 | interpState.fp = (u4*) self->curFrame; |
| 686 | interpState.pc = method->insns; |
| 687 | interpState.entryPoint = kInterpEntryInstr; |
| 688 | |
| 689 | if (dvmDebuggerOrProfilerActive()) |
| 690 | interpState.nextMode = INTERP_DBG; |
| 691 | else |
| 692 | interpState.nextMode = INTERP_STD; |
| 693 | |
| 694 | assert(!dvmIsNativeMethod(method)); |
| 695 | |
| 696 | /* |
| 697 | * Make sure the class is ready to go. Shouldn't be possible to get |
| 698 | * here otherwise. |
| 699 | */ |
| 700 | if (method->clazz->status < CLASS_INITIALIZING || |
| 701 | method->clazz->status == CLASS_ERROR) |
| 702 | { |
| 703 | LOGE("ERROR: tried to execute code in unprepared class '%s' (%d)\n", |
| 704 | method->clazz->descriptor, method->clazz->status); |
| 705 | dvmDumpThread(self, false); |
| 706 | dvmAbort(); |
| 707 | } |
| 708 | |
| 709 | typedef bool (*Interpreter)(Thread*, InterpState*); |
| 710 | Interpreter stdInterp; |
| 711 | if (gDvm.executionMode == kExecutionModeInterpFast) |
| 712 | stdInterp = dvmMterpStd; |
| 713 | else |
| 714 | stdInterp = dvmInterpretStd; |
| 715 | |
| 716 | change = true; |
| 717 | while (change) { |
| 718 | switch (interpState.nextMode) { |
| 719 | case INTERP_STD: |
| 720 | LOGVV("threadid=%d: interp STD\n", self->threadId); |
| 721 | change = (*stdInterp)(self, &interpState); |
| 722 | break; |
| 723 | #if defined(WITH_PROFILER) || defined(WITH_DEBUGGER) |
| 724 | case INTERP_DBG: |
| 725 | LOGVV("threadid=%d: interp DBG\n", self->threadId); |
| 726 | change = dvmInterpretDbg(self, &interpState); |
| 727 | break; |
| 728 | #endif |
| 729 | default: |
| 730 | dvmAbort(); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | *pResult = interpState.retval; |
| 735 | } |
| The Android Open Source Project | 89c1feb | 2008-12-17 18:03:55 -0800 | [diff] [blame] | 736 | |