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