blob: 32122ebf935afed7e25c4c2c70366cb8fe1734fb [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 Brazdildcfa89b2018-10-31 11:04:10 +000037#include "base/hiddenapi_flags.h"
David Sehr79e26072018-04-06 17:58:50 -070038#include "base/mem_map.h"
David Sehrc431b9d2018-03-02 12:01:51 -080039#include "base/os.h"
40#include "base/utils.h"
Mathieu Chartier818cb802018-05-11 05:30:16 +000041#include "dex/art_dex_file_loader.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080042#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080043#include "dex/dex_file-inl.h"
44#include "dex/dex_file_layout.h"
45#include "dex/dex_file_loader.h"
46#include "dex/dex_file_types.h"
47#include "dex/dex_file_verifier.h"
48#include "dex/dex_instruction-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070049#include "dex_ir_builder.h"
Jeff Haoec7f1a92017-03-13 16:24:24 -070050#include "dex_verify.h"
David Sehrcdcfde72016-09-26 07:44:04 -070051#include "dex_visualize.h"
Jeff Haoa8621002016-10-04 18:13:44 +000052#include "dex_writer.h"
David Sehr82d046e2018-04-23 08:14:19 -070053#include "profile/profile_compilation_info.h"
David Sehr7629f602016-08-07 16:01:51 -070054
55namespace art {
56
Andreas Gampe46ee31b2016-12-14 10:11:49 -080057using android::base::StringPrintf;
58
David Sehr7629f602016-08-07 16:01:51 -070059/*
David Sehr7629f602016-08-07 16:01:51 -070060 * Flags for use with createAccessFlagStr().
61 */
62enum AccessFor {
63 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
64};
65const int kNumFlags = 18;
66
67/*
68 * Gets 2 little-endian bytes.
69 */
70static inline uint16_t Get2LE(unsigned char const* src) {
71 return src[0] | (src[1] << 8);
72}
73
74/*
75 * Converts the class name portion of a type descriptor to human-readable
76 * "dotted" form. For example, "Ljava/lang/String;" becomes "String".
77 */
Orion Hodsonfe42d212018-08-24 14:01:14 +010078static std::string DescriptorClassToName(const char* str) {
David Sehr7629f602016-08-07 16:01:51 -070079 std::string descriptor(str);
80 // Reduce to just the class name prefix.
81 size_t last_slash = descriptor.rfind('/');
82 if (last_slash == std::string::npos) {
83 last_slash = 0;
84 }
85 // Start past the '/' or 'L'.
86 last_slash++;
87
88 // Copy class name over, trimming trailing ';'.
89 size_t size = descriptor.size() - 1 - last_slash;
90 std::string result(descriptor.substr(last_slash, size));
91
David Sehr7629f602016-08-07 16:01:51 -070092 return result;
93}
94
95/*
96 * Returns string representing the boolean value.
97 */
98static const char* StrBool(bool val) {
99 return val ? "true" : "false";
100}
101
102/*
103 * Returns a quoted string representing the boolean value.
104 */
105static const char* QuotedBool(bool val) {
106 return val ? "\"true\"" : "\"false\"";
107}
108
109/*
110 * Returns a quoted string representing the access flags.
111 */
112static const char* QuotedVisibility(uint32_t access_flags) {
113 if (access_flags & kAccPublic) {
114 return "\"public\"";
115 } else if (access_flags & kAccProtected) {
116 return "\"protected\"";
117 } else if (access_flags & kAccPrivate) {
118 return "\"private\"";
119 } else {
120 return "\"package\"";
121 }
122}
123
124/*
125 * Counts the number of '1' bits in a word.
126 */
127static int CountOnes(uint32_t val) {
128 val = val - ((val >> 1) & 0x55555555);
129 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
130 return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
131}
132
133/*
134 * Creates a new string with human-readable access flags.
135 *
136 * In the base language the access_flags fields are type uint16_t; in Dalvik they're uint32_t.
137 */
138static char* CreateAccessFlagStr(uint32_t flags, AccessFor for_what) {
139 static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
140 {
141 "PUBLIC", /* 0x00001 */
142 "PRIVATE", /* 0x00002 */
143 "PROTECTED", /* 0x00004 */
144 "STATIC", /* 0x00008 */
145 "FINAL", /* 0x00010 */
146 "?", /* 0x00020 */
147 "?", /* 0x00040 */
148 "?", /* 0x00080 */
149 "?", /* 0x00100 */
150 "INTERFACE", /* 0x00200 */
151 "ABSTRACT", /* 0x00400 */
152 "?", /* 0x00800 */
153 "SYNTHETIC", /* 0x01000 */
154 "ANNOTATION", /* 0x02000 */
155 "ENUM", /* 0x04000 */
156 "?", /* 0x08000 */
157 "VERIFIED", /* 0x10000 */
158 "OPTIMIZED", /* 0x20000 */
159 }, {
160 "PUBLIC", /* 0x00001 */
161 "PRIVATE", /* 0x00002 */
162 "PROTECTED", /* 0x00004 */
163 "STATIC", /* 0x00008 */
164 "FINAL", /* 0x00010 */
165 "SYNCHRONIZED", /* 0x00020 */
166 "BRIDGE", /* 0x00040 */
167 "VARARGS", /* 0x00080 */
168 "NATIVE", /* 0x00100 */
169 "?", /* 0x00200 */
170 "ABSTRACT", /* 0x00400 */
171 "STRICT", /* 0x00800 */
172 "SYNTHETIC", /* 0x01000 */
173 "?", /* 0x02000 */
174 "?", /* 0x04000 */
175 "MIRANDA", /* 0x08000 */
176 "CONSTRUCTOR", /* 0x10000 */
177 "DECLARED_SYNCHRONIZED", /* 0x20000 */
178 }, {
179 "PUBLIC", /* 0x00001 */
180 "PRIVATE", /* 0x00002 */
181 "PROTECTED", /* 0x00004 */
182 "STATIC", /* 0x00008 */
183 "FINAL", /* 0x00010 */
184 "?", /* 0x00020 */
185 "VOLATILE", /* 0x00040 */
186 "TRANSIENT", /* 0x00080 */
187 "?", /* 0x00100 */
188 "?", /* 0x00200 */
189 "?", /* 0x00400 */
190 "?", /* 0x00800 */
191 "SYNTHETIC", /* 0x01000 */
192 "?", /* 0x02000 */
193 "ENUM", /* 0x04000 */
194 "?", /* 0x08000 */
195 "?", /* 0x10000 */
196 "?", /* 0x20000 */
197 },
198 };
199
200 // Allocate enough storage to hold the expected number of strings,
201 // plus a space between each. We over-allocate, using the longest
202 // string above as the base metric.
203 const int kLongest = 21; // The strlen of longest string above.
204 const int count = CountOnes(flags);
205 char* str;
206 char* cp;
207 cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
208
209 for (int i = 0; i < kNumFlags; i++) {
210 if (flags & 0x01) {
211 const char* accessStr = kAccessStrings[for_what][i];
212 const int len = strlen(accessStr);
213 if (cp != str) {
214 *cp++ = ' ';
215 }
216 memcpy(cp, accessStr, len);
217 cp += len;
218 }
219 flags >>= 1;
220 } // for
221
222 *cp = '\0';
223 return str;
224}
225
David Brazdildcfa89b2018-10-31 11:04:10 +0000226static std::string GetHiddenapiFlagStr(uint32_t hiddenapi_flags) {
227 std::string api_list(hiddenapi::ApiList::FromDexFlags(hiddenapi_flags).GetName());
228 std::transform(api_list.begin(), api_list.end(), api_list.begin(), ::toupper);
229 return api_list;
David Brazdil20c765f2018-10-27 21:45:15 +0000230}
231
David Sehr7629f602016-08-07 16:01:51 -0700232static std::string GetSignatureForProtoId(const dex_ir::ProtoId* proto) {
233 if (proto == nullptr) {
234 return "<no signature>";
235 }
236
David Sehr7629f602016-08-07 16:01:51 -0700237 std::string result("(");
Jeff Haoa8621002016-10-04 18:13:44 +0000238 const dex_ir::TypeList* type_list = proto->Parameters();
239 if (type_list != nullptr) {
240 for (const dex_ir::TypeId* type_id : *type_list->GetTypeList()) {
241 result += type_id->GetStringId()->Data();
242 }
David Sehr7629f602016-08-07 16:01:51 -0700243 }
244 result += ")";
245 result += proto->ReturnType()->GetStringId()->Data();
246 return result;
247}
248
249/*
250 * Copies character data from "data" to "out", converting non-ASCII values
251 * to fprintf format chars or an ASCII filler ('.' or '?').
252 *
253 * The output buffer must be able to hold (2*len)+1 bytes. The result is
254 * NULL-terminated.
255 */
256static void Asciify(char* out, const unsigned char* data, size_t len) {
Andreas Gampec74d9cb2018-09-20 13:44:44 -0700257 for (; len != 0u; --len) {
David Sehr7629f602016-08-07 16:01:51 -0700258 if (*data < 0x20) {
259 // Could do more here, but we don't need them yet.
260 switch (*data) {
261 case '\0':
262 *out++ = '\\';
263 *out++ = '0';
264 break;
265 case '\n':
266 *out++ = '\\';
267 *out++ = 'n';
268 break;
269 default:
270 *out++ = '.';
271 break;
272 } // switch
273 } else if (*data >= 0x80) {
274 *out++ = '?';
275 } else {
276 *out++ = *data;
277 }
278 data++;
279 } // while
280 *out = '\0';
281}
282
283/*
284 * Dumps a string value with some escape characters.
285 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800286static void DumpEscapedString(const char* p, FILE* out_file) {
287 fputs("\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700288 for (; *p; p++) {
289 switch (*p) {
290 case '\\':
Jeff Haoea7c6292016-11-14 18:10:16 -0800291 fputs("\\\\", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700292 break;
293 case '\"':
Jeff Haoea7c6292016-11-14 18:10:16 -0800294 fputs("\\\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700295 break;
296 case '\t':
Jeff Haoea7c6292016-11-14 18:10:16 -0800297 fputs("\\t", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700298 break;
299 case '\n':
Jeff Haoea7c6292016-11-14 18:10:16 -0800300 fputs("\\n", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700301 break;
302 case '\r':
Jeff Haoea7c6292016-11-14 18:10:16 -0800303 fputs("\\r", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700304 break;
305 default:
Jeff Haoea7c6292016-11-14 18:10:16 -0800306 putc(*p, out_file);
David Sehr7629f602016-08-07 16:01:51 -0700307 } // switch
308 } // for
Jeff Haoea7c6292016-11-14 18:10:16 -0800309 fputs("\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700310}
311
312/*
313 * Dumps a string as an XML attribute value.
314 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800315static void DumpXmlAttribute(const char* p, FILE* out_file) {
David Sehr7629f602016-08-07 16:01:51 -0700316 for (; *p; p++) {
317 switch (*p) {
318 case '&':
Jeff Haoea7c6292016-11-14 18:10:16 -0800319 fputs("&amp;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700320 break;
321 case '<':
Jeff Haoea7c6292016-11-14 18:10:16 -0800322 fputs("&lt;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700323 break;
324 case '>':
Jeff Haoea7c6292016-11-14 18:10:16 -0800325 fputs("&gt;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700326 break;
327 case '"':
Jeff Haoea7c6292016-11-14 18:10:16 -0800328 fputs("&quot;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700329 break;
330 case '\t':
Jeff Haoea7c6292016-11-14 18:10:16 -0800331 fputs("&#x9;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700332 break;
333 case '\n':
Jeff Haoea7c6292016-11-14 18:10:16 -0800334 fputs("&#xA;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700335 break;
336 case '\r':
Jeff Haoea7c6292016-11-14 18:10:16 -0800337 fputs("&#xD;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700338 break;
339 default:
Jeff Haoea7c6292016-11-14 18:10:16 -0800340 putc(*p, out_file);
David Sehr7629f602016-08-07 16:01:51 -0700341 } // switch
342 } // for
343}
344
David Sehr7629f602016-08-07 16:01:51 -0700345/*
346 * Helper for dumpInstruction(), which builds the string
347 * representation for the index in the given instruction.
348 * Returns a pointer to a buffer of sufficient size.
349 */
350static std::unique_ptr<char[]> IndexString(dex_ir::Header* header,
351 const Instruction* dec_insn,
352 size_t buf_size) {
353 std::unique_ptr<char[]> buf(new char[buf_size]);
354 // Determine index and width of the string.
355 uint32_t index = 0;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700356 uint32_t secondary_index = dex::kDexNoIndex;
David Sehr7629f602016-08-07 16:01:51 -0700357 uint32_t width = 4;
358 switch (Instruction::FormatOf(dec_insn->Opcode())) {
359 // SOME NOT SUPPORTED:
360 // case Instruction::k20bc:
361 case Instruction::k21c:
362 case Instruction::k35c:
363 // case Instruction::k35ms:
364 case Instruction::k3rc:
365 // case Instruction::k3rms:
366 // case Instruction::k35mi:
367 // case Instruction::k3rmi:
368 index = dec_insn->VRegB();
369 width = 4;
370 break;
371 case Instruction::k31c:
372 index = dec_insn->VRegB();
373 width = 8;
374 break;
375 case Instruction::k22c:
376 // case Instruction::k22cs:
377 index = dec_insn->VRegC();
378 width = 4;
379 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100380 case Instruction::k45cc:
381 case Instruction::k4rcc:
382 index = dec_insn->VRegB();
383 secondary_index = dec_insn->VRegH();
384 width = 4;
David Sehr7639cdc2017-04-15 10:06:21 -0700385 break;
David Sehr7629f602016-08-07 16:01:51 -0700386 default:
387 break;
388 } // switch
389
390 // Determine index type.
391 size_t outSize = 0;
392 switch (Instruction::IndexTypeOf(dec_insn->Opcode())) {
393 case Instruction::kIndexUnknown:
394 // This function should never get called for this type, but do
395 // something sensible here, just to help with debugging.
396 outSize = snprintf(buf.get(), buf_size, "<unknown-index>");
397 break;
398 case Instruction::kIndexNone:
399 // This function should never get called for this type, but do
400 // something sensible here, just to help with debugging.
401 outSize = snprintf(buf.get(), buf_size, "<no-index>");
402 break;
403 case Instruction::kIndexTypeRef:
David Sehr2b5a38f2018-06-14 15:13:04 -0700404 if (index < header->TypeIds().Size()) {
405 const char* tp = header->TypeIds()[index]->GetStringId()->Data();
David Sehr7629f602016-08-07 16:01:51 -0700406 outSize = snprintf(buf.get(), buf_size, "%s // type@%0*x", tp, width, index);
407 } else {
408 outSize = snprintf(buf.get(), buf_size, "<type?> // type@%0*x", width, index);
409 }
410 break;
411 case Instruction::kIndexStringRef:
David Sehr2b5a38f2018-06-14 15:13:04 -0700412 if (index < header->StringIds().Size()) {
413 const char* st = header->StringIds()[index]->Data();
David Sehr7629f602016-08-07 16:01:51 -0700414 outSize = snprintf(buf.get(), buf_size, "\"%s\" // string@%0*x", st, width, index);
415 } else {
416 outSize = snprintf(buf.get(), buf_size, "<string?> // string@%0*x", width, index);
417 }
418 break;
419 case Instruction::kIndexMethodRef:
David Sehr2b5a38f2018-06-14 15:13:04 -0700420 if (index < header->MethodIds().Size()) {
421 dex_ir::MethodId* method_id = header->MethodIds()[index];
David Sehr7629f602016-08-07 16:01:51 -0700422 const char* name = method_id->Name()->Data();
David Sehr72359222016-09-07 13:04:01 -0700423 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -0700424 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
425 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // method@%0*x",
David Sehr72359222016-09-07 13:04:01 -0700426 back_descriptor, name, type_descriptor.c_str(), width, index);
David Sehr7629f602016-08-07 16:01:51 -0700427 } else {
428 outSize = snprintf(buf.get(), buf_size, "<method?> // method@%0*x", width, index);
429 }
430 break;
431 case Instruction::kIndexFieldRef:
David Sehr2b5a38f2018-06-14 15:13:04 -0700432 if (index < header->FieldIds().Size()) {
433 dex_ir::FieldId* field_id = header->FieldIds()[index];
David Sehr7629f602016-08-07 16:01:51 -0700434 const char* name = field_id->Name()->Data();
435 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
436 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
437 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // field@%0*x",
438 back_descriptor, name, type_descriptor, width, index);
439 } else {
440 outSize = snprintf(buf.get(), buf_size, "<field?> // field@%0*x", width, index);
441 }
442 break;
443 case Instruction::kIndexVtableOffset:
444 outSize = snprintf(buf.get(), buf_size, "[%0*x] // vtable #%0*x",
445 width, index, width, index);
446 break;
447 case Instruction::kIndexFieldOffset:
448 outSize = snprintf(buf.get(), buf_size, "[obj+%0*x]", width, index);
449 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100450 case Instruction::kIndexMethodAndProtoRef: {
451 std::string method("<method?>");
452 std::string proto("<proto?>");
David Sehr2b5a38f2018-06-14 15:13:04 -0700453 if (index < header->MethodIds().Size()) {
454 dex_ir::MethodId* method_id = header->MethodIds()[index];
Orion Hodsonb34bb192016-10-18 17:02:58 +0100455 const char* name = method_id->Name()->Data();
456 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
457 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
458 method = StringPrintf("%s.%s:%s", back_descriptor, name, type_descriptor.c_str());
459 }
David Sehr2b5a38f2018-06-14 15:13:04 -0700460 if (secondary_index < header->ProtoIds().Size()) {
461 dex_ir::ProtoId* proto_id = header->ProtoIds()[secondary_index];
Orion Hodsonb34bb192016-10-18 17:02:58 +0100462 proto = GetSignatureForProtoId(proto_id);
463 }
464 outSize = snprintf(buf.get(), buf_size, "%s, %s // method@%0*x, proto@%0*x",
465 method.c_str(), proto.c_str(), width, index, width, secondary_index);
Jeff Haoea7c6292016-11-14 18:10:16 -0800466 }
467 break;
468 // SOME NOT SUPPORTED:
469 // case Instruction::kIndexVaries:
470 // case Instruction::kIndexInlineMethod:
David Sehr7629f602016-08-07 16:01:51 -0700471 default:
472 outSize = snprintf(buf.get(), buf_size, "<?>");
473 break;
474 } // switch
475
476 // Determine success of string construction.
477 if (outSize >= buf_size) {
478 // The buffer wasn't big enough; retry with computed size. Note: snprintf()
479 // doesn't count/ the '\0' as part of its returned size, so we add explicit
480 // space for it here.
481 return IndexString(header, dec_insn, outSize + 1);
482 }
483 return buf;
484}
485
486/*
Jeff Haoea7c6292016-11-14 18:10:16 -0800487 * Dumps encoded annotation.
488 */
489void DexLayout::DumpEncodedAnnotation(dex_ir::EncodedAnnotation* annotation) {
490 fputs(annotation->GetType()->GetStringId()->Data(), out_file_);
491 // Display all name=value pairs.
492 for (auto& subannotation : *annotation->GetAnnotationElements()) {
493 fputc(' ', out_file_);
494 fputs(subannotation->GetName()->Data(), out_file_);
495 fputc('=', out_file_);
496 DumpEncodedValue(subannotation->GetValue());
497 }
498}
499/*
500 * Dumps encoded value.
501 */
502void DexLayout::DumpEncodedValue(const dex_ir::EncodedValue* data) {
503 switch (data->Type()) {
504 case DexFile::kDexAnnotationByte:
505 fprintf(out_file_, "%" PRId8, data->GetByte());
506 break;
507 case DexFile::kDexAnnotationShort:
508 fprintf(out_file_, "%" PRId16, data->GetShort());
509 break;
510 case DexFile::kDexAnnotationChar:
511 fprintf(out_file_, "%" PRIu16, data->GetChar());
512 break;
513 case DexFile::kDexAnnotationInt:
514 fprintf(out_file_, "%" PRId32, data->GetInt());
515 break;
516 case DexFile::kDexAnnotationLong:
517 fprintf(out_file_, "%" PRId64, data->GetLong());
518 break;
519 case DexFile::kDexAnnotationFloat: {
520 fprintf(out_file_, "%g", data->GetFloat());
521 break;
522 }
523 case DexFile::kDexAnnotationDouble: {
524 fprintf(out_file_, "%g", data->GetDouble());
525 break;
526 }
527 case DexFile::kDexAnnotationString: {
528 dex_ir::StringId* string_id = data->GetStringId();
529 if (options_.output_format_ == kOutputPlain) {
530 DumpEscapedString(string_id->Data(), out_file_);
531 } else {
532 DumpXmlAttribute(string_id->Data(), out_file_);
533 }
534 break;
535 }
536 case DexFile::kDexAnnotationType: {
537 dex_ir::TypeId* type_id = data->GetTypeId();
538 fputs(type_id->GetStringId()->Data(), out_file_);
539 break;
540 }
541 case DexFile::kDexAnnotationField:
542 case DexFile::kDexAnnotationEnum: {
543 dex_ir::FieldId* field_id = data->GetFieldId();
544 fputs(field_id->Name()->Data(), out_file_);
545 break;
546 }
547 case DexFile::kDexAnnotationMethod: {
548 dex_ir::MethodId* method_id = data->GetMethodId();
549 fputs(method_id->Name()->Data(), out_file_);
550 break;
551 }
552 case DexFile::kDexAnnotationArray: {
553 fputc('{', out_file_);
554 // Display all elements.
555 for (auto& value : *data->GetEncodedArray()->GetEncodedValues()) {
556 fputc(' ', out_file_);
557 DumpEncodedValue(value.get());
558 }
559 fputs(" }", out_file_);
560 break;
561 }
562 case DexFile::kDexAnnotationAnnotation: {
563 DumpEncodedAnnotation(data->GetEncodedAnnotation());
564 break;
565 }
566 case DexFile::kDexAnnotationNull:
567 fputs("null", out_file_);
568 break;
569 case DexFile::kDexAnnotationBoolean:
570 fputs(StrBool(data->GetBoolean()), out_file_);
571 break;
572 default:
573 fputs("????", out_file_);
574 break;
575 } // switch
576}
577
578/*
579 * Dumps the file header.
580 */
581void DexLayout::DumpFileHeader() {
582 char sanitized[8 * 2 + 1];
Jeff Haoea7c6292016-11-14 18:10:16 -0800583 fprintf(out_file_, "DEX file header:\n");
584 Asciify(sanitized, header_->Magic(), 8);
585 fprintf(out_file_, "magic : '%s'\n", sanitized);
586 fprintf(out_file_, "checksum : %08x\n", header_->Checksum());
587 fprintf(out_file_, "signature : %02x%02x...%02x%02x\n",
588 header_->Signature()[0], header_->Signature()[1],
589 header_->Signature()[DexFile::kSha1DigestSize - 2],
590 header_->Signature()[DexFile::kSha1DigestSize - 1]);
591 fprintf(out_file_, "file_size : %d\n", header_->FileSize());
592 fprintf(out_file_, "header_size : %d\n", header_->HeaderSize());
593 fprintf(out_file_, "link_size : %d\n", header_->LinkSize());
594 fprintf(out_file_, "link_off : %d (0x%06x)\n",
595 header_->LinkOffset(), header_->LinkOffset());
David Sehr2b5a38f2018-06-14 15:13:04 -0700596 fprintf(out_file_, "string_ids_size : %d\n", header_->StringIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800597 fprintf(out_file_, "string_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700598 header_->StringIds().GetOffset(), header_->StringIds().GetOffset());
599 fprintf(out_file_, "type_ids_size : %d\n", header_->TypeIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800600 fprintf(out_file_, "type_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700601 header_->TypeIds().GetOffset(), header_->TypeIds().GetOffset());
602 fprintf(out_file_, "proto_ids_size : %d\n", header_->ProtoIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800603 fprintf(out_file_, "proto_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700604 header_->ProtoIds().GetOffset(), header_->ProtoIds().GetOffset());
605 fprintf(out_file_, "field_ids_size : %d\n", header_->FieldIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800606 fprintf(out_file_, "field_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700607 header_->FieldIds().GetOffset(), header_->FieldIds().GetOffset());
608 fprintf(out_file_, "method_ids_size : %d\n", header_->MethodIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800609 fprintf(out_file_, "method_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700610 header_->MethodIds().GetOffset(), header_->MethodIds().GetOffset());
611 fprintf(out_file_, "class_defs_size : %d\n", header_->ClassDefs().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800612 fprintf(out_file_, "class_defs_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700613 header_->ClassDefs().GetOffset(), header_->ClassDefs().GetOffset());
Jeff Haoea7c6292016-11-14 18:10:16 -0800614 fprintf(out_file_, "data_size : %d\n", header_->DataSize());
615 fprintf(out_file_, "data_off : %d (0x%06x)\n\n",
616 header_->DataOffset(), header_->DataOffset());
617}
618
619/*
620 * Dumps a class_def_item.
621 */
622void DexLayout::DumpClassDef(int idx) {
623 // General class information.
David Sehr2b5a38f2018-06-14 15:13:04 -0700624 dex_ir::ClassDef* class_def = header_->ClassDefs()[idx];
Jeff Haoea7c6292016-11-14 18:10:16 -0800625 fprintf(out_file_, "Class #%d header:\n", idx);
626 fprintf(out_file_, "class_idx : %d\n", class_def->ClassType()->GetIndex());
627 fprintf(out_file_, "access_flags : %d (0x%04x)\n",
628 class_def->GetAccessFlags(), class_def->GetAccessFlags());
629 uint32_t superclass_idx = class_def->Superclass() == nullptr ?
630 DexFile::kDexNoIndex16 : class_def->Superclass()->GetIndex();
631 fprintf(out_file_, "superclass_idx : %d\n", superclass_idx);
632 fprintf(out_file_, "interfaces_off : %d (0x%06x)\n",
633 class_def->InterfacesOffset(), class_def->InterfacesOffset());
634 uint32_t source_file_offset = 0xffffffffU;
635 if (class_def->SourceFile() != nullptr) {
636 source_file_offset = class_def->SourceFile()->GetIndex();
637 }
638 fprintf(out_file_, "source_file_idx : %d\n", source_file_offset);
639 uint32_t annotations_offset = 0;
640 if (class_def->Annotations() != nullptr) {
641 annotations_offset = class_def->Annotations()->GetOffset();
642 }
643 fprintf(out_file_, "annotations_off : %d (0x%06x)\n",
644 annotations_offset, annotations_offset);
645 if (class_def->GetClassData() == nullptr) {
646 fprintf(out_file_, "class_data_off : %d (0x%06x)\n", 0, 0);
647 } else {
648 fprintf(out_file_, "class_data_off : %d (0x%06x)\n",
649 class_def->GetClassData()->GetOffset(), class_def->GetClassData()->GetOffset());
650 }
651
652 // Fields and methods.
653 dex_ir::ClassData* class_data = class_def->GetClassData();
654 if (class_data != nullptr && class_data->StaticFields() != nullptr) {
655 fprintf(out_file_, "static_fields_size : %zu\n", class_data->StaticFields()->size());
656 } else {
657 fprintf(out_file_, "static_fields_size : 0\n");
658 }
659 if (class_data != nullptr && class_data->InstanceFields() != nullptr) {
660 fprintf(out_file_, "instance_fields_size: %zu\n", class_data->InstanceFields()->size());
661 } else {
662 fprintf(out_file_, "instance_fields_size: 0\n");
663 }
664 if (class_data != nullptr && class_data->DirectMethods() != nullptr) {
665 fprintf(out_file_, "direct_methods_size : %zu\n", class_data->DirectMethods()->size());
666 } else {
667 fprintf(out_file_, "direct_methods_size : 0\n");
668 }
669 if (class_data != nullptr && class_data->VirtualMethods() != nullptr) {
670 fprintf(out_file_, "virtual_methods_size: %zu\n", class_data->VirtualMethods()->size());
671 } else {
672 fprintf(out_file_, "virtual_methods_size: 0\n");
673 }
674 fprintf(out_file_, "\n");
675}
676
677/**
678 * Dumps an annotation set item.
679 */
680void DexLayout::DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item) {
681 if (set_item == nullptr || set_item->GetItems()->size() == 0) {
682 fputs(" empty-annotation-set\n", out_file_);
683 return;
684 }
685 for (dex_ir::AnnotationItem* annotation : *set_item->GetItems()) {
686 if (annotation == nullptr) {
687 continue;
688 }
689 fputs(" ", out_file_);
690 switch (annotation->GetVisibility()) {
691 case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", out_file_); break;
692 case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", out_file_); break;
693 case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", out_file_); break;
694 default: fputs("VISIBILITY_UNKNOWN ", out_file_); break;
695 } // switch
696 DumpEncodedAnnotation(annotation->GetAnnotation());
697 fputc('\n', out_file_);
698 }
699}
700
701/*
702 * Dumps class annotations.
703 */
704void DexLayout::DumpClassAnnotations(int idx) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700705 dex_ir::ClassDef* class_def = header_->ClassDefs()[idx];
Jeff Haoea7c6292016-11-14 18:10:16 -0800706 dex_ir::AnnotationsDirectoryItem* annotations_directory = class_def->Annotations();
707 if (annotations_directory == nullptr) {
708 return; // none
709 }
710
711 fprintf(out_file_, "Class #%d annotations:\n", idx);
712
713 dex_ir::AnnotationSetItem* class_set_item = annotations_directory->GetClassAnnotation();
714 dex_ir::FieldAnnotationVector* fields = annotations_directory->GetFieldAnnotations();
715 dex_ir::MethodAnnotationVector* methods = annotations_directory->GetMethodAnnotations();
716 dex_ir::ParameterAnnotationVector* parameters = annotations_directory->GetParameterAnnotations();
717
718 // Annotations on the class itself.
719 if (class_set_item != nullptr) {
720 fprintf(out_file_, "Annotations on class\n");
721 DumpAnnotationSetItem(class_set_item);
722 }
723
724 // Annotations on fields.
725 if (fields != nullptr) {
726 for (auto& field : *fields) {
727 const dex_ir::FieldId* field_id = field->GetFieldId();
728 const uint32_t field_idx = field_id->GetIndex();
729 const char* field_name = field_id->Name()->Data();
730 fprintf(out_file_, "Annotations on field #%u '%s'\n", field_idx, field_name);
731 DumpAnnotationSetItem(field->GetAnnotationSetItem());
732 }
733 }
734
735 // Annotations on methods.
736 if (methods != nullptr) {
737 for (auto& method : *methods) {
738 const dex_ir::MethodId* method_id = method->GetMethodId();
739 const uint32_t method_idx = method_id->GetIndex();
740 const char* method_name = method_id->Name()->Data();
741 fprintf(out_file_, "Annotations on method #%u '%s'\n", method_idx, method_name);
742 DumpAnnotationSetItem(method->GetAnnotationSetItem());
743 }
744 }
745
746 // Annotations on method parameters.
747 if (parameters != nullptr) {
748 for (auto& parameter : *parameters) {
749 const dex_ir::MethodId* method_id = parameter->GetMethodId();
750 const uint32_t method_idx = method_id->GetIndex();
751 const char* method_name = method_id->Name()->Data();
752 fprintf(out_file_, "Annotations on method #%u '%s' parameters\n", method_idx, method_name);
753 uint32_t j = 0;
754 for (dex_ir::AnnotationSetItem* annotation : *parameter->GetAnnotations()->GetItems()) {
755 fprintf(out_file_, "#%u\n", j);
756 DumpAnnotationSetItem(annotation);
757 ++j;
758 }
759 }
760 }
761
762 fputc('\n', out_file_);
763}
764
765/*
766 * Dumps an interface that a class declares to implement.
767 */
768void DexLayout::DumpInterface(const dex_ir::TypeId* type_item, int i) {
769 const char* interface_name = type_item->GetStringId()->Data();
770 if (options_.output_format_ == kOutputPlain) {
771 fprintf(out_file_, " #%d : '%s'\n", i, interface_name);
772 } else {
Orion Hodsonfe42d212018-08-24 14:01:14 +0100773 std::string dot(DescriptorToDot(interface_name));
Jeff Haoea7c6292016-11-14 18:10:16 -0800774 fprintf(out_file_, "<implements name=\"%s\">\n</implements>\n", dot.c_str());
775 }
776}
777
778/*
779 * Dumps the catches table associated with the code.
780 */
781void DexLayout::DumpCatches(const dex_ir::CodeItem* code) {
782 const uint16_t tries_size = code->TriesSize();
783
784 // No catch table.
785 if (tries_size == 0) {
786 fprintf(out_file_, " catches : (none)\n");
787 return;
788 }
789
790 // Dump all table entries.
791 fprintf(out_file_, " catches : %d\n", tries_size);
792 std::vector<std::unique_ptr<const dex_ir::TryItem>>* tries = code->Tries();
793 for (uint32_t i = 0; i < tries_size; i++) {
794 const dex_ir::TryItem* try_item = (*tries)[i].get();
795 const uint32_t start = try_item->StartAddr();
796 const uint32_t end = start + try_item->InsnCount();
797 fprintf(out_file_, " 0x%04x - 0x%04x\n", start, end);
798 for (auto& handler : *try_item->GetHandlers()->GetHandlers()) {
799 const dex_ir::TypeId* type_id = handler->GetTypeId();
800 const char* descriptor = (type_id == nullptr) ? "<any>" : type_id->GetStringId()->Data();
801 fprintf(out_file_, " %s -> 0x%04x\n", descriptor, handler->GetAddress());
802 } // for
803 } // for
804}
805
806/*
David Sehr7629f602016-08-07 16:01:51 -0700807 * Dumps a single instruction.
808 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800809void DexLayout::DumpInstruction(const dex_ir::CodeItem* code,
810 uint32_t code_offset,
811 uint32_t insn_idx,
812 uint32_t insn_width,
813 const Instruction* dec_insn) {
David Sehr7629f602016-08-07 16:01:51 -0700814 // Address of instruction (expressed as byte offset).
815 fprintf(out_file_, "%06x:", code_offset + 0x10 + insn_idx * 2);
816
817 // Dump (part of) raw bytes.
818 const uint16_t* insns = code->Insns();
819 for (uint32_t i = 0; i < 8; i++) {
820 if (i < insn_width) {
821 if (i == 7) {
822 fprintf(out_file_, " ... ");
823 } else {
824 // Print 16-bit value in little-endian order.
825 const uint8_t* bytePtr = (const uint8_t*) &insns[insn_idx + i];
826 fprintf(out_file_, " %02x%02x", bytePtr[0], bytePtr[1]);
827 }
828 } else {
829 fputs(" ", out_file_);
830 }
831 } // for
832
833 // Dump pseudo-instruction or opcode.
834 if (dec_insn->Opcode() == Instruction::NOP) {
835 const uint16_t instr = Get2LE((const uint8_t*) &insns[insn_idx]);
836 if (instr == Instruction::kPackedSwitchSignature) {
837 fprintf(out_file_, "|%04x: packed-switch-data (%d units)", insn_idx, insn_width);
838 } else if (instr == Instruction::kSparseSwitchSignature) {
839 fprintf(out_file_, "|%04x: sparse-switch-data (%d units)", insn_idx, insn_width);
840 } else if (instr == Instruction::kArrayDataSignature) {
841 fprintf(out_file_, "|%04x: array-data (%d units)", insn_idx, insn_width);
842 } else {
843 fprintf(out_file_, "|%04x: nop // spacer", insn_idx);
844 }
845 } else {
846 fprintf(out_file_, "|%04x: %s", insn_idx, dec_insn->Name());
847 }
848
849 // Set up additional argument.
850 std::unique_ptr<char[]> index_buf;
851 if (Instruction::IndexTypeOf(dec_insn->Opcode()) != Instruction::kIndexNone) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800852 index_buf = IndexString(header_, dec_insn, 200);
David Sehr7629f602016-08-07 16:01:51 -0700853 }
854
855 // Dump the instruction.
856 //
857 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
858 //
859 switch (Instruction::FormatOf(dec_insn->Opcode())) {
860 case Instruction::k10x: // op
861 break;
862 case Instruction::k12x: // op vA, vB
863 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
864 break;
865 case Instruction::k11n: // op vA, #+B
866 fprintf(out_file_, " v%d, #int %d // #%x",
867 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint8_t)dec_insn->VRegB());
868 break;
869 case Instruction::k11x: // op vAA
870 fprintf(out_file_, " v%d", dec_insn->VRegA());
871 break;
872 case Instruction::k10t: // op +AA
873 case Instruction::k20t: { // op +AAAA
874 const int32_t targ = (int32_t) dec_insn->VRegA();
875 fprintf(out_file_, " %04x // %c%04x",
876 insn_idx + targ,
877 (targ < 0) ? '-' : '+',
878 (targ < 0) ? -targ : targ);
879 break;
880 }
881 case Instruction::k22x: // op vAA, vBBBB
882 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
883 break;
884 case Instruction::k21t: { // op vAA, +BBBB
885 const int32_t targ = (int32_t) dec_insn->VRegB();
886 fprintf(out_file_, " v%d, %04x // %c%04x", dec_insn->VRegA(),
887 insn_idx + targ,
888 (targ < 0) ? '-' : '+',
889 (targ < 0) ? -targ : targ);
890 break;
891 }
892 case Instruction::k21s: // op vAA, #+BBBB
893 fprintf(out_file_, " v%d, #int %d // #%x",
894 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint16_t)dec_insn->VRegB());
895 break;
896 case Instruction::k21h: // op vAA, #+BBBB0000[00000000]
897 // The printed format varies a bit based on the actual opcode.
898 if (dec_insn->Opcode() == Instruction::CONST_HIGH16) {
899 const int32_t value = dec_insn->VRegB() << 16;
900 fprintf(out_file_, " v%d, #int %d // #%x",
901 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
902 } else {
903 const int64_t value = ((int64_t) dec_insn->VRegB()) << 48;
904 fprintf(out_file_, " v%d, #long %" PRId64 " // #%x",
905 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
906 }
907 break;
908 case Instruction::k21c: // op vAA, thing@BBBB
909 case Instruction::k31c: // op vAA, thing@BBBBBBBB
910 fprintf(out_file_, " v%d, %s", dec_insn->VRegA(), index_buf.get());
911 break;
912 case Instruction::k23x: // op vAA, vBB, vCC
913 fprintf(out_file_, " v%d, v%d, v%d",
914 dec_insn->VRegA(), dec_insn->VRegB(), dec_insn->VRegC());
915 break;
916 case Instruction::k22b: // op vAA, vBB, #+CC
917 fprintf(out_file_, " v%d, v%d, #int %d // #%02x",
918 dec_insn->VRegA(), dec_insn->VRegB(),
919 (int32_t) dec_insn->VRegC(), (uint8_t) dec_insn->VRegC());
920 break;
921 case Instruction::k22t: { // op vA, vB, +CCCC
922 const int32_t targ = (int32_t) dec_insn->VRegC();
923 fprintf(out_file_, " v%d, v%d, %04x // %c%04x",
924 dec_insn->VRegA(), dec_insn->VRegB(),
925 insn_idx + targ,
926 (targ < 0) ? '-' : '+',
927 (targ < 0) ? -targ : targ);
928 break;
929 }
930 case Instruction::k22s: // op vA, vB, #+CCCC
931 fprintf(out_file_, " v%d, v%d, #int %d // #%04x",
932 dec_insn->VRegA(), dec_insn->VRegB(),
933 (int32_t) dec_insn->VRegC(), (uint16_t) dec_insn->VRegC());
934 break;
935 case Instruction::k22c: // op vA, vB, thing@CCCC
936 // NOT SUPPORTED:
937 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
938 fprintf(out_file_, " v%d, v%d, %s",
939 dec_insn->VRegA(), dec_insn->VRegB(), index_buf.get());
940 break;
941 case Instruction::k30t:
942 fprintf(out_file_, " #%08x", dec_insn->VRegA());
943 break;
944 case Instruction::k31i: { // op vAA, #+BBBBBBBB
945 // This is often, but not always, a float.
946 union {
947 float f;
948 uint32_t i;
949 } conv;
950 conv.i = dec_insn->VRegB();
951 fprintf(out_file_, " v%d, #float %g // #%08x",
952 dec_insn->VRegA(), conv.f, dec_insn->VRegB());
953 break;
954 }
955 case Instruction::k31t: // op vAA, offset +BBBBBBBB
956 fprintf(out_file_, " v%d, %08x // +%08x",
957 dec_insn->VRegA(), insn_idx + dec_insn->VRegB(), dec_insn->VRegB());
958 break;
959 case Instruction::k32x: // op vAAAA, vBBBB
960 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
961 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100962 case Instruction::k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
963 case Instruction::k45cc: { // op {vC, vD, vE, vF, vG}, meth@BBBB, proto@HHHH
David Sehr7629f602016-08-07 16:01:51 -0700964 // NOT SUPPORTED:
965 // case Instruction::k35ms: // [opt] invoke-virtual+super
966 // case Instruction::k35mi: // [opt] inline invoke
967 uint32_t arg[Instruction::kMaxVarArgRegs];
968 dec_insn->GetVarArgs(arg);
969 fputs(" {", out_file_);
970 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
971 if (i == 0) {
972 fprintf(out_file_, "v%d", arg[i]);
973 } else {
974 fprintf(out_file_, ", v%d", arg[i]);
975 }
976 } // for
977 fprintf(out_file_, "}, %s", index_buf.get());
978 break;
979 }
Orion Hodsonb34bb192016-10-18 17:02:58 +0100980 case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
981 case Instruction::k4rcc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB, proto@HHHH
David Sehr7629f602016-08-07 16:01:51 -0700982 // NOT SUPPORTED:
983 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
984 // case Instruction::k3rmi: // [opt] execute-inline/range
985 {
986 // This doesn't match the "dx" output when some of the args are
987 // 64-bit values -- dx only shows the first register.
988 fputs(" {", out_file_);
989 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
990 if (i == 0) {
991 fprintf(out_file_, "v%d", dec_insn->VRegC() + i);
992 } else {
993 fprintf(out_file_, ", v%d", dec_insn->VRegC() + i);
994 }
995 } // for
996 fprintf(out_file_, "}, %s", index_buf.get());
997 }
998 break;
999 case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB
1000 // This is often, but not always, a double.
1001 union {
1002 double d;
1003 uint64_t j;
1004 } conv;
1005 conv.j = dec_insn->WideVRegB();
1006 fprintf(out_file_, " v%d, #double %g // #%016" PRIx64,
1007 dec_insn->VRegA(), conv.d, dec_insn->WideVRegB());
1008 break;
1009 }
1010 // NOT SUPPORTED:
1011 // case Instruction::k00x: // unknown op or breakpoint
1012 // break;
1013 default:
1014 fprintf(out_file_, " ???");
1015 break;
1016 } // switch
1017
1018 fputc('\n', out_file_);
1019}
1020
1021/*
1022 * Dumps a bytecode disassembly.
1023 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001024void DexLayout::DumpBytecodes(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001025 dex_ir::MethodId* method_id = header_->MethodIds()[idx];
David Sehr7629f602016-08-07 16:01:51 -07001026 const char* name = method_id->Name()->Data();
David Sehr72359222016-09-07 13:04:01 -07001027 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -07001028 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1029
1030 // Generate header.
Orion Hodsonfe42d212018-08-24 14:01:14 +01001031 std::string dot(DescriptorToDot(back_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001032 fprintf(out_file_, "%06x: |[%06x] %s.%s:%s\n",
David Sehr72359222016-09-07 13:04:01 -07001033 code_offset, code_offset, dot.c_str(), name, type_descriptor.c_str());
David Sehr7629f602016-08-07 16:01:51 -07001034
1035 // Iterate over all instructions.
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001036 for (const DexInstructionPcPair& inst : code->Instructions()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001037 const uint32_t insn_width = inst->SizeInCodeUnits();
David Sehr7629f602016-08-07 16:01:51 -07001038 if (insn_width == 0) {
Andreas Gampe221d9812018-01-22 17:48:56 -08001039 LOG(WARNING) << "GLITCH: zero-width instruction at idx=0x" << std::hex << inst.DexPc();
David Sehr7629f602016-08-07 16:01:51 -07001040 break;
1041 }
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001042 DumpInstruction(code, code_offset, inst.DexPc(), insn_width, &inst.Inst());
David Sehr7629f602016-08-07 16:01:51 -07001043 } // for
1044}
1045
1046/*
David Sehraa6abb02017-10-12 08:25:11 -07001047 * Lookup functions.
1048 */
David Sehr2b5a38f2018-06-14 15:13:04 -07001049static const char* StringDataByIdx(uint32_t idx, dex_ir::Header* header) {
1050 dex_ir::StringId* string_id = header->GetStringIdOrNullPtr(idx);
David Sehraa6abb02017-10-12 08:25:11 -07001051 if (string_id == nullptr) {
1052 return nullptr;
1053 }
1054 return string_id->Data();
1055}
1056
David Sehr2b5a38f2018-06-14 15:13:04 -07001057static const char* StringDataByTypeIdx(uint16_t idx, dex_ir::Header* header) {
1058 dex_ir::TypeId* type_id = header->GetTypeIdOrNullPtr(idx);
David Sehraa6abb02017-10-12 08:25:11 -07001059 if (type_id == nullptr) {
1060 return nullptr;
1061 }
1062 dex_ir::StringId* string_id = type_id->GetStringId();
1063 if (string_id == nullptr) {
1064 return nullptr;
1065 }
1066 return string_id->Data();
1067}
1068
1069
1070/*
David Sehr7629f602016-08-07 16:01:51 -07001071 * Dumps code of a method.
1072 */
David Sehraa6abb02017-10-12 08:25:11 -07001073void DexLayout::DumpCode(uint32_t idx,
1074 const dex_ir::CodeItem* code,
1075 uint32_t code_offset,
1076 const char* declaring_class_descriptor,
1077 const char* method_name,
1078 bool is_static,
1079 const dex_ir::ProtoId* proto) {
David Sehr7629f602016-08-07 16:01:51 -07001080 fprintf(out_file_, " registers : %d\n", code->RegistersSize());
1081 fprintf(out_file_, " ins : %d\n", code->InsSize());
1082 fprintf(out_file_, " outs : %d\n", code->OutsSize());
1083 fprintf(out_file_, " insns size : %d 16-bit code units\n",
1084 code->InsnsSize());
1085
1086 // Bytecode disassembly, if requested.
1087 if (options_.disassemble_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001088 DumpBytecodes(idx, code, code_offset);
David Sehr7629f602016-08-07 16:01:51 -07001089 }
1090
1091 // Try-catch blocks.
1092 DumpCatches(code);
1093
1094 // Positions and locals table in the debug info.
David Sehraa6abb02017-10-12 08:25:11 -07001095 dex_ir::DebugInfoItem* debug_info = code->DebugInfo();
David Sehr7629f602016-08-07 16:01:51 -07001096 fprintf(out_file_, " positions : \n");
David Sehraa6abb02017-10-12 08:25:11 -07001097 if (debug_info != nullptr) {
1098 DexFile::DecodeDebugPositionInfo(debug_info->GetDebugInfo(),
1099 [this](uint32_t idx) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001100 return StringDataByIdx(idx, this->header_);
David Sehraa6abb02017-10-12 08:25:11 -07001101 },
Mathieu Chartier3e2e1232018-09-11 12:35:30 -07001102 [&](const DexFile::PositionInfo& entry) {
1103 fprintf(out_file_,
1104 " 0x%04x line=%d\n",
1105 entry.address_,
1106 entry.line_);
1107 return false;
1108 });
David Sehraa6abb02017-10-12 08:25:11 -07001109 }
David Sehr7629f602016-08-07 16:01:51 -07001110 fprintf(out_file_, " locals : \n");
David Sehraa6abb02017-10-12 08:25:11 -07001111 if (debug_info != nullptr) {
1112 std::vector<const char*> arg_descriptors;
1113 const dex_ir::TypeList* parameters = proto->Parameters();
1114 if (parameters != nullptr) {
1115 const dex_ir::TypeIdVector* parameter_type_vector = parameters->GetTypeList();
1116 if (parameter_type_vector != nullptr) {
1117 for (const dex_ir::TypeId* type_id : *parameter_type_vector) {
1118 arg_descriptors.push_back(type_id->GetStringId()->Data());
1119 }
1120 }
1121 }
1122 DexFile::DecodeDebugLocalInfo(debug_info->GetDebugInfo(),
1123 "DexLayout in-memory",
1124 declaring_class_descriptor,
1125 arg_descriptors,
1126 method_name,
1127 is_static,
1128 code->RegistersSize(),
1129 code->InsSize(),
1130 code->InsnsSize(),
1131 [this](uint32_t idx) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001132 return StringDataByIdx(idx, this->header_);
David Sehraa6abb02017-10-12 08:25:11 -07001133 },
1134 [this](uint32_t idx) {
1135 return
1136 StringDataByTypeIdx(dchecked_integral_cast<uint16_t>(idx),
David Sehr2b5a38f2018-06-14 15:13:04 -07001137 this->header_);
David Sehraa6abb02017-10-12 08:25:11 -07001138 },
Mathieu Chartiere5afbf32018-09-12 17:51:54 -07001139 [&](const DexFile::LocalInfo& entry) {
1140 const char* signature =
1141 entry.signature_ != nullptr ? entry.signature_ : "";
1142 fprintf(out_file_,
1143 " 0x%04x - 0x%04x reg=%d %s %s %s\n",
1144 entry.start_address_,
1145 entry.end_address_,
1146 entry.reg_,
1147 entry.name_,
1148 entry.descriptor_,
1149 signature);
1150 });
David Sehraa6abb02017-10-12 08:25:11 -07001151 }
David Sehr7629f602016-08-07 16:01:51 -07001152}
1153
1154/*
1155 * Dumps a method.
1156 */
David Brazdil20c765f2018-10-27 21:45:15 +00001157void DexLayout::DumpMethod(uint32_t idx,
1158 uint32_t flags,
1159 uint32_t hiddenapi_flags,
1160 const dex_ir::CodeItem* code,
1161 int i) {
David Sehr7629f602016-08-07 16:01:51 -07001162 // Bail for anything private if export only requested.
1163 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1164 return;
1165 }
1166
David Sehr2b5a38f2018-06-14 15:13:04 -07001167 dex_ir::MethodId* method_id = header_->MethodIds()[idx];
David Sehr7629f602016-08-07 16:01:51 -07001168 const char* name = method_id->Name()->Data();
1169 char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str());
1170 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1171 char* access_str = CreateAccessFlagStr(flags, kAccessForMethod);
1172
1173 if (options_.output_format_ == kOutputPlain) {
1174 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1175 fprintf(out_file_, " name : '%s'\n", name);
1176 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1177 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
David Brazdil20c765f2018-10-27 21:45:15 +00001178 if (hiddenapi_flags != 0u) {
David Brazdildcfa89b2018-10-31 11:04:10 +00001179 fprintf(out_file_,
1180 " hiddenapi : 0x%04x (%s)\n",
1181 hiddenapi_flags,
1182 GetHiddenapiFlagStr(hiddenapi_flags).c_str());
David Brazdil20c765f2018-10-27 21:45:15 +00001183 }
David Sehr7629f602016-08-07 16:01:51 -07001184 if (code == nullptr) {
1185 fprintf(out_file_, " code : (none)\n");
1186 } else {
1187 fprintf(out_file_, " code -\n");
David Sehraa6abb02017-10-12 08:25:11 -07001188 DumpCode(idx,
1189 code,
1190 code->GetOffset(),
1191 back_descriptor,
1192 name,
1193 (flags & kAccStatic) != 0,
1194 method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -07001195 }
1196 if (options_.disassemble_) {
1197 fputc('\n', out_file_);
1198 }
1199 } else if (options_.output_format_ == kOutputXml) {
1200 const bool constructor = (name[0] == '<');
1201
1202 // Method name and prototype.
1203 if (constructor) {
Orion Hodsonfe42d212018-08-24 14:01:14 +01001204 std::string dot(DescriptorClassToName(back_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001205 fprintf(out_file_, "<constructor name=\"%s\"\n", dot.c_str());
Orion Hodsonfe42d212018-08-24 14:01:14 +01001206 dot = DescriptorToDot(back_descriptor);
David Sehr7629f602016-08-07 16:01:51 -07001207 fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
1208 } else {
1209 fprintf(out_file_, "<method name=\"%s\"\n", name);
1210 const char* return_type = strrchr(type_descriptor, ')');
1211 if (return_type == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08001212 LOG(ERROR) << "bad method type descriptor '" << type_descriptor << "'";
David Sehr7629f602016-08-07 16:01:51 -07001213 goto bail;
1214 }
Orion Hodsonfe42d212018-08-24 14:01:14 +01001215 std::string dot(DescriptorToDot(return_type + 1));
David Sehr7629f602016-08-07 16:01:51 -07001216 fprintf(out_file_, " return=\"%s\"\n", dot.c_str());
1217 fprintf(out_file_, " abstract=%s\n", QuotedBool((flags & kAccAbstract) != 0));
1218 fprintf(out_file_, " native=%s\n", QuotedBool((flags & kAccNative) != 0));
1219 fprintf(out_file_, " synchronized=%s\n", QuotedBool(
1220 (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0));
1221 }
1222
1223 // Additional method flags.
1224 fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
1225 fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
1226 // The "deprecated=" not knowable w/o parsing annotations.
1227 fprintf(out_file_, " visibility=%s\n>\n", QuotedVisibility(flags));
1228
1229 // Parameters.
1230 if (type_descriptor[0] != '(') {
Andreas Gampe221d9812018-01-22 17:48:56 -08001231 LOG(ERROR) << "ERROR: bad descriptor '" << type_descriptor << "'";
David Sehr7629f602016-08-07 16:01:51 -07001232 goto bail;
1233 }
1234 char* tmp_buf = reinterpret_cast<char*>(malloc(strlen(type_descriptor) + 1));
1235 const char* base = type_descriptor + 1;
1236 int arg_num = 0;
1237 while (*base != ')') {
1238 char* cp = tmp_buf;
1239 while (*base == '[') {
1240 *cp++ = *base++;
1241 }
1242 if (*base == 'L') {
1243 // Copy through ';'.
1244 do {
1245 *cp = *base++;
1246 } while (*cp++ != ';');
1247 } else {
1248 // Primitive char, copy it.
1249 if (strchr("ZBCSIFJD", *base) == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08001250 LOG(ERROR) << "ERROR: bad method signature '" << base << "'";
David Sehr7629f602016-08-07 16:01:51 -07001251 break; // while
1252 }
1253 *cp++ = *base++;
1254 }
1255 // Null terminate and display.
1256 *cp++ = '\0';
Orion Hodsonfe42d212018-08-24 14:01:14 +01001257 std::string dot(DescriptorToDot(tmp_buf));
David Sehr7629f602016-08-07 16:01:51 -07001258 fprintf(out_file_, "<parameter name=\"arg%d\" type=\"%s\">\n"
1259 "</parameter>\n", arg_num++, dot.c_str());
1260 } // while
1261 free(tmp_buf);
1262 if (constructor) {
1263 fprintf(out_file_, "</constructor>\n");
1264 } else {
1265 fprintf(out_file_, "</method>\n");
1266 }
1267 }
1268
1269 bail:
1270 free(type_descriptor);
1271 free(access_str);
1272}
1273
1274/*
1275 * Dumps a static (class) field.
1276 */
David Brazdil20c765f2018-10-27 21:45:15 +00001277void DexLayout::DumpSField(uint32_t idx,
1278 uint32_t flags,
1279 uint32_t hiddenapi_flags,
1280 int i,
1281 dex_ir::EncodedValue* init) {
David Sehr7629f602016-08-07 16:01:51 -07001282 // Bail for anything private if export only requested.
1283 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1284 return;
1285 }
1286
David Sehr2b5a38f2018-06-14 15:13:04 -07001287 dex_ir::FieldId* field_id = header_->FieldIds()[idx];
David Sehr7629f602016-08-07 16:01:51 -07001288 const char* name = field_id->Name()->Data();
1289 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
1290 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
1291 char* access_str = CreateAccessFlagStr(flags, kAccessForField);
1292
1293 if (options_.output_format_ == kOutputPlain) {
1294 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1295 fprintf(out_file_, " name : '%s'\n", name);
1296 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1297 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
David Brazdil20c765f2018-10-27 21:45:15 +00001298 if (hiddenapi_flags != 0u) {
David Brazdildcfa89b2018-10-31 11:04:10 +00001299 fprintf(out_file_,
1300 " hiddenapi : 0x%04x (%s)\n",
1301 hiddenapi_flags,
1302 GetHiddenapiFlagStr(hiddenapi_flags).c_str());
David Brazdil20c765f2018-10-27 21:45:15 +00001303 }
David Sehr7629f602016-08-07 16:01:51 -07001304 if (init != nullptr) {
1305 fputs(" value : ", out_file_);
1306 DumpEncodedValue(init);
1307 fputs("\n", out_file_);
1308 }
1309 } else if (options_.output_format_ == kOutputXml) {
1310 fprintf(out_file_, "<field name=\"%s\"\n", name);
Orion Hodsonfe42d212018-08-24 14:01:14 +01001311 std::string dot(DescriptorToDot(type_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001312 fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
1313 fprintf(out_file_, " transient=%s\n", QuotedBool((flags & kAccTransient) != 0));
1314 fprintf(out_file_, " volatile=%s\n", QuotedBool((flags & kAccVolatile) != 0));
1315 // The "value=" is not knowable w/o parsing annotations.
1316 fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
1317 fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
1318 // The "deprecated=" is not knowable w/o parsing annotations.
1319 fprintf(out_file_, " visibility=%s\n", QuotedVisibility(flags));
1320 if (init != nullptr) {
1321 fputs(" value=\"", out_file_);
1322 DumpEncodedValue(init);
1323 fputs("\"\n", out_file_);
1324 }
1325 fputs(">\n</field>\n", out_file_);
1326 }
1327
1328 free(access_str);
1329}
1330
1331/*
1332 * Dumps an instance field.
1333 */
David Brazdil20c765f2018-10-27 21:45:15 +00001334void DexLayout::DumpIField(uint32_t idx,
1335 uint32_t flags,
1336 uint32_t hiddenapi_flags,
1337 int i) {
1338 DumpSField(idx, flags, hiddenapi_flags, i, nullptr);
David Sehr7629f602016-08-07 16:01:51 -07001339}
1340
1341/*
David Sehr7629f602016-08-07 16:01:51 -07001342 * Dumps the class.
1343 *
1344 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1345 *
1346 * If "*last_package" is nullptr or does not match the current class' package,
1347 * the value will be replaced with a newly-allocated string.
1348 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001349void DexLayout::DumpClass(int idx, char** last_package) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001350 dex_ir::ClassDef* class_def = header_->ClassDefs()[idx];
David Sehr7629f602016-08-07 16:01:51 -07001351 // Omitting non-public class.
1352 if (options_.exports_only_ && (class_def->GetAccessFlags() & kAccPublic) == 0) {
1353 return;
1354 }
1355
1356 if (options_.show_section_headers_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001357 DumpClassDef(idx);
David Sehr7629f602016-08-07 16:01:51 -07001358 }
1359
1360 if (options_.show_annotations_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001361 DumpClassAnnotations(idx);
David Sehr7629f602016-08-07 16:01:51 -07001362 }
1363
David Sehr7629f602016-08-07 16:01:51 -07001364 // For the XML output, show the package name. Ideally we'd gather
1365 // up the classes, sort them, and dump them alphabetically so the
1366 // package name wouldn't jump around, but that's not a great plan
1367 // for something that needs to run on the device.
David Sehr2b5a38f2018-06-14 15:13:04 -07001368 const char* class_descriptor = header_->ClassDefs()[idx]->ClassType()->GetStringId()->Data();
David Sehr7629f602016-08-07 16:01:51 -07001369 if (!(class_descriptor[0] == 'L' &&
1370 class_descriptor[strlen(class_descriptor)-1] == ';')) {
1371 // Arrays and primitives should not be defined explicitly. Keep going?
Andreas Gampe221d9812018-01-22 17:48:56 -08001372 LOG(ERROR) << "Malformed class name '" << class_descriptor << "'";
David Sehr7629f602016-08-07 16:01:51 -07001373 } else if (options_.output_format_ == kOutputXml) {
1374 char* mangle = strdup(class_descriptor + 1);
1375 mangle[strlen(mangle)-1] = '\0';
1376
1377 // Reduce to just the package name.
1378 char* last_slash = strrchr(mangle, '/');
1379 if (last_slash != nullptr) {
1380 *last_slash = '\0';
1381 } else {
1382 *mangle = '\0';
1383 }
1384
1385 for (char* cp = mangle; *cp != '\0'; cp++) {
1386 if (*cp == '/') {
1387 *cp = '.';
1388 }
1389 } // for
1390
1391 if (*last_package == nullptr || strcmp(mangle, *last_package) != 0) {
1392 // Start of a new package.
1393 if (*last_package != nullptr) {
1394 fprintf(out_file_, "</package>\n");
1395 }
1396 fprintf(out_file_, "<package name=\"%s\"\n>\n", mangle);
1397 free(*last_package);
1398 *last_package = mangle;
1399 } else {
1400 free(mangle);
1401 }
1402 }
1403
1404 // General class information.
1405 char* access_str = CreateAccessFlagStr(class_def->GetAccessFlags(), kAccessForClass);
1406 const char* superclass_descriptor = nullptr;
1407 if (class_def->Superclass() != nullptr) {
1408 superclass_descriptor = class_def->Superclass()->GetStringId()->Data();
1409 }
1410 if (options_.output_format_ == kOutputPlain) {
1411 fprintf(out_file_, "Class #%d -\n", idx);
1412 fprintf(out_file_, " Class descriptor : '%s'\n", class_descriptor);
1413 fprintf(out_file_, " Access flags : 0x%04x (%s)\n",
1414 class_def->GetAccessFlags(), access_str);
1415 if (superclass_descriptor != nullptr) {
1416 fprintf(out_file_, " Superclass : '%s'\n", superclass_descriptor);
1417 }
1418 fprintf(out_file_, " Interfaces -\n");
1419 } else {
Orion Hodsonfe42d212018-08-24 14:01:14 +01001420 std::string dot(DescriptorClassToName(class_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001421 fprintf(out_file_, "<class name=\"%s\"\n", dot.c_str());
1422 if (superclass_descriptor != nullptr) {
Orion Hodsonfe42d212018-08-24 14:01:14 +01001423 dot = DescriptorToDot(superclass_descriptor);
David Sehr7629f602016-08-07 16:01:51 -07001424 fprintf(out_file_, " extends=\"%s\"\n", dot.c_str());
1425 }
1426 fprintf(out_file_, " interface=%s\n",
1427 QuotedBool((class_def->GetAccessFlags() & kAccInterface) != 0));
1428 fprintf(out_file_, " abstract=%s\n",
1429 QuotedBool((class_def->GetAccessFlags() & kAccAbstract) != 0));
1430 fprintf(out_file_, " static=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccStatic) != 0));
1431 fprintf(out_file_, " final=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccFinal) != 0));
1432 // The "deprecated=" not knowable w/o parsing annotations.
1433 fprintf(out_file_, " visibility=%s\n", QuotedVisibility(class_def->GetAccessFlags()));
1434 fprintf(out_file_, ">\n");
1435 }
1436
1437 // Interfaces.
Jeff Haocc829592017-03-14 16:13:39 -07001438 const dex_ir::TypeList* interfaces = class_def->Interfaces();
David Sehr853a8e12016-09-01 13:03:50 -07001439 if (interfaces != nullptr) {
Jeff Haocc829592017-03-14 16:13:39 -07001440 const dex_ir::TypeIdVector* interfaces_vector = interfaces->GetTypeList();
1441 for (uint32_t i = 0; i < interfaces_vector->size(); i++) {
1442 DumpInterface((*interfaces_vector)[i], i);
David Sehr853a8e12016-09-01 13:03:50 -07001443 } // for
1444 }
David Sehr7629f602016-08-07 16:01:51 -07001445
1446 // Fields and methods.
1447 dex_ir::ClassData* class_data = class_def->GetClassData();
1448 // Prepare data for static fields.
Jeff Hao3ab96b42016-09-09 18:35:01 -07001449 dex_ir::EncodedArrayItem* static_values = class_def->StaticValues();
1450 dex_ir::EncodedValueVector* encoded_values =
1451 static_values == nullptr ? nullptr : static_values->GetEncodedValues();
1452 const uint32_t encoded_values_size = (encoded_values == nullptr) ? 0 : encoded_values->size();
David Sehr7629f602016-08-07 16:01:51 -07001453
1454 // Static fields.
1455 if (options_.output_format_ == kOutputPlain) {
1456 fprintf(out_file_, " Static fields -\n");
1457 }
David Sehr853a8e12016-09-01 13:03:50 -07001458 if (class_data != nullptr) {
1459 dex_ir::FieldItemVector* static_fields = class_data->StaticFields();
1460 if (static_fields != nullptr) {
1461 for (uint32_t i = 0; i < static_fields->size(); i++) {
David Sehrd83437c2018-06-11 14:06:23 -07001462 DumpSField((*static_fields)[i].GetFieldId()->GetIndex(),
1463 (*static_fields)[i].GetAccessFlags(),
David Brazdil20c765f2018-10-27 21:45:15 +00001464 dex_ir::HiddenapiClassData::GetFlags(header_, class_def, &(*static_fields)[i]),
David Sehr853a8e12016-09-01 13:03:50 -07001465 i,
Jeff Hao3ab96b42016-09-09 18:35:01 -07001466 i < encoded_values_size ? (*encoded_values)[i].get() : nullptr);
David Sehr853a8e12016-09-01 13:03:50 -07001467 } // for
1468 }
1469 }
David Sehr7629f602016-08-07 16:01:51 -07001470
1471 // Instance fields.
1472 if (options_.output_format_ == kOutputPlain) {
1473 fprintf(out_file_, " Instance fields -\n");
1474 }
David Sehr853a8e12016-09-01 13:03:50 -07001475 if (class_data != nullptr) {
1476 dex_ir::FieldItemVector* instance_fields = class_data->InstanceFields();
1477 if (instance_fields != nullptr) {
1478 for (uint32_t i = 0; i < instance_fields->size(); i++) {
David Sehrd83437c2018-06-11 14:06:23 -07001479 DumpIField((*instance_fields)[i].GetFieldId()->GetIndex(),
1480 (*instance_fields)[i].GetAccessFlags(),
David Brazdil20c765f2018-10-27 21:45:15 +00001481 dex_ir::HiddenapiClassData::GetFlags(header_, class_def, &(*instance_fields)[i]),
David Sehr853a8e12016-09-01 13:03:50 -07001482 i);
1483 } // for
1484 }
1485 }
David Sehr7629f602016-08-07 16:01:51 -07001486
1487 // Direct methods.
1488 if (options_.output_format_ == kOutputPlain) {
1489 fprintf(out_file_, " Direct methods -\n");
1490 }
David Sehr853a8e12016-09-01 13:03:50 -07001491 if (class_data != nullptr) {
1492 dex_ir::MethodItemVector* direct_methods = class_data->DirectMethods();
1493 if (direct_methods != nullptr) {
1494 for (uint32_t i = 0; i < direct_methods->size(); i++) {
David Sehrd83437c2018-06-11 14:06:23 -07001495 DumpMethod((*direct_methods)[i].GetMethodId()->GetIndex(),
1496 (*direct_methods)[i].GetAccessFlags(),
David Brazdil20c765f2018-10-27 21:45:15 +00001497 dex_ir::HiddenapiClassData::GetFlags(header_, class_def, &(*direct_methods)[i]),
David Sehrd83437c2018-06-11 14:06:23 -07001498 (*direct_methods)[i].GetCodeItem(),
David Brazdil20c765f2018-10-27 21:45:15 +00001499 i);
David Sehr853a8e12016-09-01 13:03:50 -07001500 } // for
1501 }
1502 }
David Sehr7629f602016-08-07 16:01:51 -07001503
1504 // Virtual methods.
1505 if (options_.output_format_ == kOutputPlain) {
1506 fprintf(out_file_, " Virtual methods -\n");
1507 }
David Sehr853a8e12016-09-01 13:03:50 -07001508 if (class_data != nullptr) {
1509 dex_ir::MethodItemVector* virtual_methods = class_data->VirtualMethods();
1510 if (virtual_methods != nullptr) {
1511 for (uint32_t i = 0; i < virtual_methods->size(); i++) {
David Sehrd83437c2018-06-11 14:06:23 -07001512 DumpMethod((*virtual_methods)[i].GetMethodId()->GetIndex(),
1513 (*virtual_methods)[i].GetAccessFlags(),
David Brazdil20c765f2018-10-27 21:45:15 +00001514 dex_ir::HiddenapiClassData::GetFlags(header_, class_def, &(*virtual_methods)[i]),
David Sehrd83437c2018-06-11 14:06:23 -07001515 (*virtual_methods)[i].GetCodeItem(),
David Sehr853a8e12016-09-01 13:03:50 -07001516 i);
1517 } // for
1518 }
1519 }
David Sehr7629f602016-08-07 16:01:51 -07001520
1521 // End of class.
1522 if (options_.output_format_ == kOutputPlain) {
1523 const char* file_name = "unknown";
1524 if (class_def->SourceFile() != nullptr) {
1525 file_name = class_def->SourceFile()->Data();
1526 }
1527 const dex_ir::StringId* source_file = class_def->SourceFile();
1528 fprintf(out_file_, " source_file_idx : %d (%s)\n\n",
Jeff Hao3ab96b42016-09-09 18:35:01 -07001529 source_file == nullptr ? 0xffffffffU : source_file->GetIndex(), file_name);
David Sehr7629f602016-08-07 16:01:51 -07001530 } else if (options_.output_format_ == kOutputXml) {
1531 fprintf(out_file_, "</class>\n");
1532 }
1533
1534 free(access_str);
1535}
1536
Jeff Haoea7c6292016-11-14 18:10:16 -08001537void DexLayout::DumpDexFile() {
David Sehr7629f602016-08-07 16:01:51 -07001538 // Headers.
1539 if (options_.show_file_headers_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001540 DumpFileHeader();
David Sehr7629f602016-08-07 16:01:51 -07001541 }
1542
1543 // Open XML context.
1544 if (options_.output_format_ == kOutputXml) {
1545 fprintf(out_file_, "<api>\n");
1546 }
1547
1548 // Iterate over all classes.
1549 char* package = nullptr;
David Sehr2b5a38f2018-06-14 15:13:04 -07001550 const uint32_t class_defs_size = header_->ClassDefs().Size();
David Sehr7629f602016-08-07 16:01:51 -07001551 for (uint32_t i = 0; i < class_defs_size; i++) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001552 DumpClass(i, &package);
David Sehr7629f602016-08-07 16:01:51 -07001553 } // for
1554
1555 // Free the last package allocated.
1556 if (package != nullptr) {
1557 fprintf(out_file_, "</package>\n");
1558 free(package);
1559 }
1560
1561 // Close XML context.
1562 if (options_.output_format_ == kOutputXml) {
1563 fprintf(out_file_, "</api>\n");
1564 }
Jeff Haoea7c6292016-11-14 18:10:16 -08001565}
Jeff Hao3ab96b42016-09-09 18:35:01 -07001566
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001567void DexLayout::LayoutClassDefsAndClassData(const DexFile* dex_file) {
Jeff Hao042e8982016-10-19 11:17:11 -07001568 std::vector<dex_ir::ClassDef*> new_class_def_order;
David Sehr2b5a38f2018-06-14 15:13:04 -07001569 for (auto& class_def : header_->ClassDefs()) {
Jeff Hao042e8982016-10-19 11:17:11 -07001570 dex::TypeIndex type_idx(class_def->ClassType()->GetIndex());
1571 if (info_->ContainsClass(*dex_file, type_idx)) {
1572 new_class_def_order.push_back(class_def.get());
1573 }
1574 }
David Sehr2b5a38f2018-06-14 15:13:04 -07001575 for (auto& class_def : header_->ClassDefs()) {
Jeff Hao042e8982016-10-19 11:17:11 -07001576 dex::TypeIndex type_idx(class_def->ClassType()->GetIndex());
1577 if (!info_->ContainsClass(*dex_file, type_idx)) {
1578 new_class_def_order.push_back(class_def.get());
1579 }
1580 }
Jeff Haoe17f5892017-02-23 16:14:04 -08001581 std::unordered_set<dex_ir::ClassData*> visited_class_data;
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001582 size_t class_data_index = 0;
David Sehr2b5a38f2018-06-14 15:13:04 -07001583 auto& class_datas = header_->ClassDatas();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001584 for (dex_ir::ClassDef* class_def : new_class_def_order) {
Jeff Haoe17f5892017-02-23 16:14:04 -08001585 dex_ir::ClassData* class_data = class_def->GetClassData();
1586 if (class_data != nullptr && visited_class_data.find(class_data) == visited_class_data.end()) {
Jeff Haoe17f5892017-02-23 16:14:04 -08001587 visited_class_data.insert(class_data);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001588 // Overwrite the existing vector with the new ordering, note that the sets of objects are
1589 // equivalent, but the order changes. This is why this is not a memory leak.
1590 // TODO: Consider cleaning this up with a shared_ptr.
Andreas Gampeafaf7f82018-10-16 11:32:38 -07001591 class_datas[class_data_index].release(); // NOLINT b/117926937
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001592 class_datas[class_data_index].reset(class_data);
1593 ++class_data_index;
Jeff Hao042e8982016-10-19 11:17:11 -07001594 }
1595 }
David Sehr2b5a38f2018-06-14 15:13:04 -07001596 CHECK_EQ(class_data_index, class_datas.Size());
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001597
Mathieu Chartier2c4b0842017-12-13 11:49:51 -08001598 if (DexLayout::kChangeClassDefOrder) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001599 // This currently produces dex files that violate the spec since the super class class_def is
1600 // supposed to occur before any subclasses.
David Sehr2b5a38f2018-06-14 15:13:04 -07001601 dex_ir::CollectionVector<dex_ir::ClassDef>& class_defs = header_->ClassDefs();
1602 CHECK_EQ(new_class_def_order.size(), class_defs.Size());
1603 for (size_t i = 0; i < class_defs.Size(); ++i) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001604 // Overwrite the existing vector with the new ordering, note that the sets of objects are
1605 // equivalent, but the order changes. This is why this is not a memory leak.
1606 // TODO: Consider cleaning this up with a shared_ptr.
Andreas Gampeafaf7f82018-10-16 11:32:38 -07001607 class_defs[i].release(); // NOLINT b/117926937
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001608 class_defs[i].reset(new_class_def_order[i]);
1609 }
1610 }
Jeff Hao042e8982016-10-19 11:17:11 -07001611}
1612
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001613void DexLayout::LayoutStringData(const DexFile* dex_file) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001614 const size_t num_strings = header_->StringIds().Size();
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001615 std::vector<bool> is_shorty(num_strings, false);
1616 std::vector<bool> from_hot_method(num_strings, false);
David Sehr2b5a38f2018-06-14 15:13:04 -07001617 for (auto& class_def : header_->ClassDefs()) {
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001618 // 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 -07001619 // as hot. Add its super class and interfaces as well, which can be used during initialization.
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001620 const bool is_profile_class =
1621 info_->ContainsClass(*dex_file, dex::TypeIndex(class_def->ClassType()->GetIndex()));
1622 if (is_profile_class) {
1623 from_hot_method[class_def->ClassType()->GetStringId()->GetIndex()] = true;
Jeff Haoacc83d72017-07-06 17:51:01 -07001624 const dex_ir::TypeId* superclass = class_def->Superclass();
1625 if (superclass != nullptr) {
1626 from_hot_method[superclass->GetStringId()->GetIndex()] = true;
1627 }
1628 const dex_ir::TypeList* interfaces = class_def->Interfaces();
1629 if (interfaces != nullptr) {
1630 for (const dex_ir::TypeId* interface_type : *interfaces->GetTypeList()) {
1631 from_hot_method[interface_type->GetStringId()->GetIndex()] = true;
1632 }
1633 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001634 }
1635 dex_ir::ClassData* data = class_def->GetClassData();
1636 if (data == nullptr) {
1637 continue;
1638 }
1639 for (size_t i = 0; i < 2; ++i) {
1640 for (auto& method : *(i == 0 ? data->DirectMethods() : data->VirtualMethods())) {
David Sehrd83437c2018-06-11 14:06:23 -07001641 const dex_ir::MethodId* method_id = method.GetMethodId();
1642 dex_ir::CodeItem* code_item = method.GetCodeItem();
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001643 if (code_item == nullptr) {
1644 continue;
1645 }
1646 const bool is_clinit = is_profile_class &&
David Sehrd83437c2018-06-11 14:06:23 -07001647 (method.GetAccessFlags() & kAccConstructor) != 0 &&
1648 (method.GetAccessFlags() & kAccStatic) != 0;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001649 const bool method_executed = is_clinit ||
Mathieu Chartiere46f3a82017-06-19 19:54:12 -07001650 info_->GetMethodHotness(MethodReference(dex_file, method_id->GetIndex())).IsInProfile();
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001651 if (!method_executed) {
1652 continue;
1653 }
1654 is_shorty[method_id->Proto()->Shorty()->GetIndex()] = true;
1655 dex_ir::CodeFixups* fixups = code_item->GetCodeFixups();
1656 if (fixups == nullptr) {
1657 continue;
1658 }
Jeff Haoacc83d72017-07-06 17:51:01 -07001659 // Add const-strings.
Vladimir Marko219cb902017-12-07 16:20:39 +00001660 for (dex_ir::StringId* id : fixups->StringIds()) {
Jeff Haoacc83d72017-07-06 17:51:01 -07001661 from_hot_method[id->GetIndex()] = true;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001662 }
Jeff Haoacc83d72017-07-06 17:51:01 -07001663 // Add field classes, names, and types.
Vladimir Marko219cb902017-12-07 16:20:39 +00001664 for (dex_ir::FieldId* id : fixups->FieldIds()) {
Jeff Haoacc83d72017-07-06 17:51:01 -07001665 // TODO: Only visit field ids from static getters and setters.
1666 from_hot_method[id->Class()->GetStringId()->GetIndex()] = true;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001667 from_hot_method[id->Name()->GetIndex()] = true;
1668 from_hot_method[id->Type()->GetStringId()->GetIndex()] = true;
1669 }
Jeff Haoacc83d72017-07-06 17:51:01 -07001670 // For clinits, add referenced method classes, names, and protos.
1671 if (is_clinit) {
Vladimir Marko219cb902017-12-07 16:20:39 +00001672 for (dex_ir::MethodId* id : fixups->MethodIds()) {
Jeff Haoacc83d72017-07-06 17:51:01 -07001673 from_hot_method[id->Class()->GetStringId()->GetIndex()] = true;
1674 from_hot_method[id->Name()->GetIndex()] = true;
1675 is_shorty[id->Proto()->Shorty()->GetIndex()] = true;
1676 }
1677 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001678 }
1679 }
1680 }
1681 // Sort string data by specified order.
1682 std::vector<dex_ir::StringId*> string_ids;
David Sehr2b5a38f2018-06-14 15:13:04 -07001683 for (auto& string_id : header_->StringIds()) {
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001684 string_ids.push_back(string_id.get());
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001685 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001686 std::sort(string_ids.begin(),
1687 string_ids.end(),
1688 [&is_shorty, &from_hot_method](const dex_ir::StringId* a,
1689 const dex_ir::StringId* b) {
1690 const bool a_is_hot = from_hot_method[a->GetIndex()];
1691 const bool b_is_hot = from_hot_method[b->GetIndex()];
1692 if (a_is_hot != b_is_hot) {
1693 return a_is_hot < b_is_hot;
1694 }
1695 // After hot methods are partitioned, subpartition shorties.
1696 const bool a_is_shorty = is_shorty[a->GetIndex()];
1697 const bool b_is_shorty = is_shorty[b->GetIndex()];
1698 if (a_is_shorty != b_is_shorty) {
1699 return a_is_shorty < b_is_shorty;
1700 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001701 // Order by index by default.
1702 return a->GetIndex() < b->GetIndex();
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001703 });
David Sehr2b5a38f2018-06-14 15:13:04 -07001704 auto& string_datas = header_->StringDatas();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001705 // Now we know what order we want the string data, reorder them.
1706 size_t data_index = 0;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001707 for (dex_ir::StringId* string_id : string_ids) {
Andreas Gampeafaf7f82018-10-16 11:32:38 -07001708 string_datas[data_index].release(); // NOLINT b/117926937
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001709 string_datas[data_index].reset(string_id->DataItem());
1710 ++data_index;
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001711 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001712 if (kIsDebugBuild) {
1713 std::unordered_set<dex_ir::StringData*> visited;
1714 for (const std::unique_ptr<dex_ir::StringData>& data : string_datas) {
1715 visited.insert(data.get());
1716 }
David Sehr2b5a38f2018-06-14 15:13:04 -07001717 for (auto& string_id : header_->StringIds()) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001718 CHECK(visited.find(string_id->DataItem()) != visited.end());
1719 }
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001720 }
David Sehr2b5a38f2018-06-14 15:13:04 -07001721 CHECK_EQ(data_index, string_datas.Size());
Mathieu Chartierfa0aa092017-03-27 15:43:54 -07001722}
1723
Jeff Haoe17f5892017-02-23 16:14:04 -08001724// Orders code items according to specified class data ordering.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001725void DexLayout::LayoutCodeItems(const DexFile* dex_file) {
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001726 static constexpr InvokeType invoke_types[] = {
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001727 kDirect,
1728 kVirtual
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001729 };
1730
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001731 std::unordered_map<dex_ir::CodeItem*, LayoutType>& code_item_layout =
1732 layout_hotness_info_.code_item_layout_;
1733
1734 // Assign hotness flags to all code items.
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001735 for (InvokeType invoke_type : invoke_types) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001736 for (auto& class_def : header_->ClassDefs()) {
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001737 const bool is_profile_class =
1738 info_->ContainsClass(*dex_file, dex::TypeIndex(class_def->ClassType()->GetIndex()));
1739
1740 // Skip classes that are not defined in this dex file.
1741 dex_ir::ClassData* class_data = class_def->GetClassData();
1742 if (class_data == nullptr) {
1743 continue;
Jeff Haoe17f5892017-02-23 16:14:04 -08001744 }
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001745 for (auto& method : *(invoke_type == InvokeType::kDirect
1746 ? class_data->DirectMethods()
1747 : class_data->VirtualMethods())) {
David Sehrd83437c2018-06-11 14:06:23 -07001748 const dex_ir::MethodId *method_id = method.GetMethodId();
1749 dex_ir::CodeItem *code_item = method.GetCodeItem();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001750 if (code_item == nullptr) {
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001751 continue;
1752 }
1753 // Separate executed methods (clinits and profiled methods) from unexecuted methods.
David Sehrd83437c2018-06-11 14:06:23 -07001754 const bool is_clinit = (method.GetAccessFlags() & kAccConstructor) != 0 &&
1755 (method.GetAccessFlags() & kAccStatic) != 0;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001756 const bool is_startup_clinit = is_profile_class && is_clinit;
1757 using Hotness = ProfileCompilationInfo::MethodHotness;
1758 Hotness hotness = info_->GetMethodHotness(MethodReference(dex_file, method_id->GetIndex()));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001759 LayoutType state = LayoutType::kLayoutTypeUnused;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001760 if (hotness.IsHot()) {
1761 // Hot code is compiled, maybe one day it won't be accessed. So lay it out together for
1762 // now.
Mathieu Chartier120aa282017-08-05 16:03:03 -07001763 state = LayoutType::kLayoutTypeHot;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001764 } else if (is_startup_clinit || hotness.GetFlags() == Hotness::kFlagStartup) {
1765 // Startup clinit or a method that only has the startup flag.
Mathieu Chartier120aa282017-08-05 16:03:03 -07001766 state = LayoutType::kLayoutTypeStartupOnly;
Mathieu Chartier7c1be8b2017-06-15 13:56:05 -07001767 } else if (is_clinit) {
Mathieu Chartier120aa282017-08-05 16:03:03 -07001768 state = LayoutType::kLayoutTypeUsedOnce;
Mathieu Chartiere46f3a82017-06-19 19:54:12 -07001769 } else if (hotness.IsInProfile()) {
Mathieu Chartier120aa282017-08-05 16:03:03 -07001770 state = LayoutType::kLayoutTypeSometimesUsed;
Jeff Hao206cbaa2017-06-07 19:11:01 -07001771 }
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001772 auto it = code_item_layout.emplace(code_item, state);
1773 if (!it.second) {
1774 LayoutType& layout_type = it.first->second;
1775 // Already exists, merge the hotness.
1776 layout_type = MergeLayoutType(layout_type, state);
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001777 }
1778 }
1779 }
Shubham Ajmera36a282b2017-04-03 10:04:28 -07001780 }
Jeff Hao042e8982016-10-19 11:17:11 -07001781
David Sehr2b5a38f2018-06-14 15:13:04 -07001782 const auto& code_items = header_->CodeItems();
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001783 if (VLOG_IS_ON(dex)) {
1784 size_t layout_count[static_cast<size_t>(LayoutType::kLayoutTypeCount)] = {};
1785 for (const std::unique_ptr<dex_ir::CodeItem>& code_item : code_items) {
1786 auto it = code_item_layout.find(code_item.get());
1787 DCHECK(it != code_item_layout.end());
1788 ++layout_count[static_cast<size_t>(it->second)];
1789 }
1790 for (size_t i = 0; i < static_cast<size_t>(LayoutType::kLayoutTypeCount); ++i) {
1791 LOG(INFO) << "Code items in category " << i << " count=" << layout_count[i];
Jeff Haoe17f5892017-02-23 16:14:04 -08001792 }
1793 }
Jeff Hao042e8982016-10-19 11:17:11 -07001794
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001795 // Sort the code items vector by new layout. The writing process will take care of calculating
1796 // all the offsets. Stable sort to preserve any existing locality that might be there.
1797 std::stable_sort(code_items.begin(),
1798 code_items.end(),
1799 [&](const std::unique_ptr<dex_ir::CodeItem>& a,
1800 const std::unique_ptr<dex_ir::CodeItem>& b) {
1801 auto it_a = code_item_layout.find(a.get());
1802 auto it_b = code_item_layout.find(b.get());
1803 DCHECK(it_a != code_item_layout.end());
1804 DCHECK(it_b != code_item_layout.end());
1805 const LayoutType layout_type_a = it_a->second;
1806 const LayoutType layout_type_b = it_b->second;
1807 return layout_type_a < layout_type_b;
1808 });
Jeff Hao042e8982016-10-19 11:17:11 -07001809}
1810
1811void DexLayout::LayoutOutputFile(const DexFile* dex_file) {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001812 LayoutStringData(dex_file);
1813 LayoutClassDefsAndClassData(dex_file);
1814 LayoutCodeItems(dex_file);
Jeff Hao042e8982016-10-19 11:17:11 -07001815}
1816
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001817bool DexLayout::OutputDexFile(const DexFile* input_dex_file,
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001818 bool compute_offsets,
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001819 std::unique_ptr<DexContainer>* dex_container,
1820 std::string* error_msg) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001821 const std::string& dex_file_location = input_dex_file->GetLocation();
Jeff Haoea7c6292016-11-14 18:10:16 -08001822 std::unique_ptr<File> new_file;
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001823 // If options_.output_dex_directory_ is non null, we are outputting to a file.
1824 if (options_.output_dex_directory_ != nullptr) {
Jeff Haoa8621002016-10-04 18:13:44 +00001825 std::string output_location(options_.output_dex_directory_);
Mathieu Chartier41468402018-08-29 11:39:00 -07001826 const size_t last_slash = dex_file_location.rfind('/');
Jeff Haoea7c6292016-11-14 18:10:16 -08001827 std::string dex_file_directory = dex_file_location.substr(0, last_slash + 1);
1828 if (output_location == dex_file_directory) {
1829 output_location = dex_file_location + ".new";
Jeff Haoea7c6292016-11-14 18:10:16 -08001830 } else {
Mathieu Chartier41468402018-08-29 11:39:00 -07001831 if (!output_location.empty() && output_location.back() != '/') {
1832 output_location += "/";
1833 }
1834 const size_t separator = dex_file_location.rfind('!');
1835 if (separator != std::string::npos) {
1836 output_location += dex_file_location.substr(separator + 1);
1837 } else {
1838 output_location += "classes.dex";
1839 }
Jeff Haoea7c6292016-11-14 18:10:16 -08001840 }
1841 new_file.reset(OS::CreateEmptyFile(output_location.c_str()));
Jeff Hao3ba51e82017-04-12 16:14:54 -07001842 if (new_file == nullptr) {
1843 LOG(ERROR) << "Could not create dex writer output file: " << output_location;
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001844 return false;
Jeff Hao3ba51e82017-04-12 16:14:54 -07001845 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001846 }
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001847 if (!DexWriter::Output(this, dex_container, compute_offsets, error_msg)) {
1848 return false;
1849 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001850 if (new_file != nullptr) {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001851 DexContainer* const container = dex_container->get();
1852 DexContainer::Section* const main_section = container->GetMainSection();
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001853 if (!new_file->WriteFully(main_section->Begin(), main_section->Size())) {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001854 LOG(ERROR) << "Failed to write main section for dex file " << dex_file_location;
1855 new_file->Erase();
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001856 return false;
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001857 }
1858 DexContainer::Section* const data_section = container->GetDataSection();
1859 if (!new_file->WriteFully(data_section->Begin(), data_section->Size())) {
1860 LOG(ERROR) << "Failed to write data section for dex file " << dex_file_location;
David Sehr7639cdc2017-04-15 10:06:21 -07001861 new_file->Erase();
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001862 return false;
David Sehr7639cdc2017-04-15 10:06:21 -07001863 }
Jeff Haoea7c6292016-11-14 18:10:16 -08001864 UNUSED(new_file->FlushCloseOrErase());
1865 }
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001866 return true;
Jeff Haoea7c6292016-11-14 18:10:16 -08001867}
1868
1869/*
1870 * Dumps the requested sections of the file.
1871 */
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001872bool DexLayout::ProcessDexFile(const char* file_name,
Jeff Haoea7c6292016-11-14 18:10:16 -08001873 const DexFile* dex_file,
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001874 size_t dex_file_index,
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001875 std::unique_ptr<DexContainer>* dex_container,
1876 std::string* error_msg) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001877 const bool has_output_container = dex_container != nullptr;
1878 const bool output = options_.output_dex_directory_ != nullptr || has_output_container;
1879
David Sehr2b5a38f2018-06-14 15:13:04 -07001880 // Try to avoid eagerly assigning offsets to find bugs since Offset will abort if the offset
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001881 // is unassigned.
1882 bool eagerly_assign_offsets = false;
1883 if (options_.visualize_pattern_ || options_.show_section_statistics_ || options_.dump_) {
1884 // These options required the offsets for dumping purposes.
1885 eagerly_assign_offsets = true;
1886 }
Mathieu Chartier75175552018-01-25 11:23:01 -08001887 std::unique_ptr<dex_ir::Header> header(dex_ir::DexIrBuilder(*dex_file,
1888 eagerly_assign_offsets,
1889 GetOptions()));
Jeff Haoea7c6292016-11-14 18:10:16 -08001890 SetHeader(header.get());
1891
1892 if (options_.verbose_) {
1893 fprintf(out_file_, "Opened '%s', DEX version '%.3s'\n",
1894 file_name, dex_file->GetHeader().magic_ + 4);
1895 }
1896
1897 if (options_.visualize_pattern_) {
1898 VisualizeDexLayout(header_, dex_file, dex_file_index, info_);
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001899 return true;
Jeff Haoea7c6292016-11-14 18:10:16 -08001900 }
1901
David Sehr93357492017-03-09 08:02:44 -08001902 if (options_.show_section_statistics_) {
1903 ShowDexSectionStatistics(header_, dex_file_index);
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001904 return true;
David Sehr93357492017-03-09 08:02:44 -08001905 }
1906
Jeff Haoea7c6292016-11-14 18:10:16 -08001907 // Dump dex file.
1908 if (options_.dump_) {
1909 DumpDexFile();
1910 }
1911
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001912 // In case we are outputting to a file, keep it open so we can verify.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001913 if (output) {
1914 // Layout information about what strings and code items are hot. Used by the writing process
1915 // to generate the sections that are stored in the oat file.
1916 bool do_layout = info_ != nullptr;
1917 if (do_layout) {
Jeff Hao042e8982016-10-19 11:17:11 -07001918 LayoutOutputFile(dex_file);
1919 }
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001920 // The output needs a dex container, use a temporary one.
1921 std::unique_ptr<DexContainer> temp_container;
1922 if (dex_container == nullptr) {
1923 dex_container = &temp_container;
1924 }
Mathieu Chartier21cf2582018-01-08 17:09:48 -08001925 // If we didn't set the offsets eagerly, we definitely need to compute them here.
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001926 if (!OutputDexFile(dex_file, do_layout || !eagerly_assign_offsets, dex_container, error_msg)) {
1927 return false;
1928 }
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001929
1930 // Clear header before verifying to reduce peak RAM usage.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001931 const size_t file_size = header_->FileSize();
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001932 header.reset();
1933
1934 // Verify the output dex file's structure, only enabled by default for debug builds.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08001935 if (options_.verify_output_ && has_output_container) {
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001936 std::string location = "memory mapped file for " + std::string(file_name);
Mathieu Chartier8740c662018-01-11 14:50:02 -08001937 // Dex file verifier cannot handle compact dex.
1938 bool verify = options_.compact_dex_level_ == CompactDexLevel::kCompactDexLevelNone;
Mathieu Chartier818cb802018-05-11 05:30:16 +00001939 const ArtDexFileLoader dex_file_loader;
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001940 DexContainer::Section* const main_section = (*dex_container)->GetMainSection();
1941 DexContainer::Section* const data_section = (*dex_container)->GetDataSection();
1942 DCHECK_EQ(file_size, main_section->Size())
1943 << main_section->Size() << " " << data_section->Size();
David Sehr013fd802018-01-11 22:55:24 -08001944 std::unique_ptr<const DexFile> output_dex_file(
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001945 dex_file_loader.OpenWithDataSection(
1946 main_section->Begin(),
1947 main_section->Size(),
1948 data_section->Begin(),
1949 data_section->Size(),
1950 location,
Andreas Gampe9b031f72018-10-04 11:03:34 -07001951 /* location_checksum= */ 0,
1952 /*oat_dex_file=*/ nullptr,
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001953 verify,
Andreas Gampe9b031f72018-10-04 11:03:34 -07001954 /*verify_checksum=*/ false,
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001955 error_msg));
1956 CHECK(output_dex_file != nullptr) << "Failed to re-open output file:" << *error_msg;
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001957
1958 // Do IR-level comparison between input and output. This check ignores potential differences
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001959 // due to layout, so offsets are not checked. Instead, it checks the data contents of each
1960 // item.
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001961 //
1962 // Regenerate output IR to catch any bugs that might happen during writing.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001963 std::unique_ptr<dex_ir::Header> output_header(
1964 dex_ir::DexIrBuilder(*output_dex_file,
Andreas Gampe9b031f72018-10-04 11:03:34 -07001965 /*eagerly_assign_offsets=*/ true,
Mathieu Chartier75175552018-01-25 11:23:01 -08001966 GetOptions()));
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001967 std::unique_ptr<dex_ir::Header> orig_header(
1968 dex_ir::DexIrBuilder(*dex_file,
Andreas Gampe9b031f72018-10-04 11:03:34 -07001969 /*eagerly_assign_offsets=*/ true,
Mathieu Chartier75175552018-01-25 11:23:01 -08001970 GetOptions()));
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001971 CHECK(VerifyOutputDexFile(output_header.get(), orig_header.get(), error_msg)) << *error_msg;
Mathieu Chartier2ef3b882017-10-20 19:50:39 -07001972 }
Jeff Hao3ab96b42016-09-09 18:35:01 -07001973 }
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001974 return true;
David Sehr7629f602016-08-07 16:01:51 -07001975}
1976
1977/*
1978 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
1979 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001980int DexLayout::ProcessFile(const char* file_name) {
David Sehr7629f602016-08-07 16:01:51 -07001981 if (options_.verbose_) {
1982 fprintf(out_file_, "Processing '%s'...\n", file_name);
1983 }
1984
1985 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
1986 // all of which are Zip archives with "classes.dex" inside.
1987 const bool verify_checksum = !options_.ignore_bad_checksum_;
1988 std::string error_msg;
Mathieu Chartier818cb802018-05-11 05:30:16 +00001989 const ArtDexFileLoader dex_file_loader;
David Sehr7629f602016-08-07 16:01:51 -07001990 std::vector<std::unique_ptr<const DexFile>> dex_files;
Mathieu Chartier818cb802018-05-11 05:30:16 +00001991 if (!dex_file_loader.Open(
Andreas Gampe9b031f72018-10-04 11:03:34 -07001992 file_name, file_name, /* verify= */ true, verify_checksum, &error_msg, &dex_files)) {
David Sehr7629f602016-08-07 16:01:51 -07001993 // Display returned error message to user. Note that this error behavior
1994 // differs from the error messages shown by the original Dalvik dexdump.
Andreas Gampe221d9812018-01-22 17:48:56 -08001995 LOG(ERROR) << error_msg;
David Sehr7629f602016-08-07 16:01:51 -07001996 return -1;
1997 }
1998
1999 // Success. Either report checksum verification or process
2000 // all dex files found in given file.
2001 if (options_.checksum_only_) {
2002 fprintf(out_file_, "Checksum verified\n");
2003 } else {
2004 for (size_t i = 0; i < dex_files.size(); i++) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -08002005 // Pass in a null container to avoid output by default.
Mathieu Chartier05f90d12018-02-07 13:47:17 -08002006 if (!ProcessDexFile(file_name,
2007 dex_files[i].get(),
2008 i,
Andreas Gampe9b031f72018-10-04 11:03:34 -07002009 /*dex_container=*/ nullptr,
Mathieu Chartier05f90d12018-02-07 13:47:17 -08002010 &error_msg)) {
2011 LOG(WARNING) << "Failed to run dex file " << i << " in " << file_name << " : " << error_msg;
2012 }
David Sehr7629f602016-08-07 16:01:51 -07002013 }
2014 }
2015 return 0;
2016}
2017
2018} // namespace art