blob: 000d1356b9e4e6e44af7e147aeabd93b553abb5e [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>
Andreas Gampe0dfc3152017-04-24 07:58:06 -070027#include <sys/mman.h> // For the PROT_* and MAP_* constants.
David Sehr7629f602016-08-07 16:01:51 -070028
29#include <iostream>
30#include <memory>
31#include <sstream>
32#include <vector>
33
Andreas Gampe46ee31b2016-12-14 10:11:49 -080034#include "android-base/stringprintf.h"
35
Andreas Gampe57943812017-12-06 21:39:13 -080036#include "base/logging.h" // For VLOG_IS_ON.
David Sehr9e734c72018-01-04 17:56:19 -080037#include "dex/dex_file-inl.h"
38#include "dex/dex_file_layout.h"
39#include "dex/dex_file_loader.h"
40#include "dex/dex_file_types.h"
41#include "dex/dex_file_verifier.h"
42#include "dex/dex_instruction-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070043#include "dex_ir_builder.h"
Jeff Haoec7f1a92017-03-13 16:24:24 -070044#include "dex_verify.h"
David Sehrcdcfde72016-09-26 07:44:04 -070045#include "dex_visualize.h"
Jeff Haoa8621002016-10-04 18:13:44 +000046#include "dex_writer.h"
Calin Juravle33083d62017-01-18 15:29:12 -080047#include "jit/profile_compilation_info.h"
Jeff Haoea7c6292016-11-14 18:10:16 -080048#include "mem_map.h"
Nicolas Geoffrayfd1a6c22016-10-04 11:01:17 +000049#include "os.h"
David Sehr7629f602016-08-07 16:01:51 -070050#include "utils.h"
51
52namespace art {
53
Andreas Gampe46ee31b2016-12-14 10:11:49 -080054using android::base::StringPrintf;
55
David Sehr7629f602016-08-07 16:01:51 -070056/*
David Sehr7629f602016-08-07 16:01:51 -070057 * Flags for use with createAccessFlagStr().
58 */
59enum AccessFor {
60 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
61};
62const int kNumFlags = 18;
63
64/*
65 * Gets 2 little-endian bytes.
66 */
67static inline uint16_t Get2LE(unsigned char const* src) {
68 return src[0] | (src[1] << 8);
69}
70
71/*
Jeff Haoc3acfc52016-08-29 14:18:26 -070072 * Converts a type descriptor to human-readable "dotted" form. For
73 * example, "Ljava/lang/String;" becomes "java.lang.String", and
74 * "[I" becomes "int[]". Also converts '$' to '.', which means this
75 * form can't be converted back to a descriptor.
76 */
77static std::string DescriptorToDotWrapper(const char* descriptor) {
78 std::string result = DescriptorToDot(descriptor);
79 size_t found = result.find('$');
80 while (found != std::string::npos) {
81 result[found] = '.';
82 found = result.find('$', found);
83 }
84 return result;
85}
86
87/*
David Sehr7629f602016-08-07 16:01:51 -070088 * Converts the class name portion of a type descriptor to human-readable
89 * "dotted" form. For example, "Ljava/lang/String;" becomes "String".
90 */
91static std::string DescriptorClassToDot(const char* str) {
92 std::string descriptor(str);
93 // Reduce to just the class name prefix.
94 size_t last_slash = descriptor.rfind('/');
95 if (last_slash == std::string::npos) {
96 last_slash = 0;
97 }
98 // Start past the '/' or 'L'.
99 last_slash++;
100
101 // Copy class name over, trimming trailing ';'.
102 size_t size = descriptor.size() - 1 - last_slash;
103 std::string result(descriptor.substr(last_slash, size));
104
105 // Replace '$' with '.'.
106 size_t dollar_sign = result.find('$');
107 while (dollar_sign != std::string::npos) {
108 result[dollar_sign] = '.';
109 dollar_sign = result.find('$', dollar_sign);
110 }
111
112 return result;
113}
114
115/*
116 * Returns string representing the boolean value.
117 */
118static const char* StrBool(bool val) {
119 return val ? "true" : "false";
120}
121
122/*
123 * Returns a quoted string representing the boolean value.
124 */
125static const char* QuotedBool(bool val) {
126 return val ? "\"true\"" : "\"false\"";
127}
128
129/*
130 * Returns a quoted string representing the access flags.
131 */
132static const char* QuotedVisibility(uint32_t access_flags) {
133 if (access_flags & kAccPublic) {
134 return "\"public\"";
135 } else if (access_flags & kAccProtected) {
136 return "\"protected\"";
137 } else if (access_flags & kAccPrivate) {
138 return "\"private\"";
139 } else {
140 return "\"package\"";
141 }
142}
143
144/*
145 * Counts the number of '1' bits in a word.
146 */
147static int CountOnes(uint32_t val) {
148 val = val - ((val >> 1) & 0x55555555);
149 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
150 return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
151}
152
153/*
154 * Creates a new string with human-readable access flags.
155 *
156 * In the base language the access_flags fields are type uint16_t; in Dalvik they're uint32_t.
157 */
158static char* CreateAccessFlagStr(uint32_t flags, AccessFor for_what) {
159 static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
160 {
161 "PUBLIC", /* 0x00001 */
162 "PRIVATE", /* 0x00002 */
163 "PROTECTED", /* 0x00004 */
164 "STATIC", /* 0x00008 */
165 "FINAL", /* 0x00010 */
166 "?", /* 0x00020 */
167 "?", /* 0x00040 */
168 "?", /* 0x00080 */
169 "?", /* 0x00100 */
170 "INTERFACE", /* 0x00200 */
171 "ABSTRACT", /* 0x00400 */
172 "?", /* 0x00800 */
173 "SYNTHETIC", /* 0x01000 */
174 "ANNOTATION", /* 0x02000 */
175 "ENUM", /* 0x04000 */
176 "?", /* 0x08000 */
177 "VERIFIED", /* 0x10000 */
178 "OPTIMIZED", /* 0x20000 */
179 }, {
180 "PUBLIC", /* 0x00001 */
181 "PRIVATE", /* 0x00002 */
182 "PROTECTED", /* 0x00004 */
183 "STATIC", /* 0x00008 */
184 "FINAL", /* 0x00010 */
185 "SYNCHRONIZED", /* 0x00020 */
186 "BRIDGE", /* 0x00040 */
187 "VARARGS", /* 0x00080 */
188 "NATIVE", /* 0x00100 */
189 "?", /* 0x00200 */
190 "ABSTRACT", /* 0x00400 */
191 "STRICT", /* 0x00800 */
192 "SYNTHETIC", /* 0x01000 */
193 "?", /* 0x02000 */
194 "?", /* 0x04000 */
195 "MIRANDA", /* 0x08000 */
196 "CONSTRUCTOR", /* 0x10000 */
197 "DECLARED_SYNCHRONIZED", /* 0x20000 */
198 }, {
199 "PUBLIC", /* 0x00001 */
200 "PRIVATE", /* 0x00002 */
201 "PROTECTED", /* 0x00004 */
202 "STATIC", /* 0x00008 */
203 "FINAL", /* 0x00010 */
204 "?", /* 0x00020 */
205 "VOLATILE", /* 0x00040 */
206 "TRANSIENT", /* 0x00080 */
207 "?", /* 0x00100 */
208 "?", /* 0x00200 */
209 "?", /* 0x00400 */
210 "?", /* 0x00800 */
211 "SYNTHETIC", /* 0x01000 */
212 "?", /* 0x02000 */
213 "ENUM", /* 0x04000 */
214 "?", /* 0x08000 */
215 "?", /* 0x10000 */
216 "?", /* 0x20000 */
217 },
218 };
219
220 // Allocate enough storage to hold the expected number of strings,
221 // plus a space between each. We over-allocate, using the longest
222 // string above as the base metric.
223 const int kLongest = 21; // The strlen of longest string above.
224 const int count = CountOnes(flags);
225 char* str;
226 char* cp;
227 cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
228
229 for (int i = 0; i < kNumFlags; i++) {
230 if (flags & 0x01) {
231 const char* accessStr = kAccessStrings[for_what][i];
232 const int len = strlen(accessStr);
233 if (cp != str) {
234 *cp++ = ' ';
235 }
236 memcpy(cp, accessStr, len);
237 cp += len;
238 }
239 flags >>= 1;
240 } // for
241
242 *cp = '\0';
243 return str;
244}
245
246static std::string GetSignatureForProtoId(const dex_ir::ProtoId* proto) {
247 if (proto == nullptr) {
248 return "<no signature>";
249 }
250
David Sehr7629f602016-08-07 16:01:51 -0700251 std::string result("(");
Jeff Haoa8621002016-10-04 18:13:44 +0000252 const dex_ir::TypeList* type_list = proto->Parameters();
253 if (type_list != nullptr) {
254 for (const dex_ir::TypeId* type_id : *type_list->GetTypeList()) {
255 result += type_id->GetStringId()->Data();
256 }
David Sehr7629f602016-08-07 16:01:51 -0700257 }
258 result += ")";
259 result += proto->ReturnType()->GetStringId()->Data();
260 return result;
261}
262
263/*
264 * Copies character data from "data" to "out", converting non-ASCII values
265 * to fprintf format chars or an ASCII filler ('.' or '?').
266 *
267 * The output buffer must be able to hold (2*len)+1 bytes. The result is
268 * NULL-terminated.
269 */
270static void Asciify(char* out, const unsigned char* data, size_t len) {
271 while (len--) {
272 if (*data < 0x20) {
273 // Could do more here, but we don't need them yet.
274 switch (*data) {
275 case '\0':
276 *out++ = '\\';
277 *out++ = '0';
278 break;
279 case '\n':
280 *out++ = '\\';
281 *out++ = 'n';
282 break;
283 default:
284 *out++ = '.';
285 break;
286 } // switch
287 } else if (*data >= 0x80) {
288 *out++ = '?';
289 } else {
290 *out++ = *data;
291 }
292 data++;
293 } // while
294 *out = '\0';
295}
296
297/*
298 * Dumps a string value with some escape characters.
299 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800300static void DumpEscapedString(const char* p, FILE* out_file) {
301 fputs("\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700302 for (; *p; p++) {
303 switch (*p) {
304 case '\\':
Jeff Haoea7c6292016-11-14 18:10:16 -0800305 fputs("\\\\", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700306 break;
307 case '\"':
Jeff Haoea7c6292016-11-14 18:10:16 -0800308 fputs("\\\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700309 break;
310 case '\t':
Jeff Haoea7c6292016-11-14 18:10:16 -0800311 fputs("\\t", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700312 break;
313 case '\n':
Jeff Haoea7c6292016-11-14 18:10:16 -0800314 fputs("\\n", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700315 break;
316 case '\r':
Jeff Haoea7c6292016-11-14 18:10:16 -0800317 fputs("\\r", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700318 break;
319 default:
Jeff Haoea7c6292016-11-14 18:10:16 -0800320 putc(*p, out_file);
David Sehr7629f602016-08-07 16:01:51 -0700321 } // switch
322 } // for
Jeff Haoea7c6292016-11-14 18:10:16 -0800323 fputs("\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700324}
325
326/*
327 * Dumps a string as an XML attribute value.
328 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800329static void DumpXmlAttribute(const char* p, FILE* out_file) {
David Sehr7629f602016-08-07 16:01:51 -0700330 for (; *p; p++) {
331 switch (*p) {
332 case '&':
Jeff Haoea7c6292016-11-14 18:10:16 -0800333 fputs("&amp;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700334 break;
335 case '<':
Jeff Haoea7c6292016-11-14 18:10:16 -0800336 fputs("&lt;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700337 break;
338 case '>':
Jeff Haoea7c6292016-11-14 18:10:16 -0800339 fputs("&gt;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700340 break;
341 case '"':
Jeff Haoea7c6292016-11-14 18:10:16 -0800342 fputs("&quot;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700343 break;
344 case '\t':
Jeff Haoea7c6292016-11-14 18:10:16 -0800345 fputs("&#x9;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700346 break;
347 case '\n':
Jeff Haoea7c6292016-11-14 18:10:16 -0800348 fputs("&#xA;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700349 break;
350 case '\r':
Jeff Haoea7c6292016-11-14 18:10:16 -0800351 fputs("&#xD;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700352 break;
353 default:
Jeff Haoea7c6292016-11-14 18:10:16 -0800354 putc(*p, out_file);
David Sehr7629f602016-08-07 16:01:51 -0700355 } // switch
356 } // for
357}
358
David Sehr7629f602016-08-07 16:01:51 -0700359/*
360 * Helper for dumpInstruction(), which builds the string
361 * representation for the index in the given instruction.
362 * Returns a pointer to a buffer of sufficient size.
363 */
364static std::unique_ptr<char[]> IndexString(dex_ir::Header* header,
365 const Instruction* dec_insn,
366 size_t buf_size) {
367 std::unique_ptr<char[]> buf(new char[buf_size]);
368 // Determine index and width of the string.
369 uint32_t index = 0;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700370 uint32_t secondary_index = dex::kDexNoIndex;
David Sehr7629f602016-08-07 16:01:51 -0700371 uint32_t width = 4;
372 switch (Instruction::FormatOf(dec_insn->Opcode())) {
373 // SOME NOT SUPPORTED:
374 // case Instruction::k20bc:
375 case Instruction::k21c:
376 case Instruction::k35c:
377 // case Instruction::k35ms:
378 case Instruction::k3rc:
379 // case Instruction::k3rms:
380 // case Instruction::k35mi:
381 // case Instruction::k3rmi:
382 index = dec_insn->VRegB();
383 width = 4;
384 break;
385 case Instruction::k31c:
386 index = dec_insn->VRegB();
387 width = 8;
388 break;
389 case Instruction::k22c:
390 // case Instruction::k22cs:
391 index = dec_insn->VRegC();
392 width = 4;
393 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100394 case Instruction::k45cc:
395 case Instruction::k4rcc:
396 index = dec_insn->VRegB();
397 secondary_index = dec_insn->VRegH();
398 width = 4;
David Sehr7639cdc2017-04-15 10:06:21 -0700399 break;
David Sehr7629f602016-08-07 16:01:51 -0700400 default:
401 break;
402 } // switch
403
404 // Determine index type.
405 size_t outSize = 0;
406 switch (Instruction::IndexTypeOf(dec_insn->Opcode())) {
407 case Instruction::kIndexUnknown:
408 // This function should never get called for this type, but do
409 // something sensible here, just to help with debugging.
410 outSize = snprintf(buf.get(), buf_size, "<unknown-index>");
411 break;
412 case Instruction::kIndexNone:
413 // This function should never get called for this type, but do
414 // something sensible here, just to help with debugging.
415 outSize = snprintf(buf.get(), buf_size, "<no-index>");
416 break;
417 case Instruction::kIndexTypeRef:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700418 if (index < header->GetCollections().TypeIdsSize()) {
419 const char* tp = header->GetCollections().GetTypeId(index)->GetStringId()->Data();
David Sehr7629f602016-08-07 16:01:51 -0700420 outSize = snprintf(buf.get(), buf_size, "%s // type@%0*x", tp, width, index);
421 } else {
422 outSize = snprintf(buf.get(), buf_size, "<type?> // type@%0*x", width, index);
423 }
424 break;
425 case Instruction::kIndexStringRef:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700426 if (index < header->GetCollections().StringIdsSize()) {
427 const char* st = header->GetCollections().GetStringId(index)->Data();
David Sehr7629f602016-08-07 16:01:51 -0700428 outSize = snprintf(buf.get(), buf_size, "\"%s\" // string@%0*x", st, width, index);
429 } else {
430 outSize = snprintf(buf.get(), buf_size, "<string?> // string@%0*x", width, index);
431 }
432 break;
433 case Instruction::kIndexMethodRef:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700434 if (index < header->GetCollections().MethodIdsSize()) {
435 dex_ir::MethodId* method_id = header->GetCollections().GetMethodId(index);
David Sehr7629f602016-08-07 16:01:51 -0700436 const char* name = method_id->Name()->Data();
David Sehr72359222016-09-07 13:04:01 -0700437 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -0700438 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
439 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // method@%0*x",
David Sehr72359222016-09-07 13:04:01 -0700440 back_descriptor, name, type_descriptor.c_str(), width, index);
David Sehr7629f602016-08-07 16:01:51 -0700441 } else {
442 outSize = snprintf(buf.get(), buf_size, "<method?> // method@%0*x", width, index);
443 }
444 break;
445 case Instruction::kIndexFieldRef:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700446 if (index < header->GetCollections().FieldIdsSize()) {
447 dex_ir::FieldId* field_id = header->GetCollections().GetFieldId(index);
David Sehr7629f602016-08-07 16:01:51 -0700448 const char* name = field_id->Name()->Data();
449 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
450 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
451 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // field@%0*x",
452 back_descriptor, name, type_descriptor, width, index);
453 } else {
454 outSize = snprintf(buf.get(), buf_size, "<field?> // field@%0*x", width, index);
455 }
456 break;
457 case Instruction::kIndexVtableOffset:
458 outSize = snprintf(buf.get(), buf_size, "[%0*x] // vtable #%0*x",
459 width, index, width, index);
460 break;
461 case Instruction::kIndexFieldOffset:
462 outSize = snprintf(buf.get(), buf_size, "[obj+%0*x]", width, index);
463 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100464 case Instruction::kIndexMethodAndProtoRef: {
465 std::string method("<method?>");
466 std::string proto("<proto?>");
467 if (index < header->GetCollections().MethodIdsSize()) {
468 dex_ir::MethodId* method_id = header->GetCollections().GetMethodId(index);
469 const char* name = method_id->Name()->Data();
470 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
471 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
472 method = StringPrintf("%s.%s:%s", back_descriptor, name, type_descriptor.c_str());
473 }
474 if (secondary_index < header->GetCollections().ProtoIdsSize()) {
475 dex_ir::ProtoId* proto_id = header->GetCollections().GetProtoId(secondary_index);
476 proto = GetSignatureForProtoId(proto_id);
477 }
478 outSize = snprintf(buf.get(), buf_size, "%s, %s // method@%0*x, proto@%0*x",
479 method.c_str(), proto.c_str(), width, index, width, secondary_index);
Jeff Haoea7c6292016-11-14 18:10:16 -0800480 }
481 break;
482 // SOME NOT SUPPORTED:
483 // case Instruction::kIndexVaries:
484 // case Instruction::kIndexInlineMethod:
David Sehr7629f602016-08-07 16:01:51 -0700485 default:
486 outSize = snprintf(buf.get(), buf_size, "<?>");
487 break;
488 } // switch
489
490 // Determine success of string construction.
491 if (outSize >= buf_size) {
492 // The buffer wasn't big enough; retry with computed size. Note: snprintf()
493 // doesn't count/ the '\0' as part of its returned size, so we add explicit
494 // space for it here.
495 return IndexString(header, dec_insn, outSize + 1);
496 }
497 return buf;
498}
499
500/*
Jeff Haoea7c6292016-11-14 18:10:16 -0800501 * Dumps encoded annotation.
502 */
503void DexLayout::DumpEncodedAnnotation(dex_ir::EncodedAnnotation* annotation) {
504 fputs(annotation->GetType()->GetStringId()->Data(), out_file_);
505 // Display all name=value pairs.
506 for (auto& subannotation : *annotation->GetAnnotationElements()) {
507 fputc(' ', out_file_);
508 fputs(subannotation->GetName()->Data(), out_file_);
509 fputc('=', out_file_);
510 DumpEncodedValue(subannotation->GetValue());
511 }
512}
513/*
514 * Dumps encoded value.
515 */
516void DexLayout::DumpEncodedValue(const dex_ir::EncodedValue* data) {
517 switch (data->Type()) {
518 case DexFile::kDexAnnotationByte:
519 fprintf(out_file_, "%" PRId8, data->GetByte());
520 break;
521 case DexFile::kDexAnnotationShort:
522 fprintf(out_file_, "%" PRId16, data->GetShort());
523 break;
524 case DexFile::kDexAnnotationChar:
525 fprintf(out_file_, "%" PRIu16, data->GetChar());
526 break;
527 case DexFile::kDexAnnotationInt:
528 fprintf(out_file_, "%" PRId32, data->GetInt());
529 break;
530 case DexFile::kDexAnnotationLong:
531 fprintf(out_file_, "%" PRId64, data->GetLong());
532 break;
533 case DexFile::kDexAnnotationFloat: {
534 fprintf(out_file_, "%g", data->GetFloat());
535 break;
536 }
537 case DexFile::kDexAnnotationDouble: {
538 fprintf(out_file_, "%g", data->GetDouble());
539 break;
540 }
541 case DexFile::kDexAnnotationString: {
542 dex_ir::StringId* string_id = data->GetStringId();
543 if (options_.output_format_ == kOutputPlain) {
544 DumpEscapedString(string_id->Data(), out_file_);
545 } else {
546 DumpXmlAttribute(string_id->Data(), out_file_);
547 }
548 break;
549 }
550 case DexFile::kDexAnnotationType: {
551 dex_ir::TypeId* type_id = data->GetTypeId();
552 fputs(type_id->GetStringId()->Data(), out_file_);
553 break;
554 }
555 case DexFile::kDexAnnotationField:
556 case DexFile::kDexAnnotationEnum: {
557 dex_ir::FieldId* field_id = data->GetFieldId();
558 fputs(field_id->Name()->Data(), out_file_);
559 break;
560 }
561 case DexFile::kDexAnnotationMethod: {
562 dex_ir::MethodId* method_id = data->GetMethodId();
563 fputs(method_id->Name()->Data(), out_file_);
564 break;
565 }
566 case DexFile::kDexAnnotationArray: {
567 fputc('{', out_file_);
568 // Display all elements.
569 for (auto& value : *data->GetEncodedArray()->GetEncodedValues()) {
570 fputc(' ', out_file_);
571 DumpEncodedValue(value.get());
572 }
573 fputs(" }", out_file_);
574 break;
575 }
576 case DexFile::kDexAnnotationAnnotation: {
577 DumpEncodedAnnotation(data->GetEncodedAnnotation());
578 break;
579 }
580 case DexFile::kDexAnnotationNull:
581 fputs("null", out_file_);
582 break;
583 case DexFile::kDexAnnotationBoolean:
584 fputs(StrBool(data->GetBoolean()), out_file_);
585 break;
586 default:
587 fputs("????", out_file_);
588 break;
589 } // switch
590}
591
592/*
593 * Dumps the file header.
594 */
595void DexLayout::DumpFileHeader() {
596 char sanitized[8 * 2 + 1];
597 dex_ir::Collections& collections = header_->GetCollections();
598 fprintf(out_file_, "DEX file header:\n");
599 Asciify(sanitized, header_->Magic(), 8);
600 fprintf(out_file_, "magic : '%s'\n", sanitized);
601 fprintf(out_file_, "checksum : %08x\n", header_->Checksum());
602 fprintf(out_file_, "signature : %02x%02x...%02x%02x\n",
603 header_->Signature()[0], header_->Signature()[1],
604 header_->Signature()[DexFile::kSha1DigestSize - 2],
605 header_->Signature()[DexFile::kSha1DigestSize - 1]);
606 fprintf(out_file_, "file_size : %d\n", header_->FileSize());
607 fprintf(out_file_, "header_size : %d\n", header_->HeaderSize());
608 fprintf(out_file_, "link_size : %d\n", header_->LinkSize());
609 fprintf(out_file_, "link_off : %d (0x%06x)\n",
610 header_->LinkOffset(), header_->LinkOffset());
611 fprintf(out_file_, "string_ids_size : %d\n", collections.StringIdsSize());
612 fprintf(out_file_, "string_ids_off : %d (0x%06x)\n",
613 collections.StringIdsOffset(), collections.StringIdsOffset());
614 fprintf(out_file_, "type_ids_size : %d\n", collections.TypeIdsSize());
615 fprintf(out_file_, "type_ids_off : %d (0x%06x)\n",
616 collections.TypeIdsOffset(), collections.TypeIdsOffset());
617 fprintf(out_file_, "proto_ids_size : %d\n", collections.ProtoIdsSize());
618 fprintf(out_file_, "proto_ids_off : %d (0x%06x)\n",
619 collections.ProtoIdsOffset(), collections.ProtoIdsOffset());
620 fprintf(out_file_, "field_ids_size : %d\n", collections.FieldIdsSize());
621 fprintf(out_file_, "field_ids_off : %d (0x%06x)\n",
622 collections.FieldIdsOffset(), collections.FieldIdsOffset());
623 fprintf(out_file_, "method_ids_size : %d\n", collections.MethodIdsSize());
624 fprintf(out_file_, "method_ids_off : %d (0x%06x)\n",
625 collections.MethodIdsOffset(), collections.MethodIdsOffset());
626 fprintf(out_file_, "class_defs_size : %d\n", collections.ClassDefsSize());
627 fprintf(out_file_, "class_defs_off : %d (0x%06x)\n",
628 collections.ClassDefsOffset(), collections.ClassDefsOffset());
629 fprintf(out_file_, "data_size : %d\n", header_->DataSize());
630 fprintf(out_file_, "data_off : %d (0x%06x)\n\n",
631 header_->DataOffset(), header_->DataOffset());
632}
633
634/*
635 * Dumps a class_def_item.
636 */
637void DexLayout::DumpClassDef(int idx) {
638 // General class information.
639 dex_ir::ClassDef* class_def = header_->GetCollections().GetClassDef(idx);
640 fprintf(out_file_, "Class #%d header:\n", idx);
641 fprintf(out_file_, "class_idx : %d\n", class_def->ClassType()->GetIndex());
642 fprintf(out_file_, "access_flags : %d (0x%04x)\n",
643 class_def->GetAccessFlags(), class_def->GetAccessFlags());
644 uint32_t superclass_idx = class_def->Superclass() == nullptr ?
645 DexFile::kDexNoIndex16 : class_def->Superclass()->GetIndex();
646 fprintf(out_file_, "superclass_idx : %d\n", superclass_idx);
647 fprintf(out_file_, "interfaces_off : %d (0x%06x)\n",
648 class_def->InterfacesOffset(), class_def->InterfacesOffset());
649 uint32_t source_file_offset = 0xffffffffU;
650 if (class_def->SourceFile() != nullptr) {
651 source_file_offset = class_def->SourceFile()->GetIndex();
652 }
653 fprintf(out_file_, "source_file_idx : %d\n", source_file_offset);
654 uint32_t annotations_offset = 0;
655 if (class_def->Annotations() != nullptr) {
656 annotations_offset = class_def->Annotations()->GetOffset();
657 }
658 fprintf(out_file_, "annotations_off : %d (0x%06x)\n",
659 annotations_offset, annotations_offset);
660 if (class_def->GetClassData() == nullptr) {
661 fprintf(out_file_, "class_data_off : %d (0x%06x)\n", 0, 0);
662 } else {
663 fprintf(out_file_, "class_data_off : %d (0x%06x)\n",
664 class_def->GetClassData()->GetOffset(), class_def->GetClassData()->GetOffset());
665 }
666
667 // Fields and methods.
668 dex_ir::ClassData* class_data = class_def->GetClassData();
669 if (class_data != nullptr && class_data->StaticFields() != nullptr) {
670 fprintf(out_file_, "static_fields_size : %zu\n", class_data->StaticFields()->size());
671 } else {
672 fprintf(out_file_, "static_fields_size : 0\n");
673 }
674 if (class_data != nullptr && class_data->InstanceFields() != nullptr) {
675 fprintf(out_file_, "instance_fields_size: %zu\n", class_data->InstanceFields()->size());
676 } else {
677 fprintf(out_file_, "instance_fields_size: 0\n");
678 }
679 if (class_data != nullptr && class_data->DirectMethods() != nullptr) {
680 fprintf(out_file_, "direct_methods_size : %zu\n", class_data->DirectMethods()->size());
681 } else {
682 fprintf(out_file_, "direct_methods_size : 0\n");
683 }
684 if (class_data != nullptr && class_data->VirtualMethods() != nullptr) {
685 fprintf(out_file_, "virtual_methods_size: %zu\n", class_data->VirtualMethods()->size());
686 } else {
687 fprintf(out_file_, "virtual_methods_size: 0\n");
688 }
689 fprintf(out_file_, "\n");
690}
691
692/**
693 * Dumps an annotation set item.
694 */
695void DexLayout::DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item) {
696 if (set_item == nullptr || set_item->GetItems()->size() == 0) {
697 fputs(" empty-annotation-set\n", out_file_);
698 return;
699 }
700 for (dex_ir::AnnotationItem* annotation : *set_item->GetItems()) {
701 if (annotation == nullptr) {
702 continue;
703 }
704 fputs(" ", out_file_);
705 switch (annotation->GetVisibility()) {
706 case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", out_file_); break;
707 case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", out_file_); break;
708 case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", out_file_); break;
709 default: fputs("VISIBILITY_UNKNOWN ", out_file_); break;
710 } // switch
711 DumpEncodedAnnotation(annotation->GetAnnotation());
712 fputc('\n', out_file_);
713 }
714}
715
716/*
717 * Dumps class annotations.
718 */
719void DexLayout::DumpClassAnnotations(int idx) {
720 dex_ir::ClassDef* class_def = header_->GetCollections().GetClassDef(idx);
721 dex_ir::AnnotationsDirectoryItem* annotations_directory = class_def->Annotations();
722 if (annotations_directory == nullptr) {
723 return; // none
724 }
725
726 fprintf(out_file_, "Class #%d annotations:\n", idx);
727
728 dex_ir::AnnotationSetItem* class_set_item = annotations_directory->GetClassAnnotation();
729 dex_ir::FieldAnnotationVector* fields = annotations_directory->GetFieldAnnotations();
730 dex_ir::MethodAnnotationVector* methods = annotations_directory->GetMethodAnnotations();
731 dex_ir::ParameterAnnotationVector* parameters = annotations_directory->GetParameterAnnotations();
732
733 // Annotations on the class itself.
734 if (class_set_item != nullptr) {
735 fprintf(out_file_, "Annotations on class\n");
736 DumpAnnotationSetItem(class_set_item);
737 }
738
739 // Annotations on fields.
740 if (fields != nullptr) {
741 for (auto& field : *fields) {
742 const dex_ir::FieldId* field_id = field->GetFieldId();
743 const uint32_t field_idx = field_id->GetIndex();
744 const char* field_name = field_id->Name()->Data();
745 fprintf(out_file_, "Annotations on field #%u '%s'\n", field_idx, field_name);
746 DumpAnnotationSetItem(field->GetAnnotationSetItem());
747 }
748 }
749
750 // Annotations on methods.
751 if (methods != nullptr) {
752 for (auto& method : *methods) {
753 const dex_ir::MethodId* method_id = method->GetMethodId();
754 const uint32_t method_idx = method_id->GetIndex();
755 const char* method_name = method_id->Name()->Data();
756 fprintf(out_file_, "Annotations on method #%u '%s'\n", method_idx, method_name);
757 DumpAnnotationSetItem(method->GetAnnotationSetItem());
758 }
759 }
760
761 // Annotations on method parameters.
762 if (parameters != nullptr) {
763 for (auto& parameter : *parameters) {
764 const dex_ir::MethodId* method_id = parameter->GetMethodId();
765 const uint32_t method_idx = method_id->GetIndex();
766 const char* method_name = method_id->Name()->Data();
767 fprintf(out_file_, "Annotations on method #%u '%s' parameters\n", method_idx, method_name);
768 uint32_t j = 0;
769 for (dex_ir::AnnotationSetItem* annotation : *parameter->GetAnnotations()->GetItems()) {
770 fprintf(out_file_, "#%u\n", j);
771 DumpAnnotationSetItem(annotation);
772 ++j;
773 }
774 }
775 }
776
777 fputc('\n', out_file_);
778}
779
780/*
781 * Dumps an interface that a class declares to implement.
782 */
783void DexLayout::DumpInterface(const dex_ir::TypeId* type_item, int i) {
784 const char* interface_name = type_item->GetStringId()->Data();
785 if (options_.output_format_ == kOutputPlain) {
786 fprintf(out_file_, " #%d : '%s'\n", i, interface_name);
787 } else {
788 std::string dot(DescriptorToDotWrapper(interface_name));
789 fprintf(out_file_, "<implements name=\"%s\">\n</implements>\n", dot.c_str());
790 }
791}
792
793/*
794 * Dumps the catches table associated with the code.
795 */
796void DexLayout::DumpCatches(const dex_ir::CodeItem* code) {
797 const uint16_t tries_size = code->TriesSize();
798
799 // No catch table.
800 if (tries_size == 0) {
801 fprintf(out_file_, " catches : (none)\n");
802 return;
803 }
804
805 // Dump all table entries.
806 fprintf(out_file_, " catches : %d\n", tries_size);
807 std::vector<std::unique_ptr<const dex_ir::TryItem>>* tries = code->Tries();
808 for (uint32_t i = 0; i < tries_size; i++) {
809 const dex_ir::TryItem* try_item = (*tries)[i].get();
810 const uint32_t start = try_item->StartAddr();
811 const uint32_t end = start + try_item->InsnCount();
812 fprintf(out_file_, " 0x%04x - 0x%04x\n", start, end);
813 for (auto& handler : *try_item->GetHandlers()->GetHandlers()) {
814 const dex_ir::TypeId* type_id = handler->GetTypeId();
815 const char* descriptor = (type_id == nullptr) ? "<any>" : type_id->GetStringId()->Data();
816 fprintf(out_file_, " %s -> 0x%04x\n", descriptor, handler->GetAddress());
817 } // for
818 } // for
819}
820
821/*
David Sehr7629f602016-08-07 16:01:51 -0700822 * Dumps a single instruction.
823 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800824void DexLayout::DumpInstruction(const dex_ir::CodeItem* code,
825 uint32_t code_offset,
826 uint32_t insn_idx,
827 uint32_t insn_width,
828 const Instruction* dec_insn) {
David Sehr7629f602016-08-07 16:01:51 -0700829 // Address of instruction (expressed as byte offset).
830 fprintf(out_file_, "%06x:", code_offset + 0x10 + insn_idx * 2);
831
832 // Dump (part of) raw bytes.
833 const uint16_t* insns = code->Insns();
834 for (uint32_t i = 0; i < 8; i++) {
835 if (i < insn_width) {
836 if (i == 7) {
837 fprintf(out_file_, " ... ");
838 } else {
839 // Print 16-bit value in little-endian order.
840 const uint8_t* bytePtr = (const uint8_t*) &insns[insn_idx + i];
841 fprintf(out_file_, " %02x%02x", bytePtr[0], bytePtr[1]);
842 }
843 } else {
844 fputs(" ", out_file_);
845 }
846 } // for
847
848 // Dump pseudo-instruction or opcode.
849 if (dec_insn->Opcode() == Instruction::NOP) {
850 const uint16_t instr = Get2LE((const uint8_t*) &insns[insn_idx]);
851 if (instr == Instruction::kPackedSwitchSignature) {
852 fprintf(out_file_, "|%04x: packed-switch-data (%d units)", insn_idx, insn_width);
853 } else if (instr == Instruction::kSparseSwitchSignature) {
854 fprintf(out_file_, "|%04x: sparse-switch-data (%d units)", insn_idx, insn_width);
855 } else if (instr == Instruction::kArrayDataSignature) {
856 fprintf(out_file_, "|%04x: array-data (%d units)", insn_idx, insn_width);
857 } else {
858 fprintf(out_file_, "|%04x: nop // spacer", insn_idx);
859 }
860 } else {
861 fprintf(out_file_, "|%04x: %s", insn_idx, dec_insn->Name());
862 }
863
864 // Set up additional argument.
865 std::unique_ptr<char[]> index_buf;
866 if (Instruction::IndexTypeOf(dec_insn->Opcode()) != Instruction::kIndexNone) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800867 index_buf = IndexString(header_, dec_insn, 200);
David Sehr7629f602016-08-07 16:01:51 -0700868 }
869
870 // Dump the instruction.
871 //
872 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
873 //
874 switch (Instruction::FormatOf(dec_insn->Opcode())) {
875 case Instruction::k10x: // op
876 break;
877 case Instruction::k12x: // op vA, vB
878 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
879 break;
880 case Instruction::k11n: // op vA, #+B
881 fprintf(out_file_, " v%d, #int %d // #%x",
882 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint8_t)dec_insn->VRegB());
883 break;
884 case Instruction::k11x: // op vAA
885 fprintf(out_file_, " v%d", dec_insn->VRegA());
886 break;
887 case Instruction::k10t: // op +AA
888 case Instruction::k20t: { // op +AAAA
889 const int32_t targ = (int32_t) dec_insn->VRegA();
890 fprintf(out_file_, " %04x // %c%04x",
891 insn_idx + targ,
892 (targ < 0) ? '-' : '+',
893 (targ < 0) ? -targ : targ);
894 break;
895 }
896 case Instruction::k22x: // op vAA, vBBBB
897 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
898 break;
899 case Instruction::k21t: { // op vAA, +BBBB
900 const int32_t targ = (int32_t) dec_insn->VRegB();
901 fprintf(out_file_, " v%d, %04x // %c%04x", dec_insn->VRegA(),
902 insn_idx + targ,
903 (targ < 0) ? '-' : '+',
904 (targ < 0) ? -targ : targ);
905 break;
906 }
907 case Instruction::k21s: // op vAA, #+BBBB
908 fprintf(out_file_, " v%d, #int %d // #%x",
909 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint16_t)dec_insn->VRegB());
910 break;
911 case Instruction::k21h: // op vAA, #+BBBB0000[00000000]
912 // The printed format varies a bit based on the actual opcode.
913 if (dec_insn->Opcode() == Instruction::CONST_HIGH16) {
914 const int32_t value = dec_insn->VRegB() << 16;
915 fprintf(out_file_, " v%d, #int %d // #%x",
916 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
917 } else {
918 const int64_t value = ((int64_t) dec_insn->VRegB()) << 48;
919 fprintf(out_file_, " v%d, #long %" PRId64 " // #%x",
920 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
921 }
922 break;
923 case Instruction::k21c: // op vAA, thing@BBBB
924 case Instruction::k31c: // op vAA, thing@BBBBBBBB
925 fprintf(out_file_, " v%d, %s", dec_insn->VRegA(), index_buf.get());
926 break;
927 case Instruction::k23x: // op vAA, vBB, vCC
928 fprintf(out_file_, " v%d, v%d, v%d",
929 dec_insn->VRegA(), dec_insn->VRegB(), dec_insn->VRegC());
930 break;
931 case Instruction::k22b: // op vAA, vBB, #+CC
932 fprintf(out_file_, " v%d, v%d, #int %d // #%02x",
933 dec_insn->VRegA(), dec_insn->VRegB(),
934 (int32_t) dec_insn->VRegC(), (uint8_t) dec_insn->VRegC());
935 break;
936 case Instruction::k22t: { // op vA, vB, +CCCC
937 const int32_t targ = (int32_t) dec_insn->VRegC();
938 fprintf(out_file_, " v%d, v%d, %04x // %c%04x",
939 dec_insn->VRegA(), dec_insn->VRegB(),
940 insn_idx + targ,
941 (targ < 0) ? '-' : '+',
942 (targ < 0) ? -targ : targ);
943 break;
944 }
945 case Instruction::k22s: // op vA, vB, #+CCCC
946 fprintf(out_file_, " v%d, v%d, #int %d // #%04x",
947 dec_insn->VRegA(), dec_insn->VRegB(),
948 (int32_t) dec_insn->VRegC(), (uint16_t) dec_insn->VRegC());
949 break;
950 case Instruction::k22c: // op vA, vB, thing@CCCC
951 // NOT SUPPORTED:
952 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
953 fprintf(out_file_, " v%d, v%d, %s",
954 dec_insn->VRegA(), dec_insn->VRegB(), index_buf.get());
955 break;
956 case Instruction::k30t:
957 fprintf(out_file_, " #%08x", dec_insn->VRegA());
958 break;
959 case Instruction::k31i: { // op vAA, #+BBBBBBBB
960 // This is often, but not always, a float.
961 union {
962 float f;
963 uint32_t i;
964 } conv;
965 conv.i = dec_insn->VRegB();
966 fprintf(out_file_, " v%d, #float %g // #%08x",
967 dec_insn->VRegA(), conv.f, dec_insn->VRegB());
968 break;
969 }
970 case Instruction::k31t: // op vAA, offset +BBBBBBBB
971 fprintf(out_file_, " v%d, %08x // +%08x",
972 dec_insn->VRegA(), insn_idx + dec_insn->VRegB(), dec_insn->VRegB());
973 break;
974 case Instruction::k32x: // op vAAAA, vBBBB
975 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
976 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100977 case Instruction::k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
978 case Instruction::k45cc: { // op {vC, vD, vE, vF, vG}, meth@BBBB, proto@HHHH
David Sehr7629f602016-08-07 16:01:51 -0700979 // NOT SUPPORTED:
980 // case Instruction::k35ms: // [opt] invoke-virtual+super
981 // case Instruction::k35mi: // [opt] inline invoke
982 uint32_t arg[Instruction::kMaxVarArgRegs];
983 dec_insn->GetVarArgs(arg);
984 fputs(" {", out_file_);
985 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
986 if (i == 0) {
987 fprintf(out_file_, "v%d", arg[i]);
988 } else {
989 fprintf(out_file_, ", v%d", arg[i]);
990 }
991 } // for
992 fprintf(out_file_, "}, %s", index_buf.get());
993 break;
994 }
Orion Hodsonb34bb192016-10-18 17:02:58 +0100995 case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
996 case Instruction::k4rcc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB, proto@HHHH
David Sehr7629f602016-08-07 16:01:51 -0700997 // NOT SUPPORTED:
998 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
999 // case Instruction::k3rmi: // [opt] execute-inline/range
1000 {
1001 // This doesn't match the "dx" output when some of the args are
1002 // 64-bit values -- dx only shows the first register.
1003 fputs(" {", out_file_);
1004 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
1005 if (i == 0) {
1006 fprintf(out_file_, "v%d", dec_insn->VRegC() + i);
1007 } else {
1008 fprintf(out_file_, ", v%d", dec_insn->VRegC() + i);
1009 }
1010 } // for
1011 fprintf(out_file_, "}, %s", index_buf.get());
1012 }
1013 break;
1014 case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB
1015 // This is often, but not always, a double.
1016 union {
1017 double d;
1018 uint64_t j;
1019 } conv;
1020 conv.j = dec_insn->WideVRegB();
1021 fprintf(out_file_, " v%d, #double %g // #%016" PRIx64,
1022 dec_insn->VRegA(), conv.d, dec_insn->WideVRegB());
1023 break;
1024 }
1025 // NOT SUPPORTED:
1026 // case Instruction::k00x: // unknown op or breakpoint
1027 // break;
1028 default:
1029 fprintf(out_file_, " ???");
1030 break;
1031 } // switch
1032
1033 fputc('\n', out_file_);
1034}
1035
1036/*
1037 * Dumps a bytecode disassembly.
1038 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001039void DexLayout::DumpBytecodes(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset) {
1040 dex_ir::MethodId* method_id = header_->GetCollections().GetMethodId(idx);
David Sehr7629f602016-08-07 16:01:51 -07001041 const char* name = method_id->Name()->Data();
David Sehr72359222016-09-07 13:04:01 -07001042 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -07001043 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1044
1045 // Generate header.
Jeff Haoc3acfc52016-08-29 14:18:26 -07001046 std::string dot(DescriptorToDotWrapper(back_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001047 fprintf(out_file_, "%06x: |[%06x] %s.%s:%s\n",
David Sehr72359222016-09-07 13:04:01 -07001048 code_offset, code_offset, dot.c_str(), name, type_descriptor.c_str());
David Sehr7629f602016-08-07 16:01:51 -07001049
1050 // Iterate over all instructions.
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001051 for (const DexInstructionPcPair& inst : code->Instructions()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001052 const uint32_t insn_width = inst->SizeInCodeUnits();
David Sehr7629f602016-08-07 16:01:51 -07001053 if (insn_width == 0) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001054 fprintf(stderr, "GLITCH: zero-width instruction at idx=0x%04x\n", inst.DexPc());
David Sehr7629f602016-08-07 16:01:51 -07001055 break;
1056 }
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001057 DumpInstruction(code, code_offset, inst.DexPc(), insn_width, &inst.Inst());
David Sehr7629f602016-08-07 16:01:51 -07001058 } // for
1059}
1060
1061/*
David Sehraa6abb02017-10-12 08:25:11 -07001062 * Callback for dumping each positions table entry.
1063 */
1064static bool DumpPositionsCb(void* context, const DexFile::PositionInfo& entry) {
1065 FILE* out_file = reinterpret_cast<FILE*>(context);
1066 fprintf(out_file, " 0x%04x line=%d\n", entry.address_, entry.line_);
1067 return false;
1068}
1069
1070/*
1071 * Callback for dumping locals table entry.
1072 */
1073static void DumpLocalsCb(void* context, const DexFile::LocalInfo& entry) {
1074 const char* signature = entry.signature_ != nullptr ? entry.signature_ : "";
1075 FILE* out_file = reinterpret_cast<FILE*>(context);
1076 fprintf(out_file, " 0x%04x - 0x%04x reg=%d %s %s %s\n",
1077 entry.start_address_, entry.end_address_, entry.reg_,
1078 entry.name_, entry.descriptor_, signature);
1079}
1080
1081/*
1082 * Lookup functions.
1083 */
1084static const char* StringDataByIdx(uint32_t idx, dex_ir::Collections& collections) {
1085 dex_ir::StringId* string_id = collections.GetStringIdOrNullPtr(idx);
1086 if (string_id == nullptr) {
1087 return nullptr;
1088 }
1089 return string_id->Data();
1090}
1091
1092static const char* StringDataByTypeIdx(uint16_t idx, dex_ir::Collections& collections) {
1093 dex_ir::TypeId* type_id = collections.GetTypeIdOrNullPtr(idx);
1094 if (type_id == nullptr) {
1095 return nullptr;
1096 }
1097 dex_ir::StringId* string_id = type_id->GetStringId();
1098 if (string_id == nullptr) {
1099 return nullptr;
1100 }
1101 return string_id->Data();
1102}
1103
1104
1105/*
David Sehr7629f602016-08-07 16:01:51 -07001106 * Dumps code of a method.
1107 */
David Sehraa6abb02017-10-12 08:25:11 -07001108void DexLayout::DumpCode(uint32_t idx,
1109 const dex_ir::CodeItem* code,
1110 uint32_t code_offset,
1111 const char* declaring_class_descriptor,
1112 const char* method_name,
1113 bool is_static,
1114 const dex_ir::ProtoId* proto) {
David Sehr7629f602016-08-07 16:01:51 -07001115 fprintf(out_file_, " registers : %d\n", code->RegistersSize());
1116 fprintf(out_file_, " ins : %d\n", code->InsSize());
1117 fprintf(out_file_, " outs : %d\n", code->OutsSize());
1118 fprintf(out_file_, " insns size : %d 16-bit code units\n",
1119 code->InsnsSize());
1120
1121 // Bytecode disassembly, if requested.
1122 if (options_.disassemble_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001123 DumpBytecodes(idx, code, code_offset);
David Sehr7629f602016-08-07 16:01:51 -07001124 }
1125
1126 // Try-catch blocks.
1127 DumpCatches(code);
1128
1129 // Positions and locals table in the debug info.
David Sehraa6abb02017-10-12 08:25:11 -07001130 dex_ir::DebugInfoItem* debug_info = code->DebugInfo();
David Sehr7629f602016-08-07 16:01:51 -07001131 fprintf(out_file_, " positions : \n");
David Sehraa6abb02017-10-12 08:25:11 -07001132 if (debug_info != nullptr) {
1133 DexFile::DecodeDebugPositionInfo(debug_info->GetDebugInfo(),
1134 [this](uint32_t idx) {
1135 return StringDataByIdx(idx, this->header_->GetCollections());
1136 },
1137 DumpPositionsCb,
1138 out_file_);
1139 }
David Sehr7629f602016-08-07 16:01:51 -07001140 fprintf(out_file_, " locals : \n");
David Sehraa6abb02017-10-12 08:25:11 -07001141 if (debug_info != nullptr) {
1142 std::vector<const char*> arg_descriptors;
1143 const dex_ir::TypeList* parameters = proto->Parameters();
1144 if (parameters != nullptr) {
1145 const dex_ir::TypeIdVector* parameter_type_vector = parameters->GetTypeList();
1146 if (parameter_type_vector != nullptr) {
1147 for (const dex_ir::TypeId* type_id : *parameter_type_vector) {
1148 arg_descriptors.push_back(type_id->GetStringId()->Data());
1149 }
1150 }
1151 }
1152 DexFile::DecodeDebugLocalInfo(debug_info->GetDebugInfo(),
1153 "DexLayout in-memory",
1154 declaring_class_descriptor,
1155 arg_descriptors,
1156 method_name,
1157 is_static,
1158 code->RegistersSize(),
1159 code->InsSize(),
1160 code->InsnsSize(),
1161 [this](uint32_t idx) {
1162 return StringDataByIdx(idx, this->header_->GetCollections());
1163 },
1164 [this](uint32_t idx) {
1165 return
1166 StringDataByTypeIdx(dchecked_integral_cast<uint16_t>(idx),
1167 this->header_->GetCollections());
1168 },
1169 DumpLocalsCb,
1170 out_file_);
1171 }
David Sehr7629f602016-08-07 16:01:51 -07001172}
1173
1174/*
1175 * Dumps a method.
1176 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001177void DexLayout::DumpMethod(uint32_t idx, uint32_t flags, const dex_ir::CodeItem* code, int i) {
David Sehr7629f602016-08-07 16:01:51 -07001178 // Bail for anything private if export only requested.
1179 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1180 return;
1181 }
1182
Jeff Haoea7c6292016-11-14 18:10:16 -08001183 dex_ir::MethodId* method_id = header_->GetCollections().GetMethodId(idx);
David Sehr7629f602016-08-07 16:01:51 -07001184 const char* name = method_id->Name()->Data();
1185 char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str());
1186 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1187 char* access_str = CreateAccessFlagStr(flags, kAccessForMethod);
1188
1189 if (options_.output_format_ == kOutputPlain) {
1190 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1191 fprintf(out_file_, " name : '%s'\n", name);
1192 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1193 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
1194 if (code == nullptr) {
1195 fprintf(out_file_, " code : (none)\n");
1196 } else {
1197 fprintf(out_file_, " code -\n");
David Sehraa6abb02017-10-12 08:25:11 -07001198 DumpCode(idx,
1199 code,
1200 code->GetOffset(),
1201 back_descriptor,
1202 name,
1203 (flags & kAccStatic) != 0,
1204 method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -07001205 }
1206 if (options_.disassemble_) {
1207 fputc('\n', out_file_);
1208 }
1209 } else if (options_.output_format_ == kOutputXml) {
1210 const bool constructor = (name[0] == '<');
1211
1212 // Method name and prototype.
1213 if (constructor) {
1214 std::string dot(DescriptorClassToDot(back_descriptor));
1215 fprintf(out_file_, "<constructor name=\"%s\"\n", dot.c_str());
Jeff Haoc3acfc52016-08-29 14:18:26 -07001216 dot = DescriptorToDotWrapper(back_descriptor);
David Sehr7629f602016-08-07 16:01:51 -07001217 fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
1218 } else {
1219 fprintf(out_file_, "<method name=\"%s\"\n", name);
1220 const char* return_type = strrchr(type_descriptor, ')');
1221 if (return_type == nullptr) {
1222 fprintf(stderr, "bad method type descriptor '%s'\n", type_descriptor);
1223 goto bail;
1224 }
Jeff Haoc3acfc52016-08-29 14:18:26 -07001225 std::string dot(DescriptorToDotWrapper(return_type + 1));
David Sehr7629f602016-08-07 16:01:51 -07001226 fprintf(out_file_, " return=\"%s\"\n", dot.c_str());
1227 fprintf(out_file_, " abstract=%s\n", QuotedBool((flags & kAccAbstract) != 0));
1228 fprintf(out_file_, " native=%s\n", QuotedBool((flags & kAccNative) != 0));
1229 fprintf(out_file_, " synchronized=%s\n", QuotedBool(
1230 (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0));
1231 }
1232
1233 // Additional method flags.
1234 fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
1235 fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
1236 // The "deprecated=" not knowable w/o parsing annotations.
1237 fprintf(out_file_, " visibility=%s\n>\n", QuotedVisibility(flags));
1238
1239 // Parameters.
1240 if (type_descriptor[0] != '(') {
1241 fprintf(stderr, "ERROR: bad descriptor '%s'\n", type_descriptor);
1242 goto bail;
1243 }
1244 char* tmp_buf = reinterpret_cast<char*>(malloc(strlen(type_descriptor) + 1));
1245 const char* base = type_descriptor + 1;
1246 int arg_num = 0;
1247 while (*base != ')') {
1248 char* cp = tmp_buf;
1249 while (*base == '[') {
1250 *cp++ = *base++;
1251 }
1252 if (*base == 'L') {
1253 // Copy through ';'.
1254 do {
1255 *cp = *base++;
1256 } while (*cp++ != ';');
1257 } else {
1258 // Primitive char, copy it.
1259 if (strchr("ZBCSIFJD", *base) == nullptr) {
1260 fprintf(stderr, "ERROR: bad method signature '%s'\n", base);
1261 break; // while
1262 }
1263 *cp++ = *base++;
1264 }
1265 // Null terminate and display.
1266 *cp++ = '\0';
Jeff Haoc3acfc52016-08-29 14:18:26 -07001267 std::string dot(DescriptorToDotWrapper(tmp_buf));
David Sehr7629f602016-08-07 16:01:51 -07001268 fprintf(out_file_, "<parameter name=\"arg%d\" type=\"%s\">\n"
1269 "</parameter>\n", arg_num++, dot.c_str());
1270 } // while
1271 free(tmp_buf);
1272 if (constructor) {
1273 fprintf(out_file_, "</constructor>\n");
1274 } else {
1275 fprintf(out_file_, "</method>\n");
1276 }
1277 }
1278
1279 bail:
1280 free(type_descriptor);
1281 free(access_str);
1282}
1283
1284/*
1285 * Dumps a static (class) field.
1286 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001287void DexLayout::DumpSField(uint32_t idx, uint32_t flags, int i, dex_ir::EncodedValue* init) {
David Sehr7629f602016-08-07 16:01:51 -07001288 // Bail for anything private if export only requested.
1289 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1290 return;
1291 }
1292
Jeff Haoea7c6292016-11-14 18:10:16 -08001293 dex_ir::FieldId* field_id = header_->GetCollections().GetFieldId(idx);
David Sehr7629f602016-08-07 16:01:51 -07001294 const char* name = field_id->Name()->Data();
1295 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
1296 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
1297 char* access_str = CreateAccessFlagStr(flags, kAccessForField);
1298
1299 if (options_.output_format_ == kOutputPlain) {
1300 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1301 fprintf(out_file_, " name : '%s'\n", name);
1302 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1303 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
1304 if (init != nullptr) {
1305 fputs(" value : ", out_file_);
1306 DumpEncodedValue(init);
1307 fputs("\n", out_file_);
1308 }
1309 } else if (options_.output_format_ == kOutputXml) {
1310 fprintf(out_file_, "<field name=\"%s\"\n", name);
Jeff Haoc3acfc52016-08-29 14:18:26 -07001311 std::string dot(DescriptorToDotWrapper(type_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001312 fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
1313 fprintf(out_file_, " transient=%s\n", QuotedBool((flags & kAccTransient) != 0));
1314 fprintf(out_file_, " volatile=%s\n", QuotedBool((flags & kAccVolatile) != 0));
1315 // The "value=" is not knowable w/o parsing annotations.
1316 fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
1317 fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
1318 // The "deprecated=" is not knowable w/o parsing annotations.
1319 fprintf(out_file_, " visibility=%s\n", QuotedVisibility(flags));
1320 if (init != nullptr) {
1321 fputs(" value=\"", out_file_);
1322 DumpEncodedValue(init);
1323 fputs("\"\n", out_file_);
1324 }
1325 fputs(">\n</field>\n", out_file_);
1326 }
1327
1328 free(access_str);
1329}
1330
1331/*
1332 * Dumps an instance field.
1333 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001334void DexLayout::DumpIField(uint32_t idx, uint32_t flags, int i) {
1335 DumpSField(idx, flags, i, nullptr);
David Sehr7629f602016-08-07 16:01:51 -07001336}
1337
1338/*
David Sehr7629f602016-08-07 16:01:51 -07001339 * Dumps the class.
1340 *
1341 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1342 *
1343 * If "*last_package" is nullptr or does not match the current class' package,
1344 * the value will be replaced with a newly-allocated string.
1345 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001346void DexLayout::DumpClass(int idx, char** last_package) {
1347 dex_ir::ClassDef* class_def = header_->GetCollections().GetClassDef(idx);
David Sehr7629f602016-08-07 16:01:51 -07001348 // Omitting non-public class.
1349 if (options_.exports_only_ && (class_def->GetAccessFlags() & kAccPublic) == 0) {
1350 return;
1351 }
1352
1353 if (options_.show_section_headers_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001354 DumpClassDef(idx);
David Sehr7629f602016-08-07 16:01:51 -07001355 }
1356
1357 if (options_.show_annotations_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001358 DumpClassAnnotations(idx);
David Sehr7629f602016-08-07 16:01:51 -07001359 }
1360
David Sehr7629f602016-08-07 16:01:51 -07001361 // For the XML output, show the package name. Ideally we'd gather
1362 // up the classes, sort them, and dump them alphabetically so the
1363 // package name wouldn't jump around, but that's not a great plan
1364 // for something that needs to run on the device.
Jeff Hao3ab96b42016-09-09 18:35:01 -07001365 const char* class_descriptor =
Jeff Haoea7c6292016-11-14 18:10:16 -08001366 header_->GetCollections().GetClassDef(idx)->ClassType()->GetStringId()->Data();
David Sehr7629f602016-08-07 16:01:51 -07001367 if (!(class_descriptor[0] == 'L' &&
1368 class_descriptor[strlen(class_descriptor)-1] == ';')) {
1369 // Arrays and primitives should not be defined explicitly. Keep going?
1370 fprintf(stderr, "Malformed class name '%s'\n", class_descriptor);
1371 } else if (options_.output_format_ == kOutputXml) {
1372 char* mangle = strdup(class_descriptor + 1);
1373 mangle[strlen(mangle)-1] = '\0';
1374
1375 // Reduce to just the package name.
1376 char* last_slash = strrchr(mangle, '/');
1377 if (last_slash != nullptr) {
1378 *last_slash = '\0';
1379 } else {
1380 *mangle = '\0';
1381 }
1382
1383 for (char* cp = mangle; *cp != '\0'; cp++) {
1384 if (*cp == '/') {
1385 *cp = '.';
1386 }
1387 } // for
1388
1389 if (*last_package == nullptr || strcmp(mangle, *last_package) != 0) {
1390 // Start of a new package.
1391 if (*last_package != nullptr) {
1392 fprintf(out_file_, "</package>\n");
1393 }
1394 fprintf(out_file_, "<package name=\"%s\"\n>\n", mangle);
1395 free(*last_package);
1396 *last_package = mangle;
1397 } else {
1398 free(mangle);
1399 }
1400 }
1401
1402 // General class information.
1403 char* access_str = CreateAccessFlagStr(class_def->GetAccessFlags(), kAccessForClass);
1404 const char* superclass_descriptor = nullptr;
1405 if (class_def->Superclass() != nullptr) {
1406 superclass_descriptor = class_def->Superclass()->GetStringId()->Data();
1407 }
1408 if (options_.output_format_ == kOutputPlain) {
1409 fprintf(out_file_, "Class #%d -\n", idx);
1410 fprintf(out_file_, " Class descriptor : '%s'\n", class_descriptor);
1411 fprintf(out_file_, " Access flags : 0x%04x (%s)\n",
1412 class_def->GetAccessFlags(), access_str);
1413 if (superclass_descriptor != nullptr) {
1414 fprintf(out_file_, " Superclass : '%s'\n", superclass_descriptor);
1415 }
1416 fprintf(out_file_, " Interfaces -\n");
1417 } else {
1418 std::string dot(DescriptorClassToDot(class_descriptor));
1419 fprintf(out_file_, "<class name=\"%s\"\n", dot.c_str());
1420 if (superclass_descriptor != nullptr) {
Jeff Haoc3acfc52016-08-29 14:18:26 -07001421 dot = DescriptorToDotWrapper(superclass_descriptor);
David Sehr7629f602016-08-07 16:01:51 -07001422 fprintf(out_file_, " extends=\"%s\"\n", dot.c_str());
1423 }
1424 fprintf(out_file_, " interface=%s\n",
1425 QuotedBool((class_def->GetAccessFlags() & kAccInterface) != 0));
1426 fprintf(out_file_, " abstract=%s\n",
1427 QuotedBool((class_def->GetAccessFlags() & kAccAbstract) != 0));
1428 fprintf(out_file_, " static=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccStatic) != 0));
1429 fprintf(out_file_, " final=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccFinal) != 0));
1430 // The "deprecated=" not knowable w/o parsing annotations.
1431 fprintf(out_file_, " visibility=%s\n", QuotedVisibility(class_def->GetAccessFlags()));
1432 fprintf(out_file_, ">\n");
1433 }
1434
1435 // Interfaces.
Jeff Haocc829592017-03-14 16:13:39 -07001436 const dex_ir::TypeList* interfaces = class_def->Interfaces();
David Sehr853a8e12016-09-01 13:03:50 -07001437 if (interfaces != nullptr) {
Jeff Haocc829592017-03-14 16:13:39 -07001438 const dex_ir::TypeIdVector* interfaces_vector = interfaces->GetTypeList();
1439 for (uint32_t i = 0; i < interfaces_vector->size(); i++) {
1440 DumpInterface((*interfaces_vector)[i], i);
David Sehr853a8e12016-09-01 13:03:50 -07001441 } // for
1442 }
David Sehr7629f602016-08-07 16:01:51 -07001443
1444 // Fields and methods.
1445 dex_ir::ClassData* class_data = class_def->GetClassData();
1446 // Prepare data for static fields.
Jeff Hao3ab96b42016-09-09 18:35:01 -07001447 dex_ir::EncodedArrayItem* static_values = class_def->StaticValues();
1448 dex_ir::EncodedValueVector* encoded_values =
1449 static_values == nullptr ? nullptr : static_values->GetEncodedValues();
1450 const uint32_t encoded_values_size = (encoded_values == nullptr) ? 0 : encoded_values->size();
David Sehr7629f602016-08-07 16:01:51 -07001451
1452 // Static fields.
1453 if (options_.output_format_ == kOutputPlain) {
1454 fprintf(out_file_, " Static fields -\n");
1455 }
David Sehr853a8e12016-09-01 13:03:50 -07001456 if (class_data != nullptr) {
1457 dex_ir::FieldItemVector* static_fields = class_data->StaticFields();
1458 if (static_fields != nullptr) {
1459 for (uint32_t i = 0; i < static_fields->size(); i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001460 DumpSField((*static_fields)[i]->GetFieldId()->GetIndex(),
David Sehr853a8e12016-09-01 13:03:50 -07001461 (*static_fields)[i]->GetAccessFlags(),
1462 i,
Jeff Hao3ab96b42016-09-09 18:35:01 -07001463 i < encoded_values_size ? (*encoded_values)[i].get() : nullptr);
David Sehr853a8e12016-09-01 13:03:50 -07001464 } // for
1465 }
1466 }
David Sehr7629f602016-08-07 16:01:51 -07001467
1468 // Instance fields.
1469 if (options_.output_format_ == kOutputPlain) {
1470 fprintf(out_file_, " Instance fields -\n");
1471 }
David Sehr853a8e12016-09-01 13:03:50 -07001472 if (class_data != nullptr) {
1473 dex_ir::FieldItemVector* instance_fields = class_data->InstanceFields();
1474 if (instance_fields != nullptr) {
1475 for (uint32_t i = 0; i < instance_fields->size(); i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001476 DumpIField((*instance_fields)[i]->GetFieldId()->GetIndex(),
David Sehr853a8e12016-09-01 13:03:50 -07001477 (*instance_fields)[i]->GetAccessFlags(),
1478 i);
1479 } // for
1480 }
1481 }
David Sehr7629f602016-08-07 16:01:51 -07001482
1483 // Direct methods.
1484 if (options_.output_format_ == kOutputPlain) {
1485 fprintf(out_file_, " Direct methods -\n");
1486 }
David Sehr853a8e12016-09-01 13:03:50 -07001487 if (class_data != nullptr) {
1488 dex_ir::MethodItemVector* direct_methods = class_data->DirectMethods();
1489 if (direct_methods != nullptr) {
1490 for (uint32_t i = 0; i < direct_methods->size(); i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001491 DumpMethod((*direct_methods)[i]->GetMethodId()->GetIndex(),
David Sehr853a8e12016-09-01 13:03:50 -07001492 (*direct_methods)[i]->GetAccessFlags(),
1493 (*direct_methods)[i]->GetCodeItem(),
1494 i);
1495 } // for
1496 }
1497 }
David Sehr7629f602016-08-07 16:01:51 -07001498
1499 // Virtual methods.
1500 if (options_.output_format_ == kOutputPlain) {
1501 fprintf(out_file_, " Virtual methods -\n");
1502 }
David Sehr853a8e12016-09-01 13:03:50 -07001503 if (class_data != nullptr) {
1504 dex_ir::MethodItemVector* virtual_methods = class_data->VirtualMethods();
1505 if (virtual_methods != nullptr) {
1506 for (uint32_t i = 0; i < virtual_methods->size(); i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001507 DumpMethod((*virtual_methods)[i]->GetMethodId()->GetIndex(),
David Sehr853a8e12016-09-01 13:03:50 -07001508 (*virtual_methods)[i]->GetAccessFlags(),
1509 (*virtual_methods)[i]->GetCodeItem(),
1510 i);
1511 } // for
1512 }
1513 }
David Sehr7629f602016-08-07 16:01:51 -07001514
1515 // End of class.
1516 if (options_.output_format_ == kOutputPlain) {
1517 const char* file_name = "unknown";
1518 if (class_def->SourceFile() != nullptr) {
1519 file_name = class_def->SourceFile()->Data();
1520 }
1521 const dex_ir::StringId* source_file = class_def->SourceFile();
1522 fprintf(out_file_, " source_file_idx : %d (%s)\n\n",
Jeff Hao3ab96b42016-09-09 18:35:01 -07001523 source_file == nullptr ? 0xffffffffU : source_file->GetIndex(), file_name);
David Sehr7629f602016-08-07 16:01:51 -07001524 } else if (options_.output_format_ == kOutputXml) {
1525 fprintf(out_file_, "</class>\n");
1526 }
1527
1528 free(access_str);
1529}
1530
Jeff Haoea7c6292016-11-14 18:10:16 -08001531void DexLayout::DumpDexFile() {
David Sehr7629f602016-08-07 16:01:51 -07001532 // Headers.
1533 if (options_.show_file_headers_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001534 DumpFileHeader();
David Sehr7629f602016-08-07 16:01:51 -07001535 }
1536
1537 // Open XML context.
1538 if (options_.output_format_ == kOutputXml) {
1539 fprintf(out_file_, "<api>\n");
1540 }
1541
1542 // Iterate over all classes.
1543 char* package = nullptr;
Jeff Haoea7c6292016-11-14 18:10:16 -08001544 const uint32_t class_defs_size = header_->GetCollections().ClassDefsSize();
David Sehr7629f602016-08-07 16:01:51 -07001545 for (uint32_t i = 0; i < class_defs_size; i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001546 DumpClass(i, &package);
David Sehr7629f602016-08-07 16:01:51 -07001547 } // for
1548
1549 // Free the last package allocated.
1550 if (package != nullptr) {
1551 fprintf(out_file_, "</package>\n");
1552 free(package);
1553 }
1554
1555 // Close XML context.
1556 if (options_.output_format_ == kOutputXml) {
1557 fprintf(out_file_, "</api>\n");
1558 }
Jeff Haoea7c6292016-11-14 18:10:16 -08001559}
Jeff Hao3ab96b42016-09-09 18:35:01 -07001560
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001561void DexLayout::LayoutClassDefsAndClassData(const DexFile* dex_file) {
Jeff Hao042e8982016-10-19 11:17:11 -07001562 std::vector<dex_ir::ClassDef*> new_class_def_order;
1563 for (std::unique_ptr<dex_ir::ClassDef>& class_def : header_->GetCollections().ClassDefs()) {
1564 dex::TypeIndex type_idx(class_def->ClassType()->GetIndex());
1565 if (info_->ContainsClass(*dex_file, type_idx)) {
1566 new_class_def_order.push_back(class_def.get());
1567 }
1568 }
1569 for (std::unique_ptr<dex_ir::ClassDef>& class_def : header_->GetCollections().ClassDefs()) {
1570 dex::TypeIndex type_idx(class_def->ClassType()->GetIndex());
1571 if (!info_->ContainsClass(*dex_file, type_idx)) {
1572 new_class_def_order.push_back(class_def.get());
1573 }
1574 }
Jeff Haoe17f5892017-02-23 16:14:04 -08001575 std::unordered_set<dex_ir::ClassData*> visited_class_data;
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001576 size_t class_data_index = 0;
1577 dex_ir::CollectionVector<dex_ir::ClassData>::Vector& class_datas =
1578 header_->GetCollections().ClassDatas();
1579 for (dex_ir::ClassDef* class_def : new_class_def_order) {
Jeff Haoe17f5892017-02-23 16:14:04 -08001580 dex_ir::ClassData* class_data = class_def->GetClassData();
1581 if (class_data != nullptr && visited_class_data.find(class_data) == visited_class_data.end()) {
Jeff Haoe17f5892017-02-23 16:14:04 -08001582 visited_class_data.insert(class_data);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001583 // Overwrite the existing vector with the new ordering, note that the sets of objects are
1584 // equivalent, but the order changes. This is why this is not a memory leak.
1585 // TODO: Consider cleaning this up with a shared_ptr.
1586 class_datas[class_data_index].release();
1587 class_datas[class_data_index].reset(class_data);
1588 ++class_data_index;
Jeff Hao042e8982016-10-19 11:17:11 -07001589 }
1590 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001591 CHECK_EQ(class_data_index, class_datas.size());
1592
Mathieu Chartier2c4b0842017-12-13 11:49:51 -08001593 if (DexLayout::kChangeClassDefOrder) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001594 // This currently produces dex files that violate the spec since the super class class_def is
1595 // supposed to occur before any subclasses.
1596 dex_ir::CollectionVector<dex_ir::ClassDef>::Vector& class_defs =
1597 header_->GetCollections().ClassDefs();
1598 CHECK_EQ(new_class_def_order.size(), class_defs.size());
1599 for (size_t i = 0; i < class_defs.size(); ++i) {
1600 // Overwrite the existing vector with the new ordering, note that the sets of objects are
1601 // equivalent, but the order changes. This is why this is not a memory leak.
1602 // TODO: Consider cleaning this up with a shared_ptr.
1603 class_defs[i].release();
1604 class_defs[i].reset(new_class_def_order[i]);
1605 }
1606 }
Jeff Hao042e8982016-10-19 11:17:11 -07001607}
1608
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001609void DexLayout::LayoutStringData(const DexFile* dex_file) {
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001610 const size_t num_strings = header_->GetCollections().StringIds().size();
1611 std::vector<bool> is_shorty(num_strings, false);
1612 std::vector<bool> from_hot_method(num_strings, false);
1613 for (std::unique_ptr<dex_ir::ClassDef>& class_def : header_->GetCollections().ClassDefs()) {
1614 // 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 -07001615 // as hot. Add its super class and interfaces as well, which can be used during initialization.
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001616 const bool is_profile_class =
1617 info_->ContainsClass(*dex_file, dex::TypeIndex(class_def->ClassType()->GetIndex()));
1618 if (is_profile_class) {
1619 from_hot_method[class_def->ClassType()->GetStringId()->GetIndex()] = true;
Jeff Haoacc83d72017-07-06 17:51:01 -07001620 const dex_ir::TypeId* superclass = class_def->Superclass();
1621 if (superclass != nullptr) {
1622 from_hot_method[superclass->GetStringId()->GetIndex()] = true;
1623 }
1624 const dex_ir::TypeList* interfaces = class_def->Interfaces();
1625 if (interfaces != nullptr) {
1626 for (const dex_ir::TypeId* interface_type : *interfaces->GetTypeList()) {
1627 from_hot_method[interface_type->GetStringId()->GetIndex()] = true;
1628 }
1629 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001630 }
1631 dex_ir::ClassData* data = class_def->GetClassData();
1632 if (data == nullptr) {
1633 continue;
1634 }
1635 for (size_t i = 0; i < 2; ++i) {
1636 for (auto& method : *(i == 0 ? data->DirectMethods() : data->VirtualMethods())) {
1637 const dex_ir::MethodId* method_id = method->GetMethodId();
1638 dex_ir::CodeItem* code_item = method->GetCodeItem();
1639 if (code_item == nullptr) {
1640 continue;
1641 }
1642 const bool is_clinit = is_profile_class &&
1643 (method->GetAccessFlags() & kAccConstructor) != 0 &&
1644 (method->GetAccessFlags() & kAccStatic) != 0;
1645 const bool method_executed = is_clinit ||
Mathieu Chartiere46f3a82017-06-19 19:54:12 -07001646 info_->GetMethodHotness(MethodReference(dex_file, method_id->GetIndex())).IsInProfile();
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001647 if (!method_executed) {
1648 continue;
1649 }
1650 is_shorty[method_id->Proto()->Shorty()->GetIndex()] = true;
1651 dex_ir::CodeFixups* fixups = code_item->GetCodeFixups();
1652 if (fixups == nullptr) {
1653 continue;
1654 }
Jeff Haoacc83d72017-07-06 17:51:01 -07001655 // Add const-strings.
Vladimir Marko219cb902017-12-07 16:20:39 +00001656 for (dex_ir::StringId* id : fixups->StringIds()) {
Jeff Haoacc83d72017-07-06 17:51:01 -07001657 from_hot_method[id->GetIndex()] = true;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001658 }
Jeff Haoacc83d72017-07-06 17:51:01 -07001659 // Add field classes, names, and types.
Vladimir Marko219cb902017-12-07 16:20:39 +00001660 for (dex_ir::FieldId* id : fixups->FieldIds()) {
Jeff Haoacc83d72017-07-06 17:51:01 -07001661 // TODO: Only visit field ids from static getters and setters.
1662 from_hot_method[id->Class()->GetStringId()->GetIndex()] = true;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001663 from_hot_method[id->Name()->GetIndex()] = true;
1664 from_hot_method[id->Type()->GetStringId()->GetIndex()] = true;
1665 }
Jeff Haoacc83d72017-07-06 17:51:01 -07001666 // For clinits, add referenced method classes, names, and protos.
1667 if (is_clinit) {
Vladimir Marko219cb902017-12-07 16:20:39 +00001668 for (dex_ir::MethodId* id : fixups->MethodIds()) {
Jeff Haoacc83d72017-07-06 17:51:01 -07001669 from_hot_method[id->Class()->GetStringId()->GetIndex()] = true;
1670 from_hot_method[id->Name()->GetIndex()] = true;
1671 is_shorty[id->Proto()->Shorty()->GetIndex()] = true;
1672 }
1673 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001674 }
1675 }
1676 }
1677 // Sort string data by specified order.
1678 std::vector<dex_ir::StringId*> string_ids;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001679 for (auto& string_id : header_->GetCollections().StringIds()) {
1680 string_ids.push_back(string_id.get());
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001681 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001682 std::sort(string_ids.begin(),
1683 string_ids.end(),
1684 [&is_shorty, &from_hot_method](const dex_ir::StringId* a,
1685 const dex_ir::StringId* b) {
1686 const bool a_is_hot = from_hot_method[a->GetIndex()];
1687 const bool b_is_hot = from_hot_method[b->GetIndex()];
1688 if (a_is_hot != b_is_hot) {
1689 return a_is_hot < b_is_hot;
1690 }
1691 // After hot methods are partitioned, subpartition shorties.
1692 const bool a_is_shorty = is_shorty[a->GetIndex()];
1693 const bool b_is_shorty = is_shorty[b->GetIndex()];
1694 if (a_is_shorty != b_is_shorty) {
1695 return a_is_shorty < b_is_shorty;
1696 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001697 // Order by index by default.
1698 return a->GetIndex() < b->GetIndex();
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001699 });
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001700 dex_ir::CollectionVector<dex_ir::StringData>::Vector& string_datas =
1701 header_->GetCollections().StringDatas();
1702 // Now we know what order we want the string data, reorder them.
1703 size_t data_index = 0;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001704 for (dex_ir::StringId* string_id : string_ids) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001705 string_datas[data_index].release();
1706 string_datas[data_index].reset(string_id->DataItem());
1707 ++data_index;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001708 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001709 if (kIsDebugBuild) {
1710 std::unordered_set<dex_ir::StringData*> visited;
1711 for (const std::unique_ptr<dex_ir::StringData>& data : string_datas) {
1712 visited.insert(data.get());
1713 }
1714 for (auto& string_id : header_->GetCollections().StringIds()) {
1715 CHECK(visited.find(string_id->DataItem()) != visited.end());
1716 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001717 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001718 CHECK_EQ(data_index, string_datas.size());
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001719}
1720
Jeff Haoe17f5892017-02-23 16:14:04 -08001721// Orders code items according to specified class data ordering.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001722void DexLayout::LayoutCodeItems(const DexFile* dex_file) {
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001723 static constexpr InvokeType invoke_types[] = {
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001724 kDirect,
1725 kVirtual
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001726 };
1727
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001728 std::unordered_map<dex_ir::CodeItem*, LayoutType>& code_item_layout =
1729 layout_hotness_info_.code_item_layout_;
1730
1731 // Assign hotness flags to all code items.
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001732 for (InvokeType invoke_type : invoke_types) {
1733 for (std::unique_ptr<dex_ir::ClassDef>& class_def : header_->GetCollections().ClassDefs()) {
1734 const bool is_profile_class =
1735 info_->ContainsClass(*dex_file, dex::TypeIndex(class_def->ClassType()->GetIndex()));
1736
1737 // Skip classes that are not defined in this dex file.
1738 dex_ir::ClassData* class_data = class_def->GetClassData();
1739 if (class_data == nullptr) {
1740 continue;
Jeff Haoe17f5892017-02-23 16:14:04 -08001741 }
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001742 for (auto& method : *(invoke_type == InvokeType::kDirect
1743 ? class_data->DirectMethods()
1744 : class_data->VirtualMethods())) {
1745 const dex_ir::MethodId *method_id = method->GetMethodId();
1746 dex_ir::CodeItem *code_item = method->GetCodeItem();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001747 if (code_item == nullptr) {
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001748 continue;
1749 }
1750 // Separate executed methods (clinits and profiled methods) from unexecuted methods.
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001751 const bool is_clinit = (method->GetAccessFlags() & kAccConstructor) != 0 &&
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001752 (method->GetAccessFlags() & kAccStatic) != 0;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001753 const bool is_startup_clinit = is_profile_class && is_clinit;
1754 using Hotness = ProfileCompilationInfo::MethodHotness;
1755 Hotness hotness = info_->GetMethodHotness(MethodReference(dex_file, method_id->GetIndex()));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001756 LayoutType state = LayoutType::kLayoutTypeUnused;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001757 if (hotness.IsHot()) {
1758 // Hot code is compiled, maybe one day it won't be accessed. So lay it out together for
1759 // now.
Mathieu Chartier120aa282017-08-05 16:03:03 -07001760 state = LayoutType::kLayoutTypeHot;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001761 } else if (is_startup_clinit || hotness.GetFlags() == Hotness::kFlagStartup) {
1762 // Startup clinit or a method that only has the startup flag.
Mathieu Chartier120aa282017-08-05 16:03:03 -07001763 state = LayoutType::kLayoutTypeStartupOnly;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001764 } else if (is_clinit) {
Mathieu Chartier120aa282017-08-05 16:03:03 -07001765 state = LayoutType::kLayoutTypeUsedOnce;
Mathieu Chartiere46f3a82017-06-19 19:54:12 -07001766 } else if (hotness.IsInProfile()) {
Mathieu Chartier120aa282017-08-05 16:03:03 -07001767 state = LayoutType::kLayoutTypeSometimesUsed;
Jeff Hao206cbaa2017-06-07 19:11:01 -07001768 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001769 auto it = code_item_layout.emplace(code_item, state);
1770 if (!it.second) {
1771 LayoutType& layout_type = it.first->second;
1772 // Already exists, merge the hotness.
1773 layout_type = MergeLayoutType(layout_type, state);
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001774 }
1775 }
1776 }
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001777 }
Jeff Hao042e8982016-10-19 11:17:11 -07001778
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001779 dex_ir::CollectionVector<dex_ir::CodeItem>::Vector& code_items =
1780 header_->GetCollections().CodeItems();
1781 if (VLOG_IS_ON(dex)) {
1782 size_t layout_count[static_cast<size_t>(LayoutType::kLayoutTypeCount)] = {};
1783 for (const std::unique_ptr<dex_ir::CodeItem>& code_item : code_items) {
1784 auto it = code_item_layout.find(code_item.get());
1785 DCHECK(it != code_item_layout.end());
1786 ++layout_count[static_cast<size_t>(it->second)];
1787 }
1788 for (size_t i = 0; i < static_cast<size_t>(LayoutType::kLayoutTypeCount); ++i) {
1789 LOG(INFO) << "Code items in category " << i << " count=" << layout_count[i];
Jeff Haoe17f5892017-02-23 16:14:04 -08001790 }
1791 }
Jeff Hao042e8982016-10-19 11:17:11 -07001792
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001793 // Sort the code items vector by new layout. The writing process will take care of calculating
1794 // all the offsets. Stable sort to preserve any existing locality that might be there.
1795 std::stable_sort(code_items.begin(),
1796 code_items.end(),
1797 [&](const std::unique_ptr<dex_ir::CodeItem>& a,
1798 const std::unique_ptr<dex_ir::CodeItem>& b) {
1799 auto it_a = code_item_layout.find(a.get());
1800 auto it_b = code_item_layout.find(b.get());
1801 DCHECK(it_a != code_item_layout.end());
1802 DCHECK(it_b != code_item_layout.end());
1803 const LayoutType layout_type_a = it_a->second;
1804 const LayoutType layout_type_b = it_b->second;
1805 return layout_type_a < layout_type_b;
1806 });
Jeff Hao042e8982016-10-19 11:17:11 -07001807}
1808
1809void DexLayout::LayoutOutputFile(const DexFile* dex_file) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001810 LayoutStringData(dex_file);
1811 LayoutClassDefsAndClassData(dex_file);
1812 LayoutCodeItems(dex_file);
Jeff Hao042e8982016-10-19 11:17:11 -07001813}
1814
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001815void DexLayout::OutputDexFile(const DexFile* dex_file, bool compute_offsets) {
Jeff Haoec7f1a92017-03-13 16:24:24 -07001816 const std::string& dex_file_location = dex_file->GetLocation();
Jeff Haoea7c6292016-11-14 18:10:16 -08001817 std::string error_msg;
1818 std::unique_ptr<File> new_file;
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001819 // Since we allow dex growth, we need to size the map larger than the original input to be safe.
1820 // Reserve an extra 10% to add some buffer room. Note that this is probably more than
1821 // necessary.
1822 constexpr size_t kReserveFraction = 10;
1823 const size_t max_size = header_->FileSize() + header_->FileSize() / kReserveFraction;
Jeff Haoea7c6292016-11-14 18:10:16 -08001824 if (!options_.output_to_memmap_) {
Jeff Haoa8621002016-10-04 18:13:44 +00001825 std::string output_location(options_.output_dex_directory_);
Andreas Gampe37c58462017-03-27 15:14:27 -07001826 size_t last_slash = dex_file_location.rfind('/');
Jeff Haoea7c6292016-11-14 18:10:16 -08001827 std::string dex_file_directory = dex_file_location.substr(0, last_slash + 1);
1828 if (output_location == dex_file_directory) {
1829 output_location = dex_file_location + ".new";
1830 } else if (last_slash != std::string::npos) {
1831 output_location += dex_file_location.substr(last_slash);
1832 } else {
1833 output_location += "/" + dex_file_location + ".new";
1834 }
1835 new_file.reset(OS::CreateEmptyFile(output_location.c_str()));
Jeff Hao3ba51e82017-04-12 16:14:54 -07001836 if (new_file == nullptr) {
1837 LOG(ERROR) << "Could not create dex writer output file: " << output_location;
1838 return;
1839 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001840 if (ftruncate(new_file->Fd(), max_size) != 0) {
David Sehr7639cdc2017-04-15 10:06:21 -07001841 LOG(ERROR) << "Could not grow dex writer output file: " << output_location;;
1842 new_file->Erase();
1843 return;
1844 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001845 mem_map_.reset(MemMap::MapFile(max_size, PROT_READ | PROT_WRITE, MAP_SHARED,
Jeff Haoea7c6292016-11-14 18:10:16 -08001846 new_file->Fd(), 0, /*low_4gb*/ false, output_location.c_str(), &error_msg));
1847 } else {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001848 mem_map_.reset(MemMap::MapAnonymous("layout dex", nullptr, max_size,
Jeff Haoea7c6292016-11-14 18:10:16 -08001849 PROT_READ | PROT_WRITE, /* low_4gb */ false, /* reuse */ false, &error_msg));
1850 }
1851 if (mem_map_ == nullptr) {
1852 LOG(ERROR) << "Could not create mem map for dex writer output: " << error_msg;
Jeff Hao3ba51e82017-04-12 16:14:54 -07001853 if (new_file != nullptr) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001854 new_file->Erase();
1855 }
1856 return;
1857 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001858 DexWriter::Output(header_, mem_map_.get(), this, compute_offsets, options_.compact_dex_level_);
Jeff Haoea7c6292016-11-14 18:10:16 -08001859 if (new_file != nullptr) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001860 // Since we make the memmap larger than needed, shrink the file back down to not leave extra
1861 // padding.
1862 int res = new_file->SetLength(header_->FileSize());
1863 if (res != 0) {
1864 LOG(ERROR) << "Truncating file resulted in " << res;
1865 }
Jeff Haoea7c6292016-11-14 18:10:16 -08001866 UNUSED(new_file->FlushCloseOrErase());
1867 }
1868}
1869
1870/*
1871 * Dumps the requested sections of the file.
1872 */
1873void DexLayout::ProcessDexFile(const char* file_name,
1874 const DexFile* dex_file,
1875 size_t dex_file_index) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001876 const bool output = options_.output_dex_directory_ != nullptr || options_.output_to_memmap_;
1877 // Try to avoid eagerly assigning offsets to find bugs since GetOffset will abort if the offset
1878 // is unassigned.
1879 bool eagerly_assign_offsets = false;
1880 if (options_.visualize_pattern_ || options_.show_section_statistics_ || options_.dump_) {
1881 // These options required the offsets for dumping purposes.
1882 eagerly_assign_offsets = true;
1883 }
1884 std::unique_ptr<dex_ir::Header> header(dex_ir::DexIrBuilder(*dex_file, eagerly_assign_offsets));
Jeff Haoea7c6292016-11-14 18:10:16 -08001885 SetHeader(header.get());
1886
1887 if (options_.verbose_) {
1888 fprintf(out_file_, "Opened '%s', DEX version '%.3s'\n",
1889 file_name, dex_file->GetHeader().magic_ + 4);
1890 }
1891
1892 if (options_.visualize_pattern_) {
1893 VisualizeDexLayout(header_, dex_file, dex_file_index, info_);
1894 return;
1895 }
1896
David Sehr93357492017-03-09 08:02:44 -08001897 if (options_.show_section_statistics_) {
1898 ShowDexSectionStatistics(header_, dex_file_index);
1899 return;
1900 }
1901
Jeff Haoea7c6292016-11-14 18:10:16 -08001902 // Dump dex file.
1903 if (options_.dump_) {
1904 DumpDexFile();
1905 }
1906
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001907 // In case we are outputting to a file, keep it open so we can verify.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001908 if (output) {
1909 // Layout information about what strings and code items are hot. Used by the writing process
1910 // to generate the sections that are stored in the oat file.
1911 bool do_layout = info_ != nullptr;
1912 if (do_layout) {
Jeff Hao042e8982016-10-19 11:17:11 -07001913 LayoutOutputFile(dex_file);
1914 }
Mathieu Chartier21cf2582018-01-08 17:09:48 -08001915 // If we didn't set the offsets eagerly, we definitely need to compute them here.
1916 OutputDexFile(dex_file, do_layout || !eagerly_assign_offsets);
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001917
1918 // Clear header before verifying to reduce peak RAM usage.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001919 const size_t file_size = header_->FileSize();
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001920 header.reset();
1921
1922 // Verify the output dex file's structure, only enabled by default for debug builds.
1923 if (options_.verify_output_) {
1924 std::string error_msg;
1925 std::string location = "memory mapped file for " + std::string(file_name);
1926 std::unique_ptr<const DexFile> output_dex_file(DexFileLoader::Open(mem_map_->Begin(),
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001927 file_size,
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001928 location,
1929 /* checksum */ 0,
1930 /*oat_dex_file*/ nullptr,
1931 /*verify*/ true,
1932 /*verify_checksum*/ false,
1933 &error_msg));
1934 CHECK(output_dex_file != nullptr) << "Failed to re-open output file:" << error_msg;
1935
1936 // Do IR-level comparison between input and output. This check ignores potential differences
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001937 // due to layout, so offsets are not checked. Instead, it checks the data contents of each
1938 // item.
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001939 //
1940 // Regenerate output IR to catch any bugs that might happen during writing.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001941 std::unique_ptr<dex_ir::Header> output_header(
1942 dex_ir::DexIrBuilder(*output_dex_file,
1943 /*eagerly_assign_offsets*/ true));
1944 std::unique_ptr<dex_ir::Header> orig_header(
1945 dex_ir::DexIrBuilder(*dex_file,
1946 /*eagerly_assign_offsets*/ true));
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001947 CHECK(VerifyOutputDexFile(output_header.get(), orig_header.get(), &error_msg)) << error_msg;
1948 }
Jeff Hao3ab96b42016-09-09 18:35:01 -07001949 }
David Sehr7629f602016-08-07 16:01:51 -07001950}
1951
1952/*
1953 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
1954 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001955int DexLayout::ProcessFile(const char* file_name) {
David Sehr7629f602016-08-07 16:01:51 -07001956 if (options_.verbose_) {
1957 fprintf(out_file_, "Processing '%s'...\n", file_name);
1958 }
1959
1960 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
1961 // all of which are Zip archives with "classes.dex" inside.
1962 const bool verify_checksum = !options_.ignore_bad_checksum_;
1963 std::string error_msg;
1964 std::vector<std::unique_ptr<const DexFile>> dex_files;
Nicolas Geoffray095c6c92017-10-19 13:59:55 +01001965 if (!DexFileLoader::Open(
1966 file_name, file_name, /* verify */ true, verify_checksum, &error_msg, &dex_files)) {
David Sehr7629f602016-08-07 16:01:51 -07001967 // Display returned error message to user. Note that this error behavior
1968 // differs from the error messages shown by the original Dalvik dexdump.
1969 fputs(error_msg.c_str(), stderr);
1970 fputc('\n', stderr);
1971 return -1;
1972 }
1973
1974 // Success. Either report checksum verification or process
1975 // all dex files found in given file.
1976 if (options_.checksum_only_) {
1977 fprintf(out_file_, "Checksum verified\n");
1978 } else {
1979 for (size_t i = 0; i < dex_files.size(); i++) {
David Sehrcdcfde72016-09-26 07:44:04 -07001980 ProcessDexFile(file_name, dex_files[i].get(), i);
David Sehr7629f602016-08-07 16:01:51 -07001981 }
1982 }
1983 return 0;
1984}
1985
1986} // namespace art