blob: c51e50b577abc3f5b72089b2c472142cfd4ac9f1 [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 Sehr013fd802018-01-11 22:55:24 -080037#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080038#include "dex/dex_file-inl.h"
39#include "dex/dex_file_layout.h"
40#include "dex/dex_file_loader.h"
41#include "dex/dex_file_types.h"
42#include "dex/dex_file_verifier.h"
43#include "dex/dex_instruction-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070044#include "dex_ir_builder.h"
Jeff Haoec7f1a92017-03-13 16:24:24 -070045#include "dex_verify.h"
David Sehrcdcfde72016-09-26 07:44:04 -070046#include "dex_visualize.h"
Jeff Haoa8621002016-10-04 18:13:44 +000047#include "dex_writer.h"
Calin Juravle33083d62017-01-18 15:29:12 -080048#include "jit/profile_compilation_info.h"
Jeff Haoea7c6292016-11-14 18:10:16 -080049#include "mem_map.h"
Nicolas Geoffrayfd1a6c22016-10-04 11:01:17 +000050#include "os.h"
David Sehr7629f602016-08-07 16:01:51 -070051#include "utils.h"
52
53namespace art {
54
Andreas Gampe46ee31b2016-12-14 10:11:49 -080055using android::base::StringPrintf;
56
David Sehr7629f602016-08-07 16:01:51 -070057/*
David Sehr7629f602016-08-07 16:01:51 -070058 * Flags for use with createAccessFlagStr().
59 */
60enum AccessFor {
61 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
62};
63const int kNumFlags = 18;
64
65/*
66 * Gets 2 little-endian bytes.
67 */
68static inline uint16_t Get2LE(unsigned char const* src) {
69 return src[0] | (src[1] << 8);
70}
71
72/*
Jeff Haoc3acfc52016-08-29 14:18:26 -070073 * Converts a type descriptor to human-readable "dotted" form. For
74 * example, "Ljava/lang/String;" becomes "java.lang.String", and
75 * "[I" becomes "int[]". Also converts '$' to '.', which means this
76 * form can't be converted back to a descriptor.
77 */
78static std::string DescriptorToDotWrapper(const char* descriptor) {
79 std::string result = DescriptorToDot(descriptor);
80 size_t found = result.find('$');
81 while (found != std::string::npos) {
82 result[found] = '.';
83 found = result.find('$', found);
84 }
85 return result;
86}
87
88/*
David Sehr7629f602016-08-07 16:01:51 -070089 * Converts the class name portion of a type descriptor to human-readable
90 * "dotted" form. For example, "Ljava/lang/String;" becomes "String".
91 */
92static std::string DescriptorClassToDot(const char* str) {
93 std::string descriptor(str);
94 // Reduce to just the class name prefix.
95 size_t last_slash = descriptor.rfind('/');
96 if (last_slash == std::string::npos) {
97 last_slash = 0;
98 }
99 // Start past the '/' or 'L'.
100 last_slash++;
101
102 // Copy class name over, trimming trailing ';'.
103 size_t size = descriptor.size() - 1 - last_slash;
104 std::string result(descriptor.substr(last_slash, size));
105
106 // Replace '$' with '.'.
107 size_t dollar_sign = result.find('$');
108 while (dollar_sign != std::string::npos) {
109 result[dollar_sign] = '.';
110 dollar_sign = result.find('$', dollar_sign);
111 }
112
113 return result;
114}
115
116/*
117 * Returns string representing the boolean value.
118 */
119static const char* StrBool(bool val) {
120 return val ? "true" : "false";
121}
122
123/*
124 * Returns a quoted string representing the boolean value.
125 */
126static const char* QuotedBool(bool val) {
127 return val ? "\"true\"" : "\"false\"";
128}
129
130/*
131 * Returns a quoted string representing the access flags.
132 */
133static const char* QuotedVisibility(uint32_t access_flags) {
134 if (access_flags & kAccPublic) {
135 return "\"public\"";
136 } else if (access_flags & kAccProtected) {
137 return "\"protected\"";
138 } else if (access_flags & kAccPrivate) {
139 return "\"private\"";
140 } else {
141 return "\"package\"";
142 }
143}
144
145/*
146 * Counts the number of '1' bits in a word.
147 */
148static int CountOnes(uint32_t val) {
149 val = val - ((val >> 1) & 0x55555555);
150 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
151 return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
152}
153
154/*
155 * Creates a new string with human-readable access flags.
156 *
157 * In the base language the access_flags fields are type uint16_t; in Dalvik they're uint32_t.
158 */
159static char* CreateAccessFlagStr(uint32_t flags, AccessFor for_what) {
160 static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
161 {
162 "PUBLIC", /* 0x00001 */
163 "PRIVATE", /* 0x00002 */
164 "PROTECTED", /* 0x00004 */
165 "STATIC", /* 0x00008 */
166 "FINAL", /* 0x00010 */
167 "?", /* 0x00020 */
168 "?", /* 0x00040 */
169 "?", /* 0x00080 */
170 "?", /* 0x00100 */
171 "INTERFACE", /* 0x00200 */
172 "ABSTRACT", /* 0x00400 */
173 "?", /* 0x00800 */
174 "SYNTHETIC", /* 0x01000 */
175 "ANNOTATION", /* 0x02000 */
176 "ENUM", /* 0x04000 */
177 "?", /* 0x08000 */
178 "VERIFIED", /* 0x10000 */
179 "OPTIMIZED", /* 0x20000 */
180 }, {
181 "PUBLIC", /* 0x00001 */
182 "PRIVATE", /* 0x00002 */
183 "PROTECTED", /* 0x00004 */
184 "STATIC", /* 0x00008 */
185 "FINAL", /* 0x00010 */
186 "SYNCHRONIZED", /* 0x00020 */
187 "BRIDGE", /* 0x00040 */
188 "VARARGS", /* 0x00080 */
189 "NATIVE", /* 0x00100 */
190 "?", /* 0x00200 */
191 "ABSTRACT", /* 0x00400 */
192 "STRICT", /* 0x00800 */
193 "SYNTHETIC", /* 0x01000 */
194 "?", /* 0x02000 */
195 "?", /* 0x04000 */
196 "MIRANDA", /* 0x08000 */
197 "CONSTRUCTOR", /* 0x10000 */
198 "DECLARED_SYNCHRONIZED", /* 0x20000 */
199 }, {
200 "PUBLIC", /* 0x00001 */
201 "PRIVATE", /* 0x00002 */
202 "PROTECTED", /* 0x00004 */
203 "STATIC", /* 0x00008 */
204 "FINAL", /* 0x00010 */
205 "?", /* 0x00020 */
206 "VOLATILE", /* 0x00040 */
207 "TRANSIENT", /* 0x00080 */
208 "?", /* 0x00100 */
209 "?", /* 0x00200 */
210 "?", /* 0x00400 */
211 "?", /* 0x00800 */
212 "SYNTHETIC", /* 0x01000 */
213 "?", /* 0x02000 */
214 "ENUM", /* 0x04000 */
215 "?", /* 0x08000 */
216 "?", /* 0x10000 */
217 "?", /* 0x20000 */
218 },
219 };
220
221 // Allocate enough storage to hold the expected number of strings,
222 // plus a space between each. We over-allocate, using the longest
223 // string above as the base metric.
224 const int kLongest = 21; // The strlen of longest string above.
225 const int count = CountOnes(flags);
226 char* str;
227 char* cp;
228 cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
229
230 for (int i = 0; i < kNumFlags; i++) {
231 if (flags & 0x01) {
232 const char* accessStr = kAccessStrings[for_what][i];
233 const int len = strlen(accessStr);
234 if (cp != str) {
235 *cp++ = ' ';
236 }
237 memcpy(cp, accessStr, len);
238 cp += len;
239 }
240 flags >>= 1;
241 } // for
242
243 *cp = '\0';
244 return str;
245}
246
247static std::string GetSignatureForProtoId(const dex_ir::ProtoId* proto) {
248 if (proto == nullptr) {
249 return "<no signature>";
250 }
251
David Sehr7629f602016-08-07 16:01:51 -0700252 std::string result("(");
Jeff Haoa8621002016-10-04 18:13:44 +0000253 const dex_ir::TypeList* type_list = proto->Parameters();
254 if (type_list != nullptr) {
255 for (const dex_ir::TypeId* type_id : *type_list->GetTypeList()) {
256 result += type_id->GetStringId()->Data();
257 }
David Sehr7629f602016-08-07 16:01:51 -0700258 }
259 result += ")";
260 result += proto->ReturnType()->GetStringId()->Data();
261 return result;
262}
263
264/*
265 * Copies character data from "data" to "out", converting non-ASCII values
266 * to fprintf format chars or an ASCII filler ('.' or '?').
267 *
268 * The output buffer must be able to hold (2*len)+1 bytes. The result is
269 * NULL-terminated.
270 */
271static void Asciify(char* out, const unsigned char* data, size_t len) {
272 while (len--) {
273 if (*data < 0x20) {
274 // Could do more here, but we don't need them yet.
275 switch (*data) {
276 case '\0':
277 *out++ = '\\';
278 *out++ = '0';
279 break;
280 case '\n':
281 *out++ = '\\';
282 *out++ = 'n';
283 break;
284 default:
285 *out++ = '.';
286 break;
287 } // switch
288 } else if (*data >= 0x80) {
289 *out++ = '?';
290 } else {
291 *out++ = *data;
292 }
293 data++;
294 } // while
295 *out = '\0';
296}
297
298/*
299 * Dumps a string value with some escape characters.
300 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800301static void DumpEscapedString(const char* p, FILE* out_file) {
302 fputs("\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700303 for (; *p; p++) {
304 switch (*p) {
305 case '\\':
Jeff Haoea7c6292016-11-14 18:10:16 -0800306 fputs("\\\\", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700307 break;
308 case '\"':
Jeff Haoea7c6292016-11-14 18:10:16 -0800309 fputs("\\\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700310 break;
311 case '\t':
Jeff Haoea7c6292016-11-14 18:10:16 -0800312 fputs("\\t", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700313 break;
314 case '\n':
Jeff Haoea7c6292016-11-14 18:10:16 -0800315 fputs("\\n", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700316 break;
317 case '\r':
Jeff Haoea7c6292016-11-14 18:10:16 -0800318 fputs("\\r", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700319 break;
320 default:
Jeff Haoea7c6292016-11-14 18:10:16 -0800321 putc(*p, out_file);
David Sehr7629f602016-08-07 16:01:51 -0700322 } // switch
323 } // for
Jeff Haoea7c6292016-11-14 18:10:16 -0800324 fputs("\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700325}
326
327/*
328 * Dumps a string as an XML attribute value.
329 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800330static void DumpXmlAttribute(const char* p, FILE* out_file) {
David Sehr7629f602016-08-07 16:01:51 -0700331 for (; *p; p++) {
332 switch (*p) {
333 case '&':
Jeff Haoea7c6292016-11-14 18:10:16 -0800334 fputs("&amp;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700335 break;
336 case '<':
Jeff Haoea7c6292016-11-14 18:10:16 -0800337 fputs("&lt;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700338 break;
339 case '>':
Jeff Haoea7c6292016-11-14 18:10:16 -0800340 fputs("&gt;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700341 break;
342 case '"':
Jeff Haoea7c6292016-11-14 18:10:16 -0800343 fputs("&quot;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700344 break;
345 case '\t':
Jeff Haoea7c6292016-11-14 18:10:16 -0800346 fputs("&#x9;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700347 break;
348 case '\n':
Jeff Haoea7c6292016-11-14 18:10:16 -0800349 fputs("&#xA;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700350 break;
351 case '\r':
Jeff Haoea7c6292016-11-14 18:10:16 -0800352 fputs("&#xD;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700353 break;
354 default:
Jeff Haoea7c6292016-11-14 18:10:16 -0800355 putc(*p, out_file);
David Sehr7629f602016-08-07 16:01:51 -0700356 } // switch
357 } // for
358}
359
David Sehr7629f602016-08-07 16:01:51 -0700360/*
361 * Helper for dumpInstruction(), which builds the string
362 * representation for the index in the given instruction.
363 * Returns a pointer to a buffer of sufficient size.
364 */
365static std::unique_ptr<char[]> IndexString(dex_ir::Header* header,
366 const Instruction* dec_insn,
367 size_t buf_size) {
368 std::unique_ptr<char[]> buf(new char[buf_size]);
369 // Determine index and width of the string.
370 uint32_t index = 0;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700371 uint32_t secondary_index = dex::kDexNoIndex;
David Sehr7629f602016-08-07 16:01:51 -0700372 uint32_t width = 4;
373 switch (Instruction::FormatOf(dec_insn->Opcode())) {
374 // SOME NOT SUPPORTED:
375 // case Instruction::k20bc:
376 case Instruction::k21c:
377 case Instruction::k35c:
378 // case Instruction::k35ms:
379 case Instruction::k3rc:
380 // case Instruction::k3rms:
381 // case Instruction::k35mi:
382 // case Instruction::k3rmi:
383 index = dec_insn->VRegB();
384 width = 4;
385 break;
386 case Instruction::k31c:
387 index = dec_insn->VRegB();
388 width = 8;
389 break;
390 case Instruction::k22c:
391 // case Instruction::k22cs:
392 index = dec_insn->VRegC();
393 width = 4;
394 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100395 case Instruction::k45cc:
396 case Instruction::k4rcc:
397 index = dec_insn->VRegB();
398 secondary_index = dec_insn->VRegH();
399 width = 4;
David Sehr7639cdc2017-04-15 10:06:21 -0700400 break;
David Sehr7629f602016-08-07 16:01:51 -0700401 default:
402 break;
403 } // switch
404
405 // Determine index type.
406 size_t outSize = 0;
407 switch (Instruction::IndexTypeOf(dec_insn->Opcode())) {
408 case Instruction::kIndexUnknown:
409 // This function should never get called for this type, but do
410 // something sensible here, just to help with debugging.
411 outSize = snprintf(buf.get(), buf_size, "<unknown-index>");
412 break;
413 case Instruction::kIndexNone:
414 // This function should never get called for this type, but do
415 // something sensible here, just to help with debugging.
416 outSize = snprintf(buf.get(), buf_size, "<no-index>");
417 break;
418 case Instruction::kIndexTypeRef:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700419 if (index < header->GetCollections().TypeIdsSize()) {
420 const char* tp = header->GetCollections().GetTypeId(index)->GetStringId()->Data();
David Sehr7629f602016-08-07 16:01:51 -0700421 outSize = snprintf(buf.get(), buf_size, "%s // type@%0*x", tp, width, index);
422 } else {
423 outSize = snprintf(buf.get(), buf_size, "<type?> // type@%0*x", width, index);
424 }
425 break;
426 case Instruction::kIndexStringRef:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700427 if (index < header->GetCollections().StringIdsSize()) {
428 const char* st = header->GetCollections().GetStringId(index)->Data();
David Sehr7629f602016-08-07 16:01:51 -0700429 outSize = snprintf(buf.get(), buf_size, "\"%s\" // string@%0*x", st, width, index);
430 } else {
431 outSize = snprintf(buf.get(), buf_size, "<string?> // string@%0*x", width, index);
432 }
433 break;
434 case Instruction::kIndexMethodRef:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700435 if (index < header->GetCollections().MethodIdsSize()) {
436 dex_ir::MethodId* method_id = header->GetCollections().GetMethodId(index);
David Sehr7629f602016-08-07 16:01:51 -0700437 const char* name = method_id->Name()->Data();
David Sehr72359222016-09-07 13:04:01 -0700438 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -0700439 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
440 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // method@%0*x",
David Sehr72359222016-09-07 13:04:01 -0700441 back_descriptor, name, type_descriptor.c_str(), width, index);
David Sehr7629f602016-08-07 16:01:51 -0700442 } else {
443 outSize = snprintf(buf.get(), buf_size, "<method?> // method@%0*x", width, index);
444 }
445 break;
446 case Instruction::kIndexFieldRef:
Jeff Hao3ab96b42016-09-09 18:35:01 -0700447 if (index < header->GetCollections().FieldIdsSize()) {
448 dex_ir::FieldId* field_id = header->GetCollections().GetFieldId(index);
David Sehr7629f602016-08-07 16:01:51 -0700449 const char* name = field_id->Name()->Data();
450 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
451 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
452 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // field@%0*x",
453 back_descriptor, name, type_descriptor, width, index);
454 } else {
455 outSize = snprintf(buf.get(), buf_size, "<field?> // field@%0*x", width, index);
456 }
457 break;
458 case Instruction::kIndexVtableOffset:
459 outSize = snprintf(buf.get(), buf_size, "[%0*x] // vtable #%0*x",
460 width, index, width, index);
461 break;
462 case Instruction::kIndexFieldOffset:
463 outSize = snprintf(buf.get(), buf_size, "[obj+%0*x]", width, index);
464 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100465 case Instruction::kIndexMethodAndProtoRef: {
466 std::string method("<method?>");
467 std::string proto("<proto?>");
468 if (index < header->GetCollections().MethodIdsSize()) {
469 dex_ir::MethodId* method_id = header->GetCollections().GetMethodId(index);
470 const char* name = method_id->Name()->Data();
471 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
472 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
473 method = StringPrintf("%s.%s:%s", back_descriptor, name, type_descriptor.c_str());
474 }
475 if (secondary_index < header->GetCollections().ProtoIdsSize()) {
476 dex_ir::ProtoId* proto_id = header->GetCollections().GetProtoId(secondary_index);
477 proto = GetSignatureForProtoId(proto_id);
478 }
479 outSize = snprintf(buf.get(), buf_size, "%s, %s // method@%0*x, proto@%0*x",
480 method.c_str(), proto.c_str(), width, index, width, secondary_index);
Jeff Haoea7c6292016-11-14 18:10:16 -0800481 }
482 break;
483 // SOME NOT SUPPORTED:
484 // case Instruction::kIndexVaries:
485 // case Instruction::kIndexInlineMethod:
David Sehr7629f602016-08-07 16:01:51 -0700486 default:
487 outSize = snprintf(buf.get(), buf_size, "<?>");
488 break;
489 } // switch
490
491 // Determine success of string construction.
492 if (outSize >= buf_size) {
493 // The buffer wasn't big enough; retry with computed size. Note: snprintf()
494 // doesn't count/ the '\0' as part of its returned size, so we add explicit
495 // space for it here.
496 return IndexString(header, dec_insn, outSize + 1);
497 }
498 return buf;
499}
500
501/*
Jeff Haoea7c6292016-11-14 18:10:16 -0800502 * Dumps encoded annotation.
503 */
504void DexLayout::DumpEncodedAnnotation(dex_ir::EncodedAnnotation* annotation) {
505 fputs(annotation->GetType()->GetStringId()->Data(), out_file_);
506 // Display all name=value pairs.
507 for (auto& subannotation : *annotation->GetAnnotationElements()) {
508 fputc(' ', out_file_);
509 fputs(subannotation->GetName()->Data(), out_file_);
510 fputc('=', out_file_);
511 DumpEncodedValue(subannotation->GetValue());
512 }
513}
514/*
515 * Dumps encoded value.
516 */
517void DexLayout::DumpEncodedValue(const dex_ir::EncodedValue* data) {
518 switch (data->Type()) {
519 case DexFile::kDexAnnotationByte:
520 fprintf(out_file_, "%" PRId8, data->GetByte());
521 break;
522 case DexFile::kDexAnnotationShort:
523 fprintf(out_file_, "%" PRId16, data->GetShort());
524 break;
525 case DexFile::kDexAnnotationChar:
526 fprintf(out_file_, "%" PRIu16, data->GetChar());
527 break;
528 case DexFile::kDexAnnotationInt:
529 fprintf(out_file_, "%" PRId32, data->GetInt());
530 break;
531 case DexFile::kDexAnnotationLong:
532 fprintf(out_file_, "%" PRId64, data->GetLong());
533 break;
534 case DexFile::kDexAnnotationFloat: {
535 fprintf(out_file_, "%g", data->GetFloat());
536 break;
537 }
538 case DexFile::kDexAnnotationDouble: {
539 fprintf(out_file_, "%g", data->GetDouble());
540 break;
541 }
542 case DexFile::kDexAnnotationString: {
543 dex_ir::StringId* string_id = data->GetStringId();
544 if (options_.output_format_ == kOutputPlain) {
545 DumpEscapedString(string_id->Data(), out_file_);
546 } else {
547 DumpXmlAttribute(string_id->Data(), out_file_);
548 }
549 break;
550 }
551 case DexFile::kDexAnnotationType: {
552 dex_ir::TypeId* type_id = data->GetTypeId();
553 fputs(type_id->GetStringId()->Data(), out_file_);
554 break;
555 }
556 case DexFile::kDexAnnotationField:
557 case DexFile::kDexAnnotationEnum: {
558 dex_ir::FieldId* field_id = data->GetFieldId();
559 fputs(field_id->Name()->Data(), out_file_);
560 break;
561 }
562 case DexFile::kDexAnnotationMethod: {
563 dex_ir::MethodId* method_id = data->GetMethodId();
564 fputs(method_id->Name()->Data(), out_file_);
565 break;
566 }
567 case DexFile::kDexAnnotationArray: {
568 fputc('{', out_file_);
569 // Display all elements.
570 for (auto& value : *data->GetEncodedArray()->GetEncodedValues()) {
571 fputc(' ', out_file_);
572 DumpEncodedValue(value.get());
573 }
574 fputs(" }", out_file_);
575 break;
576 }
577 case DexFile::kDexAnnotationAnnotation: {
578 DumpEncodedAnnotation(data->GetEncodedAnnotation());
579 break;
580 }
581 case DexFile::kDexAnnotationNull:
582 fputs("null", out_file_);
583 break;
584 case DexFile::kDexAnnotationBoolean:
585 fputs(StrBool(data->GetBoolean()), out_file_);
586 break;
587 default:
588 fputs("????", out_file_);
589 break;
590 } // switch
591}
592
593/*
594 * Dumps the file header.
595 */
596void DexLayout::DumpFileHeader() {
597 char sanitized[8 * 2 + 1];
598 dex_ir::Collections& collections = header_->GetCollections();
599 fprintf(out_file_, "DEX file header:\n");
600 Asciify(sanitized, header_->Magic(), 8);
601 fprintf(out_file_, "magic : '%s'\n", sanitized);
602 fprintf(out_file_, "checksum : %08x\n", header_->Checksum());
603 fprintf(out_file_, "signature : %02x%02x...%02x%02x\n",
604 header_->Signature()[0], header_->Signature()[1],
605 header_->Signature()[DexFile::kSha1DigestSize - 2],
606 header_->Signature()[DexFile::kSha1DigestSize - 1]);
607 fprintf(out_file_, "file_size : %d\n", header_->FileSize());
608 fprintf(out_file_, "header_size : %d\n", header_->HeaderSize());
609 fprintf(out_file_, "link_size : %d\n", header_->LinkSize());
610 fprintf(out_file_, "link_off : %d (0x%06x)\n",
611 header_->LinkOffset(), header_->LinkOffset());
612 fprintf(out_file_, "string_ids_size : %d\n", collections.StringIdsSize());
613 fprintf(out_file_, "string_ids_off : %d (0x%06x)\n",
614 collections.StringIdsOffset(), collections.StringIdsOffset());
615 fprintf(out_file_, "type_ids_size : %d\n", collections.TypeIdsSize());
616 fprintf(out_file_, "type_ids_off : %d (0x%06x)\n",
617 collections.TypeIdsOffset(), collections.TypeIdsOffset());
618 fprintf(out_file_, "proto_ids_size : %d\n", collections.ProtoIdsSize());
619 fprintf(out_file_, "proto_ids_off : %d (0x%06x)\n",
620 collections.ProtoIdsOffset(), collections.ProtoIdsOffset());
621 fprintf(out_file_, "field_ids_size : %d\n", collections.FieldIdsSize());
622 fprintf(out_file_, "field_ids_off : %d (0x%06x)\n",
623 collections.FieldIdsOffset(), collections.FieldIdsOffset());
624 fprintf(out_file_, "method_ids_size : %d\n", collections.MethodIdsSize());
625 fprintf(out_file_, "method_ids_off : %d (0x%06x)\n",
626 collections.MethodIdsOffset(), collections.MethodIdsOffset());
627 fprintf(out_file_, "class_defs_size : %d\n", collections.ClassDefsSize());
628 fprintf(out_file_, "class_defs_off : %d (0x%06x)\n",
629 collections.ClassDefsOffset(), collections.ClassDefsOffset());
630 fprintf(out_file_, "data_size : %d\n", header_->DataSize());
631 fprintf(out_file_, "data_off : %d (0x%06x)\n\n",
632 header_->DataOffset(), header_->DataOffset());
633}
634
635/*
636 * Dumps a class_def_item.
637 */
638void DexLayout::DumpClassDef(int idx) {
639 // General class information.
640 dex_ir::ClassDef* class_def = header_->GetCollections().GetClassDef(idx);
641 fprintf(out_file_, "Class #%d header:\n", idx);
642 fprintf(out_file_, "class_idx : %d\n", class_def->ClassType()->GetIndex());
643 fprintf(out_file_, "access_flags : %d (0x%04x)\n",
644 class_def->GetAccessFlags(), class_def->GetAccessFlags());
645 uint32_t superclass_idx = class_def->Superclass() == nullptr ?
646 DexFile::kDexNoIndex16 : class_def->Superclass()->GetIndex();
647 fprintf(out_file_, "superclass_idx : %d\n", superclass_idx);
648 fprintf(out_file_, "interfaces_off : %d (0x%06x)\n",
649 class_def->InterfacesOffset(), class_def->InterfacesOffset());
650 uint32_t source_file_offset = 0xffffffffU;
651 if (class_def->SourceFile() != nullptr) {
652 source_file_offset = class_def->SourceFile()->GetIndex();
653 }
654 fprintf(out_file_, "source_file_idx : %d\n", source_file_offset);
655 uint32_t annotations_offset = 0;
656 if (class_def->Annotations() != nullptr) {
657 annotations_offset = class_def->Annotations()->GetOffset();
658 }
659 fprintf(out_file_, "annotations_off : %d (0x%06x)\n",
660 annotations_offset, annotations_offset);
661 if (class_def->GetClassData() == nullptr) {
662 fprintf(out_file_, "class_data_off : %d (0x%06x)\n", 0, 0);
663 } else {
664 fprintf(out_file_, "class_data_off : %d (0x%06x)\n",
665 class_def->GetClassData()->GetOffset(), class_def->GetClassData()->GetOffset());
666 }
667
668 // Fields and methods.
669 dex_ir::ClassData* class_data = class_def->GetClassData();
670 if (class_data != nullptr && class_data->StaticFields() != nullptr) {
671 fprintf(out_file_, "static_fields_size : %zu\n", class_data->StaticFields()->size());
672 } else {
673 fprintf(out_file_, "static_fields_size : 0\n");
674 }
675 if (class_data != nullptr && class_data->InstanceFields() != nullptr) {
676 fprintf(out_file_, "instance_fields_size: %zu\n", class_data->InstanceFields()->size());
677 } else {
678 fprintf(out_file_, "instance_fields_size: 0\n");
679 }
680 if (class_data != nullptr && class_data->DirectMethods() != nullptr) {
681 fprintf(out_file_, "direct_methods_size : %zu\n", class_data->DirectMethods()->size());
682 } else {
683 fprintf(out_file_, "direct_methods_size : 0\n");
684 }
685 if (class_data != nullptr && class_data->VirtualMethods() != nullptr) {
686 fprintf(out_file_, "virtual_methods_size: %zu\n", class_data->VirtualMethods()->size());
687 } else {
688 fprintf(out_file_, "virtual_methods_size: 0\n");
689 }
690 fprintf(out_file_, "\n");
691}
692
693/**
694 * Dumps an annotation set item.
695 */
696void DexLayout::DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item) {
697 if (set_item == nullptr || set_item->GetItems()->size() == 0) {
698 fputs(" empty-annotation-set\n", out_file_);
699 return;
700 }
701 for (dex_ir::AnnotationItem* annotation : *set_item->GetItems()) {
702 if (annotation == nullptr) {
703 continue;
704 }
705 fputs(" ", out_file_);
706 switch (annotation->GetVisibility()) {
707 case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", out_file_); break;
708 case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", out_file_); break;
709 case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", out_file_); break;
710 default: fputs("VISIBILITY_UNKNOWN ", out_file_); break;
711 } // switch
712 DumpEncodedAnnotation(annotation->GetAnnotation());
713 fputc('\n', out_file_);
714 }
715}
716
717/*
718 * Dumps class annotations.
719 */
720void DexLayout::DumpClassAnnotations(int idx) {
721 dex_ir::ClassDef* class_def = header_->GetCollections().GetClassDef(idx);
722 dex_ir::AnnotationsDirectoryItem* annotations_directory = class_def->Annotations();
723 if (annotations_directory == nullptr) {
724 return; // none
725 }
726
727 fprintf(out_file_, "Class #%d annotations:\n", idx);
728
729 dex_ir::AnnotationSetItem* class_set_item = annotations_directory->GetClassAnnotation();
730 dex_ir::FieldAnnotationVector* fields = annotations_directory->GetFieldAnnotations();
731 dex_ir::MethodAnnotationVector* methods = annotations_directory->GetMethodAnnotations();
732 dex_ir::ParameterAnnotationVector* parameters = annotations_directory->GetParameterAnnotations();
733
734 // Annotations on the class itself.
735 if (class_set_item != nullptr) {
736 fprintf(out_file_, "Annotations on class\n");
737 DumpAnnotationSetItem(class_set_item);
738 }
739
740 // Annotations on fields.
741 if (fields != nullptr) {
742 for (auto& field : *fields) {
743 const dex_ir::FieldId* field_id = field->GetFieldId();
744 const uint32_t field_idx = field_id->GetIndex();
745 const char* field_name = field_id->Name()->Data();
746 fprintf(out_file_, "Annotations on field #%u '%s'\n", field_idx, field_name);
747 DumpAnnotationSetItem(field->GetAnnotationSetItem());
748 }
749 }
750
751 // Annotations on methods.
752 if (methods != nullptr) {
753 for (auto& method : *methods) {
754 const dex_ir::MethodId* method_id = method->GetMethodId();
755 const uint32_t method_idx = method_id->GetIndex();
756 const char* method_name = method_id->Name()->Data();
757 fprintf(out_file_, "Annotations on method #%u '%s'\n", method_idx, method_name);
758 DumpAnnotationSetItem(method->GetAnnotationSetItem());
759 }
760 }
761
762 // Annotations on method parameters.
763 if (parameters != nullptr) {
764 for (auto& parameter : *parameters) {
765 const dex_ir::MethodId* method_id = parameter->GetMethodId();
766 const uint32_t method_idx = method_id->GetIndex();
767 const char* method_name = method_id->Name()->Data();
768 fprintf(out_file_, "Annotations on method #%u '%s' parameters\n", method_idx, method_name);
769 uint32_t j = 0;
770 for (dex_ir::AnnotationSetItem* annotation : *parameter->GetAnnotations()->GetItems()) {
771 fprintf(out_file_, "#%u\n", j);
772 DumpAnnotationSetItem(annotation);
773 ++j;
774 }
775 }
776 }
777
778 fputc('\n', out_file_);
779}
780
781/*
782 * Dumps an interface that a class declares to implement.
783 */
784void DexLayout::DumpInterface(const dex_ir::TypeId* type_item, int i) {
785 const char* interface_name = type_item->GetStringId()->Data();
786 if (options_.output_format_ == kOutputPlain) {
787 fprintf(out_file_, " #%d : '%s'\n", i, interface_name);
788 } else {
789 std::string dot(DescriptorToDotWrapper(interface_name));
790 fprintf(out_file_, "<implements name=\"%s\">\n</implements>\n", dot.c_str());
791 }
792}
793
794/*
795 * Dumps the catches table associated with the code.
796 */
797void DexLayout::DumpCatches(const dex_ir::CodeItem* code) {
798 const uint16_t tries_size = code->TriesSize();
799
800 // No catch table.
801 if (tries_size == 0) {
802 fprintf(out_file_, " catches : (none)\n");
803 return;
804 }
805
806 // Dump all table entries.
807 fprintf(out_file_, " catches : %d\n", tries_size);
808 std::vector<std::unique_ptr<const dex_ir::TryItem>>* tries = code->Tries();
809 for (uint32_t i = 0; i < tries_size; i++) {
810 const dex_ir::TryItem* try_item = (*tries)[i].get();
811 const uint32_t start = try_item->StartAddr();
812 const uint32_t end = start + try_item->InsnCount();
813 fprintf(out_file_, " 0x%04x - 0x%04x\n", start, end);
814 for (auto& handler : *try_item->GetHandlers()->GetHandlers()) {
815 const dex_ir::TypeId* type_id = handler->GetTypeId();
816 const char* descriptor = (type_id == nullptr) ? "<any>" : type_id->GetStringId()->Data();
817 fprintf(out_file_, " %s -> 0x%04x\n", descriptor, handler->GetAddress());
818 } // for
819 } // for
820}
821
822/*
David Sehr7629f602016-08-07 16:01:51 -0700823 * Dumps a single instruction.
824 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800825void DexLayout::DumpInstruction(const dex_ir::CodeItem* code,
826 uint32_t code_offset,
827 uint32_t insn_idx,
828 uint32_t insn_width,
829 const Instruction* dec_insn) {
David Sehr7629f602016-08-07 16:01:51 -0700830 // Address of instruction (expressed as byte offset).
831 fprintf(out_file_, "%06x:", code_offset + 0x10 + insn_idx * 2);
832
833 // Dump (part of) raw bytes.
834 const uint16_t* insns = code->Insns();
835 for (uint32_t i = 0; i < 8; i++) {
836 if (i < insn_width) {
837 if (i == 7) {
838 fprintf(out_file_, " ... ");
839 } else {
840 // Print 16-bit value in little-endian order.
841 const uint8_t* bytePtr = (const uint8_t*) &insns[insn_idx + i];
842 fprintf(out_file_, " %02x%02x", bytePtr[0], bytePtr[1]);
843 }
844 } else {
845 fputs(" ", out_file_);
846 }
847 } // for
848
849 // Dump pseudo-instruction or opcode.
850 if (dec_insn->Opcode() == Instruction::NOP) {
851 const uint16_t instr = Get2LE((const uint8_t*) &insns[insn_idx]);
852 if (instr == Instruction::kPackedSwitchSignature) {
853 fprintf(out_file_, "|%04x: packed-switch-data (%d units)", insn_idx, insn_width);
854 } else if (instr == Instruction::kSparseSwitchSignature) {
855 fprintf(out_file_, "|%04x: sparse-switch-data (%d units)", insn_idx, insn_width);
856 } else if (instr == Instruction::kArrayDataSignature) {
857 fprintf(out_file_, "|%04x: array-data (%d units)", insn_idx, insn_width);
858 } else {
859 fprintf(out_file_, "|%04x: nop // spacer", insn_idx);
860 }
861 } else {
862 fprintf(out_file_, "|%04x: %s", insn_idx, dec_insn->Name());
863 }
864
865 // Set up additional argument.
866 std::unique_ptr<char[]> index_buf;
867 if (Instruction::IndexTypeOf(dec_insn->Opcode()) != Instruction::kIndexNone) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800868 index_buf = IndexString(header_, dec_insn, 200);
David Sehr7629f602016-08-07 16:01:51 -0700869 }
870
871 // Dump the instruction.
872 //
873 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
874 //
875 switch (Instruction::FormatOf(dec_insn->Opcode())) {
876 case Instruction::k10x: // op
877 break;
878 case Instruction::k12x: // op vA, vB
879 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
880 break;
881 case Instruction::k11n: // op vA, #+B
882 fprintf(out_file_, " v%d, #int %d // #%x",
883 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint8_t)dec_insn->VRegB());
884 break;
885 case Instruction::k11x: // op vAA
886 fprintf(out_file_, " v%d", dec_insn->VRegA());
887 break;
888 case Instruction::k10t: // op +AA
889 case Instruction::k20t: { // op +AAAA
890 const int32_t targ = (int32_t) dec_insn->VRegA();
891 fprintf(out_file_, " %04x // %c%04x",
892 insn_idx + targ,
893 (targ < 0) ? '-' : '+',
894 (targ < 0) ? -targ : targ);
895 break;
896 }
897 case Instruction::k22x: // op vAA, vBBBB
898 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
899 break;
900 case Instruction::k21t: { // op vAA, +BBBB
901 const int32_t targ = (int32_t) dec_insn->VRegB();
902 fprintf(out_file_, " v%d, %04x // %c%04x", dec_insn->VRegA(),
903 insn_idx + targ,
904 (targ < 0) ? '-' : '+',
905 (targ < 0) ? -targ : targ);
906 break;
907 }
908 case Instruction::k21s: // op vAA, #+BBBB
909 fprintf(out_file_, " v%d, #int %d // #%x",
910 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint16_t)dec_insn->VRegB());
911 break;
912 case Instruction::k21h: // op vAA, #+BBBB0000[00000000]
913 // The printed format varies a bit based on the actual opcode.
914 if (dec_insn->Opcode() == Instruction::CONST_HIGH16) {
915 const int32_t value = dec_insn->VRegB() << 16;
916 fprintf(out_file_, " v%d, #int %d // #%x",
917 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
918 } else {
919 const int64_t value = ((int64_t) dec_insn->VRegB()) << 48;
920 fprintf(out_file_, " v%d, #long %" PRId64 " // #%x",
921 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
922 }
923 break;
924 case Instruction::k21c: // op vAA, thing@BBBB
925 case Instruction::k31c: // op vAA, thing@BBBBBBBB
926 fprintf(out_file_, " v%d, %s", dec_insn->VRegA(), index_buf.get());
927 break;
928 case Instruction::k23x: // op vAA, vBB, vCC
929 fprintf(out_file_, " v%d, v%d, v%d",
930 dec_insn->VRegA(), dec_insn->VRegB(), dec_insn->VRegC());
931 break;
932 case Instruction::k22b: // op vAA, vBB, #+CC
933 fprintf(out_file_, " v%d, v%d, #int %d // #%02x",
934 dec_insn->VRegA(), dec_insn->VRegB(),
935 (int32_t) dec_insn->VRegC(), (uint8_t) dec_insn->VRegC());
936 break;
937 case Instruction::k22t: { // op vA, vB, +CCCC
938 const int32_t targ = (int32_t) dec_insn->VRegC();
939 fprintf(out_file_, " v%d, v%d, %04x // %c%04x",
940 dec_insn->VRegA(), dec_insn->VRegB(),
941 insn_idx + targ,
942 (targ < 0) ? '-' : '+',
943 (targ < 0) ? -targ : targ);
944 break;
945 }
946 case Instruction::k22s: // op vA, vB, #+CCCC
947 fprintf(out_file_, " v%d, v%d, #int %d // #%04x",
948 dec_insn->VRegA(), dec_insn->VRegB(),
949 (int32_t) dec_insn->VRegC(), (uint16_t) dec_insn->VRegC());
950 break;
951 case Instruction::k22c: // op vA, vB, thing@CCCC
952 // NOT SUPPORTED:
953 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
954 fprintf(out_file_, " v%d, v%d, %s",
955 dec_insn->VRegA(), dec_insn->VRegB(), index_buf.get());
956 break;
957 case Instruction::k30t:
958 fprintf(out_file_, " #%08x", dec_insn->VRegA());
959 break;
960 case Instruction::k31i: { // op vAA, #+BBBBBBBB
961 // This is often, but not always, a float.
962 union {
963 float f;
964 uint32_t i;
965 } conv;
966 conv.i = dec_insn->VRegB();
967 fprintf(out_file_, " v%d, #float %g // #%08x",
968 dec_insn->VRegA(), conv.f, dec_insn->VRegB());
969 break;
970 }
971 case Instruction::k31t: // op vAA, offset +BBBBBBBB
972 fprintf(out_file_, " v%d, %08x // +%08x",
973 dec_insn->VRegA(), insn_idx + dec_insn->VRegB(), dec_insn->VRegB());
974 break;
975 case Instruction::k32x: // op vAAAA, vBBBB
976 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
977 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100978 case Instruction::k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
979 case Instruction::k45cc: { // op {vC, vD, vE, vF, vG}, meth@BBBB, proto@HHHH
David Sehr7629f602016-08-07 16:01:51 -0700980 // NOT SUPPORTED:
981 // case Instruction::k35ms: // [opt] invoke-virtual+super
982 // case Instruction::k35mi: // [opt] inline invoke
983 uint32_t arg[Instruction::kMaxVarArgRegs];
984 dec_insn->GetVarArgs(arg);
985 fputs(" {", out_file_);
986 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
987 if (i == 0) {
988 fprintf(out_file_, "v%d", arg[i]);
989 } else {
990 fprintf(out_file_, ", v%d", arg[i]);
991 }
992 } // for
993 fprintf(out_file_, "}, %s", index_buf.get());
994 break;
995 }
Orion Hodsonb34bb192016-10-18 17:02:58 +0100996 case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
997 case Instruction::k4rcc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB, proto@HHHH
David Sehr7629f602016-08-07 16:01:51 -0700998 // NOT SUPPORTED:
999 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
1000 // case Instruction::k3rmi: // [opt] execute-inline/range
1001 {
1002 // This doesn't match the "dx" output when some of the args are
1003 // 64-bit values -- dx only shows the first register.
1004 fputs(" {", out_file_);
1005 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
1006 if (i == 0) {
1007 fprintf(out_file_, "v%d", dec_insn->VRegC() + i);
1008 } else {
1009 fprintf(out_file_, ", v%d", dec_insn->VRegC() + i);
1010 }
1011 } // for
1012 fprintf(out_file_, "}, %s", index_buf.get());
1013 }
1014 break;
1015 case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB
1016 // This is often, but not always, a double.
1017 union {
1018 double d;
1019 uint64_t j;
1020 } conv;
1021 conv.j = dec_insn->WideVRegB();
1022 fprintf(out_file_, " v%d, #double %g // #%016" PRIx64,
1023 dec_insn->VRegA(), conv.d, dec_insn->WideVRegB());
1024 break;
1025 }
1026 // NOT SUPPORTED:
1027 // case Instruction::k00x: // unknown op or breakpoint
1028 // break;
1029 default:
1030 fprintf(out_file_, " ???");
1031 break;
1032 } // switch
1033
1034 fputc('\n', out_file_);
1035}
1036
1037/*
1038 * Dumps a bytecode disassembly.
1039 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001040void DexLayout::DumpBytecodes(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset) {
1041 dex_ir::MethodId* method_id = header_->GetCollections().GetMethodId(idx);
David Sehr7629f602016-08-07 16:01:51 -07001042 const char* name = method_id->Name()->Data();
David Sehr72359222016-09-07 13:04:01 -07001043 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -07001044 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1045
1046 // Generate header.
Jeff Haoc3acfc52016-08-29 14:18:26 -07001047 std::string dot(DescriptorToDotWrapper(back_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001048 fprintf(out_file_, "%06x: |[%06x] %s.%s:%s\n",
David Sehr72359222016-09-07 13:04:01 -07001049 code_offset, code_offset, dot.c_str(), name, type_descriptor.c_str());
David Sehr7629f602016-08-07 16:01:51 -07001050
1051 // Iterate over all instructions.
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001052 for (const DexInstructionPcPair& inst : code->Instructions()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001053 const uint32_t insn_width = inst->SizeInCodeUnits();
David Sehr7629f602016-08-07 16:01:51 -07001054 if (insn_width == 0) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001055 fprintf(stderr, "GLITCH: zero-width instruction at idx=0x%04x\n", inst.DexPc());
David Sehr7629f602016-08-07 16:01:51 -07001056 break;
1057 }
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001058 DumpInstruction(code, code_offset, inst.DexPc(), insn_width, &inst.Inst());
David Sehr7629f602016-08-07 16:01:51 -07001059 } // for
1060}
1061
1062/*
David Sehraa6abb02017-10-12 08:25:11 -07001063 * Callback for dumping each positions table entry.
1064 */
1065static bool DumpPositionsCb(void* context, const DexFile::PositionInfo& entry) {
1066 FILE* out_file = reinterpret_cast<FILE*>(context);
1067 fprintf(out_file, " 0x%04x line=%d\n", entry.address_, entry.line_);
1068 return false;
1069}
1070
1071/*
1072 * Callback for dumping locals table entry.
1073 */
1074static void DumpLocalsCb(void* context, const DexFile::LocalInfo& entry) {
1075 const char* signature = entry.signature_ != nullptr ? entry.signature_ : "";
1076 FILE* out_file = reinterpret_cast<FILE*>(context);
1077 fprintf(out_file, " 0x%04x - 0x%04x reg=%d %s %s %s\n",
1078 entry.start_address_, entry.end_address_, entry.reg_,
1079 entry.name_, entry.descriptor_, signature);
1080}
1081
1082/*
1083 * Lookup functions.
1084 */
1085static const char* StringDataByIdx(uint32_t idx, dex_ir::Collections& collections) {
1086 dex_ir::StringId* string_id = collections.GetStringIdOrNullPtr(idx);
1087 if (string_id == nullptr) {
1088 return nullptr;
1089 }
1090 return string_id->Data();
1091}
1092
1093static const char* StringDataByTypeIdx(uint16_t idx, dex_ir::Collections& collections) {
1094 dex_ir::TypeId* type_id = collections.GetTypeIdOrNullPtr(idx);
1095 if (type_id == nullptr) {
1096 return nullptr;
1097 }
1098 dex_ir::StringId* string_id = type_id->GetStringId();
1099 if (string_id == nullptr) {
1100 return nullptr;
1101 }
1102 return string_id->Data();
1103}
1104
1105
1106/*
David Sehr7629f602016-08-07 16:01:51 -07001107 * Dumps code of a method.
1108 */
David Sehraa6abb02017-10-12 08:25:11 -07001109void DexLayout::DumpCode(uint32_t idx,
1110 const dex_ir::CodeItem* code,
1111 uint32_t code_offset,
1112 const char* declaring_class_descriptor,
1113 const char* method_name,
1114 bool is_static,
1115 const dex_ir::ProtoId* proto) {
David Sehr7629f602016-08-07 16:01:51 -07001116 fprintf(out_file_, " registers : %d\n", code->RegistersSize());
1117 fprintf(out_file_, " ins : %d\n", code->InsSize());
1118 fprintf(out_file_, " outs : %d\n", code->OutsSize());
1119 fprintf(out_file_, " insns size : %d 16-bit code units\n",
1120 code->InsnsSize());
1121
1122 // Bytecode disassembly, if requested.
1123 if (options_.disassemble_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001124 DumpBytecodes(idx, code, code_offset);
David Sehr7629f602016-08-07 16:01:51 -07001125 }
1126
1127 // Try-catch blocks.
1128 DumpCatches(code);
1129
1130 // Positions and locals table in the debug info.
David Sehraa6abb02017-10-12 08:25:11 -07001131 dex_ir::DebugInfoItem* debug_info = code->DebugInfo();
David Sehr7629f602016-08-07 16:01:51 -07001132 fprintf(out_file_, " positions : \n");
David Sehraa6abb02017-10-12 08:25:11 -07001133 if (debug_info != nullptr) {
1134 DexFile::DecodeDebugPositionInfo(debug_info->GetDebugInfo(),
1135 [this](uint32_t idx) {
1136 return StringDataByIdx(idx, this->header_->GetCollections());
1137 },
1138 DumpPositionsCb,
1139 out_file_);
1140 }
David Sehr7629f602016-08-07 16:01:51 -07001141 fprintf(out_file_, " locals : \n");
David Sehraa6abb02017-10-12 08:25:11 -07001142 if (debug_info != nullptr) {
1143 std::vector<const char*> arg_descriptors;
1144 const dex_ir::TypeList* parameters = proto->Parameters();
1145 if (parameters != nullptr) {
1146 const dex_ir::TypeIdVector* parameter_type_vector = parameters->GetTypeList();
1147 if (parameter_type_vector != nullptr) {
1148 for (const dex_ir::TypeId* type_id : *parameter_type_vector) {
1149 arg_descriptors.push_back(type_id->GetStringId()->Data());
1150 }
1151 }
1152 }
1153 DexFile::DecodeDebugLocalInfo(debug_info->GetDebugInfo(),
1154 "DexLayout in-memory",
1155 declaring_class_descriptor,
1156 arg_descriptors,
1157 method_name,
1158 is_static,
1159 code->RegistersSize(),
1160 code->InsSize(),
1161 code->InsnsSize(),
1162 [this](uint32_t idx) {
1163 return StringDataByIdx(idx, this->header_->GetCollections());
1164 },
1165 [this](uint32_t idx) {
1166 return
1167 StringDataByTypeIdx(dchecked_integral_cast<uint16_t>(idx),
1168 this->header_->GetCollections());
1169 },
1170 DumpLocalsCb,
1171 out_file_);
1172 }
David Sehr7629f602016-08-07 16:01:51 -07001173}
1174
1175/*
1176 * Dumps a method.
1177 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001178void DexLayout::DumpMethod(uint32_t idx, uint32_t flags, const dex_ir::CodeItem* code, int i) {
David Sehr7629f602016-08-07 16:01:51 -07001179 // Bail for anything private if export only requested.
1180 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1181 return;
1182 }
1183
Jeff Haoea7c6292016-11-14 18:10:16 -08001184 dex_ir::MethodId* method_id = header_->GetCollections().GetMethodId(idx);
David Sehr7629f602016-08-07 16:01:51 -07001185 const char* name = method_id->Name()->Data();
1186 char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str());
1187 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1188 char* access_str = CreateAccessFlagStr(flags, kAccessForMethod);
1189
1190 if (options_.output_format_ == kOutputPlain) {
1191 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1192 fprintf(out_file_, " name : '%s'\n", name);
1193 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1194 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
1195 if (code == nullptr) {
1196 fprintf(out_file_, " code : (none)\n");
1197 } else {
1198 fprintf(out_file_, " code -\n");
David Sehraa6abb02017-10-12 08:25:11 -07001199 DumpCode(idx,
1200 code,
1201 code->GetOffset(),
1202 back_descriptor,
1203 name,
1204 (flags & kAccStatic) != 0,
1205 method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -07001206 }
1207 if (options_.disassemble_) {
1208 fputc('\n', out_file_);
1209 }
1210 } else if (options_.output_format_ == kOutputXml) {
1211 const bool constructor = (name[0] == '<');
1212
1213 // Method name and prototype.
1214 if (constructor) {
1215 std::string dot(DescriptorClassToDot(back_descriptor));
1216 fprintf(out_file_, "<constructor name=\"%s\"\n", dot.c_str());
Jeff Haoc3acfc52016-08-29 14:18:26 -07001217 dot = DescriptorToDotWrapper(back_descriptor);
David Sehr7629f602016-08-07 16:01:51 -07001218 fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
1219 } else {
1220 fprintf(out_file_, "<method name=\"%s\"\n", name);
1221 const char* return_type = strrchr(type_descriptor, ')');
1222 if (return_type == nullptr) {
1223 fprintf(stderr, "bad method type descriptor '%s'\n", type_descriptor);
1224 goto bail;
1225 }
Jeff Haoc3acfc52016-08-29 14:18:26 -07001226 std::string dot(DescriptorToDotWrapper(return_type + 1));
David Sehr7629f602016-08-07 16:01:51 -07001227 fprintf(out_file_, " return=\"%s\"\n", dot.c_str());
1228 fprintf(out_file_, " abstract=%s\n", QuotedBool((flags & kAccAbstract) != 0));
1229 fprintf(out_file_, " native=%s\n", QuotedBool((flags & kAccNative) != 0));
1230 fprintf(out_file_, " synchronized=%s\n", QuotedBool(
1231 (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0));
1232 }
1233
1234 // Additional method flags.
1235 fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
1236 fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
1237 // The "deprecated=" not knowable w/o parsing annotations.
1238 fprintf(out_file_, " visibility=%s\n>\n", QuotedVisibility(flags));
1239
1240 // Parameters.
1241 if (type_descriptor[0] != '(') {
1242 fprintf(stderr, "ERROR: bad descriptor '%s'\n", type_descriptor);
1243 goto bail;
1244 }
1245 char* tmp_buf = reinterpret_cast<char*>(malloc(strlen(type_descriptor) + 1));
1246 const char* base = type_descriptor + 1;
1247 int arg_num = 0;
1248 while (*base != ')') {
1249 char* cp = tmp_buf;
1250 while (*base == '[') {
1251 *cp++ = *base++;
1252 }
1253 if (*base == 'L') {
1254 // Copy through ';'.
1255 do {
1256 *cp = *base++;
1257 } while (*cp++ != ';');
1258 } else {
1259 // Primitive char, copy it.
1260 if (strchr("ZBCSIFJD", *base) == nullptr) {
1261 fprintf(stderr, "ERROR: bad method signature '%s'\n", base);
1262 break; // while
1263 }
1264 *cp++ = *base++;
1265 }
1266 // Null terminate and display.
1267 *cp++ = '\0';
Jeff Haoc3acfc52016-08-29 14:18:26 -07001268 std::string dot(DescriptorToDotWrapper(tmp_buf));
David Sehr7629f602016-08-07 16:01:51 -07001269 fprintf(out_file_, "<parameter name=\"arg%d\" type=\"%s\">\n"
1270 "</parameter>\n", arg_num++, dot.c_str());
1271 } // while
1272 free(tmp_buf);
1273 if (constructor) {
1274 fprintf(out_file_, "</constructor>\n");
1275 } else {
1276 fprintf(out_file_, "</method>\n");
1277 }
1278 }
1279
1280 bail:
1281 free(type_descriptor);
1282 free(access_str);
1283}
1284
1285/*
1286 * Dumps a static (class) field.
1287 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001288void DexLayout::DumpSField(uint32_t idx, uint32_t flags, int i, dex_ir::EncodedValue* init) {
David Sehr7629f602016-08-07 16:01:51 -07001289 // Bail for anything private if export only requested.
1290 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1291 return;
1292 }
1293
Jeff Haoea7c6292016-11-14 18:10:16 -08001294 dex_ir::FieldId* field_id = header_->GetCollections().GetFieldId(idx);
David Sehr7629f602016-08-07 16:01:51 -07001295 const char* name = field_id->Name()->Data();
1296 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
1297 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
1298 char* access_str = CreateAccessFlagStr(flags, kAccessForField);
1299
1300 if (options_.output_format_ == kOutputPlain) {
1301 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1302 fprintf(out_file_, " name : '%s'\n", name);
1303 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1304 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
1305 if (init != nullptr) {
1306 fputs(" value : ", out_file_);
1307 DumpEncodedValue(init);
1308 fputs("\n", out_file_);
1309 }
1310 } else if (options_.output_format_ == kOutputXml) {
1311 fprintf(out_file_, "<field name=\"%s\"\n", name);
Jeff Haoc3acfc52016-08-29 14:18:26 -07001312 std::string dot(DescriptorToDotWrapper(type_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001313 fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
1314 fprintf(out_file_, " transient=%s\n", QuotedBool((flags & kAccTransient) != 0));
1315 fprintf(out_file_, " volatile=%s\n", QuotedBool((flags & kAccVolatile) != 0));
1316 // The "value=" is not knowable w/o parsing annotations.
1317 fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
1318 fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
1319 // The "deprecated=" is not knowable w/o parsing annotations.
1320 fprintf(out_file_, " visibility=%s\n", QuotedVisibility(flags));
1321 if (init != nullptr) {
1322 fputs(" value=\"", out_file_);
1323 DumpEncodedValue(init);
1324 fputs("\"\n", out_file_);
1325 }
1326 fputs(">\n</field>\n", out_file_);
1327 }
1328
1329 free(access_str);
1330}
1331
1332/*
1333 * Dumps an instance field.
1334 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001335void DexLayout::DumpIField(uint32_t idx, uint32_t flags, int i) {
1336 DumpSField(idx, flags, i, nullptr);
David Sehr7629f602016-08-07 16:01:51 -07001337}
1338
1339/*
David Sehr7629f602016-08-07 16:01:51 -07001340 * Dumps the class.
1341 *
1342 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1343 *
1344 * If "*last_package" is nullptr or does not match the current class' package,
1345 * the value will be replaced with a newly-allocated string.
1346 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001347void DexLayout::DumpClass(int idx, char** last_package) {
1348 dex_ir::ClassDef* class_def = header_->GetCollections().GetClassDef(idx);
David Sehr7629f602016-08-07 16:01:51 -07001349 // Omitting non-public class.
1350 if (options_.exports_only_ && (class_def->GetAccessFlags() & kAccPublic) == 0) {
1351 return;
1352 }
1353
1354 if (options_.show_section_headers_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001355 DumpClassDef(idx);
David Sehr7629f602016-08-07 16:01:51 -07001356 }
1357
1358 if (options_.show_annotations_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001359 DumpClassAnnotations(idx);
David Sehr7629f602016-08-07 16:01:51 -07001360 }
1361
David Sehr7629f602016-08-07 16:01:51 -07001362 // For the XML output, show the package name. Ideally we'd gather
1363 // up the classes, sort them, and dump them alphabetically so the
1364 // package name wouldn't jump around, but that's not a great plan
1365 // for something that needs to run on the device.
Jeff Hao3ab96b42016-09-09 18:35:01 -07001366 const char* class_descriptor =
Jeff Haoea7c6292016-11-14 18:10:16 -08001367 header_->GetCollections().GetClassDef(idx)->ClassType()->GetStringId()->Data();
David Sehr7629f602016-08-07 16:01:51 -07001368 if (!(class_descriptor[0] == 'L' &&
1369 class_descriptor[strlen(class_descriptor)-1] == ';')) {
1370 // Arrays and primitives should not be defined explicitly. Keep going?
1371 fprintf(stderr, "Malformed class name '%s'\n", class_descriptor);
1372 } else if (options_.output_format_ == kOutputXml) {
1373 char* mangle = strdup(class_descriptor + 1);
1374 mangle[strlen(mangle)-1] = '\0';
1375
1376 // Reduce to just the package name.
1377 char* last_slash = strrchr(mangle, '/');
1378 if (last_slash != nullptr) {
1379 *last_slash = '\0';
1380 } else {
1381 *mangle = '\0';
1382 }
1383
1384 for (char* cp = mangle; *cp != '\0'; cp++) {
1385 if (*cp == '/') {
1386 *cp = '.';
1387 }
1388 } // for
1389
1390 if (*last_package == nullptr || strcmp(mangle, *last_package) != 0) {
1391 // Start of a new package.
1392 if (*last_package != nullptr) {
1393 fprintf(out_file_, "</package>\n");
1394 }
1395 fprintf(out_file_, "<package name=\"%s\"\n>\n", mangle);
1396 free(*last_package);
1397 *last_package = mangle;
1398 } else {
1399 free(mangle);
1400 }
1401 }
1402
1403 // General class information.
1404 char* access_str = CreateAccessFlagStr(class_def->GetAccessFlags(), kAccessForClass);
1405 const char* superclass_descriptor = nullptr;
1406 if (class_def->Superclass() != nullptr) {
1407 superclass_descriptor = class_def->Superclass()->GetStringId()->Data();
1408 }
1409 if (options_.output_format_ == kOutputPlain) {
1410 fprintf(out_file_, "Class #%d -\n", idx);
1411 fprintf(out_file_, " Class descriptor : '%s'\n", class_descriptor);
1412 fprintf(out_file_, " Access flags : 0x%04x (%s)\n",
1413 class_def->GetAccessFlags(), access_str);
1414 if (superclass_descriptor != nullptr) {
1415 fprintf(out_file_, " Superclass : '%s'\n", superclass_descriptor);
1416 }
1417 fprintf(out_file_, " Interfaces -\n");
1418 } else {
1419 std::string dot(DescriptorClassToDot(class_descriptor));
1420 fprintf(out_file_, "<class name=\"%s\"\n", dot.c_str());
1421 if (superclass_descriptor != nullptr) {
Jeff Haoc3acfc52016-08-29 14:18:26 -07001422 dot = DescriptorToDotWrapper(superclass_descriptor);
David Sehr7629f602016-08-07 16:01:51 -07001423 fprintf(out_file_, " extends=\"%s\"\n", dot.c_str());
1424 }
1425 fprintf(out_file_, " interface=%s\n",
1426 QuotedBool((class_def->GetAccessFlags() & kAccInterface) != 0));
1427 fprintf(out_file_, " abstract=%s\n",
1428 QuotedBool((class_def->GetAccessFlags() & kAccAbstract) != 0));
1429 fprintf(out_file_, " static=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccStatic) != 0));
1430 fprintf(out_file_, " final=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccFinal) != 0));
1431 // The "deprecated=" not knowable w/o parsing annotations.
1432 fprintf(out_file_, " visibility=%s\n", QuotedVisibility(class_def->GetAccessFlags()));
1433 fprintf(out_file_, ">\n");
1434 }
1435
1436 // Interfaces.
Jeff Haocc829592017-03-14 16:13:39 -07001437 const dex_ir::TypeList* interfaces = class_def->Interfaces();
David Sehr853a8e12016-09-01 13:03:50 -07001438 if (interfaces != nullptr) {
Jeff Haocc829592017-03-14 16:13:39 -07001439 const dex_ir::TypeIdVector* interfaces_vector = interfaces->GetTypeList();
1440 for (uint32_t i = 0; i < interfaces_vector->size(); i++) {
1441 DumpInterface((*interfaces_vector)[i], i);
David Sehr853a8e12016-09-01 13:03:50 -07001442 } // for
1443 }
David Sehr7629f602016-08-07 16:01:51 -07001444
1445 // Fields and methods.
1446 dex_ir::ClassData* class_data = class_def->GetClassData();
1447 // Prepare data for static fields.
Jeff Hao3ab96b42016-09-09 18:35:01 -07001448 dex_ir::EncodedArrayItem* static_values = class_def->StaticValues();
1449 dex_ir::EncodedValueVector* encoded_values =
1450 static_values == nullptr ? nullptr : static_values->GetEncodedValues();
1451 const uint32_t encoded_values_size = (encoded_values == nullptr) ? 0 : encoded_values->size();
David Sehr7629f602016-08-07 16:01:51 -07001452
1453 // Static fields.
1454 if (options_.output_format_ == kOutputPlain) {
1455 fprintf(out_file_, " Static fields -\n");
1456 }
David Sehr853a8e12016-09-01 13:03:50 -07001457 if (class_data != nullptr) {
1458 dex_ir::FieldItemVector* static_fields = class_data->StaticFields();
1459 if (static_fields != nullptr) {
1460 for (uint32_t i = 0; i < static_fields->size(); i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001461 DumpSField((*static_fields)[i]->GetFieldId()->GetIndex(),
David Sehr853a8e12016-09-01 13:03:50 -07001462 (*static_fields)[i]->GetAccessFlags(),
1463 i,
Jeff Hao3ab96b42016-09-09 18:35:01 -07001464 i < encoded_values_size ? (*encoded_values)[i].get() : nullptr);
David Sehr853a8e12016-09-01 13:03:50 -07001465 } // for
1466 }
1467 }
David Sehr7629f602016-08-07 16:01:51 -07001468
1469 // Instance fields.
1470 if (options_.output_format_ == kOutputPlain) {
1471 fprintf(out_file_, " Instance fields -\n");
1472 }
David Sehr853a8e12016-09-01 13:03:50 -07001473 if (class_data != nullptr) {
1474 dex_ir::FieldItemVector* instance_fields = class_data->InstanceFields();
1475 if (instance_fields != nullptr) {
1476 for (uint32_t i = 0; i < instance_fields->size(); i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001477 DumpIField((*instance_fields)[i]->GetFieldId()->GetIndex(),
David Sehr853a8e12016-09-01 13:03:50 -07001478 (*instance_fields)[i]->GetAccessFlags(),
1479 i);
1480 } // for
1481 }
1482 }
David Sehr7629f602016-08-07 16:01:51 -07001483
1484 // Direct methods.
1485 if (options_.output_format_ == kOutputPlain) {
1486 fprintf(out_file_, " Direct methods -\n");
1487 }
David Sehr853a8e12016-09-01 13:03:50 -07001488 if (class_data != nullptr) {
1489 dex_ir::MethodItemVector* direct_methods = class_data->DirectMethods();
1490 if (direct_methods != nullptr) {
1491 for (uint32_t i = 0; i < direct_methods->size(); i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001492 DumpMethod((*direct_methods)[i]->GetMethodId()->GetIndex(),
David Sehr853a8e12016-09-01 13:03:50 -07001493 (*direct_methods)[i]->GetAccessFlags(),
1494 (*direct_methods)[i]->GetCodeItem(),
1495 i);
1496 } // for
1497 }
1498 }
David Sehr7629f602016-08-07 16:01:51 -07001499
1500 // Virtual methods.
1501 if (options_.output_format_ == kOutputPlain) {
1502 fprintf(out_file_, " Virtual methods -\n");
1503 }
David Sehr853a8e12016-09-01 13:03:50 -07001504 if (class_data != nullptr) {
1505 dex_ir::MethodItemVector* virtual_methods = class_data->VirtualMethods();
1506 if (virtual_methods != nullptr) {
1507 for (uint32_t i = 0; i < virtual_methods->size(); i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001508 DumpMethod((*virtual_methods)[i]->GetMethodId()->GetIndex(),
David Sehr853a8e12016-09-01 13:03:50 -07001509 (*virtual_methods)[i]->GetAccessFlags(),
1510 (*virtual_methods)[i]->GetCodeItem(),
1511 i);
1512 } // for
1513 }
1514 }
David Sehr7629f602016-08-07 16:01:51 -07001515
1516 // End of class.
1517 if (options_.output_format_ == kOutputPlain) {
1518 const char* file_name = "unknown";
1519 if (class_def->SourceFile() != nullptr) {
1520 file_name = class_def->SourceFile()->Data();
1521 }
1522 const dex_ir::StringId* source_file = class_def->SourceFile();
1523 fprintf(out_file_, " source_file_idx : %d (%s)\n\n",
Jeff Hao3ab96b42016-09-09 18:35:01 -07001524 source_file == nullptr ? 0xffffffffU : source_file->GetIndex(), file_name);
David Sehr7629f602016-08-07 16:01:51 -07001525 } else if (options_.output_format_ == kOutputXml) {
1526 fprintf(out_file_, "</class>\n");
1527 }
1528
1529 free(access_str);
1530}
1531
Jeff Haoea7c6292016-11-14 18:10:16 -08001532void DexLayout::DumpDexFile() {
David Sehr7629f602016-08-07 16:01:51 -07001533 // Headers.
1534 if (options_.show_file_headers_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001535 DumpFileHeader();
David Sehr7629f602016-08-07 16:01:51 -07001536 }
1537
1538 // Open XML context.
1539 if (options_.output_format_ == kOutputXml) {
1540 fprintf(out_file_, "<api>\n");
1541 }
1542
1543 // Iterate over all classes.
1544 char* package = nullptr;
Jeff Haoea7c6292016-11-14 18:10:16 -08001545 const uint32_t class_defs_size = header_->GetCollections().ClassDefsSize();
David Sehr7629f602016-08-07 16:01:51 -07001546 for (uint32_t i = 0; i < class_defs_size; i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001547 DumpClass(i, &package);
David Sehr7629f602016-08-07 16:01:51 -07001548 } // for
1549
1550 // Free the last package allocated.
1551 if (package != nullptr) {
1552 fprintf(out_file_, "</package>\n");
1553 free(package);
1554 }
1555
1556 // Close XML context.
1557 if (options_.output_format_ == kOutputXml) {
1558 fprintf(out_file_, "</api>\n");
1559 }
Jeff Haoea7c6292016-11-14 18:10:16 -08001560}
Jeff Hao3ab96b42016-09-09 18:35:01 -07001561
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001562void DexLayout::LayoutClassDefsAndClassData(const DexFile* dex_file) {
Jeff Hao042e8982016-10-19 11:17:11 -07001563 std::vector<dex_ir::ClassDef*> new_class_def_order;
1564 for (std::unique_ptr<dex_ir::ClassDef>& class_def : header_->GetCollections().ClassDefs()) {
1565 dex::TypeIndex type_idx(class_def->ClassType()->GetIndex());
1566 if (info_->ContainsClass(*dex_file, type_idx)) {
1567 new_class_def_order.push_back(class_def.get());
1568 }
1569 }
1570 for (std::unique_ptr<dex_ir::ClassDef>& class_def : header_->GetCollections().ClassDefs()) {
1571 dex::TypeIndex type_idx(class_def->ClassType()->GetIndex());
1572 if (!info_->ContainsClass(*dex_file, type_idx)) {
1573 new_class_def_order.push_back(class_def.get());
1574 }
1575 }
Jeff Haoe17f5892017-02-23 16:14:04 -08001576 std::unordered_set<dex_ir::ClassData*> visited_class_data;
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001577 size_t class_data_index = 0;
1578 dex_ir::CollectionVector<dex_ir::ClassData>::Vector& class_datas =
1579 header_->GetCollections().ClassDatas();
1580 for (dex_ir::ClassDef* class_def : new_class_def_order) {
Jeff Haoe17f5892017-02-23 16:14:04 -08001581 dex_ir::ClassData* class_data = class_def->GetClassData();
1582 if (class_data != nullptr && visited_class_data.find(class_data) == visited_class_data.end()) {
Jeff Haoe17f5892017-02-23 16:14:04 -08001583 visited_class_data.insert(class_data);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001584 // Overwrite the existing vector with the new ordering, note that the sets of objects are
1585 // equivalent, but the order changes. This is why this is not a memory leak.
1586 // TODO: Consider cleaning this up with a shared_ptr.
1587 class_datas[class_data_index].release();
1588 class_datas[class_data_index].reset(class_data);
1589 ++class_data_index;
Jeff Hao042e8982016-10-19 11:17:11 -07001590 }
1591 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001592 CHECK_EQ(class_data_index, class_datas.size());
1593
Mathieu Chartier2c4b0842017-12-13 11:49:51 -08001594 if (DexLayout::kChangeClassDefOrder) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001595 // This currently produces dex files that violate the spec since the super class class_def is
1596 // supposed to occur before any subclasses.
1597 dex_ir::CollectionVector<dex_ir::ClassDef>::Vector& class_defs =
1598 header_->GetCollections().ClassDefs();
1599 CHECK_EQ(new_class_def_order.size(), class_defs.size());
1600 for (size_t i = 0; i < class_defs.size(); ++i) {
1601 // Overwrite the existing vector with the new ordering, note that the sets of objects are
1602 // equivalent, but the order changes. This is why this is not a memory leak.
1603 // TODO: Consider cleaning this up with a shared_ptr.
1604 class_defs[i].release();
1605 class_defs[i].reset(new_class_def_order[i]);
1606 }
1607 }
Jeff Hao042e8982016-10-19 11:17:11 -07001608}
1609
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001610void DexLayout::LayoutStringData(const DexFile* dex_file) {
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001611 const size_t num_strings = header_->GetCollections().StringIds().size();
1612 std::vector<bool> is_shorty(num_strings, false);
1613 std::vector<bool> from_hot_method(num_strings, false);
1614 for (std::unique_ptr<dex_ir::ClassDef>& class_def : header_->GetCollections().ClassDefs()) {
1615 // 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 -07001616 // as hot. Add its super class and interfaces as well, which can be used during initialization.
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001617 const bool is_profile_class =
1618 info_->ContainsClass(*dex_file, dex::TypeIndex(class_def->ClassType()->GetIndex()));
1619 if (is_profile_class) {
1620 from_hot_method[class_def->ClassType()->GetStringId()->GetIndex()] = true;
Jeff Haoacc83d72017-07-06 17:51:01 -07001621 const dex_ir::TypeId* superclass = class_def->Superclass();
1622 if (superclass != nullptr) {
1623 from_hot_method[superclass->GetStringId()->GetIndex()] = true;
1624 }
1625 const dex_ir::TypeList* interfaces = class_def->Interfaces();
1626 if (interfaces != nullptr) {
1627 for (const dex_ir::TypeId* interface_type : *interfaces->GetTypeList()) {
1628 from_hot_method[interface_type->GetStringId()->GetIndex()] = true;
1629 }
1630 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001631 }
1632 dex_ir::ClassData* data = class_def->GetClassData();
1633 if (data == nullptr) {
1634 continue;
1635 }
1636 for (size_t i = 0; i < 2; ++i) {
1637 for (auto& method : *(i == 0 ? data->DirectMethods() : data->VirtualMethods())) {
1638 const dex_ir::MethodId* method_id = method->GetMethodId();
1639 dex_ir::CodeItem* code_item = method->GetCodeItem();
1640 if (code_item == nullptr) {
1641 continue;
1642 }
1643 const bool is_clinit = is_profile_class &&
1644 (method->GetAccessFlags() & kAccConstructor) != 0 &&
1645 (method->GetAccessFlags() & kAccStatic) != 0;
1646 const bool method_executed = is_clinit ||
Mathieu Chartiere46f3a82017-06-19 19:54:12 -07001647 info_->GetMethodHotness(MethodReference(dex_file, method_id->GetIndex())).IsInProfile();
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001648 if (!method_executed) {
1649 continue;
1650 }
1651 is_shorty[method_id->Proto()->Shorty()->GetIndex()] = true;
1652 dex_ir::CodeFixups* fixups = code_item->GetCodeFixups();
1653 if (fixups == nullptr) {
1654 continue;
1655 }
Jeff Haoacc83d72017-07-06 17:51:01 -07001656 // Add const-strings.
Vladimir Marko219cb902017-12-07 16:20:39 +00001657 for (dex_ir::StringId* id : fixups->StringIds()) {
Jeff Haoacc83d72017-07-06 17:51:01 -07001658 from_hot_method[id->GetIndex()] = true;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001659 }
Jeff Haoacc83d72017-07-06 17:51:01 -07001660 // Add field classes, names, and types.
Vladimir Marko219cb902017-12-07 16:20:39 +00001661 for (dex_ir::FieldId* id : fixups->FieldIds()) {
Jeff Haoacc83d72017-07-06 17:51:01 -07001662 // TODO: Only visit field ids from static getters and setters.
1663 from_hot_method[id->Class()->GetStringId()->GetIndex()] = true;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001664 from_hot_method[id->Name()->GetIndex()] = true;
1665 from_hot_method[id->Type()->GetStringId()->GetIndex()] = true;
1666 }
Jeff Haoacc83d72017-07-06 17:51:01 -07001667 // For clinits, add referenced method classes, names, and protos.
1668 if (is_clinit) {
Vladimir Marko219cb902017-12-07 16:20:39 +00001669 for (dex_ir::MethodId* id : fixups->MethodIds()) {
Jeff Haoacc83d72017-07-06 17:51:01 -07001670 from_hot_method[id->Class()->GetStringId()->GetIndex()] = true;
1671 from_hot_method[id->Name()->GetIndex()] = true;
1672 is_shorty[id->Proto()->Shorty()->GetIndex()] = true;
1673 }
1674 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001675 }
1676 }
1677 }
1678 // Sort string data by specified order.
1679 std::vector<dex_ir::StringId*> string_ids;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001680 for (auto& string_id : header_->GetCollections().StringIds()) {
1681 string_ids.push_back(string_id.get());
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001682 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001683 std::sort(string_ids.begin(),
1684 string_ids.end(),
1685 [&is_shorty, &from_hot_method](const dex_ir::StringId* a,
1686 const dex_ir::StringId* b) {
1687 const bool a_is_hot = from_hot_method[a->GetIndex()];
1688 const bool b_is_hot = from_hot_method[b->GetIndex()];
1689 if (a_is_hot != b_is_hot) {
1690 return a_is_hot < b_is_hot;
1691 }
1692 // After hot methods are partitioned, subpartition shorties.
1693 const bool a_is_shorty = is_shorty[a->GetIndex()];
1694 const bool b_is_shorty = is_shorty[b->GetIndex()];
1695 if (a_is_shorty != b_is_shorty) {
1696 return a_is_shorty < b_is_shorty;
1697 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001698 // Order by index by default.
1699 return a->GetIndex() < b->GetIndex();
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001700 });
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001701 dex_ir::CollectionVector<dex_ir::StringData>::Vector& string_datas =
1702 header_->GetCollections().StringDatas();
1703 // Now we know what order we want the string data, reorder them.
1704 size_t data_index = 0;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001705 for (dex_ir::StringId* string_id : string_ids) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001706 string_datas[data_index].release();
1707 string_datas[data_index].reset(string_id->DataItem());
1708 ++data_index;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001709 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001710 if (kIsDebugBuild) {
1711 std::unordered_set<dex_ir::StringData*> visited;
1712 for (const std::unique_ptr<dex_ir::StringData>& data : string_datas) {
1713 visited.insert(data.get());
1714 }
1715 for (auto& string_id : header_->GetCollections().StringIds()) {
1716 CHECK(visited.find(string_id->DataItem()) != visited.end());
1717 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001718 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001719 CHECK_EQ(data_index, string_datas.size());
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001720}
1721
Jeff Haoe17f5892017-02-23 16:14:04 -08001722// Orders code items according to specified class data ordering.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001723void DexLayout::LayoutCodeItems(const DexFile* dex_file) {
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001724 static constexpr InvokeType invoke_types[] = {
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001725 kDirect,
1726 kVirtual
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001727 };
1728
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001729 std::unordered_map<dex_ir::CodeItem*, LayoutType>& code_item_layout =
1730 layout_hotness_info_.code_item_layout_;
1731
1732 // Assign hotness flags to all code items.
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001733 for (InvokeType invoke_type : invoke_types) {
1734 for (std::unique_ptr<dex_ir::ClassDef>& class_def : header_->GetCollections().ClassDefs()) {
1735 const bool is_profile_class =
1736 info_->ContainsClass(*dex_file, dex::TypeIndex(class_def->ClassType()->GetIndex()));
1737
1738 // Skip classes that are not defined in this dex file.
1739 dex_ir::ClassData* class_data = class_def->GetClassData();
1740 if (class_data == nullptr) {
1741 continue;
Jeff Haoe17f5892017-02-23 16:14:04 -08001742 }
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001743 for (auto& method : *(invoke_type == InvokeType::kDirect
1744 ? class_data->DirectMethods()
1745 : class_data->VirtualMethods())) {
1746 const dex_ir::MethodId *method_id = method->GetMethodId();
1747 dex_ir::CodeItem *code_item = method->GetCodeItem();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001748 if (code_item == nullptr) {
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001749 continue;
1750 }
1751 // Separate executed methods (clinits and profiled methods) from unexecuted methods.
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001752 const bool is_clinit = (method->GetAccessFlags() & kAccConstructor) != 0 &&
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001753 (method->GetAccessFlags() & kAccStatic) != 0;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001754 const bool is_startup_clinit = is_profile_class && is_clinit;
1755 using Hotness = ProfileCompilationInfo::MethodHotness;
1756 Hotness hotness = info_->GetMethodHotness(MethodReference(dex_file, method_id->GetIndex()));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001757 LayoutType state = LayoutType::kLayoutTypeUnused;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001758 if (hotness.IsHot()) {
1759 // Hot code is compiled, maybe one day it won't be accessed. So lay it out together for
1760 // now.
Mathieu Chartier120aa282017-08-05 16:03:03 -07001761 state = LayoutType::kLayoutTypeHot;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001762 } else if (is_startup_clinit || hotness.GetFlags() == Hotness::kFlagStartup) {
1763 // Startup clinit or a method that only has the startup flag.
Mathieu Chartier120aa282017-08-05 16:03:03 -07001764 state = LayoutType::kLayoutTypeStartupOnly;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001765 } else if (is_clinit) {
Mathieu Chartier120aa282017-08-05 16:03:03 -07001766 state = LayoutType::kLayoutTypeUsedOnce;
Mathieu Chartiere46f3a82017-06-19 19:54:12 -07001767 } else if (hotness.IsInProfile()) {
Mathieu Chartier120aa282017-08-05 16:03:03 -07001768 state = LayoutType::kLayoutTypeSometimesUsed;
Jeff Hao206cbaa2017-06-07 19:11:01 -07001769 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001770 auto it = code_item_layout.emplace(code_item, state);
1771 if (!it.second) {
1772 LayoutType& layout_type = it.first->second;
1773 // Already exists, merge the hotness.
1774 layout_type = MergeLayoutType(layout_type, state);
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001775 }
1776 }
1777 }
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001778 }
Jeff Hao042e8982016-10-19 11:17:11 -07001779
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001780 dex_ir::CollectionVector<dex_ir::CodeItem>::Vector& code_items =
1781 header_->GetCollections().CodeItems();
1782 if (VLOG_IS_ON(dex)) {
1783 size_t layout_count[static_cast<size_t>(LayoutType::kLayoutTypeCount)] = {};
1784 for (const std::unique_ptr<dex_ir::CodeItem>& code_item : code_items) {
1785 auto it = code_item_layout.find(code_item.get());
1786 DCHECK(it != code_item_layout.end());
1787 ++layout_count[static_cast<size_t>(it->second)];
1788 }
1789 for (size_t i = 0; i < static_cast<size_t>(LayoutType::kLayoutTypeCount); ++i) {
1790 LOG(INFO) << "Code items in category " << i << " count=" << layout_count[i];
Jeff Haoe17f5892017-02-23 16:14:04 -08001791 }
1792 }
Jeff Hao042e8982016-10-19 11:17:11 -07001793
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001794 // Sort the code items vector by new layout. The writing process will take care of calculating
1795 // all the offsets. Stable sort to preserve any existing locality that might be there.
1796 std::stable_sort(code_items.begin(),
1797 code_items.end(),
1798 [&](const std::unique_ptr<dex_ir::CodeItem>& a,
1799 const std::unique_ptr<dex_ir::CodeItem>& b) {
1800 auto it_a = code_item_layout.find(a.get());
1801 auto it_b = code_item_layout.find(b.get());
1802 DCHECK(it_a != code_item_layout.end());
1803 DCHECK(it_b != code_item_layout.end());
1804 const LayoutType layout_type_a = it_a->second;
1805 const LayoutType layout_type_b = it_b->second;
1806 return layout_type_a < layout_type_b;
1807 });
Jeff Hao042e8982016-10-19 11:17:11 -07001808}
1809
1810void DexLayout::LayoutOutputFile(const DexFile* dex_file) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001811 LayoutStringData(dex_file);
1812 LayoutClassDefsAndClassData(dex_file);
1813 LayoutCodeItems(dex_file);
Jeff Hao042e8982016-10-19 11:17:11 -07001814}
1815
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001816void DexLayout::OutputDexFile(const DexFile* input_dex_file,
1817 bool compute_offsets,
1818 std::unique_ptr<DexContainer>* dex_container) {
1819 const std::string& dex_file_location = input_dex_file->GetLocation();
Jeff Haoea7c6292016-11-14 18:10:16 -08001820 std::string error_msg;
1821 std::unique_ptr<File> new_file;
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001822 // If options_.output_dex_directory_ is non null, we are outputting to a file.
1823 if (options_.output_dex_directory_ != nullptr) {
Jeff Haoa8621002016-10-04 18:13:44 +00001824 std::string output_location(options_.output_dex_directory_);
Andreas Gampe37c58462017-03-27 15:14:27 -07001825 size_t last_slash = dex_file_location.rfind('/');
Jeff Haoea7c6292016-11-14 18:10:16 -08001826 std::string dex_file_directory = dex_file_location.substr(0, last_slash + 1);
1827 if (output_location == dex_file_directory) {
1828 output_location = dex_file_location + ".new";
1829 } else if (last_slash != std::string::npos) {
1830 output_location += dex_file_location.substr(last_slash);
1831 } else {
1832 output_location += "/" + dex_file_location + ".new";
1833 }
1834 new_file.reset(OS::CreateEmptyFile(output_location.c_str()));
Jeff Hao3ba51e82017-04-12 16:14:54 -07001835 if (new_file == nullptr) {
1836 LOG(ERROR) << "Could not create dex writer output file: " << output_location;
1837 return;
1838 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001839 }
1840 DexWriter::Output(this, dex_container, compute_offsets);
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001841 if (new_file != nullptr) {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001842 DexContainer* const container = dex_container->get();
1843 DexContainer::Section* const main_section = container->GetMainSection();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001844 if (!new_file->WriteFully(main_section->Begin(), main_section->Size())) {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001845 LOG(ERROR) << "Failed to write main section for dex file " << dex_file_location;
1846 new_file->Erase();
1847 return;
1848 }
1849 DexContainer::Section* const data_section = container->GetDataSection();
1850 if (!new_file->WriteFully(data_section->Begin(), data_section->Size())) {
1851 LOG(ERROR) << "Failed to write data section for dex file " << dex_file_location;
David Sehr7639cdc2017-04-15 10:06:21 -07001852 new_file->Erase();
1853 return;
1854 }
Jeff Haoea7c6292016-11-14 18:10:16 -08001855 UNUSED(new_file->FlushCloseOrErase());
1856 }
1857}
1858
1859/*
1860 * Dumps the requested sections of the file.
1861 */
1862void DexLayout::ProcessDexFile(const char* file_name,
1863 const DexFile* dex_file,
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001864 size_t dex_file_index,
1865 std::unique_ptr<DexContainer>* dex_container) {
1866 const bool has_output_container = dex_container != nullptr;
1867 const bool output = options_.output_dex_directory_ != nullptr || has_output_container;
1868
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001869 // Try to avoid eagerly assigning offsets to find bugs since GetOffset will abort if the offset
1870 // is unassigned.
1871 bool eagerly_assign_offsets = false;
1872 if (options_.visualize_pattern_ || options_.show_section_statistics_ || options_.dump_) {
1873 // These options required the offsets for dumping purposes.
1874 eagerly_assign_offsets = true;
1875 }
Mathieu Chartier75175552018-01-25 11:23:01 -08001876 std::unique_ptr<dex_ir::Header> header(dex_ir::DexIrBuilder(*dex_file,
1877 eagerly_assign_offsets,
1878 GetOptions()));
Jeff Haoea7c6292016-11-14 18:10:16 -08001879 SetHeader(header.get());
1880
1881 if (options_.verbose_) {
1882 fprintf(out_file_, "Opened '%s', DEX version '%.3s'\n",
1883 file_name, dex_file->GetHeader().magic_ + 4);
1884 }
1885
1886 if (options_.visualize_pattern_) {
1887 VisualizeDexLayout(header_, dex_file, dex_file_index, info_);
1888 return;
1889 }
1890
David Sehr93357492017-03-09 08:02:44 -08001891 if (options_.show_section_statistics_) {
1892 ShowDexSectionStatistics(header_, dex_file_index);
1893 return;
1894 }
1895
Jeff Haoea7c6292016-11-14 18:10:16 -08001896 // Dump dex file.
1897 if (options_.dump_) {
1898 DumpDexFile();
1899 }
1900
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001901 // In case we are outputting to a file, keep it open so we can verify.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001902 if (output) {
1903 // Layout information about what strings and code items are hot. Used by the writing process
1904 // to generate the sections that are stored in the oat file.
1905 bool do_layout = info_ != nullptr;
1906 if (do_layout) {
Jeff Hao042e8982016-10-19 11:17:11 -07001907 LayoutOutputFile(dex_file);
1908 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001909 // The output needs a dex container, use a temporary one.
1910 std::unique_ptr<DexContainer> temp_container;
1911 if (dex_container == nullptr) {
1912 dex_container = &temp_container;
1913 }
Mathieu Chartier21cf2582018-01-08 17:09:48 -08001914 // If we didn't set the offsets eagerly, we definitely need to compute them here.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001915 OutputDexFile(dex_file, do_layout || !eagerly_assign_offsets, dex_container);
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001916
1917 // Clear header before verifying to reduce peak RAM usage.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001918 const size_t file_size = header_->FileSize();
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001919 header.reset();
1920
1921 // Verify the output dex file's structure, only enabled by default for debug builds.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001922 if (options_.verify_output_ && has_output_container) {
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001923 std::string error_msg;
1924 std::string location = "memory mapped file for " + std::string(file_name);
Mathieu Chartier8740c662018-01-11 14:50:02 -08001925 // Dex file verifier cannot handle compact dex.
1926 bool verify = options_.compact_dex_level_ == CompactDexLevel::kCompactDexLevelNone;
David Sehr013fd802018-01-11 22:55:24 -08001927 const ArtDexFileLoader dex_file_loader;
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001928 DexContainer::Section* const main_section = (*dex_container)->GetMainSection();
1929 DexContainer::Section* const data_section = (*dex_container)->GetDataSection();
1930 DCHECK_EQ(file_size, main_section->Size())
1931 << main_section->Size() << " " << data_section->Size();
David Sehr013fd802018-01-11 22:55:24 -08001932 std::unique_ptr<const DexFile> output_dex_file(
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001933 dex_file_loader.OpenWithDataSection(
1934 main_section->Begin(),
1935 main_section->Size(),
1936 data_section->Begin(),
1937 data_section->Size(),
1938 location,
1939 /* checksum */ 0,
1940 /*oat_dex_file*/ nullptr,
1941 verify,
1942 /*verify_checksum*/ false,
1943 &error_msg));
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001944 CHECK(output_dex_file != nullptr) << "Failed to re-open output file:" << error_msg;
1945
1946 // Do IR-level comparison between input and output. This check ignores potential differences
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001947 // due to layout, so offsets are not checked. Instead, it checks the data contents of each
1948 // item.
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001949 //
1950 // Regenerate output IR to catch any bugs that might happen during writing.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001951 std::unique_ptr<dex_ir::Header> output_header(
1952 dex_ir::DexIrBuilder(*output_dex_file,
Mathieu Chartier75175552018-01-25 11:23:01 -08001953 /*eagerly_assign_offsets*/ true,
1954 GetOptions()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001955 std::unique_ptr<dex_ir::Header> orig_header(
1956 dex_ir::DexIrBuilder(*dex_file,
Mathieu Chartier75175552018-01-25 11:23:01 -08001957 /*eagerly_assign_offsets*/ true,
1958 GetOptions()));
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001959 CHECK(VerifyOutputDexFile(output_header.get(), orig_header.get(), &error_msg)) << error_msg;
1960 }
Jeff Hao3ab96b42016-09-09 18:35:01 -07001961 }
David Sehr7629f602016-08-07 16:01:51 -07001962}
1963
1964/*
1965 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
1966 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001967int DexLayout::ProcessFile(const char* file_name) {
David Sehr7629f602016-08-07 16:01:51 -07001968 if (options_.verbose_) {
1969 fprintf(out_file_, "Processing '%s'...\n", file_name);
1970 }
1971
1972 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
1973 // all of which are Zip archives with "classes.dex" inside.
1974 const bool verify_checksum = !options_.ignore_bad_checksum_;
1975 std::string error_msg;
David Sehr013fd802018-01-11 22:55:24 -08001976 const ArtDexFileLoader dex_file_loader;
David Sehr7629f602016-08-07 16:01:51 -07001977 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr013fd802018-01-11 22:55:24 -08001978 if (!dex_file_loader.Open(
Nicolas Geoffray095c6c92017-10-19 13:59:55 +01001979 file_name, file_name, /* verify */ true, verify_checksum, &error_msg, &dex_files)) {
David Sehr7629f602016-08-07 16:01:51 -07001980 // Display returned error message to user. Note that this error behavior
1981 // differs from the error messages shown by the original Dalvik dexdump.
1982 fputs(error_msg.c_str(), stderr);
1983 fputc('\n', stderr);
1984 return -1;
1985 }
1986
1987 // Success. Either report checksum verification or process
1988 // all dex files found in given file.
1989 if (options_.checksum_only_) {
1990 fprintf(out_file_, "Checksum verified\n");
1991 } else {
1992 for (size_t i = 0; i < dex_files.size(); i++) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001993 // Pass in a null container to avoid output by default.
1994 ProcessDexFile(file_name, dex_files[i].get(), i, /*dex_container*/ nullptr);
David Sehr7629f602016-08-07 16:01:51 -07001995 }
1996 }
1997 return 0;
1998}
1999
2000} // namespace art