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 */
dexCreateInstrWidthTable(void)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_INVOKE_DIRECT_EMPTY:
306 width = -3;
307 break;
308
309 /* these should never appear */
310 case OP_UNUSED_3E:
311 case OP_UNUSED_3F:
312 case OP_UNUSED_40:
313 case OP_UNUSED_41:
314 case OP_UNUSED_42:
315 case OP_UNUSED_43:
316 case OP_UNUSED_73:
317 case OP_UNUSED_79:
318 case OP_UNUSED_7A:
319 case OP_UNUSED_E3:
320 case OP_UNUSED_E4:
321 case OP_UNUSED_E5:
322 case OP_UNUSED_E6:
323 case OP_UNUSED_E7:
324 case OP_UNUSED_E8:
325 case OP_UNUSED_E9:
326 case OP_UNUSED_EA:
327 case OP_UNUSED_EB:
328 case OP_UNUSED_EC:
329 case OP_UNUSED_EF:
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 */
dexCreateInstrFlagsTable(void)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 flags = kInstrCanContinue;
620 break;
621 case OP_IGET_QUICK:
622 case OP_IGET_WIDE_QUICK:
623 case OP_IGET_OBJECT_QUICK:
624 case OP_IPUT_QUICK:
625 case OP_IPUT_WIDE_QUICK:
626 case OP_IPUT_OBJECT_QUICK:
627 flags = kInstrCanContinue | kInstrCanThrow;
628 break;
629
630 case OP_INVOKE_VIRTUAL_QUICK:
631 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
632 case OP_INVOKE_SUPER_QUICK:
633 case OP_INVOKE_SUPER_QUICK_RANGE:
634 case OP_INVOKE_DIRECT_EMPTY:
635 flags = kInstrCanContinue | kInstrCanThrow | kInstrInvoke;
636 break;
637
638 /* these should never appear */
639 case OP_UNUSED_3E:
640 case OP_UNUSED_3F:
641 case OP_UNUSED_40:
642 case OP_UNUSED_41:
643 case OP_UNUSED_42:
644 case OP_UNUSED_43:
645 case OP_UNUSED_73:
646 case OP_UNUSED_79:
647 case OP_UNUSED_7A:
648 case OP_UNUSED_E3:
649 case OP_UNUSED_E4:
650 case OP_UNUSED_E5:
651 case OP_UNUSED_E6:
652 case OP_UNUSED_E7:
653 case OP_UNUSED_E8:
654 case OP_UNUSED_E9:
655 case OP_UNUSED_EA:
656 case OP_UNUSED_EB:
657 case OP_UNUSED_EC:
658 case OP_UNUSED_EF:
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 */
dexCreateInstrFormatTable(void)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_INVOKE_DIRECT_EMPTY:
989 fmt = kFmt35c;
990 break;
991
992 /* these should never appear */
993 case OP_UNUSED_3E:
994 case OP_UNUSED_3F:
995 case OP_UNUSED_40:
996 case OP_UNUSED_41:
997 case OP_UNUSED_42:
998 case OP_UNUSED_43:
999 case OP_UNUSED_73:
1000 case OP_UNUSED_79:
1001 case OP_UNUSED_7A:
1002 case OP_UNUSED_E3:
1003 case OP_UNUSED_E4:
1004 case OP_UNUSED_E5:
1005 case OP_UNUSED_E6:
1006 case OP_UNUSED_E7:
1007 case OP_UNUSED_E8:
1008 case OP_UNUSED_E9:
1009 case OP_UNUSED_EA:
1010 case OP_UNUSED_EB:
1011 case OP_UNUSED_EC:
1012 case OP_UNUSED_EF:
1013 case OP_UNUSED_F1:
1014 case OP_UNUSED_FC:
1015 case OP_UNUSED_FD:
1016 case OP_UNUSED_FE:
1017 case OP_UNUSED_FF:
1018 fmt = kFmtUnknown;
1019 break;
1020
1021 /*
1022 * DO NOT add a "default" clause here. Without it the compiler will
1023 * complain if an instruction is missing (which is desirable).
1024 */
1025 }
1026
1027 instFmt[opc] = fmt;
1028 }
1029
1030 return instFmt;
1031 }
1032
1033 /*
1034 * Copied from InterpCore.h. Used for instruction decoding.
1035 */
1036 #define FETCH(_offset) (insns[(_offset)])
1037 #define INST_INST(_inst) ((_inst) & 0xff)
1038 #define INST_A(_inst) (((u2)(_inst) >> 8) & 0x0f)
1039 #define INST_B(_inst) ((u2)(_inst) >> 12)
1040 #define INST_AA(_inst) ((_inst) >> 8)
1041
1042 /*
1043 * Decode the instruction pointed to by "insns".
1044 *
1045 * Fills out the pieces of "pDec" that are affected by the current
1046 * instruction. Does not touch anything else.
1047 */
dexDecodeInstruction(const InstructionFormat * fmts,const u2 * insns,DecodedInstruction * pDec)1048 void dexDecodeInstruction(const InstructionFormat* fmts, const u2* insns,
1049 DecodedInstruction* pDec)
1050 {
1051 u2 inst = *insns;
1052
1053 pDec->opCode = (OpCode) INST_INST(inst);
1054
1055 switch (dexGetInstrFormat(fmts, pDec->opCode)) {
1056 case kFmt10x: // op
1057 /* nothing to do; copy the AA bits out for the verifier */
1058 pDec->vA = INST_AA(inst);
1059 break;
1060 case kFmt12x: // op vA, vB
1061 pDec->vA = INST_A(inst);
1062 pDec->vB = INST_B(inst);
1063 break;
1064 case kFmt11n: // op vA, #+B
1065 pDec->vA = INST_A(inst);
1066 pDec->vB = (s4) (INST_B(inst) << 28) >> 28; // sign extend 4-bit value
1067 break;
1068 case kFmt11x: // op vAA
1069 pDec->vA = INST_AA(inst);
1070 break;
1071 case kFmt10t: // op +AA
1072 pDec->vA = (s1) INST_AA(inst); // sign-extend 8-bit value
1073 break;
1074 case kFmt20t: // op +AAAA
1075 pDec->vA = (s2) FETCH(1); // sign-extend 16-bit value
1076 break;
1077 case kFmt20bc: // op AA, thing@BBBB
1078 case kFmt21c: // op vAA, thing@BBBB
1079 case kFmt22x: // op vAA, vBBBB
1080 pDec->vA = INST_AA(inst);
1081 pDec->vB = FETCH(1);
1082 break;
1083 case kFmt21s: // op vAA, #+BBBB
1084 case kFmt21t: // op vAA, +BBBB
1085 pDec->vA = INST_AA(inst);
1086 pDec->vB = (s2) FETCH(1); // sign-extend 16-bit value
1087 break;
1088 case kFmt21h: // op vAA, #+BBBB0000[00000000]
1089 pDec->vA = INST_AA(inst);
1090 /*
1091 * The value should be treated as right-zero-extended, but we don't
1092 * actually do that here. Among other things, we don't know if it's
1093 * the top bits of a 32- or 64-bit value.
1094 */
1095 pDec->vB = FETCH(1);
1096 break;
1097 case kFmt23x: // op vAA, vBB, vCC
1098 pDec->vA = INST_AA(inst);
1099 pDec->vB = FETCH(1) & 0xff;
1100 pDec->vC = FETCH(1) >> 8;
1101 break;
1102 case kFmt22b: // op vAA, vBB, #+CC
1103 pDec->vA = INST_AA(inst);
1104 pDec->vB = FETCH(1) & 0xff;
1105 pDec->vC = (s1) (FETCH(1) >> 8); // sign-extend 8-bit value
1106 break;
1107 case kFmt22s: // op vA, vB, #+CCCC
1108 case kFmt22t: // op vA, vB, +CCCC
1109 pDec->vA = INST_A(inst);
1110 pDec->vB = INST_B(inst);
1111 pDec->vC = (s2) FETCH(1); // sign-extend 16-bit value
1112 break;
1113 case kFmt22c: // op vA, vB, thing@CCCC
1114 case kFmt22cs: // [opt] op vA, vB, field offset CCCC
1115 pDec->vA = INST_A(inst);
1116 pDec->vB = INST_B(inst);
1117 pDec->vC = FETCH(1);
1118 break;
1119 case kFmt30t: // op +AAAAAAAA
1120 pDec->vA = FETCH(1) | ((u4) FETCH(2) << 16); // signed 32-bit value
1121 break;
1122 case kFmt31t: // op vAA, +BBBBBBBB
1123 case kFmt31c: // op vAA, thing@BBBBBBBB
1124 pDec->vA = INST_AA(inst);
1125 pDec->vB = FETCH(1) | ((u4) FETCH(2) << 16); // 32-bit value
1126 break;
1127 case kFmt32x: // op vAAAA, vBBBB
1128 pDec->vA = FETCH(1);
1129 pDec->vB = FETCH(2);
1130 break;
1131 case kFmt31i: // op vAA, #+BBBBBBBB
1132 pDec->vA = INST_AA(inst);
1133 pDec->vB = FETCH(1) | ((u4) FETCH(2) << 16);
1134 break;
1135 case kFmt35c: // op vB, {vD..vG,vA}, thing@CCCC
1136 case kFmt35ms: // [opt] invoke-virtual+super
1137 {
1138 /*
1139 * The lettering changes that came about when we went from 4 args
1140 * to 5 made the "range" versions of the calls different from
1141 * the non-range versions. We have the choice between decoding
1142 * them the way the spec shows and having lots of conditionals
1143 * in the verifier, or mapping the values onto their original
1144 * registers and leaving the verifier intact.
1145 *
1146 * Current plan is to leave the verifier alone. We can fix it
1147 * later if it's architecturally unbearable.
1148 *
1149 * Bottom line: method constant is always in vB.
1150 */
1151 u2 regList;
1152 int i, count;
1153
1154 pDec->vA = INST_B(inst);
1155 pDec->vB = FETCH(1);
1156 regList = FETCH(2);
1157
1158 if (pDec->vA > 5) {
1159 LOGW("Invalid arg count in 35c/35ms (%d)\n", pDec->vA);
1160 goto bail;
1161 }
1162 count = pDec->vA;
1163 if (count == 5) {
1164 /* 5th arg comes from A field in instruction */
1165 pDec->arg[4] = INST_A(inst);
1166 count--;
1167 }
1168 for (i = 0; i < count; i++) {
1169 pDec->arg[i] = regList & 0x0f;
1170 regList >>= 4;
1171 }
1172 /* copy arg[0] to vC; we don't have vD/vE/vF, so ignore those */
1173 if (pDec->vA > 0)
1174 pDec->vC = pDec->arg[0];
1175 }
1176 break;
1177 case kFmt3inline: // [opt] inline invoke
1178 {
1179 u2 regList;
1180 int i;
1181
1182 pDec->vA = INST_B(inst);
1183 pDec->vB = FETCH(1);
1184 regList = FETCH(2);
1185
1186 if (pDec->vA > 4) {
1187 LOGW("Invalid arg count in 3inline (%d)\n", pDec->vA);
1188 goto bail;
1189 }
1190 for (i = 0; i < (int) pDec->vA; i++) {
1191 pDec->arg[i] = regList & 0x0f;
1192 regList >>= 4;
1193 }
1194 /* copy arg[0] to vC; we don't have vD/vE/vF, so ignore those */
1195 if (pDec->vA > 0)
1196 pDec->vC = pDec->arg[0];
1197 }
1198 break;
1199 case kFmt35fs: // [opt] invoke-interface
1200 assert(false); // TODO
1201 break;
1202 case kFmt3rc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
1203 case kFmt3rms: // [opt] invoke-virtual+super/range
1204 pDec->vA = INST_AA(inst);
1205 pDec->vB = FETCH(1);
1206 pDec->vC = FETCH(2);
1207 break;
1208 case kFmt3rfs: // [opt] invoke-interface/range
1209 assert(false); // TODO
1210 break;
1211 case kFmt51l: // op vAA, #+BBBBBBBBBBBBBBBB
1212 pDec->vA = INST_AA(inst);
1213 pDec->vB_wide = FETCH(1);
1214 pDec->vB_wide |= (u8)FETCH(2) << 16;
1215 pDec->vB_wide |= (u8)FETCH(3) << 32;
1216 pDec->vB_wide |= (u8)FETCH(4) << 48;
1217 break;
1218 default:
1219 LOGW("Can't decode unexpected format %d (op=%d)\n",
1220 dexGetInstrFormat(fmts, pDec->opCode), pDec->opCode);
1221 assert(false);
1222 break;
1223 }
1224
1225 bail:
1226 ;
1227 }
1228
1229 /*
1230 * Return the width of the specified instruction, or 0 if not defined. Also
1231 * works for special OP_NOP entries, including switch statement data tables
1232 * and array data.
1233 */
dexGetInstrOrTableWidthAbs(const InstructionWidth * widths,const u2 * insns)1234 int dexGetInstrOrTableWidthAbs(const InstructionWidth* widths, const u2* insns)
1235 {
1236 int width;
1237
1238 if (*insns == kPackedSwitchSignature) {
1239 width = 4 + insns[1] * 2;
1240 } else if (*insns == kSparseSwitchSignature) {
1241 width = 2 + insns[1] * 4;
1242 } else if (*insns == kArrayDataSignature) {
1243 u2 elemWidth = insns[1];
1244 u4 len = insns[2] | (((u4)insns[3]) << 16);
1245 width = 4 + (elemWidth * len + 1) / 2;
1246 } else {
1247 width = dexGetInstrWidthAbs(widths, INST_INST(insns[0]));
1248 }
1249 return width;
1250 }
1251