blob: 1b8412d86a3f45287f0dd40dd9ce1eaa52df149a [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 Sehr79e26072018-04-06 17:58:50 -070037#include "base/mem_map.h"
David Sehrc431b9d2018-03-02 12:01:51 -080038#include "base/os.h"
39#include "base/utils.h"
Mathieu Chartier818cb802018-05-11 05:30:16 +000040#include "dex/art_dex_file_loader.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080041#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080042#include "dex/dex_file-inl.h"
43#include "dex/dex_file_layout.h"
44#include "dex/dex_file_loader.h"
45#include "dex/dex_file_types.h"
46#include "dex/dex_file_verifier.h"
47#include "dex/dex_instruction-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070048#include "dex_ir_builder.h"
Jeff Haoec7f1a92017-03-13 16:24:24 -070049#include "dex_verify.h"
David Sehrcdcfde72016-09-26 07:44:04 -070050#include "dex_visualize.h"
Jeff Haoa8621002016-10-04 18:13:44 +000051#include "dex_writer.h"
David Sehr82d046e2018-04-23 08:14:19 -070052#include "profile/profile_compilation_info.h"
David Sehr7629f602016-08-07 16:01:51 -070053
54namespace art {
55
Andreas Gampe46ee31b2016-12-14 10:11:49 -080056using android::base::StringPrintf;
57
David Sehr7629f602016-08-07 16:01:51 -070058/*
David Sehr7629f602016-08-07 16:01:51 -070059 * Flags for use with createAccessFlagStr().
60 */
61enum AccessFor {
62 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
63};
64const int kNumFlags = 18;
65
66/*
67 * Gets 2 little-endian bytes.
68 */
69static inline uint16_t Get2LE(unsigned char const* src) {
70 return src[0] | (src[1] << 8);
71}
72
73/*
74 * Converts the class name portion of a type descriptor to human-readable
75 * "dotted" form. For example, "Ljava/lang/String;" becomes "String".
76 */
Orion Hodsonfe42d212018-08-24 14:01:14 +010077static std::string DescriptorClassToName(const char* str) {
David Sehr7629f602016-08-07 16:01:51 -070078 std::string descriptor(str);
79 // Reduce to just the class name prefix.
80 size_t last_slash = descriptor.rfind('/');
81 if (last_slash == std::string::npos) {
82 last_slash = 0;
83 }
84 // Start past the '/' or 'L'.
85 last_slash++;
86
87 // Copy class name over, trimming trailing ';'.
88 size_t size = descriptor.size() - 1 - last_slash;
89 std::string result(descriptor.substr(last_slash, size));
90
David Sehr7629f602016-08-07 16:01:51 -070091 return result;
92}
93
94/*
95 * Returns string representing the boolean value.
96 */
97static const char* StrBool(bool val) {
98 return val ? "true" : "false";
99}
100
101/*
102 * Returns a quoted string representing the boolean value.
103 */
104static const char* QuotedBool(bool val) {
105 return val ? "\"true\"" : "\"false\"";
106}
107
108/*
109 * Returns a quoted string representing the access flags.
110 */
111static const char* QuotedVisibility(uint32_t access_flags) {
112 if (access_flags & kAccPublic) {
113 return "\"public\"";
114 } else if (access_flags & kAccProtected) {
115 return "\"protected\"";
116 } else if (access_flags & kAccPrivate) {
117 return "\"private\"";
118 } else {
119 return "\"package\"";
120 }
121}
122
123/*
124 * Counts the number of '1' bits in a word.
125 */
126static int CountOnes(uint32_t val) {
127 val = val - ((val >> 1) & 0x55555555);
128 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
129 return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
130}
131
132/*
133 * Creates a new string with human-readable access flags.
134 *
135 * In the base language the access_flags fields are type uint16_t; in Dalvik they're uint32_t.
136 */
137static char* CreateAccessFlagStr(uint32_t flags, AccessFor for_what) {
138 static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
139 {
140 "PUBLIC", /* 0x00001 */
141 "PRIVATE", /* 0x00002 */
142 "PROTECTED", /* 0x00004 */
143 "STATIC", /* 0x00008 */
144 "FINAL", /* 0x00010 */
145 "?", /* 0x00020 */
146 "?", /* 0x00040 */
147 "?", /* 0x00080 */
148 "?", /* 0x00100 */
149 "INTERFACE", /* 0x00200 */
150 "ABSTRACT", /* 0x00400 */
151 "?", /* 0x00800 */
152 "SYNTHETIC", /* 0x01000 */
153 "ANNOTATION", /* 0x02000 */
154 "ENUM", /* 0x04000 */
155 "?", /* 0x08000 */
156 "VERIFIED", /* 0x10000 */
157 "OPTIMIZED", /* 0x20000 */
158 }, {
159 "PUBLIC", /* 0x00001 */
160 "PRIVATE", /* 0x00002 */
161 "PROTECTED", /* 0x00004 */
162 "STATIC", /* 0x00008 */
163 "FINAL", /* 0x00010 */
164 "SYNCHRONIZED", /* 0x00020 */
165 "BRIDGE", /* 0x00040 */
166 "VARARGS", /* 0x00080 */
167 "NATIVE", /* 0x00100 */
168 "?", /* 0x00200 */
169 "ABSTRACT", /* 0x00400 */
170 "STRICT", /* 0x00800 */
171 "SYNTHETIC", /* 0x01000 */
172 "?", /* 0x02000 */
173 "?", /* 0x04000 */
174 "MIRANDA", /* 0x08000 */
175 "CONSTRUCTOR", /* 0x10000 */
176 "DECLARED_SYNCHRONIZED", /* 0x20000 */
177 }, {
178 "PUBLIC", /* 0x00001 */
179 "PRIVATE", /* 0x00002 */
180 "PROTECTED", /* 0x00004 */
181 "STATIC", /* 0x00008 */
182 "FINAL", /* 0x00010 */
183 "?", /* 0x00020 */
184 "VOLATILE", /* 0x00040 */
185 "TRANSIENT", /* 0x00080 */
186 "?", /* 0x00100 */
187 "?", /* 0x00200 */
188 "?", /* 0x00400 */
189 "?", /* 0x00800 */
190 "SYNTHETIC", /* 0x01000 */
191 "?", /* 0x02000 */
192 "ENUM", /* 0x04000 */
193 "?", /* 0x08000 */
194 "?", /* 0x10000 */
195 "?", /* 0x20000 */
196 },
197 };
198
199 // Allocate enough storage to hold the expected number of strings,
200 // plus a space between each. We over-allocate, using the longest
201 // string above as the base metric.
202 const int kLongest = 21; // The strlen of longest string above.
203 const int count = CountOnes(flags);
204 char* str;
205 char* cp;
206 cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
207
208 for (int i = 0; i < kNumFlags; i++) {
209 if (flags & 0x01) {
210 const char* accessStr = kAccessStrings[for_what][i];
211 const int len = strlen(accessStr);
212 if (cp != str) {
213 *cp++ = ' ';
214 }
215 memcpy(cp, accessStr, len);
216 cp += len;
217 }
218 flags >>= 1;
219 } // for
220
221 *cp = '\0';
222 return str;
223}
224
225static std::string GetSignatureForProtoId(const dex_ir::ProtoId* proto) {
226 if (proto == nullptr) {
227 return "<no signature>";
228 }
229
David Sehr7629f602016-08-07 16:01:51 -0700230 std::string result("(");
Jeff Haoa8621002016-10-04 18:13:44 +0000231 const dex_ir::TypeList* type_list = proto->Parameters();
232 if (type_list != nullptr) {
233 for (const dex_ir::TypeId* type_id : *type_list->GetTypeList()) {
234 result += type_id->GetStringId()->Data();
235 }
David Sehr7629f602016-08-07 16:01:51 -0700236 }
237 result += ")";
238 result += proto->ReturnType()->GetStringId()->Data();
239 return result;
240}
241
242/*
243 * Copies character data from "data" to "out", converting non-ASCII values
244 * to fprintf format chars or an ASCII filler ('.' or '?').
245 *
246 * The output buffer must be able to hold (2*len)+1 bytes. The result is
247 * NULL-terminated.
248 */
249static void Asciify(char* out, const unsigned char* data, size_t len) {
Andreas Gampec74d9cb2018-09-20 13:44:44 -0700250 for (; len != 0u; --len) {
David Sehr7629f602016-08-07 16:01:51 -0700251 if (*data < 0x20) {
252 // Could do more here, but we don't need them yet.
253 switch (*data) {
254 case '\0':
255 *out++ = '\\';
256 *out++ = '0';
257 break;
258 case '\n':
259 *out++ = '\\';
260 *out++ = 'n';
261 break;
262 default:
263 *out++ = '.';
264 break;
265 } // switch
266 } else if (*data >= 0x80) {
267 *out++ = '?';
268 } else {
269 *out++ = *data;
270 }
271 data++;
272 } // while
273 *out = '\0';
274}
275
276/*
277 * Dumps a string value with some escape characters.
278 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800279static void DumpEscapedString(const char* p, FILE* out_file) {
280 fputs("\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700281 for (; *p; p++) {
282 switch (*p) {
283 case '\\':
Jeff Haoea7c6292016-11-14 18:10:16 -0800284 fputs("\\\\", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700285 break;
286 case '\"':
Jeff Haoea7c6292016-11-14 18:10:16 -0800287 fputs("\\\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700288 break;
289 case '\t':
Jeff Haoea7c6292016-11-14 18:10:16 -0800290 fputs("\\t", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700291 break;
292 case '\n':
Jeff Haoea7c6292016-11-14 18:10:16 -0800293 fputs("\\n", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700294 break;
295 case '\r':
Jeff Haoea7c6292016-11-14 18:10:16 -0800296 fputs("\\r", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700297 break;
298 default:
Jeff Haoea7c6292016-11-14 18:10:16 -0800299 putc(*p, out_file);
David Sehr7629f602016-08-07 16:01:51 -0700300 } // switch
301 } // for
Jeff Haoea7c6292016-11-14 18:10:16 -0800302 fputs("\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700303}
304
305/*
306 * Dumps a string as an XML attribute value.
307 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800308static void DumpXmlAttribute(const char* p, FILE* out_file) {
David Sehr7629f602016-08-07 16:01:51 -0700309 for (; *p; p++) {
310 switch (*p) {
311 case '&':
Jeff Haoea7c6292016-11-14 18:10:16 -0800312 fputs("&amp;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700313 break;
314 case '<':
Jeff Haoea7c6292016-11-14 18:10:16 -0800315 fputs("&lt;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700316 break;
317 case '>':
Jeff Haoea7c6292016-11-14 18:10:16 -0800318 fputs("&gt;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700319 break;
320 case '"':
Jeff Haoea7c6292016-11-14 18:10:16 -0800321 fputs("&quot;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700322 break;
323 case '\t':
Jeff Haoea7c6292016-11-14 18:10:16 -0800324 fputs("&#x9;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700325 break;
326 case '\n':
Jeff Haoea7c6292016-11-14 18:10:16 -0800327 fputs("&#xA;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700328 break;
329 case '\r':
Jeff Haoea7c6292016-11-14 18:10:16 -0800330 fputs("&#xD;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700331 break;
332 default:
Jeff Haoea7c6292016-11-14 18:10:16 -0800333 putc(*p, out_file);
David Sehr7629f602016-08-07 16:01:51 -0700334 } // switch
335 } // for
336}
337
David Sehr7629f602016-08-07 16:01:51 -0700338/*
339 * Helper for dumpInstruction(), which builds the string
340 * representation for the index in the given instruction.
341 * Returns a pointer to a buffer of sufficient size.
342 */
343static std::unique_ptr<char[]> IndexString(dex_ir::Header* header,
344 const Instruction* dec_insn,
345 size_t buf_size) {
346 std::unique_ptr<char[]> buf(new char[buf_size]);
347 // Determine index and width of the string.
348 uint32_t index = 0;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700349 uint32_t secondary_index = dex::kDexNoIndex;
David Sehr7629f602016-08-07 16:01:51 -0700350 uint32_t width = 4;
351 switch (Instruction::FormatOf(dec_insn->Opcode())) {
352 // SOME NOT SUPPORTED:
353 // case Instruction::k20bc:
354 case Instruction::k21c:
355 case Instruction::k35c:
356 // case Instruction::k35ms:
357 case Instruction::k3rc:
358 // case Instruction::k3rms:
359 // case Instruction::k35mi:
360 // case Instruction::k3rmi:
361 index = dec_insn->VRegB();
362 width = 4;
363 break;
364 case Instruction::k31c:
365 index = dec_insn->VRegB();
366 width = 8;
367 break;
368 case Instruction::k22c:
369 // case Instruction::k22cs:
370 index = dec_insn->VRegC();
371 width = 4;
372 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100373 case Instruction::k45cc:
374 case Instruction::k4rcc:
375 index = dec_insn->VRegB();
376 secondary_index = dec_insn->VRegH();
377 width = 4;
David Sehr7639cdc2017-04-15 10:06:21 -0700378 break;
David Sehr7629f602016-08-07 16:01:51 -0700379 default:
380 break;
381 } // switch
382
383 // Determine index type.
384 size_t outSize = 0;
385 switch (Instruction::IndexTypeOf(dec_insn->Opcode())) {
386 case Instruction::kIndexUnknown:
387 // This function should never get called for this type, but do
388 // something sensible here, just to help with debugging.
389 outSize = snprintf(buf.get(), buf_size, "<unknown-index>");
390 break;
391 case Instruction::kIndexNone:
392 // This function should never get called for this type, but do
393 // something sensible here, just to help with debugging.
394 outSize = snprintf(buf.get(), buf_size, "<no-index>");
395 break;
396 case Instruction::kIndexTypeRef:
David Sehr2b5a38f2018-06-14 15:13:04 -0700397 if (index < header->TypeIds().Size()) {
398 const char* tp = header->TypeIds()[index]->GetStringId()->Data();
David Sehr7629f602016-08-07 16:01:51 -0700399 outSize = snprintf(buf.get(), buf_size, "%s // type@%0*x", tp, width, index);
400 } else {
401 outSize = snprintf(buf.get(), buf_size, "<type?> // type@%0*x", width, index);
402 }
403 break;
404 case Instruction::kIndexStringRef:
David Sehr2b5a38f2018-06-14 15:13:04 -0700405 if (index < header->StringIds().Size()) {
406 const char* st = header->StringIds()[index]->Data();
David Sehr7629f602016-08-07 16:01:51 -0700407 outSize = snprintf(buf.get(), buf_size, "\"%s\" // string@%0*x", st, width, index);
408 } else {
409 outSize = snprintf(buf.get(), buf_size, "<string?> // string@%0*x", width, index);
410 }
411 break;
412 case Instruction::kIndexMethodRef:
David Sehr2b5a38f2018-06-14 15:13:04 -0700413 if (index < header->MethodIds().Size()) {
414 dex_ir::MethodId* method_id = header->MethodIds()[index];
David Sehr7629f602016-08-07 16:01:51 -0700415 const char* name = method_id->Name()->Data();
David Sehr72359222016-09-07 13:04:01 -0700416 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -0700417 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
418 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // method@%0*x",
David Sehr72359222016-09-07 13:04:01 -0700419 back_descriptor, name, type_descriptor.c_str(), width, index);
David Sehr7629f602016-08-07 16:01:51 -0700420 } else {
421 outSize = snprintf(buf.get(), buf_size, "<method?> // method@%0*x", width, index);
422 }
423 break;
424 case Instruction::kIndexFieldRef:
David Sehr2b5a38f2018-06-14 15:13:04 -0700425 if (index < header->FieldIds().Size()) {
426 dex_ir::FieldId* field_id = header->FieldIds()[index];
David Sehr7629f602016-08-07 16:01:51 -0700427 const char* name = field_id->Name()->Data();
428 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
429 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
430 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // field@%0*x",
431 back_descriptor, name, type_descriptor, width, index);
432 } else {
433 outSize = snprintf(buf.get(), buf_size, "<field?> // field@%0*x", width, index);
434 }
435 break;
436 case Instruction::kIndexVtableOffset:
437 outSize = snprintf(buf.get(), buf_size, "[%0*x] // vtable #%0*x",
438 width, index, width, index);
439 break;
440 case Instruction::kIndexFieldOffset:
441 outSize = snprintf(buf.get(), buf_size, "[obj+%0*x]", width, index);
442 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100443 case Instruction::kIndexMethodAndProtoRef: {
444 std::string method("<method?>");
445 std::string proto("<proto?>");
David Sehr2b5a38f2018-06-14 15:13:04 -0700446 if (index < header->MethodIds().Size()) {
447 dex_ir::MethodId* method_id = header->MethodIds()[index];
Orion Hodsonb34bb192016-10-18 17:02:58 +0100448 const char* name = method_id->Name()->Data();
449 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
450 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
451 method = StringPrintf("%s.%s:%s", back_descriptor, name, type_descriptor.c_str());
452 }
David Sehr2b5a38f2018-06-14 15:13:04 -0700453 if (secondary_index < header->ProtoIds().Size()) {
454 dex_ir::ProtoId* proto_id = header->ProtoIds()[secondary_index];
Orion Hodsonb34bb192016-10-18 17:02:58 +0100455 proto = GetSignatureForProtoId(proto_id);
456 }
457 outSize = snprintf(buf.get(), buf_size, "%s, %s // method@%0*x, proto@%0*x",
458 method.c_str(), proto.c_str(), width, index, width, secondary_index);
Jeff Haoea7c6292016-11-14 18:10:16 -0800459 }
460 break;
461 // SOME NOT SUPPORTED:
462 // case Instruction::kIndexVaries:
463 // case Instruction::kIndexInlineMethod:
David Sehr7629f602016-08-07 16:01:51 -0700464 default:
465 outSize = snprintf(buf.get(), buf_size, "<?>");
466 break;
467 } // switch
468
469 // Determine success of string construction.
470 if (outSize >= buf_size) {
471 // The buffer wasn't big enough; retry with computed size. Note: snprintf()
472 // doesn't count/ the '\0' as part of its returned size, so we add explicit
473 // space for it here.
474 return IndexString(header, dec_insn, outSize + 1);
475 }
476 return buf;
477}
478
479/*
Jeff Haoea7c6292016-11-14 18:10:16 -0800480 * Dumps encoded annotation.
481 */
482void DexLayout::DumpEncodedAnnotation(dex_ir::EncodedAnnotation* annotation) {
483 fputs(annotation->GetType()->GetStringId()->Data(), out_file_);
484 // Display all name=value pairs.
485 for (auto& subannotation : *annotation->GetAnnotationElements()) {
486 fputc(' ', out_file_);
487 fputs(subannotation->GetName()->Data(), out_file_);
488 fputc('=', out_file_);
489 DumpEncodedValue(subannotation->GetValue());
490 }
491}
492/*
493 * Dumps encoded value.
494 */
495void DexLayout::DumpEncodedValue(const dex_ir::EncodedValue* data) {
496 switch (data->Type()) {
497 case DexFile::kDexAnnotationByte:
498 fprintf(out_file_, "%" PRId8, data->GetByte());
499 break;
500 case DexFile::kDexAnnotationShort:
501 fprintf(out_file_, "%" PRId16, data->GetShort());
502 break;
503 case DexFile::kDexAnnotationChar:
504 fprintf(out_file_, "%" PRIu16, data->GetChar());
505 break;
506 case DexFile::kDexAnnotationInt:
507 fprintf(out_file_, "%" PRId32, data->GetInt());
508 break;
509 case DexFile::kDexAnnotationLong:
510 fprintf(out_file_, "%" PRId64, data->GetLong());
511 break;
512 case DexFile::kDexAnnotationFloat: {
513 fprintf(out_file_, "%g", data->GetFloat());
514 break;
515 }
516 case DexFile::kDexAnnotationDouble: {
517 fprintf(out_file_, "%g", data->GetDouble());
518 break;
519 }
520 case DexFile::kDexAnnotationString: {
521 dex_ir::StringId* string_id = data->GetStringId();
522 if (options_.output_format_ == kOutputPlain) {
523 DumpEscapedString(string_id->Data(), out_file_);
524 } else {
525 DumpXmlAttribute(string_id->Data(), out_file_);
526 }
527 break;
528 }
529 case DexFile::kDexAnnotationType: {
530 dex_ir::TypeId* type_id = data->GetTypeId();
531 fputs(type_id->GetStringId()->Data(), out_file_);
532 break;
533 }
534 case DexFile::kDexAnnotationField:
535 case DexFile::kDexAnnotationEnum: {
536 dex_ir::FieldId* field_id = data->GetFieldId();
537 fputs(field_id->Name()->Data(), out_file_);
538 break;
539 }
540 case DexFile::kDexAnnotationMethod: {
541 dex_ir::MethodId* method_id = data->GetMethodId();
542 fputs(method_id->Name()->Data(), out_file_);
543 break;
544 }
545 case DexFile::kDexAnnotationArray: {
546 fputc('{', out_file_);
547 // Display all elements.
548 for (auto& value : *data->GetEncodedArray()->GetEncodedValues()) {
549 fputc(' ', out_file_);
550 DumpEncodedValue(value.get());
551 }
552 fputs(" }", out_file_);
553 break;
554 }
555 case DexFile::kDexAnnotationAnnotation: {
556 DumpEncodedAnnotation(data->GetEncodedAnnotation());
557 break;
558 }
559 case DexFile::kDexAnnotationNull:
560 fputs("null", out_file_);
561 break;
562 case DexFile::kDexAnnotationBoolean:
563 fputs(StrBool(data->GetBoolean()), out_file_);
564 break;
565 default:
566 fputs("????", out_file_);
567 break;
568 } // switch
569}
570
571/*
572 * Dumps the file header.
573 */
574void DexLayout::DumpFileHeader() {
575 char sanitized[8 * 2 + 1];
Jeff Haoea7c6292016-11-14 18:10:16 -0800576 fprintf(out_file_, "DEX file header:\n");
577 Asciify(sanitized, header_->Magic(), 8);
578 fprintf(out_file_, "magic : '%s'\n", sanitized);
579 fprintf(out_file_, "checksum : %08x\n", header_->Checksum());
580 fprintf(out_file_, "signature : %02x%02x...%02x%02x\n",
581 header_->Signature()[0], header_->Signature()[1],
582 header_->Signature()[DexFile::kSha1DigestSize - 2],
583 header_->Signature()[DexFile::kSha1DigestSize - 1]);
584 fprintf(out_file_, "file_size : %d\n", header_->FileSize());
585 fprintf(out_file_, "header_size : %d\n", header_->HeaderSize());
586 fprintf(out_file_, "link_size : %d\n", header_->LinkSize());
587 fprintf(out_file_, "link_off : %d (0x%06x)\n",
588 header_->LinkOffset(), header_->LinkOffset());
David Sehr2b5a38f2018-06-14 15:13:04 -0700589 fprintf(out_file_, "string_ids_size : %d\n", header_->StringIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800590 fprintf(out_file_, "string_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700591 header_->StringIds().GetOffset(), header_->StringIds().GetOffset());
592 fprintf(out_file_, "type_ids_size : %d\n", header_->TypeIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800593 fprintf(out_file_, "type_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700594 header_->TypeIds().GetOffset(), header_->TypeIds().GetOffset());
595 fprintf(out_file_, "proto_ids_size : %d\n", header_->ProtoIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800596 fprintf(out_file_, "proto_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700597 header_->ProtoIds().GetOffset(), header_->ProtoIds().GetOffset());
598 fprintf(out_file_, "field_ids_size : %d\n", header_->FieldIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800599 fprintf(out_file_, "field_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700600 header_->FieldIds().GetOffset(), header_->FieldIds().GetOffset());
601 fprintf(out_file_, "method_ids_size : %d\n", header_->MethodIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800602 fprintf(out_file_, "method_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700603 header_->MethodIds().GetOffset(), header_->MethodIds().GetOffset());
604 fprintf(out_file_, "class_defs_size : %d\n", header_->ClassDefs().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800605 fprintf(out_file_, "class_defs_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700606 header_->ClassDefs().GetOffset(), header_->ClassDefs().GetOffset());
Jeff Haoea7c6292016-11-14 18:10:16 -0800607 fprintf(out_file_, "data_size : %d\n", header_->DataSize());
608 fprintf(out_file_, "data_off : %d (0x%06x)\n\n",
609 header_->DataOffset(), header_->DataOffset());
610}
611
612/*
613 * Dumps a class_def_item.
614 */
615void DexLayout::DumpClassDef(int idx) {
616 // General class information.
David Sehr2b5a38f2018-06-14 15:13:04 -0700617 dex_ir::ClassDef* class_def = header_->ClassDefs()[idx];
Jeff Haoea7c6292016-11-14 18:10:16 -0800618 fprintf(out_file_, "Class #%d header:\n", idx);
619 fprintf(out_file_, "class_idx : %d\n", class_def->ClassType()->GetIndex());
620 fprintf(out_file_, "access_flags : %d (0x%04x)\n",
621 class_def->GetAccessFlags(), class_def->GetAccessFlags());
622 uint32_t superclass_idx = class_def->Superclass() == nullptr ?
623 DexFile::kDexNoIndex16 : class_def->Superclass()->GetIndex();
624 fprintf(out_file_, "superclass_idx : %d\n", superclass_idx);
625 fprintf(out_file_, "interfaces_off : %d (0x%06x)\n",
626 class_def->InterfacesOffset(), class_def->InterfacesOffset());
627 uint32_t source_file_offset = 0xffffffffU;
628 if (class_def->SourceFile() != nullptr) {
629 source_file_offset = class_def->SourceFile()->GetIndex();
630 }
631 fprintf(out_file_, "source_file_idx : %d\n", source_file_offset);
632 uint32_t annotations_offset = 0;
633 if (class_def->Annotations() != nullptr) {
634 annotations_offset = class_def->Annotations()->GetOffset();
635 }
636 fprintf(out_file_, "annotations_off : %d (0x%06x)\n",
637 annotations_offset, annotations_offset);
638 if (class_def->GetClassData() == nullptr) {
639 fprintf(out_file_, "class_data_off : %d (0x%06x)\n", 0, 0);
640 } else {
641 fprintf(out_file_, "class_data_off : %d (0x%06x)\n",
642 class_def->GetClassData()->GetOffset(), class_def->GetClassData()->GetOffset());
643 }
644
645 // Fields and methods.
646 dex_ir::ClassData* class_data = class_def->GetClassData();
647 if (class_data != nullptr && class_data->StaticFields() != nullptr) {
648 fprintf(out_file_, "static_fields_size : %zu\n", class_data->StaticFields()->size());
649 } else {
650 fprintf(out_file_, "static_fields_size : 0\n");
651 }
652 if (class_data != nullptr && class_data->InstanceFields() != nullptr) {
653 fprintf(out_file_, "instance_fields_size: %zu\n", class_data->InstanceFields()->size());
654 } else {
655 fprintf(out_file_, "instance_fields_size: 0\n");
656 }
657 if (class_data != nullptr && class_data->DirectMethods() != nullptr) {
658 fprintf(out_file_, "direct_methods_size : %zu\n", class_data->DirectMethods()->size());
659 } else {
660 fprintf(out_file_, "direct_methods_size : 0\n");
661 }
662 if (class_data != nullptr && class_data->VirtualMethods() != nullptr) {
663 fprintf(out_file_, "virtual_methods_size: %zu\n", class_data->VirtualMethods()->size());
664 } else {
665 fprintf(out_file_, "virtual_methods_size: 0\n");
666 }
667 fprintf(out_file_, "\n");
668}
669
670/**
671 * Dumps an annotation set item.
672 */
673void DexLayout::DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item) {
674 if (set_item == nullptr || set_item->GetItems()->size() == 0) {
675 fputs(" empty-annotation-set\n", out_file_);
676 return;
677 }
678 for (dex_ir::AnnotationItem* annotation : *set_item->GetItems()) {
679 if (annotation == nullptr) {
680 continue;
681 }
682 fputs(" ", out_file_);
683 switch (annotation->GetVisibility()) {
684 case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", out_file_); break;
685 case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", out_file_); break;
686 case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", out_file_); break;
687 default: fputs("VISIBILITY_UNKNOWN ", out_file_); break;
688 } // switch
689 DumpEncodedAnnotation(annotation->GetAnnotation());
690 fputc('\n', out_file_);
691 }
692}
693
694/*
695 * Dumps class annotations.
696 */
697void DexLayout::DumpClassAnnotations(int idx) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700698 dex_ir::ClassDef* class_def = header_->ClassDefs()[idx];
Jeff Haoea7c6292016-11-14 18:10:16 -0800699 dex_ir::AnnotationsDirectoryItem* annotations_directory = class_def->Annotations();
700 if (annotations_directory == nullptr) {
701 return; // none
702 }
703
704 fprintf(out_file_, "Class #%d annotations:\n", idx);
705
706 dex_ir::AnnotationSetItem* class_set_item = annotations_directory->GetClassAnnotation();
707 dex_ir::FieldAnnotationVector* fields = annotations_directory->GetFieldAnnotations();
708 dex_ir::MethodAnnotationVector* methods = annotations_directory->GetMethodAnnotations();
709 dex_ir::ParameterAnnotationVector* parameters = annotations_directory->GetParameterAnnotations();
710
711 // Annotations on the class itself.
712 if (class_set_item != nullptr) {
713 fprintf(out_file_, "Annotations on class\n");
714 DumpAnnotationSetItem(class_set_item);
715 }
716
717 // Annotations on fields.
718 if (fields != nullptr) {
719 for (auto& field : *fields) {
720 const dex_ir::FieldId* field_id = field->GetFieldId();
721 const uint32_t field_idx = field_id->GetIndex();
722 const char* field_name = field_id->Name()->Data();
723 fprintf(out_file_, "Annotations on field #%u '%s'\n", field_idx, field_name);
724 DumpAnnotationSetItem(field->GetAnnotationSetItem());
725 }
726 }
727
728 // Annotations on methods.
729 if (methods != nullptr) {
730 for (auto& method : *methods) {
731 const dex_ir::MethodId* method_id = method->GetMethodId();
732 const uint32_t method_idx = method_id->GetIndex();
733 const char* method_name = method_id->Name()->Data();
734 fprintf(out_file_, "Annotations on method #%u '%s'\n", method_idx, method_name);
735 DumpAnnotationSetItem(method->GetAnnotationSetItem());
736 }
737 }
738
739 // Annotations on method parameters.
740 if (parameters != nullptr) {
741 for (auto& parameter : *parameters) {
742 const dex_ir::MethodId* method_id = parameter->GetMethodId();
743 const uint32_t method_idx = method_id->GetIndex();
744 const char* method_name = method_id->Name()->Data();
745 fprintf(out_file_, "Annotations on method #%u '%s' parameters\n", method_idx, method_name);
746 uint32_t j = 0;
747 for (dex_ir::AnnotationSetItem* annotation : *parameter->GetAnnotations()->GetItems()) {
748 fprintf(out_file_, "#%u\n", j);
749 DumpAnnotationSetItem(annotation);
750 ++j;
751 }
752 }
753 }
754
755 fputc('\n', out_file_);
756}
757
758/*
759 * Dumps an interface that a class declares to implement.
760 */
761void DexLayout::DumpInterface(const dex_ir::TypeId* type_item, int i) {
762 const char* interface_name = type_item->GetStringId()->Data();
763 if (options_.output_format_ == kOutputPlain) {
764 fprintf(out_file_, " #%d : '%s'\n", i, interface_name);
765 } else {
Orion Hodsonfe42d212018-08-24 14:01:14 +0100766 std::string dot(DescriptorToDot(interface_name));
Jeff Haoea7c6292016-11-14 18:10:16 -0800767 fprintf(out_file_, "<implements name=\"%s\">\n</implements>\n", dot.c_str());
768 }
769}
770
771/*
772 * Dumps the catches table associated with the code.
773 */
774void DexLayout::DumpCatches(const dex_ir::CodeItem* code) {
775 const uint16_t tries_size = code->TriesSize();
776
777 // No catch table.
778 if (tries_size == 0) {
779 fprintf(out_file_, " catches : (none)\n");
780 return;
781 }
782
783 // Dump all table entries.
784 fprintf(out_file_, " catches : %d\n", tries_size);
785 std::vector<std::unique_ptr<const dex_ir::TryItem>>* tries = code->Tries();
786 for (uint32_t i = 0; i < tries_size; i++) {
787 const dex_ir::TryItem* try_item = (*tries)[i].get();
788 const uint32_t start = try_item->StartAddr();
789 const uint32_t end = start + try_item->InsnCount();
790 fprintf(out_file_, " 0x%04x - 0x%04x\n", start, end);
791 for (auto& handler : *try_item->GetHandlers()->GetHandlers()) {
792 const dex_ir::TypeId* type_id = handler->GetTypeId();
793 const char* descriptor = (type_id == nullptr) ? "<any>" : type_id->GetStringId()->Data();
794 fprintf(out_file_, " %s -> 0x%04x\n", descriptor, handler->GetAddress());
795 } // for
796 } // for
797}
798
799/*
David Sehr7629f602016-08-07 16:01:51 -0700800 * Dumps a single instruction.
801 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800802void DexLayout::DumpInstruction(const dex_ir::CodeItem* code,
803 uint32_t code_offset,
804 uint32_t insn_idx,
805 uint32_t insn_width,
806 const Instruction* dec_insn) {
David Sehr7629f602016-08-07 16:01:51 -0700807 // Address of instruction (expressed as byte offset).
808 fprintf(out_file_, "%06x:", code_offset + 0x10 + insn_idx * 2);
809
810 // Dump (part of) raw bytes.
811 const uint16_t* insns = code->Insns();
812 for (uint32_t i = 0; i < 8; i++) {
813 if (i < insn_width) {
814 if (i == 7) {
815 fprintf(out_file_, " ... ");
816 } else {
817 // Print 16-bit value in little-endian order.
818 const uint8_t* bytePtr = (const uint8_t*) &insns[insn_idx + i];
819 fprintf(out_file_, " %02x%02x", bytePtr[0], bytePtr[1]);
820 }
821 } else {
822 fputs(" ", out_file_);
823 }
824 } // for
825
826 // Dump pseudo-instruction or opcode.
827 if (dec_insn->Opcode() == Instruction::NOP) {
828 const uint16_t instr = Get2LE((const uint8_t*) &insns[insn_idx]);
829 if (instr == Instruction::kPackedSwitchSignature) {
830 fprintf(out_file_, "|%04x: packed-switch-data (%d units)", insn_idx, insn_width);
831 } else if (instr == Instruction::kSparseSwitchSignature) {
832 fprintf(out_file_, "|%04x: sparse-switch-data (%d units)", insn_idx, insn_width);
833 } else if (instr == Instruction::kArrayDataSignature) {
834 fprintf(out_file_, "|%04x: array-data (%d units)", insn_idx, insn_width);
835 } else {
836 fprintf(out_file_, "|%04x: nop // spacer", insn_idx);
837 }
838 } else {
839 fprintf(out_file_, "|%04x: %s", insn_idx, dec_insn->Name());
840 }
841
842 // Set up additional argument.
843 std::unique_ptr<char[]> index_buf;
844 if (Instruction::IndexTypeOf(dec_insn->Opcode()) != Instruction::kIndexNone) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800845 index_buf = IndexString(header_, dec_insn, 200);
David Sehr7629f602016-08-07 16:01:51 -0700846 }
847
848 // Dump the instruction.
849 //
850 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
851 //
852 switch (Instruction::FormatOf(dec_insn->Opcode())) {
853 case Instruction::k10x: // op
854 break;
855 case Instruction::k12x: // op vA, vB
856 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
857 break;
858 case Instruction::k11n: // op vA, #+B
859 fprintf(out_file_, " v%d, #int %d // #%x",
860 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint8_t)dec_insn->VRegB());
861 break;
862 case Instruction::k11x: // op vAA
863 fprintf(out_file_, " v%d", dec_insn->VRegA());
864 break;
865 case Instruction::k10t: // op +AA
866 case Instruction::k20t: { // op +AAAA
867 const int32_t targ = (int32_t) dec_insn->VRegA();
868 fprintf(out_file_, " %04x // %c%04x",
869 insn_idx + targ,
870 (targ < 0) ? '-' : '+',
871 (targ < 0) ? -targ : targ);
872 break;
873 }
874 case Instruction::k22x: // op vAA, vBBBB
875 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
876 break;
877 case Instruction::k21t: { // op vAA, +BBBB
878 const int32_t targ = (int32_t) dec_insn->VRegB();
879 fprintf(out_file_, " v%d, %04x // %c%04x", dec_insn->VRegA(),
880 insn_idx + targ,
881 (targ < 0) ? '-' : '+',
882 (targ < 0) ? -targ : targ);
883 break;
884 }
885 case Instruction::k21s: // op vAA, #+BBBB
886 fprintf(out_file_, " v%d, #int %d // #%x",
887 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint16_t)dec_insn->VRegB());
888 break;
889 case Instruction::k21h: // op vAA, #+BBBB0000[00000000]
890 // The printed format varies a bit based on the actual opcode.
891 if (dec_insn->Opcode() == Instruction::CONST_HIGH16) {
892 const int32_t value = dec_insn->VRegB() << 16;
893 fprintf(out_file_, " v%d, #int %d // #%x",
894 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
895 } else {
896 const int64_t value = ((int64_t) dec_insn->VRegB()) << 48;
897 fprintf(out_file_, " v%d, #long %" PRId64 " // #%x",
898 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
899 }
900 break;
901 case Instruction::k21c: // op vAA, thing@BBBB
902 case Instruction::k31c: // op vAA, thing@BBBBBBBB
903 fprintf(out_file_, " v%d, %s", dec_insn->VRegA(), index_buf.get());
904 break;
905 case Instruction::k23x: // op vAA, vBB, vCC
906 fprintf(out_file_, " v%d, v%d, v%d",
907 dec_insn->VRegA(), dec_insn->VRegB(), dec_insn->VRegC());
908 break;
909 case Instruction::k22b: // op vAA, vBB, #+CC
910 fprintf(out_file_, " v%d, v%d, #int %d // #%02x",
911 dec_insn->VRegA(), dec_insn->VRegB(),
912 (int32_t) dec_insn->VRegC(), (uint8_t) dec_insn->VRegC());
913 break;
914 case Instruction::k22t: { // op vA, vB, +CCCC
915 const int32_t targ = (int32_t) dec_insn->VRegC();
916 fprintf(out_file_, " v%d, v%d, %04x // %c%04x",
917 dec_insn->VRegA(), dec_insn->VRegB(),
918 insn_idx + targ,
919 (targ < 0) ? '-' : '+',
920 (targ < 0) ? -targ : targ);
921 break;
922 }
923 case Instruction::k22s: // op vA, vB, #+CCCC
924 fprintf(out_file_, " v%d, v%d, #int %d // #%04x",
925 dec_insn->VRegA(), dec_insn->VRegB(),
926 (int32_t) dec_insn->VRegC(), (uint16_t) dec_insn->VRegC());
927 break;
928 case Instruction::k22c: // op vA, vB, thing@CCCC
929 // NOT SUPPORTED:
930 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
931 fprintf(out_file_, " v%d, v%d, %s",
932 dec_insn->VRegA(), dec_insn->VRegB(), index_buf.get());
933 break;
934 case Instruction::k30t:
935 fprintf(out_file_, " #%08x", dec_insn->VRegA());
936 break;
937 case Instruction::k31i: { // op vAA, #+BBBBBBBB
938 // This is often, but not always, a float.
939 union {
940 float f;
941 uint32_t i;
942 } conv;
943 conv.i = dec_insn->VRegB();
944 fprintf(out_file_, " v%d, #float %g // #%08x",
945 dec_insn->VRegA(), conv.f, dec_insn->VRegB());
946 break;
947 }
948 case Instruction::k31t: // op vAA, offset +BBBBBBBB
949 fprintf(out_file_, " v%d, %08x // +%08x",
950 dec_insn->VRegA(), insn_idx + dec_insn->VRegB(), dec_insn->VRegB());
951 break;
952 case Instruction::k32x: // op vAAAA, vBBBB
953 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
954 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100955 case Instruction::k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
956 case Instruction::k45cc: { // op {vC, vD, vE, vF, vG}, meth@BBBB, proto@HHHH
David Sehr7629f602016-08-07 16:01:51 -0700957 // NOT SUPPORTED:
958 // case Instruction::k35ms: // [opt] invoke-virtual+super
959 // case Instruction::k35mi: // [opt] inline invoke
960 uint32_t arg[Instruction::kMaxVarArgRegs];
961 dec_insn->GetVarArgs(arg);
962 fputs(" {", out_file_);
963 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
964 if (i == 0) {
965 fprintf(out_file_, "v%d", arg[i]);
966 } else {
967 fprintf(out_file_, ", v%d", arg[i]);
968 }
969 } // for
970 fprintf(out_file_, "}, %s", index_buf.get());
971 break;
972 }
Orion Hodsonb34bb192016-10-18 17:02:58 +0100973 case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
974 case Instruction::k4rcc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB, proto@HHHH
David Sehr7629f602016-08-07 16:01:51 -0700975 // NOT SUPPORTED:
976 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
977 // case Instruction::k3rmi: // [opt] execute-inline/range
978 {
979 // This doesn't match the "dx" output when some of the args are
980 // 64-bit values -- dx only shows the first register.
981 fputs(" {", out_file_);
982 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
983 if (i == 0) {
984 fprintf(out_file_, "v%d", dec_insn->VRegC() + i);
985 } else {
986 fprintf(out_file_, ", v%d", dec_insn->VRegC() + i);
987 }
988 } // for
989 fprintf(out_file_, "}, %s", index_buf.get());
990 }
991 break;
992 case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB
993 // This is often, but not always, a double.
994 union {
995 double d;
996 uint64_t j;
997 } conv;
998 conv.j = dec_insn->WideVRegB();
999 fprintf(out_file_, " v%d, #double %g // #%016" PRIx64,
1000 dec_insn->VRegA(), conv.d, dec_insn->WideVRegB());
1001 break;
1002 }
1003 // NOT SUPPORTED:
1004 // case Instruction::k00x: // unknown op or breakpoint
1005 // break;
1006 default:
1007 fprintf(out_file_, " ???");
1008 break;
1009 } // switch
1010
1011 fputc('\n', out_file_);
1012}
1013
1014/*
1015 * Dumps a bytecode disassembly.
1016 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001017void DexLayout::DumpBytecodes(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001018 dex_ir::MethodId* method_id = header_->MethodIds()[idx];
David Sehr7629f602016-08-07 16:01:51 -07001019 const char* name = method_id->Name()->Data();
David Sehr72359222016-09-07 13:04:01 -07001020 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -07001021 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1022
1023 // Generate header.
Orion Hodsonfe42d212018-08-24 14:01:14 +01001024 std::string dot(DescriptorToDot(back_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001025 fprintf(out_file_, "%06x: |[%06x] %s.%s:%s\n",
David Sehr72359222016-09-07 13:04:01 -07001026 code_offset, code_offset, dot.c_str(), name, type_descriptor.c_str());
David Sehr7629f602016-08-07 16:01:51 -07001027
1028 // Iterate over all instructions.
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001029 for (const DexInstructionPcPair& inst : code->Instructions()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001030 const uint32_t insn_width = inst->SizeInCodeUnits();
David Sehr7629f602016-08-07 16:01:51 -07001031 if (insn_width == 0) {
Andreas Gampe221d9812018-01-22 17:48:56 -08001032 LOG(WARNING) << "GLITCH: zero-width instruction at idx=0x" << std::hex << inst.DexPc();
David Sehr7629f602016-08-07 16:01:51 -07001033 break;
1034 }
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001035 DumpInstruction(code, code_offset, inst.DexPc(), insn_width, &inst.Inst());
David Sehr7629f602016-08-07 16:01:51 -07001036 } // for
1037}
1038
1039/*
David Sehraa6abb02017-10-12 08:25:11 -07001040 * Lookup functions.
1041 */
David Sehr2b5a38f2018-06-14 15:13:04 -07001042static const char* StringDataByIdx(uint32_t idx, dex_ir::Header* header) {
1043 dex_ir::StringId* string_id = header->GetStringIdOrNullPtr(idx);
David Sehraa6abb02017-10-12 08:25:11 -07001044 if (string_id == nullptr) {
1045 return nullptr;
1046 }
1047 return string_id->Data();
1048}
1049
David Sehr2b5a38f2018-06-14 15:13:04 -07001050static const char* StringDataByTypeIdx(uint16_t idx, dex_ir::Header* header) {
1051 dex_ir::TypeId* type_id = header->GetTypeIdOrNullPtr(idx);
David Sehraa6abb02017-10-12 08:25:11 -07001052 if (type_id == nullptr) {
1053 return nullptr;
1054 }
1055 dex_ir::StringId* string_id = type_id->GetStringId();
1056 if (string_id == nullptr) {
1057 return nullptr;
1058 }
1059 return string_id->Data();
1060}
1061
1062
1063/*
David Sehr7629f602016-08-07 16:01:51 -07001064 * Dumps code of a method.
1065 */
David Sehraa6abb02017-10-12 08:25:11 -07001066void DexLayout::DumpCode(uint32_t idx,
1067 const dex_ir::CodeItem* code,
1068 uint32_t code_offset,
1069 const char* declaring_class_descriptor,
1070 const char* method_name,
1071 bool is_static,
1072 const dex_ir::ProtoId* proto) {
David Sehr7629f602016-08-07 16:01:51 -07001073 fprintf(out_file_, " registers : %d\n", code->RegistersSize());
1074 fprintf(out_file_, " ins : %d\n", code->InsSize());
1075 fprintf(out_file_, " outs : %d\n", code->OutsSize());
1076 fprintf(out_file_, " insns size : %d 16-bit code units\n",
1077 code->InsnsSize());
1078
1079 // Bytecode disassembly, if requested.
1080 if (options_.disassemble_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001081 DumpBytecodes(idx, code, code_offset);
David Sehr7629f602016-08-07 16:01:51 -07001082 }
1083
1084 // Try-catch blocks.
1085 DumpCatches(code);
1086
1087 // Positions and locals table in the debug info.
David Sehraa6abb02017-10-12 08:25:11 -07001088 dex_ir::DebugInfoItem* debug_info = code->DebugInfo();
David Sehr7629f602016-08-07 16:01:51 -07001089 fprintf(out_file_, " positions : \n");
David Sehraa6abb02017-10-12 08:25:11 -07001090 if (debug_info != nullptr) {
1091 DexFile::DecodeDebugPositionInfo(debug_info->GetDebugInfo(),
1092 [this](uint32_t idx) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001093 return StringDataByIdx(idx, this->header_);
David Sehraa6abb02017-10-12 08:25:11 -07001094 },
Mathieu Chartier3e2e1232018-09-11 12:35:30 -07001095 [&](const DexFile::PositionInfo& entry) {
1096 fprintf(out_file_,
1097 " 0x%04x line=%d\n",
1098 entry.address_,
1099 entry.line_);
1100 return false;
1101 });
David Sehraa6abb02017-10-12 08:25:11 -07001102 }
David Sehr7629f602016-08-07 16:01:51 -07001103 fprintf(out_file_, " locals : \n");
David Sehraa6abb02017-10-12 08:25:11 -07001104 if (debug_info != nullptr) {
1105 std::vector<const char*> arg_descriptors;
1106 const dex_ir::TypeList* parameters = proto->Parameters();
1107 if (parameters != nullptr) {
1108 const dex_ir::TypeIdVector* parameter_type_vector = parameters->GetTypeList();
1109 if (parameter_type_vector != nullptr) {
1110 for (const dex_ir::TypeId* type_id : *parameter_type_vector) {
1111 arg_descriptors.push_back(type_id->GetStringId()->Data());
1112 }
1113 }
1114 }
1115 DexFile::DecodeDebugLocalInfo(debug_info->GetDebugInfo(),
1116 "DexLayout in-memory",
1117 declaring_class_descriptor,
1118 arg_descriptors,
1119 method_name,
1120 is_static,
1121 code->RegistersSize(),
1122 code->InsSize(),
1123 code->InsnsSize(),
1124 [this](uint32_t idx) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001125 return StringDataByIdx(idx, this->header_);
David Sehraa6abb02017-10-12 08:25:11 -07001126 },
1127 [this](uint32_t idx) {
1128 return
1129 StringDataByTypeIdx(dchecked_integral_cast<uint16_t>(idx),
David Sehr2b5a38f2018-06-14 15:13:04 -07001130 this->header_);
David Sehraa6abb02017-10-12 08:25:11 -07001131 },
Mathieu Chartiere5afbf32018-09-12 17:51:54 -07001132 [&](const DexFile::LocalInfo& entry) {
1133 const char* signature =
1134 entry.signature_ != nullptr ? entry.signature_ : "";
1135 fprintf(out_file_,
1136 " 0x%04x - 0x%04x reg=%d %s %s %s\n",
1137 entry.start_address_,
1138 entry.end_address_,
1139 entry.reg_,
1140 entry.name_,
1141 entry.descriptor_,
1142 signature);
1143 });
David Sehraa6abb02017-10-12 08:25:11 -07001144 }
David Sehr7629f602016-08-07 16:01:51 -07001145}
1146
1147/*
1148 * Dumps a method.
1149 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001150void DexLayout::DumpMethod(uint32_t idx, uint32_t flags, const dex_ir::CodeItem* code, int i) {
David Sehr7629f602016-08-07 16:01:51 -07001151 // Bail for anything private if export only requested.
1152 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1153 return;
1154 }
1155
David Sehr2b5a38f2018-06-14 15:13:04 -07001156 dex_ir::MethodId* method_id = header_->MethodIds()[idx];
David Sehr7629f602016-08-07 16:01:51 -07001157 const char* name = method_id->Name()->Data();
1158 char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str());
1159 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1160 char* access_str = CreateAccessFlagStr(flags, kAccessForMethod);
1161
1162 if (options_.output_format_ == kOutputPlain) {
1163 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1164 fprintf(out_file_, " name : '%s'\n", name);
1165 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1166 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
1167 if (code == nullptr) {
1168 fprintf(out_file_, " code : (none)\n");
1169 } else {
1170 fprintf(out_file_, " code -\n");
David Sehraa6abb02017-10-12 08:25:11 -07001171 DumpCode(idx,
1172 code,
1173 code->GetOffset(),
1174 back_descriptor,
1175 name,
1176 (flags & kAccStatic) != 0,
1177 method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -07001178 }
1179 if (options_.disassemble_) {
1180 fputc('\n', out_file_);
1181 }
1182 } else if (options_.output_format_ == kOutputXml) {
1183 const bool constructor = (name[0] == '<');
1184
1185 // Method name and prototype.
1186 if (constructor) {
Orion Hodsonfe42d212018-08-24 14:01:14 +01001187 std::string dot(DescriptorClassToName(back_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001188 fprintf(out_file_, "<constructor name=\"%s\"\n", dot.c_str());
Orion Hodsonfe42d212018-08-24 14:01:14 +01001189 dot = DescriptorToDot(back_descriptor);
David Sehr7629f602016-08-07 16:01:51 -07001190 fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
1191 } else {
1192 fprintf(out_file_, "<method name=\"%s\"\n", name);
1193 const char* return_type = strrchr(type_descriptor, ')');
1194 if (return_type == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08001195 LOG(ERROR) << "bad method type descriptor '" << type_descriptor << "'";
David Sehr7629f602016-08-07 16:01:51 -07001196 goto bail;
1197 }
Orion Hodsonfe42d212018-08-24 14:01:14 +01001198 std::string dot(DescriptorToDot(return_type + 1));
David Sehr7629f602016-08-07 16:01:51 -07001199 fprintf(out_file_, " return=\"%s\"\n", dot.c_str());
1200 fprintf(out_file_, " abstract=%s\n", QuotedBool((flags & kAccAbstract) != 0));
1201 fprintf(out_file_, " native=%s\n", QuotedBool((flags & kAccNative) != 0));
1202 fprintf(out_file_, " synchronized=%s\n", QuotedBool(
1203 (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0));
1204 }
1205
1206 // Additional method flags.
1207 fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
1208 fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
1209 // The "deprecated=" not knowable w/o parsing annotations.
1210 fprintf(out_file_, " visibility=%s\n>\n", QuotedVisibility(flags));
1211
1212 // Parameters.
1213 if (type_descriptor[0] != '(') {
Andreas Gampe221d9812018-01-22 17:48:56 -08001214 LOG(ERROR) << "ERROR: bad descriptor '" << type_descriptor << "'";
David Sehr7629f602016-08-07 16:01:51 -07001215 goto bail;
1216 }
1217 char* tmp_buf = reinterpret_cast<char*>(malloc(strlen(type_descriptor) + 1));
1218 const char* base = type_descriptor + 1;
1219 int arg_num = 0;
1220 while (*base != ')') {
1221 char* cp = tmp_buf;
1222 while (*base == '[') {
1223 *cp++ = *base++;
1224 }
1225 if (*base == 'L') {
1226 // Copy through ';'.
1227 do {
1228 *cp = *base++;
1229 } while (*cp++ != ';');
1230 } else {
1231 // Primitive char, copy it.
1232 if (strchr("ZBCSIFJD", *base) == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08001233 LOG(ERROR) << "ERROR: bad method signature '" << base << "'";
David Sehr7629f602016-08-07 16:01:51 -07001234 break; // while
1235 }
1236 *cp++ = *base++;
1237 }
1238 // Null terminate and display.
1239 *cp++ = '\0';
Orion Hodsonfe42d212018-08-24 14:01:14 +01001240 std::string dot(DescriptorToDot(tmp_buf));
David Sehr7629f602016-08-07 16:01:51 -07001241 fprintf(out_file_, "<parameter name=\"arg%d\" type=\"%s\">\n"
1242 "</parameter>\n", arg_num++, dot.c_str());
1243 } // while
1244 free(tmp_buf);
1245 if (constructor) {
1246 fprintf(out_file_, "</constructor>\n");
1247 } else {
1248 fprintf(out_file_, "</method>\n");
1249 }
1250 }
1251
1252 bail:
1253 free(type_descriptor);
1254 free(access_str);
1255}
1256
1257/*
1258 * Dumps a static (class) field.
1259 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001260void DexLayout::DumpSField(uint32_t idx, uint32_t flags, int i, dex_ir::EncodedValue* init) {
David Sehr7629f602016-08-07 16:01:51 -07001261 // Bail for anything private if export only requested.
1262 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1263 return;
1264 }
1265
David Sehr2b5a38f2018-06-14 15:13:04 -07001266 dex_ir::FieldId* field_id = header_->FieldIds()[idx];
David Sehr7629f602016-08-07 16:01:51 -07001267 const char* name = field_id->Name()->Data();
1268 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
1269 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
1270 char* access_str = CreateAccessFlagStr(flags, kAccessForField);
1271
1272 if (options_.output_format_ == kOutputPlain) {
1273 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1274 fprintf(out_file_, " name : '%s'\n", name);
1275 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1276 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
1277 if (init != nullptr) {
1278 fputs(" value : ", out_file_);
1279 DumpEncodedValue(init);
1280 fputs("\n", out_file_);
1281 }
1282 } else if (options_.output_format_ == kOutputXml) {
1283 fprintf(out_file_, "<field name=\"%s\"\n", name);
Orion Hodsonfe42d212018-08-24 14:01:14 +01001284 std::string dot(DescriptorToDot(type_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001285 fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
1286 fprintf(out_file_, " transient=%s\n", QuotedBool((flags & kAccTransient) != 0));
1287 fprintf(out_file_, " volatile=%s\n", QuotedBool((flags & kAccVolatile) != 0));
1288 // The "value=" is not knowable w/o parsing annotations.
1289 fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
1290 fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
1291 // The "deprecated=" is not knowable w/o parsing annotations.
1292 fprintf(out_file_, " visibility=%s\n", QuotedVisibility(flags));
1293 if (init != nullptr) {
1294 fputs(" value=\"", out_file_);
1295 DumpEncodedValue(init);
1296 fputs("\"\n", out_file_);
1297 }
1298 fputs(">\n</field>\n", out_file_);
1299 }
1300
1301 free(access_str);
1302}
1303
1304/*
1305 * Dumps an instance field.
1306 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001307void DexLayout::DumpIField(uint32_t idx, uint32_t flags, int i) {
1308 DumpSField(idx, flags, i, nullptr);
David Sehr7629f602016-08-07 16:01:51 -07001309}
1310
1311/*
David Sehr7629f602016-08-07 16:01:51 -07001312 * Dumps the class.
1313 *
1314 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1315 *
1316 * If "*last_package" is nullptr or does not match the current class' package,
1317 * the value will be replaced with a newly-allocated string.
1318 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001319void DexLayout::DumpClass(int idx, char** last_package) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001320 dex_ir::ClassDef* class_def = header_->ClassDefs()[idx];
David Sehr7629f602016-08-07 16:01:51 -07001321 // Omitting non-public class.
1322 if (options_.exports_only_ && (class_def->GetAccessFlags() & kAccPublic) == 0) {
1323 return;
1324 }
1325
1326 if (options_.show_section_headers_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001327 DumpClassDef(idx);
David Sehr7629f602016-08-07 16:01:51 -07001328 }
1329
1330 if (options_.show_annotations_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001331 DumpClassAnnotations(idx);
David Sehr7629f602016-08-07 16:01:51 -07001332 }
1333
David Sehr7629f602016-08-07 16:01:51 -07001334 // For the XML output, show the package name. Ideally we'd gather
1335 // up the classes, sort them, and dump them alphabetically so the
1336 // package name wouldn't jump around, but that's not a great plan
1337 // for something that needs to run on the device.
David Sehr2b5a38f2018-06-14 15:13:04 -07001338 const char* class_descriptor = header_->ClassDefs()[idx]->ClassType()->GetStringId()->Data();
David Sehr7629f602016-08-07 16:01:51 -07001339 if (!(class_descriptor[0] == 'L' &&
1340 class_descriptor[strlen(class_descriptor)-1] == ';')) {
1341 // Arrays and primitives should not be defined explicitly. Keep going?
Andreas Gampe221d9812018-01-22 17:48:56 -08001342 LOG(ERROR) << "Malformed class name '" << class_descriptor << "'";
David Sehr7629f602016-08-07 16:01:51 -07001343 } else if (options_.output_format_ == kOutputXml) {
1344 char* mangle = strdup(class_descriptor + 1);
1345 mangle[strlen(mangle)-1] = '\0';
1346
1347 // Reduce to just the package name.
1348 char* last_slash = strrchr(mangle, '/');
1349 if (last_slash != nullptr) {
1350 *last_slash = '\0';
1351 } else {
1352 *mangle = '\0';
1353 }
1354
1355 for (char* cp = mangle; *cp != '\0'; cp++) {
1356 if (*cp == '/') {
1357 *cp = '.';
1358 }
1359 } // for
1360
1361 if (*last_package == nullptr || strcmp(mangle, *last_package) != 0) {
1362 // Start of a new package.
1363 if (*last_package != nullptr) {
1364 fprintf(out_file_, "</package>\n");
1365 }
1366 fprintf(out_file_, "<package name=\"%s\"\n>\n", mangle);
1367 free(*last_package);
1368 *last_package = mangle;
1369 } else {
1370 free(mangle);
1371 }
1372 }
1373
1374 // General class information.
1375 char* access_str = CreateAccessFlagStr(class_def->GetAccessFlags(), kAccessForClass);
1376 const char* superclass_descriptor = nullptr;
1377 if (class_def->Superclass() != nullptr) {
1378 superclass_descriptor = class_def->Superclass()->GetStringId()->Data();
1379 }
1380 if (options_.output_format_ == kOutputPlain) {
1381 fprintf(out_file_, "Class #%d -\n", idx);
1382 fprintf(out_file_, " Class descriptor : '%s'\n", class_descriptor);
1383 fprintf(out_file_, " Access flags : 0x%04x (%s)\n",
1384 class_def->GetAccessFlags(), access_str);
1385 if (superclass_descriptor != nullptr) {
1386 fprintf(out_file_, " Superclass : '%s'\n", superclass_descriptor);
1387 }
1388 fprintf(out_file_, " Interfaces -\n");
1389 } else {
Orion Hodsonfe42d212018-08-24 14:01:14 +01001390 std::string dot(DescriptorClassToName(class_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001391 fprintf(out_file_, "<class name=\"%s\"\n", dot.c_str());
1392 if (superclass_descriptor != nullptr) {
Orion Hodsonfe42d212018-08-24 14:01:14 +01001393 dot = DescriptorToDot(superclass_descriptor);
David Sehr7629f602016-08-07 16:01:51 -07001394 fprintf(out_file_, " extends=\"%s\"\n", dot.c_str());
1395 }
1396 fprintf(out_file_, " interface=%s\n",
1397 QuotedBool((class_def->GetAccessFlags() & kAccInterface) != 0));
1398 fprintf(out_file_, " abstract=%s\n",
1399 QuotedBool((class_def->GetAccessFlags() & kAccAbstract) != 0));
1400 fprintf(out_file_, " static=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccStatic) != 0));
1401 fprintf(out_file_, " final=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccFinal) != 0));
1402 // The "deprecated=" not knowable w/o parsing annotations.
1403 fprintf(out_file_, " visibility=%s\n", QuotedVisibility(class_def->GetAccessFlags()));
1404 fprintf(out_file_, ">\n");
1405 }
1406
1407 // Interfaces.
Jeff Haocc829592017-03-14 16:13:39 -07001408 const dex_ir::TypeList* interfaces = class_def->Interfaces();
David Sehr853a8e12016-09-01 13:03:50 -07001409 if (interfaces != nullptr) {
Jeff Haocc829592017-03-14 16:13:39 -07001410 const dex_ir::TypeIdVector* interfaces_vector = interfaces->GetTypeList();
1411 for (uint32_t i = 0; i < interfaces_vector->size(); i++) {
1412 DumpInterface((*interfaces_vector)[i], i);
David Sehr853a8e12016-09-01 13:03:50 -07001413 } // for
1414 }
David Sehr7629f602016-08-07 16:01:51 -07001415
1416 // Fields and methods.
1417 dex_ir::ClassData* class_data = class_def->GetClassData();
1418 // Prepare data for static fields.
Jeff Hao3ab96b42016-09-09 18:35:01 -07001419 dex_ir::EncodedArrayItem* static_values = class_def->StaticValues();
1420 dex_ir::EncodedValueVector* encoded_values =
1421 static_values == nullptr ? nullptr : static_values->GetEncodedValues();
1422 const uint32_t encoded_values_size = (encoded_values == nullptr) ? 0 : encoded_values->size();
David Sehr7629f602016-08-07 16:01:51 -07001423
1424 // Static fields.
1425 if (options_.output_format_ == kOutputPlain) {
1426 fprintf(out_file_, " Static fields -\n");
1427 }
David Sehr853a8e12016-09-01 13:03:50 -07001428 if (class_data != nullptr) {
1429 dex_ir::FieldItemVector* static_fields = class_data->StaticFields();
1430 if (static_fields != nullptr) {
1431 for (uint32_t i = 0; i < static_fields->size(); i++) {
David Sehrd83437c2018-06-11 14:06:23 -07001432 DumpSField((*static_fields)[i].GetFieldId()->GetIndex(),
1433 (*static_fields)[i].GetAccessFlags(),
David Sehr853a8e12016-09-01 13:03:50 -07001434 i,
Jeff Hao3ab96b42016-09-09 18:35:01 -07001435 i < encoded_values_size ? (*encoded_values)[i].get() : nullptr);
David Sehr853a8e12016-09-01 13:03:50 -07001436 } // for
1437 }
1438 }
David Sehr7629f602016-08-07 16:01:51 -07001439
1440 // Instance fields.
1441 if (options_.output_format_ == kOutputPlain) {
1442 fprintf(out_file_, " Instance fields -\n");
1443 }
David Sehr853a8e12016-09-01 13:03:50 -07001444 if (class_data != nullptr) {
1445 dex_ir::FieldItemVector* instance_fields = class_data->InstanceFields();
1446 if (instance_fields != nullptr) {
1447 for (uint32_t i = 0; i < instance_fields->size(); i++) {
David Sehrd83437c2018-06-11 14:06:23 -07001448 DumpIField((*instance_fields)[i].GetFieldId()->GetIndex(),
1449 (*instance_fields)[i].GetAccessFlags(),
David Sehr853a8e12016-09-01 13:03:50 -07001450 i);
1451 } // for
1452 }
1453 }
David Sehr7629f602016-08-07 16:01:51 -07001454
1455 // Direct methods.
1456 if (options_.output_format_ == kOutputPlain) {
1457 fprintf(out_file_, " Direct methods -\n");
1458 }
David Sehr853a8e12016-09-01 13:03:50 -07001459 if (class_data != nullptr) {
1460 dex_ir::MethodItemVector* direct_methods = class_data->DirectMethods();
1461 if (direct_methods != nullptr) {
1462 for (uint32_t i = 0; i < direct_methods->size(); i++) {
David Sehrd83437c2018-06-11 14:06:23 -07001463 DumpMethod((*direct_methods)[i].GetMethodId()->GetIndex(),
1464 (*direct_methods)[i].GetAccessFlags(),
1465 (*direct_methods)[i].GetCodeItem(),
David Sehr853a8e12016-09-01 13:03:50 -07001466 i);
1467 } // for
1468 }
1469 }
David Sehr7629f602016-08-07 16:01:51 -07001470
1471 // Virtual methods.
1472 if (options_.output_format_ == kOutputPlain) {
1473 fprintf(out_file_, " Virtual methods -\n");
1474 }
David Sehr853a8e12016-09-01 13:03:50 -07001475 if (class_data != nullptr) {
1476 dex_ir::MethodItemVector* virtual_methods = class_data->VirtualMethods();
1477 if (virtual_methods != nullptr) {
1478 for (uint32_t i = 0; i < virtual_methods->size(); i++) {
David Sehrd83437c2018-06-11 14:06:23 -07001479 DumpMethod((*virtual_methods)[i].GetMethodId()->GetIndex(),
1480 (*virtual_methods)[i].GetAccessFlags(),
1481 (*virtual_methods)[i].GetCodeItem(),
David Sehr853a8e12016-09-01 13:03:50 -07001482 i);
1483 } // for
1484 }
1485 }
David Sehr7629f602016-08-07 16:01:51 -07001486
1487 // End of class.
1488 if (options_.output_format_ == kOutputPlain) {
1489 const char* file_name = "unknown";
1490 if (class_def->SourceFile() != nullptr) {
1491 file_name = class_def->SourceFile()->Data();
1492 }
1493 const dex_ir::StringId* source_file = class_def->SourceFile();
1494 fprintf(out_file_, " source_file_idx : %d (%s)\n\n",
Jeff Hao3ab96b42016-09-09 18:35:01 -07001495 source_file == nullptr ? 0xffffffffU : source_file->GetIndex(), file_name);
David Sehr7629f602016-08-07 16:01:51 -07001496 } else if (options_.output_format_ == kOutputXml) {
1497 fprintf(out_file_, "</class>\n");
1498 }
1499
1500 free(access_str);
1501}
1502
Jeff Haoea7c6292016-11-14 18:10:16 -08001503void DexLayout::DumpDexFile() {
David Sehr7629f602016-08-07 16:01:51 -07001504 // Headers.
1505 if (options_.show_file_headers_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001506 DumpFileHeader();
David Sehr7629f602016-08-07 16:01:51 -07001507 }
1508
1509 // Open XML context.
1510 if (options_.output_format_ == kOutputXml) {
1511 fprintf(out_file_, "<api>\n");
1512 }
1513
1514 // Iterate over all classes.
1515 char* package = nullptr;
David Sehr2b5a38f2018-06-14 15:13:04 -07001516 const uint32_t class_defs_size = header_->ClassDefs().Size();
David Sehr7629f602016-08-07 16:01:51 -07001517 for (uint32_t i = 0; i < class_defs_size; i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001518 DumpClass(i, &package);
David Sehr7629f602016-08-07 16:01:51 -07001519 } // for
1520
1521 // Free the last package allocated.
1522 if (package != nullptr) {
1523 fprintf(out_file_, "</package>\n");
1524 free(package);
1525 }
1526
1527 // Close XML context.
1528 if (options_.output_format_ == kOutputXml) {
1529 fprintf(out_file_, "</api>\n");
1530 }
Jeff Haoea7c6292016-11-14 18:10:16 -08001531}
Jeff Hao3ab96b42016-09-09 18:35:01 -07001532
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001533void DexLayout::LayoutClassDefsAndClassData(const DexFile* dex_file) {
Jeff Hao042e8982016-10-19 11:17:11 -07001534 std::vector<dex_ir::ClassDef*> new_class_def_order;
David Sehr2b5a38f2018-06-14 15:13:04 -07001535 for (auto& class_def : header_->ClassDefs()) {
Jeff Hao042e8982016-10-19 11:17:11 -07001536 dex::TypeIndex type_idx(class_def->ClassType()->GetIndex());
1537 if (info_->ContainsClass(*dex_file, type_idx)) {
1538 new_class_def_order.push_back(class_def.get());
1539 }
1540 }
David Sehr2b5a38f2018-06-14 15:13:04 -07001541 for (auto& class_def : header_->ClassDefs()) {
Jeff Hao042e8982016-10-19 11:17:11 -07001542 dex::TypeIndex type_idx(class_def->ClassType()->GetIndex());
1543 if (!info_->ContainsClass(*dex_file, type_idx)) {
1544 new_class_def_order.push_back(class_def.get());
1545 }
1546 }
Jeff Haoe17f5892017-02-23 16:14:04 -08001547 std::unordered_set<dex_ir::ClassData*> visited_class_data;
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001548 size_t class_data_index = 0;
David Sehr2b5a38f2018-06-14 15:13:04 -07001549 auto& class_datas = header_->ClassDatas();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001550 for (dex_ir::ClassDef* class_def : new_class_def_order) {
Jeff Haoe17f5892017-02-23 16:14:04 -08001551 dex_ir::ClassData* class_data = class_def->GetClassData();
1552 if (class_data != nullptr && visited_class_data.find(class_data) == visited_class_data.end()) {
Jeff Haoe17f5892017-02-23 16:14:04 -08001553 visited_class_data.insert(class_data);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001554 // Overwrite the existing vector with the new ordering, note that the sets of objects are
1555 // equivalent, but the order changes. This is why this is not a memory leak.
1556 // TODO: Consider cleaning this up with a shared_ptr.
1557 class_datas[class_data_index].release();
1558 class_datas[class_data_index].reset(class_data);
1559 ++class_data_index;
Jeff Hao042e8982016-10-19 11:17:11 -07001560 }
1561 }
David Sehr2b5a38f2018-06-14 15:13:04 -07001562 CHECK_EQ(class_data_index, class_datas.Size());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001563
Mathieu Chartier2c4b0842017-12-13 11:49:51 -08001564 if (DexLayout::kChangeClassDefOrder) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001565 // This currently produces dex files that violate the spec since the super class class_def is
1566 // supposed to occur before any subclasses.
David Sehr2b5a38f2018-06-14 15:13:04 -07001567 dex_ir::CollectionVector<dex_ir::ClassDef>& class_defs = header_->ClassDefs();
1568 CHECK_EQ(new_class_def_order.size(), class_defs.Size());
1569 for (size_t i = 0; i < class_defs.Size(); ++i) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001570 // Overwrite the existing vector with the new ordering, note that the sets of objects are
1571 // equivalent, but the order changes. This is why this is not a memory leak.
1572 // TODO: Consider cleaning this up with a shared_ptr.
1573 class_defs[i].release();
1574 class_defs[i].reset(new_class_def_order[i]);
1575 }
1576 }
Jeff Hao042e8982016-10-19 11:17:11 -07001577}
1578
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001579void DexLayout::LayoutStringData(const DexFile* dex_file) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001580 const size_t num_strings = header_->StringIds().Size();
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001581 std::vector<bool> is_shorty(num_strings, false);
1582 std::vector<bool> from_hot_method(num_strings, false);
David Sehr2b5a38f2018-06-14 15:13:04 -07001583 for (auto& class_def : header_->ClassDefs()) {
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001584 // 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 -07001585 // as hot. Add its super class and interfaces as well, which can be used during initialization.
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001586 const bool is_profile_class =
1587 info_->ContainsClass(*dex_file, dex::TypeIndex(class_def->ClassType()->GetIndex()));
1588 if (is_profile_class) {
1589 from_hot_method[class_def->ClassType()->GetStringId()->GetIndex()] = true;
Jeff Haoacc83d72017-07-06 17:51:01 -07001590 const dex_ir::TypeId* superclass = class_def->Superclass();
1591 if (superclass != nullptr) {
1592 from_hot_method[superclass->GetStringId()->GetIndex()] = true;
1593 }
1594 const dex_ir::TypeList* interfaces = class_def->Interfaces();
1595 if (interfaces != nullptr) {
1596 for (const dex_ir::TypeId* interface_type : *interfaces->GetTypeList()) {
1597 from_hot_method[interface_type->GetStringId()->GetIndex()] = true;
1598 }
1599 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001600 }
1601 dex_ir::ClassData* data = class_def->GetClassData();
1602 if (data == nullptr) {
1603 continue;
1604 }
1605 for (size_t i = 0; i < 2; ++i) {
1606 for (auto& method : *(i == 0 ? data->DirectMethods() : data->VirtualMethods())) {
David Sehrd83437c2018-06-11 14:06:23 -07001607 const dex_ir::MethodId* method_id = method.GetMethodId();
1608 dex_ir::CodeItem* code_item = method.GetCodeItem();
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001609 if (code_item == nullptr) {
1610 continue;
1611 }
1612 const bool is_clinit = is_profile_class &&
David Sehrd83437c2018-06-11 14:06:23 -07001613 (method.GetAccessFlags() & kAccConstructor) != 0 &&
1614 (method.GetAccessFlags() & kAccStatic) != 0;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001615 const bool method_executed = is_clinit ||
Mathieu Chartiere46f3a82017-06-19 19:54:12 -07001616 info_->GetMethodHotness(MethodReference(dex_file, method_id->GetIndex())).IsInProfile();
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001617 if (!method_executed) {
1618 continue;
1619 }
1620 is_shorty[method_id->Proto()->Shorty()->GetIndex()] = true;
1621 dex_ir::CodeFixups* fixups = code_item->GetCodeFixups();
1622 if (fixups == nullptr) {
1623 continue;
1624 }
Jeff Haoacc83d72017-07-06 17:51:01 -07001625 // Add const-strings.
Vladimir Marko219cb902017-12-07 16:20:39 +00001626 for (dex_ir::StringId* id : fixups->StringIds()) {
Jeff Haoacc83d72017-07-06 17:51:01 -07001627 from_hot_method[id->GetIndex()] = true;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001628 }
Jeff Haoacc83d72017-07-06 17:51:01 -07001629 // Add field classes, names, and types.
Vladimir Marko219cb902017-12-07 16:20:39 +00001630 for (dex_ir::FieldId* id : fixups->FieldIds()) {
Jeff Haoacc83d72017-07-06 17:51:01 -07001631 // TODO: Only visit field ids from static getters and setters.
1632 from_hot_method[id->Class()->GetStringId()->GetIndex()] = true;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001633 from_hot_method[id->Name()->GetIndex()] = true;
1634 from_hot_method[id->Type()->GetStringId()->GetIndex()] = true;
1635 }
Jeff Haoacc83d72017-07-06 17:51:01 -07001636 // For clinits, add referenced method classes, names, and protos.
1637 if (is_clinit) {
Vladimir Marko219cb902017-12-07 16:20:39 +00001638 for (dex_ir::MethodId* id : fixups->MethodIds()) {
Jeff Haoacc83d72017-07-06 17:51:01 -07001639 from_hot_method[id->Class()->GetStringId()->GetIndex()] = true;
1640 from_hot_method[id->Name()->GetIndex()] = true;
1641 is_shorty[id->Proto()->Shorty()->GetIndex()] = true;
1642 }
1643 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001644 }
1645 }
1646 }
1647 // Sort string data by specified order.
1648 std::vector<dex_ir::StringId*> string_ids;
David Sehr2b5a38f2018-06-14 15:13:04 -07001649 for (auto& string_id : header_->StringIds()) {
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001650 string_ids.push_back(string_id.get());
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001651 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001652 std::sort(string_ids.begin(),
1653 string_ids.end(),
1654 [&is_shorty, &from_hot_method](const dex_ir::StringId* a,
1655 const dex_ir::StringId* b) {
1656 const bool a_is_hot = from_hot_method[a->GetIndex()];
1657 const bool b_is_hot = from_hot_method[b->GetIndex()];
1658 if (a_is_hot != b_is_hot) {
1659 return a_is_hot < b_is_hot;
1660 }
1661 // After hot methods are partitioned, subpartition shorties.
1662 const bool a_is_shorty = is_shorty[a->GetIndex()];
1663 const bool b_is_shorty = is_shorty[b->GetIndex()];
1664 if (a_is_shorty != b_is_shorty) {
1665 return a_is_shorty < b_is_shorty;
1666 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001667 // Order by index by default.
1668 return a->GetIndex() < b->GetIndex();
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001669 });
David Sehr2b5a38f2018-06-14 15:13:04 -07001670 auto& string_datas = header_->StringDatas();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001671 // Now we know what order we want the string data, reorder them.
1672 size_t data_index = 0;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001673 for (dex_ir::StringId* string_id : string_ids) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001674 string_datas[data_index].release();
1675 string_datas[data_index].reset(string_id->DataItem());
1676 ++data_index;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001677 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001678 if (kIsDebugBuild) {
1679 std::unordered_set<dex_ir::StringData*> visited;
1680 for (const std::unique_ptr<dex_ir::StringData>& data : string_datas) {
1681 visited.insert(data.get());
1682 }
David Sehr2b5a38f2018-06-14 15:13:04 -07001683 for (auto& string_id : header_->StringIds()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001684 CHECK(visited.find(string_id->DataItem()) != visited.end());
1685 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001686 }
David Sehr2b5a38f2018-06-14 15:13:04 -07001687 CHECK_EQ(data_index, string_datas.Size());
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001688}
1689
Jeff Haoe17f5892017-02-23 16:14:04 -08001690// Orders code items according to specified class data ordering.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001691void DexLayout::LayoutCodeItems(const DexFile* dex_file) {
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001692 static constexpr InvokeType invoke_types[] = {
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001693 kDirect,
1694 kVirtual
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001695 };
1696
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001697 std::unordered_map<dex_ir::CodeItem*, LayoutType>& code_item_layout =
1698 layout_hotness_info_.code_item_layout_;
1699
1700 // Assign hotness flags to all code items.
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001701 for (InvokeType invoke_type : invoke_types) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001702 for (auto& class_def : header_->ClassDefs()) {
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001703 const bool is_profile_class =
1704 info_->ContainsClass(*dex_file, dex::TypeIndex(class_def->ClassType()->GetIndex()));
1705
1706 // Skip classes that are not defined in this dex file.
1707 dex_ir::ClassData* class_data = class_def->GetClassData();
1708 if (class_data == nullptr) {
1709 continue;
Jeff Haoe17f5892017-02-23 16:14:04 -08001710 }
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001711 for (auto& method : *(invoke_type == InvokeType::kDirect
1712 ? class_data->DirectMethods()
1713 : class_data->VirtualMethods())) {
David Sehrd83437c2018-06-11 14:06:23 -07001714 const dex_ir::MethodId *method_id = method.GetMethodId();
1715 dex_ir::CodeItem *code_item = method.GetCodeItem();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001716 if (code_item == nullptr) {
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001717 continue;
1718 }
1719 // Separate executed methods (clinits and profiled methods) from unexecuted methods.
David Sehrd83437c2018-06-11 14:06:23 -07001720 const bool is_clinit = (method.GetAccessFlags() & kAccConstructor) != 0 &&
1721 (method.GetAccessFlags() & kAccStatic) != 0;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001722 const bool is_startup_clinit = is_profile_class && is_clinit;
1723 using Hotness = ProfileCompilationInfo::MethodHotness;
1724 Hotness hotness = info_->GetMethodHotness(MethodReference(dex_file, method_id->GetIndex()));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001725 LayoutType state = LayoutType::kLayoutTypeUnused;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001726 if (hotness.IsHot()) {
1727 // Hot code is compiled, maybe one day it won't be accessed. So lay it out together for
1728 // now.
Mathieu Chartier120aa282017-08-05 16:03:03 -07001729 state = LayoutType::kLayoutTypeHot;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001730 } else if (is_startup_clinit || hotness.GetFlags() == Hotness::kFlagStartup) {
1731 // Startup clinit or a method that only has the startup flag.
Mathieu Chartier120aa282017-08-05 16:03:03 -07001732 state = LayoutType::kLayoutTypeStartupOnly;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001733 } else if (is_clinit) {
Mathieu Chartier120aa282017-08-05 16:03:03 -07001734 state = LayoutType::kLayoutTypeUsedOnce;
Mathieu Chartiere46f3a82017-06-19 19:54:12 -07001735 } else if (hotness.IsInProfile()) {
Mathieu Chartier120aa282017-08-05 16:03:03 -07001736 state = LayoutType::kLayoutTypeSometimesUsed;
Jeff Hao206cbaa2017-06-07 19:11:01 -07001737 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001738 auto it = code_item_layout.emplace(code_item, state);
1739 if (!it.second) {
1740 LayoutType& layout_type = it.first->second;
1741 // Already exists, merge the hotness.
1742 layout_type = MergeLayoutType(layout_type, state);
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001743 }
1744 }
1745 }
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001746 }
Jeff Hao042e8982016-10-19 11:17:11 -07001747
David Sehr2b5a38f2018-06-14 15:13:04 -07001748 const auto& code_items = header_->CodeItems();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001749 if (VLOG_IS_ON(dex)) {
1750 size_t layout_count[static_cast<size_t>(LayoutType::kLayoutTypeCount)] = {};
1751 for (const std::unique_ptr<dex_ir::CodeItem>& code_item : code_items) {
1752 auto it = code_item_layout.find(code_item.get());
1753 DCHECK(it != code_item_layout.end());
1754 ++layout_count[static_cast<size_t>(it->second)];
1755 }
1756 for (size_t i = 0; i < static_cast<size_t>(LayoutType::kLayoutTypeCount); ++i) {
1757 LOG(INFO) << "Code items in category " << i << " count=" << layout_count[i];
Jeff Haoe17f5892017-02-23 16:14:04 -08001758 }
1759 }
Jeff Hao042e8982016-10-19 11:17:11 -07001760
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001761 // Sort the code items vector by new layout. The writing process will take care of calculating
1762 // all the offsets. Stable sort to preserve any existing locality that might be there.
1763 std::stable_sort(code_items.begin(),
1764 code_items.end(),
1765 [&](const std::unique_ptr<dex_ir::CodeItem>& a,
1766 const std::unique_ptr<dex_ir::CodeItem>& b) {
1767 auto it_a = code_item_layout.find(a.get());
1768 auto it_b = code_item_layout.find(b.get());
1769 DCHECK(it_a != code_item_layout.end());
1770 DCHECK(it_b != code_item_layout.end());
1771 const LayoutType layout_type_a = it_a->second;
1772 const LayoutType layout_type_b = it_b->second;
1773 return layout_type_a < layout_type_b;
1774 });
Jeff Hao042e8982016-10-19 11:17:11 -07001775}
1776
1777void DexLayout::LayoutOutputFile(const DexFile* dex_file) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001778 LayoutStringData(dex_file);
1779 LayoutClassDefsAndClassData(dex_file);
1780 LayoutCodeItems(dex_file);
Jeff Hao042e8982016-10-19 11:17:11 -07001781}
1782
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001783bool DexLayout::OutputDexFile(const DexFile* input_dex_file,
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001784 bool compute_offsets,
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001785 std::unique_ptr<DexContainer>* dex_container,
1786 std::string* error_msg) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001787 const std::string& dex_file_location = input_dex_file->GetLocation();
Jeff Haoea7c6292016-11-14 18:10:16 -08001788 std::unique_ptr<File> new_file;
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001789 // If options_.output_dex_directory_ is non null, we are outputting to a file.
1790 if (options_.output_dex_directory_ != nullptr) {
Jeff Haoa8621002016-10-04 18:13:44 +00001791 std::string output_location(options_.output_dex_directory_);
Mathieu Chartier41468402018-08-29 11:39:00 -07001792 const size_t last_slash = dex_file_location.rfind('/');
Jeff Haoea7c6292016-11-14 18:10:16 -08001793 std::string dex_file_directory = dex_file_location.substr(0, last_slash + 1);
1794 if (output_location == dex_file_directory) {
1795 output_location = dex_file_location + ".new";
Jeff Haoea7c6292016-11-14 18:10:16 -08001796 } else {
Mathieu Chartier41468402018-08-29 11:39:00 -07001797 if (!output_location.empty() && output_location.back() != '/') {
1798 output_location += "/";
1799 }
1800 const size_t separator = dex_file_location.rfind('!');
1801 if (separator != std::string::npos) {
1802 output_location += dex_file_location.substr(separator + 1);
1803 } else {
1804 output_location += "classes.dex";
1805 }
Jeff Haoea7c6292016-11-14 18:10:16 -08001806 }
1807 new_file.reset(OS::CreateEmptyFile(output_location.c_str()));
Jeff Hao3ba51e82017-04-12 16:14:54 -07001808 if (new_file == nullptr) {
1809 LOG(ERROR) << "Could not create dex writer output file: " << output_location;
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001810 return false;
Jeff Hao3ba51e82017-04-12 16:14:54 -07001811 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001812 }
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001813 if (!DexWriter::Output(this, dex_container, compute_offsets, error_msg)) {
1814 return false;
1815 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001816 if (new_file != nullptr) {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001817 DexContainer* const container = dex_container->get();
1818 DexContainer::Section* const main_section = container->GetMainSection();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001819 if (!new_file->WriteFully(main_section->Begin(), main_section->Size())) {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001820 LOG(ERROR) << "Failed to write main section for dex file " << dex_file_location;
1821 new_file->Erase();
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001822 return false;
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001823 }
1824 DexContainer::Section* const data_section = container->GetDataSection();
1825 if (!new_file->WriteFully(data_section->Begin(), data_section->Size())) {
1826 LOG(ERROR) << "Failed to write data section for dex file " << dex_file_location;
David Sehr7639cdc2017-04-15 10:06:21 -07001827 new_file->Erase();
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001828 return false;
David Sehr7639cdc2017-04-15 10:06:21 -07001829 }
Jeff Haoea7c6292016-11-14 18:10:16 -08001830 UNUSED(new_file->FlushCloseOrErase());
1831 }
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001832 return true;
Jeff Haoea7c6292016-11-14 18:10:16 -08001833}
1834
1835/*
1836 * Dumps the requested sections of the file.
1837 */
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001838bool DexLayout::ProcessDexFile(const char* file_name,
Jeff Haoea7c6292016-11-14 18:10:16 -08001839 const DexFile* dex_file,
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001840 size_t dex_file_index,
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001841 std::unique_ptr<DexContainer>* dex_container,
1842 std::string* error_msg) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001843 const bool has_output_container = dex_container != nullptr;
1844 const bool output = options_.output_dex_directory_ != nullptr || has_output_container;
1845
David Sehr2b5a38f2018-06-14 15:13:04 -07001846 // Try to avoid eagerly assigning offsets to find bugs since Offset will abort if the offset
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001847 // is unassigned.
1848 bool eagerly_assign_offsets = false;
1849 if (options_.visualize_pattern_ || options_.show_section_statistics_ || options_.dump_) {
1850 // These options required the offsets for dumping purposes.
1851 eagerly_assign_offsets = true;
1852 }
Mathieu Chartier75175552018-01-25 11:23:01 -08001853 std::unique_ptr<dex_ir::Header> header(dex_ir::DexIrBuilder(*dex_file,
1854 eagerly_assign_offsets,
1855 GetOptions()));
Jeff Haoea7c6292016-11-14 18:10:16 -08001856 SetHeader(header.get());
1857
1858 if (options_.verbose_) {
1859 fprintf(out_file_, "Opened '%s', DEX version '%.3s'\n",
1860 file_name, dex_file->GetHeader().magic_ + 4);
1861 }
1862
1863 if (options_.visualize_pattern_) {
1864 VisualizeDexLayout(header_, dex_file, dex_file_index, info_);
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001865 return true;
Jeff Haoea7c6292016-11-14 18:10:16 -08001866 }
1867
David Sehr93357492017-03-09 08:02:44 -08001868 if (options_.show_section_statistics_) {
1869 ShowDexSectionStatistics(header_, dex_file_index);
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001870 return true;
David Sehr93357492017-03-09 08:02:44 -08001871 }
1872
Jeff Haoea7c6292016-11-14 18:10:16 -08001873 // Dump dex file.
1874 if (options_.dump_) {
1875 DumpDexFile();
1876 }
1877
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001878 // In case we are outputting to a file, keep it open so we can verify.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001879 if (output) {
1880 // Layout information about what strings and code items are hot. Used by the writing process
1881 // to generate the sections that are stored in the oat file.
1882 bool do_layout = info_ != nullptr;
1883 if (do_layout) {
Jeff Hao042e8982016-10-19 11:17:11 -07001884 LayoutOutputFile(dex_file);
1885 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001886 // The output needs a dex container, use a temporary one.
1887 std::unique_ptr<DexContainer> temp_container;
1888 if (dex_container == nullptr) {
1889 dex_container = &temp_container;
1890 }
Mathieu Chartier21cf2582018-01-08 17:09:48 -08001891 // If we didn't set the offsets eagerly, we definitely need to compute them here.
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001892 if (!OutputDexFile(dex_file, do_layout || !eagerly_assign_offsets, dex_container, error_msg)) {
1893 return false;
1894 }
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001895
1896 // Clear header before verifying to reduce peak RAM usage.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001897 const size_t file_size = header_->FileSize();
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001898 header.reset();
1899
1900 // Verify the output dex file's structure, only enabled by default for debug builds.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001901 if (options_.verify_output_ && has_output_container) {
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001902 std::string location = "memory mapped file for " + std::string(file_name);
Mathieu Chartier8740c662018-01-11 14:50:02 -08001903 // Dex file verifier cannot handle compact dex.
1904 bool verify = options_.compact_dex_level_ == CompactDexLevel::kCompactDexLevelNone;
Mathieu Chartier818cb802018-05-11 05:30:16 +00001905 const ArtDexFileLoader dex_file_loader;
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001906 DexContainer::Section* const main_section = (*dex_container)->GetMainSection();
1907 DexContainer::Section* const data_section = (*dex_container)->GetDataSection();
1908 DCHECK_EQ(file_size, main_section->Size())
1909 << main_section->Size() << " " << data_section->Size();
David Sehr013fd802018-01-11 22:55:24 -08001910 std::unique_ptr<const DexFile> output_dex_file(
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001911 dex_file_loader.OpenWithDataSection(
1912 main_section->Begin(),
1913 main_section->Size(),
1914 data_section->Begin(),
1915 data_section->Size(),
1916 location,
1917 /* checksum */ 0,
1918 /*oat_dex_file*/ nullptr,
1919 verify,
1920 /*verify_checksum*/ false,
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001921 error_msg));
1922 CHECK(output_dex_file != nullptr) << "Failed to re-open output file:" << *error_msg;
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001923
1924 // Do IR-level comparison between input and output. This check ignores potential differences
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001925 // due to layout, so offsets are not checked. Instead, it checks the data contents of each
1926 // item.
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001927 //
1928 // Regenerate output IR to catch any bugs that might happen during writing.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001929 std::unique_ptr<dex_ir::Header> output_header(
1930 dex_ir::DexIrBuilder(*output_dex_file,
Mathieu Chartier75175552018-01-25 11:23:01 -08001931 /*eagerly_assign_offsets*/ true,
1932 GetOptions()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001933 std::unique_ptr<dex_ir::Header> orig_header(
1934 dex_ir::DexIrBuilder(*dex_file,
Mathieu Chartier75175552018-01-25 11:23:01 -08001935 /*eagerly_assign_offsets*/ true,
1936 GetOptions()));
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001937 CHECK(VerifyOutputDexFile(output_header.get(), orig_header.get(), error_msg)) << *error_msg;
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001938 }
Jeff Hao3ab96b42016-09-09 18:35:01 -07001939 }
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001940 return true;
David Sehr7629f602016-08-07 16:01:51 -07001941}
1942
1943/*
1944 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
1945 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001946int DexLayout::ProcessFile(const char* file_name) {
David Sehr7629f602016-08-07 16:01:51 -07001947 if (options_.verbose_) {
1948 fprintf(out_file_, "Processing '%s'...\n", file_name);
1949 }
1950
1951 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
1952 // all of which are Zip archives with "classes.dex" inside.
1953 const bool verify_checksum = !options_.ignore_bad_checksum_;
1954 std::string error_msg;
Mathieu Chartier818cb802018-05-11 05:30:16 +00001955 const ArtDexFileLoader dex_file_loader;
David Sehr7629f602016-08-07 16:01:51 -07001956 std::vector<std::unique_ptr<const DexFile>> dex_files;
Mathieu Chartier818cb802018-05-11 05:30:16 +00001957 if (!dex_file_loader.Open(
1958 file_name, file_name, /* verify */ true, verify_checksum, &error_msg, &dex_files)) {
David Sehr7629f602016-08-07 16:01:51 -07001959 // Display returned error message to user. Note that this error behavior
1960 // differs from the error messages shown by the original Dalvik dexdump.
Andreas Gampe221d9812018-01-22 17:48:56 -08001961 LOG(ERROR) << error_msg;
David Sehr7629f602016-08-07 16:01:51 -07001962 return -1;
1963 }
1964
1965 // Success. Either report checksum verification or process
1966 // all dex files found in given file.
1967 if (options_.checksum_only_) {
1968 fprintf(out_file_, "Checksum verified\n");
1969 } else {
1970 for (size_t i = 0; i < dex_files.size(); i++) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001971 // Pass in a null container to avoid output by default.
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001972 if (!ProcessDexFile(file_name,
1973 dex_files[i].get(),
1974 i,
1975 /*dex_container*/ nullptr,
1976 &error_msg)) {
1977 LOG(WARNING) << "Failed to run dex file " << i << " in " << file_name << " : " << error_msg;
1978 }
David Sehr7629f602016-08-07 16:01:51 -07001979 }
1980 }
1981 return 0;
1982}
1983
1984} // namespace art