David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 27 | #include <sys/mman.h> // For the PROT_* and MAP_* constants. |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 28 | |
| 29 | #include <iostream> |
| 30 | #include <memory> |
| 31 | #include <sstream> |
| 32 | #include <vector> |
| 33 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 34 | #include "android-base/stringprintf.h" |
| 35 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 36 | #include "base/logging.h" // For VLOG_IS_ON. |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 37 | #include "base/mem_map.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 38 | #include "base/os.h" |
| 39 | #include "base/utils.h" |
Mathieu Chartier | 818cb80 | 2018-05-11 05:30:16 +0000 | [diff] [blame] | 40 | #include "dex/art_dex_file_loader.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 41 | #include "dex/descriptors_names.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 42 | #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 Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 48 | #include "dex_ir_builder.h" |
Jeff Hao | ec7f1a9 | 2017-03-13 16:24:24 -0700 | [diff] [blame] | 49 | #include "dex_verify.h" |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 50 | #include "dex_visualize.h" |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 51 | #include "dex_writer.h" |
David Sehr | 82d046e | 2018-04-23 08:14:19 -0700 | [diff] [blame] | 52 | #include "profile/profile_compilation_info.h" |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 53 | |
| 54 | namespace art { |
| 55 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 56 | using android::base::StringPrintf; |
| 57 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 58 | /* |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 59 | * Flags for use with createAccessFlagStr(). |
| 60 | */ |
| 61 | enum AccessFor { |
| 62 | kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX |
| 63 | }; |
| 64 | const int kNumFlags = 18; |
| 65 | |
| 66 | /* |
| 67 | * Gets 2 little-endian bytes. |
| 68 | */ |
| 69 | static 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 Hodson | fe42d21 | 2018-08-24 14:01:14 +0100 | [diff] [blame] | 77 | static std::string DescriptorClassToName(const char* str) { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 78 | 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 Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 91 | return result; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Returns string representing the boolean value. |
| 96 | */ |
| 97 | static const char* StrBool(bool val) { |
| 98 | return val ? "true" : "false"; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Returns a quoted string representing the boolean value. |
| 103 | */ |
| 104 | static const char* QuotedBool(bool val) { |
| 105 | return val ? "\"true\"" : "\"false\""; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Returns a quoted string representing the access flags. |
| 110 | */ |
| 111 | static 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 | */ |
| 126 | static 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 | */ |
| 137 | static 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 | |
| 225 | static std::string GetSignatureForProtoId(const dex_ir::ProtoId* proto) { |
| 226 | if (proto == nullptr) { |
| 227 | return "<no signature>"; |
| 228 | } |
| 229 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 230 | std::string result("("); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 231 | const dex_ir::TypeList* type_list = proto->Parameters(); |
| 232 | if (type_list != nullptr) { |
| 233 | for (const dex_ir::TypeId* type_id : *type_list->GetTypeList()) { |
| 234 | result += type_id->GetStringId()->Data(); |
| 235 | } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 236 | } |
| 237 | result += ")"; |
| 238 | result += proto->ReturnType()->GetStringId()->Data(); |
| 239 | return result; |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Copies character data from "data" to "out", converting non-ASCII values |
| 244 | * to fprintf format chars or an ASCII filler ('.' or '?'). |
| 245 | * |
| 246 | * The output buffer must be able to hold (2*len)+1 bytes. The result is |
| 247 | * NULL-terminated. |
| 248 | */ |
| 249 | static void Asciify(char* out, const unsigned char* data, size_t len) { |
Andreas Gampe | c74d9cb | 2018-09-20 13:44:44 -0700 | [diff] [blame] | 250 | for (; len != 0u; --len) { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 251 | if (*data < 0x20) { |
| 252 | // Could do more here, but we don't need them yet. |
| 253 | switch (*data) { |
| 254 | case '\0': |
| 255 | *out++ = '\\'; |
| 256 | *out++ = '0'; |
| 257 | break; |
| 258 | case '\n': |
| 259 | *out++ = '\\'; |
| 260 | *out++ = 'n'; |
| 261 | break; |
| 262 | default: |
| 263 | *out++ = '.'; |
| 264 | break; |
| 265 | } // switch |
| 266 | } else if (*data >= 0x80) { |
| 267 | *out++ = '?'; |
| 268 | } else { |
| 269 | *out++ = *data; |
| 270 | } |
| 271 | data++; |
| 272 | } // while |
| 273 | *out = '\0'; |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Dumps a string value with some escape characters. |
| 278 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 279 | static void DumpEscapedString(const char* p, FILE* out_file) { |
| 280 | fputs("\"", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 281 | for (; *p; p++) { |
| 282 | switch (*p) { |
| 283 | case '\\': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 284 | fputs("\\\\", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 285 | break; |
| 286 | case '\"': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 287 | fputs("\\\"", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 288 | break; |
| 289 | case '\t': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 290 | fputs("\\t", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 291 | break; |
| 292 | case '\n': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 293 | fputs("\\n", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 294 | break; |
| 295 | case '\r': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 296 | fputs("\\r", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 297 | break; |
| 298 | default: |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 299 | putc(*p, out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 300 | } // switch |
| 301 | } // for |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 302 | fputs("\"", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Dumps a string as an XML attribute value. |
| 307 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 308 | static void DumpXmlAttribute(const char* p, FILE* out_file) { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 309 | for (; *p; p++) { |
| 310 | switch (*p) { |
| 311 | case '&': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 312 | fputs("&", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 313 | break; |
| 314 | case '<': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 315 | fputs("<", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 316 | break; |
| 317 | case '>': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 318 | fputs(">", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 319 | break; |
| 320 | case '"': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 321 | fputs(""", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 322 | break; |
| 323 | case '\t': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 324 | fputs("	", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 325 | break; |
| 326 | case '\n': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 327 | fputs("
", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 328 | break; |
| 329 | case '\r': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 330 | fputs("
", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 331 | break; |
| 332 | default: |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 333 | putc(*p, out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 334 | } // switch |
| 335 | } // for |
| 336 | } |
| 337 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 338 | /* |
| 339 | * Helper for dumpInstruction(), which builds the string |
| 340 | * representation for the index in the given instruction. |
| 341 | * Returns a pointer to a buffer of sufficient size. |
| 342 | */ |
| 343 | static std::unique_ptr<char[]> IndexString(dex_ir::Header* header, |
| 344 | const Instruction* dec_insn, |
| 345 | size_t buf_size) { |
| 346 | std::unique_ptr<char[]> buf(new char[buf_size]); |
| 347 | // Determine index and width of the string. |
| 348 | uint32_t index = 0; |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 349 | uint32_t secondary_index = dex::kDexNoIndex; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 350 | uint32_t width = 4; |
| 351 | switch (Instruction::FormatOf(dec_insn->Opcode())) { |
| 352 | // SOME NOT SUPPORTED: |
| 353 | // case Instruction::k20bc: |
| 354 | case Instruction::k21c: |
| 355 | case Instruction::k35c: |
| 356 | // case Instruction::k35ms: |
| 357 | case Instruction::k3rc: |
| 358 | // case Instruction::k3rms: |
| 359 | // case Instruction::k35mi: |
| 360 | // case Instruction::k3rmi: |
| 361 | index = dec_insn->VRegB(); |
| 362 | width = 4; |
| 363 | break; |
| 364 | case Instruction::k31c: |
| 365 | index = dec_insn->VRegB(); |
| 366 | width = 8; |
| 367 | break; |
| 368 | case Instruction::k22c: |
| 369 | // case Instruction::k22cs: |
| 370 | index = dec_insn->VRegC(); |
| 371 | width = 4; |
| 372 | break; |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 373 | case Instruction::k45cc: |
| 374 | case Instruction::k4rcc: |
| 375 | index = dec_insn->VRegB(); |
| 376 | secondary_index = dec_insn->VRegH(); |
| 377 | width = 4; |
David Sehr | 7639cdc | 2017-04-15 10:06:21 -0700 | [diff] [blame] | 378 | break; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 379 | default: |
| 380 | break; |
| 381 | } // switch |
| 382 | |
| 383 | // Determine index type. |
| 384 | size_t outSize = 0; |
| 385 | switch (Instruction::IndexTypeOf(dec_insn->Opcode())) { |
| 386 | case Instruction::kIndexUnknown: |
| 387 | // This function should never get called for this type, but do |
| 388 | // something sensible here, just to help with debugging. |
| 389 | outSize = snprintf(buf.get(), buf_size, "<unknown-index>"); |
| 390 | break; |
| 391 | case Instruction::kIndexNone: |
| 392 | // This function should never get called for this type, but do |
| 393 | // something sensible here, just to help with debugging. |
| 394 | outSize = snprintf(buf.get(), buf_size, "<no-index>"); |
| 395 | break; |
| 396 | case Instruction::kIndexTypeRef: |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 397 | if (index < header->TypeIds().Size()) { |
| 398 | const char* tp = header->TypeIds()[index]->GetStringId()->Data(); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 399 | outSize = snprintf(buf.get(), buf_size, "%s // type@%0*x", tp, width, index); |
| 400 | } else { |
| 401 | outSize = snprintf(buf.get(), buf_size, "<type?> // type@%0*x", width, index); |
| 402 | } |
| 403 | break; |
| 404 | case Instruction::kIndexStringRef: |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 405 | if (index < header->StringIds().Size()) { |
| 406 | const char* st = header->StringIds()[index]->Data(); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 407 | outSize = snprintf(buf.get(), buf_size, "\"%s\" // string@%0*x", st, width, index); |
| 408 | } else { |
| 409 | outSize = snprintf(buf.get(), buf_size, "<string?> // string@%0*x", width, index); |
| 410 | } |
| 411 | break; |
| 412 | case Instruction::kIndexMethodRef: |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 413 | if (index < header->MethodIds().Size()) { |
| 414 | dex_ir::MethodId* method_id = header->MethodIds()[index]; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 415 | const char* name = method_id->Name()->Data(); |
David Sehr | 7235922 | 2016-09-07 13:04:01 -0700 | [diff] [blame] | 416 | std::string type_descriptor = GetSignatureForProtoId(method_id->Proto()); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 417 | const char* back_descriptor = method_id->Class()->GetStringId()->Data(); |
| 418 | outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // method@%0*x", |
David Sehr | 7235922 | 2016-09-07 13:04:01 -0700 | [diff] [blame] | 419 | back_descriptor, name, type_descriptor.c_str(), width, index); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 420 | } else { |
| 421 | outSize = snprintf(buf.get(), buf_size, "<method?> // method@%0*x", width, index); |
| 422 | } |
| 423 | break; |
| 424 | case Instruction::kIndexFieldRef: |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 425 | if (index < header->FieldIds().Size()) { |
| 426 | dex_ir::FieldId* field_id = header->FieldIds()[index]; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 427 | const char* name = field_id->Name()->Data(); |
| 428 | const char* type_descriptor = field_id->Type()->GetStringId()->Data(); |
| 429 | const char* back_descriptor = field_id->Class()->GetStringId()->Data(); |
| 430 | outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // field@%0*x", |
| 431 | back_descriptor, name, type_descriptor, width, index); |
| 432 | } else { |
| 433 | outSize = snprintf(buf.get(), buf_size, "<field?> // field@%0*x", width, index); |
| 434 | } |
| 435 | break; |
| 436 | case Instruction::kIndexVtableOffset: |
| 437 | outSize = snprintf(buf.get(), buf_size, "[%0*x] // vtable #%0*x", |
| 438 | width, index, width, index); |
| 439 | break; |
| 440 | case Instruction::kIndexFieldOffset: |
| 441 | outSize = snprintf(buf.get(), buf_size, "[obj+%0*x]", width, index); |
| 442 | break; |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 443 | case Instruction::kIndexMethodAndProtoRef: { |
| 444 | std::string method("<method?>"); |
| 445 | std::string proto("<proto?>"); |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 446 | if (index < header->MethodIds().Size()) { |
| 447 | dex_ir::MethodId* method_id = header->MethodIds()[index]; |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 448 | const char* name = method_id->Name()->Data(); |
| 449 | std::string type_descriptor = GetSignatureForProtoId(method_id->Proto()); |
| 450 | const char* back_descriptor = method_id->Class()->GetStringId()->Data(); |
| 451 | method = StringPrintf("%s.%s:%s", back_descriptor, name, type_descriptor.c_str()); |
| 452 | } |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 453 | if (secondary_index < header->ProtoIds().Size()) { |
| 454 | dex_ir::ProtoId* proto_id = header->ProtoIds()[secondary_index]; |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 455 | proto = GetSignatureForProtoId(proto_id); |
| 456 | } |
| 457 | outSize = snprintf(buf.get(), buf_size, "%s, %s // method@%0*x, proto@%0*x", |
| 458 | method.c_str(), proto.c_str(), width, index, width, secondary_index); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 459 | } |
| 460 | break; |
| 461 | // SOME NOT SUPPORTED: |
| 462 | // case Instruction::kIndexVaries: |
| 463 | // case Instruction::kIndexInlineMethod: |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 464 | default: |
| 465 | outSize = snprintf(buf.get(), buf_size, "<?>"); |
| 466 | break; |
| 467 | } // switch |
| 468 | |
| 469 | // Determine success of string construction. |
| 470 | if (outSize >= buf_size) { |
| 471 | // The buffer wasn't big enough; retry with computed size. Note: snprintf() |
| 472 | // doesn't count/ the '\0' as part of its returned size, so we add explicit |
| 473 | // space for it here. |
| 474 | return IndexString(header, dec_insn, outSize + 1); |
| 475 | } |
| 476 | return buf; |
| 477 | } |
| 478 | |
| 479 | /* |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 480 | * Dumps encoded annotation. |
| 481 | */ |
| 482 | void DexLayout::DumpEncodedAnnotation(dex_ir::EncodedAnnotation* annotation) { |
| 483 | fputs(annotation->GetType()->GetStringId()->Data(), out_file_); |
| 484 | // Display all name=value pairs. |
| 485 | for (auto& subannotation : *annotation->GetAnnotationElements()) { |
| 486 | fputc(' ', out_file_); |
| 487 | fputs(subannotation->GetName()->Data(), out_file_); |
| 488 | fputc('=', out_file_); |
| 489 | DumpEncodedValue(subannotation->GetValue()); |
| 490 | } |
| 491 | } |
| 492 | /* |
| 493 | * Dumps encoded value. |
| 494 | */ |
| 495 | void DexLayout::DumpEncodedValue(const dex_ir::EncodedValue* data) { |
| 496 | switch (data->Type()) { |
| 497 | case DexFile::kDexAnnotationByte: |
| 498 | fprintf(out_file_, "%" PRId8, data->GetByte()); |
| 499 | break; |
| 500 | case DexFile::kDexAnnotationShort: |
| 501 | fprintf(out_file_, "%" PRId16, data->GetShort()); |
| 502 | break; |
| 503 | case DexFile::kDexAnnotationChar: |
| 504 | fprintf(out_file_, "%" PRIu16, data->GetChar()); |
| 505 | break; |
| 506 | case DexFile::kDexAnnotationInt: |
| 507 | fprintf(out_file_, "%" PRId32, data->GetInt()); |
| 508 | break; |
| 509 | case DexFile::kDexAnnotationLong: |
| 510 | fprintf(out_file_, "%" PRId64, data->GetLong()); |
| 511 | break; |
| 512 | case DexFile::kDexAnnotationFloat: { |
| 513 | fprintf(out_file_, "%g", data->GetFloat()); |
| 514 | break; |
| 515 | } |
| 516 | case DexFile::kDexAnnotationDouble: { |
| 517 | fprintf(out_file_, "%g", data->GetDouble()); |
| 518 | break; |
| 519 | } |
| 520 | case DexFile::kDexAnnotationString: { |
| 521 | dex_ir::StringId* string_id = data->GetStringId(); |
| 522 | if (options_.output_format_ == kOutputPlain) { |
| 523 | DumpEscapedString(string_id->Data(), out_file_); |
| 524 | } else { |
| 525 | DumpXmlAttribute(string_id->Data(), out_file_); |
| 526 | } |
| 527 | break; |
| 528 | } |
| 529 | case DexFile::kDexAnnotationType: { |
| 530 | dex_ir::TypeId* type_id = data->GetTypeId(); |
| 531 | fputs(type_id->GetStringId()->Data(), out_file_); |
| 532 | break; |
| 533 | } |
| 534 | case DexFile::kDexAnnotationField: |
| 535 | case DexFile::kDexAnnotationEnum: { |
| 536 | dex_ir::FieldId* field_id = data->GetFieldId(); |
| 537 | fputs(field_id->Name()->Data(), out_file_); |
| 538 | break; |
| 539 | } |
| 540 | case DexFile::kDexAnnotationMethod: { |
| 541 | dex_ir::MethodId* method_id = data->GetMethodId(); |
| 542 | fputs(method_id->Name()->Data(), out_file_); |
| 543 | break; |
| 544 | } |
| 545 | case DexFile::kDexAnnotationArray: { |
| 546 | fputc('{', out_file_); |
| 547 | // Display all elements. |
| 548 | for (auto& value : *data->GetEncodedArray()->GetEncodedValues()) { |
| 549 | fputc(' ', out_file_); |
| 550 | DumpEncodedValue(value.get()); |
| 551 | } |
| 552 | fputs(" }", out_file_); |
| 553 | break; |
| 554 | } |
| 555 | case DexFile::kDexAnnotationAnnotation: { |
| 556 | DumpEncodedAnnotation(data->GetEncodedAnnotation()); |
| 557 | break; |
| 558 | } |
| 559 | case DexFile::kDexAnnotationNull: |
| 560 | fputs("null", out_file_); |
| 561 | break; |
| 562 | case DexFile::kDexAnnotationBoolean: |
| 563 | fputs(StrBool(data->GetBoolean()), out_file_); |
| 564 | break; |
| 565 | default: |
| 566 | fputs("????", out_file_); |
| 567 | break; |
| 568 | } // switch |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * Dumps the file header. |
| 573 | */ |
| 574 | void DexLayout::DumpFileHeader() { |
| 575 | char sanitized[8 * 2 + 1]; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 576 | fprintf(out_file_, "DEX file header:\n"); |
| 577 | Asciify(sanitized, header_->Magic(), 8); |
| 578 | fprintf(out_file_, "magic : '%s'\n", sanitized); |
| 579 | fprintf(out_file_, "checksum : %08x\n", header_->Checksum()); |
| 580 | fprintf(out_file_, "signature : %02x%02x...%02x%02x\n", |
| 581 | header_->Signature()[0], header_->Signature()[1], |
| 582 | header_->Signature()[DexFile::kSha1DigestSize - 2], |
| 583 | header_->Signature()[DexFile::kSha1DigestSize - 1]); |
| 584 | fprintf(out_file_, "file_size : %d\n", header_->FileSize()); |
| 585 | fprintf(out_file_, "header_size : %d\n", header_->HeaderSize()); |
| 586 | fprintf(out_file_, "link_size : %d\n", header_->LinkSize()); |
| 587 | fprintf(out_file_, "link_off : %d (0x%06x)\n", |
| 588 | header_->LinkOffset(), header_->LinkOffset()); |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 589 | fprintf(out_file_, "string_ids_size : %d\n", header_->StringIds().Size()); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 590 | fprintf(out_file_, "string_ids_off : %d (0x%06x)\n", |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 591 | header_->StringIds().GetOffset(), header_->StringIds().GetOffset()); |
| 592 | fprintf(out_file_, "type_ids_size : %d\n", header_->TypeIds().Size()); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 593 | fprintf(out_file_, "type_ids_off : %d (0x%06x)\n", |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 594 | header_->TypeIds().GetOffset(), header_->TypeIds().GetOffset()); |
| 595 | fprintf(out_file_, "proto_ids_size : %d\n", header_->ProtoIds().Size()); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 596 | fprintf(out_file_, "proto_ids_off : %d (0x%06x)\n", |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 597 | header_->ProtoIds().GetOffset(), header_->ProtoIds().GetOffset()); |
| 598 | fprintf(out_file_, "field_ids_size : %d\n", header_->FieldIds().Size()); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 599 | fprintf(out_file_, "field_ids_off : %d (0x%06x)\n", |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 600 | header_->FieldIds().GetOffset(), header_->FieldIds().GetOffset()); |
| 601 | fprintf(out_file_, "method_ids_size : %d\n", header_->MethodIds().Size()); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 602 | fprintf(out_file_, "method_ids_off : %d (0x%06x)\n", |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 603 | header_->MethodIds().GetOffset(), header_->MethodIds().GetOffset()); |
| 604 | fprintf(out_file_, "class_defs_size : %d\n", header_->ClassDefs().Size()); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 605 | fprintf(out_file_, "class_defs_off : %d (0x%06x)\n", |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 606 | header_->ClassDefs().GetOffset(), header_->ClassDefs().GetOffset()); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 607 | fprintf(out_file_, "data_size : %d\n", header_->DataSize()); |
| 608 | fprintf(out_file_, "data_off : %d (0x%06x)\n\n", |
| 609 | header_->DataOffset(), header_->DataOffset()); |
| 610 | } |
| 611 | |
| 612 | /* |
| 613 | * Dumps a class_def_item. |
| 614 | */ |
| 615 | void DexLayout::DumpClassDef(int idx) { |
| 616 | // General class information. |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 617 | dex_ir::ClassDef* class_def = header_->ClassDefs()[idx]; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 618 | fprintf(out_file_, "Class #%d header:\n", idx); |
| 619 | fprintf(out_file_, "class_idx : %d\n", class_def->ClassType()->GetIndex()); |
| 620 | fprintf(out_file_, "access_flags : %d (0x%04x)\n", |
| 621 | class_def->GetAccessFlags(), class_def->GetAccessFlags()); |
| 622 | uint32_t superclass_idx = class_def->Superclass() == nullptr ? |
| 623 | DexFile::kDexNoIndex16 : class_def->Superclass()->GetIndex(); |
| 624 | fprintf(out_file_, "superclass_idx : %d\n", superclass_idx); |
| 625 | fprintf(out_file_, "interfaces_off : %d (0x%06x)\n", |
| 626 | class_def->InterfacesOffset(), class_def->InterfacesOffset()); |
| 627 | uint32_t source_file_offset = 0xffffffffU; |
| 628 | if (class_def->SourceFile() != nullptr) { |
| 629 | source_file_offset = class_def->SourceFile()->GetIndex(); |
| 630 | } |
| 631 | fprintf(out_file_, "source_file_idx : %d\n", source_file_offset); |
| 632 | uint32_t annotations_offset = 0; |
| 633 | if (class_def->Annotations() != nullptr) { |
| 634 | annotations_offset = class_def->Annotations()->GetOffset(); |
| 635 | } |
| 636 | fprintf(out_file_, "annotations_off : %d (0x%06x)\n", |
| 637 | annotations_offset, annotations_offset); |
| 638 | if (class_def->GetClassData() == nullptr) { |
| 639 | fprintf(out_file_, "class_data_off : %d (0x%06x)\n", 0, 0); |
| 640 | } else { |
| 641 | fprintf(out_file_, "class_data_off : %d (0x%06x)\n", |
| 642 | class_def->GetClassData()->GetOffset(), class_def->GetClassData()->GetOffset()); |
| 643 | } |
| 644 | |
| 645 | // Fields and methods. |
| 646 | dex_ir::ClassData* class_data = class_def->GetClassData(); |
| 647 | if (class_data != nullptr && class_data->StaticFields() != nullptr) { |
| 648 | fprintf(out_file_, "static_fields_size : %zu\n", class_data->StaticFields()->size()); |
| 649 | } else { |
| 650 | fprintf(out_file_, "static_fields_size : 0\n"); |
| 651 | } |
| 652 | if (class_data != nullptr && class_data->InstanceFields() != nullptr) { |
| 653 | fprintf(out_file_, "instance_fields_size: %zu\n", class_data->InstanceFields()->size()); |
| 654 | } else { |
| 655 | fprintf(out_file_, "instance_fields_size: 0\n"); |
| 656 | } |
| 657 | if (class_data != nullptr && class_data->DirectMethods() != nullptr) { |
| 658 | fprintf(out_file_, "direct_methods_size : %zu\n", class_data->DirectMethods()->size()); |
| 659 | } else { |
| 660 | fprintf(out_file_, "direct_methods_size : 0\n"); |
| 661 | } |
| 662 | if (class_data != nullptr && class_data->VirtualMethods() != nullptr) { |
| 663 | fprintf(out_file_, "virtual_methods_size: %zu\n", class_data->VirtualMethods()->size()); |
| 664 | } else { |
| 665 | fprintf(out_file_, "virtual_methods_size: 0\n"); |
| 666 | } |
| 667 | fprintf(out_file_, "\n"); |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Dumps an annotation set item. |
| 672 | */ |
| 673 | void DexLayout::DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item) { |
| 674 | if (set_item == nullptr || set_item->GetItems()->size() == 0) { |
| 675 | fputs(" empty-annotation-set\n", out_file_); |
| 676 | return; |
| 677 | } |
| 678 | for (dex_ir::AnnotationItem* annotation : *set_item->GetItems()) { |
| 679 | if (annotation == nullptr) { |
| 680 | continue; |
| 681 | } |
| 682 | fputs(" ", out_file_); |
| 683 | switch (annotation->GetVisibility()) { |
| 684 | case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", out_file_); break; |
| 685 | case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", out_file_); break; |
| 686 | case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", out_file_); break; |
| 687 | default: fputs("VISIBILITY_UNKNOWN ", out_file_); break; |
| 688 | } // switch |
| 689 | DumpEncodedAnnotation(annotation->GetAnnotation()); |
| 690 | fputc('\n', out_file_); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | * Dumps class annotations. |
| 696 | */ |
| 697 | void DexLayout::DumpClassAnnotations(int idx) { |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 698 | dex_ir::ClassDef* class_def = header_->ClassDefs()[idx]; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 699 | dex_ir::AnnotationsDirectoryItem* annotations_directory = class_def->Annotations(); |
| 700 | if (annotations_directory == nullptr) { |
| 701 | return; // none |
| 702 | } |
| 703 | |
| 704 | fprintf(out_file_, "Class #%d annotations:\n", idx); |
| 705 | |
| 706 | dex_ir::AnnotationSetItem* class_set_item = annotations_directory->GetClassAnnotation(); |
| 707 | dex_ir::FieldAnnotationVector* fields = annotations_directory->GetFieldAnnotations(); |
| 708 | dex_ir::MethodAnnotationVector* methods = annotations_directory->GetMethodAnnotations(); |
| 709 | dex_ir::ParameterAnnotationVector* parameters = annotations_directory->GetParameterAnnotations(); |
| 710 | |
| 711 | // Annotations on the class itself. |
| 712 | if (class_set_item != nullptr) { |
| 713 | fprintf(out_file_, "Annotations on class\n"); |
| 714 | DumpAnnotationSetItem(class_set_item); |
| 715 | } |
| 716 | |
| 717 | // Annotations on fields. |
| 718 | if (fields != nullptr) { |
| 719 | for (auto& field : *fields) { |
| 720 | const dex_ir::FieldId* field_id = field->GetFieldId(); |
| 721 | const uint32_t field_idx = field_id->GetIndex(); |
| 722 | const char* field_name = field_id->Name()->Data(); |
| 723 | fprintf(out_file_, "Annotations on field #%u '%s'\n", field_idx, field_name); |
| 724 | DumpAnnotationSetItem(field->GetAnnotationSetItem()); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | // Annotations on methods. |
| 729 | if (methods != nullptr) { |
| 730 | for (auto& method : *methods) { |
| 731 | const dex_ir::MethodId* method_id = method->GetMethodId(); |
| 732 | const uint32_t method_idx = method_id->GetIndex(); |
| 733 | const char* method_name = method_id->Name()->Data(); |
| 734 | fprintf(out_file_, "Annotations on method #%u '%s'\n", method_idx, method_name); |
| 735 | DumpAnnotationSetItem(method->GetAnnotationSetItem()); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | // Annotations on method parameters. |
| 740 | if (parameters != nullptr) { |
| 741 | for (auto& parameter : *parameters) { |
| 742 | const dex_ir::MethodId* method_id = parameter->GetMethodId(); |
| 743 | const uint32_t method_idx = method_id->GetIndex(); |
| 744 | const char* method_name = method_id->Name()->Data(); |
| 745 | fprintf(out_file_, "Annotations on method #%u '%s' parameters\n", method_idx, method_name); |
| 746 | uint32_t j = 0; |
| 747 | for (dex_ir::AnnotationSetItem* annotation : *parameter->GetAnnotations()->GetItems()) { |
| 748 | fprintf(out_file_, "#%u\n", j); |
| 749 | DumpAnnotationSetItem(annotation); |
| 750 | ++j; |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | fputc('\n', out_file_); |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | * Dumps an interface that a class declares to implement. |
| 760 | */ |
| 761 | void DexLayout::DumpInterface(const dex_ir::TypeId* type_item, int i) { |
| 762 | const char* interface_name = type_item->GetStringId()->Data(); |
| 763 | if (options_.output_format_ == kOutputPlain) { |
| 764 | fprintf(out_file_, " #%d : '%s'\n", i, interface_name); |
| 765 | } else { |
Orion Hodson | fe42d21 | 2018-08-24 14:01:14 +0100 | [diff] [blame] | 766 | std::string dot(DescriptorToDot(interface_name)); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 767 | fprintf(out_file_, "<implements name=\"%s\">\n</implements>\n", dot.c_str()); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | /* |
| 772 | * Dumps the catches table associated with the code. |
| 773 | */ |
| 774 | void DexLayout::DumpCatches(const dex_ir::CodeItem* code) { |
| 775 | const uint16_t tries_size = code->TriesSize(); |
| 776 | |
| 777 | // No catch table. |
| 778 | if (tries_size == 0) { |
| 779 | fprintf(out_file_, " catches : (none)\n"); |
| 780 | return; |
| 781 | } |
| 782 | |
| 783 | // Dump all table entries. |
| 784 | fprintf(out_file_, " catches : %d\n", tries_size); |
| 785 | std::vector<std::unique_ptr<const dex_ir::TryItem>>* tries = code->Tries(); |
| 786 | for (uint32_t i = 0; i < tries_size; i++) { |
| 787 | const dex_ir::TryItem* try_item = (*tries)[i].get(); |
| 788 | const uint32_t start = try_item->StartAddr(); |
| 789 | const uint32_t end = start + try_item->InsnCount(); |
| 790 | fprintf(out_file_, " 0x%04x - 0x%04x\n", start, end); |
| 791 | for (auto& handler : *try_item->GetHandlers()->GetHandlers()) { |
| 792 | const dex_ir::TypeId* type_id = handler->GetTypeId(); |
| 793 | const char* descriptor = (type_id == nullptr) ? "<any>" : type_id->GetStringId()->Data(); |
| 794 | fprintf(out_file_, " %s -> 0x%04x\n", descriptor, handler->GetAddress()); |
| 795 | } // for |
| 796 | } // for |
| 797 | } |
| 798 | |
| 799 | /* |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 800 | * Dumps a single instruction. |
| 801 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 802 | void DexLayout::DumpInstruction(const dex_ir::CodeItem* code, |
| 803 | uint32_t code_offset, |
| 804 | uint32_t insn_idx, |
| 805 | uint32_t insn_width, |
| 806 | const Instruction* dec_insn) { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 807 | // Address of instruction (expressed as byte offset). |
| 808 | fprintf(out_file_, "%06x:", code_offset + 0x10 + insn_idx * 2); |
| 809 | |
| 810 | // Dump (part of) raw bytes. |
| 811 | const uint16_t* insns = code->Insns(); |
| 812 | for (uint32_t i = 0; i < 8; i++) { |
| 813 | if (i < insn_width) { |
| 814 | if (i == 7) { |
| 815 | fprintf(out_file_, " ... "); |
| 816 | } else { |
| 817 | // Print 16-bit value in little-endian order. |
| 818 | const uint8_t* bytePtr = (const uint8_t*) &insns[insn_idx + i]; |
| 819 | fprintf(out_file_, " %02x%02x", bytePtr[0], bytePtr[1]); |
| 820 | } |
| 821 | } else { |
| 822 | fputs(" ", out_file_); |
| 823 | } |
| 824 | } // for |
| 825 | |
| 826 | // Dump pseudo-instruction or opcode. |
| 827 | if (dec_insn->Opcode() == Instruction::NOP) { |
| 828 | const uint16_t instr = Get2LE((const uint8_t*) &insns[insn_idx]); |
| 829 | if (instr == Instruction::kPackedSwitchSignature) { |
| 830 | fprintf(out_file_, "|%04x: packed-switch-data (%d units)", insn_idx, insn_width); |
| 831 | } else if (instr == Instruction::kSparseSwitchSignature) { |
| 832 | fprintf(out_file_, "|%04x: sparse-switch-data (%d units)", insn_idx, insn_width); |
| 833 | } else if (instr == Instruction::kArrayDataSignature) { |
| 834 | fprintf(out_file_, "|%04x: array-data (%d units)", insn_idx, insn_width); |
| 835 | } else { |
| 836 | fprintf(out_file_, "|%04x: nop // spacer", insn_idx); |
| 837 | } |
| 838 | } else { |
| 839 | fprintf(out_file_, "|%04x: %s", insn_idx, dec_insn->Name()); |
| 840 | } |
| 841 | |
| 842 | // Set up additional argument. |
| 843 | std::unique_ptr<char[]> index_buf; |
| 844 | if (Instruction::IndexTypeOf(dec_insn->Opcode()) != Instruction::kIndexNone) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 845 | index_buf = IndexString(header_, dec_insn, 200); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | // Dump the instruction. |
| 849 | // |
| 850 | // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original. |
| 851 | // |
| 852 | switch (Instruction::FormatOf(dec_insn->Opcode())) { |
| 853 | case Instruction::k10x: // op |
| 854 | break; |
| 855 | case Instruction::k12x: // op vA, vB |
| 856 | fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB()); |
| 857 | break; |
| 858 | case Instruction::k11n: // op vA, #+B |
| 859 | fprintf(out_file_, " v%d, #int %d // #%x", |
| 860 | dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint8_t)dec_insn->VRegB()); |
| 861 | break; |
| 862 | case Instruction::k11x: // op vAA |
| 863 | fprintf(out_file_, " v%d", dec_insn->VRegA()); |
| 864 | break; |
| 865 | case Instruction::k10t: // op +AA |
| 866 | case Instruction::k20t: { // op +AAAA |
| 867 | const int32_t targ = (int32_t) dec_insn->VRegA(); |
| 868 | fprintf(out_file_, " %04x // %c%04x", |
| 869 | insn_idx + targ, |
| 870 | (targ < 0) ? '-' : '+', |
| 871 | (targ < 0) ? -targ : targ); |
| 872 | break; |
| 873 | } |
| 874 | case Instruction::k22x: // op vAA, vBBBB |
| 875 | fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB()); |
| 876 | break; |
| 877 | case Instruction::k21t: { // op vAA, +BBBB |
| 878 | const int32_t targ = (int32_t) dec_insn->VRegB(); |
| 879 | fprintf(out_file_, " v%d, %04x // %c%04x", dec_insn->VRegA(), |
| 880 | insn_idx + targ, |
| 881 | (targ < 0) ? '-' : '+', |
| 882 | (targ < 0) ? -targ : targ); |
| 883 | break; |
| 884 | } |
| 885 | case Instruction::k21s: // op vAA, #+BBBB |
| 886 | fprintf(out_file_, " v%d, #int %d // #%x", |
| 887 | dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint16_t)dec_insn->VRegB()); |
| 888 | break; |
| 889 | case Instruction::k21h: // op vAA, #+BBBB0000[00000000] |
| 890 | // The printed format varies a bit based on the actual opcode. |
| 891 | if (dec_insn->Opcode() == Instruction::CONST_HIGH16) { |
| 892 | const int32_t value = dec_insn->VRegB() << 16; |
| 893 | fprintf(out_file_, " v%d, #int %d // #%x", |
| 894 | dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB()); |
| 895 | } else { |
| 896 | const int64_t value = ((int64_t) dec_insn->VRegB()) << 48; |
| 897 | fprintf(out_file_, " v%d, #long %" PRId64 " // #%x", |
| 898 | dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB()); |
| 899 | } |
| 900 | break; |
| 901 | case Instruction::k21c: // op vAA, thing@BBBB |
| 902 | case Instruction::k31c: // op vAA, thing@BBBBBBBB |
| 903 | fprintf(out_file_, " v%d, %s", dec_insn->VRegA(), index_buf.get()); |
| 904 | break; |
| 905 | case Instruction::k23x: // op vAA, vBB, vCC |
| 906 | fprintf(out_file_, " v%d, v%d, v%d", |
| 907 | dec_insn->VRegA(), dec_insn->VRegB(), dec_insn->VRegC()); |
| 908 | break; |
| 909 | case Instruction::k22b: // op vAA, vBB, #+CC |
| 910 | fprintf(out_file_, " v%d, v%d, #int %d // #%02x", |
| 911 | dec_insn->VRegA(), dec_insn->VRegB(), |
| 912 | (int32_t) dec_insn->VRegC(), (uint8_t) dec_insn->VRegC()); |
| 913 | break; |
| 914 | case Instruction::k22t: { // op vA, vB, +CCCC |
| 915 | const int32_t targ = (int32_t) dec_insn->VRegC(); |
| 916 | fprintf(out_file_, " v%d, v%d, %04x // %c%04x", |
| 917 | dec_insn->VRegA(), dec_insn->VRegB(), |
| 918 | insn_idx + targ, |
| 919 | (targ < 0) ? '-' : '+', |
| 920 | (targ < 0) ? -targ : targ); |
| 921 | break; |
| 922 | } |
| 923 | case Instruction::k22s: // op vA, vB, #+CCCC |
| 924 | fprintf(out_file_, " v%d, v%d, #int %d // #%04x", |
| 925 | dec_insn->VRegA(), dec_insn->VRegB(), |
| 926 | (int32_t) dec_insn->VRegC(), (uint16_t) dec_insn->VRegC()); |
| 927 | break; |
| 928 | case Instruction::k22c: // op vA, vB, thing@CCCC |
| 929 | // NOT SUPPORTED: |
| 930 | // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC |
| 931 | fprintf(out_file_, " v%d, v%d, %s", |
| 932 | dec_insn->VRegA(), dec_insn->VRegB(), index_buf.get()); |
| 933 | break; |
| 934 | case Instruction::k30t: |
| 935 | fprintf(out_file_, " #%08x", dec_insn->VRegA()); |
| 936 | break; |
| 937 | case Instruction::k31i: { // op vAA, #+BBBBBBBB |
| 938 | // This is often, but not always, a float. |
| 939 | union { |
| 940 | float f; |
| 941 | uint32_t i; |
| 942 | } conv; |
| 943 | conv.i = dec_insn->VRegB(); |
| 944 | fprintf(out_file_, " v%d, #float %g // #%08x", |
| 945 | dec_insn->VRegA(), conv.f, dec_insn->VRegB()); |
| 946 | break; |
| 947 | } |
| 948 | case Instruction::k31t: // op vAA, offset +BBBBBBBB |
| 949 | fprintf(out_file_, " v%d, %08x // +%08x", |
| 950 | dec_insn->VRegA(), insn_idx + dec_insn->VRegB(), dec_insn->VRegB()); |
| 951 | break; |
| 952 | case Instruction::k32x: // op vAAAA, vBBBB |
| 953 | fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB()); |
| 954 | break; |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 955 | case Instruction::k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB |
| 956 | case Instruction::k45cc: { // op {vC, vD, vE, vF, vG}, meth@BBBB, proto@HHHH |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 957 | // NOT SUPPORTED: |
| 958 | // case Instruction::k35ms: // [opt] invoke-virtual+super |
| 959 | // case Instruction::k35mi: // [opt] inline invoke |
| 960 | uint32_t arg[Instruction::kMaxVarArgRegs]; |
| 961 | dec_insn->GetVarArgs(arg); |
| 962 | fputs(" {", out_file_); |
| 963 | for (int i = 0, n = dec_insn->VRegA(); i < n; i++) { |
| 964 | if (i == 0) { |
| 965 | fprintf(out_file_, "v%d", arg[i]); |
| 966 | } else { |
| 967 | fprintf(out_file_, ", v%d", arg[i]); |
| 968 | } |
| 969 | } // for |
| 970 | fprintf(out_file_, "}, %s", index_buf.get()); |
| 971 | break; |
| 972 | } |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 973 | case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB |
| 974 | case Instruction::k4rcc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB, proto@HHHH |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 975 | // NOT SUPPORTED: |
| 976 | // case Instruction::k3rms: // [opt] invoke-virtual+super/range |
| 977 | // case Instruction::k3rmi: // [opt] execute-inline/range |
| 978 | { |
| 979 | // This doesn't match the "dx" output when some of the args are |
| 980 | // 64-bit values -- dx only shows the first register. |
| 981 | fputs(" {", out_file_); |
| 982 | for (int i = 0, n = dec_insn->VRegA(); i < n; i++) { |
| 983 | if (i == 0) { |
| 984 | fprintf(out_file_, "v%d", dec_insn->VRegC() + i); |
| 985 | } else { |
| 986 | fprintf(out_file_, ", v%d", dec_insn->VRegC() + i); |
| 987 | } |
| 988 | } // for |
| 989 | fprintf(out_file_, "}, %s", index_buf.get()); |
| 990 | } |
| 991 | break; |
| 992 | case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB |
| 993 | // This is often, but not always, a double. |
| 994 | union { |
| 995 | double d; |
| 996 | uint64_t j; |
| 997 | } conv; |
| 998 | conv.j = dec_insn->WideVRegB(); |
| 999 | fprintf(out_file_, " v%d, #double %g // #%016" PRIx64, |
| 1000 | dec_insn->VRegA(), conv.d, dec_insn->WideVRegB()); |
| 1001 | break; |
| 1002 | } |
| 1003 | // NOT SUPPORTED: |
| 1004 | // case Instruction::k00x: // unknown op or breakpoint |
| 1005 | // break; |
| 1006 | default: |
| 1007 | fprintf(out_file_, " ???"); |
| 1008 | break; |
| 1009 | } // switch |
| 1010 | |
| 1011 | fputc('\n', out_file_); |
| 1012 | } |
| 1013 | |
| 1014 | /* |
| 1015 | * Dumps a bytecode disassembly. |
| 1016 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1017 | void DexLayout::DumpBytecodes(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset) { |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1018 | dex_ir::MethodId* method_id = header_->MethodIds()[idx]; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1019 | const char* name = method_id->Name()->Data(); |
David Sehr | 7235922 | 2016-09-07 13:04:01 -0700 | [diff] [blame] | 1020 | std::string type_descriptor = GetSignatureForProtoId(method_id->Proto()); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1021 | const char* back_descriptor = method_id->Class()->GetStringId()->Data(); |
| 1022 | |
| 1023 | // Generate header. |
Orion Hodson | fe42d21 | 2018-08-24 14:01:14 +0100 | [diff] [blame] | 1024 | std::string dot(DescriptorToDot(back_descriptor)); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1025 | fprintf(out_file_, "%06x: |[%06x] %s.%s:%s\n", |
David Sehr | 7235922 | 2016-09-07 13:04:01 -0700 | [diff] [blame] | 1026 | code_offset, code_offset, dot.c_str(), name, type_descriptor.c_str()); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1027 | |
| 1028 | // Iterate over all instructions. |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1029 | for (const DexInstructionPcPair& inst : code->Instructions()) { |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1030 | const uint32_t insn_width = inst->SizeInCodeUnits(); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1031 | if (insn_width == 0) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 1032 | LOG(WARNING) << "GLITCH: zero-width instruction at idx=0x" << std::hex << inst.DexPc(); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1033 | break; |
| 1034 | } |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1035 | DumpInstruction(code, code_offset, inst.DexPc(), insn_width, &inst.Inst()); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1036 | } // for |
| 1037 | } |
| 1038 | |
| 1039 | /* |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 1040 | * Lookup functions. |
| 1041 | */ |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1042 | static const char* StringDataByIdx(uint32_t idx, dex_ir::Header* header) { |
| 1043 | dex_ir::StringId* string_id = header->GetStringIdOrNullPtr(idx); |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 1044 | if (string_id == nullptr) { |
| 1045 | return nullptr; |
| 1046 | } |
| 1047 | return string_id->Data(); |
| 1048 | } |
| 1049 | |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1050 | static const char* StringDataByTypeIdx(uint16_t idx, dex_ir::Header* header) { |
| 1051 | dex_ir::TypeId* type_id = header->GetTypeIdOrNullPtr(idx); |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 1052 | if (type_id == nullptr) { |
| 1053 | return nullptr; |
| 1054 | } |
| 1055 | dex_ir::StringId* string_id = type_id->GetStringId(); |
| 1056 | if (string_id == nullptr) { |
| 1057 | return nullptr; |
| 1058 | } |
| 1059 | return string_id->Data(); |
| 1060 | } |
| 1061 | |
| 1062 | |
| 1063 | /* |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1064 | * Dumps code of a method. |
| 1065 | */ |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 1066 | void DexLayout::DumpCode(uint32_t idx, |
| 1067 | const dex_ir::CodeItem* code, |
| 1068 | uint32_t code_offset, |
| 1069 | const char* declaring_class_descriptor, |
| 1070 | const char* method_name, |
| 1071 | bool is_static, |
| 1072 | const dex_ir::ProtoId* proto) { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1073 | fprintf(out_file_, " registers : %d\n", code->RegistersSize()); |
| 1074 | fprintf(out_file_, " ins : %d\n", code->InsSize()); |
| 1075 | fprintf(out_file_, " outs : %d\n", code->OutsSize()); |
| 1076 | fprintf(out_file_, " insns size : %d 16-bit code units\n", |
| 1077 | code->InsnsSize()); |
| 1078 | |
| 1079 | // Bytecode disassembly, if requested. |
| 1080 | if (options_.disassemble_) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1081 | DumpBytecodes(idx, code, code_offset); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | // Try-catch blocks. |
| 1085 | DumpCatches(code); |
| 1086 | |
| 1087 | // Positions and locals table in the debug info. |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 1088 | dex_ir::DebugInfoItem* debug_info = code->DebugInfo(); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1089 | fprintf(out_file_, " positions : \n"); |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 1090 | if (debug_info != nullptr) { |
| 1091 | DexFile::DecodeDebugPositionInfo(debug_info->GetDebugInfo(), |
| 1092 | [this](uint32_t idx) { |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1093 | return StringDataByIdx(idx, this->header_); |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 1094 | }, |
Mathieu Chartier | 3e2e123 | 2018-09-11 12:35:30 -0700 | [diff] [blame] | 1095 | [&](const DexFile::PositionInfo& entry) { |
| 1096 | fprintf(out_file_, |
| 1097 | " 0x%04x line=%d\n", |
| 1098 | entry.address_, |
| 1099 | entry.line_); |
| 1100 | return false; |
| 1101 | }); |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 1102 | } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1103 | fprintf(out_file_, " locals : \n"); |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 1104 | if (debug_info != nullptr) { |
| 1105 | std::vector<const char*> arg_descriptors; |
| 1106 | const dex_ir::TypeList* parameters = proto->Parameters(); |
| 1107 | if (parameters != nullptr) { |
| 1108 | const dex_ir::TypeIdVector* parameter_type_vector = parameters->GetTypeList(); |
| 1109 | if (parameter_type_vector != nullptr) { |
| 1110 | for (const dex_ir::TypeId* type_id : *parameter_type_vector) { |
| 1111 | arg_descriptors.push_back(type_id->GetStringId()->Data()); |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | DexFile::DecodeDebugLocalInfo(debug_info->GetDebugInfo(), |
| 1116 | "DexLayout in-memory", |
| 1117 | declaring_class_descriptor, |
| 1118 | arg_descriptors, |
| 1119 | method_name, |
| 1120 | is_static, |
| 1121 | code->RegistersSize(), |
| 1122 | code->InsSize(), |
| 1123 | code->InsnsSize(), |
| 1124 | [this](uint32_t idx) { |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1125 | return StringDataByIdx(idx, this->header_); |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 1126 | }, |
| 1127 | [this](uint32_t idx) { |
| 1128 | return |
| 1129 | StringDataByTypeIdx(dchecked_integral_cast<uint16_t>(idx), |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1130 | this->header_); |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 1131 | }, |
Mathieu Chartier | e5afbf3 | 2018-09-12 17:51:54 -0700 | [diff] [blame] | 1132 | [&](const DexFile::LocalInfo& entry) { |
| 1133 | const char* signature = |
| 1134 | entry.signature_ != nullptr ? entry.signature_ : ""; |
| 1135 | fprintf(out_file_, |
| 1136 | " 0x%04x - 0x%04x reg=%d %s %s %s\n", |
| 1137 | entry.start_address_, |
| 1138 | entry.end_address_, |
| 1139 | entry.reg_, |
| 1140 | entry.name_, |
| 1141 | entry.descriptor_, |
| 1142 | signature); |
| 1143 | }); |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 1144 | } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | /* |
| 1148 | * Dumps a method. |
| 1149 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1150 | void DexLayout::DumpMethod(uint32_t idx, uint32_t flags, const dex_ir::CodeItem* code, int i) { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1151 | // Bail for anything private if export only requested. |
| 1152 | if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) { |
| 1153 | return; |
| 1154 | } |
| 1155 | |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1156 | dex_ir::MethodId* method_id = header_->MethodIds()[idx]; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1157 | const char* name = method_id->Name()->Data(); |
| 1158 | char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str()); |
| 1159 | const char* back_descriptor = method_id->Class()->GetStringId()->Data(); |
| 1160 | char* access_str = CreateAccessFlagStr(flags, kAccessForMethod); |
| 1161 | |
| 1162 | if (options_.output_format_ == kOutputPlain) { |
| 1163 | fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor); |
| 1164 | fprintf(out_file_, " name : '%s'\n", name); |
| 1165 | fprintf(out_file_, " type : '%s'\n", type_descriptor); |
| 1166 | fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str); |
| 1167 | if (code == nullptr) { |
| 1168 | fprintf(out_file_, " code : (none)\n"); |
| 1169 | } else { |
| 1170 | fprintf(out_file_, " code -\n"); |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 1171 | DumpCode(idx, |
| 1172 | code, |
| 1173 | code->GetOffset(), |
| 1174 | back_descriptor, |
| 1175 | name, |
| 1176 | (flags & kAccStatic) != 0, |
| 1177 | method_id->Proto()); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1178 | } |
| 1179 | if (options_.disassemble_) { |
| 1180 | fputc('\n', out_file_); |
| 1181 | } |
| 1182 | } else if (options_.output_format_ == kOutputXml) { |
| 1183 | const bool constructor = (name[0] == '<'); |
| 1184 | |
| 1185 | // Method name and prototype. |
| 1186 | if (constructor) { |
Orion Hodson | fe42d21 | 2018-08-24 14:01:14 +0100 | [diff] [blame] | 1187 | std::string dot(DescriptorClassToName(back_descriptor)); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1188 | fprintf(out_file_, "<constructor name=\"%s\"\n", dot.c_str()); |
Orion Hodson | fe42d21 | 2018-08-24 14:01:14 +0100 | [diff] [blame] | 1189 | dot = DescriptorToDot(back_descriptor); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1190 | fprintf(out_file_, " type=\"%s\"\n", dot.c_str()); |
| 1191 | } else { |
| 1192 | fprintf(out_file_, "<method name=\"%s\"\n", name); |
| 1193 | const char* return_type = strrchr(type_descriptor, ')'); |
| 1194 | if (return_type == nullptr) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 1195 | LOG(ERROR) << "bad method type descriptor '" << type_descriptor << "'"; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1196 | goto bail; |
| 1197 | } |
Orion Hodson | fe42d21 | 2018-08-24 14:01:14 +0100 | [diff] [blame] | 1198 | std::string dot(DescriptorToDot(return_type + 1)); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1199 | fprintf(out_file_, " return=\"%s\"\n", dot.c_str()); |
| 1200 | fprintf(out_file_, " abstract=%s\n", QuotedBool((flags & kAccAbstract) != 0)); |
| 1201 | fprintf(out_file_, " native=%s\n", QuotedBool((flags & kAccNative) != 0)); |
| 1202 | fprintf(out_file_, " synchronized=%s\n", QuotedBool( |
| 1203 | (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0)); |
| 1204 | } |
| 1205 | |
| 1206 | // Additional method flags. |
| 1207 | fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0)); |
| 1208 | fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0)); |
| 1209 | // The "deprecated=" not knowable w/o parsing annotations. |
| 1210 | fprintf(out_file_, " visibility=%s\n>\n", QuotedVisibility(flags)); |
| 1211 | |
| 1212 | // Parameters. |
| 1213 | if (type_descriptor[0] != '(') { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 1214 | LOG(ERROR) << "ERROR: bad descriptor '" << type_descriptor << "'"; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1215 | goto bail; |
| 1216 | } |
| 1217 | char* tmp_buf = reinterpret_cast<char*>(malloc(strlen(type_descriptor) + 1)); |
| 1218 | const char* base = type_descriptor + 1; |
| 1219 | int arg_num = 0; |
| 1220 | while (*base != ')') { |
| 1221 | char* cp = tmp_buf; |
| 1222 | while (*base == '[') { |
| 1223 | *cp++ = *base++; |
| 1224 | } |
| 1225 | if (*base == 'L') { |
| 1226 | // Copy through ';'. |
| 1227 | do { |
| 1228 | *cp = *base++; |
| 1229 | } while (*cp++ != ';'); |
| 1230 | } else { |
| 1231 | // Primitive char, copy it. |
| 1232 | if (strchr("ZBCSIFJD", *base) == nullptr) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 1233 | LOG(ERROR) << "ERROR: bad method signature '" << base << "'"; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1234 | break; // while |
| 1235 | } |
| 1236 | *cp++ = *base++; |
| 1237 | } |
| 1238 | // Null terminate and display. |
| 1239 | *cp++ = '\0'; |
Orion Hodson | fe42d21 | 2018-08-24 14:01:14 +0100 | [diff] [blame] | 1240 | std::string dot(DescriptorToDot(tmp_buf)); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1241 | fprintf(out_file_, "<parameter name=\"arg%d\" type=\"%s\">\n" |
| 1242 | "</parameter>\n", arg_num++, dot.c_str()); |
| 1243 | } // while |
| 1244 | free(tmp_buf); |
| 1245 | if (constructor) { |
| 1246 | fprintf(out_file_, "</constructor>\n"); |
| 1247 | } else { |
| 1248 | fprintf(out_file_, "</method>\n"); |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | bail: |
| 1253 | free(type_descriptor); |
| 1254 | free(access_str); |
| 1255 | } |
| 1256 | |
| 1257 | /* |
| 1258 | * Dumps a static (class) field. |
| 1259 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1260 | void DexLayout::DumpSField(uint32_t idx, uint32_t flags, int i, dex_ir::EncodedValue* init) { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1261 | // Bail for anything private if export only requested. |
| 1262 | if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) { |
| 1263 | return; |
| 1264 | } |
| 1265 | |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1266 | dex_ir::FieldId* field_id = header_->FieldIds()[idx]; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1267 | const char* name = field_id->Name()->Data(); |
| 1268 | const char* type_descriptor = field_id->Type()->GetStringId()->Data(); |
| 1269 | const char* back_descriptor = field_id->Class()->GetStringId()->Data(); |
| 1270 | char* access_str = CreateAccessFlagStr(flags, kAccessForField); |
| 1271 | |
| 1272 | if (options_.output_format_ == kOutputPlain) { |
| 1273 | fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor); |
| 1274 | fprintf(out_file_, " name : '%s'\n", name); |
| 1275 | fprintf(out_file_, " type : '%s'\n", type_descriptor); |
| 1276 | fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str); |
| 1277 | if (init != nullptr) { |
| 1278 | fputs(" value : ", out_file_); |
| 1279 | DumpEncodedValue(init); |
| 1280 | fputs("\n", out_file_); |
| 1281 | } |
| 1282 | } else if (options_.output_format_ == kOutputXml) { |
| 1283 | fprintf(out_file_, "<field name=\"%s\"\n", name); |
Orion Hodson | fe42d21 | 2018-08-24 14:01:14 +0100 | [diff] [blame] | 1284 | std::string dot(DescriptorToDot(type_descriptor)); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1285 | fprintf(out_file_, " type=\"%s\"\n", dot.c_str()); |
| 1286 | fprintf(out_file_, " transient=%s\n", QuotedBool((flags & kAccTransient) != 0)); |
| 1287 | fprintf(out_file_, " volatile=%s\n", QuotedBool((flags & kAccVolatile) != 0)); |
| 1288 | // The "value=" is not knowable w/o parsing annotations. |
| 1289 | fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0)); |
| 1290 | fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0)); |
| 1291 | // The "deprecated=" is not knowable w/o parsing annotations. |
| 1292 | fprintf(out_file_, " visibility=%s\n", QuotedVisibility(flags)); |
| 1293 | if (init != nullptr) { |
| 1294 | fputs(" value=\"", out_file_); |
| 1295 | DumpEncodedValue(init); |
| 1296 | fputs("\"\n", out_file_); |
| 1297 | } |
| 1298 | fputs(">\n</field>\n", out_file_); |
| 1299 | } |
| 1300 | |
| 1301 | free(access_str); |
| 1302 | } |
| 1303 | |
| 1304 | /* |
| 1305 | * Dumps an instance field. |
| 1306 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1307 | void DexLayout::DumpIField(uint32_t idx, uint32_t flags, int i) { |
| 1308 | DumpSField(idx, flags, i, nullptr); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | /* |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1312 | * Dumps the class. |
| 1313 | * |
| 1314 | * Note "idx" is a DexClassDef index, not a DexTypeId index. |
| 1315 | * |
| 1316 | * If "*last_package" is nullptr or does not match the current class' package, |
| 1317 | * the value will be replaced with a newly-allocated string. |
| 1318 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1319 | void DexLayout::DumpClass(int idx, char** last_package) { |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1320 | dex_ir::ClassDef* class_def = header_->ClassDefs()[idx]; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1321 | // Omitting non-public class. |
| 1322 | if (options_.exports_only_ && (class_def->GetAccessFlags() & kAccPublic) == 0) { |
| 1323 | return; |
| 1324 | } |
| 1325 | |
| 1326 | if (options_.show_section_headers_) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1327 | DumpClassDef(idx); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | if (options_.show_annotations_) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1331 | DumpClassAnnotations(idx); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1334 | // For the XML output, show the package name. Ideally we'd gather |
| 1335 | // up the classes, sort them, and dump them alphabetically so the |
| 1336 | // package name wouldn't jump around, but that's not a great plan |
| 1337 | // for something that needs to run on the device. |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1338 | const char* class_descriptor = header_->ClassDefs()[idx]->ClassType()->GetStringId()->Data(); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1339 | if (!(class_descriptor[0] == 'L' && |
| 1340 | class_descriptor[strlen(class_descriptor)-1] == ';')) { |
| 1341 | // Arrays and primitives should not be defined explicitly. Keep going? |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 1342 | LOG(ERROR) << "Malformed class name '" << class_descriptor << "'"; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1343 | } else if (options_.output_format_ == kOutputXml) { |
| 1344 | char* mangle = strdup(class_descriptor + 1); |
| 1345 | mangle[strlen(mangle)-1] = '\0'; |
| 1346 | |
| 1347 | // Reduce to just the package name. |
| 1348 | char* last_slash = strrchr(mangle, '/'); |
| 1349 | if (last_slash != nullptr) { |
| 1350 | *last_slash = '\0'; |
| 1351 | } else { |
| 1352 | *mangle = '\0'; |
| 1353 | } |
| 1354 | |
| 1355 | for (char* cp = mangle; *cp != '\0'; cp++) { |
| 1356 | if (*cp == '/') { |
| 1357 | *cp = '.'; |
| 1358 | } |
| 1359 | } // for |
| 1360 | |
| 1361 | if (*last_package == nullptr || strcmp(mangle, *last_package) != 0) { |
| 1362 | // Start of a new package. |
| 1363 | if (*last_package != nullptr) { |
| 1364 | fprintf(out_file_, "</package>\n"); |
| 1365 | } |
| 1366 | fprintf(out_file_, "<package name=\"%s\"\n>\n", mangle); |
| 1367 | free(*last_package); |
| 1368 | *last_package = mangle; |
| 1369 | } else { |
| 1370 | free(mangle); |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | // General class information. |
| 1375 | char* access_str = CreateAccessFlagStr(class_def->GetAccessFlags(), kAccessForClass); |
| 1376 | const char* superclass_descriptor = nullptr; |
| 1377 | if (class_def->Superclass() != nullptr) { |
| 1378 | superclass_descriptor = class_def->Superclass()->GetStringId()->Data(); |
| 1379 | } |
| 1380 | if (options_.output_format_ == kOutputPlain) { |
| 1381 | fprintf(out_file_, "Class #%d -\n", idx); |
| 1382 | fprintf(out_file_, " Class descriptor : '%s'\n", class_descriptor); |
| 1383 | fprintf(out_file_, " Access flags : 0x%04x (%s)\n", |
| 1384 | class_def->GetAccessFlags(), access_str); |
| 1385 | if (superclass_descriptor != nullptr) { |
| 1386 | fprintf(out_file_, " Superclass : '%s'\n", superclass_descriptor); |
| 1387 | } |
| 1388 | fprintf(out_file_, " Interfaces -\n"); |
| 1389 | } else { |
Orion Hodson | fe42d21 | 2018-08-24 14:01:14 +0100 | [diff] [blame] | 1390 | std::string dot(DescriptorClassToName(class_descriptor)); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1391 | fprintf(out_file_, "<class name=\"%s\"\n", dot.c_str()); |
| 1392 | if (superclass_descriptor != nullptr) { |
Orion Hodson | fe42d21 | 2018-08-24 14:01:14 +0100 | [diff] [blame] | 1393 | dot = DescriptorToDot(superclass_descriptor); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1394 | fprintf(out_file_, " extends=\"%s\"\n", dot.c_str()); |
| 1395 | } |
| 1396 | fprintf(out_file_, " interface=%s\n", |
| 1397 | QuotedBool((class_def->GetAccessFlags() & kAccInterface) != 0)); |
| 1398 | fprintf(out_file_, " abstract=%s\n", |
| 1399 | QuotedBool((class_def->GetAccessFlags() & kAccAbstract) != 0)); |
| 1400 | fprintf(out_file_, " static=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccStatic) != 0)); |
| 1401 | fprintf(out_file_, " final=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccFinal) != 0)); |
| 1402 | // The "deprecated=" not knowable w/o parsing annotations. |
| 1403 | fprintf(out_file_, " visibility=%s\n", QuotedVisibility(class_def->GetAccessFlags())); |
| 1404 | fprintf(out_file_, ">\n"); |
| 1405 | } |
| 1406 | |
| 1407 | // Interfaces. |
Jeff Hao | cc82959 | 2017-03-14 16:13:39 -0700 | [diff] [blame] | 1408 | const dex_ir::TypeList* interfaces = class_def->Interfaces(); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1409 | if (interfaces != nullptr) { |
Jeff Hao | cc82959 | 2017-03-14 16:13:39 -0700 | [diff] [blame] | 1410 | const dex_ir::TypeIdVector* interfaces_vector = interfaces->GetTypeList(); |
| 1411 | for (uint32_t i = 0; i < interfaces_vector->size(); i++) { |
| 1412 | DumpInterface((*interfaces_vector)[i], i); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1413 | } // for |
| 1414 | } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1415 | |
| 1416 | // Fields and methods. |
| 1417 | dex_ir::ClassData* class_data = class_def->GetClassData(); |
| 1418 | // Prepare data for static fields. |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 1419 | dex_ir::EncodedArrayItem* static_values = class_def->StaticValues(); |
| 1420 | dex_ir::EncodedValueVector* encoded_values = |
| 1421 | static_values == nullptr ? nullptr : static_values->GetEncodedValues(); |
| 1422 | const uint32_t encoded_values_size = (encoded_values == nullptr) ? 0 : encoded_values->size(); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1423 | |
| 1424 | // Static fields. |
| 1425 | if (options_.output_format_ == kOutputPlain) { |
| 1426 | fprintf(out_file_, " Static fields -\n"); |
| 1427 | } |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1428 | if (class_data != nullptr) { |
| 1429 | dex_ir::FieldItemVector* static_fields = class_data->StaticFields(); |
| 1430 | if (static_fields != nullptr) { |
| 1431 | for (uint32_t i = 0; i < static_fields->size(); i++) { |
David Sehr | d83437c | 2018-06-11 14:06:23 -0700 | [diff] [blame] | 1432 | DumpSField((*static_fields)[i].GetFieldId()->GetIndex(), |
| 1433 | (*static_fields)[i].GetAccessFlags(), |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1434 | i, |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 1435 | i < encoded_values_size ? (*encoded_values)[i].get() : nullptr); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1436 | } // for |
| 1437 | } |
| 1438 | } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1439 | |
| 1440 | // Instance fields. |
| 1441 | if (options_.output_format_ == kOutputPlain) { |
| 1442 | fprintf(out_file_, " Instance fields -\n"); |
| 1443 | } |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1444 | if (class_data != nullptr) { |
| 1445 | dex_ir::FieldItemVector* instance_fields = class_data->InstanceFields(); |
| 1446 | if (instance_fields != nullptr) { |
| 1447 | for (uint32_t i = 0; i < instance_fields->size(); i++) { |
David Sehr | d83437c | 2018-06-11 14:06:23 -0700 | [diff] [blame] | 1448 | DumpIField((*instance_fields)[i].GetFieldId()->GetIndex(), |
| 1449 | (*instance_fields)[i].GetAccessFlags(), |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1450 | i); |
| 1451 | } // for |
| 1452 | } |
| 1453 | } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1454 | |
| 1455 | // Direct methods. |
| 1456 | if (options_.output_format_ == kOutputPlain) { |
| 1457 | fprintf(out_file_, " Direct methods -\n"); |
| 1458 | } |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1459 | if (class_data != nullptr) { |
| 1460 | dex_ir::MethodItemVector* direct_methods = class_data->DirectMethods(); |
| 1461 | if (direct_methods != nullptr) { |
| 1462 | for (uint32_t i = 0; i < direct_methods->size(); i++) { |
David Sehr | d83437c | 2018-06-11 14:06:23 -0700 | [diff] [blame] | 1463 | DumpMethod((*direct_methods)[i].GetMethodId()->GetIndex(), |
| 1464 | (*direct_methods)[i].GetAccessFlags(), |
| 1465 | (*direct_methods)[i].GetCodeItem(), |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1466 | i); |
| 1467 | } // for |
| 1468 | } |
| 1469 | } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1470 | |
| 1471 | // Virtual methods. |
| 1472 | if (options_.output_format_ == kOutputPlain) { |
| 1473 | fprintf(out_file_, " Virtual methods -\n"); |
| 1474 | } |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1475 | if (class_data != nullptr) { |
| 1476 | dex_ir::MethodItemVector* virtual_methods = class_data->VirtualMethods(); |
| 1477 | if (virtual_methods != nullptr) { |
| 1478 | for (uint32_t i = 0; i < virtual_methods->size(); i++) { |
David Sehr | d83437c | 2018-06-11 14:06:23 -0700 | [diff] [blame] | 1479 | DumpMethod((*virtual_methods)[i].GetMethodId()->GetIndex(), |
| 1480 | (*virtual_methods)[i].GetAccessFlags(), |
| 1481 | (*virtual_methods)[i].GetCodeItem(), |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1482 | i); |
| 1483 | } // for |
| 1484 | } |
| 1485 | } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1486 | |
| 1487 | // End of class. |
| 1488 | if (options_.output_format_ == kOutputPlain) { |
| 1489 | const char* file_name = "unknown"; |
| 1490 | if (class_def->SourceFile() != nullptr) { |
| 1491 | file_name = class_def->SourceFile()->Data(); |
| 1492 | } |
| 1493 | const dex_ir::StringId* source_file = class_def->SourceFile(); |
| 1494 | fprintf(out_file_, " source_file_idx : %d (%s)\n\n", |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 1495 | source_file == nullptr ? 0xffffffffU : source_file->GetIndex(), file_name); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1496 | } else if (options_.output_format_ == kOutputXml) { |
| 1497 | fprintf(out_file_, "</class>\n"); |
| 1498 | } |
| 1499 | |
| 1500 | free(access_str); |
| 1501 | } |
| 1502 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1503 | void DexLayout::DumpDexFile() { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1504 | // Headers. |
| 1505 | if (options_.show_file_headers_) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1506 | DumpFileHeader(); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | // Open XML context. |
| 1510 | if (options_.output_format_ == kOutputXml) { |
| 1511 | fprintf(out_file_, "<api>\n"); |
| 1512 | } |
| 1513 | |
| 1514 | // Iterate over all classes. |
| 1515 | char* package = nullptr; |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1516 | const uint32_t class_defs_size = header_->ClassDefs().Size(); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1517 | for (uint32_t i = 0; i < class_defs_size; i++) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1518 | DumpClass(i, &package); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1519 | } // for |
| 1520 | |
| 1521 | // Free the last package allocated. |
| 1522 | if (package != nullptr) { |
| 1523 | fprintf(out_file_, "</package>\n"); |
| 1524 | free(package); |
| 1525 | } |
| 1526 | |
| 1527 | // Close XML context. |
| 1528 | if (options_.output_format_ == kOutputXml) { |
| 1529 | fprintf(out_file_, "</api>\n"); |
| 1530 | } |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1531 | } |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 1532 | |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1533 | void DexLayout::LayoutClassDefsAndClassData(const DexFile* dex_file) { |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1534 | std::vector<dex_ir::ClassDef*> new_class_def_order; |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1535 | for (auto& class_def : header_->ClassDefs()) { |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1536 | dex::TypeIndex type_idx(class_def->ClassType()->GetIndex()); |
| 1537 | if (info_->ContainsClass(*dex_file, type_idx)) { |
| 1538 | new_class_def_order.push_back(class_def.get()); |
| 1539 | } |
| 1540 | } |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1541 | for (auto& class_def : header_->ClassDefs()) { |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1542 | dex::TypeIndex type_idx(class_def->ClassType()->GetIndex()); |
| 1543 | if (!info_->ContainsClass(*dex_file, type_idx)) { |
| 1544 | new_class_def_order.push_back(class_def.get()); |
| 1545 | } |
| 1546 | } |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1547 | std::unordered_set<dex_ir::ClassData*> visited_class_data; |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1548 | size_t class_data_index = 0; |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1549 | auto& class_datas = header_->ClassDatas(); |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1550 | for (dex_ir::ClassDef* class_def : new_class_def_order) { |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1551 | dex_ir::ClassData* class_data = class_def->GetClassData(); |
| 1552 | if (class_data != nullptr && visited_class_data.find(class_data) == visited_class_data.end()) { |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1553 | visited_class_data.insert(class_data); |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1554 | // Overwrite the existing vector with the new ordering, note that the sets of objects are |
| 1555 | // equivalent, but the order changes. This is why this is not a memory leak. |
| 1556 | // TODO: Consider cleaning this up with a shared_ptr. |
| 1557 | class_datas[class_data_index].release(); |
| 1558 | class_datas[class_data_index].reset(class_data); |
| 1559 | ++class_data_index; |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1560 | } |
| 1561 | } |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1562 | CHECK_EQ(class_data_index, class_datas.Size()); |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1563 | |
Mathieu Chartier | 2c4b084 | 2017-12-13 11:49:51 -0800 | [diff] [blame] | 1564 | if (DexLayout::kChangeClassDefOrder) { |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1565 | // This currently produces dex files that violate the spec since the super class class_def is |
| 1566 | // supposed to occur before any subclasses. |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1567 | dex_ir::CollectionVector<dex_ir::ClassDef>& class_defs = header_->ClassDefs(); |
| 1568 | CHECK_EQ(new_class_def_order.size(), class_defs.Size()); |
| 1569 | for (size_t i = 0; i < class_defs.Size(); ++i) { |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1570 | // Overwrite the existing vector with the new ordering, note that the sets of objects are |
| 1571 | // equivalent, but the order changes. This is why this is not a memory leak. |
| 1572 | // TODO: Consider cleaning this up with a shared_ptr. |
| 1573 | class_defs[i].release(); |
| 1574 | class_defs[i].reset(new_class_def_order[i]); |
| 1575 | } |
| 1576 | } |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1579 | void DexLayout::LayoutStringData(const DexFile* dex_file) { |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1580 | const size_t num_strings = header_->StringIds().Size(); |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1581 | std::vector<bool> is_shorty(num_strings, false); |
| 1582 | std::vector<bool> from_hot_method(num_strings, false); |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1583 | for (auto& class_def : header_->ClassDefs()) { |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1584 | // A name of a profile class is probably going to get looked up by ClassTable::Lookup, mark it |
Jeff Hao | acc83d7 | 2017-07-06 17:51:01 -0700 | [diff] [blame] | 1585 | // as hot. Add its super class and interfaces as well, which can be used during initialization. |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1586 | const bool is_profile_class = |
| 1587 | info_->ContainsClass(*dex_file, dex::TypeIndex(class_def->ClassType()->GetIndex())); |
| 1588 | if (is_profile_class) { |
| 1589 | from_hot_method[class_def->ClassType()->GetStringId()->GetIndex()] = true; |
Jeff Hao | acc83d7 | 2017-07-06 17:51:01 -0700 | [diff] [blame] | 1590 | const dex_ir::TypeId* superclass = class_def->Superclass(); |
| 1591 | if (superclass != nullptr) { |
| 1592 | from_hot_method[superclass->GetStringId()->GetIndex()] = true; |
| 1593 | } |
| 1594 | const dex_ir::TypeList* interfaces = class_def->Interfaces(); |
| 1595 | if (interfaces != nullptr) { |
| 1596 | for (const dex_ir::TypeId* interface_type : *interfaces->GetTypeList()) { |
| 1597 | from_hot_method[interface_type->GetStringId()->GetIndex()] = true; |
| 1598 | } |
| 1599 | } |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1600 | } |
| 1601 | dex_ir::ClassData* data = class_def->GetClassData(); |
| 1602 | if (data == nullptr) { |
| 1603 | continue; |
| 1604 | } |
| 1605 | for (size_t i = 0; i < 2; ++i) { |
| 1606 | for (auto& method : *(i == 0 ? data->DirectMethods() : data->VirtualMethods())) { |
David Sehr | d83437c | 2018-06-11 14:06:23 -0700 | [diff] [blame] | 1607 | const dex_ir::MethodId* method_id = method.GetMethodId(); |
| 1608 | dex_ir::CodeItem* code_item = method.GetCodeItem(); |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1609 | if (code_item == nullptr) { |
| 1610 | continue; |
| 1611 | } |
| 1612 | const bool is_clinit = is_profile_class && |
David Sehr | d83437c | 2018-06-11 14:06:23 -0700 | [diff] [blame] | 1613 | (method.GetAccessFlags() & kAccConstructor) != 0 && |
| 1614 | (method.GetAccessFlags() & kAccStatic) != 0; |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1615 | const bool method_executed = is_clinit || |
Mathieu Chartier | e46f3a8 | 2017-06-19 19:54:12 -0700 | [diff] [blame] | 1616 | info_->GetMethodHotness(MethodReference(dex_file, method_id->GetIndex())).IsInProfile(); |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1617 | if (!method_executed) { |
| 1618 | continue; |
| 1619 | } |
| 1620 | is_shorty[method_id->Proto()->Shorty()->GetIndex()] = true; |
| 1621 | dex_ir::CodeFixups* fixups = code_item->GetCodeFixups(); |
| 1622 | if (fixups == nullptr) { |
| 1623 | continue; |
| 1624 | } |
Jeff Hao | acc83d7 | 2017-07-06 17:51:01 -0700 | [diff] [blame] | 1625 | // Add const-strings. |
Vladimir Marko | 219cb90 | 2017-12-07 16:20:39 +0000 | [diff] [blame] | 1626 | for (dex_ir::StringId* id : fixups->StringIds()) { |
Jeff Hao | acc83d7 | 2017-07-06 17:51:01 -0700 | [diff] [blame] | 1627 | from_hot_method[id->GetIndex()] = true; |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1628 | } |
Jeff Hao | acc83d7 | 2017-07-06 17:51:01 -0700 | [diff] [blame] | 1629 | // Add field classes, names, and types. |
Vladimir Marko | 219cb90 | 2017-12-07 16:20:39 +0000 | [diff] [blame] | 1630 | for (dex_ir::FieldId* id : fixups->FieldIds()) { |
Jeff Hao | acc83d7 | 2017-07-06 17:51:01 -0700 | [diff] [blame] | 1631 | // TODO: Only visit field ids from static getters and setters. |
| 1632 | from_hot_method[id->Class()->GetStringId()->GetIndex()] = true; |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1633 | from_hot_method[id->Name()->GetIndex()] = true; |
| 1634 | from_hot_method[id->Type()->GetStringId()->GetIndex()] = true; |
| 1635 | } |
Jeff Hao | acc83d7 | 2017-07-06 17:51:01 -0700 | [diff] [blame] | 1636 | // For clinits, add referenced method classes, names, and protos. |
| 1637 | if (is_clinit) { |
Vladimir Marko | 219cb90 | 2017-12-07 16:20:39 +0000 | [diff] [blame] | 1638 | for (dex_ir::MethodId* id : fixups->MethodIds()) { |
Jeff Hao | acc83d7 | 2017-07-06 17:51:01 -0700 | [diff] [blame] | 1639 | from_hot_method[id->Class()->GetStringId()->GetIndex()] = true; |
| 1640 | from_hot_method[id->Name()->GetIndex()] = true; |
| 1641 | is_shorty[id->Proto()->Shorty()->GetIndex()] = true; |
| 1642 | } |
| 1643 | } |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1644 | } |
| 1645 | } |
| 1646 | } |
| 1647 | // Sort string data by specified order. |
| 1648 | std::vector<dex_ir::StringId*> string_ids; |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1649 | for (auto& string_id : header_->StringIds()) { |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1650 | string_ids.push_back(string_id.get()); |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1651 | } |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1652 | std::sort(string_ids.begin(), |
| 1653 | string_ids.end(), |
| 1654 | [&is_shorty, &from_hot_method](const dex_ir::StringId* a, |
| 1655 | const dex_ir::StringId* b) { |
| 1656 | const bool a_is_hot = from_hot_method[a->GetIndex()]; |
| 1657 | const bool b_is_hot = from_hot_method[b->GetIndex()]; |
| 1658 | if (a_is_hot != b_is_hot) { |
| 1659 | return a_is_hot < b_is_hot; |
| 1660 | } |
| 1661 | // After hot methods are partitioned, subpartition shorties. |
| 1662 | const bool a_is_shorty = is_shorty[a->GetIndex()]; |
| 1663 | const bool b_is_shorty = is_shorty[b->GetIndex()]; |
| 1664 | if (a_is_shorty != b_is_shorty) { |
| 1665 | return a_is_shorty < b_is_shorty; |
| 1666 | } |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1667 | // Order by index by default. |
| 1668 | return a->GetIndex() < b->GetIndex(); |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1669 | }); |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1670 | auto& string_datas = header_->StringDatas(); |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1671 | // Now we know what order we want the string data, reorder them. |
| 1672 | size_t data_index = 0; |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1673 | for (dex_ir::StringId* string_id : string_ids) { |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1674 | string_datas[data_index].release(); |
| 1675 | string_datas[data_index].reset(string_id->DataItem()); |
| 1676 | ++data_index; |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1677 | } |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1678 | if (kIsDebugBuild) { |
| 1679 | std::unordered_set<dex_ir::StringData*> visited; |
| 1680 | for (const std::unique_ptr<dex_ir::StringData>& data : string_datas) { |
| 1681 | visited.insert(data.get()); |
| 1682 | } |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1683 | for (auto& string_id : header_->StringIds()) { |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1684 | CHECK(visited.find(string_id->DataItem()) != visited.end()); |
| 1685 | } |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1686 | } |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1687 | CHECK_EQ(data_index, string_datas.Size()); |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1690 | // Orders code items according to specified class data ordering. |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1691 | void DexLayout::LayoutCodeItems(const DexFile* dex_file) { |
Shubham Ajmera | 36a282b | 2017-04-03 10:04:28 -0700 | [diff] [blame] | 1692 | static constexpr InvokeType invoke_types[] = { |
Mathieu Chartier | 7c1be8b | 2017-06-15 13:56:05 -0700 | [diff] [blame] | 1693 | kDirect, |
| 1694 | kVirtual |
Shubham Ajmera | 36a282b | 2017-04-03 10:04:28 -0700 | [diff] [blame] | 1695 | }; |
| 1696 | |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1697 | std::unordered_map<dex_ir::CodeItem*, LayoutType>& code_item_layout = |
| 1698 | layout_hotness_info_.code_item_layout_; |
| 1699 | |
| 1700 | // Assign hotness flags to all code items. |
Shubham Ajmera | 36a282b | 2017-04-03 10:04:28 -0700 | [diff] [blame] | 1701 | for (InvokeType invoke_type : invoke_types) { |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1702 | for (auto& class_def : header_->ClassDefs()) { |
Shubham Ajmera | 36a282b | 2017-04-03 10:04:28 -0700 | [diff] [blame] | 1703 | const bool is_profile_class = |
| 1704 | info_->ContainsClass(*dex_file, dex::TypeIndex(class_def->ClassType()->GetIndex())); |
| 1705 | |
| 1706 | // Skip classes that are not defined in this dex file. |
| 1707 | dex_ir::ClassData* class_data = class_def->GetClassData(); |
| 1708 | if (class_data == nullptr) { |
| 1709 | continue; |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1710 | } |
Shubham Ajmera | 36a282b | 2017-04-03 10:04:28 -0700 | [diff] [blame] | 1711 | for (auto& method : *(invoke_type == InvokeType::kDirect |
| 1712 | ? class_data->DirectMethods() |
| 1713 | : class_data->VirtualMethods())) { |
David Sehr | d83437c | 2018-06-11 14:06:23 -0700 | [diff] [blame] | 1714 | const dex_ir::MethodId *method_id = method.GetMethodId(); |
| 1715 | dex_ir::CodeItem *code_item = method.GetCodeItem(); |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1716 | if (code_item == nullptr) { |
Shubham Ajmera | 36a282b | 2017-04-03 10:04:28 -0700 | [diff] [blame] | 1717 | continue; |
| 1718 | } |
| 1719 | // Separate executed methods (clinits and profiled methods) from unexecuted methods. |
David Sehr | d83437c | 2018-06-11 14:06:23 -0700 | [diff] [blame] | 1720 | const bool is_clinit = (method.GetAccessFlags() & kAccConstructor) != 0 && |
| 1721 | (method.GetAccessFlags() & kAccStatic) != 0; |
Mathieu Chartier | 7c1be8b | 2017-06-15 13:56:05 -0700 | [diff] [blame] | 1722 | const bool is_startup_clinit = is_profile_class && is_clinit; |
| 1723 | using Hotness = ProfileCompilationInfo::MethodHotness; |
| 1724 | Hotness hotness = info_->GetMethodHotness(MethodReference(dex_file, method_id->GetIndex())); |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1725 | LayoutType state = LayoutType::kLayoutTypeUnused; |
Mathieu Chartier | 7c1be8b | 2017-06-15 13:56:05 -0700 | [diff] [blame] | 1726 | if (hotness.IsHot()) { |
| 1727 | // Hot code is compiled, maybe one day it won't be accessed. So lay it out together for |
| 1728 | // now. |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1729 | state = LayoutType::kLayoutTypeHot; |
Mathieu Chartier | 7c1be8b | 2017-06-15 13:56:05 -0700 | [diff] [blame] | 1730 | } else if (is_startup_clinit || hotness.GetFlags() == Hotness::kFlagStartup) { |
| 1731 | // Startup clinit or a method that only has the startup flag. |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1732 | state = LayoutType::kLayoutTypeStartupOnly; |
Mathieu Chartier | 7c1be8b | 2017-06-15 13:56:05 -0700 | [diff] [blame] | 1733 | } else if (is_clinit) { |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1734 | state = LayoutType::kLayoutTypeUsedOnce; |
Mathieu Chartier | e46f3a8 | 2017-06-19 19:54:12 -0700 | [diff] [blame] | 1735 | } else if (hotness.IsInProfile()) { |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1736 | state = LayoutType::kLayoutTypeSometimesUsed; |
Jeff Hao | 206cbaa | 2017-06-07 19:11:01 -0700 | [diff] [blame] | 1737 | } |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1738 | auto it = code_item_layout.emplace(code_item, state); |
| 1739 | if (!it.second) { |
| 1740 | LayoutType& layout_type = it.first->second; |
| 1741 | // Already exists, merge the hotness. |
| 1742 | layout_type = MergeLayoutType(layout_type, state); |
Shubham Ajmera | 36a282b | 2017-04-03 10:04:28 -0700 | [diff] [blame] | 1743 | } |
| 1744 | } |
| 1745 | } |
Shubham Ajmera | 36a282b | 2017-04-03 10:04:28 -0700 | [diff] [blame] | 1746 | } |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1747 | |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1748 | const auto& code_items = header_->CodeItems(); |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1749 | if (VLOG_IS_ON(dex)) { |
| 1750 | size_t layout_count[static_cast<size_t>(LayoutType::kLayoutTypeCount)] = {}; |
| 1751 | for (const std::unique_ptr<dex_ir::CodeItem>& code_item : code_items) { |
| 1752 | auto it = code_item_layout.find(code_item.get()); |
| 1753 | DCHECK(it != code_item_layout.end()); |
| 1754 | ++layout_count[static_cast<size_t>(it->second)]; |
| 1755 | } |
| 1756 | for (size_t i = 0; i < static_cast<size_t>(LayoutType::kLayoutTypeCount); ++i) { |
| 1757 | LOG(INFO) << "Code items in category " << i << " count=" << layout_count[i]; |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1758 | } |
| 1759 | } |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1760 | |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1761 | // Sort the code items vector by new layout. The writing process will take care of calculating |
| 1762 | // all the offsets. Stable sort to preserve any existing locality that might be there. |
| 1763 | std::stable_sort(code_items.begin(), |
| 1764 | code_items.end(), |
| 1765 | [&](const std::unique_ptr<dex_ir::CodeItem>& a, |
| 1766 | const std::unique_ptr<dex_ir::CodeItem>& b) { |
| 1767 | auto it_a = code_item_layout.find(a.get()); |
| 1768 | auto it_b = code_item_layout.find(b.get()); |
| 1769 | DCHECK(it_a != code_item_layout.end()); |
| 1770 | DCHECK(it_b != code_item_layout.end()); |
| 1771 | const LayoutType layout_type_a = it_a->second; |
| 1772 | const LayoutType layout_type_b = it_b->second; |
| 1773 | return layout_type_a < layout_type_b; |
| 1774 | }); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | void DexLayout::LayoutOutputFile(const DexFile* dex_file) { |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1778 | LayoutStringData(dex_file); |
| 1779 | LayoutClassDefsAndClassData(dex_file); |
| 1780 | LayoutCodeItems(dex_file); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 1783 | bool DexLayout::OutputDexFile(const DexFile* input_dex_file, |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1784 | bool compute_offsets, |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 1785 | std::unique_ptr<DexContainer>* dex_container, |
| 1786 | std::string* error_msg) { |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1787 | const std::string& dex_file_location = input_dex_file->GetLocation(); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1788 | std::unique_ptr<File> new_file; |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1789 | // If options_.output_dex_directory_ is non null, we are outputting to a file. |
| 1790 | if (options_.output_dex_directory_ != nullptr) { |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 1791 | std::string output_location(options_.output_dex_directory_); |
Mathieu Chartier | 4146840 | 2018-08-29 11:39:00 -0700 | [diff] [blame] | 1792 | const size_t last_slash = dex_file_location.rfind('/'); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1793 | std::string dex_file_directory = dex_file_location.substr(0, last_slash + 1); |
| 1794 | if (output_location == dex_file_directory) { |
| 1795 | output_location = dex_file_location + ".new"; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1796 | } else { |
Mathieu Chartier | 4146840 | 2018-08-29 11:39:00 -0700 | [diff] [blame] | 1797 | if (!output_location.empty() && output_location.back() != '/') { |
| 1798 | output_location += "/"; |
| 1799 | } |
| 1800 | const size_t separator = dex_file_location.rfind('!'); |
| 1801 | if (separator != std::string::npos) { |
| 1802 | output_location += dex_file_location.substr(separator + 1); |
| 1803 | } else { |
| 1804 | output_location += "classes.dex"; |
| 1805 | } |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1806 | } |
| 1807 | new_file.reset(OS::CreateEmptyFile(output_location.c_str())); |
Jeff Hao | 3ba51e8 | 2017-04-12 16:14:54 -0700 | [diff] [blame] | 1808 | if (new_file == nullptr) { |
| 1809 | LOG(ERROR) << "Could not create dex writer output file: " << output_location; |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 1810 | return false; |
Jeff Hao | 3ba51e8 | 2017-04-12 16:14:54 -0700 | [diff] [blame] | 1811 | } |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1812 | } |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 1813 | if (!DexWriter::Output(this, dex_container, compute_offsets, error_msg)) { |
| 1814 | return false; |
| 1815 | } |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1816 | if (new_file != nullptr) { |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1817 | DexContainer* const container = dex_container->get(); |
| 1818 | DexContainer::Section* const main_section = container->GetMainSection(); |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1819 | if (!new_file->WriteFully(main_section->Begin(), main_section->Size())) { |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1820 | LOG(ERROR) << "Failed to write main section for dex file " << dex_file_location; |
| 1821 | new_file->Erase(); |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 1822 | return false; |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1823 | } |
| 1824 | DexContainer::Section* const data_section = container->GetDataSection(); |
| 1825 | if (!new_file->WriteFully(data_section->Begin(), data_section->Size())) { |
| 1826 | LOG(ERROR) << "Failed to write data section for dex file " << dex_file_location; |
David Sehr | 7639cdc | 2017-04-15 10:06:21 -0700 | [diff] [blame] | 1827 | new_file->Erase(); |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 1828 | return false; |
David Sehr | 7639cdc | 2017-04-15 10:06:21 -0700 | [diff] [blame] | 1829 | } |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1830 | UNUSED(new_file->FlushCloseOrErase()); |
| 1831 | } |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 1832 | return true; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | /* |
| 1836 | * Dumps the requested sections of the file. |
| 1837 | */ |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 1838 | bool DexLayout::ProcessDexFile(const char* file_name, |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1839 | const DexFile* dex_file, |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1840 | size_t dex_file_index, |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 1841 | std::unique_ptr<DexContainer>* dex_container, |
| 1842 | std::string* error_msg) { |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1843 | const bool has_output_container = dex_container != nullptr; |
| 1844 | const bool output = options_.output_dex_directory_ != nullptr || has_output_container; |
| 1845 | |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 1846 | // Try to avoid eagerly assigning offsets to find bugs since Offset will abort if the offset |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1847 | // is unassigned. |
| 1848 | bool eagerly_assign_offsets = false; |
| 1849 | if (options_.visualize_pattern_ || options_.show_section_statistics_ || options_.dump_) { |
| 1850 | // These options required the offsets for dumping purposes. |
| 1851 | eagerly_assign_offsets = true; |
| 1852 | } |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 1853 | std::unique_ptr<dex_ir::Header> header(dex_ir::DexIrBuilder(*dex_file, |
| 1854 | eagerly_assign_offsets, |
| 1855 | GetOptions())); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1856 | SetHeader(header.get()); |
| 1857 | |
| 1858 | if (options_.verbose_) { |
| 1859 | fprintf(out_file_, "Opened '%s', DEX version '%.3s'\n", |
| 1860 | file_name, dex_file->GetHeader().magic_ + 4); |
| 1861 | } |
| 1862 | |
| 1863 | if (options_.visualize_pattern_) { |
| 1864 | VisualizeDexLayout(header_, dex_file, dex_file_index, info_); |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 1865 | return true; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1866 | } |
| 1867 | |
David Sehr | 9335749 | 2017-03-09 08:02:44 -0800 | [diff] [blame] | 1868 | if (options_.show_section_statistics_) { |
| 1869 | ShowDexSectionStatistics(header_, dex_file_index); |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 1870 | return true; |
David Sehr | 9335749 | 2017-03-09 08:02:44 -0800 | [diff] [blame] | 1871 | } |
| 1872 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1873 | // Dump dex file. |
| 1874 | if (options_.dump_) { |
| 1875 | DumpDexFile(); |
| 1876 | } |
| 1877 | |
Mathieu Chartier | 2ef3b88 | 2017-10-20 19:50:39 -0700 | [diff] [blame] | 1878 | // In case we are outputting to a file, keep it open so we can verify. |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1879 | if (output) { |
| 1880 | // Layout information about what strings and code items are hot. Used by the writing process |
| 1881 | // to generate the sections that are stored in the oat file. |
| 1882 | bool do_layout = info_ != nullptr; |
| 1883 | if (do_layout) { |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1884 | LayoutOutputFile(dex_file); |
| 1885 | } |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1886 | // The output needs a dex container, use a temporary one. |
| 1887 | std::unique_ptr<DexContainer> temp_container; |
| 1888 | if (dex_container == nullptr) { |
| 1889 | dex_container = &temp_container; |
| 1890 | } |
Mathieu Chartier | 21cf258 | 2018-01-08 17:09:48 -0800 | [diff] [blame] | 1891 | // If we didn't set the offsets eagerly, we definitely need to compute them here. |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 1892 | if (!OutputDexFile(dex_file, do_layout || !eagerly_assign_offsets, dex_container, error_msg)) { |
| 1893 | return false; |
| 1894 | } |
Mathieu Chartier | 2ef3b88 | 2017-10-20 19:50:39 -0700 | [diff] [blame] | 1895 | |
| 1896 | // Clear header before verifying to reduce peak RAM usage. |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1897 | const size_t file_size = header_->FileSize(); |
Mathieu Chartier | 2ef3b88 | 2017-10-20 19:50:39 -0700 | [diff] [blame] | 1898 | header.reset(); |
| 1899 | |
| 1900 | // Verify the output dex file's structure, only enabled by default for debug builds. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1901 | if (options_.verify_output_ && has_output_container) { |
Mathieu Chartier | 2ef3b88 | 2017-10-20 19:50:39 -0700 | [diff] [blame] | 1902 | std::string location = "memory mapped file for " + std::string(file_name); |
Mathieu Chartier | 8740c66 | 2018-01-11 14:50:02 -0800 | [diff] [blame] | 1903 | // Dex file verifier cannot handle compact dex. |
| 1904 | bool verify = options_.compact_dex_level_ == CompactDexLevel::kCompactDexLevelNone; |
Mathieu Chartier | 818cb80 | 2018-05-11 05:30:16 +0000 | [diff] [blame] | 1905 | const ArtDexFileLoader dex_file_loader; |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1906 | DexContainer::Section* const main_section = (*dex_container)->GetMainSection(); |
| 1907 | DexContainer::Section* const data_section = (*dex_container)->GetDataSection(); |
| 1908 | DCHECK_EQ(file_size, main_section->Size()) |
| 1909 | << main_section->Size() << " " << data_section->Size(); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 1910 | std::unique_ptr<const DexFile> output_dex_file( |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1911 | dex_file_loader.OpenWithDataSection( |
| 1912 | main_section->Begin(), |
| 1913 | main_section->Size(), |
| 1914 | data_section->Begin(), |
| 1915 | data_section->Size(), |
| 1916 | location, |
| 1917 | /* checksum */ 0, |
| 1918 | /*oat_dex_file*/ nullptr, |
| 1919 | verify, |
| 1920 | /*verify_checksum*/ false, |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 1921 | error_msg)); |
| 1922 | CHECK(output_dex_file != nullptr) << "Failed to re-open output file:" << *error_msg; |
Mathieu Chartier | 2ef3b88 | 2017-10-20 19:50:39 -0700 | [diff] [blame] | 1923 | |
| 1924 | // Do IR-level comparison between input and output. This check ignores potential differences |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1925 | // due to layout, so offsets are not checked. Instead, it checks the data contents of each |
| 1926 | // item. |
Mathieu Chartier | 2ef3b88 | 2017-10-20 19:50:39 -0700 | [diff] [blame] | 1927 | // |
| 1928 | // Regenerate output IR to catch any bugs that might happen during writing. |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1929 | std::unique_ptr<dex_ir::Header> output_header( |
| 1930 | dex_ir::DexIrBuilder(*output_dex_file, |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 1931 | /*eagerly_assign_offsets*/ true, |
| 1932 | GetOptions())); |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1933 | std::unique_ptr<dex_ir::Header> orig_header( |
| 1934 | dex_ir::DexIrBuilder(*dex_file, |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 1935 | /*eagerly_assign_offsets*/ true, |
| 1936 | GetOptions())); |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 1937 | CHECK(VerifyOutputDexFile(output_header.get(), orig_header.get(), error_msg)) << *error_msg; |
Mathieu Chartier | 2ef3b88 | 2017-10-20 19:50:39 -0700 | [diff] [blame] | 1938 | } |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 1939 | } |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 1940 | return true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | /* |
| 1944 | * Processes a single file (either direct .dex or indirect .zip/.jar/.apk). |
| 1945 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1946 | int DexLayout::ProcessFile(const char* file_name) { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1947 | if (options_.verbose_) { |
| 1948 | fprintf(out_file_, "Processing '%s'...\n", file_name); |
| 1949 | } |
| 1950 | |
| 1951 | // If the file is not a .dex file, the function tries .zip/.jar/.apk files, |
| 1952 | // all of which are Zip archives with "classes.dex" inside. |
| 1953 | const bool verify_checksum = !options_.ignore_bad_checksum_; |
| 1954 | std::string error_msg; |
Mathieu Chartier | 818cb80 | 2018-05-11 05:30:16 +0000 | [diff] [blame] | 1955 | const ArtDexFileLoader dex_file_loader; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1956 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Mathieu Chartier | 818cb80 | 2018-05-11 05:30:16 +0000 | [diff] [blame] | 1957 | if (!dex_file_loader.Open( |
| 1958 | file_name, file_name, /* verify */ true, verify_checksum, &error_msg, &dex_files)) { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1959 | // Display returned error message to user. Note that this error behavior |
| 1960 | // differs from the error messages shown by the original Dalvik dexdump. |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 1961 | LOG(ERROR) << error_msg; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1962 | return -1; |
| 1963 | } |
| 1964 | |
| 1965 | // Success. Either report checksum verification or process |
| 1966 | // all dex files found in given file. |
| 1967 | if (options_.checksum_only_) { |
| 1968 | fprintf(out_file_, "Checksum verified\n"); |
| 1969 | } else { |
| 1970 | for (size_t i = 0; i < dex_files.size(); i++) { |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1971 | // Pass in a null container to avoid output by default. |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 1972 | if (!ProcessDexFile(file_name, |
| 1973 | dex_files[i].get(), |
| 1974 | i, |
| 1975 | /*dex_container*/ nullptr, |
| 1976 | &error_msg)) { |
| 1977 | LOG(WARNING) << "Failed to run dex file " << i << " in " << file_name << " : " << error_msg; |
| 1978 | } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1979 | } |
| 1980 | } |
| 1981 | return 0; |
| 1982 | } |
| 1983 | |
| 1984 | } // namespace art |