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