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