blob: 7382a97d3573faf1880deb21b3004022ba880f3d [file] [log] [blame]
David Sehr7629f602016-08-07 16:01:51 -07001/*
2 * Copyright (C) 2016 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 * Implementation file of the dexlayout utility.
17 *
18 * This is a tool to read dex files into an internal representation,
19 * reorganize the representation, and emit dex files with a better
20 * file layout.
21 */
22
23#include "dexlayout.h"
24
25#include <inttypes.h>
26#include <stdio.h>
27
28#include <iostream>
29#include <memory>
30#include <sstream>
31#include <vector>
32
Andreas Gampe46ee31b2016-12-14 10:11:49 -080033#include "android-base/stringprintf.h"
34
Andreas Gampe57943812017-12-06 21:39:13 -080035#include "base/logging.h" // For VLOG_IS_ON.
David Brazdildcfa89b2018-10-31 11:04:10 +000036#include "base/hiddenapi_flags.h"
David Sehr79e26072018-04-06 17:58:50 -070037#include "base/mem_map.h"
David Sehr9d9227a2018-12-19 12:32:50 -080038#include "base/mman.h" // For the PROT_* and MAP_* constants.
David Sehrc431b9d2018-03-02 12:01:51 -080039#include "base/os.h"
40#include "base/utils.h"
Mathieu Chartier818cb802018-05-11 05:30:16 +000041#include "dex/art_dex_file_loader.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080042#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080043#include "dex/dex_file-inl.h"
44#include "dex/dex_file_layout.h"
45#include "dex/dex_file_loader.h"
46#include "dex/dex_file_types.h"
47#include "dex/dex_file_verifier.h"
48#include "dex/dex_instruction-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070049#include "dex_ir_builder.h"
Jeff Haoec7f1a92017-03-13 16:24:24 -070050#include "dex_verify.h"
David Sehrcdcfde72016-09-26 07:44:04 -070051#include "dex_visualize.h"
Jeff Haoa8621002016-10-04 18:13:44 +000052#include "dex_writer.h"
David Sehr82d046e2018-04-23 08:14:19 -070053#include "profile/profile_compilation_info.h"
David Sehr7629f602016-08-07 16:01:51 -070054
55namespace art {
56
Andreas Gampe46ee31b2016-12-14 10:11:49 -080057using android::base::StringPrintf;
58
David Sehr7629f602016-08-07 16:01:51 -070059/*
David Sehr7629f602016-08-07 16:01:51 -070060 * Flags for use with createAccessFlagStr().
61 */
62enum AccessFor {
63 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
64};
65const int kNumFlags = 18;
66
67/*
68 * Gets 2 little-endian bytes.
69 */
70static inline uint16_t Get2LE(unsigned char const* src) {
71 return src[0] | (src[1] << 8);
72}
73
74/*
75 * Converts the class name portion of a type descriptor to human-readable
76 * "dotted" form. For example, "Ljava/lang/String;" becomes "String".
77 */
Orion Hodsonfe42d212018-08-24 14:01:14 +010078static std::string DescriptorClassToName(const char* str) {
David Sehr7629f602016-08-07 16:01:51 -070079 std::string descriptor(str);
80 // Reduce to just the class name prefix.
81 size_t last_slash = descriptor.rfind('/');
82 if (last_slash == std::string::npos) {
83 last_slash = 0;
84 }
85 // Start past the '/' or 'L'.
86 last_slash++;
87
88 // Copy class name over, trimming trailing ';'.
89 size_t size = descriptor.size() - 1 - last_slash;
90 std::string result(descriptor.substr(last_slash, size));
91
David Sehr7629f602016-08-07 16:01:51 -070092 return result;
93}
94
95/*
96 * Returns string representing the boolean value.
97 */
98static const char* StrBool(bool val) {
99 return val ? "true" : "false";
100}
101
102/*
103 * Returns a quoted string representing the boolean value.
104 */
105static const char* QuotedBool(bool val) {
106 return val ? "\"true\"" : "\"false\"";
107}
108
109/*
110 * Returns a quoted string representing the access flags.
111 */
112static const char* QuotedVisibility(uint32_t access_flags) {
113 if (access_flags & kAccPublic) {
114 return "\"public\"";
115 } else if (access_flags & kAccProtected) {
116 return "\"protected\"";
117 } else if (access_flags & kAccPrivate) {
118 return "\"private\"";
119 } else {
120 return "\"package\"";
121 }
122}
123
124/*
125 * Counts the number of '1' bits in a word.
126 */
127static int CountOnes(uint32_t val) {
128 val = val - ((val >> 1) & 0x55555555);
129 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
130 return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
131}
132
133/*
134 * Creates a new string with human-readable access flags.
135 *
136 * In the base language the access_flags fields are type uint16_t; in Dalvik they're uint32_t.
137 */
138static char* CreateAccessFlagStr(uint32_t flags, AccessFor for_what) {
139 static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
140 {
141 "PUBLIC", /* 0x00001 */
142 "PRIVATE", /* 0x00002 */
143 "PROTECTED", /* 0x00004 */
144 "STATIC", /* 0x00008 */
145 "FINAL", /* 0x00010 */
146 "?", /* 0x00020 */
147 "?", /* 0x00040 */
148 "?", /* 0x00080 */
149 "?", /* 0x00100 */
150 "INTERFACE", /* 0x00200 */
151 "ABSTRACT", /* 0x00400 */
152 "?", /* 0x00800 */
153 "SYNTHETIC", /* 0x01000 */
154 "ANNOTATION", /* 0x02000 */
155 "ENUM", /* 0x04000 */
156 "?", /* 0x08000 */
157 "VERIFIED", /* 0x10000 */
158 "OPTIMIZED", /* 0x20000 */
159 }, {
160 "PUBLIC", /* 0x00001 */
161 "PRIVATE", /* 0x00002 */
162 "PROTECTED", /* 0x00004 */
163 "STATIC", /* 0x00008 */
164 "FINAL", /* 0x00010 */
165 "SYNCHRONIZED", /* 0x00020 */
166 "BRIDGE", /* 0x00040 */
167 "VARARGS", /* 0x00080 */
168 "NATIVE", /* 0x00100 */
169 "?", /* 0x00200 */
170 "ABSTRACT", /* 0x00400 */
171 "STRICT", /* 0x00800 */
172 "SYNTHETIC", /* 0x01000 */
173 "?", /* 0x02000 */
174 "?", /* 0x04000 */
175 "MIRANDA", /* 0x08000 */
176 "CONSTRUCTOR", /* 0x10000 */
177 "DECLARED_SYNCHRONIZED", /* 0x20000 */
178 }, {
179 "PUBLIC", /* 0x00001 */
180 "PRIVATE", /* 0x00002 */
181 "PROTECTED", /* 0x00004 */
182 "STATIC", /* 0x00008 */
183 "FINAL", /* 0x00010 */
184 "?", /* 0x00020 */
185 "VOLATILE", /* 0x00040 */
186 "TRANSIENT", /* 0x00080 */
187 "?", /* 0x00100 */
188 "?", /* 0x00200 */
189 "?", /* 0x00400 */
190 "?", /* 0x00800 */
191 "SYNTHETIC", /* 0x01000 */
192 "?", /* 0x02000 */
193 "ENUM", /* 0x04000 */
194 "?", /* 0x08000 */
195 "?", /* 0x10000 */
196 "?", /* 0x20000 */
197 },
198 };
199
200 // Allocate enough storage to hold the expected number of strings,
201 // plus a space between each. We over-allocate, using the longest
202 // string above as the base metric.
203 const int kLongest = 21; // The strlen of longest string above.
204 const int count = CountOnes(flags);
205 char* str;
206 char* cp;
207 cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
208
209 for (int i = 0; i < kNumFlags; i++) {
210 if (flags & 0x01) {
211 const char* accessStr = kAccessStrings[for_what][i];
212 const int len = strlen(accessStr);
213 if (cp != str) {
214 *cp++ = ' ';
215 }
216 memcpy(cp, accessStr, len);
217 cp += len;
218 }
219 flags >>= 1;
220 } // for
221
222 *cp = '\0';
223 return str;
224}
225
David Brazdildcfa89b2018-10-31 11:04:10 +0000226static std::string GetHiddenapiFlagStr(uint32_t hiddenapi_flags) {
David Brazdil90faceb2018-12-14 14:36:15 +0000227 std::stringstream ss;
228 hiddenapi::ApiList(hiddenapi_flags).Dump(ss);
229 std::string api_list = ss.str();
David Brazdildcfa89b2018-10-31 11:04:10 +0000230 std::transform(api_list.begin(), api_list.end(), api_list.begin(), ::toupper);
231 return api_list;
David Brazdil20c765f2018-10-27 21:45:15 +0000232}
233
David Sehr7629f602016-08-07 16:01:51 -0700234static std::string GetSignatureForProtoId(const dex_ir::ProtoId* proto) {
235 if (proto == nullptr) {
236 return "<no signature>";
237 }
238
David Sehr7629f602016-08-07 16:01:51 -0700239 std::string result("(");
Jeff Haoa8621002016-10-04 18:13:44 +0000240 const dex_ir::TypeList* type_list = proto->Parameters();
241 if (type_list != nullptr) {
242 for (const dex_ir::TypeId* type_id : *type_list->GetTypeList()) {
243 result += type_id->GetStringId()->Data();
244 }
David Sehr7629f602016-08-07 16:01:51 -0700245 }
246 result += ")";
247 result += proto->ReturnType()->GetStringId()->Data();
248 return result;
249}
250
251/*
252 * Copies character data from "data" to "out", converting non-ASCII values
253 * to fprintf format chars or an ASCII filler ('.' or '?').
254 *
255 * The output buffer must be able to hold (2*len)+1 bytes. The result is
256 * NULL-terminated.
257 */
258static void Asciify(char* out, const unsigned char* data, size_t len) {
Andreas Gampec74d9cb2018-09-20 13:44:44 -0700259 for (; len != 0u; --len) {
David Sehr7629f602016-08-07 16:01:51 -0700260 if (*data < 0x20) {
261 // Could do more here, but we don't need them yet.
262 switch (*data) {
263 case '\0':
264 *out++ = '\\';
265 *out++ = '0';
266 break;
267 case '\n':
268 *out++ = '\\';
269 *out++ = 'n';
270 break;
271 default:
272 *out++ = '.';
273 break;
274 } // switch
275 } else if (*data >= 0x80) {
276 *out++ = '?';
277 } else {
278 *out++ = *data;
279 }
280 data++;
281 } // while
282 *out = '\0';
283}
284
285/*
286 * Dumps a string value with some escape characters.
287 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800288static void DumpEscapedString(const char* p, FILE* out_file) {
289 fputs("\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700290 for (; *p; p++) {
291 switch (*p) {
292 case '\\':
Jeff Haoea7c6292016-11-14 18:10:16 -0800293 fputs("\\\\", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700294 break;
295 case '\"':
Jeff Haoea7c6292016-11-14 18:10:16 -0800296 fputs("\\\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700297 break;
298 case '\t':
Jeff Haoea7c6292016-11-14 18:10:16 -0800299 fputs("\\t", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700300 break;
301 case '\n':
Jeff Haoea7c6292016-11-14 18:10:16 -0800302 fputs("\\n", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700303 break;
304 case '\r':
Jeff Haoea7c6292016-11-14 18:10:16 -0800305 fputs("\\r", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700306 break;
307 default:
Jeff Haoea7c6292016-11-14 18:10:16 -0800308 putc(*p, out_file);
David Sehr7629f602016-08-07 16:01:51 -0700309 } // switch
310 } // for
Jeff Haoea7c6292016-11-14 18:10:16 -0800311 fputs("\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700312}
313
314/*
315 * Dumps a string as an XML attribute value.
316 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800317static void DumpXmlAttribute(const char* p, FILE* out_file) {
David Sehr7629f602016-08-07 16:01:51 -0700318 for (; *p; p++) {
319 switch (*p) {
320 case '&':
Jeff Haoea7c6292016-11-14 18:10:16 -0800321 fputs("&amp;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700322 break;
323 case '<':
Jeff Haoea7c6292016-11-14 18:10:16 -0800324 fputs("&lt;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700325 break;
326 case '>':
Jeff Haoea7c6292016-11-14 18:10:16 -0800327 fputs("&gt;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700328 break;
329 case '"':
Jeff Haoea7c6292016-11-14 18:10:16 -0800330 fputs("&quot;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700331 break;
332 case '\t':
Jeff Haoea7c6292016-11-14 18:10:16 -0800333 fputs("&#x9;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700334 break;
335 case '\n':
Jeff Haoea7c6292016-11-14 18:10:16 -0800336 fputs("&#xA;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700337 break;
338 case '\r':
Jeff Haoea7c6292016-11-14 18:10:16 -0800339 fputs("&#xD;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700340 break;
341 default:
Jeff Haoea7c6292016-11-14 18:10:16 -0800342 putc(*p, out_file);
David Sehr7629f602016-08-07 16:01:51 -0700343 } // switch
344 } // for
345}
346
David Sehr7629f602016-08-07 16:01:51 -0700347/*
348 * Helper for dumpInstruction(), which builds the string
349 * representation for the index in the given instruction.
350 * Returns a pointer to a buffer of sufficient size.
351 */
352static std::unique_ptr<char[]> IndexString(dex_ir::Header* header,
353 const Instruction* dec_insn,
354 size_t buf_size) {
355 std::unique_ptr<char[]> buf(new char[buf_size]);
356 // Determine index and width of the string.
357 uint32_t index = 0;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700358 uint32_t secondary_index = dex::kDexNoIndex;
David Sehr7629f602016-08-07 16:01:51 -0700359 uint32_t width = 4;
360 switch (Instruction::FormatOf(dec_insn->Opcode())) {
361 // SOME NOT SUPPORTED:
362 // case Instruction::k20bc:
363 case Instruction::k21c:
364 case Instruction::k35c:
365 // case Instruction::k35ms:
366 case Instruction::k3rc:
367 // case Instruction::k3rms:
368 // case Instruction::k35mi:
369 // case Instruction::k3rmi:
370 index = dec_insn->VRegB();
371 width = 4;
372 break;
373 case Instruction::k31c:
374 index = dec_insn->VRegB();
375 width = 8;
376 break;
377 case Instruction::k22c:
378 // case Instruction::k22cs:
379 index = dec_insn->VRegC();
380 width = 4;
381 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100382 case Instruction::k45cc:
383 case Instruction::k4rcc:
384 index = dec_insn->VRegB();
385 secondary_index = dec_insn->VRegH();
386 width = 4;
David Sehr7639cdc2017-04-15 10:06:21 -0700387 break;
David Sehr7629f602016-08-07 16:01:51 -0700388 default:
389 break;
390 } // switch
391
392 // Determine index type.
393 size_t outSize = 0;
394 switch (Instruction::IndexTypeOf(dec_insn->Opcode())) {
395 case Instruction::kIndexUnknown:
396 // This function should never get called for this type, but do
397 // something sensible here, just to help with debugging.
398 outSize = snprintf(buf.get(), buf_size, "<unknown-index>");
399 break;
400 case Instruction::kIndexNone:
401 // This function should never get called for this type, but do
402 // something sensible here, just to help with debugging.
403 outSize = snprintf(buf.get(), buf_size, "<no-index>");
404 break;
405 case Instruction::kIndexTypeRef:
David Sehr2b5a38f2018-06-14 15:13:04 -0700406 if (index < header->TypeIds().Size()) {
407 const char* tp = header->TypeIds()[index]->GetStringId()->Data();
David Sehr7629f602016-08-07 16:01:51 -0700408 outSize = snprintf(buf.get(), buf_size, "%s // type@%0*x", tp, width, index);
409 } else {
410 outSize = snprintf(buf.get(), buf_size, "<type?> // type@%0*x", width, index);
411 }
412 break;
413 case Instruction::kIndexStringRef:
David Sehr2b5a38f2018-06-14 15:13:04 -0700414 if (index < header->StringIds().Size()) {
415 const char* st = header->StringIds()[index]->Data();
David Sehr7629f602016-08-07 16:01:51 -0700416 outSize = snprintf(buf.get(), buf_size, "\"%s\" // string@%0*x", st, width, index);
417 } else {
418 outSize = snprintf(buf.get(), buf_size, "<string?> // string@%0*x", width, index);
419 }
420 break;
421 case Instruction::kIndexMethodRef:
David Sehr2b5a38f2018-06-14 15:13:04 -0700422 if (index < header->MethodIds().Size()) {
423 dex_ir::MethodId* method_id = header->MethodIds()[index];
David Sehr7629f602016-08-07 16:01:51 -0700424 const char* name = method_id->Name()->Data();
David Sehr72359222016-09-07 13:04:01 -0700425 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -0700426 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
427 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // method@%0*x",
David Sehr72359222016-09-07 13:04:01 -0700428 back_descriptor, name, type_descriptor.c_str(), width, index);
David Sehr7629f602016-08-07 16:01:51 -0700429 } else {
430 outSize = snprintf(buf.get(), buf_size, "<method?> // method@%0*x", width, index);
431 }
432 break;
433 case Instruction::kIndexFieldRef:
David Sehr2b5a38f2018-06-14 15:13:04 -0700434 if (index < header->FieldIds().Size()) {
435 dex_ir::FieldId* field_id = header->FieldIds()[index];
David Sehr7629f602016-08-07 16:01:51 -0700436 const char* name = field_id->Name()->Data();
437 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
438 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
439 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // field@%0*x",
440 back_descriptor, name, type_descriptor, width, index);
441 } else {
442 outSize = snprintf(buf.get(), buf_size, "<field?> // field@%0*x", width, index);
443 }
444 break;
445 case Instruction::kIndexVtableOffset:
446 outSize = snprintf(buf.get(), buf_size, "[%0*x] // vtable #%0*x",
447 width, index, width, index);
448 break;
449 case Instruction::kIndexFieldOffset:
450 outSize = snprintf(buf.get(), buf_size, "[obj+%0*x]", width, index);
451 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100452 case Instruction::kIndexMethodAndProtoRef: {
453 std::string method("<method?>");
454 std::string proto("<proto?>");
David Sehr2b5a38f2018-06-14 15:13:04 -0700455 if (index < header->MethodIds().Size()) {
456 dex_ir::MethodId* method_id = header->MethodIds()[index];
Orion Hodsonb34bb192016-10-18 17:02:58 +0100457 const char* name = method_id->Name()->Data();
458 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
459 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
460 method = StringPrintf("%s.%s:%s", back_descriptor, name, type_descriptor.c_str());
461 }
David Sehr2b5a38f2018-06-14 15:13:04 -0700462 if (secondary_index < header->ProtoIds().Size()) {
463 dex_ir::ProtoId* proto_id = header->ProtoIds()[secondary_index];
Orion Hodsonb34bb192016-10-18 17:02:58 +0100464 proto = GetSignatureForProtoId(proto_id);
465 }
466 outSize = snprintf(buf.get(), buf_size, "%s, %s // method@%0*x, proto@%0*x",
467 method.c_str(), proto.c_str(), width, index, width, secondary_index);
Jeff Haoea7c6292016-11-14 18:10:16 -0800468 }
469 break;
470 // SOME NOT SUPPORTED:
471 // case Instruction::kIndexVaries:
472 // case Instruction::kIndexInlineMethod:
David Sehr7629f602016-08-07 16:01:51 -0700473 default:
474 outSize = snprintf(buf.get(), buf_size, "<?>");
475 break;
476 } // switch
477
478 // Determine success of string construction.
479 if (outSize >= buf_size) {
480 // The buffer wasn't big enough; retry with computed size. Note: snprintf()
481 // doesn't count/ the '\0' as part of its returned size, so we add explicit
482 // space for it here.
483 return IndexString(header, dec_insn, outSize + 1);
484 }
485 return buf;
486}
487
488/*
Jeff Haoea7c6292016-11-14 18:10:16 -0800489 * Dumps encoded annotation.
490 */
491void DexLayout::DumpEncodedAnnotation(dex_ir::EncodedAnnotation* annotation) {
492 fputs(annotation->GetType()->GetStringId()->Data(), out_file_);
493 // Display all name=value pairs.
494 for (auto& subannotation : *annotation->GetAnnotationElements()) {
495 fputc(' ', out_file_);
496 fputs(subannotation->GetName()->Data(), out_file_);
497 fputc('=', out_file_);
498 DumpEncodedValue(subannotation->GetValue());
499 }
500}
501/*
502 * Dumps encoded value.
503 */
504void DexLayout::DumpEncodedValue(const dex_ir::EncodedValue* data) {
505 switch (data->Type()) {
506 case DexFile::kDexAnnotationByte:
507 fprintf(out_file_, "%" PRId8, data->GetByte());
508 break;
509 case DexFile::kDexAnnotationShort:
510 fprintf(out_file_, "%" PRId16, data->GetShort());
511 break;
512 case DexFile::kDexAnnotationChar:
513 fprintf(out_file_, "%" PRIu16, data->GetChar());
514 break;
515 case DexFile::kDexAnnotationInt:
516 fprintf(out_file_, "%" PRId32, data->GetInt());
517 break;
518 case DexFile::kDexAnnotationLong:
519 fprintf(out_file_, "%" PRId64, data->GetLong());
520 break;
521 case DexFile::kDexAnnotationFloat: {
522 fprintf(out_file_, "%g", data->GetFloat());
523 break;
524 }
525 case DexFile::kDexAnnotationDouble: {
526 fprintf(out_file_, "%g", data->GetDouble());
527 break;
528 }
529 case DexFile::kDexAnnotationString: {
530 dex_ir::StringId* string_id = data->GetStringId();
531 if (options_.output_format_ == kOutputPlain) {
532 DumpEscapedString(string_id->Data(), out_file_);
533 } else {
534 DumpXmlAttribute(string_id->Data(), out_file_);
535 }
536 break;
537 }
538 case DexFile::kDexAnnotationType: {
539 dex_ir::TypeId* type_id = data->GetTypeId();
540 fputs(type_id->GetStringId()->Data(), out_file_);
541 break;
542 }
543 case DexFile::kDexAnnotationField:
544 case DexFile::kDexAnnotationEnum: {
545 dex_ir::FieldId* field_id = data->GetFieldId();
546 fputs(field_id->Name()->Data(), out_file_);
547 break;
548 }
549 case DexFile::kDexAnnotationMethod: {
550 dex_ir::MethodId* method_id = data->GetMethodId();
551 fputs(method_id->Name()->Data(), out_file_);
552 break;
553 }
554 case DexFile::kDexAnnotationArray: {
555 fputc('{', out_file_);
556 // Display all elements.
557 for (auto& value : *data->GetEncodedArray()->GetEncodedValues()) {
558 fputc(' ', out_file_);
559 DumpEncodedValue(value.get());
560 }
561 fputs(" }", out_file_);
562 break;
563 }
564 case DexFile::kDexAnnotationAnnotation: {
565 DumpEncodedAnnotation(data->GetEncodedAnnotation());
566 break;
567 }
568 case DexFile::kDexAnnotationNull:
569 fputs("null", out_file_);
570 break;
571 case DexFile::kDexAnnotationBoolean:
572 fputs(StrBool(data->GetBoolean()), out_file_);
573 break;
574 default:
575 fputs("????", out_file_);
576 break;
577 } // switch
578}
579
580/*
581 * Dumps the file header.
582 */
583void DexLayout::DumpFileHeader() {
584 char sanitized[8 * 2 + 1];
Jeff Haoea7c6292016-11-14 18:10:16 -0800585 fprintf(out_file_, "DEX file header:\n");
586 Asciify(sanitized, header_->Magic(), 8);
587 fprintf(out_file_, "magic : '%s'\n", sanitized);
588 fprintf(out_file_, "checksum : %08x\n", header_->Checksum());
589 fprintf(out_file_, "signature : %02x%02x...%02x%02x\n",
590 header_->Signature()[0], header_->Signature()[1],
591 header_->Signature()[DexFile::kSha1DigestSize - 2],
592 header_->Signature()[DexFile::kSha1DigestSize - 1]);
593 fprintf(out_file_, "file_size : %d\n", header_->FileSize());
594 fprintf(out_file_, "header_size : %d\n", header_->HeaderSize());
595 fprintf(out_file_, "link_size : %d\n", header_->LinkSize());
596 fprintf(out_file_, "link_off : %d (0x%06x)\n",
597 header_->LinkOffset(), header_->LinkOffset());
David Sehr2b5a38f2018-06-14 15:13:04 -0700598 fprintf(out_file_, "string_ids_size : %d\n", header_->StringIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800599 fprintf(out_file_, "string_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700600 header_->StringIds().GetOffset(), header_->StringIds().GetOffset());
601 fprintf(out_file_, "type_ids_size : %d\n", header_->TypeIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800602 fprintf(out_file_, "type_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700603 header_->TypeIds().GetOffset(), header_->TypeIds().GetOffset());
604 fprintf(out_file_, "proto_ids_size : %d\n", header_->ProtoIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800605 fprintf(out_file_, "proto_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700606 header_->ProtoIds().GetOffset(), header_->ProtoIds().GetOffset());
607 fprintf(out_file_, "field_ids_size : %d\n", header_->FieldIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800608 fprintf(out_file_, "field_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700609 header_->FieldIds().GetOffset(), header_->FieldIds().GetOffset());
610 fprintf(out_file_, "method_ids_size : %d\n", header_->MethodIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800611 fprintf(out_file_, "method_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700612 header_->MethodIds().GetOffset(), header_->MethodIds().GetOffset());
613 fprintf(out_file_, "class_defs_size : %d\n", header_->ClassDefs().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800614 fprintf(out_file_, "class_defs_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700615 header_->ClassDefs().GetOffset(), header_->ClassDefs().GetOffset());
Jeff Haoea7c6292016-11-14 18:10:16 -0800616 fprintf(out_file_, "data_size : %d\n", header_->DataSize());
617 fprintf(out_file_, "data_off : %d (0x%06x)\n\n",
618 header_->DataOffset(), header_->DataOffset());
619}
620
621/*
622 * Dumps a class_def_item.
623 */
624void DexLayout::DumpClassDef(int idx) {
625 // General class information.
David Sehr2b5a38f2018-06-14 15:13:04 -0700626 dex_ir::ClassDef* class_def = header_->ClassDefs()[idx];
Jeff Haoea7c6292016-11-14 18:10:16 -0800627 fprintf(out_file_, "Class #%d header:\n", idx);
628 fprintf(out_file_, "class_idx : %d\n", class_def->ClassType()->GetIndex());
629 fprintf(out_file_, "access_flags : %d (0x%04x)\n",
630 class_def->GetAccessFlags(), class_def->GetAccessFlags());
631 uint32_t superclass_idx = class_def->Superclass() == nullptr ?
632 DexFile::kDexNoIndex16 : class_def->Superclass()->GetIndex();
633 fprintf(out_file_, "superclass_idx : %d\n", superclass_idx);
634 fprintf(out_file_, "interfaces_off : %d (0x%06x)\n",
635 class_def->InterfacesOffset(), class_def->InterfacesOffset());
636 uint32_t source_file_offset = 0xffffffffU;
637 if (class_def->SourceFile() != nullptr) {
638 source_file_offset = class_def->SourceFile()->GetIndex();
639 }
640 fprintf(out_file_, "source_file_idx : %d\n", source_file_offset);
641 uint32_t annotations_offset = 0;
642 if (class_def->Annotations() != nullptr) {
643 annotations_offset = class_def->Annotations()->GetOffset();
644 }
645 fprintf(out_file_, "annotations_off : %d (0x%06x)\n",
646 annotations_offset, annotations_offset);
647 if (class_def->GetClassData() == nullptr) {
648 fprintf(out_file_, "class_data_off : %d (0x%06x)\n", 0, 0);
649 } else {
650 fprintf(out_file_, "class_data_off : %d (0x%06x)\n",
651 class_def->GetClassData()->GetOffset(), class_def->GetClassData()->GetOffset());
652 }
653
654 // Fields and methods.
655 dex_ir::ClassData* class_data = class_def->GetClassData();
656 if (class_data != nullptr && class_data->StaticFields() != nullptr) {
657 fprintf(out_file_, "static_fields_size : %zu\n", class_data->StaticFields()->size());
658 } else {
659 fprintf(out_file_, "static_fields_size : 0\n");
660 }
661 if (class_data != nullptr && class_data->InstanceFields() != nullptr) {
662 fprintf(out_file_, "instance_fields_size: %zu\n", class_data->InstanceFields()->size());
663 } else {
664 fprintf(out_file_, "instance_fields_size: 0\n");
665 }
666 if (class_data != nullptr && class_data->DirectMethods() != nullptr) {
667 fprintf(out_file_, "direct_methods_size : %zu\n", class_data->DirectMethods()->size());
668 } else {
669 fprintf(out_file_, "direct_methods_size : 0\n");
670 }
671 if (class_data != nullptr && class_data->VirtualMethods() != nullptr) {
672 fprintf(out_file_, "virtual_methods_size: %zu\n", class_data->VirtualMethods()->size());
673 } else {
674 fprintf(out_file_, "virtual_methods_size: 0\n");
675 }
676 fprintf(out_file_, "\n");
677}
678
679/**
680 * Dumps an annotation set item.
681 */
682void DexLayout::DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item) {
683 if (set_item == nullptr || set_item->GetItems()->size() == 0) {
684 fputs(" empty-annotation-set\n", out_file_);
685 return;
686 }
687 for (dex_ir::AnnotationItem* annotation : *set_item->GetItems()) {
688 if (annotation == nullptr) {
689 continue;
690 }
691 fputs(" ", out_file_);
692 switch (annotation->GetVisibility()) {
693 case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", out_file_); break;
694 case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", out_file_); break;
695 case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", out_file_); break;
696 default: fputs("VISIBILITY_UNKNOWN ", out_file_); break;
697 } // switch
698 DumpEncodedAnnotation(annotation->GetAnnotation());
699 fputc('\n', out_file_);
700 }
701}
702
703/*
704 * Dumps class annotations.
705 */
706void DexLayout::DumpClassAnnotations(int idx) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700707 dex_ir::ClassDef* class_def = header_->ClassDefs()[idx];
Jeff Haoea7c6292016-11-14 18:10:16 -0800708 dex_ir::AnnotationsDirectoryItem* annotations_directory = class_def->Annotations();
709 if (annotations_directory == nullptr) {
710 return; // none
711 }
712
713 fprintf(out_file_, "Class #%d annotations:\n", idx);
714
715 dex_ir::AnnotationSetItem* class_set_item = annotations_directory->GetClassAnnotation();
716 dex_ir::FieldAnnotationVector* fields = annotations_directory->GetFieldAnnotations();
717 dex_ir::MethodAnnotationVector* methods = annotations_directory->GetMethodAnnotations();
718 dex_ir::ParameterAnnotationVector* parameters = annotations_directory->GetParameterAnnotations();
719
720 // Annotations on the class itself.
721 if (class_set_item != nullptr) {
722 fprintf(out_file_, "Annotations on class\n");
723 DumpAnnotationSetItem(class_set_item);
724 }
725
726 // Annotations on fields.
727 if (fields != nullptr) {
728 for (auto& field : *fields) {
729 const dex_ir::FieldId* field_id = field->GetFieldId();
730 const uint32_t field_idx = field_id->GetIndex();
731 const char* field_name = field_id->Name()->Data();
732 fprintf(out_file_, "Annotations on field #%u '%s'\n", field_idx, field_name);
733 DumpAnnotationSetItem(field->GetAnnotationSetItem());
734 }
735 }
736
737 // Annotations on methods.
738 if (methods != nullptr) {
739 for (auto& method : *methods) {
740 const dex_ir::MethodId* method_id = method->GetMethodId();
741 const uint32_t method_idx = method_id->GetIndex();
742 const char* method_name = method_id->Name()->Data();
743 fprintf(out_file_, "Annotations on method #%u '%s'\n", method_idx, method_name);
744 DumpAnnotationSetItem(method->GetAnnotationSetItem());
745 }
746 }
747
748 // Annotations on method parameters.
749 if (parameters != nullptr) {
750 for (auto& parameter : *parameters) {
751 const dex_ir::MethodId* method_id = parameter->GetMethodId();
752 const uint32_t method_idx = method_id->GetIndex();
753 const char* method_name = method_id->Name()->Data();
754 fprintf(out_file_, "Annotations on method #%u '%s' parameters\n", method_idx, method_name);
755 uint32_t j = 0;
756 for (dex_ir::AnnotationSetItem* annotation : *parameter->GetAnnotations()->GetItems()) {
757 fprintf(out_file_, "#%u\n", j);
758 DumpAnnotationSetItem(annotation);
759 ++j;
760 }
761 }
762 }
763
764 fputc('\n', out_file_);
765}
766
767/*
768 * Dumps an interface that a class declares to implement.
769 */
770void DexLayout::DumpInterface(const dex_ir::TypeId* type_item, int i) {
771 const char* interface_name = type_item->GetStringId()->Data();
772 if (options_.output_format_ == kOutputPlain) {
773 fprintf(out_file_, " #%d : '%s'\n", i, interface_name);
774 } else {
Orion Hodsonfe42d212018-08-24 14:01:14 +0100775 std::string dot(DescriptorToDot(interface_name));
Jeff Haoea7c6292016-11-14 18:10:16 -0800776 fprintf(out_file_, "<implements name=\"%s\">\n</implements>\n", dot.c_str());
777 }
778}
779
780/*
781 * Dumps the catches table associated with the code.
782 */
783void DexLayout::DumpCatches(const dex_ir::CodeItem* code) {
784 const uint16_t tries_size = code->TriesSize();
785
786 // No catch table.
787 if (tries_size == 0) {
788 fprintf(out_file_, " catches : (none)\n");
789 return;
790 }
791
792 // Dump all table entries.
793 fprintf(out_file_, " catches : %d\n", tries_size);
794 std::vector<std::unique_ptr<const dex_ir::TryItem>>* tries = code->Tries();
795 for (uint32_t i = 0; i < tries_size; i++) {
796 const dex_ir::TryItem* try_item = (*tries)[i].get();
797 const uint32_t start = try_item->StartAddr();
798 const uint32_t end = start + try_item->InsnCount();
799 fprintf(out_file_, " 0x%04x - 0x%04x\n", start, end);
800 for (auto& handler : *try_item->GetHandlers()->GetHandlers()) {
801 const dex_ir::TypeId* type_id = handler->GetTypeId();
802 const char* descriptor = (type_id == nullptr) ? "<any>" : type_id->GetStringId()->Data();
803 fprintf(out_file_, " %s -> 0x%04x\n", descriptor, handler->GetAddress());
804 } // for
805 } // for
806}
807
808/*
David Sehr7629f602016-08-07 16:01:51 -0700809 * Dumps a single instruction.
810 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800811void DexLayout::DumpInstruction(const dex_ir::CodeItem* code,
812 uint32_t code_offset,
813 uint32_t insn_idx,
814 uint32_t insn_width,
815 const Instruction* dec_insn) {
David Sehr7629f602016-08-07 16:01:51 -0700816 // Address of instruction (expressed as byte offset).
817 fprintf(out_file_, "%06x:", code_offset + 0x10 + insn_idx * 2);
818
819 // Dump (part of) raw bytes.
820 const uint16_t* insns = code->Insns();
821 for (uint32_t i = 0; i < 8; i++) {
822 if (i < insn_width) {
823 if (i == 7) {
824 fprintf(out_file_, " ... ");
825 } else {
826 // Print 16-bit value in little-endian order.
827 const uint8_t* bytePtr = (const uint8_t*) &insns[insn_idx + i];
828 fprintf(out_file_, " %02x%02x", bytePtr[0], bytePtr[1]);
829 }
830 } else {
831 fputs(" ", out_file_);
832 }
833 } // for
834
835 // Dump pseudo-instruction or opcode.
836 if (dec_insn->Opcode() == Instruction::NOP) {
837 const uint16_t instr = Get2LE((const uint8_t*) &insns[insn_idx]);
838 if (instr == Instruction::kPackedSwitchSignature) {
839 fprintf(out_file_, "|%04x: packed-switch-data (%d units)", insn_idx, insn_width);
840 } else if (instr == Instruction::kSparseSwitchSignature) {
841 fprintf(out_file_, "|%04x: sparse-switch-data (%d units)", insn_idx, insn_width);
842 } else if (instr == Instruction::kArrayDataSignature) {
843 fprintf(out_file_, "|%04x: array-data (%d units)", insn_idx, insn_width);
844 } else {
845 fprintf(out_file_, "|%04x: nop // spacer", insn_idx);
846 }
847 } else {
848 fprintf(out_file_, "|%04x: %s", insn_idx, dec_insn->Name());
849 }
850
851 // Set up additional argument.
852 std::unique_ptr<char[]> index_buf;
853 if (Instruction::IndexTypeOf(dec_insn->Opcode()) != Instruction::kIndexNone) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800854 index_buf = IndexString(header_, dec_insn, 200);
David Sehr7629f602016-08-07 16:01:51 -0700855 }
856
857 // Dump the instruction.
858 //
859 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
860 //
861 switch (Instruction::FormatOf(dec_insn->Opcode())) {
862 case Instruction::k10x: // op
863 break;
864 case Instruction::k12x: // op vA, vB
865 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
866 break;
867 case Instruction::k11n: // op vA, #+B
868 fprintf(out_file_, " v%d, #int %d // #%x",
869 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint8_t)dec_insn->VRegB());
870 break;
871 case Instruction::k11x: // op vAA
872 fprintf(out_file_, " v%d", dec_insn->VRegA());
873 break;
874 case Instruction::k10t: // op +AA
875 case Instruction::k20t: { // op +AAAA
876 const int32_t targ = (int32_t) dec_insn->VRegA();
877 fprintf(out_file_, " %04x // %c%04x",
878 insn_idx + targ,
879 (targ < 0) ? '-' : '+',
880 (targ < 0) ? -targ : targ);
881 break;
882 }
883 case Instruction::k22x: // op vAA, vBBBB
884 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
885 break;
886 case Instruction::k21t: { // op vAA, +BBBB
887 const int32_t targ = (int32_t) dec_insn->VRegB();
888 fprintf(out_file_, " v%d, %04x // %c%04x", dec_insn->VRegA(),
889 insn_idx + targ,
890 (targ < 0) ? '-' : '+',
891 (targ < 0) ? -targ : targ);
892 break;
893 }
894 case Instruction::k21s: // op vAA, #+BBBB
895 fprintf(out_file_, " v%d, #int %d // #%x",
896 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint16_t)dec_insn->VRegB());
897 break;
898 case Instruction::k21h: // op vAA, #+BBBB0000[00000000]
899 // The printed format varies a bit based on the actual opcode.
900 if (dec_insn->Opcode() == Instruction::CONST_HIGH16) {
901 const int32_t value = dec_insn->VRegB() << 16;
902 fprintf(out_file_, " v%d, #int %d // #%x",
903 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
904 } else {
905 const int64_t value = ((int64_t) dec_insn->VRegB()) << 48;
906 fprintf(out_file_, " v%d, #long %" PRId64 " // #%x",
907 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
908 }
909 break;
910 case Instruction::k21c: // op vAA, thing@BBBB
911 case Instruction::k31c: // op vAA, thing@BBBBBBBB
912 fprintf(out_file_, " v%d, %s", dec_insn->VRegA(), index_buf.get());
913 break;
914 case Instruction::k23x: // op vAA, vBB, vCC
915 fprintf(out_file_, " v%d, v%d, v%d",
916 dec_insn->VRegA(), dec_insn->VRegB(), dec_insn->VRegC());
917 break;
918 case Instruction::k22b: // op vAA, vBB, #+CC
919 fprintf(out_file_, " v%d, v%d, #int %d // #%02x",
920 dec_insn->VRegA(), dec_insn->VRegB(),
921 (int32_t) dec_insn->VRegC(), (uint8_t) dec_insn->VRegC());
922 break;
923 case Instruction::k22t: { // op vA, vB, +CCCC
924 const int32_t targ = (int32_t) dec_insn->VRegC();
925 fprintf(out_file_, " v%d, v%d, %04x // %c%04x",
926 dec_insn->VRegA(), dec_insn->VRegB(),
927 insn_idx + targ,
928 (targ < 0) ? '-' : '+',
929 (targ < 0) ? -targ : targ);
930 break;
931 }
932 case Instruction::k22s: // op vA, vB, #+CCCC
933 fprintf(out_file_, " v%d, v%d, #int %d // #%04x",
934 dec_insn->VRegA(), dec_insn->VRegB(),
935 (int32_t) dec_insn->VRegC(), (uint16_t) dec_insn->VRegC());
936 break;
937 case Instruction::k22c: // op vA, vB, thing@CCCC
938 // NOT SUPPORTED:
939 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
940 fprintf(out_file_, " v%d, v%d, %s",
941 dec_insn->VRegA(), dec_insn->VRegB(), index_buf.get());
942 break;
943 case Instruction::k30t:
944 fprintf(out_file_, " #%08x", dec_insn->VRegA());
945 break;
946 case Instruction::k31i: { // op vAA, #+BBBBBBBB
947 // This is often, but not always, a float.
948 union {
949 float f;
950 uint32_t i;
951 } conv;
952 conv.i = dec_insn->VRegB();
953 fprintf(out_file_, " v%d, #float %g // #%08x",
954 dec_insn->VRegA(), conv.f, dec_insn->VRegB());
955 break;
956 }
957 case Instruction::k31t: // op vAA, offset +BBBBBBBB
958 fprintf(out_file_, " v%d, %08x // +%08x",
959 dec_insn->VRegA(), insn_idx + dec_insn->VRegB(), dec_insn->VRegB());
960 break;
961 case Instruction::k32x: // op vAAAA, vBBBB
962 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
963 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100964 case Instruction::k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
965 case Instruction::k45cc: { // op {vC, vD, vE, vF, vG}, meth@BBBB, proto@HHHH
David Sehr7629f602016-08-07 16:01:51 -0700966 // NOT SUPPORTED:
967 // case Instruction::k35ms: // [opt] invoke-virtual+super
968 // case Instruction::k35mi: // [opt] inline invoke
969 uint32_t arg[Instruction::kMaxVarArgRegs];
970 dec_insn->GetVarArgs(arg);
971 fputs(" {", out_file_);
972 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
973 if (i == 0) {
974 fprintf(out_file_, "v%d", arg[i]);
975 } else {
976 fprintf(out_file_, ", v%d", arg[i]);
977 }
978 } // for
979 fprintf(out_file_, "}, %s", index_buf.get());
980 break;
981 }
Orion Hodsonb34bb192016-10-18 17:02:58 +0100982 case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
983 case Instruction::k4rcc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB, proto@HHHH
David Sehr7629f602016-08-07 16:01:51 -0700984 // NOT SUPPORTED:
985 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
986 // case Instruction::k3rmi: // [opt] execute-inline/range
987 {
988 // This doesn't match the "dx" output when some of the args are
989 // 64-bit values -- dx only shows the first register.
990 fputs(" {", out_file_);
991 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
992 if (i == 0) {
993 fprintf(out_file_, "v%d", dec_insn->VRegC() + i);
994 } else {
995 fprintf(out_file_, ", v%d", dec_insn->VRegC() + i);
996 }
997 } // for
998 fprintf(out_file_, "}, %s", index_buf.get());
999 }
1000 break;
1001 case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB
1002 // This is often, but not always, a double.
1003 union {
1004 double d;
1005 uint64_t j;
1006 } conv;
1007 conv.j = dec_insn->WideVRegB();
1008 fprintf(out_file_, " v%d, #double %g // #%016" PRIx64,
1009 dec_insn->VRegA(), conv.d, dec_insn->WideVRegB());
1010 break;
1011 }
1012 // NOT SUPPORTED:
1013 // case Instruction::k00x: // unknown op or breakpoint
1014 // break;
1015 default:
1016 fprintf(out_file_, " ???");
1017 break;
1018 } // switch
1019
1020 fputc('\n', out_file_);
1021}
1022
1023/*
1024 * Dumps a bytecode disassembly.
1025 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001026void DexLayout::DumpBytecodes(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001027 dex_ir::MethodId* method_id = header_->MethodIds()[idx];
David Sehr7629f602016-08-07 16:01:51 -07001028 const char* name = method_id->Name()->Data();
David Sehr72359222016-09-07 13:04:01 -07001029 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -07001030 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1031
1032 // Generate header.
Orion Hodsonfe42d212018-08-24 14:01:14 +01001033 std::string dot(DescriptorToDot(back_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001034 fprintf(out_file_, "%06x: |[%06x] %s.%s:%s\n",
David Sehr72359222016-09-07 13:04:01 -07001035 code_offset, code_offset, dot.c_str(), name, type_descriptor.c_str());
David Sehr7629f602016-08-07 16:01:51 -07001036
1037 // Iterate over all instructions.
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001038 for (const DexInstructionPcPair& inst : code->Instructions()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001039 const uint32_t insn_width = inst->SizeInCodeUnits();
David Sehr7629f602016-08-07 16:01:51 -07001040 if (insn_width == 0) {
Andreas Gampe221d9812018-01-22 17:48:56 -08001041 LOG(WARNING) << "GLITCH: zero-width instruction at idx=0x" << std::hex << inst.DexPc();
David Sehr7629f602016-08-07 16:01:51 -07001042 break;
1043 }
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001044 DumpInstruction(code, code_offset, inst.DexPc(), insn_width, &inst.Inst());
David Sehr7629f602016-08-07 16:01:51 -07001045 } // for
1046}
1047
1048/*
David Sehraa6abb02017-10-12 08:25:11 -07001049 * Lookup functions.
1050 */
David Sehr2b5a38f2018-06-14 15:13:04 -07001051static const char* StringDataByIdx(uint32_t idx, dex_ir::Header* header) {
1052 dex_ir::StringId* string_id = header->GetStringIdOrNullPtr(idx);
David Sehraa6abb02017-10-12 08:25:11 -07001053 if (string_id == nullptr) {
1054 return nullptr;
1055 }
1056 return string_id->Data();
1057}
1058
David Sehr2b5a38f2018-06-14 15:13:04 -07001059static const char* StringDataByTypeIdx(uint16_t idx, dex_ir::Header* header) {
1060 dex_ir::TypeId* type_id = header->GetTypeIdOrNullPtr(idx);
David Sehraa6abb02017-10-12 08:25:11 -07001061 if (type_id == nullptr) {
1062 return nullptr;
1063 }
1064 dex_ir::StringId* string_id = type_id->GetStringId();
1065 if (string_id == nullptr) {
1066 return nullptr;
1067 }
1068 return string_id->Data();
1069}
1070
1071
1072/*
David Sehr7629f602016-08-07 16:01:51 -07001073 * Dumps code of a method.
1074 */
David Sehraa6abb02017-10-12 08:25:11 -07001075void DexLayout::DumpCode(uint32_t idx,
1076 const dex_ir::CodeItem* code,
1077 uint32_t code_offset,
1078 const char* declaring_class_descriptor,
1079 const char* method_name,
1080 bool is_static,
1081 const dex_ir::ProtoId* proto) {
David Sehr7629f602016-08-07 16:01:51 -07001082 fprintf(out_file_, " registers : %d\n", code->RegistersSize());
1083 fprintf(out_file_, " ins : %d\n", code->InsSize());
1084 fprintf(out_file_, " outs : %d\n", code->OutsSize());
1085 fprintf(out_file_, " insns size : %d 16-bit code units\n",
1086 code->InsnsSize());
1087
1088 // Bytecode disassembly, if requested.
1089 if (options_.disassemble_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001090 DumpBytecodes(idx, code, code_offset);
David Sehr7629f602016-08-07 16:01:51 -07001091 }
1092
1093 // Try-catch blocks.
1094 DumpCatches(code);
1095
1096 // Positions and locals table in the debug info.
David Sehraa6abb02017-10-12 08:25:11 -07001097 dex_ir::DebugInfoItem* debug_info = code->DebugInfo();
David Sehr7629f602016-08-07 16:01:51 -07001098 fprintf(out_file_, " positions : \n");
David Sehraa6abb02017-10-12 08:25:11 -07001099 if (debug_info != nullptr) {
1100 DexFile::DecodeDebugPositionInfo(debug_info->GetDebugInfo(),
1101 [this](uint32_t idx) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001102 return StringDataByIdx(idx, this->header_);
David Sehraa6abb02017-10-12 08:25:11 -07001103 },
Mathieu Chartier3e2e1232018-09-11 12:35:30 -07001104 [&](const DexFile::PositionInfo& entry) {
1105 fprintf(out_file_,
1106 " 0x%04x line=%d\n",
1107 entry.address_,
1108 entry.line_);
1109 return false;
1110 });
David Sehraa6abb02017-10-12 08:25:11 -07001111 }
David Sehr7629f602016-08-07 16:01:51 -07001112 fprintf(out_file_, " locals : \n");
David Sehraa6abb02017-10-12 08:25:11 -07001113 if (debug_info != nullptr) {
1114 std::vector<const char*> arg_descriptors;
1115 const dex_ir::TypeList* parameters = proto->Parameters();
1116 if (parameters != nullptr) {
1117 const dex_ir::TypeIdVector* parameter_type_vector = parameters->GetTypeList();
1118 if (parameter_type_vector != nullptr) {
1119 for (const dex_ir::TypeId* type_id : *parameter_type_vector) {
1120 arg_descriptors.push_back(type_id->GetStringId()->Data());
1121 }
1122 }
1123 }
1124 DexFile::DecodeDebugLocalInfo(debug_info->GetDebugInfo(),
1125 "DexLayout in-memory",
1126 declaring_class_descriptor,
1127 arg_descriptors,
1128 method_name,
1129 is_static,
1130 code->RegistersSize(),
1131 code->InsSize(),
1132 code->InsnsSize(),
1133 [this](uint32_t idx) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001134 return StringDataByIdx(idx, this->header_);
David Sehraa6abb02017-10-12 08:25:11 -07001135 },
1136 [this](uint32_t idx) {
1137 return
1138 StringDataByTypeIdx(dchecked_integral_cast<uint16_t>(idx),
David Sehr2b5a38f2018-06-14 15:13:04 -07001139 this->header_);
David Sehraa6abb02017-10-12 08:25:11 -07001140 },
Mathieu Chartiere5afbf32018-09-12 17:51:54 -07001141 [&](const DexFile::LocalInfo& entry) {
1142 const char* signature =
1143 entry.signature_ != nullptr ? entry.signature_ : "";
1144 fprintf(out_file_,
1145 " 0x%04x - 0x%04x reg=%d %s %s %s\n",
1146 entry.start_address_,
1147 entry.end_address_,
1148 entry.reg_,
1149 entry.name_,
1150 entry.descriptor_,
1151 signature);
1152 });
David Sehraa6abb02017-10-12 08:25:11 -07001153 }
David Sehr7629f602016-08-07 16:01:51 -07001154}
1155
1156/*
1157 * Dumps a method.
1158 */
David Brazdil20c765f2018-10-27 21:45:15 +00001159void DexLayout::DumpMethod(uint32_t idx,
1160 uint32_t flags,
1161 uint32_t hiddenapi_flags,
1162 const dex_ir::CodeItem* code,
1163 int i) {
David Sehr7629f602016-08-07 16:01:51 -07001164 // Bail for anything private if export only requested.
1165 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1166 return;
1167 }
1168
David Sehr2b5a38f2018-06-14 15:13:04 -07001169 dex_ir::MethodId* method_id = header_->MethodIds()[idx];
David Sehr7629f602016-08-07 16:01:51 -07001170 const char* name = method_id->Name()->Data();
1171 char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str());
1172 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1173 char* access_str = CreateAccessFlagStr(flags, kAccessForMethod);
1174
1175 if (options_.output_format_ == kOutputPlain) {
1176 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1177 fprintf(out_file_, " name : '%s'\n", name);
1178 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1179 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
David Brazdil20c765f2018-10-27 21:45:15 +00001180 if (hiddenapi_flags != 0u) {
David Brazdildcfa89b2018-10-31 11:04:10 +00001181 fprintf(out_file_,
1182 " hiddenapi : 0x%04x (%s)\n",
1183 hiddenapi_flags,
1184 GetHiddenapiFlagStr(hiddenapi_flags).c_str());
David Brazdil20c765f2018-10-27 21:45:15 +00001185 }
David Sehr7629f602016-08-07 16:01:51 -07001186 if (code == nullptr) {
1187 fprintf(out_file_, " code : (none)\n");
1188 } else {
1189 fprintf(out_file_, " code -\n");
David Sehraa6abb02017-10-12 08:25:11 -07001190 DumpCode(idx,
1191 code,
1192 code->GetOffset(),
1193 back_descriptor,
1194 name,
1195 (flags & kAccStatic) != 0,
1196 method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -07001197 }
1198 if (options_.disassemble_) {
1199 fputc('\n', out_file_);
1200 }
1201 } else if (options_.output_format_ == kOutputXml) {
1202 const bool constructor = (name[0] == '<');
1203
1204 // Method name and prototype.
1205 if (constructor) {
Orion Hodsonfe42d212018-08-24 14:01:14 +01001206 std::string dot(DescriptorClassToName(back_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001207 fprintf(out_file_, "<constructor name=\"%s\"\n", dot.c_str());
Orion Hodsonfe42d212018-08-24 14:01:14 +01001208 dot = DescriptorToDot(back_descriptor);
David Sehr7629f602016-08-07 16:01:51 -07001209 fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
1210 } else {
1211 fprintf(out_file_, "<method name=\"%s\"\n", name);
1212 const char* return_type = strrchr(type_descriptor, ')');
1213 if (return_type == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08001214 LOG(ERROR) << "bad method type descriptor '" << type_descriptor << "'";
David Sehr7629f602016-08-07 16:01:51 -07001215 goto bail;
1216 }
Orion Hodsonfe42d212018-08-24 14:01:14 +01001217 std::string dot(DescriptorToDot(return_type + 1));
David Sehr7629f602016-08-07 16:01:51 -07001218 fprintf(out_file_, " return=\"%s\"\n", dot.c_str());
1219 fprintf(out_file_, " abstract=%s\n", QuotedBool((flags & kAccAbstract) != 0));
1220 fprintf(out_file_, " native=%s\n", QuotedBool((flags & kAccNative) != 0));
1221 fprintf(out_file_, " synchronized=%s\n", QuotedBool(
1222 (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0));
1223 }
1224
1225 // Additional method flags.
1226 fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
1227 fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
1228 // The "deprecated=" not knowable w/o parsing annotations.
1229 fprintf(out_file_, " visibility=%s\n>\n", QuotedVisibility(flags));
1230
1231 // Parameters.
1232 if (type_descriptor[0] != '(') {
Andreas Gampe221d9812018-01-22 17:48:56 -08001233 LOG(ERROR) << "ERROR: bad descriptor '" << type_descriptor << "'";
David Sehr7629f602016-08-07 16:01:51 -07001234 goto bail;
1235 }
1236 char* tmp_buf = reinterpret_cast<char*>(malloc(strlen(type_descriptor) + 1));
1237 const char* base = type_descriptor + 1;
1238 int arg_num = 0;
1239 while (*base != ')') {
1240 char* cp = tmp_buf;
1241 while (*base == '[') {
1242 *cp++ = *base++;
1243 }
1244 if (*base == 'L') {
1245 // Copy through ';'.
1246 do {
1247 *cp = *base++;
1248 } while (*cp++ != ';');
1249 } else {
1250 // Primitive char, copy it.
1251 if (strchr("ZBCSIFJD", *base) == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08001252 LOG(ERROR) << "ERROR: bad method signature '" << base << "'";
David Sehr7629f602016-08-07 16:01:51 -07001253 break; // while
1254 }
1255 *cp++ = *base++;
1256 }
1257 // Null terminate and display.
1258 *cp++ = '\0';
Orion Hodsonfe42d212018-08-24 14:01:14 +01001259 std::string dot(DescriptorToDot(tmp_buf));
David Sehr7629f602016-08-07 16:01:51 -07001260 fprintf(out_file_, "<parameter name=\"arg%d\" type=\"%s\">\n"
1261 "</parameter>\n", arg_num++, dot.c_str());
1262 } // while
1263 free(tmp_buf);
1264 if (constructor) {
1265 fprintf(out_file_, "</constructor>\n");
1266 } else {
1267 fprintf(out_file_, "</method>\n");
1268 }
1269 }
1270
1271 bail:
1272 free(type_descriptor);
1273 free(access_str);
1274}
1275
1276/*
1277 * Dumps a static (class) field.
1278 */
David Brazdil20c765f2018-10-27 21:45:15 +00001279void DexLayout::DumpSField(uint32_t idx,
1280 uint32_t flags,
1281 uint32_t hiddenapi_flags,
1282 int i,
1283 dex_ir::EncodedValue* init) {
David Sehr7629f602016-08-07 16:01:51 -07001284 // Bail for anything private if export only requested.
1285 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1286 return;
1287 }
1288
David Sehr2b5a38f2018-06-14 15:13:04 -07001289 dex_ir::FieldId* field_id = header_->FieldIds()[idx];
David Sehr7629f602016-08-07 16:01:51 -07001290 const char* name = field_id->Name()->Data();
1291 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
1292 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
1293 char* access_str = CreateAccessFlagStr(flags, kAccessForField);
1294
1295 if (options_.output_format_ == kOutputPlain) {
1296 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1297 fprintf(out_file_, " name : '%s'\n", name);
1298 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1299 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
David Brazdil20c765f2018-10-27 21:45:15 +00001300 if (hiddenapi_flags != 0u) {
David Brazdildcfa89b2018-10-31 11:04:10 +00001301 fprintf(out_file_,
1302 " hiddenapi : 0x%04x (%s)\n",
1303 hiddenapi_flags,
1304 GetHiddenapiFlagStr(hiddenapi_flags).c_str());
David Brazdil20c765f2018-10-27 21:45:15 +00001305 }
David Sehr7629f602016-08-07 16:01:51 -07001306 if (init != nullptr) {
1307 fputs(" value : ", out_file_);
1308 DumpEncodedValue(init);
1309 fputs("\n", out_file_);
1310 }
1311 } else if (options_.output_format_ == kOutputXml) {
1312 fprintf(out_file_, "<field name=\"%s\"\n", name);
Orion Hodsonfe42d212018-08-24 14:01:14 +01001313 std::string dot(DescriptorToDot(type_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001314 fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
1315 fprintf(out_file_, " transient=%s\n", QuotedBool((flags & kAccTransient) != 0));
1316 fprintf(out_file_, " volatile=%s\n", QuotedBool((flags & kAccVolatile) != 0));
1317 // The "value=" is not knowable w/o parsing annotations.
1318 fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
1319 fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
1320 // The "deprecated=" is not knowable w/o parsing annotations.
1321 fprintf(out_file_, " visibility=%s\n", QuotedVisibility(flags));
1322 if (init != nullptr) {
1323 fputs(" value=\"", out_file_);
1324 DumpEncodedValue(init);
1325 fputs("\"\n", out_file_);
1326 }
1327 fputs(">\n</field>\n", out_file_);
1328 }
1329
1330 free(access_str);
1331}
1332
1333/*
1334 * Dumps an instance field.
1335 */
David Brazdil20c765f2018-10-27 21:45:15 +00001336void DexLayout::DumpIField(uint32_t idx,
1337 uint32_t flags,
1338 uint32_t hiddenapi_flags,
1339 int i) {
1340 DumpSField(idx, flags, hiddenapi_flags, i, nullptr);
David Sehr7629f602016-08-07 16:01:51 -07001341}
1342
1343/*
David Sehr7629f602016-08-07 16:01:51 -07001344 * Dumps the class.
1345 *
1346 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1347 *
1348 * If "*last_package" is nullptr or does not match the current class' package,
1349 * the value will be replaced with a newly-allocated string.
1350 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001351void DexLayout::DumpClass(int idx, char** last_package) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001352 dex_ir::ClassDef* class_def = header_->ClassDefs()[idx];
David Sehr7629f602016-08-07 16:01:51 -07001353 // Omitting non-public class.
1354 if (options_.exports_only_ && (class_def->GetAccessFlags() & kAccPublic) == 0) {
1355 return;
1356 }
1357
1358 if (options_.show_section_headers_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001359 DumpClassDef(idx);
David Sehr7629f602016-08-07 16:01:51 -07001360 }
1361
1362 if (options_.show_annotations_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001363 DumpClassAnnotations(idx);
David Sehr7629f602016-08-07 16:01:51 -07001364 }
1365
David Sehr7629f602016-08-07 16:01:51 -07001366 // For the XML output, show the package name. Ideally we'd gather
1367 // up the classes, sort them, and dump them alphabetically so the
1368 // package name wouldn't jump around, but that's not a great plan
1369 // for something that needs to run on the device.
David Sehr2b5a38f2018-06-14 15:13:04 -07001370 const char* class_descriptor = header_->ClassDefs()[idx]->ClassType()->GetStringId()->Data();
David Sehr7629f602016-08-07 16:01:51 -07001371 if (!(class_descriptor[0] == 'L' &&
1372 class_descriptor[strlen(class_descriptor)-1] == ';')) {
1373 // Arrays and primitives should not be defined explicitly. Keep going?
Andreas Gampe221d9812018-01-22 17:48:56 -08001374 LOG(ERROR) << "Malformed class name '" << class_descriptor << "'";
David Sehr7629f602016-08-07 16:01:51 -07001375 } else if (options_.output_format_ == kOutputXml) {
1376 char* mangle = strdup(class_descriptor + 1);
1377 mangle[strlen(mangle)-1] = '\0';
1378
1379 // Reduce to just the package name.
1380 char* last_slash = strrchr(mangle, '/');
1381 if (last_slash != nullptr) {
1382 *last_slash = '\0';
1383 } else {
1384 *mangle = '\0';
1385 }
1386
1387 for (char* cp = mangle; *cp != '\0'; cp++) {
1388 if (*cp == '/') {
1389 *cp = '.';
1390 }
1391 } // for
1392
1393 if (*last_package == nullptr || strcmp(mangle, *last_package) != 0) {
1394 // Start of a new package.
1395 if (*last_package != nullptr) {
1396 fprintf(out_file_, "</package>\n");
1397 }
1398 fprintf(out_file_, "<package name=\"%s\"\n>\n", mangle);
1399 free(*last_package);
1400 *last_package = mangle;
1401 } else {
1402 free(mangle);
1403 }
1404 }
1405
1406 // General class information.
1407 char* access_str = CreateAccessFlagStr(class_def->GetAccessFlags(), kAccessForClass);
1408 const char* superclass_descriptor = nullptr;
1409 if (class_def->Superclass() != nullptr) {
1410 superclass_descriptor = class_def->Superclass()->GetStringId()->Data();
1411 }
1412 if (options_.output_format_ == kOutputPlain) {
1413 fprintf(out_file_, "Class #%d -\n", idx);
1414 fprintf(out_file_, " Class descriptor : '%s'\n", class_descriptor);
1415 fprintf(out_file_, " Access flags : 0x%04x (%s)\n",
1416 class_def->GetAccessFlags(), access_str);
1417 if (superclass_descriptor != nullptr) {
1418 fprintf(out_file_, " Superclass : '%s'\n", superclass_descriptor);
1419 }
1420 fprintf(out_file_, " Interfaces -\n");
1421 } else {
Orion Hodsonfe42d212018-08-24 14:01:14 +01001422 std::string dot(DescriptorClassToName(class_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001423 fprintf(out_file_, "<class name=\"%s\"\n", dot.c_str());
1424 if (superclass_descriptor != nullptr) {
Orion Hodsonfe42d212018-08-24 14:01:14 +01001425 dot = DescriptorToDot(superclass_descriptor);
David Sehr7629f602016-08-07 16:01:51 -07001426 fprintf(out_file_, " extends=\"%s\"\n", dot.c_str());
1427 }
1428 fprintf(out_file_, " interface=%s\n",
1429 QuotedBool((class_def->GetAccessFlags() & kAccInterface) != 0));
1430 fprintf(out_file_, " abstract=%s\n",
1431 QuotedBool((class_def->GetAccessFlags() & kAccAbstract) != 0));
1432 fprintf(out_file_, " static=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccStatic) != 0));
1433 fprintf(out_file_, " final=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccFinal) != 0));
1434 // The "deprecated=" not knowable w/o parsing annotations.
1435 fprintf(out_file_, " visibility=%s\n", QuotedVisibility(class_def->GetAccessFlags()));
1436 fprintf(out_file_, ">\n");
1437 }
1438
1439 // Interfaces.
Jeff Haocc829592017-03-14 16:13:39 -07001440 const dex_ir::TypeList* interfaces = class_def->Interfaces();
David Sehr853a8e12016-09-01 13:03:50 -07001441 if (interfaces != nullptr) {
Jeff Haocc829592017-03-14 16:13:39 -07001442 const dex_ir::TypeIdVector* interfaces_vector = interfaces->GetTypeList();
1443 for (uint32_t i = 0; i < interfaces_vector->size(); i++) {
1444 DumpInterface((*interfaces_vector)[i], i);
David Sehr853a8e12016-09-01 13:03:50 -07001445 } // for
1446 }
David Sehr7629f602016-08-07 16:01:51 -07001447
1448 // Fields and methods.
1449 dex_ir::ClassData* class_data = class_def->GetClassData();
1450 // Prepare data for static fields.
Jeff Hao3ab96b42016-09-09 18:35:01 -07001451 dex_ir::EncodedArrayItem* static_values = class_def->StaticValues();
1452 dex_ir::EncodedValueVector* encoded_values =
1453 static_values == nullptr ? nullptr : static_values->GetEncodedValues();
1454 const uint32_t encoded_values_size = (encoded_values == nullptr) ? 0 : encoded_values->size();
David Sehr7629f602016-08-07 16:01:51 -07001455
1456 // Static fields.
1457 if (options_.output_format_ == kOutputPlain) {
1458 fprintf(out_file_, " Static fields -\n");
1459 }
David Sehr853a8e12016-09-01 13:03:50 -07001460 if (class_data != nullptr) {
1461 dex_ir::FieldItemVector* static_fields = class_data->StaticFields();
1462 if (static_fields != nullptr) {
1463 for (uint32_t i = 0; i < static_fields->size(); i++) {
David Sehrd83437c2018-06-11 14:06:23 -07001464 DumpSField((*static_fields)[i].GetFieldId()->GetIndex(),
1465 (*static_fields)[i].GetAccessFlags(),
David Brazdil20c765f2018-10-27 21:45:15 +00001466 dex_ir::HiddenapiClassData::GetFlags(header_, class_def, &(*static_fields)[i]),
David Sehr853a8e12016-09-01 13:03:50 -07001467 i,
Jeff Hao3ab96b42016-09-09 18:35:01 -07001468 i < encoded_values_size ? (*encoded_values)[i].get() : nullptr);
David Sehr853a8e12016-09-01 13:03:50 -07001469 } // for
1470 }
1471 }
David Sehr7629f602016-08-07 16:01:51 -07001472
1473 // Instance fields.
1474 if (options_.output_format_ == kOutputPlain) {
1475 fprintf(out_file_, " Instance fields -\n");
1476 }
David Sehr853a8e12016-09-01 13:03:50 -07001477 if (class_data != nullptr) {
1478 dex_ir::FieldItemVector* instance_fields = class_data->InstanceFields();
1479 if (instance_fields != nullptr) {
1480 for (uint32_t i = 0; i < instance_fields->size(); i++) {
David Sehrd83437c2018-06-11 14:06:23 -07001481 DumpIField((*instance_fields)[i].GetFieldId()->GetIndex(),
1482 (*instance_fields)[i].GetAccessFlags(),
David Brazdil20c765f2018-10-27 21:45:15 +00001483 dex_ir::HiddenapiClassData::GetFlags(header_, class_def, &(*instance_fields)[i]),
David Sehr853a8e12016-09-01 13:03:50 -07001484 i);
1485 } // for
1486 }
1487 }
David Sehr7629f602016-08-07 16:01:51 -07001488
1489 // Direct methods.
1490 if (options_.output_format_ == kOutputPlain) {
1491 fprintf(out_file_, " Direct methods -\n");
1492 }
David Sehr853a8e12016-09-01 13:03:50 -07001493 if (class_data != nullptr) {
1494 dex_ir::MethodItemVector* direct_methods = class_data->DirectMethods();
1495 if (direct_methods != nullptr) {
1496 for (uint32_t i = 0; i < direct_methods->size(); i++) {
David Sehrd83437c2018-06-11 14:06:23 -07001497 DumpMethod((*direct_methods)[i].GetMethodId()->GetIndex(),
1498 (*direct_methods)[i].GetAccessFlags(),
David Brazdil20c765f2018-10-27 21:45:15 +00001499 dex_ir::HiddenapiClassData::GetFlags(header_, class_def, &(*direct_methods)[i]),
David Sehrd83437c2018-06-11 14:06:23 -07001500 (*direct_methods)[i].GetCodeItem(),
David Brazdil20c765f2018-10-27 21:45:15 +00001501 i);
David Sehr853a8e12016-09-01 13:03:50 -07001502 } // for
1503 }
1504 }
David Sehr7629f602016-08-07 16:01:51 -07001505
1506 // Virtual methods.
1507 if (options_.output_format_ == kOutputPlain) {
1508 fprintf(out_file_, " Virtual methods -\n");
1509 }
David Sehr853a8e12016-09-01 13:03:50 -07001510 if (class_data != nullptr) {
1511 dex_ir::MethodItemVector* virtual_methods = class_data->VirtualMethods();
1512 if (virtual_methods != nullptr) {
1513 for (uint32_t i = 0; i < virtual_methods->size(); i++) {
David Sehrd83437c2018-06-11 14:06:23 -07001514 DumpMethod((*virtual_methods)[i].GetMethodId()->GetIndex(),
1515 (*virtual_methods)[i].GetAccessFlags(),
David Brazdil20c765f2018-10-27 21:45:15 +00001516 dex_ir::HiddenapiClassData::GetFlags(header_, class_def, &(*virtual_methods)[i]),
David Sehrd83437c2018-06-11 14:06:23 -07001517 (*virtual_methods)[i].GetCodeItem(),
David Sehr853a8e12016-09-01 13:03:50 -07001518 i);
1519 } // for
1520 }
1521 }
David Sehr7629f602016-08-07 16:01:51 -07001522
1523 // End of class.
1524 if (options_.output_format_ == kOutputPlain) {
1525 const char* file_name = "unknown";
1526 if (class_def->SourceFile() != nullptr) {
1527 file_name = class_def->SourceFile()->Data();
1528 }
1529 const dex_ir::StringId* source_file = class_def->SourceFile();
1530 fprintf(out_file_, " source_file_idx : %d (%s)\n\n",
Jeff Hao3ab96b42016-09-09 18:35:01 -07001531 source_file == nullptr ? 0xffffffffU : source_file->GetIndex(), file_name);
David Sehr7629f602016-08-07 16:01:51 -07001532 } else if (options_.output_format_ == kOutputXml) {
1533 fprintf(out_file_, "</class>\n");
1534 }
1535
1536 free(access_str);
1537}
1538
Jeff Haoea7c6292016-11-14 18:10:16 -08001539void DexLayout::DumpDexFile() {
David Sehr7629f602016-08-07 16:01:51 -07001540 // Headers.
1541 if (options_.show_file_headers_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001542 DumpFileHeader();
David Sehr7629f602016-08-07 16:01:51 -07001543 }
1544
1545 // Open XML context.
1546 if (options_.output_format_ == kOutputXml) {
1547 fprintf(out_file_, "<api>\n");
1548 }
1549
1550 // Iterate over all classes.
1551 char* package = nullptr;
David Sehr2b5a38f2018-06-14 15:13:04 -07001552 const uint32_t class_defs_size = header_->ClassDefs().Size();
David Sehr7629f602016-08-07 16:01:51 -07001553 for (uint32_t i = 0; i < class_defs_size; i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001554 DumpClass(i, &package);
David Sehr7629f602016-08-07 16:01:51 -07001555 } // for
1556
1557 // Free the last package allocated.
1558 if (package != nullptr) {
1559 fprintf(out_file_, "</package>\n");
1560 free(package);
1561 }
1562
1563 // Close XML context.
1564 if (options_.output_format_ == kOutputXml) {
1565 fprintf(out_file_, "</api>\n");
1566 }
Jeff Haoea7c6292016-11-14 18:10:16 -08001567}
Jeff Hao3ab96b42016-09-09 18:35:01 -07001568
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001569void DexLayout::LayoutClassDefsAndClassData(const DexFile* dex_file) {
Jeff Hao042e8982016-10-19 11:17:11 -07001570 std::vector<dex_ir::ClassDef*> new_class_def_order;
David Sehr2b5a38f2018-06-14 15:13:04 -07001571 for (auto& class_def : header_->ClassDefs()) {
Jeff Hao042e8982016-10-19 11:17:11 -07001572 dex::TypeIndex type_idx(class_def->ClassType()->GetIndex());
1573 if (info_->ContainsClass(*dex_file, type_idx)) {
1574 new_class_def_order.push_back(class_def.get());
1575 }
1576 }
David Sehr2b5a38f2018-06-14 15:13:04 -07001577 for (auto& class_def : header_->ClassDefs()) {
Jeff Hao042e8982016-10-19 11:17:11 -07001578 dex::TypeIndex type_idx(class_def->ClassType()->GetIndex());
1579 if (!info_->ContainsClass(*dex_file, type_idx)) {
1580 new_class_def_order.push_back(class_def.get());
1581 }
1582 }
Jeff Haoe17f5892017-02-23 16:14:04 -08001583 std::unordered_set<dex_ir::ClassData*> visited_class_data;
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001584 size_t class_data_index = 0;
David Sehr2b5a38f2018-06-14 15:13:04 -07001585 auto& class_datas = header_->ClassDatas();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001586 for (dex_ir::ClassDef* class_def : new_class_def_order) {
Jeff Haoe17f5892017-02-23 16:14:04 -08001587 dex_ir::ClassData* class_data = class_def->GetClassData();
1588 if (class_data != nullptr && visited_class_data.find(class_data) == visited_class_data.end()) {
Jeff Haoe17f5892017-02-23 16:14:04 -08001589 visited_class_data.insert(class_data);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001590 // Overwrite the existing vector with the new ordering, note that the sets of objects are
1591 // equivalent, but the order changes. This is why this is not a memory leak.
1592 // TODO: Consider cleaning this up with a shared_ptr.
Andreas Gampeafaf7f82018-10-16 11:32:38 -07001593 class_datas[class_data_index].release(); // NOLINT b/117926937
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001594 class_datas[class_data_index].reset(class_data);
1595 ++class_data_index;
Jeff Hao042e8982016-10-19 11:17:11 -07001596 }
1597 }
David Sehr2b5a38f2018-06-14 15:13:04 -07001598 CHECK_EQ(class_data_index, class_datas.Size());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001599
Mathieu Chartier2c4b0842017-12-13 11:49:51 -08001600 if (DexLayout::kChangeClassDefOrder) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001601 // This currently produces dex files that violate the spec since the super class class_def is
1602 // supposed to occur before any subclasses.
David Sehr2b5a38f2018-06-14 15:13:04 -07001603 dex_ir::CollectionVector<dex_ir::ClassDef>& class_defs = header_->ClassDefs();
1604 CHECK_EQ(new_class_def_order.size(), class_defs.Size());
1605 for (size_t i = 0; i < class_defs.Size(); ++i) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001606 // Overwrite the existing vector with the new ordering, note that the sets of objects are
1607 // equivalent, but the order changes. This is why this is not a memory leak.
1608 // TODO: Consider cleaning this up with a shared_ptr.
Andreas Gampeafaf7f82018-10-16 11:32:38 -07001609 class_defs[i].release(); // NOLINT b/117926937
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001610 class_defs[i].reset(new_class_def_order[i]);
1611 }
1612 }
Jeff Hao042e8982016-10-19 11:17:11 -07001613}
1614
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001615void DexLayout::LayoutStringData(const DexFile* dex_file) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001616 const size_t num_strings = header_->StringIds().Size();
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001617 std::vector<bool> is_shorty(num_strings, false);
1618 std::vector<bool> from_hot_method(num_strings, false);
David Sehr2b5a38f2018-06-14 15:13:04 -07001619 for (auto& class_def : header_->ClassDefs()) {
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001620 // A name of a profile class is probably going to get looked up by ClassTable::Lookup, mark it
Jeff Haoacc83d72017-07-06 17:51:01 -07001621 // as hot. Add its super class and interfaces as well, which can be used during initialization.
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001622 const bool is_profile_class =
1623 info_->ContainsClass(*dex_file, dex::TypeIndex(class_def->ClassType()->GetIndex()));
1624 if (is_profile_class) {
1625 from_hot_method[class_def->ClassType()->GetStringId()->GetIndex()] = true;
Jeff Haoacc83d72017-07-06 17:51:01 -07001626 const dex_ir::TypeId* superclass = class_def->Superclass();
1627 if (superclass != nullptr) {
1628 from_hot_method[superclass->GetStringId()->GetIndex()] = true;
1629 }
1630 const dex_ir::TypeList* interfaces = class_def->Interfaces();
1631 if (interfaces != nullptr) {
1632 for (const dex_ir::TypeId* interface_type : *interfaces->GetTypeList()) {
1633 from_hot_method[interface_type->GetStringId()->GetIndex()] = true;
1634 }
1635 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001636 }
1637 dex_ir::ClassData* data = class_def->GetClassData();
1638 if (data == nullptr) {
1639 continue;
1640 }
1641 for (size_t i = 0; i < 2; ++i) {
1642 for (auto& method : *(i == 0 ? data->DirectMethods() : data->VirtualMethods())) {
David Sehrd83437c2018-06-11 14:06:23 -07001643 const dex_ir::MethodId* method_id = method.GetMethodId();
1644 dex_ir::CodeItem* code_item = method.GetCodeItem();
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001645 if (code_item == nullptr) {
1646 continue;
1647 }
1648 const bool is_clinit = is_profile_class &&
David Sehrd83437c2018-06-11 14:06:23 -07001649 (method.GetAccessFlags() & kAccConstructor) != 0 &&
1650 (method.GetAccessFlags() & kAccStatic) != 0;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001651 const bool method_executed = is_clinit ||
Mathieu Chartiere46f3a82017-06-19 19:54:12 -07001652 info_->GetMethodHotness(MethodReference(dex_file, method_id->GetIndex())).IsInProfile();
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001653 if (!method_executed) {
1654 continue;
1655 }
1656 is_shorty[method_id->Proto()->Shorty()->GetIndex()] = true;
1657 dex_ir::CodeFixups* fixups = code_item->GetCodeFixups();
1658 if (fixups == nullptr) {
1659 continue;
1660 }
Jeff Haoacc83d72017-07-06 17:51:01 -07001661 // Add const-strings.
Vladimir Marko219cb902017-12-07 16:20:39 +00001662 for (dex_ir::StringId* id : fixups->StringIds()) {
Jeff Haoacc83d72017-07-06 17:51:01 -07001663 from_hot_method[id->GetIndex()] = true;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001664 }
Jeff Haoacc83d72017-07-06 17:51:01 -07001665 // Add field classes, names, and types.
Vladimir Marko219cb902017-12-07 16:20:39 +00001666 for (dex_ir::FieldId* id : fixups->FieldIds()) {
Jeff Haoacc83d72017-07-06 17:51:01 -07001667 // TODO: Only visit field ids from static getters and setters.
1668 from_hot_method[id->Class()->GetStringId()->GetIndex()] = true;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001669 from_hot_method[id->Name()->GetIndex()] = true;
1670 from_hot_method[id->Type()->GetStringId()->GetIndex()] = true;
1671 }
Jeff Haoacc83d72017-07-06 17:51:01 -07001672 // For clinits, add referenced method classes, names, and protos.
1673 if (is_clinit) {
Vladimir Marko219cb902017-12-07 16:20:39 +00001674 for (dex_ir::MethodId* id : fixups->MethodIds()) {
Jeff Haoacc83d72017-07-06 17:51:01 -07001675 from_hot_method[id->Class()->GetStringId()->GetIndex()] = true;
1676 from_hot_method[id->Name()->GetIndex()] = true;
1677 is_shorty[id->Proto()->Shorty()->GetIndex()] = true;
1678 }
1679 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001680 }
1681 }
1682 }
1683 // Sort string data by specified order.
1684 std::vector<dex_ir::StringId*> string_ids;
David Sehr2b5a38f2018-06-14 15:13:04 -07001685 for (auto& string_id : header_->StringIds()) {
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001686 string_ids.push_back(string_id.get());
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001687 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001688 std::sort(string_ids.begin(),
1689 string_ids.end(),
1690 [&is_shorty, &from_hot_method](const dex_ir::StringId* a,
1691 const dex_ir::StringId* b) {
1692 const bool a_is_hot = from_hot_method[a->GetIndex()];
1693 const bool b_is_hot = from_hot_method[b->GetIndex()];
1694 if (a_is_hot != b_is_hot) {
1695 return a_is_hot < b_is_hot;
1696 }
1697 // After hot methods are partitioned, subpartition shorties.
1698 const bool a_is_shorty = is_shorty[a->GetIndex()];
1699 const bool b_is_shorty = is_shorty[b->GetIndex()];
1700 if (a_is_shorty != b_is_shorty) {
1701 return a_is_shorty < b_is_shorty;
1702 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001703 // Order by index by default.
1704 return a->GetIndex() < b->GetIndex();
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001705 });
David Sehr2b5a38f2018-06-14 15:13:04 -07001706 auto& string_datas = header_->StringDatas();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001707 // Now we know what order we want the string data, reorder them.
1708 size_t data_index = 0;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001709 for (dex_ir::StringId* string_id : string_ids) {
Andreas Gampeafaf7f82018-10-16 11:32:38 -07001710 string_datas[data_index].release(); // NOLINT b/117926937
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001711 string_datas[data_index].reset(string_id->DataItem());
1712 ++data_index;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001713 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001714 if (kIsDebugBuild) {
1715 std::unordered_set<dex_ir::StringData*> visited;
1716 for (const std::unique_ptr<dex_ir::StringData>& data : string_datas) {
1717 visited.insert(data.get());
1718 }
David Sehr2b5a38f2018-06-14 15:13:04 -07001719 for (auto& string_id : header_->StringIds()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001720 CHECK(visited.find(string_id->DataItem()) != visited.end());
1721 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001722 }
David Sehr2b5a38f2018-06-14 15:13:04 -07001723 CHECK_EQ(data_index, string_datas.Size());
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001724}
1725
Jeff Haoe17f5892017-02-23 16:14:04 -08001726// Orders code items according to specified class data ordering.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001727void DexLayout::LayoutCodeItems(const DexFile* dex_file) {
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001728 static constexpr InvokeType invoke_types[] = {
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001729 kDirect,
1730 kVirtual
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001731 };
1732
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001733 std::unordered_map<dex_ir::CodeItem*, LayoutType>& code_item_layout =
1734 layout_hotness_info_.code_item_layout_;
1735
1736 // Assign hotness flags to all code items.
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001737 for (InvokeType invoke_type : invoke_types) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001738 for (auto& class_def : header_->ClassDefs()) {
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001739 const bool is_profile_class =
1740 info_->ContainsClass(*dex_file, dex::TypeIndex(class_def->ClassType()->GetIndex()));
1741
1742 // Skip classes that are not defined in this dex file.
1743 dex_ir::ClassData* class_data = class_def->GetClassData();
1744 if (class_data == nullptr) {
1745 continue;
Jeff Haoe17f5892017-02-23 16:14:04 -08001746 }
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001747 for (auto& method : *(invoke_type == InvokeType::kDirect
1748 ? class_data->DirectMethods()
1749 : class_data->VirtualMethods())) {
David Sehrd83437c2018-06-11 14:06:23 -07001750 const dex_ir::MethodId *method_id = method.GetMethodId();
1751 dex_ir::CodeItem *code_item = method.GetCodeItem();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001752 if (code_item == nullptr) {
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001753 continue;
1754 }
1755 // Separate executed methods (clinits and profiled methods) from unexecuted methods.
David Sehrd83437c2018-06-11 14:06:23 -07001756 const bool is_clinit = (method.GetAccessFlags() & kAccConstructor) != 0 &&
1757 (method.GetAccessFlags() & kAccStatic) != 0;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001758 const bool is_startup_clinit = is_profile_class && is_clinit;
1759 using Hotness = ProfileCompilationInfo::MethodHotness;
1760 Hotness hotness = info_->GetMethodHotness(MethodReference(dex_file, method_id->GetIndex()));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001761 LayoutType state = LayoutType::kLayoutTypeUnused;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001762 if (hotness.IsHot()) {
1763 // Hot code is compiled, maybe one day it won't be accessed. So lay it out together for
1764 // now.
Mathieu Chartier120aa282017-08-05 16:03:03 -07001765 state = LayoutType::kLayoutTypeHot;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001766 } else if (is_startup_clinit || hotness.GetFlags() == Hotness::kFlagStartup) {
1767 // Startup clinit or a method that only has the startup flag.
Mathieu Chartier120aa282017-08-05 16:03:03 -07001768 state = LayoutType::kLayoutTypeStartupOnly;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001769 } else if (is_clinit) {
Mathieu Chartier120aa282017-08-05 16:03:03 -07001770 state = LayoutType::kLayoutTypeUsedOnce;
Mathieu Chartiere46f3a82017-06-19 19:54:12 -07001771 } else if (hotness.IsInProfile()) {
Mathieu Chartier120aa282017-08-05 16:03:03 -07001772 state = LayoutType::kLayoutTypeSometimesUsed;
Jeff Hao206cbaa2017-06-07 19:11:01 -07001773 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001774 auto it = code_item_layout.emplace(code_item, state);
1775 if (!it.second) {
1776 LayoutType& layout_type = it.first->second;
1777 // Already exists, merge the hotness.
1778 layout_type = MergeLayoutType(layout_type, state);
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001779 }
1780 }
1781 }
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001782 }
Jeff Hao042e8982016-10-19 11:17:11 -07001783
David Sehr2b5a38f2018-06-14 15:13:04 -07001784 const auto& code_items = header_->CodeItems();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001785 if (VLOG_IS_ON(dex)) {
1786 size_t layout_count[static_cast<size_t>(LayoutType::kLayoutTypeCount)] = {};
1787 for (const std::unique_ptr<dex_ir::CodeItem>& code_item : code_items) {
1788 auto it = code_item_layout.find(code_item.get());
1789 DCHECK(it != code_item_layout.end());
1790 ++layout_count[static_cast<size_t>(it->second)];
1791 }
1792 for (size_t i = 0; i < static_cast<size_t>(LayoutType::kLayoutTypeCount); ++i) {
1793 LOG(INFO) << "Code items in category " << i << " count=" << layout_count[i];
Jeff Haoe17f5892017-02-23 16:14:04 -08001794 }
1795 }
Jeff Hao042e8982016-10-19 11:17:11 -07001796
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001797 // Sort the code items vector by new layout. The writing process will take care of calculating
1798 // all the offsets. Stable sort to preserve any existing locality that might be there.
1799 std::stable_sort(code_items.begin(),
1800 code_items.end(),
1801 [&](const std::unique_ptr<dex_ir::CodeItem>& a,
1802 const std::unique_ptr<dex_ir::CodeItem>& b) {
1803 auto it_a = code_item_layout.find(a.get());
1804 auto it_b = code_item_layout.find(b.get());
1805 DCHECK(it_a != code_item_layout.end());
1806 DCHECK(it_b != code_item_layout.end());
1807 const LayoutType layout_type_a = it_a->second;
1808 const LayoutType layout_type_b = it_b->second;
1809 return layout_type_a < layout_type_b;
1810 });
Jeff Hao042e8982016-10-19 11:17:11 -07001811}
1812
1813void DexLayout::LayoutOutputFile(const DexFile* dex_file) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001814 LayoutStringData(dex_file);
1815 LayoutClassDefsAndClassData(dex_file);
1816 LayoutCodeItems(dex_file);
Jeff Hao042e8982016-10-19 11:17:11 -07001817}
1818
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001819bool DexLayout::OutputDexFile(const DexFile* input_dex_file,
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001820 bool compute_offsets,
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001821 std::unique_ptr<DexContainer>* dex_container,
1822 std::string* error_msg) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001823 const std::string& dex_file_location = input_dex_file->GetLocation();
Jeff Haoea7c6292016-11-14 18:10:16 -08001824 std::unique_ptr<File> new_file;
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001825 // If options_.output_dex_directory_ is non null, we are outputting to a file.
1826 if (options_.output_dex_directory_ != nullptr) {
Jeff Haoa8621002016-10-04 18:13:44 +00001827 std::string output_location(options_.output_dex_directory_);
Mathieu Chartier41468402018-08-29 11:39:00 -07001828 const size_t last_slash = dex_file_location.rfind('/');
Jeff Haoea7c6292016-11-14 18:10:16 -08001829 std::string dex_file_directory = dex_file_location.substr(0, last_slash + 1);
1830 if (output_location == dex_file_directory) {
1831 output_location = dex_file_location + ".new";
Jeff Haoea7c6292016-11-14 18:10:16 -08001832 } else {
Mathieu Chartier41468402018-08-29 11:39:00 -07001833 if (!output_location.empty() && output_location.back() != '/') {
1834 output_location += "/";
1835 }
1836 const size_t separator = dex_file_location.rfind('!');
1837 if (separator != std::string::npos) {
1838 output_location += dex_file_location.substr(separator + 1);
1839 } else {
1840 output_location += "classes.dex";
1841 }
Jeff Haoea7c6292016-11-14 18:10:16 -08001842 }
1843 new_file.reset(OS::CreateEmptyFile(output_location.c_str()));
Jeff Hao3ba51e82017-04-12 16:14:54 -07001844 if (new_file == nullptr) {
1845 LOG(ERROR) << "Could not create dex writer output file: " << output_location;
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001846 return false;
Jeff Hao3ba51e82017-04-12 16:14:54 -07001847 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001848 }
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001849 if (!DexWriter::Output(this, dex_container, compute_offsets, error_msg)) {
1850 return false;
1851 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001852 if (new_file != nullptr) {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001853 DexContainer* const container = dex_container->get();
1854 DexContainer::Section* const main_section = container->GetMainSection();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001855 if (!new_file->WriteFully(main_section->Begin(), main_section->Size())) {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001856 LOG(ERROR) << "Failed to write main section for dex file " << dex_file_location;
1857 new_file->Erase();
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001858 return false;
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001859 }
1860 DexContainer::Section* const data_section = container->GetDataSection();
1861 if (!new_file->WriteFully(data_section->Begin(), data_section->Size())) {
1862 LOG(ERROR) << "Failed to write data section for dex file " << dex_file_location;
David Sehr7639cdc2017-04-15 10:06:21 -07001863 new_file->Erase();
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001864 return false;
David Sehr7639cdc2017-04-15 10:06:21 -07001865 }
Jeff Haoea7c6292016-11-14 18:10:16 -08001866 UNUSED(new_file->FlushCloseOrErase());
1867 }
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001868 return true;
Jeff Haoea7c6292016-11-14 18:10:16 -08001869}
1870
1871/*
1872 * Dumps the requested sections of the file.
1873 */
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001874bool DexLayout::ProcessDexFile(const char* file_name,
Jeff Haoea7c6292016-11-14 18:10:16 -08001875 const DexFile* dex_file,
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001876 size_t dex_file_index,
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001877 std::unique_ptr<DexContainer>* dex_container,
1878 std::string* error_msg) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001879 const bool has_output_container = dex_container != nullptr;
1880 const bool output = options_.output_dex_directory_ != nullptr || has_output_container;
1881
David Sehr2b5a38f2018-06-14 15:13:04 -07001882 // Try to avoid eagerly assigning offsets to find bugs since Offset will abort if the offset
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001883 // is unassigned.
1884 bool eagerly_assign_offsets = false;
1885 if (options_.visualize_pattern_ || options_.show_section_statistics_ || options_.dump_) {
1886 // These options required the offsets for dumping purposes.
1887 eagerly_assign_offsets = true;
1888 }
Mathieu Chartier75175552018-01-25 11:23:01 -08001889 std::unique_ptr<dex_ir::Header> header(dex_ir::DexIrBuilder(*dex_file,
1890 eagerly_assign_offsets,
1891 GetOptions()));
Jeff Haoea7c6292016-11-14 18:10:16 -08001892 SetHeader(header.get());
1893
1894 if (options_.verbose_) {
1895 fprintf(out_file_, "Opened '%s', DEX version '%.3s'\n",
1896 file_name, dex_file->GetHeader().magic_ + 4);
1897 }
1898
1899 if (options_.visualize_pattern_) {
1900 VisualizeDexLayout(header_, dex_file, dex_file_index, info_);
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001901 return true;
Jeff Haoea7c6292016-11-14 18:10:16 -08001902 }
1903
David Sehr93357492017-03-09 08:02:44 -08001904 if (options_.show_section_statistics_) {
1905 ShowDexSectionStatistics(header_, dex_file_index);
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001906 return true;
David Sehr93357492017-03-09 08:02:44 -08001907 }
1908
Jeff Haoea7c6292016-11-14 18:10:16 -08001909 // Dump dex file.
1910 if (options_.dump_) {
1911 DumpDexFile();
1912 }
1913
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001914 // In case we are outputting to a file, keep it open so we can verify.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001915 if (output) {
1916 // Layout information about what strings and code items are hot. Used by the writing process
1917 // to generate the sections that are stored in the oat file.
1918 bool do_layout = info_ != nullptr;
1919 if (do_layout) {
Jeff Hao042e8982016-10-19 11:17:11 -07001920 LayoutOutputFile(dex_file);
1921 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001922 // The output needs a dex container, use a temporary one.
1923 std::unique_ptr<DexContainer> temp_container;
1924 if (dex_container == nullptr) {
1925 dex_container = &temp_container;
1926 }
Mathieu Chartier21cf2582018-01-08 17:09:48 -08001927 // If we didn't set the offsets eagerly, we definitely need to compute them here.
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001928 if (!OutputDexFile(dex_file, do_layout || !eagerly_assign_offsets, dex_container, error_msg)) {
1929 return false;
1930 }
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001931
1932 // Clear header before verifying to reduce peak RAM usage.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001933 const size_t file_size = header_->FileSize();
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001934 header.reset();
1935
1936 // Verify the output dex file's structure, only enabled by default for debug builds.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001937 if (options_.verify_output_ && has_output_container) {
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001938 std::string location = "memory mapped file for " + std::string(file_name);
Mathieu Chartier8740c662018-01-11 14:50:02 -08001939 // Dex file verifier cannot handle compact dex.
1940 bool verify = options_.compact_dex_level_ == CompactDexLevel::kCompactDexLevelNone;
Mathieu Chartier818cb802018-05-11 05:30:16 +00001941 const ArtDexFileLoader dex_file_loader;
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001942 DexContainer::Section* const main_section = (*dex_container)->GetMainSection();
1943 DexContainer::Section* const data_section = (*dex_container)->GetDataSection();
1944 DCHECK_EQ(file_size, main_section->Size())
1945 << main_section->Size() << " " << data_section->Size();
David Sehr013fd802018-01-11 22:55:24 -08001946 std::unique_ptr<const DexFile> output_dex_file(
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001947 dex_file_loader.OpenWithDataSection(
1948 main_section->Begin(),
1949 main_section->Size(),
1950 data_section->Begin(),
1951 data_section->Size(),
1952 location,
Andreas Gampe9b031f72018-10-04 11:03:34 -07001953 /* location_checksum= */ 0,
1954 /*oat_dex_file=*/ nullptr,
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001955 verify,
Andreas Gampe9b031f72018-10-04 11:03:34 -07001956 /*verify_checksum=*/ false,
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001957 error_msg));
1958 CHECK(output_dex_file != nullptr) << "Failed to re-open output file:" << *error_msg;
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001959
1960 // Do IR-level comparison between input and output. This check ignores potential differences
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001961 // due to layout, so offsets are not checked. Instead, it checks the data contents of each
1962 // item.
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001963 //
1964 // Regenerate output IR to catch any bugs that might happen during writing.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001965 std::unique_ptr<dex_ir::Header> output_header(
1966 dex_ir::DexIrBuilder(*output_dex_file,
Andreas Gampe9b031f72018-10-04 11:03:34 -07001967 /*eagerly_assign_offsets=*/ true,
Mathieu Chartier75175552018-01-25 11:23:01 -08001968 GetOptions()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001969 std::unique_ptr<dex_ir::Header> orig_header(
1970 dex_ir::DexIrBuilder(*dex_file,
Andreas Gampe9b031f72018-10-04 11:03:34 -07001971 /*eagerly_assign_offsets=*/ true,
Mathieu Chartier75175552018-01-25 11:23:01 -08001972 GetOptions()));
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001973 CHECK(VerifyOutputDexFile(output_header.get(), orig_header.get(), error_msg)) << *error_msg;
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001974 }
Jeff Hao3ab96b42016-09-09 18:35:01 -07001975 }
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001976 return true;
David Sehr7629f602016-08-07 16:01:51 -07001977}
1978
1979/*
1980 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
1981 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001982int DexLayout::ProcessFile(const char* file_name) {
David Sehr7629f602016-08-07 16:01:51 -07001983 if (options_.verbose_) {
1984 fprintf(out_file_, "Processing '%s'...\n", file_name);
1985 }
1986
1987 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
1988 // all of which are Zip archives with "classes.dex" inside.
1989 const bool verify_checksum = !options_.ignore_bad_checksum_;
1990 std::string error_msg;
Mathieu Chartier818cb802018-05-11 05:30:16 +00001991 const ArtDexFileLoader dex_file_loader;
David Sehr7629f602016-08-07 16:01:51 -07001992 std::vector<std::unique_ptr<const DexFile>> dex_files;
Mathieu Chartier818cb802018-05-11 05:30:16 +00001993 if (!dex_file_loader.Open(
Andreas Gampe9b031f72018-10-04 11:03:34 -07001994 file_name, file_name, /* verify= */ true, verify_checksum, &error_msg, &dex_files)) {
David Sehr7629f602016-08-07 16:01:51 -07001995 // Display returned error message to user. Note that this error behavior
1996 // differs from the error messages shown by the original Dalvik dexdump.
Andreas Gampe221d9812018-01-22 17:48:56 -08001997 LOG(ERROR) << error_msg;
David Sehr7629f602016-08-07 16:01:51 -07001998 return -1;
1999 }
2000
2001 // Success. Either report checksum verification or process
2002 // all dex files found in given file.
2003 if (options_.checksum_only_) {
2004 fprintf(out_file_, "Checksum verified\n");
2005 } else {
2006 for (size_t i = 0; i < dex_files.size(); i++) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08002007 // Pass in a null container to avoid output by default.
Mathieu Chartier05f90d12018-02-07 13:47:17 -08002008 if (!ProcessDexFile(file_name,
2009 dex_files[i].get(),
2010 i,
Andreas Gampe9b031f72018-10-04 11:03:34 -07002011 /*dex_container=*/ nullptr,
Mathieu Chartier05f90d12018-02-07 13:47:17 -08002012 &error_msg)) {
2013 LOG(WARNING) << "Failed to run dex file " << i << " in " << file_name << " : " << error_msg;
2014 }
David Sehr7629f602016-08-07 16:01:51 -07002015 }
2016 }
2017 return 0;
2018}
2019
2020} // namespace art