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