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 instruction utility functions. 19 */ 20 #include "InstrUtils.h" 21 22 #include <stdlib.h> 23 24 25 /* 26 * Generate a table that holds the width of all instructions. 27 * 28 * Standard instructions have positive values, optimizer instructions 29 * have negative values, unimplemented instructions have a width of zero. 30 * 31 * I'm doing it with a giant switch statement because it's easier to 32 * maintain and update than a static table with 256 unadorned integers, 33 * and if we're missing a case gcc emits a "warning: enumeration value not 34 * handled" message. 35 * 36 * (To save space in the binary we could generate a static table with a 37 * command-line utility.) 38 */ 39 InstructionWidth* dexCreateInstrWidthTable(void) 40 { 41 #ifdef __ARM_ARCH_7A__ 42 /* hack to work around mysterious problem on emulator */ 43 LOGD("creating instr width table\n"); 44 #endif 45 InstructionWidth* instrWidth; 46 int i; 47 48 instrWidth = malloc(sizeof(InstructionWidth) * kNumDalvikInstructions); 49 if (instrWidth == NULL) 50 return NULL; 51 52 for (i = 0; i < kNumDalvikInstructions; i++) { 53 OpCode opc = (OpCode) i; 54 int width = 0; 55 56 switch (opc) { 57 case OP_NOP: /* note data for e.g. switch-* encoded "inside" a NOP */ 58 case OP_MOVE: 59 case OP_MOVE_WIDE: 60 case OP_MOVE_OBJECT: 61 case OP_MOVE_RESULT: 62 case OP_MOVE_RESULT_WIDE: 63 case OP_MOVE_RESULT_OBJECT: 64 case OP_MOVE_EXCEPTION: 65 case OP_RETURN_VOID: 66 case OP_RETURN: 67 case OP_RETURN_WIDE: 68 case OP_RETURN_OBJECT: 69 case OP_CONST_4: 70 case OP_MONITOR_ENTER: 71 case OP_MONITOR_EXIT: 72 case OP_ARRAY_LENGTH: 73 case OP_THROW: 74 case OP_GOTO: 75 case OP_NEG_INT: 76 case OP_NOT_INT: 77 case OP_NEG_LONG: 78 case OP_NOT_LONG: 79 case OP_NEG_FLOAT: 80 case OP_NEG_DOUBLE: 81 case OP_INT_TO_LONG: 82 case OP_INT_TO_FLOAT: 83 case OP_INT_TO_DOUBLE: 84 case OP_LONG_TO_INT: 85 case OP_LONG_TO_FLOAT: 86 case OP_LONG_TO_DOUBLE: 87 case OP_FLOAT_TO_INT: 88 case OP_FLOAT_TO_LONG: 89 case OP_FLOAT_TO_DOUBLE: 90 case OP_DOUBLE_TO_INT: 91 case OP_DOUBLE_TO_LONG: 92 case OP_DOUBLE_TO_FLOAT: 93 case OP_INT_TO_BYTE: 94 case OP_INT_TO_CHAR: 95 case OP_INT_TO_SHORT: 96 case OP_ADD_INT_2ADDR: 97 case OP_SUB_INT_2ADDR: 98 case OP_MUL_INT_2ADDR: 99 case OP_DIV_INT_2ADDR: 100 case OP_REM_INT_2ADDR: 101 case OP_AND_INT_2ADDR: 102 case OP_OR_INT_2ADDR: 103 case OP_XOR_INT_2ADDR: 104 case OP_SHL_INT_2ADDR: 105 case OP_SHR_INT_2ADDR: 106 case OP_USHR_INT_2ADDR: 107 case OP_ADD_LONG_2ADDR: 108 case OP_SUB_LONG_2ADDR: 109 case OP_MUL_LONG_2ADDR: 110 case OP_DIV_LONG_2ADDR: 111 case OP_REM_LONG_2ADDR: 112 case OP_AND_LONG_2ADDR: 113 case OP_OR_LONG_2ADDR: 114 case OP_XOR_LONG_2ADDR: 115 case OP_SHL_LONG_2ADDR: 116 case OP_SHR_LONG_2ADDR: 117 case OP_USHR_LONG_2ADDR: 118 case OP_ADD_FLOAT_2ADDR: 119 case OP_SUB_FLOAT_2ADDR: 120 case OP_MUL_FLOAT_2ADDR: 121 case OP_DIV_FLOAT_2ADDR: 122 case OP_REM_FLOAT_2ADDR: 123 case OP_ADD_DOUBLE_2ADDR: 124 case OP_SUB_DOUBLE_2ADDR: 125 case OP_MUL_DOUBLE_2ADDR: 126 case OP_DIV_DOUBLE_2ADDR: 127 case OP_REM_DOUBLE_2ADDR: 128 width = 1; 129 break; 130 131 case OP_MOVE_FROM16: 132 case OP_MOVE_WIDE_FROM16: 133 case OP_MOVE_OBJECT_FROM16: 134 case OP_CONST_16: 135 case OP_CONST_HIGH16: 136 case OP_CONST_WIDE_16: 137 case OP_CONST_WIDE_HIGH16: 138 case OP_CONST_STRING: 139 case OP_CONST_CLASS: 140 case OP_CHECK_CAST: 141 case OP_INSTANCE_OF: 142 case OP_NEW_INSTANCE: 143 case OP_NEW_ARRAY: 144 case OP_CMPL_FLOAT: 145 case OP_CMPG_FLOAT: 146 case OP_CMPL_DOUBLE: 147 case OP_CMPG_DOUBLE: 148 case OP_CMP_LONG: 149 case OP_GOTO_16: 150 case OP_IF_EQ: 151 case OP_IF_NE: 152 case OP_IF_LT: 153 case OP_IF_GE: 154 case OP_IF_GT: 155 case OP_IF_LE: 156 case OP_IF_EQZ: 157 case OP_IF_NEZ: 158 case OP_IF_LTZ: 159 case OP_IF_GEZ: 160 case OP_IF_GTZ: 161 case OP_IF_LEZ: 162 case OP_AGET: 163 case OP_AGET_WIDE: 164 case OP_AGET_OBJECT: 165 case OP_AGET_BOOLEAN: 166 case OP_AGET_BYTE: 167 case OP_AGET_CHAR: 168 case OP_AGET_SHORT: 169 case OP_APUT: 170 case OP_APUT_WIDE: 171 case OP_APUT_OBJECT: 172 case OP_APUT_BOOLEAN: 173 case OP_APUT_BYTE: 174 case OP_APUT_CHAR: 175 case OP_APUT_SHORT: 176 case OP_IGET: 177 case OP_IGET_WIDE: 178 case OP_IGET_OBJECT: 179 case OP_IGET_BOOLEAN: 180 case OP_IGET_BYTE: 181 case OP_IGET_CHAR: 182 case OP_IGET_SHORT: 183 case OP_IPUT: 184 case OP_IPUT_WIDE: 185 case OP_IPUT_OBJECT: 186 case OP_IPUT_BOOLEAN: 187 case OP_IPUT_BYTE: 188 case OP_IPUT_CHAR: 189 case OP_IPUT_SHORT: 190 case OP_SGET: 191 case OP_SGET_WIDE: 192 case OP_SGET_OBJECT: 193 case OP_SGET_BOOLEAN: 194 case OP_SGET_BYTE: 195 case OP_SGET_CHAR: 196 case OP_SGET_SHORT: 197 case OP_SPUT: 198 case OP_SPUT_WIDE: 199 case OP_SPUT_OBJECT: 200 case OP_SPUT_BOOLEAN: 201 case OP_SPUT_BYTE: 202 case OP_SPUT_CHAR: 203 case OP_SPUT_SHORT: 204 case OP_ADD_INT: 205 case OP_SUB_INT: 206 case OP_MUL_INT: 207 case OP_DIV_INT: 208 case OP_REM_INT: 209 case OP_AND_INT: 210 case OP_OR_INT: 211 case OP_XOR_INT: 212 case OP_SHL_INT: 213 case OP_SHR_INT: 214 case OP_USHR_INT: 215 case OP_ADD_LONG: 216 case OP_SUB_LONG: 217 case OP_MUL_LONG: 218 case OP_DIV_LONG: 219 case OP_REM_LONG: 220 case OP_AND_LONG: 221 case OP_OR_LONG: 222 case OP_XOR_LONG: 223 case OP_SHL_LONG: 224 case OP_SHR_LONG: 225 case OP_USHR_LONG: 226 case OP_ADD_FLOAT: 227 case OP_SUB_FLOAT: 228 case OP_MUL_FLOAT: 229 case OP_DIV_FLOAT: 230 case OP_REM_FLOAT: 231 case OP_ADD_DOUBLE: 232 case OP_SUB_DOUBLE: 233 case OP_MUL_DOUBLE: 234 case OP_DIV_DOUBLE: 235 case OP_REM_DOUBLE: 236 case OP_ADD_INT_LIT16: 237 case OP_RSUB_INT: 238 case OP_MUL_INT_LIT16: 239 case OP_DIV_INT_LIT16: 240 case OP_REM_INT_LIT16: 241 case OP_AND_INT_LIT16: 242 case OP_OR_INT_LIT16: 243 case OP_XOR_INT_LIT16: 244 case OP_ADD_INT_LIT8: 245 case OP_RSUB_INT_LIT8: 246 case OP_MUL_INT_LIT8: 247 case OP_DIV_INT_LIT8: 248 case OP_REM_INT_LIT8: 249 case OP_AND_INT_LIT8: 250 case OP_OR_INT_LIT8: 251 case OP_XOR_INT_LIT8: 252 case OP_SHL_INT_LIT8: 253 case OP_SHR_INT_LIT8: 254 case OP_USHR_INT_LIT8: 255 width = 2; 256 break; 257 258 case OP_MOVE_16: 259 case OP_MOVE_WIDE_16: 260 case OP_MOVE_OBJECT_16: 261 case OP_CONST: 262 case OP_CONST_WIDE_32: 263 case OP_CONST_STRING_JUMBO: 264 case OP_GOTO_32: 265 case OP_FILLED_NEW_ARRAY: 266 case OP_FILLED_NEW_ARRAY_RANGE: 267 case OP_FILL_ARRAY_DATA: 268 case OP_PACKED_SWITCH: 269 case OP_SPARSE_SWITCH: 270 case OP_INVOKE_VIRTUAL: 271 case OP_INVOKE_SUPER: 272 case OP_INVOKE_DIRECT: 273 case OP_INVOKE_STATIC: 274 case OP_INVOKE_INTERFACE: 275 case OP_INVOKE_VIRTUAL_RANGE: 276 case OP_INVOKE_SUPER_RANGE: 277 case OP_INVOKE_DIRECT_RANGE: 278 case OP_INVOKE_STATIC_RANGE: 279 case OP_INVOKE_INTERFACE_RANGE: 280 width = 3; 281 break; 282 283 case OP_CONST_WIDE: 284 width = 5; 285 break; 286 287 /* 288 * Optimized instructions. We return negative size values for these 289 * to distinguish them. 290 */ 291 case OP_IGET_QUICK: 292 case OP_IGET_WIDE_QUICK: 293 case OP_IGET_OBJECT_QUICK: 294 case OP_IPUT_QUICK: 295 case OP_IPUT_WIDE_QUICK: 296 case OP_IPUT_OBJECT_QUICK: 297 case OP_THROW_VERIFICATION_ERROR: 298 width = -2; 299 break; 300 case OP_INVOKE_VIRTUAL_QUICK: 301 case OP_INVOKE_VIRTUAL_QUICK_RANGE: 302 case OP_INVOKE_SUPER_QUICK: 303 case OP_INVOKE_SUPER_QUICK_RANGE: 304 case OP_EXECUTE_INLINE: 305 case OP_EXECUTE_INLINE_RANGE: 306 case OP_INVOKE_DIRECT_EMPTY: 307 width = -3; 308 break; 309 310 /* these should never appear when scanning bytecode */ 311 case OP_UNUSED_3E: 312 case OP_UNUSED_3F: 313 case OP_UNUSED_40: 314 case OP_UNUSED_41: 315 case OP_UNUSED_42: 316 case OP_UNUSED_43: 317 case OP_UNUSED_73: 318 case OP_UNUSED_79: 319 case OP_UNUSED_7A: 320 case OP_UNUSED_E3: 321 case OP_UNUSED_E4: 322 case OP_UNUSED_E5: 323 case OP_UNUSED_E6: 324 case OP_UNUSED_E7: 325 case OP_UNUSED_E8: 326 case OP_UNUSED_E9: 327 case OP_UNUSED_EA: 328 case OP_UNUSED_EB: 329 case OP_BREAKPOINT: 330 case OP_UNUSED_F1: 331 case OP_UNUSED_FC: 332 case OP_UNUSED_FD: 333 case OP_UNUSED_FE: 334 case OP_UNUSED_FF: 335 assert(width == 0); 336 break; 337 338 /* 339 * DO NOT add a "default" clause here. Without it the compiler will 340 * complain if an instruction is missing (which is desirable). 341 */ 342 } 343 344 instrWidth[opc] = width; 345 } 346 347 return instrWidth; 348 } 349 350 /* 351 * Generate a table that holds instruction flags. 352 */ 353 InstructionFlags* dexCreateInstrFlagsTable(void) 354 { 355 InstructionFlags* instrFlags; 356 int i; 357 358 instrFlags = malloc(sizeof(InstructionFlags) * kNumDalvikInstructions); 359 if (instrFlags == NULL) 360 return NULL; 361 362 for (i = 0; i < kNumDalvikInstructions; i++) { 363 OpCode opc = (OpCode) i; 364 InstructionFlags flags = 0; 365 366 switch (opc) { 367 /* these don't affect the PC and can't cause an exception */ 368 case OP_NOP: 369 case OP_MOVE: 370 case OP_MOVE_FROM16: 371 case OP_MOVE_16: 372 case OP_MOVE_WIDE: 373 case OP_MOVE_WIDE_FROM16: 374 case OP_MOVE_WIDE_16: 375 case OP_MOVE_OBJECT: 376 case OP_MOVE_OBJECT_FROM16: 377 case OP_MOVE_OBJECT_16: 378 case OP_MOVE_RESULT: 379 case OP_MOVE_RESULT_WIDE: 380 case OP_MOVE_RESULT_OBJECT: 381 case OP_MOVE_EXCEPTION: 382 case OP_CONST_4: 383 case OP_CONST_16: 384 case OP_CONST: 385 case OP_CONST_HIGH16: 386 case OP_CONST_WIDE_16: 387 case OP_CONST_WIDE_32: 388 case OP_CONST_WIDE: 389 case OP_CONST_WIDE_HIGH16: 390 case OP_FILL_ARRAY_DATA: 391 case OP_CMPL_FLOAT: 392 case OP_CMPG_FLOAT: 393 case OP_CMPL_DOUBLE: 394 case OP_CMPG_DOUBLE: 395 case OP_CMP_LONG: 396 case OP_NEG_INT: 397 case OP_NOT_INT: 398 case OP_NEG_LONG: 399 case OP_NOT_LONG: 400 case OP_NEG_FLOAT: 401 case OP_NEG_DOUBLE: 402 case OP_INT_TO_LONG: 403 case OP_INT_TO_FLOAT: 404 case OP_INT_TO_DOUBLE: 405 case OP_LONG_TO_INT: 406 case OP_LONG_TO_FLOAT: 407 case OP_LONG_TO_DOUBLE: 408 case OP_FLOAT_TO_INT: 409 case OP_FLOAT_TO_LONG: 410 case OP_FLOAT_TO_DOUBLE: 411 case OP_DOUBLE_TO_INT: 412 case OP_DOUBLE_TO_LONG: 413 case OP_DOUBLE_TO_FLOAT: 414 case OP_INT_TO_BYTE: 415 case OP_INT_TO_CHAR: 416 case OP_INT_TO_SHORT: 417 case OP_ADD_INT: 418 case OP_SUB_INT: 419 case OP_MUL_INT: 420 case OP_AND_INT: 421 case OP_OR_INT: 422 case OP_XOR_INT: 423 case OP_SHL_INT: 424 case OP_SHR_INT: 425 case OP_USHR_INT: 426 case OP_ADD_LONG: 427 case OP_SUB_LONG: 428 case OP_MUL_LONG: 429 case OP_AND_LONG: 430 case OP_OR_LONG: 431 case OP_XOR_LONG: 432 case OP_SHL_LONG: 433 case OP_SHR_LONG: 434 case OP_USHR_LONG: 435 case OP_ADD_FLOAT: 436 case OP_SUB_FLOAT: 437 case OP_MUL_FLOAT: 438 case OP_DIV_FLOAT: 439 case OP_REM_FLOAT: 440 case OP_ADD_DOUBLE: 441 case OP_SUB_DOUBLE: 442 case OP_MUL_DOUBLE: 443 case OP_DIV_DOUBLE: // div by zero just returns NaN 444 case OP_REM_DOUBLE: 445 case OP_ADD_INT_2ADDR: 446 case OP_SUB_INT_2ADDR: 447 case OP_MUL_INT_2ADDR: 448 case OP_AND_INT_2ADDR: 449 case OP_OR_INT_2ADDR: 450 case OP_XOR_INT_2ADDR: 451 case OP_SHL_INT_2ADDR: 452 case OP_SHR_INT_2ADDR: 453 case OP_USHR_INT_2ADDR: 454 case OP_ADD_LONG_2ADDR: 455 case OP_SUB_LONG_2ADDR: 456 case OP_MUL_LONG_2ADDR: 457 case OP_AND_LONG_2ADDR: 458 case OP_OR_LONG_2ADDR: 459 case OP_XOR_LONG_2ADDR: 460 case OP_SHL_LONG_2ADDR: 461 case OP_SHR_LONG_2ADDR: 462 case OP_USHR_LONG_2ADDR: 463 case OP_ADD_FLOAT_2ADDR: 464 case OP_SUB_FLOAT_2ADDR: 465 case OP_MUL_FLOAT_2ADDR: 466 case OP_DIV_FLOAT_2ADDR: 467 case OP_REM_FLOAT_2ADDR: 468 case OP_ADD_DOUBLE_2ADDR: 469 case OP_SUB_DOUBLE_2ADDR: 470 case OP_MUL_DOUBLE_2ADDR: 471 case OP_DIV_DOUBLE_2ADDR: 472 case OP_REM_DOUBLE_2ADDR: 473 case OP_ADD_INT_LIT16: 474 case OP_RSUB_INT: 475 case OP_MUL_INT_LIT16: 476 case OP_AND_INT_LIT16: 477 case OP_OR_INT_LIT16: 478 case OP_XOR_INT_LIT16: 479 case OP_ADD_INT_LIT8: 480 case OP_RSUB_INT_LIT8: 481 case OP_MUL_INT_LIT8: 482 case OP_AND_INT_LIT8: 483 case OP_OR_INT_LIT8: 484 case OP_XOR_INT_LIT8: 485 case OP_SHL_INT_LIT8: 486 case OP_SHR_INT_LIT8: 487 case OP_USHR_INT_LIT8: 488 flags = kInstrCanContinue; 489 break; 490 491 /* these don't affect the PC, but can cause exceptions */ 492 case OP_CONST_STRING: 493 case OP_CONST_STRING_JUMBO: 494 case OP_CONST_CLASS: 495 case OP_MONITOR_ENTER: 496 case OP_MONITOR_EXIT: 497 case OP_CHECK_CAST: 498 case OP_INSTANCE_OF: 499 case OP_ARRAY_LENGTH: 500 case OP_NEW_INSTANCE: 501 case OP_NEW_ARRAY: 502 case OP_FILLED_NEW_ARRAY: 503 case OP_FILLED_NEW_ARRAY_RANGE: 504 case OP_AGET: 505 case OP_AGET_BOOLEAN: 506 case OP_AGET_BYTE: 507 case OP_AGET_CHAR: 508 case OP_AGET_SHORT: 509 case OP_AGET_WIDE: 510 case OP_AGET_OBJECT: 511 case OP_APUT: 512 case OP_APUT_BOOLEAN: 513 case OP_APUT_BYTE: 514 case OP_APUT_CHAR: 515 case OP_APUT_SHORT: 516 case OP_APUT_WIDE: 517 case OP_APUT_OBJECT: 518 case OP_IGET: 519 case OP_IGET_BOOLEAN: 520 case OP_IGET_BYTE: 521 case OP_IGET_CHAR: 522 case OP_IGET_SHORT: 523 case OP_IGET_WIDE: 524 case OP_IGET_OBJECT: 525 case OP_IPUT: 526 case OP_IPUT_BOOLEAN: 527 case OP_IPUT_BYTE: 528 case OP_IPUT_CHAR: 529 case OP_IPUT_SHORT: 530 case OP_IPUT_WIDE: 531 case OP_IPUT_OBJECT: 532 case OP_SGET: 533 case OP_SGET_BOOLEAN: 534 case OP_SGET_BYTE: 535 case OP_SGET_CHAR: 536 case OP_SGET_SHORT: 537 case OP_SGET_WIDE: 538 case OP_SGET_OBJECT: 539 case OP_SPUT: 540 case OP_SPUT_BOOLEAN: 541 case OP_SPUT_BYTE: 542 case OP_SPUT_CHAR: 543 case OP_SPUT_SHORT: 544 case OP_SPUT_WIDE: 545 case OP_SPUT_OBJECT: 546 case OP_DIV_INT: 547 case OP_REM_INT: 548 case OP_DIV_LONG: 549 case OP_REM_LONG: 550 case OP_DIV_INT_2ADDR: 551 case OP_REM_INT_2ADDR: 552 case OP_DIV_LONG_2ADDR: 553 case OP_REM_LONG_2ADDR: 554 case OP_DIV_INT_LIT16: 555 case OP_REM_INT_LIT16: 556 case OP_DIV_INT_LIT8: 557 case OP_REM_INT_LIT8: 558 flags = kInstrCanContinue | kInstrCanThrow; 559 break; 560 561 case OP_INVOKE_VIRTUAL: 562 case OP_INVOKE_VIRTUAL_RANGE: 563 case OP_INVOKE_SUPER: 564 case OP_INVOKE_SUPER_RANGE: 565 case OP_INVOKE_DIRECT: 566 case OP_INVOKE_DIRECT_RANGE: 567 case OP_INVOKE_STATIC: 568 case OP_INVOKE_STATIC_RANGE: 569 case OP_INVOKE_INTERFACE: 570 case OP_INVOKE_INTERFACE_RANGE: 571 flags = kInstrCanContinue | kInstrCanThrow | kInstrInvoke; 572 break; 573 574 case OP_RETURN_VOID: 575 case OP_RETURN: 576 case OP_RETURN_WIDE: 577 case OP_RETURN_OBJECT: 578 flags = kInstrCanReturn; 579 break; 580 581 case OP_THROW: 582 flags = kInstrCanThrow; 583 break; 584 585 /* unconditional branches */ 586 case OP_GOTO: 587 case OP_GOTO_16: 588 case OP_GOTO_32: 589 flags = kInstrCanBranch | kInstrUnconditional; 590 break; 591 592 /* conditional branches */ 593 case OP_IF_EQ: 594 case OP_IF_NE: 595 case OP_IF_LT: 596 case OP_IF_GE: 597 case OP_IF_GT: 598 case OP_IF_LE: 599 case OP_IF_EQZ: 600 case OP_IF_NEZ: 601 case OP_IF_LTZ: 602 case OP_IF_GEZ: 603 case OP_IF_GTZ: 604 case OP_IF_LEZ: 605 flags = kInstrCanBranch | kInstrCanContinue; 606 break; 607 608 /* switch statements; if value not in switch, it continues */ 609 case OP_PACKED_SWITCH: 610 case OP_SPARSE_SWITCH: 611 flags = kInstrCanSwitch | kInstrCanContinue; 612 break; 613 614 /* verifier/optimizer-generated instructions */ 615 case OP_THROW_VERIFICATION_ERROR: 616 flags = kInstrCanThrow; 617 break; 618 case OP_EXECUTE_INLINE: 619 case OP_EXECUTE_INLINE_RANGE: 620 flags = kInstrCanContinue | kInstrCanThrow; 621 break; 622 case OP_IGET_QUICK: 623 case OP_IGET_WIDE_QUICK: 624 case OP_IGET_OBJECT_QUICK: 625 case OP_IPUT_QUICK: 626 case OP_IPUT_WIDE_QUICK: 627 case OP_IPUT_OBJECT_QUICK: 628 flags = kInstrCanContinue | kInstrCanThrow; 629 break; 630 631 case OP_INVOKE_VIRTUAL_QUICK: 632 case OP_INVOKE_VIRTUAL_QUICK_RANGE: 633 case OP_INVOKE_SUPER_QUICK: 634 case OP_INVOKE_SUPER_QUICK_RANGE: 635 case OP_INVOKE_DIRECT_EMPTY: 636 flags = kInstrCanContinue | kInstrCanThrow | kInstrInvoke; 637 break; 638 639 /* these should never appear when scanning code */ 640 case OP_UNUSED_3E: 641 case OP_UNUSED_3F: 642 case OP_UNUSED_40: 643 case OP_UNUSED_41: 644 case OP_UNUSED_42: 645 case OP_UNUSED_43: 646 case OP_UNUSED_73: 647 case OP_UNUSED_79: 648 case OP_UNUSED_7A: 649 case OP_UNUSED_E3: 650 case OP_UNUSED_E4: 651 case OP_UNUSED_E5: 652 case OP_UNUSED_E6: 653 case OP_UNUSED_E7: 654 case OP_UNUSED_E8: 655 case OP_UNUSED_E9: 656 case OP_UNUSED_EA: 657 case OP_UNUSED_EB: 658 case OP_BREAKPOINT: 659 case OP_UNUSED_F1: 660 case OP_UNUSED_FC: 661 case OP_UNUSED_FD: 662 case OP_UNUSED_FE: 663 case OP_UNUSED_FF: 664 break; 665 666 /* 667 * DO NOT add a "default" clause here. Without it the compiler will 668 * complain if an instruction is missing (which is desirable). 669 */ 670 } 671 672 instrFlags[opc] = flags; 673 } 674 675 return instrFlags; 676 } 677 678 /* 679 * Allocate and populate a 256-element array with instruction formats. 680 * Used in conjunction with dexDecodeInstruction. 681 */ 682 InstructionFormat* dexCreateInstrFormatTable(void) 683 { 684 InstructionFormat* instFmt; 685 int i; 686 687 instFmt = malloc(sizeof(InstructionFormat) * kNumDalvikInstructions); 688 if (instFmt == NULL) 689 return NULL; 690 691 for (i = 0; i < kNumDalvikInstructions; i++) { 692 OpCode opc = (OpCode) i; 693 InstructionFormat fmt = kFmtUnknown; 694 695 switch (opc) { 696 case OP_GOTO: 697 fmt = kFmt10t; 698 break; 699 case OP_NOP: 700 case OP_RETURN_VOID: 701 fmt = kFmt10x; 702 break; 703 case OP_CONST_4: 704 fmt = kFmt11n; 705 break; 706 case OP_CONST_HIGH16: 707 case OP_CONST_WIDE_HIGH16: 708 fmt = kFmt21h; 709 break; 710 case OP_MOVE_RESULT: 711 case OP_MOVE_RESULT_WIDE: 712 case OP_MOVE_RESULT_OBJECT: 713 case OP_MOVE_EXCEPTION: 714 case OP_RETURN: 715 case OP_RETURN_WIDE: 716 case OP_RETURN_OBJECT: 717 case OP_MONITOR_ENTER: 718 case OP_MONITOR_EXIT: 719 case OP_THROW: 720 fmt = kFmt11x; 721 break; 722 case OP_MOVE: 723 case OP_MOVE_WIDE: 724 case OP_MOVE_OBJECT: 725 case OP_ARRAY_LENGTH: 726 case OP_NEG_INT: 727 case OP_NOT_INT: 728 case OP_NEG_LONG: 729 case OP_NOT_LONG: 730 case OP_NEG_FLOAT: 731 case OP_NEG_DOUBLE: 732 case OP_INT_TO_LONG: 733 case OP_INT_TO_FLOAT: 734 case OP_INT_TO_DOUBLE: 735 case OP_LONG_TO_INT: 736 case OP_LONG_TO_FLOAT: 737 case OP_LONG_TO_DOUBLE: 738 case OP_FLOAT_TO_INT: 739 case OP_FLOAT_TO_LONG: 740 case OP_FLOAT_TO_DOUBLE: 741 case OP_DOUBLE_TO_INT: 742 case OP_DOUBLE_TO_LONG: 743 case OP_DOUBLE_TO_FLOAT: 744 case OP_INT_TO_BYTE: 745 case OP_INT_TO_CHAR: 746 case OP_INT_TO_SHORT: 747 case OP_ADD_INT_2ADDR: 748 case OP_SUB_INT_2ADDR: 749 case OP_MUL_INT_2ADDR: 750 case OP_DIV_INT_2ADDR: 751 case OP_REM_INT_2ADDR: 752 case OP_AND_INT_2ADDR: 753 case OP_OR_INT_2ADDR: 754 case OP_XOR_INT_2ADDR: 755 case OP_SHL_INT_2ADDR: 756 case OP_SHR_INT_2ADDR: 757 case OP_USHR_INT_2ADDR: 758 case OP_ADD_LONG_2ADDR: 759 case OP_SUB_LONG_2ADDR: 760 case OP_MUL_LONG_2ADDR: 761 case OP_DIV_LONG_2ADDR: 762 case OP_REM_LONG_2ADDR: 763 case OP_AND_LONG_2ADDR: 764 case OP_OR_LONG_2ADDR: 765 case OP_XOR_LONG_2ADDR: 766 case OP_SHL_LONG_2ADDR: 767 case OP_SHR_LONG_2ADDR: 768 case OP_USHR_LONG_2ADDR: 769 case OP_ADD_FLOAT_2ADDR: 770 case OP_SUB_FLOAT_2ADDR: 771 case OP_MUL_FLOAT_2ADDR: 772 case OP_DIV_FLOAT_2ADDR: 773 case OP_REM_FLOAT_2ADDR: 774 case OP_ADD_DOUBLE_2ADDR: 775 case OP_SUB_DOUBLE_2ADDR: 776 case OP_MUL_DOUBLE_2ADDR: 777 case OP_DIV_DOUBLE_2ADDR: 778 case OP_REM_DOUBLE_2ADDR: 779 fmt = kFmt12x; 780 break; 781 case OP_GOTO_16: 782 fmt = kFmt20t; 783 break; 784 case OP_GOTO_32: 785 fmt = kFmt30t; 786 break; 787 case OP_CONST_STRING: 788 case OP_CONST_CLASS: 789 case OP_CHECK_CAST: 790 case OP_NEW_INSTANCE: 791 case OP_SGET: 792 case OP_SGET_WIDE: 793 case OP_SGET_OBJECT: 794 case OP_SGET_BOOLEAN: 795 case OP_SGET_BYTE: 796 case OP_SGET_CHAR: 797 case OP_SGET_SHORT: 798 case OP_SPUT: 799 case OP_SPUT_WIDE: 800 case OP_SPUT_OBJECT: 801 case OP_SPUT_BOOLEAN: 802 case OP_SPUT_BYTE: 803 case OP_SPUT_CHAR: 804 case OP_SPUT_SHORT: 805 fmt = kFmt21c; 806 break; 807 case OP_CONST_16: 808 case OP_CONST_WIDE_16: 809 fmt = kFmt21s; 810 break; 811 case OP_IF_EQZ: 812 case OP_IF_NEZ: 813 case OP_IF_LTZ: 814 case OP_IF_GEZ: 815 case OP_IF_GTZ: 816 case OP_IF_LEZ: 817 fmt = kFmt21t; 818 break; 819 case OP_FILL_ARRAY_DATA: 820 case OP_PACKED_SWITCH: 821 case OP_SPARSE_SWITCH: 822 fmt = kFmt31t; 823 break; 824 case OP_ADD_INT_LIT8: 825 case OP_RSUB_INT_LIT8: 826 case OP_MUL_INT_LIT8: 827 case OP_DIV_INT_LIT8: 828 case OP_REM_INT_LIT8: 829 case OP_AND_INT_LIT8: 830 case OP_OR_INT_LIT8: 831 case OP_XOR_INT_LIT8: 832 case OP_SHL_INT_LIT8: 833 case OP_SHR_INT_LIT8: 834 case OP_USHR_INT_LIT8: 835 fmt = kFmt22b; 836 break; 837 case OP_INSTANCE_OF: 838 case OP_NEW_ARRAY: 839 case OP_IGET: 840 case OP_IGET_WIDE: 841 case OP_IGET_OBJECT: 842 case OP_IGET_BOOLEAN: 843 case OP_IGET_BYTE: 844 case OP_IGET_CHAR: 845 case OP_IGET_SHORT: 846 case OP_IPUT: 847 case OP_IPUT_WIDE: 848 case OP_IPUT_OBJECT: 849 case OP_IPUT_BOOLEAN: 850 case OP_IPUT_BYTE: 851 case OP_IPUT_CHAR: 852 case OP_IPUT_SHORT: 853 fmt = kFmt22c; 854 break; 855 case OP_ADD_INT_LIT16: 856 case OP_RSUB_INT: 857 case OP_MUL_INT_LIT16: 858 case OP_DIV_INT_LIT16: 859 case OP_REM_INT_LIT16: 860 case OP_AND_INT_LIT16: 861 case OP_OR_INT_LIT16: 862 case OP_XOR_INT_LIT16: 863 fmt = kFmt22s; 864 break; 865 case OP_IF_EQ: 866 case OP_IF_NE: 867 case OP_IF_LT: 868 case OP_IF_GE: 869 case OP_IF_GT: 870 case OP_IF_LE: 871 fmt = kFmt22t; 872 break; 873 case OP_MOVE_FROM16: 874 case OP_MOVE_WIDE_FROM16: 875 case OP_MOVE_OBJECT_FROM16: 876 fmt = kFmt22x; 877 break; 878 case OP_CMPL_FLOAT: 879 case OP_CMPG_FLOAT: 880 case OP_CMPL_DOUBLE: 881 case OP_CMPG_DOUBLE: 882 case OP_CMP_LONG: 883 case OP_AGET: 884 case OP_AGET_WIDE: 885 case OP_AGET_OBJECT: 886 case OP_AGET_BOOLEAN: 887 case OP_AGET_BYTE: 888 case OP_AGET_CHAR: 889 case OP_AGET_SHORT: 890 case OP_APUT: 891 case OP_APUT_WIDE: 892 case OP_APUT_OBJECT: 893 case OP_APUT_BOOLEAN: 894 case OP_APUT_BYTE: 895 case OP_APUT_CHAR: 896 case OP_APUT_SHORT: 897 case OP_ADD_INT: 898 case OP_SUB_INT: 899 case OP_MUL_INT: 900 case OP_DIV_INT: 901 case OP_REM_INT: 902 case OP_AND_INT: 903 case OP_OR_INT: 904 case OP_XOR_INT: 905 case OP_SHL_INT: 906 case OP_SHR_INT: 907 case OP_USHR_INT: 908 case OP_ADD_LONG: 909 case OP_SUB_LONG: 910 case OP_MUL_LONG: 911 case OP_DIV_LONG: 912 case OP_REM_LONG: 913 case OP_AND_LONG: 914 case OP_OR_LONG: 915 case OP_XOR_LONG: 916 case OP_SHL_LONG: 917 case OP_SHR_LONG: 918 case OP_USHR_LONG: 919 case OP_ADD_FLOAT: 920 case OP_SUB_FLOAT: 921 case OP_MUL_FLOAT: 922 case OP_DIV_FLOAT: 923 case OP_REM_FLOAT: 924 case OP_ADD_DOUBLE: 925 case OP_SUB_DOUBLE: 926 case OP_MUL_DOUBLE: 927 case OP_DIV_DOUBLE: 928 case OP_REM_DOUBLE: 929 fmt = kFmt23x; 930 break; 931 case OP_CONST: 932 case OP_CONST_WIDE_32: 933 fmt = kFmt31i; 934 break; 935 case OP_CONST_STRING_JUMBO: 936 fmt = kFmt31c; 937 break; 938 case OP_MOVE_16: 939 case OP_MOVE_WIDE_16: 940 case OP_MOVE_OBJECT_16: 941 fmt = kFmt32x; 942 break; 943 case OP_FILLED_NEW_ARRAY: 944 case OP_INVOKE_VIRTUAL: 945 case OP_INVOKE_SUPER: 946 case OP_INVOKE_DIRECT: 947 case OP_INVOKE_STATIC: 948 case OP_INVOKE_INTERFACE: 949 fmt = kFmt35c; 950 break; 951 case OP_FILLED_NEW_ARRAY_RANGE: 952 case OP_INVOKE_VIRTUAL_RANGE: 953 case OP_INVOKE_SUPER_RANGE: 954 case OP_INVOKE_DIRECT_RANGE: 955 case OP_INVOKE_STATIC_RANGE: 956 case OP_INVOKE_INTERFACE_RANGE: 957 fmt = kFmt3rc; 958 break; 959 case OP_CONST_WIDE: 960 fmt = kFmt51l; 961 break; 962 963 /* 964 * Optimized instructions. 965 */ 966 case OP_THROW_VERIFICATION_ERROR: 967 fmt = kFmt20bc; 968 break; 969 case OP_IGET_QUICK: 970 case OP_IGET_WIDE_QUICK: 971 case OP_IGET_OBJECT_QUICK: 972 case OP_IPUT_QUICK: 973 case OP_IPUT_WIDE_QUICK: 974 case OP_IPUT_OBJECT_QUICK: 975 fmt = kFmt22cs; 976 break; 977 case OP_INVOKE_VIRTUAL_QUICK: 978 case OP_INVOKE_SUPER_QUICK: 979 fmt = kFmt35ms; 980 break; 981 case OP_INVOKE_VIRTUAL_QUICK_RANGE: 982 case OP_INVOKE_SUPER_QUICK_RANGE: 983 fmt = kFmt3rms; 984 break; 985 case OP_EXECUTE_INLINE: 986 fmt = kFmt3inline; 987 break; 988 case OP_EXECUTE_INLINE_RANGE: 989 fmt = kFmt3rinline; 990 break; 991 case OP_INVOKE_DIRECT_EMPTY: 992 fmt = kFmt35c; 993 break; 994 995 /* these should never appear when scanning code */ 996 case OP_UNUSED_3E: 997 case OP_UNUSED_3F: 998 case OP_UNUSED_40: 999 case OP_UNUSED_41: 1000 case OP_UNUSED_42: 1001 case OP_UNUSED_43: 1002 case OP_UNUSED_73: 1003 case OP_UNUSED_79: 1004 case OP_UNUSED_7A: 1005 case OP_UNUSED_E3: 1006 case OP_UNUSED_E4: 1007 case OP_UNUSED_E5: 1008 case OP_UNUSED_E6: 1009 case OP_UNUSED_E7: 1010 case OP_UNUSED_E8: 1011 case OP_UNUSED_E9: 1012 case OP_UNUSED_EA: 1013 case OP_UNUSED_EB: 1014 case OP_BREAKPOINT: 1015 case OP_UNUSED_F1: 1016 case OP_UNUSED_FC: 1017 case OP_UNUSED_FD: 1018 case OP_UNUSED_FE: 1019 case OP_UNUSED_FF: 1020 fmt = kFmtUnknown; 1021 break; 1022 1023 /* 1024 * DO NOT add a "default" clause here. Without it the compiler will 1025 * complain if an instruction is missing (which is desirable). 1026 */ 1027 } 1028 1029 instFmt[opc] = fmt; 1030 } 1031 1032 return instFmt; 1033 } 1034 1035 /* 1036 * Copied from InterpCore.h. Used for instruction decoding. 1037 */ 1038 #define FETCH(_offset) (insns[(_offset)]) 1039 #define INST_INST(_inst) ((_inst) & 0xff) 1040 #define INST_A(_inst) (((u2)(_inst) >> 8) & 0x0f) 1041 #define INST_B(_inst) ((u2)(_inst) >> 12) 1042 #define INST_AA(_inst) ((_inst) >> 8) 1043 1044 /* 1045 * Decode the instruction pointed to by "insns". 1046 * 1047 * Fills out the pieces of "pDec" that are affected by the current 1048 * instruction. Does not touch anything else. 1049 */ 1050 void dexDecodeInstruction(const InstructionFormat* fmts, const u2* insns, 1051 DecodedInstruction* pDec) 1052 { 1053 u2 inst = *insns; 1054 1055 pDec->opCode = (OpCode) INST_INST(inst); 1056 1057 switch (dexGetInstrFormat(fmts, pDec->opCode)) { 1058 case kFmt10x: // op 1059 /* nothing to do; copy the AA bits out for the verifier */ 1060 pDec->vA = INST_AA(inst); 1061 break; 1062 case kFmt12x: // op vA, vB 1063 pDec->vA = INST_A(inst); 1064 pDec->vB = INST_B(inst); 1065 break; 1066 case kFmt11n: // op vA, #+B 1067 pDec->vA = INST_A(inst); 1068 pDec->vB = (s4) (INST_B(inst) << 28) >> 28; // sign extend 4-bit value 1069 break; 1070 case kFmt11x: // op vAA 1071 pDec->vA = INST_AA(inst); 1072 break; 1073 case kFmt10t: // op +AA 1074 pDec->vA = (s1) INST_AA(inst); // sign-extend 8-bit value 1075 break; 1076 case kFmt20t: // op +AAAA 1077 pDec->vA = (s2) FETCH(1); // sign-extend 16-bit value 1078 break; 1079 case kFmt20bc: // op AA, thing@BBBB 1080 case kFmt21c: // op vAA, thing@BBBB 1081 case kFmt22x: // op vAA, vBBBB 1082 pDec->vA = INST_AA(inst); 1083 pDec->vB = FETCH(1); 1084 break; 1085 case kFmt21s: // op vAA, #+BBBB 1086 case kFmt21t: // op vAA, +BBBB 1087 pDec->vA = INST_AA(inst); 1088 pDec->vB = (s2) FETCH(1); // sign-extend 16-bit value 1089 break; 1090 case kFmt21h: // op vAA, #+BBBB0000[00000000] 1091 pDec->vA = INST_AA(inst); 1092 /* 1093 * The value should be treated as right-zero-extended, but we don't 1094 * actually do that here. Among other things, we don't know if it's 1095 * the top bits of a 32- or 64-bit value. 1096 */ 1097 pDec->vB = FETCH(1); 1098 break; 1099 case kFmt23x: // op vAA, vBB, vCC 1100 pDec->vA = INST_AA(inst); 1101 pDec->vB = FETCH(1) & 0xff; 1102 pDec->vC = FETCH(1) >> 8; 1103 break; 1104 case kFmt22b: // op vAA, vBB, #+CC 1105 pDec->vA = INST_AA(inst); 1106 pDec->vB = FETCH(1) & 0xff; 1107 pDec->vC = (s1) (FETCH(1) >> 8); // sign-extend 8-bit value 1108 break; 1109 case kFmt22s: // op vA, vB, #+CCCC 1110 case kFmt22t: // op vA, vB, +CCCC 1111 pDec->vA = INST_A(inst); 1112 pDec->vB = INST_B(inst); 1113 pDec->vC = (s2) FETCH(1); // sign-extend 16-bit value 1114 break; 1115 case kFmt22c: // op vA, vB, thing@CCCC 1116 case kFmt22cs: // [opt] op vA, vB, field offset CCCC 1117 pDec->vA = INST_A(inst); 1118 pDec->vB = INST_B(inst); 1119 pDec->vC = FETCH(1); 1120 break; 1121 case kFmt30t: // op +AAAAAAAA 1122 pDec->vA = FETCH(1) | ((u4) FETCH(2) << 16); // signed 32-bit value 1123 break; 1124 case kFmt31t: // op vAA, +BBBBBBBB 1125 case kFmt31c: // op vAA, thing@BBBBBBBB 1126 pDec->vA = INST_AA(inst); 1127 pDec->vB = FETCH(1) | ((u4) FETCH(2) << 16); // 32-bit value 1128 break; 1129 case kFmt32x: // op vAAAA, vBBBB 1130 pDec->vA = FETCH(1); 1131 pDec->vB = FETCH(2); 1132 break; 1133 case kFmt31i: // op vAA, #+BBBBBBBB 1134 pDec->vA = INST_AA(inst); 1135 pDec->vB = FETCH(1) | ((u4) FETCH(2) << 16); 1136 break; 1137 case kFmt35c: // op vB, {vD..vG,vA}, thing@CCCC 1138 case kFmt35ms: // [opt] invoke-virtual+super 1139 { 1140 /* 1141 * The lettering changes that came about when we went from 4 args 1142 * to 5 made the "range" versions of the calls different from 1143 * the non-range versions. We have the choice between decoding 1144 * them the way the spec shows and having lots of conditionals 1145 * in the verifier, or mapping the values onto their original 1146 * registers and leaving the verifier intact. 1147 * 1148 * Current plan is to leave the verifier alone. We can fix it 1149 * later if it's architecturally unbearable. 1150 * 1151 * Bottom line: method constant is always in vB. 1152 */ 1153 u2 regList; 1154 int i, count; 1155 1156 pDec->vA = INST_B(inst); 1157 pDec->vB = FETCH(1); 1158 regList = FETCH(2); 1159 1160 if (pDec->vA > 5) { 1161 LOGW("Invalid arg count in 35c/35ms (%d)\n", pDec->vA); 1162 goto bail; 1163 } 1164 count = pDec->vA; 1165 if (count == 5) { 1166 /* 5th arg comes from A field in instruction */ 1167 pDec->arg[4] = INST_A(inst); 1168 count--; 1169 } 1170 for (i = 0; i < count; i++) { 1171 pDec->arg[i] = regList & 0x0f; 1172 regList >>= 4; 1173 } 1174 /* copy arg[0] to vC; we don't have vD/vE/vF, so ignore those */ 1175 if (pDec->vA > 0) 1176 pDec->vC = pDec->arg[0]; 1177 } 1178 break; 1179 case kFmt3inline: // [opt] inline invoke 1180 { 1181 u2 regList; 1182 int i; 1183 1184 pDec->vA = INST_B(inst); 1185 pDec->vB = FETCH(1); 1186 regList = FETCH(2); 1187 1188 if (pDec->vA > 4) { 1189 LOGW("Invalid arg count in 3inline (%d)\n", pDec->vA); 1190 goto bail; 1191 } 1192 for (i = 0; i < (int) pDec->vA; i++) { 1193 pDec->arg[i] = regList & 0x0f; 1194 regList >>= 4; 1195 } 1196 /* copy arg[0] to vC; we don't have vD/vE/vF, so ignore those */ 1197 if (pDec->vA > 0) 1198 pDec->vC = pDec->arg[0]; 1199 } 1200 break; 1201 case kFmt35fs: // [opt] invoke-interface 1202 assert(false); // TODO 1203 break; 1204 case kFmt3rc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB 1205 case kFmt3rms: // [opt] invoke-virtual+super/range 1206 case kFmt3rinline: // [opt] execute-inline/range 1207 pDec->vA = INST_AA(inst); 1208 pDec->vB = FETCH(1); 1209 pDec->vC = FETCH(2); 1210 break; 1211 case kFmt3rfs: // [opt] invoke-interface/range 1212 assert(false); // TODO 1213 break; 1214 case kFmt51l: // op vAA, #+BBBBBBBBBBBBBBBB 1215 pDec->vA = INST_AA(inst); 1216 pDec->vB_wide = FETCH(1); 1217 pDec->vB_wide |= (u8)FETCH(2) << 16; 1218 pDec->vB_wide |= (u8)FETCH(3) << 32; 1219 pDec->vB_wide |= (u8)FETCH(4) << 48; 1220 break; 1221 default: 1222 LOGW("Can't decode unexpected format %d (op=%d)\n", 1223 dexGetInstrFormat(fmts, pDec->opCode), pDec->opCode); 1224 assert(false); 1225 break; 1226 } 1227 1228 bail: 1229 ; 1230 } 1231 1232 /* 1233 * Return the width of the specified instruction, or 0 if not defined. Also 1234 * works for special OP_NOP entries, including switch statement data tables 1235 * and array data. 1236 */ 1237 int dexGetInstrOrTableWidthAbs(const InstructionWidth* widths, const u2* insns) 1238 { 1239 int width; 1240 1241 if (*insns == kPackedSwitchSignature) { 1242 width = 4 + insns[1] * 2; 1243 } else if (*insns == kSparseSwitchSignature) { 1244 width = 2 + insns[1] * 4; 1245 } else if (*insns == kArrayDataSignature) { 1246 u2 elemWidth = insns[1]; 1247 u4 len = insns[2] | (((u4)insns[3]) << 16); 1248 width = 4 + (elemWidth * len + 1) / 2; 1249 } else { 1250 width = dexGetInstrWidthAbs(widths, INST_INST(insns[0])); 1251 } 1252 return width; 1253 } 1254