• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 #include "dex_instruction-inl.h"
18 
19 #include <inttypes.h>
20 
21 #include <iomanip>
22 #include <sstream>
23 
24 #include "android-base/stringprintf.h"
25 
26 #include "dex_file-inl.h"
27 #include "dex_instruction_list.h"
28 #include "utf.h"
29 
30 namespace art {
31 
32 using android::base::StringPrintf;
33 
34 const char* const Instruction::kInstructionNames[] = {
35 #define INSTRUCTION_NAME(o, c, pname, f, i, a, e, v) pname,
36   DEX_INSTRUCTION_LIST(INSTRUCTION_NAME)
37 #undef INSTRUCTION_NAME
38 };
39 
40 static_assert(sizeof(Instruction::InstructionDescriptor) == 8u, "Unexpected descriptor size");
41 
GetTargetOffset() const42 int32_t Instruction::GetTargetOffset() const {
43   switch (FormatOf(Opcode())) {
44     // Cases for conditional branches follow.
45     case k22t: return VRegC_22t();
46     case k21t: return VRegB_21t();
47     // Cases for unconditional branches follow.
48     case k10t: return VRegA_10t();
49     case k20t: return VRegA_20t();
50     case k30t: return VRegA_30t();
51     default:
52       LOG(FATAL) << "Tried to access the branch offset of an instruction " << Name()
53                  << " which does not have a target operand.";
54       UNREACHABLE();
55   }
56 }
57 
SizeInCodeUnitsComplexOpcode() const58 size_t Instruction::SizeInCodeUnitsComplexOpcode() const {
59   // Handle special NOP encoded variable length sequences.
60   uint16_t inst_data = Fetch16(0);
61   DCHECK_EQ(inst_data & 0xFF, 0) << DumpString(nullptr);
62   switch (inst_data) {
63     case kPackedSwitchSignature:
64       return (4 + Fetch16(1) * 2);
65     case kSparseSwitchSignature:
66       return (2 + Fetch16(1) * 4);
67     case kArrayDataSignature: {
68       uint16_t element_size = Fetch16(1);
69       uint32_t length = Fetch16(2) | ((static_cast<uint32_t>(Fetch16(3))) << 16);
70       // The plus 1 is to round up for odd size and width.
71       uint32_t result = (4 + (element_size * length + 1) / 2);
72       // This function is used only after the `MethodVerifier` checked that the 32-bit calculation
73       // does not overflow. Let's `DCHECK()` the result against a 64-bit calculation.
74       DCHECK_EQ(result,
75                 4 + (static_cast<uint64_t>(element_size) * static_cast<uint64_t>(length) + 1) / 2);
76       return result;
77     }
78     default:
79       return 1;  // NOP.
80   }
81 }
82 
DumpHex(size_t code_units) const83 std::string Instruction::DumpHex(size_t code_units) const {
84   size_t inst_length = SizeInCodeUnits();
85   if (inst_length > code_units) {
86     inst_length = code_units;
87   }
88   std::ostringstream os;
89   const uint16_t* insn = reinterpret_cast<const uint16_t*>(this);
90   for (size_t i = 0; i < inst_length; i++) {
91     os << StringPrintf("0x%04x", insn[i]) << " ";
92   }
93   for (size_t i = inst_length; i < code_units; i++) {
94     os << "       ";
95   }
96   return os.str();
97 }
98 
DumpHexLE(size_t instr_code_units) const99 std::string Instruction::DumpHexLE(size_t instr_code_units) const {
100   size_t inst_length = SizeInCodeUnits();
101   if (inst_length > instr_code_units) {
102     inst_length = instr_code_units;
103   }
104   std::ostringstream os;
105   const uint16_t* insn = reinterpret_cast<const uint16_t*>(this);
106   for (size_t i = 0; i < inst_length; i++) {
107     os << StringPrintf("%02x%02x", static_cast<uint8_t>(insn[i] & 0x00FF),
108                        static_cast<uint8_t>((insn[i] & 0xFF00) >> 8)) << " ";
109   }
110   for (size_t i = inst_length; i < instr_code_units; i++) {
111     os << "     ";
112   }
113   return os.str();
114 }
115 
DumpString(const DexFile * file) const116 std::string Instruction::DumpString(const DexFile* file) const {
117   std::ostringstream os;
118   const char* opcode = kInstructionNames[Opcode()];
119   switch (FormatOf(Opcode())) {
120     case k10x:  os << opcode; break;
121     case k12x:  os << StringPrintf("%s v%d, v%d", opcode, VRegA_12x(), VRegB_12x()); break;
122     case k11n:  os << StringPrintf("%s v%d, #%+d", opcode, VRegA_11n(), VRegB_11n()); break;
123     case k11x:  os << StringPrintf("%s v%d", opcode, VRegA_11x()); break;
124     case k10t:  os << StringPrintf("%s %+d", opcode, VRegA_10t()); break;
125     case k20t:  os << StringPrintf("%s %+d", opcode, VRegA_20t()); break;
126     case k22x:  os << StringPrintf("%s v%d, v%d", opcode, VRegA_22x(), VRegB_22x()); break;
127     case k21t:  os << StringPrintf("%s v%d, %+d", opcode, VRegA_21t(), VRegB_21t()); break;
128     case k21s:  os << StringPrintf("%s v%d, #%+d", opcode, VRegA_21s(), VRegB_21s()); break;
129     case k21h: {
130         // op vAA, #+BBBB0000[00000000]
131         if (Opcode() == CONST_HIGH16) {
132           uint32_t value = VRegB_21h() << 16;
133           os << StringPrintf("%s v%d, #int %+d // 0x%x", opcode, VRegA_21h(), value, value);
134         } else {
135           uint64_t value = static_cast<uint64_t>(VRegB_21h()) << 48;
136           os << StringPrintf("%s v%d, #long %+" PRId64 " // 0x%" PRIx64, opcode, VRegA_21h(),
137                              value, value);
138         }
139       }
140       break;
141     case k21c: {
142       switch (Opcode()) {
143         case CONST_STRING:
144           if (file != nullptr) {
145             uint32_t string_idx = VRegB_21c();
146             if (string_idx < file->NumStringIds()) {
147               os << StringPrintf(
148                   "const-string v%d, %s // string@%d",
149                   VRegA_21c(),
150                   PrintableString(file->GetStringData(dex::StringIndex(string_idx))).c_str(),
151                   string_idx);
152             } else {
153               os << StringPrintf("const-string v%d, <<invalid-string-idx-%d>> // string@%d",
154                                  VRegA_21c(),
155                                  string_idx,
156                                  string_idx);
157             }
158             break;
159           }
160           FALLTHROUGH_INTENDED;
161         case CHECK_CAST:
162         case CONST_CLASS:
163         case NEW_INSTANCE:
164           if (file != nullptr) {
165             dex::TypeIndex type_idx(VRegB_21c());
166             os << opcode << " v" << static_cast<int>(VRegA_21c()) << ", "
167                << file->PrettyType(type_idx) << " // type@" << type_idx;
168             break;
169           }
170           FALLTHROUGH_INTENDED;
171         case SGET:
172         case SGET_WIDE:
173         case SGET_OBJECT:
174         case SGET_BOOLEAN:
175         case SGET_BYTE:
176         case SGET_CHAR:
177         case SGET_SHORT:
178           if (file != nullptr) {
179             uint32_t field_idx = VRegB_21c();
180             os << opcode << "  v" << static_cast<int>(VRegA_21c()) << ", " << file->PrettyField(field_idx, true)
181                << " // field@" << field_idx;
182             break;
183           }
184           FALLTHROUGH_INTENDED;
185         case SPUT:
186         case SPUT_WIDE:
187         case SPUT_OBJECT:
188         case SPUT_BOOLEAN:
189         case SPUT_BYTE:
190         case SPUT_CHAR:
191         case SPUT_SHORT:
192           if (file != nullptr) {
193             uint32_t field_idx = VRegB_21c();
194             os << opcode << " v" << static_cast<int>(VRegA_21c()) << ", " << file->PrettyField(field_idx, true)
195                << " // field@" << field_idx;
196             break;
197           }
198           FALLTHROUGH_INTENDED;
199         default:
200           os << StringPrintf("%s v%d, thing@%d", opcode, VRegA_21c(), VRegB_21c());
201           break;
202       }
203       break;
204     }
205     case k23x:  os << StringPrintf("%s v%d, v%d, v%d", opcode, VRegA_23x(), VRegB_23x(), VRegC_23x()); break;
206     case k22b:  os << StringPrintf("%s v%d, v%d, #%+d", opcode, VRegA_22b(), VRegB_22b(), VRegC_22b()); break;
207     case k22t:  os << StringPrintf("%s v%d, v%d, %+d", opcode, VRegA_22t(), VRegB_22t(), VRegC_22t()); break;
208     case k22s:  os << StringPrintf("%s v%d, v%d, #%+d", opcode, VRegA_22s(), VRegB_22s(), VRegC_22s()); break;
209     case k22c: {
210       switch (Opcode()) {
211         case IGET:
212         case IGET_WIDE:
213         case IGET_OBJECT:
214         case IGET_BOOLEAN:
215         case IGET_BYTE:
216         case IGET_CHAR:
217         case IGET_SHORT:
218           if (file != nullptr) {
219             uint32_t field_idx = VRegC_22c();
220             os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
221                << file->PrettyField(field_idx, true) << " // field@" << field_idx;
222             break;
223           }
224           FALLTHROUGH_INTENDED;
225         case IPUT:
226         case IPUT_WIDE:
227         case IPUT_OBJECT:
228         case IPUT_BOOLEAN:
229         case IPUT_BYTE:
230         case IPUT_CHAR:
231         case IPUT_SHORT:
232           if (file != nullptr) {
233             uint32_t field_idx = VRegC_22c();
234             os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v" << static_cast<int>(VRegB_22c()) << ", "
235                << file->PrettyField(field_idx, true) << " // field@" << field_idx;
236             break;
237           }
238           FALLTHROUGH_INTENDED;
239         case INSTANCE_OF:
240           if (file != nullptr) {
241             dex::TypeIndex type_idx(VRegC_22c());
242             os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v"
243                << static_cast<int>(VRegB_22c()) << ", " << file->PrettyType(type_idx)
244                << " // type@" << type_idx.index_;
245             break;
246           }
247           FALLTHROUGH_INTENDED;
248         case NEW_ARRAY:
249           if (file != nullptr) {
250             dex::TypeIndex type_idx(VRegC_22c());
251             os << opcode << " v" << static_cast<int>(VRegA_22c()) << ", v"
252                << static_cast<int>(VRegB_22c()) << ", " << file->PrettyType(type_idx)
253                << " // type@" << type_idx.index_;
254             break;
255           }
256           FALLTHROUGH_INTENDED;
257         default:
258           os << StringPrintf("%s v%d, v%d, thing@%d", opcode, VRegA_22c(), VRegB_22c(), VRegC_22c());
259           break;
260       }
261       break;
262     }
263     case k32x:  os << StringPrintf("%s v%d, v%d", opcode, VRegA_32x(), VRegB_32x()); break;
264     case k30t:  os << StringPrintf("%s %+d", opcode, VRegA_30t()); break;
265     case k31t:  os << StringPrintf("%s v%d, %+d", opcode, VRegA_31t(), VRegB_31t()); break;
266     case k31i:  os << StringPrintf("%s v%d, #%+d", opcode, VRegA_31i(), VRegB_31i()); break;
267     case k31c:
268       if (Opcode() == CONST_STRING_JUMBO) {
269         uint32_t string_idx = VRegB_31c();
270         if (file != nullptr) {
271           if (string_idx < file->NumStringIds()) {
272             os << StringPrintf(
273                 "%s v%d, %s // string@%d",
274                 opcode,
275                 VRegA_31c(),
276                 PrintableString(file->GetStringData(dex::StringIndex(string_idx))).c_str(),
277                 string_idx);
278           } else {
279             os << StringPrintf("%s v%d, <<invalid-string-idx-%d>> // string@%d",
280                                opcode,
281                                VRegA_31c(),
282                                string_idx,
283                                string_idx);
284           }
285         } else {
286           os << StringPrintf("%s v%d, string@%d", opcode, VRegA_31c(), string_idx);
287         }
288       } else {
289         os << StringPrintf("%s v%d, thing@%d", opcode, VRegA_31c(), VRegB_31c()); break;
290       }
291       break;
292     case k35c: {
293       uint32_t arg[kMaxVarArgRegs];
294       GetVarArgs(arg);
295       auto DumpArgs = [&](size_t count) {
296         for (size_t i = 0; i < count; ++i) {
297           if (i != 0) {
298             os << ", ";
299           }
300           os << "v" << arg[i];
301         }
302       };
303       switch (Opcode()) {
304         case FILLED_NEW_ARRAY:
305         {
306           os << opcode << " {";
307           DumpArgs(VRegA_35c());
308           os << "}, type@" << VRegB_35c();
309         }
310         break;
311 
312         case INVOKE_VIRTUAL:
313         case INVOKE_SUPER:
314         case INVOKE_DIRECT:
315         case INVOKE_STATIC:
316         case INVOKE_INTERFACE:
317           if (file != nullptr) {
318             os << opcode << " {";
319             uint32_t method_idx = VRegB_35c();
320             DumpArgs(VRegA_35c());
321             os << "}, " << file->PrettyMethod(method_idx) << " // method@" << method_idx;
322             break;
323           }
324           FALLTHROUGH_INTENDED;
325         case INVOKE_CUSTOM:
326           if (file != nullptr) {
327             os << opcode << " {";
328             uint32_t call_site_idx = VRegB_35c();
329             DumpArgs(VRegA_35c());
330             os << "},  // call_site@" << call_site_idx;
331             break;
332           }
333           FALLTHROUGH_INTENDED;
334         default:
335           os << opcode << " {";
336           DumpArgs(VRegA_35c());
337           os << "}, thing@" << VRegB_35c();
338           break;
339       }
340       break;
341     }
342     case k3rc: {
343       uint16_t first_reg = VRegC_3rc();
344       uint16_t last_reg =  VRegC_3rc() + VRegA_3rc() - 1;
345       switch (Opcode()) {
346         case INVOKE_VIRTUAL_RANGE:
347         case INVOKE_SUPER_RANGE:
348         case INVOKE_DIRECT_RANGE:
349         case INVOKE_STATIC_RANGE:
350         case INVOKE_INTERFACE_RANGE:
351           if (file != nullptr) {
352             uint32_t method_idx = VRegB_3rc();
353             os << StringPrintf("%s, {v%d .. v%d}, ", opcode, first_reg, last_reg)
354                << file->PrettyMethod(method_idx) << " // method@" << method_idx;
355             break;
356           }
357           FALLTHROUGH_INTENDED;
358         case INVOKE_CUSTOM_RANGE:
359           if (file != nullptr) {
360             uint32_t call_site_idx = VRegB_3rc();
361             os << StringPrintf("%s, {v%d .. v%d}, ", opcode, first_reg, last_reg)
362                << "// call_site@" << call_site_idx;
363             break;
364           }
365           FALLTHROUGH_INTENDED;
366         default:
367           os << StringPrintf("%s, {v%d .. v%d}, ", opcode, first_reg, last_reg)
368              << "thing@" << VRegB_3rc();
369           break;
370       }
371       break;
372     }
373     case k45cc: {
374       uint32_t arg[kMaxVarArgRegs];
375       GetVarArgs(arg);
376       uint16_t method_idx = VRegB_45cc();
377       dex::ProtoIndex proto_idx(VRegH_45cc());
378       os << opcode << " {";
379       for (uint32_t i = 0; i < VRegA_45cc(); ++i) {
380         if (i != 0) {
381           os << ", ";
382         }
383         os << "v" << arg[i];
384       }
385       os << "}";
386       if (file != nullptr) {
387         os << ", " << file->PrettyMethod(method_idx)
388            << ", " << file->GetShorty(proto_idx)
389            << " // ";
390       } else {
391         os << ", ";
392       }
393       os << "method@" << method_idx << ", proto@" << proto_idx;
394       break;
395     }
396     case k4rcc:
397       switch (Opcode()) {
398         case INVOKE_POLYMORPHIC_RANGE: {
399           if (file != nullptr) {
400             uint16_t method_idx = VRegB_4rcc();
401             dex::ProtoIndex proto_idx(VRegH_4rcc());
402             os << opcode << ", {v" << VRegC_4rcc() << " .. v" << (VRegC_4rcc() + VRegA_4rcc())
403                << "}, " << file->PrettyMethod(method_idx)
404                << ", " << file->GetShorty(dex::ProtoIndex(proto_idx))
405                << " // method@" << method_idx << ", proto@" << proto_idx;
406             break;
407           }
408         }
409         FALLTHROUGH_INTENDED;
410         default: {
411           uint16_t method_idx = VRegB_4rcc();
412           dex::ProtoIndex proto_idx(VRegH_4rcc());
413           os << opcode << ", {v" << VRegC_4rcc() << " .. v" << (VRegC_4rcc() + VRegA_4rcc())
414              << "}, method@" << method_idx << ", proto@" << proto_idx;
415         }
416       }
417       break;
418     case k51l: os << StringPrintf("%s v%d, #%+" PRId64, opcode, VRegA_51l(), VRegB_51l()); break;
419     case kInvalidFormat: os << "<invalid-opcode-format>";
420   }
421   return os.str();
422 }
423 
424 // Add some checks that ensure the flags make sense. We need a subclass to be in the context of
425 // Instruction. Otherwise the flags from the instruction list don't work.
426 struct InstructionStaticAsserts : private Instruction {
427   #define IMPLIES(a, b) (!(a) || (b))
428 
429   #define VAR_ARGS_CHECK(o, c, pname, f, i, a, e, v) \
430     static_assert(IMPLIES((f) == k35c || (f) == k45cc, \
431                           ((v) & (kVerifyVarArg | kVerifyVarArgNonZero)) != 0), \
432                   "Missing var-arg verification");
433     DEX_INSTRUCTION_LIST(VAR_ARGS_CHECK)
434   #undef VAR_ARGS_CHECK
435 
436   #define VAR_ARGS_RANGE_CHECK(o, c, pname, f, i, a, e, v) \
437     static_assert(IMPLIES((f) == k3rc || (f) == k4rcc, \
438                           ((v) & (kVerifyVarArgRange | kVerifyVarArgRangeNonZero)) != 0), \
439                   "Missing var-arg verification");
440     DEX_INSTRUCTION_LIST(VAR_ARGS_RANGE_CHECK)
441   #undef VAR_ARGS_RANGE_CHECK
442 
443   #define EXPERIMENTAL_CHECK(o, c, pname, f, i, a, e, v) \
444     static_assert(kHaveExperimentalInstructions || (((a) & kExperimental) == 0), \
445                   "Unexpected experimental instruction.");
446   DEX_INSTRUCTION_LIST(EXPERIMENTAL_CHECK)
447   #undef EXPERIMENTAL_CHECK
448 };
449 
operator <<(std::ostream & os,Instruction::Code code)450 std::ostream& operator<<(std::ostream& os, Instruction::Code code) {
451   return os << Instruction::Name(code);
452 }
453 
GetOperand(size_t operand_index) const454 uint32_t RangeInstructionOperands::GetOperand(size_t operand_index) const {
455   DCHECK_LT(operand_index, GetNumberOfOperands());
456   return first_operand_ + operand_index;
457 }
458 
GetOperand(size_t operand_index) const459 uint32_t VarArgsInstructionOperands::GetOperand(size_t operand_index) const {
460   DCHECK_LT(operand_index, GetNumberOfOperands());
461   return operands_[operand_index];
462 }
463 
GetOperand(size_t operand_index) const464 uint32_t NoReceiverInstructionOperands::GetOperand(size_t operand_index) const {
465   DCHECK_LT(GetNumberOfOperands(), inner_->GetNumberOfOperands());
466   // The receiver is the first operand and since we're skipping it, we need to
467   // add 1 to the operand_index.
468   return inner_->GetOperand(operand_index + 1);
469 }
470 
471 }  // namespace art
472