blob: 09f0b20ca19fee93a9a7626004ce58749165f267 [file] [log] [blame]
David Sehr7629f602016-08-07 16:01:51 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Implementation file of the dexlayout utility.
17 *
18 * This is a tool to read dex files into an internal representation,
19 * reorganize the representation, and emit dex files with a better
20 * file layout.
21 */
22
23#include "dexlayout.h"
24
25#include <inttypes.h>
26#include <stdio.h>
Andreas Gampe0dfc3152017-04-24 07:58:06 -070027#include <sys/mman.h> // For the PROT_* and MAP_* constants.
David Sehr7629f602016-08-07 16:01:51 -070028
29#include <iostream>
30#include <memory>
31#include <sstream>
32#include <vector>
33
Andreas Gampe46ee31b2016-12-14 10:11:49 -080034#include "android-base/stringprintf.h"
35
Andreas Gampe57943812017-12-06 21:39:13 -080036#include "base/logging.h" // For VLOG_IS_ON.
David Sehr79e26072018-04-06 17:58:50 -070037#include "base/mem_map.h"
David Sehrc431b9d2018-03-02 12:01:51 -080038#include "base/os.h"
39#include "base/utils.h"
Mathieu Chartier818cb802018-05-11 05:30:16 +000040#include "dex/art_dex_file_loader.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080041#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080042#include "dex/dex_file-inl.h"
43#include "dex/dex_file_layout.h"
44#include "dex/dex_file_loader.h"
45#include "dex/dex_file_types.h"
46#include "dex/dex_file_verifier.h"
47#include "dex/dex_instruction-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070048#include "dex_ir_builder.h"
Jeff Haoec7f1a92017-03-13 16:24:24 -070049#include "dex_verify.h"
David Sehrcdcfde72016-09-26 07:44:04 -070050#include "dex_visualize.h"
Jeff Haoa8621002016-10-04 18:13:44 +000051#include "dex_writer.h"
David Sehr82d046e2018-04-23 08:14:19 -070052#include "profile/profile_compilation_info.h"
David Sehr7629f602016-08-07 16:01:51 -070053
54namespace art {
55
Andreas Gampe46ee31b2016-12-14 10:11:49 -080056using android::base::StringPrintf;
57
David Sehr7629f602016-08-07 16:01:51 -070058/*
David Sehr7629f602016-08-07 16:01:51 -070059 * Flags for use with createAccessFlagStr().
60 */
61enum AccessFor {
62 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
63};
64const int kNumFlags = 18;
65
66/*
67 * Gets 2 little-endian bytes.
68 */
69static inline uint16_t Get2LE(unsigned char const* src) {
70 return src[0] | (src[1] << 8);
71}
72
73/*
74 * Converts the class name portion of a type descriptor to human-readable
75 * "dotted" form. For example, "Ljava/lang/String;" becomes "String".
76 */
Orion Hodsonfe42d212018-08-24 14:01:14 +010077static std::string DescriptorClassToName(const char* str) {
David Sehr7629f602016-08-07 16:01:51 -070078 std::string descriptor(str);
79 // Reduce to just the class name prefix.
80 size_t last_slash = descriptor.rfind('/');
81 if (last_slash == std::string::npos) {
82 last_slash = 0;
83 }
84 // Start past the '/' or 'L'.
85 last_slash++;
86
87 // Copy class name over, trimming trailing ';'.
88 size_t size = descriptor.size() - 1 - last_slash;
89 std::string result(descriptor.substr(last_slash, size));
90
David Sehr7629f602016-08-07 16:01:51 -070091 return result;
92}
93
94/*
95 * Returns string representing the boolean value.
96 */
97static const char* StrBool(bool val) {
98 return val ? "true" : "false";
99}
100
101/*
102 * Returns a quoted string representing the boolean value.
103 */
104static const char* QuotedBool(bool val) {
105 return val ? "\"true\"" : "\"false\"";
106}
107
108/*
109 * Returns a quoted string representing the access flags.
110 */
111static const char* QuotedVisibility(uint32_t access_flags) {
112 if (access_flags & kAccPublic) {
113 return "\"public\"";
114 } else if (access_flags & kAccProtected) {
115 return "\"protected\"";
116 } else if (access_flags & kAccPrivate) {
117 return "\"private\"";
118 } else {
119 return "\"package\"";
120 }
121}
122
123/*
124 * Counts the number of '1' bits in a word.
125 */
126static int CountOnes(uint32_t val) {
127 val = val - ((val >> 1) & 0x55555555);
128 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
129 return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
130}
131
132/*
133 * Creates a new string with human-readable access flags.
134 *
135 * In the base language the access_flags fields are type uint16_t; in Dalvik they're uint32_t.
136 */
137static char* CreateAccessFlagStr(uint32_t flags, AccessFor for_what) {
138 static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
139 {
140 "PUBLIC", /* 0x00001 */
141 "PRIVATE", /* 0x00002 */
142 "PROTECTED", /* 0x00004 */
143 "STATIC", /* 0x00008 */
144 "FINAL", /* 0x00010 */
145 "?", /* 0x00020 */
146 "?", /* 0x00040 */
147 "?", /* 0x00080 */
148 "?", /* 0x00100 */
149 "INTERFACE", /* 0x00200 */
150 "ABSTRACT", /* 0x00400 */
151 "?", /* 0x00800 */
152 "SYNTHETIC", /* 0x01000 */
153 "ANNOTATION", /* 0x02000 */
154 "ENUM", /* 0x04000 */
155 "?", /* 0x08000 */
156 "VERIFIED", /* 0x10000 */
157 "OPTIMIZED", /* 0x20000 */
158 }, {
159 "PUBLIC", /* 0x00001 */
160 "PRIVATE", /* 0x00002 */
161 "PROTECTED", /* 0x00004 */
162 "STATIC", /* 0x00008 */
163 "FINAL", /* 0x00010 */
164 "SYNCHRONIZED", /* 0x00020 */
165 "BRIDGE", /* 0x00040 */
166 "VARARGS", /* 0x00080 */
167 "NATIVE", /* 0x00100 */
168 "?", /* 0x00200 */
169 "ABSTRACT", /* 0x00400 */
170 "STRICT", /* 0x00800 */
171 "SYNTHETIC", /* 0x01000 */
172 "?", /* 0x02000 */
173 "?", /* 0x04000 */
174 "MIRANDA", /* 0x08000 */
175 "CONSTRUCTOR", /* 0x10000 */
176 "DECLARED_SYNCHRONIZED", /* 0x20000 */
177 }, {
178 "PUBLIC", /* 0x00001 */
179 "PRIVATE", /* 0x00002 */
180 "PROTECTED", /* 0x00004 */
181 "STATIC", /* 0x00008 */
182 "FINAL", /* 0x00010 */
183 "?", /* 0x00020 */
184 "VOLATILE", /* 0x00040 */
185 "TRANSIENT", /* 0x00080 */
186 "?", /* 0x00100 */
187 "?", /* 0x00200 */
188 "?", /* 0x00400 */
189 "?", /* 0x00800 */
190 "SYNTHETIC", /* 0x01000 */
191 "?", /* 0x02000 */
192 "ENUM", /* 0x04000 */
193 "?", /* 0x08000 */
194 "?", /* 0x10000 */
195 "?", /* 0x20000 */
196 },
197 };
198
199 // Allocate enough storage to hold the expected number of strings,
200 // plus a space between each. We over-allocate, using the longest
201 // string above as the base metric.
202 const int kLongest = 21; // The strlen of longest string above.
203 const int count = CountOnes(flags);
204 char* str;
205 char* cp;
206 cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
207
208 for (int i = 0; i < kNumFlags; i++) {
209 if (flags & 0x01) {
210 const char* accessStr = kAccessStrings[for_what][i];
211 const int len = strlen(accessStr);
212 if (cp != str) {
213 *cp++ = ' ';
214 }
215 memcpy(cp, accessStr, len);
216 cp += len;
217 }
218 flags >>= 1;
219 } // for
220
221 *cp = '\0';
222 return str;
223}
224
David Brazdil20c765f2018-10-27 21:45:15 +0000225static const char* GetHiddenapiFlagStr(uint32_t hiddenapi_flags) {
226 static const char* const kValue[] = {
227 "WHITELIST", /* 0x0 */
228 "LIGHT_GREYLIST", /* 0x1 */
229 "DARK_GREYLIST", /* 0x2 */
230 "BLACKLIST", /* 0x3 */
231 };
232 DCHECK_LT(hiddenapi_flags, arraysize(kValue));
233 return kValue[hiddenapi_flags];
234}
235
David Sehr7629f602016-08-07 16:01:51 -0700236static std::string GetSignatureForProtoId(const dex_ir::ProtoId* proto) {
237 if (proto == nullptr) {
238 return "<no signature>";
239 }
240
David Sehr7629f602016-08-07 16:01:51 -0700241 std::string result("(");
Jeff Haoa8621002016-10-04 18:13:44 +0000242 const dex_ir::TypeList* type_list = proto->Parameters();
243 if (type_list != nullptr) {
244 for (const dex_ir::TypeId* type_id : *type_list->GetTypeList()) {
245 result += type_id->GetStringId()->Data();
246 }
David Sehr7629f602016-08-07 16:01:51 -0700247 }
248 result += ")";
249 result += proto->ReturnType()->GetStringId()->Data();
250 return result;
251}
252
253/*
254 * Copies character data from "data" to "out", converting non-ASCII values
255 * to fprintf format chars or an ASCII filler ('.' or '?').
256 *
257 * The output buffer must be able to hold (2*len)+1 bytes. The result is
258 * NULL-terminated.
259 */
260static void Asciify(char* out, const unsigned char* data, size_t len) {
Andreas Gampec74d9cb2018-09-20 13:44:44 -0700261 for (; len != 0u; --len) {
David Sehr7629f602016-08-07 16:01:51 -0700262 if (*data < 0x20) {
263 // Could do more here, but we don't need them yet.
264 switch (*data) {
265 case '\0':
266 *out++ = '\\';
267 *out++ = '0';
268 break;
269 case '\n':
270 *out++ = '\\';
271 *out++ = 'n';
272 break;
273 default:
274 *out++ = '.';
275 break;
276 } // switch
277 } else if (*data >= 0x80) {
278 *out++ = '?';
279 } else {
280 *out++ = *data;
281 }
282 data++;
283 } // while
284 *out = '\0';
285}
286
287/*
288 * Dumps a string value with some escape characters.
289 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800290static void DumpEscapedString(const char* p, FILE* out_file) {
291 fputs("\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700292 for (; *p; p++) {
293 switch (*p) {
294 case '\\':
Jeff Haoea7c6292016-11-14 18:10:16 -0800295 fputs("\\\\", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700296 break;
297 case '\"':
Jeff Haoea7c6292016-11-14 18:10:16 -0800298 fputs("\\\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700299 break;
300 case '\t':
Jeff Haoea7c6292016-11-14 18:10:16 -0800301 fputs("\\t", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700302 break;
303 case '\n':
Jeff Haoea7c6292016-11-14 18:10:16 -0800304 fputs("\\n", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700305 break;
306 case '\r':
Jeff Haoea7c6292016-11-14 18:10:16 -0800307 fputs("\\r", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700308 break;
309 default:
Jeff Haoea7c6292016-11-14 18:10:16 -0800310 putc(*p, out_file);
David Sehr7629f602016-08-07 16:01:51 -0700311 } // switch
312 } // for
Jeff Haoea7c6292016-11-14 18:10:16 -0800313 fputs("\"", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700314}
315
316/*
317 * Dumps a string as an XML attribute value.
318 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800319static void DumpXmlAttribute(const char* p, FILE* out_file) {
David Sehr7629f602016-08-07 16:01:51 -0700320 for (; *p; p++) {
321 switch (*p) {
322 case '&':
Jeff Haoea7c6292016-11-14 18:10:16 -0800323 fputs("&amp;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700324 break;
325 case '<':
Jeff Haoea7c6292016-11-14 18:10:16 -0800326 fputs("&lt;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700327 break;
328 case '>':
Jeff Haoea7c6292016-11-14 18:10:16 -0800329 fputs("&gt;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700330 break;
331 case '"':
Jeff Haoea7c6292016-11-14 18:10:16 -0800332 fputs("&quot;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700333 break;
334 case '\t':
Jeff Haoea7c6292016-11-14 18:10:16 -0800335 fputs("&#x9;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700336 break;
337 case '\n':
Jeff Haoea7c6292016-11-14 18:10:16 -0800338 fputs("&#xA;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700339 break;
340 case '\r':
Jeff Haoea7c6292016-11-14 18:10:16 -0800341 fputs("&#xD;", out_file);
David Sehr7629f602016-08-07 16:01:51 -0700342 break;
343 default:
Jeff Haoea7c6292016-11-14 18:10:16 -0800344 putc(*p, out_file);
David Sehr7629f602016-08-07 16:01:51 -0700345 } // switch
346 } // for
347}
348
David Sehr7629f602016-08-07 16:01:51 -0700349/*
350 * Helper for dumpInstruction(), which builds the string
351 * representation for the index in the given instruction.
352 * Returns a pointer to a buffer of sufficient size.
353 */
354static std::unique_ptr<char[]> IndexString(dex_ir::Header* header,
355 const Instruction* dec_insn,
356 size_t buf_size) {
357 std::unique_ptr<char[]> buf(new char[buf_size]);
358 // Determine index and width of the string.
359 uint32_t index = 0;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700360 uint32_t secondary_index = dex::kDexNoIndex;
David Sehr7629f602016-08-07 16:01:51 -0700361 uint32_t width = 4;
362 switch (Instruction::FormatOf(dec_insn->Opcode())) {
363 // SOME NOT SUPPORTED:
364 // case Instruction::k20bc:
365 case Instruction::k21c:
366 case Instruction::k35c:
367 // case Instruction::k35ms:
368 case Instruction::k3rc:
369 // case Instruction::k3rms:
370 // case Instruction::k35mi:
371 // case Instruction::k3rmi:
372 index = dec_insn->VRegB();
373 width = 4;
374 break;
375 case Instruction::k31c:
376 index = dec_insn->VRegB();
377 width = 8;
378 break;
379 case Instruction::k22c:
380 // case Instruction::k22cs:
381 index = dec_insn->VRegC();
382 width = 4;
383 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100384 case Instruction::k45cc:
385 case Instruction::k4rcc:
386 index = dec_insn->VRegB();
387 secondary_index = dec_insn->VRegH();
388 width = 4;
David Sehr7639cdc2017-04-15 10:06:21 -0700389 break;
David Sehr7629f602016-08-07 16:01:51 -0700390 default:
391 break;
392 } // switch
393
394 // Determine index type.
395 size_t outSize = 0;
396 switch (Instruction::IndexTypeOf(dec_insn->Opcode())) {
397 case Instruction::kIndexUnknown:
398 // This function should never get called for this type, but do
399 // something sensible here, just to help with debugging.
400 outSize = snprintf(buf.get(), buf_size, "<unknown-index>");
401 break;
402 case Instruction::kIndexNone:
403 // This function should never get called for this type, but do
404 // something sensible here, just to help with debugging.
405 outSize = snprintf(buf.get(), buf_size, "<no-index>");
406 break;
407 case Instruction::kIndexTypeRef:
David Sehr2b5a38f2018-06-14 15:13:04 -0700408 if (index < header->TypeIds().Size()) {
409 const char* tp = header->TypeIds()[index]->GetStringId()->Data();
David Sehr7629f602016-08-07 16:01:51 -0700410 outSize = snprintf(buf.get(), buf_size, "%s // type@%0*x", tp, width, index);
411 } else {
412 outSize = snprintf(buf.get(), buf_size, "<type?> // type@%0*x", width, index);
413 }
414 break;
415 case Instruction::kIndexStringRef:
David Sehr2b5a38f2018-06-14 15:13:04 -0700416 if (index < header->StringIds().Size()) {
417 const char* st = header->StringIds()[index]->Data();
David Sehr7629f602016-08-07 16:01:51 -0700418 outSize = snprintf(buf.get(), buf_size, "\"%s\" // string@%0*x", st, width, index);
419 } else {
420 outSize = snprintf(buf.get(), buf_size, "<string?> // string@%0*x", width, index);
421 }
422 break;
423 case Instruction::kIndexMethodRef:
David Sehr2b5a38f2018-06-14 15:13:04 -0700424 if (index < header->MethodIds().Size()) {
425 dex_ir::MethodId* method_id = header->MethodIds()[index];
David Sehr7629f602016-08-07 16:01:51 -0700426 const char* name = method_id->Name()->Data();
David Sehr72359222016-09-07 13:04:01 -0700427 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -0700428 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
429 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // method@%0*x",
David Sehr72359222016-09-07 13:04:01 -0700430 back_descriptor, name, type_descriptor.c_str(), width, index);
David Sehr7629f602016-08-07 16:01:51 -0700431 } else {
432 outSize = snprintf(buf.get(), buf_size, "<method?> // method@%0*x", width, index);
433 }
434 break;
435 case Instruction::kIndexFieldRef:
David Sehr2b5a38f2018-06-14 15:13:04 -0700436 if (index < header->FieldIds().Size()) {
437 dex_ir::FieldId* field_id = header->FieldIds()[index];
David Sehr7629f602016-08-07 16:01:51 -0700438 const char* name = field_id->Name()->Data();
439 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
440 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
441 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // field@%0*x",
442 back_descriptor, name, type_descriptor, width, index);
443 } else {
444 outSize = snprintf(buf.get(), buf_size, "<field?> // field@%0*x", width, index);
445 }
446 break;
447 case Instruction::kIndexVtableOffset:
448 outSize = snprintf(buf.get(), buf_size, "[%0*x] // vtable #%0*x",
449 width, index, width, index);
450 break;
451 case Instruction::kIndexFieldOffset:
452 outSize = snprintf(buf.get(), buf_size, "[obj+%0*x]", width, index);
453 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100454 case Instruction::kIndexMethodAndProtoRef: {
455 std::string method("<method?>");
456 std::string proto("<proto?>");
David Sehr2b5a38f2018-06-14 15:13:04 -0700457 if (index < header->MethodIds().Size()) {
458 dex_ir::MethodId* method_id = header->MethodIds()[index];
Orion Hodsonb34bb192016-10-18 17:02:58 +0100459 const char* name = method_id->Name()->Data();
460 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
461 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
462 method = StringPrintf("%s.%s:%s", back_descriptor, name, type_descriptor.c_str());
463 }
David Sehr2b5a38f2018-06-14 15:13:04 -0700464 if (secondary_index < header->ProtoIds().Size()) {
465 dex_ir::ProtoId* proto_id = header->ProtoIds()[secondary_index];
Orion Hodsonb34bb192016-10-18 17:02:58 +0100466 proto = GetSignatureForProtoId(proto_id);
467 }
468 outSize = snprintf(buf.get(), buf_size, "%s, %s // method@%0*x, proto@%0*x",
469 method.c_str(), proto.c_str(), width, index, width, secondary_index);
Jeff Haoea7c6292016-11-14 18:10:16 -0800470 }
471 break;
472 // SOME NOT SUPPORTED:
473 // case Instruction::kIndexVaries:
474 // case Instruction::kIndexInlineMethod:
David Sehr7629f602016-08-07 16:01:51 -0700475 default:
476 outSize = snprintf(buf.get(), buf_size, "<?>");
477 break;
478 } // switch
479
480 // Determine success of string construction.
481 if (outSize >= buf_size) {
482 // The buffer wasn't big enough; retry with computed size. Note: snprintf()
483 // doesn't count/ the '\0' as part of its returned size, so we add explicit
484 // space for it here.
485 return IndexString(header, dec_insn, outSize + 1);
486 }
487 return buf;
488}
489
490/*
Jeff Haoea7c6292016-11-14 18:10:16 -0800491 * Dumps encoded annotation.
492 */
493void DexLayout::DumpEncodedAnnotation(dex_ir::EncodedAnnotation* annotation) {
494 fputs(annotation->GetType()->GetStringId()->Data(), out_file_);
495 // Display all name=value pairs.
496 for (auto& subannotation : *annotation->GetAnnotationElements()) {
497 fputc(' ', out_file_);
498 fputs(subannotation->GetName()->Data(), out_file_);
499 fputc('=', out_file_);
500 DumpEncodedValue(subannotation->GetValue());
501 }
502}
503/*
504 * Dumps encoded value.
505 */
506void DexLayout::DumpEncodedValue(const dex_ir::EncodedValue* data) {
507 switch (data->Type()) {
508 case DexFile::kDexAnnotationByte:
509 fprintf(out_file_, "%" PRId8, data->GetByte());
510 break;
511 case DexFile::kDexAnnotationShort:
512 fprintf(out_file_, "%" PRId16, data->GetShort());
513 break;
514 case DexFile::kDexAnnotationChar:
515 fprintf(out_file_, "%" PRIu16, data->GetChar());
516 break;
517 case DexFile::kDexAnnotationInt:
518 fprintf(out_file_, "%" PRId32, data->GetInt());
519 break;
520 case DexFile::kDexAnnotationLong:
521 fprintf(out_file_, "%" PRId64, data->GetLong());
522 break;
523 case DexFile::kDexAnnotationFloat: {
524 fprintf(out_file_, "%g", data->GetFloat());
525 break;
526 }
527 case DexFile::kDexAnnotationDouble: {
528 fprintf(out_file_, "%g", data->GetDouble());
529 break;
530 }
531 case DexFile::kDexAnnotationString: {
532 dex_ir::StringId* string_id = data->GetStringId();
533 if (options_.output_format_ == kOutputPlain) {
534 DumpEscapedString(string_id->Data(), out_file_);
535 } else {
536 DumpXmlAttribute(string_id->Data(), out_file_);
537 }
538 break;
539 }
540 case DexFile::kDexAnnotationType: {
541 dex_ir::TypeId* type_id = data->GetTypeId();
542 fputs(type_id->GetStringId()->Data(), out_file_);
543 break;
544 }
545 case DexFile::kDexAnnotationField:
546 case DexFile::kDexAnnotationEnum: {
547 dex_ir::FieldId* field_id = data->GetFieldId();
548 fputs(field_id->Name()->Data(), out_file_);
549 break;
550 }
551 case DexFile::kDexAnnotationMethod: {
552 dex_ir::MethodId* method_id = data->GetMethodId();
553 fputs(method_id->Name()->Data(), out_file_);
554 break;
555 }
556 case DexFile::kDexAnnotationArray: {
557 fputc('{', out_file_);
558 // Display all elements.
559 for (auto& value : *data->GetEncodedArray()->GetEncodedValues()) {
560 fputc(' ', out_file_);
561 DumpEncodedValue(value.get());
562 }
563 fputs(" }", out_file_);
564 break;
565 }
566 case DexFile::kDexAnnotationAnnotation: {
567 DumpEncodedAnnotation(data->GetEncodedAnnotation());
568 break;
569 }
570 case DexFile::kDexAnnotationNull:
571 fputs("null", out_file_);
572 break;
573 case DexFile::kDexAnnotationBoolean:
574 fputs(StrBool(data->GetBoolean()), out_file_);
575 break;
576 default:
577 fputs("????", out_file_);
578 break;
579 } // switch
580}
581
582/*
583 * Dumps the file header.
584 */
585void DexLayout::DumpFileHeader() {
586 char sanitized[8 * 2 + 1];
Jeff Haoea7c6292016-11-14 18:10:16 -0800587 fprintf(out_file_, "DEX file header:\n");
588 Asciify(sanitized, header_->Magic(), 8);
589 fprintf(out_file_, "magic : '%s'\n", sanitized);
590 fprintf(out_file_, "checksum : %08x\n", header_->Checksum());
591 fprintf(out_file_, "signature : %02x%02x...%02x%02x\n",
592 header_->Signature()[0], header_->Signature()[1],
593 header_->Signature()[DexFile::kSha1DigestSize - 2],
594 header_->Signature()[DexFile::kSha1DigestSize - 1]);
595 fprintf(out_file_, "file_size : %d\n", header_->FileSize());
596 fprintf(out_file_, "header_size : %d\n", header_->HeaderSize());
597 fprintf(out_file_, "link_size : %d\n", header_->LinkSize());
598 fprintf(out_file_, "link_off : %d (0x%06x)\n",
599 header_->LinkOffset(), header_->LinkOffset());
David Sehr2b5a38f2018-06-14 15:13:04 -0700600 fprintf(out_file_, "string_ids_size : %d\n", header_->StringIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800601 fprintf(out_file_, "string_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700602 header_->StringIds().GetOffset(), header_->StringIds().GetOffset());
603 fprintf(out_file_, "type_ids_size : %d\n", header_->TypeIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800604 fprintf(out_file_, "type_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700605 header_->TypeIds().GetOffset(), header_->TypeIds().GetOffset());
606 fprintf(out_file_, "proto_ids_size : %d\n", header_->ProtoIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800607 fprintf(out_file_, "proto_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700608 header_->ProtoIds().GetOffset(), header_->ProtoIds().GetOffset());
609 fprintf(out_file_, "field_ids_size : %d\n", header_->FieldIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800610 fprintf(out_file_, "field_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700611 header_->FieldIds().GetOffset(), header_->FieldIds().GetOffset());
612 fprintf(out_file_, "method_ids_size : %d\n", header_->MethodIds().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800613 fprintf(out_file_, "method_ids_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700614 header_->MethodIds().GetOffset(), header_->MethodIds().GetOffset());
615 fprintf(out_file_, "class_defs_size : %d\n", header_->ClassDefs().Size());
Jeff Haoea7c6292016-11-14 18:10:16 -0800616 fprintf(out_file_, "class_defs_off : %d (0x%06x)\n",
David Sehr2b5a38f2018-06-14 15:13:04 -0700617 header_->ClassDefs().GetOffset(), header_->ClassDefs().GetOffset());
Jeff Haoea7c6292016-11-14 18:10:16 -0800618 fprintf(out_file_, "data_size : %d\n", header_->DataSize());
619 fprintf(out_file_, "data_off : %d (0x%06x)\n\n",
620 header_->DataOffset(), header_->DataOffset());
621}
622
623/*
624 * Dumps a class_def_item.
625 */
626void DexLayout::DumpClassDef(int idx) {
627 // General class information.
David Sehr2b5a38f2018-06-14 15:13:04 -0700628 dex_ir::ClassDef* class_def = header_->ClassDefs()[idx];
Jeff Haoea7c6292016-11-14 18:10:16 -0800629 fprintf(out_file_, "Class #%d header:\n", idx);
630 fprintf(out_file_, "class_idx : %d\n", class_def->ClassType()->GetIndex());
631 fprintf(out_file_, "access_flags : %d (0x%04x)\n",
632 class_def->GetAccessFlags(), class_def->GetAccessFlags());
633 uint32_t superclass_idx = class_def->Superclass() == nullptr ?
634 DexFile::kDexNoIndex16 : class_def->Superclass()->GetIndex();
635 fprintf(out_file_, "superclass_idx : %d\n", superclass_idx);
636 fprintf(out_file_, "interfaces_off : %d (0x%06x)\n",
637 class_def->InterfacesOffset(), class_def->InterfacesOffset());
638 uint32_t source_file_offset = 0xffffffffU;
639 if (class_def->SourceFile() != nullptr) {
640 source_file_offset = class_def->SourceFile()->GetIndex();
641 }
642 fprintf(out_file_, "source_file_idx : %d\n", source_file_offset);
643 uint32_t annotations_offset = 0;
644 if (class_def->Annotations() != nullptr) {
645 annotations_offset = class_def->Annotations()->GetOffset();
646 }
647 fprintf(out_file_, "annotations_off : %d (0x%06x)\n",
648 annotations_offset, annotations_offset);
649 if (class_def->GetClassData() == nullptr) {
650 fprintf(out_file_, "class_data_off : %d (0x%06x)\n", 0, 0);
651 } else {
652 fprintf(out_file_, "class_data_off : %d (0x%06x)\n",
653 class_def->GetClassData()->GetOffset(), class_def->GetClassData()->GetOffset());
654 }
655
656 // Fields and methods.
657 dex_ir::ClassData* class_data = class_def->GetClassData();
658 if (class_data != nullptr && class_data->StaticFields() != nullptr) {
659 fprintf(out_file_, "static_fields_size : %zu\n", class_data->StaticFields()->size());
660 } else {
661 fprintf(out_file_, "static_fields_size : 0\n");
662 }
663 if (class_data != nullptr && class_data->InstanceFields() != nullptr) {
664 fprintf(out_file_, "instance_fields_size: %zu\n", class_data->InstanceFields()->size());
665 } else {
666 fprintf(out_file_, "instance_fields_size: 0\n");
667 }
668 if (class_data != nullptr && class_data->DirectMethods() != nullptr) {
669 fprintf(out_file_, "direct_methods_size : %zu\n", class_data->DirectMethods()->size());
670 } else {
671 fprintf(out_file_, "direct_methods_size : 0\n");
672 }
673 if (class_data != nullptr && class_data->VirtualMethods() != nullptr) {
674 fprintf(out_file_, "virtual_methods_size: %zu\n", class_data->VirtualMethods()->size());
675 } else {
676 fprintf(out_file_, "virtual_methods_size: 0\n");
677 }
678 fprintf(out_file_, "\n");
679}
680
681/**
682 * Dumps an annotation set item.
683 */
684void DexLayout::DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item) {
685 if (set_item == nullptr || set_item->GetItems()->size() == 0) {
686 fputs(" empty-annotation-set\n", out_file_);
687 return;
688 }
689 for (dex_ir::AnnotationItem* annotation : *set_item->GetItems()) {
690 if (annotation == nullptr) {
691 continue;
692 }
693 fputs(" ", out_file_);
694 switch (annotation->GetVisibility()) {
695 case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", out_file_); break;
696 case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", out_file_); break;
697 case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", out_file_); break;
698 default: fputs("VISIBILITY_UNKNOWN ", out_file_); break;
699 } // switch
700 DumpEncodedAnnotation(annotation->GetAnnotation());
701 fputc('\n', out_file_);
702 }
703}
704
705/*
706 * Dumps class annotations.
707 */
708void DexLayout::DumpClassAnnotations(int idx) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700709 dex_ir::ClassDef* class_def = header_->ClassDefs()[idx];
Jeff Haoea7c6292016-11-14 18:10:16 -0800710 dex_ir::AnnotationsDirectoryItem* annotations_directory = class_def->Annotations();
711 if (annotations_directory == nullptr) {
712 return; // none
713 }
714
715 fprintf(out_file_, "Class #%d annotations:\n", idx);
716
717 dex_ir::AnnotationSetItem* class_set_item = annotations_directory->GetClassAnnotation();
718 dex_ir::FieldAnnotationVector* fields = annotations_directory->GetFieldAnnotations();
719 dex_ir::MethodAnnotationVector* methods = annotations_directory->GetMethodAnnotations();
720 dex_ir::ParameterAnnotationVector* parameters = annotations_directory->GetParameterAnnotations();
721
722 // Annotations on the class itself.
723 if (class_set_item != nullptr) {
724 fprintf(out_file_, "Annotations on class\n");
725 DumpAnnotationSetItem(class_set_item);
726 }
727
728 // Annotations on fields.
729 if (fields != nullptr) {
730 for (auto& field : *fields) {
731 const dex_ir::FieldId* field_id = field->GetFieldId();
732 const uint32_t field_idx = field_id->GetIndex();
733 const char* field_name = field_id->Name()->Data();
734 fprintf(out_file_, "Annotations on field #%u '%s'\n", field_idx, field_name);
735 DumpAnnotationSetItem(field->GetAnnotationSetItem());
736 }
737 }
738
739 // Annotations on methods.
740 if (methods != nullptr) {
741 for (auto& method : *methods) {
742 const dex_ir::MethodId* method_id = method->GetMethodId();
743 const uint32_t method_idx = method_id->GetIndex();
744 const char* method_name = method_id->Name()->Data();
745 fprintf(out_file_, "Annotations on method #%u '%s'\n", method_idx, method_name);
746 DumpAnnotationSetItem(method->GetAnnotationSetItem());
747 }
748 }
749
750 // Annotations on method parameters.
751 if (parameters != nullptr) {
752 for (auto& parameter : *parameters) {
753 const dex_ir::MethodId* method_id = parameter->GetMethodId();
754 const uint32_t method_idx = method_id->GetIndex();
755 const char* method_name = method_id->Name()->Data();
756 fprintf(out_file_, "Annotations on method #%u '%s' parameters\n", method_idx, method_name);
757 uint32_t j = 0;
758 for (dex_ir::AnnotationSetItem* annotation : *parameter->GetAnnotations()->GetItems()) {
759 fprintf(out_file_, "#%u\n", j);
760 DumpAnnotationSetItem(annotation);
761 ++j;
762 }
763 }
764 }
765
766 fputc('\n', out_file_);
767}
768
769/*
770 * Dumps an interface that a class declares to implement.
771 */
772void DexLayout::DumpInterface(const dex_ir::TypeId* type_item, int i) {
773 const char* interface_name = type_item->GetStringId()->Data();
774 if (options_.output_format_ == kOutputPlain) {
775 fprintf(out_file_, " #%d : '%s'\n", i, interface_name);
776 } else {
Orion Hodsonfe42d212018-08-24 14:01:14 +0100777 std::string dot(DescriptorToDot(interface_name));
Jeff Haoea7c6292016-11-14 18:10:16 -0800778 fprintf(out_file_, "<implements name=\"%s\">\n</implements>\n", dot.c_str());
779 }
780}
781
782/*
783 * Dumps the catches table associated with the code.
784 */
785void DexLayout::DumpCatches(const dex_ir::CodeItem* code) {
786 const uint16_t tries_size = code->TriesSize();
787
788 // No catch table.
789 if (tries_size == 0) {
790 fprintf(out_file_, " catches : (none)\n");
791 return;
792 }
793
794 // Dump all table entries.
795 fprintf(out_file_, " catches : %d\n", tries_size);
796 std::vector<std::unique_ptr<const dex_ir::TryItem>>* tries = code->Tries();
797 for (uint32_t i = 0; i < tries_size; i++) {
798 const dex_ir::TryItem* try_item = (*tries)[i].get();
799 const uint32_t start = try_item->StartAddr();
800 const uint32_t end = start + try_item->InsnCount();
801 fprintf(out_file_, " 0x%04x - 0x%04x\n", start, end);
802 for (auto& handler : *try_item->GetHandlers()->GetHandlers()) {
803 const dex_ir::TypeId* type_id = handler->GetTypeId();
804 const char* descriptor = (type_id == nullptr) ? "<any>" : type_id->GetStringId()->Data();
805 fprintf(out_file_, " %s -> 0x%04x\n", descriptor, handler->GetAddress());
806 } // for
807 } // for
808}
809
810/*
David Sehr7629f602016-08-07 16:01:51 -0700811 * Dumps a single instruction.
812 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800813void DexLayout::DumpInstruction(const dex_ir::CodeItem* code,
814 uint32_t code_offset,
815 uint32_t insn_idx,
816 uint32_t insn_width,
817 const Instruction* dec_insn) {
David Sehr7629f602016-08-07 16:01:51 -0700818 // Address of instruction (expressed as byte offset).
819 fprintf(out_file_, "%06x:", code_offset + 0x10 + insn_idx * 2);
820
821 // Dump (part of) raw bytes.
822 const uint16_t* insns = code->Insns();
823 for (uint32_t i = 0; i < 8; i++) {
824 if (i < insn_width) {
825 if (i == 7) {
826 fprintf(out_file_, " ... ");
827 } else {
828 // Print 16-bit value in little-endian order.
829 const uint8_t* bytePtr = (const uint8_t*) &insns[insn_idx + i];
830 fprintf(out_file_, " %02x%02x", bytePtr[0], bytePtr[1]);
831 }
832 } else {
833 fputs(" ", out_file_);
834 }
835 } // for
836
837 // Dump pseudo-instruction or opcode.
838 if (dec_insn->Opcode() == Instruction::NOP) {
839 const uint16_t instr = Get2LE((const uint8_t*) &insns[insn_idx]);
840 if (instr == Instruction::kPackedSwitchSignature) {
841 fprintf(out_file_, "|%04x: packed-switch-data (%d units)", insn_idx, insn_width);
842 } else if (instr == Instruction::kSparseSwitchSignature) {
843 fprintf(out_file_, "|%04x: sparse-switch-data (%d units)", insn_idx, insn_width);
844 } else if (instr == Instruction::kArrayDataSignature) {
845 fprintf(out_file_, "|%04x: array-data (%d units)", insn_idx, insn_width);
846 } else {
847 fprintf(out_file_, "|%04x: nop // spacer", insn_idx);
848 }
849 } else {
850 fprintf(out_file_, "|%04x: %s", insn_idx, dec_insn->Name());
851 }
852
853 // Set up additional argument.
854 std::unique_ptr<char[]> index_buf;
855 if (Instruction::IndexTypeOf(dec_insn->Opcode()) != Instruction::kIndexNone) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800856 index_buf = IndexString(header_, dec_insn, 200);
David Sehr7629f602016-08-07 16:01:51 -0700857 }
858
859 // Dump the instruction.
860 //
861 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
862 //
863 switch (Instruction::FormatOf(dec_insn->Opcode())) {
864 case Instruction::k10x: // op
865 break;
866 case Instruction::k12x: // op vA, vB
867 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
868 break;
869 case Instruction::k11n: // op vA, #+B
870 fprintf(out_file_, " v%d, #int %d // #%x",
871 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint8_t)dec_insn->VRegB());
872 break;
873 case Instruction::k11x: // op vAA
874 fprintf(out_file_, " v%d", dec_insn->VRegA());
875 break;
876 case Instruction::k10t: // op +AA
877 case Instruction::k20t: { // op +AAAA
878 const int32_t targ = (int32_t) dec_insn->VRegA();
879 fprintf(out_file_, " %04x // %c%04x",
880 insn_idx + targ,
881 (targ < 0) ? '-' : '+',
882 (targ < 0) ? -targ : targ);
883 break;
884 }
885 case Instruction::k22x: // op vAA, vBBBB
886 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
887 break;
888 case Instruction::k21t: { // op vAA, +BBBB
889 const int32_t targ = (int32_t) dec_insn->VRegB();
890 fprintf(out_file_, " v%d, %04x // %c%04x", dec_insn->VRegA(),
891 insn_idx + targ,
892 (targ < 0) ? '-' : '+',
893 (targ < 0) ? -targ : targ);
894 break;
895 }
896 case Instruction::k21s: // op vAA, #+BBBB
897 fprintf(out_file_, " v%d, #int %d // #%x",
898 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint16_t)dec_insn->VRegB());
899 break;
900 case Instruction::k21h: // op vAA, #+BBBB0000[00000000]
901 // The printed format varies a bit based on the actual opcode.
902 if (dec_insn->Opcode() == Instruction::CONST_HIGH16) {
903 const int32_t value = dec_insn->VRegB() << 16;
904 fprintf(out_file_, " v%d, #int %d // #%x",
905 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
906 } else {
907 const int64_t value = ((int64_t) dec_insn->VRegB()) << 48;
908 fprintf(out_file_, " v%d, #long %" PRId64 " // #%x",
909 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
910 }
911 break;
912 case Instruction::k21c: // op vAA, thing@BBBB
913 case Instruction::k31c: // op vAA, thing@BBBBBBBB
914 fprintf(out_file_, " v%d, %s", dec_insn->VRegA(), index_buf.get());
915 break;
916 case Instruction::k23x: // op vAA, vBB, vCC
917 fprintf(out_file_, " v%d, v%d, v%d",
918 dec_insn->VRegA(), dec_insn->VRegB(), dec_insn->VRegC());
919 break;
920 case Instruction::k22b: // op vAA, vBB, #+CC
921 fprintf(out_file_, " v%d, v%d, #int %d // #%02x",
922 dec_insn->VRegA(), dec_insn->VRegB(),
923 (int32_t) dec_insn->VRegC(), (uint8_t) dec_insn->VRegC());
924 break;
925 case Instruction::k22t: { // op vA, vB, +CCCC
926 const int32_t targ = (int32_t) dec_insn->VRegC();
927 fprintf(out_file_, " v%d, v%d, %04x // %c%04x",
928 dec_insn->VRegA(), dec_insn->VRegB(),
929 insn_idx + targ,
930 (targ < 0) ? '-' : '+',
931 (targ < 0) ? -targ : targ);
932 break;
933 }
934 case Instruction::k22s: // op vA, vB, #+CCCC
935 fprintf(out_file_, " v%d, v%d, #int %d // #%04x",
936 dec_insn->VRegA(), dec_insn->VRegB(),
937 (int32_t) dec_insn->VRegC(), (uint16_t) dec_insn->VRegC());
938 break;
939 case Instruction::k22c: // op vA, vB, thing@CCCC
940 // NOT SUPPORTED:
941 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
942 fprintf(out_file_, " v%d, v%d, %s",
943 dec_insn->VRegA(), dec_insn->VRegB(), index_buf.get());
944 break;
945 case Instruction::k30t:
946 fprintf(out_file_, " #%08x", dec_insn->VRegA());
947 break;
948 case Instruction::k31i: { // op vAA, #+BBBBBBBB
949 // This is often, but not always, a float.
950 union {
951 float f;
952 uint32_t i;
953 } conv;
954 conv.i = dec_insn->VRegB();
955 fprintf(out_file_, " v%d, #float %g // #%08x",
956 dec_insn->VRegA(), conv.f, dec_insn->VRegB());
957 break;
958 }
959 case Instruction::k31t: // op vAA, offset +BBBBBBBB
960 fprintf(out_file_, " v%d, %08x // +%08x",
961 dec_insn->VRegA(), insn_idx + dec_insn->VRegB(), dec_insn->VRegB());
962 break;
963 case Instruction::k32x: // op vAAAA, vBBBB
964 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
965 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100966 case Instruction::k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
967 case Instruction::k45cc: { // op {vC, vD, vE, vF, vG}, meth@BBBB, proto@HHHH
David Sehr7629f602016-08-07 16:01:51 -0700968 // NOT SUPPORTED:
969 // case Instruction::k35ms: // [opt] invoke-virtual+super
970 // case Instruction::k35mi: // [opt] inline invoke
971 uint32_t arg[Instruction::kMaxVarArgRegs];
972 dec_insn->GetVarArgs(arg);
973 fputs(" {", out_file_);
974 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
975 if (i == 0) {
976 fprintf(out_file_, "v%d", arg[i]);
977 } else {
978 fprintf(out_file_, ", v%d", arg[i]);
979 }
980 } // for
981 fprintf(out_file_, "}, %s", index_buf.get());
982 break;
983 }
Orion Hodsonb34bb192016-10-18 17:02:58 +0100984 case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
985 case Instruction::k4rcc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB, proto@HHHH
David Sehr7629f602016-08-07 16:01:51 -0700986 // NOT SUPPORTED:
987 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
988 // case Instruction::k3rmi: // [opt] execute-inline/range
989 {
990 // This doesn't match the "dx" output when some of the args are
991 // 64-bit values -- dx only shows the first register.
992 fputs(" {", out_file_);
993 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
994 if (i == 0) {
995 fprintf(out_file_, "v%d", dec_insn->VRegC() + i);
996 } else {
997 fprintf(out_file_, ", v%d", dec_insn->VRegC() + i);
998 }
999 } // for
1000 fprintf(out_file_, "}, %s", index_buf.get());
1001 }
1002 break;
1003 case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB
1004 // This is often, but not always, a double.
1005 union {
1006 double d;
1007 uint64_t j;
1008 } conv;
1009 conv.j = dec_insn->WideVRegB();
1010 fprintf(out_file_, " v%d, #double %g // #%016" PRIx64,
1011 dec_insn->VRegA(), conv.d, dec_insn->WideVRegB());
1012 break;
1013 }
1014 // NOT SUPPORTED:
1015 // case Instruction::k00x: // unknown op or breakpoint
1016 // break;
1017 default:
1018 fprintf(out_file_, " ???");
1019 break;
1020 } // switch
1021
1022 fputc('\n', out_file_);
1023}
1024
1025/*
1026 * Dumps a bytecode disassembly.
1027 */
Jeff Haoea7c6292016-11-14 18:10:16 -08001028void DexLayout::DumpBytecodes(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001029 dex_ir::MethodId* method_id = header_->MethodIds()[idx];
David Sehr7629f602016-08-07 16:01:51 -07001030 const char* name = method_id->Name()->Data();
David Sehr72359222016-09-07 13:04:01 -07001031 std::string type_descriptor = GetSignatureForProtoId(method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -07001032 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1033
1034 // Generate header.
Orion Hodsonfe42d212018-08-24 14:01:14 +01001035 std::string dot(DescriptorToDot(back_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001036 fprintf(out_file_, "%06x: |[%06x] %s.%s:%s\n",
David Sehr72359222016-09-07 13:04:01 -07001037 code_offset, code_offset, dot.c_str(), name, type_descriptor.c_str());
David Sehr7629f602016-08-07 16:01:51 -07001038
1039 // Iterate over all instructions.
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001040 for (const DexInstructionPcPair& inst : code->Instructions()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001041 const uint32_t insn_width = inst->SizeInCodeUnits();
David Sehr7629f602016-08-07 16:01:51 -07001042 if (insn_width == 0) {
Andreas Gampe221d9812018-01-22 17:48:56 -08001043 LOG(WARNING) << "GLITCH: zero-width instruction at idx=0x" << std::hex << inst.DexPc();
David Sehr7629f602016-08-07 16:01:51 -07001044 break;
1045 }
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001046 DumpInstruction(code, code_offset, inst.DexPc(), insn_width, &inst.Inst());
David Sehr7629f602016-08-07 16:01:51 -07001047 } // for
1048}
1049
1050/*
David Sehraa6abb02017-10-12 08:25:11 -07001051 * Lookup functions.
1052 */
David Sehr2b5a38f2018-06-14 15:13:04 -07001053static const char* StringDataByIdx(uint32_t idx, dex_ir::Header* header) {
1054 dex_ir::StringId* string_id = header->GetStringIdOrNullPtr(idx);
David Sehraa6abb02017-10-12 08:25:11 -07001055 if (string_id == nullptr) {
1056 return nullptr;
1057 }
1058 return string_id->Data();
1059}
1060
David Sehr2b5a38f2018-06-14 15:13:04 -07001061static const char* StringDataByTypeIdx(uint16_t idx, dex_ir::Header* header) {
1062 dex_ir::TypeId* type_id = header->GetTypeIdOrNullPtr(idx);
David Sehraa6abb02017-10-12 08:25:11 -07001063 if (type_id == nullptr) {
1064 return nullptr;
1065 }
1066 dex_ir::StringId* string_id = type_id->GetStringId();
1067 if (string_id == nullptr) {
1068 return nullptr;
1069 }
1070 return string_id->Data();
1071}
1072
1073
1074/*
David Sehr7629f602016-08-07 16:01:51 -07001075 * Dumps code of a method.
1076 */
David Sehraa6abb02017-10-12 08:25:11 -07001077void DexLayout::DumpCode(uint32_t idx,
1078 const dex_ir::CodeItem* code,
1079 uint32_t code_offset,
1080 const char* declaring_class_descriptor,
1081 const char* method_name,
1082 bool is_static,
1083 const dex_ir::ProtoId* proto) {
David Sehr7629f602016-08-07 16:01:51 -07001084 fprintf(out_file_, " registers : %d\n", code->RegistersSize());
1085 fprintf(out_file_, " ins : %d\n", code->InsSize());
1086 fprintf(out_file_, " outs : %d\n", code->OutsSize());
1087 fprintf(out_file_, " insns size : %d 16-bit code units\n",
1088 code->InsnsSize());
1089
1090 // Bytecode disassembly, if requested.
1091 if (options_.disassemble_) {
Jeff Haoea7c6292016-11-14 18:10:16 -08001092 DumpBytecodes(idx, code, code_offset);
David Sehr7629f602016-08-07 16:01:51 -07001093 }
1094
1095 // Try-catch blocks.
1096 DumpCatches(code);
1097
1098 // Positions and locals table in the debug info.
David Sehraa6abb02017-10-12 08:25:11 -07001099 dex_ir::DebugInfoItem* debug_info = code->DebugInfo();
David Sehr7629f602016-08-07 16:01:51 -07001100 fprintf(out_file_, " positions : \n");
David Sehraa6abb02017-10-12 08:25:11 -07001101 if (debug_info != nullptr) {
1102 DexFile::DecodeDebugPositionInfo(debug_info->GetDebugInfo(),
1103 [this](uint32_t idx) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001104 return StringDataByIdx(idx, this->header_);
David Sehraa6abb02017-10-12 08:25:11 -07001105 },
Mathieu Chartier3e2e1232018-09-11 12:35:30 -07001106 [&](const DexFile::PositionInfo& entry) {
1107 fprintf(out_file_,
1108 " 0x%04x line=%d\n",
1109 entry.address_,
1110 entry.line_);
1111 return false;
1112 });
David Sehraa6abb02017-10-12 08:25:11 -07001113 }
David Sehr7629f602016-08-07 16:01:51 -07001114 fprintf(out_file_, " locals : \n");
David Sehraa6abb02017-10-12 08:25:11 -07001115 if (debug_info != nullptr) {
1116 std::vector<const char*> arg_descriptors;
1117 const dex_ir::TypeList* parameters = proto->Parameters();
1118 if (parameters != nullptr) {
1119 const dex_ir::TypeIdVector* parameter_type_vector = parameters->GetTypeList();
1120 if (parameter_type_vector != nullptr) {
1121 for (const dex_ir::TypeId* type_id : *parameter_type_vector) {
1122 arg_descriptors.push_back(type_id->GetStringId()->Data());
1123 }
1124 }
1125 }
1126 DexFile::DecodeDebugLocalInfo(debug_info->GetDebugInfo(),
1127 "DexLayout in-memory",
1128 declaring_class_descriptor,
1129 arg_descriptors,
1130 method_name,
1131 is_static,
1132 code->RegistersSize(),
1133 code->InsSize(),
1134 code->InsnsSize(),
1135 [this](uint32_t idx) {
David Sehr2b5a38f2018-06-14 15:13:04 -07001136 return StringDataByIdx(idx, this->header_);
David Sehraa6abb02017-10-12 08:25:11 -07001137 },
1138 [this](uint32_t idx) {
1139 return
1140 StringDataByTypeIdx(dchecked_integral_cast<uint16_t>(idx),
David Sehr2b5a38f2018-06-14 15:13:04 -07001141 this->header_);
David Sehraa6abb02017-10-12 08:25:11 -07001142 },
Mathieu Chartiere5afbf32018-09-12 17:51:54 -07001143 [&](const DexFile::LocalInfo& entry) {
1144 const char* signature =
1145 entry.signature_ != nullptr ? entry.signature_ : "";
1146 fprintf(out_file_,
1147 " 0x%04x - 0x%04x reg=%d %s %s %s\n",
1148 entry.start_address_,
1149 entry.end_address_,
1150 entry.reg_,
1151 entry.name_,
1152 entry.descriptor_,
1153 signature);
1154 });
David Sehraa6abb02017-10-12 08:25:11 -07001155 }
David Sehr7629f602016-08-07 16:01:51 -07001156}
1157
1158/*
1159 * Dumps a method.
1160 */
David Brazdil20c765f2018-10-27 21:45:15 +00001161void DexLayout::DumpMethod(uint32_t idx,
1162 uint32_t flags,
1163 uint32_t hiddenapi_flags,
1164 const dex_ir::CodeItem* code,
1165 int i) {
David Sehr7629f602016-08-07 16:01:51 -07001166 // Bail for anything private if export only requested.
1167 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1168 return;
1169 }
1170
David Sehr2b5a38f2018-06-14 15:13:04 -07001171 dex_ir::MethodId* method_id = header_->MethodIds()[idx];
David Sehr7629f602016-08-07 16:01:51 -07001172 const char* name = method_id->Name()->Data();
1173 char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str());
1174 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1175 char* access_str = CreateAccessFlagStr(flags, kAccessForMethod);
David Brazdil20c765f2018-10-27 21:45:15 +00001176 const char* hiddenapi_str = GetHiddenapiFlagStr(hiddenapi_flags);
David Sehr7629f602016-08-07 16:01:51 -07001177
1178 if (options_.output_format_ == kOutputPlain) {
1179 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1180 fprintf(out_file_, " name : '%s'\n", name);
1181 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1182 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
David Brazdil20c765f2018-10-27 21:45:15 +00001183 if (hiddenapi_flags != 0u) {
1184 fprintf(out_file_, " hiddenapi : 0x%04x (%s)\n", hiddenapi_flags, hiddenapi_str);
1185 }
David Sehr7629f602016-08-07 16:01:51 -07001186 if (code == nullptr) {
1187 fprintf(out_file_, " code : (none)\n");
1188 } else {
1189 fprintf(out_file_, " code -\n");
David Sehraa6abb02017-10-12 08:25:11 -07001190 DumpCode(idx,
1191 code,
1192 code->GetOffset(),
1193 back_descriptor,
1194 name,
1195 (flags & kAccStatic) != 0,
1196 method_id->Proto());
David Sehr7629f602016-08-07 16:01:51 -07001197 }
1198 if (options_.disassemble_) {
1199 fputc('\n', out_file_);
1200 }
1201 } else if (options_.output_format_ == kOutputXml) {
1202 const bool constructor = (name[0] == '<');
1203
1204 // Method name and prototype.
1205 if (constructor) {
Orion Hodsonfe42d212018-08-24 14:01:14 +01001206 std::string dot(DescriptorClassToName(back_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001207 fprintf(out_file_, "<constructor name=\"%s\"\n", dot.c_str());
Orion Hodsonfe42d212018-08-24 14:01:14 +01001208 dot = DescriptorToDot(back_descriptor);
David Sehr7629f602016-08-07 16:01:51 -07001209 fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
1210 } else {
1211 fprintf(out_file_, "<method name=\"%s\"\n", name);
1212 const char* return_type = strrchr(type_descriptor, ')');
1213 if (return_type == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08001214 LOG(ERROR) << "bad method type descriptor '" << type_descriptor << "'";
David Sehr7629f602016-08-07 16:01:51 -07001215 goto bail;
1216 }
Orion Hodsonfe42d212018-08-24 14:01:14 +01001217 std::string dot(DescriptorToDot(return_type + 1));
David Sehr7629f602016-08-07 16:01:51 -07001218 fprintf(out_file_, " return=\"%s\"\n", dot.c_str());
1219 fprintf(out_file_, " abstract=%s\n", QuotedBool((flags & kAccAbstract) != 0));
1220 fprintf(out_file_, " native=%s\n", QuotedBool((flags & kAccNative) != 0));
1221 fprintf(out_file_, " synchronized=%s\n", QuotedBool(
1222 (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0));
1223 }
1224
1225 // Additional method flags.
1226 fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
1227 fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
1228 // The "deprecated=" not knowable w/o parsing annotations.
1229 fprintf(out_file_, " visibility=%s\n>\n", QuotedVisibility(flags));
1230
1231 // Parameters.
1232 if (type_descriptor[0] != '(') {
Andreas Gampe221d9812018-01-22 17:48:56 -08001233 LOG(ERROR) << "ERROR: bad descriptor '" << type_descriptor << "'";
David Sehr7629f602016-08-07 16:01:51 -07001234 goto bail;
1235 }
1236 char* tmp_buf = reinterpret_cast<char*>(malloc(strlen(type_descriptor) + 1));
1237 const char* base = type_descriptor + 1;
1238 int arg_num = 0;
1239 while (*base != ')') {
1240 char* cp = tmp_buf;
1241 while (*base == '[') {
1242 *cp++ = *base++;
1243 }
1244 if (*base == 'L') {
1245 // Copy through ';'.
1246 do {
1247 *cp = *base++;
1248 } while (*cp++ != ';');
1249 } else {
1250 // Primitive char, copy it.
1251 if (strchr("ZBCSIFJD", *base) == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08001252 LOG(ERROR) << "ERROR: bad method signature '" << base << "'";
David Sehr7629f602016-08-07 16:01:51 -07001253 break; // while
1254 }
1255 *cp++ = *base++;
1256 }
1257 // Null terminate and display.
1258 *cp++ = '\0';
Orion Hodsonfe42d212018-08-24 14:01:14 +01001259 std::string dot(DescriptorToDot(tmp_buf));
David Sehr7629f602016-08-07 16:01:51 -07001260 fprintf(out_file_, "<parameter name=\"arg%d\" type=\"%s\">\n"
1261 "</parameter>\n", arg_num++, dot.c_str());
1262 } // while
1263 free(tmp_buf);
1264 if (constructor) {
1265 fprintf(out_file_, "</constructor>\n");
1266 } else {
1267 fprintf(out_file_, "</method>\n");
1268 }
1269 }
1270
1271 bail:
1272 free(type_descriptor);
1273 free(access_str);
1274}
1275
1276/*
1277 * Dumps a static (class) field.
1278 */
David Brazdil20c765f2018-10-27 21:45:15 +00001279void DexLayout::DumpSField(uint32_t idx,
1280 uint32_t flags,
1281 uint32_t hiddenapi_flags,
1282 int i,
1283 dex_ir::EncodedValue* init) {
David Sehr7629f602016-08-07 16:01:51 -07001284 // Bail for anything private if export only requested.
1285 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1286 return;
1287 }
1288
David Sehr2b5a38f2018-06-14 15:13:04 -07001289 dex_ir::FieldId* field_id = header_->FieldIds()[idx];
David Sehr7629f602016-08-07 16:01:51 -07001290 const char* name = field_id->Name()->Data();
1291 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
1292 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
1293 char* access_str = CreateAccessFlagStr(flags, kAccessForField);
David Brazdil20c765f2018-10-27 21:45:15 +00001294 const char* hiddenapi_str = GetHiddenapiFlagStr(hiddenapi_flags);
David Sehr7629f602016-08-07 16:01:51 -07001295
1296 if (options_.output_format_ == kOutputPlain) {
1297 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1298 fprintf(out_file_, " name : '%s'\n", name);
1299 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1300 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
David Brazdil20c765f2018-10-27 21:45:15 +00001301 if (hiddenapi_flags != 0u) {
1302 fprintf(out_file_, " hiddenapi : 0x%04x (%s)\n", hiddenapi_flags, hiddenapi_str);
1303 }
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