1 /*
2  * Copyright (C) 2011 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 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <sys/stat.h>
20 
21 #include <algorithm>
22 #include <cstdlib>
23 #include <fstream>
24 #include <iomanip>
25 #include <iostream>
26 #include <map>
27 #include <optional>
28 #include <set>
29 #include <string>
30 #include <unordered_map>
31 #include <unordered_set>
32 #include <utility>
33 #include <vector>
34 
35 #include "android-base/logging.h"
36 #include "android-base/parseint.h"
37 #include "android-base/stringprintf.h"
38 #include "android-base/strings.h"
39 #include "arch/instruction_set.h"
40 #include "arch/instruction_set_features.h"
41 #include "art_field-inl.h"
42 #include "art_method-inl.h"
43 #include "base/array_ref.h"
44 #include "base/bit_utils_iterator.h"
45 #include "base/file_utils.h"
46 #include "base/indenter.h"
47 #include "base/os.h"
48 #include "base/safe_map.h"
49 #include "base/stats-inl.h"
50 #include "base/stl_util.h"
51 #include "base/unix_file/fd_file.h"
52 #include "class_linker-inl.h"
53 #include "class_linker.h"
54 #include "class_root-inl.h"
55 #include "cmdline.h"
56 #include "debug/debug_info.h"
57 #include "debug/elf_debug_writer.h"
58 #include "debug/method_debug_info.h"
59 #include "dex/art_dex_file_loader.h"
60 #include "dex/class_accessor-inl.h"
61 #include "dex/code_item_accessors-inl.h"
62 #include "dex/descriptors_names.h"
63 #include "dex/dex_file-inl.h"
64 #include "dex/dex_instruction-inl.h"
65 #include "dex/string_reference.h"
66 #include "dex/type_lookup_table.h"
67 #include "disassembler.h"
68 #include "elf/elf_builder.h"
69 #include "gc/accounting/space_bitmap-inl.h"
70 #include "gc/space/image_space.h"
71 #include "gc/space/large_object_space.h"
72 #include "gc/space/space-inl.h"
73 #include "imtable-inl.h"
74 #include "interpreter/unstarted_runtime.h"
75 #include "mirror/array-inl.h"
76 #include "mirror/class-inl.h"
77 #include "mirror/dex_cache-inl.h"
78 #include "mirror/object-inl.h"
79 #include "mirror/object_array-inl.h"
80 #include "oat/image-inl.h"
81 #include "oat/index_bss_mapping.h"
82 #include "oat/oat.h"
83 #include "oat/oat_file-inl.h"
84 #include "oat/oat_file_assistant.h"
85 #include "oat/oat_file_assistant_context.h"
86 #include "oat/oat_file_manager.h"
87 #include "oat/stack_map.h"
88 #include "scoped_thread_state_change-inl.h"
89 #include "stack.h"
90 #include "stream/buffered_output_stream.h"
91 #include "stream/file_output_stream.h"
92 #include "subtype_check.h"
93 #include "thread_list.h"
94 #include "vdex_file.h"
95 #include "verifier/method_verifier.h"
96 #include "verifier/verifier_deps.h"
97 #include "well_known_classes.h"
98 
99 namespace art {
100 
101 using android::base::StringPrintf;
102 
103 const char* image_methods_descriptions_[] = {
104   "kResolutionMethod",
105   "kImtConflictMethod",
106   "kImtUnimplementedMethod",
107   "kSaveAllCalleeSavesMethod",
108   "kSaveRefsOnlyMethod",
109   "kSaveRefsAndArgsMethod",
110   "kSaveEverythingMethod",
111   "kSaveEverythingMethodForClinit",
112   "kSaveEverythingMethodForSuspendCheck",
113 };
114 
115 const char* image_roots_descriptions_[] = {
116   "kDexCaches",
117   "kClassRoots",
118   "kSpecialRoots",
119 };
120 
121 // Map is so that we don't allocate multiple dex files for the same OatDexFile.
122 static std::map<const OatDexFile*, std::unique_ptr<const DexFile>> opened_dex_files;
123 
OpenDexFile(const OatDexFile * oat_dex_file,std::string * error_msg)124 const DexFile* OpenDexFile(const OatDexFile* oat_dex_file, std::string* error_msg) {
125   DCHECK(oat_dex_file != nullptr);
126   auto it = opened_dex_files.find(oat_dex_file);
127   if (it != opened_dex_files.end()) {
128     return it->second.get();
129   }
130   const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
131   opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
132   return ret;
133 }
134 
135 template <typename ElfTypes>
136 class OatSymbolizer final {
137  public:
OatSymbolizer(const OatFile * oat_file,const std::string & output_name,bool no_bits)138   OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
139       oat_file_(oat_file),
140       builder_(nullptr),
141       output_name_(output_name.empty() ? "symbolized.oat" : output_name),
142       no_bits_(no_bits) {
143   }
144 
Symbolize()145   bool Symbolize() {
146     const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
147     std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
148         isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
149 
150     std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
151     if (elf_file == nullptr) {
152       return false;
153     }
154     std::unique_ptr<BufferedOutputStream> output_stream =
155         std::make_unique<BufferedOutputStream>(
156             std::make_unique<FileOutputStream>(elf_file.get()));
157     builder_.reset(new ElfBuilder<ElfTypes>(isa, output_stream.get()));
158 
159     builder_->Start();
160 
161     auto* rodata = builder_->GetRoData();
162     auto* text = builder_->GetText();
163 
164     const uint8_t* rodata_begin = oat_file_->Begin();
165     const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
166     if (!no_bits_) {
167       rodata->Start();
168       rodata->WriteFully(rodata_begin, rodata_size);
169       rodata->End();
170     }
171 
172     const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
173     const size_t text_size = oat_file_->End() - text_begin;
174     if (!no_bits_) {
175       text->Start();
176       text->WriteFully(text_begin, text_size);
177       text->End();
178     }
179 
180     builder_->PrepareDynamicSection(elf_file->GetPath(),
181                                     rodata_size,
182                                     text_size,
183                                     oat_file_->DataImgRelRoSize(),
184                                     oat_file_->DataImgRelRoAppImageOffset(),
185                                     oat_file_->BssSize(),
186                                     oat_file_->BssMethodsOffset(),
187                                     oat_file_->BssRootsOffset(),
188                                     oat_file_->VdexSize());
189     builder_->WriteDynamicSection();
190 
191     const OatHeader& oat_header = oat_file_->GetOatHeader();
192     #define DO_TRAMPOLINE(fn_name)                                                            \
193       if (oat_header.Get ## fn_name ## Offset() != 0) {                                       \
194         debug::MethodDebugInfo info = {};                                                     \
195         info.custom_name = #fn_name;                                                          \
196         info.isa = oat_header.GetInstructionSet();                                            \
197         info.is_code_address_text_relative = true;                                            \
198         size_t code_offset = oat_header.Get ## fn_name ## Offset();                           \
199         code_offset -= GetInstructionSetEntryPointAdjustment(oat_header.GetInstructionSet()); \
200         info.code_address = code_offset - oat_header.GetExecutableOffset();                   \
201         info.code_size = 0;  /* The symbol lasts until the next symbol. */                    \
202         method_debug_infos_.push_back(std::move(info));                                       \
203       }
204     DO_TRAMPOLINE(JniDlsymLookupTrampoline);
205     DO_TRAMPOLINE(JniDlsymLookupCriticalTrampoline);
206     DO_TRAMPOLINE(QuickGenericJniTrampoline);
207     DO_TRAMPOLINE(QuickImtConflictTrampoline);
208     DO_TRAMPOLINE(QuickResolutionTrampoline);
209     DO_TRAMPOLINE(QuickToInterpreterBridge);
210     DO_TRAMPOLINE(NterpTrampoline);
211     #undef DO_TRAMPOLINE
212 
213     Walk();
214 
215     // TODO: Try to symbolize link-time thunks?
216     // This would require disassembling all methods to find branches outside the method code.
217 
218     // TODO: Add symbols for dex bytecode in the .dex section.
219 
220     debug::DebugInfo debug_info{};
221     debug_info.compiled_methods = ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_);
222 
223     debug::WriteDebugInfo(builder_.get(), debug_info);
224 
225     builder_->End();
226 
227     bool ret_value = builder_->Good();
228 
229     builder_.reset();
230     output_stream.reset();
231 
232     if (elf_file->FlushCloseOrErase() != 0) {
233       return false;
234     }
235     elf_file.reset();
236 
237     return ret_value;
238   }
239 
Walk()240   void Walk() {
241     std::vector<const OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
242     for (size_t i = 0; i < oat_dex_files.size(); i++) {
243       const OatDexFile* oat_dex_file = oat_dex_files[i];
244       CHECK(oat_dex_file != nullptr);
245       WalkOatDexFile(oat_dex_file);
246     }
247   }
248 
WalkOatDexFile(const OatDexFile * oat_dex_file)249   void WalkOatDexFile(const OatDexFile* oat_dex_file) {
250     std::string error_msg;
251     const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
252     if (dex_file == nullptr) {
253       return;
254     }
255     for (size_t class_def_index = 0;
256         class_def_index < dex_file->NumClassDefs();
257         class_def_index++) {
258       const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
259       OatClassType type = oat_class.GetType();
260       switch (type) {
261         case OatClassType::kAllCompiled:
262         case OatClassType::kSomeCompiled:
263           WalkOatClass(oat_class, *dex_file, class_def_index);
264           break;
265 
266         case OatClassType::kNoneCompiled:
267           // Ignore.
268           break;
269       }
270     }
271   }
272 
WalkOatClass(const OatFile::OatClass & oat_class,const DexFile & dex_file,uint32_t class_def_index)273   void WalkOatClass(const OatFile::OatClass& oat_class,
274                     const DexFile& dex_file,
275                     uint32_t class_def_index) {
276     ClassAccessor accessor(dex_file, class_def_index);
277     // Note: even if this is an interface or a native class, we still have to walk it, as there
278     //       might be a static initializer.
279     uint32_t class_method_idx = 0;
280     for (const ClassAccessor::Method& method : accessor.GetMethods()) {
281       WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
282                     dex_file,
283                     class_def_index,
284                     method.GetIndex(),
285                     method.GetCodeItem(),
286                     method.GetAccessFlags());
287     }
288   }
289 
WalkOatMethod(const OatFile::OatMethod & oat_method,const DexFile & dex_file,uint32_t class_def_index,uint32_t dex_method_index,const dex::CodeItem * code_item,uint32_t method_access_flags)290   void WalkOatMethod(const OatFile::OatMethod& oat_method,
291                      const DexFile& dex_file,
292                      uint32_t class_def_index,
293                      uint32_t dex_method_index,
294                      const dex::CodeItem* code_item,
295                      uint32_t method_access_flags) {
296     if ((method_access_flags & kAccAbstract) != 0) {
297       // Abstract method, no code.
298       return;
299     }
300     const OatHeader& oat_header = oat_file_->GetOatHeader();
301     const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
302     if (method_header == nullptr || method_header->GetCodeSize() == 0) {
303       // No code.
304       return;
305     }
306 
307     uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
308     // Clear Thumb2 bit.
309     const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
310 
311     debug::MethodDebugInfo info = {};
312     DCHECK(info.custom_name.empty());
313     info.dex_file = &dex_file;
314     info.class_def_index = class_def_index;
315     info.dex_method_index = dex_method_index;
316     info.access_flags = method_access_flags;
317     info.code_item = code_item;
318     info.isa = oat_header.GetInstructionSet();
319     info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
320     info.is_native_debuggable = oat_header.IsNativeDebuggable();
321     info.is_optimized = method_header->IsOptimized();
322     info.is_code_address_text_relative = true;
323     info.code_address = reinterpret_cast<uintptr_t>(code_address);
324     info.code_size = method_header->GetCodeSize();
325     info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
326     info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
327     info.cfi = ArrayRef<uint8_t>();
328     method_debug_infos_.push_back(info);
329   }
330 
331  private:
332   const OatFile* oat_file_;
333   std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
334   std::vector<debug::MethodDebugInfo> method_debug_infos_;
335   std::unordered_set<uint32_t> seen_offsets_;
336   const std::string output_name_;
337   bool no_bits_;
338 };
339 
340 class OatDumperOptions {
341  public:
OatDumperOptions(bool dump_vmap,bool dump_code_info_stack_maps,bool disassemble_code,bool absolute_addresses,const char * class_filter,const char * method_filter,bool list_classes,bool list_methods,bool dump_header_only,bool dump_method_and_offset_as_json,const char * export_dex_location,const char * app_image,const char * oat_filename,const char * dex_filename,uint32_t addr2instr)342   OatDumperOptions(bool dump_vmap,
343                    bool dump_code_info_stack_maps,
344                    bool disassemble_code,
345                    bool absolute_addresses,
346                    const char* class_filter,
347                    const char* method_filter,
348                    bool list_classes,
349                    bool list_methods,
350                    bool dump_header_only,
351                    bool dump_method_and_offset_as_json,
352                    const char* export_dex_location,
353                    const char* app_image,
354                    const char* oat_filename,
355                    const char* dex_filename,
356                    uint32_t addr2instr)
357       : dump_vmap_(dump_vmap),
358         dump_code_info_stack_maps_(dump_code_info_stack_maps),
359         disassemble_code_(disassemble_code),
360         absolute_addresses_(absolute_addresses),
361         class_filter_(class_filter),
362         method_filter_(method_filter),
363         list_classes_(list_classes),
364         list_methods_(list_methods),
365         dump_header_only_(dump_header_only),
366         dump_method_and_offset_as_json(dump_method_and_offset_as_json),
367         export_dex_location_(export_dex_location),
368         app_image_(app_image),
369         oat_filename_(oat_filename != nullptr ? std::make_optional(oat_filename) : std::nullopt),
370         dex_filename_(dex_filename != nullptr ? std::make_optional(dex_filename) : std::nullopt),
371         addr2instr_(addr2instr),
372         class_loader_(nullptr) {}
373 
374   const bool dump_vmap_;
375   const bool dump_code_info_stack_maps_;
376   const bool disassemble_code_;
377   const bool absolute_addresses_;
378   const char* const class_filter_;
379   const char* const method_filter_;
380   const bool list_classes_;
381   const bool list_methods_;
382   const bool dump_header_only_;
383   const bool dump_method_and_offset_as_json;
384   const char* const export_dex_location_;
385   const char* const app_image_;
386   const std::optional<std::string> oat_filename_;
387   const std::optional<std::string> dex_filename_;
388   uint32_t addr2instr_;
389   Handle<mirror::ClassLoader>* class_loader_;
390 };
391 
392 class OatDumper {
393  public:
OatDumper(const OatFile & oat_file,const OatDumperOptions & options)394   OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
395       : oat_file_(oat_file),
396         oat_dex_files_(oat_file.GetOatDexFiles()),
397         options_(options),
398         resolved_addr2instr_(0),
399         instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
400         disassembler_(Disassembler::Create(
401             instruction_set_,
402             new DisassemblerOptions(options_.absolute_addresses_,
403                                     oat_file.Begin(),
404                                     oat_file.End(),
405                                     /* can_read_literals_= */ true,
406                                     Is64BitInstructionSet(instruction_set_) ?
407                                         &Thread::DumpThreadOffset<PointerSize::k64> :
408                                         &Thread::DumpThreadOffset<PointerSize::k32>))) {
409     CHECK(options_.class_loader_ != nullptr);
410     CHECK(options_.class_filter_ != nullptr);
411     CHECK(options_.method_filter_ != nullptr);
412     AddAllOffsets();
413   }
414 
~OatDumper()415   ~OatDumper() {
416     delete disassembler_;
417   }
418 
GetInstructionSet()419   InstructionSet GetInstructionSet() {
420     return instruction_set_;
421   }
422 
423   using DexFileUniqV = std::vector<std::unique_ptr<const DexFile>>;
424 
Dump(std::ostream & os)425   bool Dump(std::ostream& os) {
426     if (options_.dump_method_and_offset_as_json) {
427       return DumpMethodAndOffsetAsJson(os);
428     }
429 
430     bool success = true;
431     const OatHeader& oat_header = oat_file_.GetOatHeader();
432 
433     os << "MAGIC:\n";
434     os << oat_header.GetMagic() << "\n\n";
435 
436     os << "LOCATION:\n";
437     os << oat_file_.GetLocation() << "\n\n";
438 
439     os << "CHECKSUM:\n";
440     os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
441 
442     os << "INSTRUCTION SET:\n";
443     os << oat_header.GetInstructionSet() << "\n\n";
444 
445     {
446       std::unique_ptr<const InstructionSetFeatures> features(
447           InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
448                                              oat_header.GetInstructionSetFeaturesBitmap()));
449       os << "INSTRUCTION SET FEATURES:\n";
450       os << features->GetFeatureString() << "\n\n";
451     }
452 
453     os << "DEX FILE COUNT:\n";
454     os << oat_header.GetDexFileCount() << "\n\n";
455 
456 #define DUMP_OAT_HEADER_OFFSET(label, offset) \
457     os << label " OFFSET:\n"; \
458     os << StringPrintf("0x%08x", oat_header.offset()); \
459     if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
460       os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
461     } \
462     os << StringPrintf("\n\n");
463 
464     DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
465     DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP TRAMPOLINE",
466                            GetJniDlsymLookupTrampolineOffset);
467     DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP CRITICAL TRAMPOLINE",
468                            GetJniDlsymLookupCriticalTrampolineOffset);
469     DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
470                            GetQuickGenericJniTrampolineOffset);
471     DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
472                            GetQuickImtConflictTrampolineOffset);
473     DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
474                            GetQuickResolutionTrampolineOffset);
475     DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
476                            GetQuickToInterpreterBridgeOffset);
477     DUMP_OAT_HEADER_OFFSET("NTERP_TRAMPOLINE",
478                            GetNterpTrampolineOffset);
479 #undef DUMP_OAT_HEADER_OFFSET
480 
481     // Print the key-value store.
482     {
483       os << "KEY VALUE STORE:\n";
484       size_t index = 0;
485       const char* key;
486       const char* value;
487       while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
488         os << key << " = " << value << "\n";
489         index++;
490       }
491       os << "\n";
492     }
493 
494     if (options_.absolute_addresses_) {
495       os << "BEGIN:\n";
496       os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
497 
498       os << "END:\n";
499       os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
500     }
501 
502     os << "SIZE:\n";
503     os << oat_file_.Size() << "\n\n";
504 
505     os << std::flush;
506 
507     // If set, adjust relative address to be searched
508     if (options_.addr2instr_ != 0) {
509       resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
510       os << "SEARCH ADDRESS (executable offset + input):\n";
511       os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
512     }
513 
514     // Dump .data.img.rel.ro entries.
515     DumpDataImgRelRoEntries(os);
516 
517     // Dump .bss summary, individual entries are dumped per dex file.
518     os << ".bss: ";
519     if (oat_file_.GetBssMethods().empty() && oat_file_.GetBssGcRoots().empty()) {
520       os << "empty.\n\n";
521     } else {
522       os << oat_file_.GetBssMethods().size() << " methods, ";
523       os << oat_file_.GetBssGcRoots().size() << " GC roots.\n\n";
524     }
525 
526     // Dumping the dex file overview is compact enough to do even if header only.
527     for (size_t i = 0; i < oat_dex_files_.size(); i++) {
528       const OatDexFile* oat_dex_file = oat_dex_files_[i];
529       CHECK(oat_dex_file != nullptr);
530       std::string error_msg;
531       const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
532       if (dex_file == nullptr) {
533         os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
534            << error_msg;
535         continue;
536       }
537 
538       const DexLayoutSections* const layout_sections = oat_dex_file->GetDexLayoutSections();
539       if (layout_sections != nullptr) {
540         os << "Layout data\n";
541         os << *layout_sections;
542         os << "\n";
543       }
544 
545       if (!options_.dump_header_only_) {
546         DumpBssMappings(os,
547                         dex_file,
548                         oat_dex_file->GetMethodBssMapping(),
549                         oat_dex_file->GetTypeBssMapping(),
550                         oat_dex_file->GetPublicTypeBssMapping(),
551                         oat_dex_file->GetPackageTypeBssMapping(),
552                         oat_dex_file->GetStringBssMapping(),
553                         oat_dex_file->GetMethodTypeBssMapping());
554       }
555     }
556 
557     if (!options_.dump_header_only_) {
558       Runtime* const runtime = Runtime::Current();
559       ClassLinker* const linker = runtime != nullptr ? runtime->GetClassLinker() : nullptr;
560 
561       if (linker != nullptr) {
562         ArrayRef<const DexFile* const> bcp_dex_files(linker->GetBootClassPath());
563         // The guarantee that we have is that we can safely take a look the BCP DexFiles in
564         // [0..number_of_compiled_bcp_dexfiles) since the runtime may add more DexFiles after that.
565         // As a note, in the case of not having mappings or in the case of multi image we
566         // purposively leave `oat_file_.bcp_bss_info` empty.
567         CHECK_LE(oat_file_.bcp_bss_info_.size(), bcp_dex_files.size());
568         for (size_t i = 0; i < oat_file_.bcp_bss_info_.size(); i++) {
569           const DexFile* const dex_file = bcp_dex_files[i];
570           os << "Entries for BCP DexFile: " << dex_file->GetLocation() << "\n";
571           DumpBssMappings(os,
572                           dex_file,
573                           oat_file_.bcp_bss_info_[i].method_bss_mapping,
574                           oat_file_.bcp_bss_info_[i].type_bss_mapping,
575                           oat_file_.bcp_bss_info_[i].public_type_bss_mapping,
576                           oat_file_.bcp_bss_info_[i].package_type_bss_mapping,
577                           oat_file_.bcp_bss_info_[i].string_bss_mapping,
578                           oat_file_.bcp_bss_info_[i].method_type_bss_mapping);
579         }
580       } else {
581         // We don't have a runtime, just dump the offsets
582         for (size_t i = 0; i < oat_file_.bcp_bss_info_.size(); i++) {
583           os << "Offsets for BCP DexFile at index " << i << "\n";
584           DumpBssOffsets(os, "ArtMethod", oat_file_.bcp_bss_info_[i].method_bss_mapping);
585           DumpBssOffsets(os, "Class", oat_file_.bcp_bss_info_[i].type_bss_mapping);
586           DumpBssOffsets(os, "Public Class", oat_file_.bcp_bss_info_[i].public_type_bss_mapping);
587           DumpBssOffsets(os, "Package Class", oat_file_.bcp_bss_info_[i].package_type_bss_mapping);
588           DumpBssOffsets(os, "String", oat_file_.bcp_bss_info_[i].string_bss_mapping);
589           DumpBssOffsets(os, "MethodType", oat_file_.bcp_bss_info_[i].method_type_bss_mapping);
590         }
591       }
592     }
593 
594     if (!options_.dump_header_only_) {
595       VariableIndentationOutputStream vios(&os);
596       VdexFile::VdexFileHeader vdex_header = oat_file_.GetVdexFile()->GetVdexFileHeader();
597       if (vdex_header.IsValid()) {
598         std::string error_msg;
599         std::vector<const DexFile*> dex_files;
600         for (size_t i = 0; i < oat_dex_files_.size(); i++) {
601           const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
602           if (dex_file == nullptr) {
603             os << "Error opening dex file: " << error_msg << std::endl;
604             return false;
605           }
606           dex_files.push_back(dex_file);
607         }
608         verifier::VerifierDeps deps(dex_files, /*output_only=*/ false);
609         if (!deps.ParseStoredData(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData())) {
610           os << "Error parsing verifier dependencies." << std::endl;
611           return false;
612         }
613         deps.Dump(&vios);
614       } else {
615         os << "UNRECOGNIZED vdex file, magic "
616            << vdex_header.GetMagic()
617            << ", version "
618            << vdex_header.GetVdexVersion()
619            << "\n";
620       }
621       for (size_t i = 0; i < oat_dex_files_.size(); i++) {
622         const OatDexFile* oat_dex_file = oat_dex_files_[i];
623         CHECK(oat_dex_file != nullptr);
624         if (!DumpOatDexFile(os, *oat_dex_file)) {
625           success = false;
626         }
627       }
628     }
629 
630     if (options_.export_dex_location_) {
631       std::string error_msg;
632       std::string vdex_filename = GetVdexFilename(oat_file_.GetLocation());
633       if (!OS::FileExists(vdex_filename.c_str())) {
634         os << "File " << vdex_filename.c_str() << " does not exist\n";
635         return false;
636       }
637 
638       DexFileUniqV vdex_dex_files;
639       std::unique_ptr<const VdexFile> vdex_file = OpenVdex(vdex_filename,
640                                                            &vdex_dex_files,
641                                                            &error_msg);
642       if (vdex_file.get() == nullptr) {
643         os << "Failed to open vdex file: " << error_msg << "\n";
644         return false;
645       }
646       if (oat_dex_files_.size() != vdex_dex_files.size()) {
647         os << "Dex files number in Vdex file does not match Dex files number in Oat file: "
648            << vdex_dex_files.size() << " vs " << oat_dex_files_.size() << '\n';
649         return false;
650       }
651 
652       size_t i = 0;
653       for (const auto& vdex_dex_file : vdex_dex_files) {
654         const OatDexFile* oat_dex_file = oat_dex_files_[i];
655         CHECK(oat_dex_file != nullptr);
656         CHECK(vdex_dex_file != nullptr);
657 
658         if (!vdex_dex_file->IsDexContainerFirstEntry()) {
659           // All the data was already exported together with the primary dex file.
660           continue;
661         }
662 
663         if (!ExportDexFile(os, *oat_dex_file, vdex_dex_file.get(), /*used_dexlayout=*/ false)) {
664           success = false;
665           break;
666         }
667         i++;
668       }
669     }
670 
671     {
672       os << "OAT FILE STATS:\n";
673       VariableIndentationOutputStream vios(&os);
674       stats_.AddBytes(oat_file_.Size());
675       stats_.DumpSizes(vios, "OatFile");
676     }
677 
678     os << std::flush;
679     return success;
680   }
681 
DumpMethodAndOffsetAsJson(std::ostream & os)682   bool DumpMethodAndOffsetAsJson(std::ostream& os) {
683     for (const OatDexFile* oat_dex_file : oat_dex_files_) {
684       CHECK(oat_dex_file != nullptr);
685       // Create the dex file early. A lot of print-out things depend on it.
686       std::string error_msg;
687       const DexFile* const dex_file = art::OpenDexFile(oat_dex_file, &error_msg);
688       if (dex_file == nullptr) {
689         LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
690                      << "': " << error_msg;
691         return false;
692       }
693       for (ClassAccessor accessor : dex_file->GetClasses()) {
694         const char* descriptor = accessor.GetDescriptor();
695         if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
696           continue;
697         }
698 
699         const uint16_t class_def_index = accessor.GetClassDefIndex();
700         const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
701         uint32_t class_method_index = 0;
702 
703         // inspired by DumpOatMethod
704         for (const ClassAccessor::Method& method : accessor.GetMethods()) {
705           uint32_t code_offset = oat_class.GetOatMethod(class_method_index).GetCodeOffset();
706           class_method_index++;
707 
708           uint32_t dex_method_idx = method.GetIndex();
709           std::string method_name = dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
710           if (method_name.find(options_.method_filter_) == std::string::npos) {
711             continue;
712           }
713 
714           std::string pretty_method = dex_file->PrettyMethod(dex_method_idx, true);
715 
716           os << StringPrintf(
717               "{\"method\":\"%s\",\"offset\":\"0x%08x\"}\n", pretty_method.c_str(), code_offset);
718         }
719       }
720     }
721     return true;
722   }
723 
ComputeSize(const void * oat_data)724   size_t ComputeSize(const void* oat_data) {
725     if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
726         reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
727       return 0;  // Address not in oat file
728     }
729     uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
730                              reinterpret_cast<uintptr_t>(oat_file_.Begin());
731     auto it = offsets_.upper_bound(begin_offset);
732     CHECK(it != offsets_.end());
733     uintptr_t end_offset = *it;
734     return end_offset - begin_offset;
735   }
736 
GetOatInstructionSet()737   InstructionSet GetOatInstructionSet() {
738     return oat_file_.GetOatHeader().GetInstructionSet();
739   }
740 
GetQuickOatCode(ArtMethod * m)741   const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
742     for (size_t i = 0; i < oat_dex_files_.size(); i++) {
743       const OatDexFile* oat_dex_file = oat_dex_files_[i];
744       CHECK(oat_dex_file != nullptr);
745       std::string error_msg;
746       const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
747       if (dex_file == nullptr) {
748         LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
749             << "': " << error_msg;
750       } else {
751         const char* descriptor = m->GetDeclaringClassDescriptor();
752         const dex::ClassDef* class_def =
753             OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
754         if (class_def != nullptr) {
755           uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
756           const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
757           uint32_t oat_method_index;
758           if (m->IsStatic() || m->IsDirect()) {
759             // Simple case where the oat method index was stashed at load time.
760             oat_method_index = m->GetMethodIndex();
761           } else {
762             // Compute the oat_method_index by search for its position in the class def.
763             ClassAccessor accessor(*dex_file, *class_def);
764             oat_method_index = accessor.NumDirectMethods();
765             bool found_virtual = false;
766             for (ClassAccessor::Method dex_method : accessor.GetVirtualMethods()) {
767               // Check method index instead of identity in case of duplicate method definitions.
768               if (dex_method.GetIndex() == m->GetDexMethodIndex()) {
769                 found_virtual = true;
770                 break;
771               }
772               ++oat_method_index;
773             }
774             CHECK(found_virtual) << "Didn't find oat method index for virtual method: "
775                                  << dex_file->PrettyMethod(m->GetDexMethodIndex());
776           }
777           return oat_class.GetOatMethod(oat_method_index).GetQuickCode();
778         }
779       }
780     }
781     return nullptr;
782   }
783 
784   // Returns nullptr and updates error_msg if the Vdex file cannot be opened, otherwise all Dex
785   // files are stored in dex_files.
OpenVdex(const std::string & vdex_filename,DexFileUniqV * dex_files,std::string * error_msg)786   std::unique_ptr<const VdexFile> OpenVdex(const std::string& vdex_filename,
787                                            /* out */ DexFileUniqV* dex_files,
788                                            /* out */ std::string* error_msg) {
789     std::unique_ptr<const File> file(OS::OpenFileForReading(vdex_filename.c_str()));
790     if (file == nullptr) {
791       *error_msg = "Could not open file " + vdex_filename + " for reading.";
792       return nullptr;
793     }
794 
795     int64_t vdex_length = file->GetLength();
796     if (vdex_length == -1) {
797       *error_msg = "Could not read the length of file " + vdex_filename;
798       return nullptr;
799     }
800 
801     MemMap mmap = MemMap::MapFile(
802         file->GetLength(),
803         PROT_READ | PROT_WRITE,
804         MAP_PRIVATE,
805         file->Fd(),
806         /* start offset= */ 0,
807         /* low_4gb= */ false,
808         vdex_filename.c_str(),
809         error_msg);
810     if (!mmap.IsValid()) {
811       *error_msg = "Failed to mmap file " + vdex_filename + ": " + *error_msg;
812       return nullptr;
813     }
814 
815     std::unique_ptr<VdexFile> vdex_file(new VdexFile(std::move(mmap)));
816     if (!vdex_file->IsValid()) {
817       *error_msg = "Vdex file is not valid";
818       return nullptr;
819     }
820 
821     DexFileUniqV tmp_dex_files;
822     if (!vdex_file->OpenAllDexFiles(&tmp_dex_files, error_msg)) {
823       *error_msg = "Failed to open Dex files from Vdex: " + *error_msg;
824       return nullptr;
825     }
826 
827     *dex_files = std::move(tmp_dex_files);
828     return vdex_file;
829   }
830 
AddStatsObject(const void * address)831   bool AddStatsObject(const void* address) {
832     return seen_stats_objects_.insert(address).second;  // Inserted new entry.
833   }
834 
835  private:
AddAllOffsets()836   void AddAllOffsets() {
837     // We don't know the length of the code for each method, but we need to know where to stop
838     // when disassembling. What we do know is that a region of code will be followed by some other
839     // region, so if we keep a sorted sequence of the start of each region, we can infer the length
840     // of a piece of code by using upper_bound to find the start of the next region.
841     for (size_t i = 0; i < oat_dex_files_.size(); i++) {
842       const OatDexFile* oat_dex_file = oat_dex_files_[i];
843       CHECK(oat_dex_file != nullptr);
844       std::string error_msg;
845       const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
846       if (dex_file == nullptr) {
847         LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
848             << "': " << error_msg;
849         continue;
850       }
851       offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
852       for (ClassAccessor accessor : dex_file->GetClasses()) {
853         const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(accessor.GetClassDefIndex());
854         for (uint32_t class_method_index = 0;
855             class_method_index < accessor.NumMethods();
856             ++class_method_index) {
857           AddOffsets(oat_class.GetOatMethod(class_method_index));
858         }
859       }
860     }
861 
862     // If the last thing in the file is code for a method, there won't be an offset for the "next"
863     // thing. Instead of having a special case in the upper_bound code, let's just add an entry
864     // for the end of the file.
865     offsets_.insert(oat_file_.Size());
866   }
867 
AlignCodeOffset(uint32_t maybe_thumb_offset)868   static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
869     return maybe_thumb_offset & ~0x1;  // TODO: Make this Thumb2 specific.
870   }
871 
AddOffsets(const OatFile::OatMethod & oat_method)872   void AddOffsets(const OatFile::OatMethod& oat_method) {
873     uint32_t code_offset = oat_method.GetCodeOffset();
874     if (oat_file_.GetOatHeader().GetInstructionSet() == InstructionSet::kThumb2) {
875       code_offset &= ~0x1;
876     }
877     offsets_.insert(code_offset);
878     offsets_.insert(oat_method.GetVmapTableOffset());
879   }
880 
DumpOatDexFile(std::ostream & os,const OatDexFile & oat_dex_file)881   bool DumpOatDexFile(std::ostream& os, const OatDexFile& oat_dex_file) {
882     bool success = true;
883     bool stop_analysis = false;
884     os << "OatDexFile:\n";
885     os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
886     os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
887 
888     if (oat_dex_file.GetOatFile()->ContainsDexCode()) {
889       const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
890 
891       // Print data range of the dex file embedded inside the corresponding vdex file.
892       const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
893       uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
894       os << StringPrintf(
895           "dex-file: 0x%08x..0x%08x\n",
896           dex_offset,
897           dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
898     } else {
899       os << StringPrintf("dex-file not in VDEX file\n");
900     }
901 
902     // Create the dex file early. A lot of print-out things depend on it.
903     std::string error_msg;
904     const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
905     if (dex_file == nullptr) {
906       os << "NOT FOUND: " << error_msg << "\n\n";
907       os << std::flush;
908       return false;
909     }
910 
911     // Print lookup table, if it exists.
912     if (oat_dex_file.GetLookupTableData() != nullptr) {
913       uint32_t table_offset = dchecked_integral_cast<uint32_t>(
914           oat_dex_file.GetLookupTableData() - oat_dex_file.GetOatFile()->DexBegin());
915       uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
916       os << StringPrintf("type-table: 0x%08x..0x%08x\n",
917                          table_offset,
918                          table_offset + table_size - 1);
919       const TypeLookupTable& lookup = oat_dex_file.GetTypeLookupTable();
920       lookup.Dump(os);
921     }
922 
923     VariableIndentationOutputStream vios(&os);
924     ScopedIndentation indent1(&vios);
925     for (ClassAccessor accessor : dex_file->GetClasses()) {
926       // TODO: Support regex
927       const char* descriptor = accessor.GetDescriptor();
928       if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
929         continue;
930       }
931 
932       const uint16_t class_def_index = accessor.GetClassDefIndex();
933       uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
934       const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
935       os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
936                          static_cast<ssize_t>(class_def_index),
937                          descriptor,
938                          oat_class_offset,
939                          accessor.GetClassIdx().index_)
940          << " (" << oat_class.GetStatus() << ")"
941          << " (" << oat_class.GetType() << ")\n";
942       // TODO: include bitmap here if type is kOatClassSomeCompiled?
943       if (options_.list_classes_) {
944         continue;
945       }
946       if (!DumpOatClass(&vios, oat_class, *dex_file, accessor, &stop_analysis)) {
947         success = false;
948       }
949       if (stop_analysis) {
950         os << std::flush;
951         return success;
952       }
953     }
954     os << "\n";
955     os << std::flush;
956     return success;
957   }
958 
959   // Backwards compatible Dex file export. If dex_file is nullptr (valid Vdex file not present) the
960   // Dex resource is extracted from the oat_dex_file and its checksum is repaired since it's not
961   // unquickened. Otherwise the dex_file has been fully unquickened and is expected to verify the
962   // original checksum.
ExportDexFile(std::ostream & os,const OatDexFile & oat_dex_file,const DexFile * dex_file,bool used_dexlayout)963   bool ExportDexFile(std::ostream& os,
964                      const OatDexFile& oat_dex_file,
965                      const DexFile* dex_file,
966                      bool used_dexlayout) {
967     std::string error_msg;
968     std::string dex_file_location = oat_dex_file.GetDexFileLocation();
969 
970     // If dex_file (from unquicken or dexlayout) is not available, the output DexFile size is the
971     // same as the one extracted from the Oat container (pre-oreo)
972     size_t fsize = dex_file == nullptr ? oat_dex_file.FileSize() : dex_file->Size();
973 
974     // Some quick checks just in case
975     if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
976       os << "Invalid dex file\n";
977       return false;
978     }
979 
980     if (dex_file == nullptr) {
981       // Exported bytecode is quickened (dex-to-dex transformations present)
982       dex_file = OpenDexFile(&oat_dex_file, &error_msg);
983       if (dex_file == nullptr) {
984         os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
985         return false;
986       }
987 
988       // Recompute checksum
989       reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
990           dex_file->CalculateChecksum();
991     } else {
992       // If dexlayout was used to convert CompactDex back to StandardDex, checksum will be updated
993       // due to `update_checksum_` option, otherwise we expect a reproducible checksum.
994       if (!used_dexlayout) {
995         // Vdex unquicken output should match original input bytecode
996         uint32_t orig_checksum =
997             reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_;
998         if (orig_checksum != dex_file->CalculateChecksum()) {
999           os << "Unexpected checksum from unquicken dex file '" << dex_file_location << "'\n";
1000           return false;
1001         }
1002       }
1003       // Extend the data range to export all the dex files in the container.
1004       CHECK(dex_file->IsDexContainerFirstEntry()) << dex_file_location;
1005       fsize = dex_file->GetHeader().ContainerSize();
1006     }
1007 
1008     // Verify output directory exists
1009     if (!OS::DirectoryExists(options_.export_dex_location_)) {
1010       // TODO: Extend OS::DirectoryExists if symlink support is required
1011       os << options_.export_dex_location_ << " output directory not found or symlink\n";
1012       return false;
1013     }
1014 
1015     // Beautify path names
1016     if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1017       return false;
1018     }
1019 
1020     std::string dex_orig_name;
1021     size_t dex_orig_pos = dex_file_location.rfind('/');
1022     if (dex_orig_pos == std::string::npos)
1023       dex_orig_name = dex_file_location;
1024     else
1025       dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1026 
1027     // A more elegant approach to efficiently name user installed apps is welcome
1028     if (dex_orig_name.size() == 8 &&
1029         dex_orig_name.compare("base.apk") == 0 &&
1030         dex_orig_pos != std::string::npos) {
1031       dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1032       size_t apk_orig_pos = dex_file_location.rfind('/');
1033       if (apk_orig_pos != std::string::npos) {
1034         dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1035       }
1036     }
1037 
1038     std::string out_dex_path(options_.export_dex_location_);
1039     if (out_dex_path.back() != '/') {
1040       out_dex_path.append("/");
1041     }
1042     out_dex_path.append(dex_orig_name);
1043     out_dex_path.append("_export.dex");
1044     if (out_dex_path.length() > PATH_MAX) {
1045       return false;
1046     }
1047 
1048     std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1049     if (file.get() == nullptr) {
1050       os << "Failed to open output dex file " << out_dex_path;
1051       return false;
1052     }
1053 
1054     bool success = file->WriteFully(dex_file->Begin(), fsize);
1055     if (!success) {
1056       os << "Failed to write dex file";
1057       file->Erase();
1058       return false;
1059     }
1060 
1061     if (file->FlushCloseOrErase() != 0) {
1062       os << "Flush and close failed";
1063       return false;
1064     }
1065 
1066     os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1067     os << std::flush;
1068 
1069     return true;
1070   }
1071 
DumpOatClass(VariableIndentationOutputStream * vios,const OatFile::OatClass & oat_class,const DexFile & dex_file,const ClassAccessor & class_accessor,bool * stop_analysis)1072   bool DumpOatClass(VariableIndentationOutputStream* vios,
1073                     const OatFile::OatClass& oat_class,
1074                     const DexFile& dex_file,
1075                     const ClassAccessor& class_accessor,
1076                     bool* stop_analysis) {
1077     bool success = true;
1078     bool addr_found = false;
1079     uint32_t class_method_index = 0;
1080     for (const ClassAccessor::Method& method : class_accessor.GetMethods()) {
1081       if (!DumpOatMethod(vios,
1082                          dex_file.GetClassDef(class_accessor.GetClassDefIndex()),
1083                          class_method_index,
1084                          oat_class,
1085                          dex_file,
1086                          method.GetIndex(),
1087                          method.GetCodeItem(),
1088                          method.GetAccessFlags(),
1089                          &addr_found)) {
1090         success = false;
1091       }
1092       if (addr_found) {
1093         *stop_analysis = true;
1094         return success;
1095       }
1096       class_method_index++;
1097     }
1098     vios->Stream() << std::flush;
1099     return success;
1100   }
1101 
1102   static constexpr uint32_t kPrologueBytes = 16;
1103 
1104   // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1105   static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1106 
DumpOatMethod(VariableIndentationOutputStream * vios,const dex::ClassDef & class_def,uint32_t class_method_index,const OatFile::OatClass & oat_class,const DexFile & dex_file,uint32_t dex_method_idx,const dex::CodeItem * code_item,uint32_t method_access_flags,bool * addr_found)1107   bool DumpOatMethod(VariableIndentationOutputStream* vios,
1108                      const dex::ClassDef& class_def,
1109                      uint32_t class_method_index,
1110                      const OatFile::OatClass& oat_class,
1111                      const DexFile& dex_file,
1112                      uint32_t dex_method_idx,
1113                      const dex::CodeItem* code_item,
1114                      uint32_t method_access_flags,
1115                      bool* addr_found) {
1116     bool success = true;
1117 
1118     CodeItemDataAccessor code_item_accessor(dex_file, code_item);
1119 
1120     // TODO: Support regex
1121     std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1122     if (method_name.find(options_.method_filter_) == std::string::npos) {
1123       return success;
1124     }
1125 
1126     std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
1127     vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1128                                    class_method_index, pretty_method.c_str(),
1129                                    dex_method_idx);
1130     if (options_.list_methods_) {
1131       return success;
1132     }
1133 
1134     uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1135     const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1136     const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1137     uint32_t code_offset = oat_method.GetCodeOffset();
1138     uint32_t code_size = oat_method.GetQuickCodeSize();
1139     if (resolved_addr2instr_ != 0) {
1140       if (resolved_addr2instr_ > code_offset + code_size) {
1141         return success;
1142       } else {
1143         *addr_found = true;  // stop analyzing file at next iteration
1144       }
1145     }
1146 
1147     // Everything below is indented at least once.
1148     ScopedIndentation indent1(vios);
1149 
1150     {
1151       vios->Stream() << "DEX CODE:\n";
1152       ScopedIndentation indent2(vios);
1153       if (code_item_accessor.HasCodeItem()) {
1154         uint32_t max_pc = code_item_accessor.InsnsSizeInCodeUnits();
1155         for (const DexInstructionPcPair& inst : code_item_accessor) {
1156           if (inst.DexPc() + inst->SizeInCodeUnits() > max_pc) {
1157             LOG(WARNING) << "GLITCH: run-away instruction at idx=0x" << std::hex << inst.DexPc();
1158             break;
1159           }
1160           vios->Stream() << StringPrintf("0x%04x: ", inst.DexPc()) << inst->DumpHexLE(5)
1161                          << StringPrintf("\t| %s\n", inst->DumpString(&dex_file).c_str());
1162         }
1163       }
1164     }
1165 
1166     std::unique_ptr<StackHandleScope<1>> hs;
1167     std::unique_ptr<verifier::MethodVerifier> verifier;
1168     if (Runtime::Current() != nullptr) {
1169       // We need to have the handle scope stay live until after the verifier since the verifier has
1170       // a handle to the dex cache from hs.
1171       ScopedObjectAccess soa(Thread::Current());
1172       hs.reset(new StackHandleScope<1>(Thread::Current()));
1173       vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1174       ScopedIndentation indent2(vios);
1175       verifier.reset(DumpVerifier(vios,
1176                                   soa,
1177                                   hs.get(),
1178                                   dex_method_idx,
1179                                   &dex_file,
1180                                   class_def,
1181                                   code_item,
1182                                   method_access_flags));
1183     }
1184     {
1185       vios->Stream() << "OatMethodOffsets ";
1186       if (options_.absolute_addresses_) {
1187         vios->Stream() << StringPrintf("%p ", oat_method_offsets);
1188       }
1189       vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
1190       if (oat_method_offsets_offset > oat_file_.Size()) {
1191         vios->Stream() << StringPrintf(
1192             "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1193             oat_method_offsets_offset, oat_file_.Size());
1194         // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
1195         vios->Stream() << std::flush;
1196         return false;
1197       }
1198 
1199       ScopedIndentation indent2(vios);
1200       vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
1201       uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1202       if (aligned_code_begin > oat_file_.Size()) {
1203         vios->Stream() << StringPrintf("WARNING: "
1204                                        "code offset 0x%08x is past end of file 0x%08zx.\n",
1205                                        aligned_code_begin, oat_file_.Size());
1206         success = false;
1207       }
1208       vios->Stream() << "\n";
1209     }
1210     {
1211       vios->Stream() << "OatQuickMethodHeader ";
1212       uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1213       const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
1214       if (AddStatsObject(method_header)) {
1215         stats_["QuickMethodHeader"].AddBytes(sizeof(*method_header));
1216       }
1217       if (options_.absolute_addresses_) {
1218         vios->Stream() << StringPrintf("%p ", method_header);
1219       }
1220       vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
1221       if (method_header_offset > oat_file_.Size() ||
1222           sizeof(OatQuickMethodHeader) > oat_file_.Size() - method_header_offset) {
1223         vios->Stream() << StringPrintf(
1224             "WARNING: oat quick method header at offset 0x%08x is past end of file 0x%08zx.\n",
1225             method_header_offset, oat_file_.Size());
1226         // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
1227         vios->Stream() << std::flush;
1228         return false;
1229       }
1230 
1231       ScopedIndentation indent2(vios);
1232       vios->Stream() << "vmap_table: ";
1233       if (options_.absolute_addresses_) {
1234         vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
1235       }
1236       uint32_t vmap_table_offset =
1237           (method_header == nullptr) ? 0 : method_header->GetCodeInfoOffset();
1238       vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
1239 
1240       size_t vmap_table_offset_limit = method_header->GetCode() - oat_file_.Begin();
1241       if (vmap_table_offset >= vmap_table_offset_limit) {
1242         vios->Stream() << StringPrintf("WARNING: "
1243                                        "vmap table offset 0x%08x is past end of file 0x%08zx. ",
1244                                        vmap_table_offset,
1245                                        vmap_table_offset_limit);
1246         success = false;
1247       } else if (options_.dump_vmap_) {
1248         DumpVmapData(vios, oat_method, code_item_accessor);
1249       }
1250     }
1251     {
1252       vios->Stream() << "QuickMethodFrameInfo\n";
1253 
1254       ScopedIndentation indent2(vios);
1255       vios->Stream()
1256           << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1257       vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1258       DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1259       vios->Stream() << "\n";
1260       vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1261       DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1262       vios->Stream() << "\n";
1263     }
1264     {
1265       // Based on spill masks from QuickMethodFrameInfo so placed
1266       // after it is dumped, but useful for understanding quick
1267       // code, so dumped here.
1268       ScopedIndentation indent2(vios);
1269       DumpVregLocations(vios->Stream(), oat_method, code_item_accessor);
1270     }
1271     {
1272       vios->Stream() << "CODE: ";
1273       {
1274         const void* code = oat_method.GetQuickCode();
1275         uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1276         uint64_t aligned_code_end = aligned_code_begin + code_size;
1277         if (AddStatsObject(code)) {
1278           stats_["Code"].AddBytes(code_size);
1279         }
1280 
1281         if (options_.absolute_addresses_) {
1282           vios->Stream() << StringPrintf("%p ", code);
1283         }
1284         vios->Stream() << StringPrintf("(code_offset=0x%08x size=%u)%s\n",
1285                                        code_offset,
1286                                        code_size,
1287                                        code != nullptr ? "..." : "");
1288 
1289         ScopedIndentation indent2(vios);
1290         if (aligned_code_begin > oat_file_.Size()) {
1291           vios->Stream() << StringPrintf("WARNING: "
1292                                          "start of code at 0x%08x is past end of file 0x%08zx.",
1293                                          aligned_code_begin, oat_file_.Size());
1294           success = false;
1295         } else if (aligned_code_end > oat_file_.Size()) {
1296           vios->Stream() << StringPrintf(
1297               "WARNING: "
1298               "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1299               "code size is 0x%08x.\n",
1300               aligned_code_end,
1301               oat_file_.Size(),
1302               code_size);
1303           success = false;
1304           if (options_.disassemble_code_) {
1305             if (aligned_code_begin + kPrologueBytes <= oat_file_.Size()) {
1306               DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
1307             }
1308           }
1309         } else if (code_size > kMaxCodeSize) {
1310           vios->Stream() << StringPrintf(
1311               "WARNING: "
1312               "code size %d is bigger than max expected threshold of %d. "
1313               "code size is 0x%08x.\n",
1314               code_size,
1315               kMaxCodeSize,
1316               code_size);
1317           success = false;
1318           if (options_.disassemble_code_) {
1319             if (aligned_code_begin + kPrologueBytes <= oat_file_.Size()) {
1320               DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
1321             }
1322           }
1323         } else if (options_.disassemble_code_) {
1324           DumpCode(vios, oat_method, code_item_accessor, !success, 0);
1325         }
1326       }
1327     }
1328     vios->Stream() << std::flush;
1329     return success;
1330   }
1331 
DumpSpillMask(std::ostream & os,uint32_t spill_mask,bool is_float)1332   void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1333     if (spill_mask == 0) {
1334       return;
1335     }
1336     os << "(";
1337     for (size_t i = 0; i < 32; i++) {
1338       if ((spill_mask & (1 << i)) != 0) {
1339         if (is_float) {
1340           os << "fr" << i;
1341         } else {
1342           os << "r" << i;
1343         }
1344         spill_mask ^= 1 << i;  // clear bit
1345         if (spill_mask != 0) {
1346           os << ", ";
1347         } else {
1348           break;
1349         }
1350       }
1351     }
1352     os << ")";
1353   }
1354 
1355   // Display data stored at the the vmap offset of an oat method.
DumpVmapData(VariableIndentationOutputStream * vios,const OatFile::OatMethod & oat_method,const CodeItemDataAccessor & code_item_accessor)1356   void DumpVmapData(VariableIndentationOutputStream* vios,
1357                     const OatFile::OatMethod& oat_method,
1358                     const CodeItemDataAccessor& code_item_accessor) {
1359     if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item_accessor)) {
1360       // The optimizing compiler outputs its CodeInfo data in the vmap table.
1361       const uint8_t* raw_code_info = oat_method.GetVmapTable();
1362       if (raw_code_info != nullptr) {
1363         CodeInfo code_info(raw_code_info);
1364         DCHECK(code_item_accessor.HasCodeItem());
1365         ScopedIndentation indent1(vios);
1366         DumpCodeInfo(vios, code_info, oat_method);
1367       }
1368     } else {
1369       // Otherwise, there is nothing to display.
1370     }
1371   }
1372 
1373   // Display a CodeInfo object emitted by the optimizing compiler.
DumpCodeInfo(VariableIndentationOutputStream * vios,const CodeInfo & code_info,const OatFile::OatMethod & oat_method)1374   void DumpCodeInfo(VariableIndentationOutputStream* vios,
1375                     const CodeInfo& code_info,
1376                     const OatFile::OatMethod& oat_method) {
1377     code_info.Dump(vios,
1378                    oat_method.GetCodeOffset(),
1379                    options_.dump_code_info_stack_maps_,
1380                    instruction_set_);
1381   }
1382 
GetOutVROffset(uint16_t out_num,InstructionSet isa)1383   static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1384     // According to stack model, the first out is above the Method referernce.
1385     return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1386   }
1387 
GetVRegOffsetFromQuickCode(const CodeItemDataAccessor & code_item_accessor,uint32_t core_spills,uint32_t fp_spills,size_t frame_size,int reg,InstructionSet isa)1388   static uint32_t GetVRegOffsetFromQuickCode(const CodeItemDataAccessor& code_item_accessor,
1389                                              uint32_t core_spills,
1390                                              uint32_t fp_spills,
1391                                              size_t frame_size,
1392                                              int reg,
1393                                              InstructionSet isa) {
1394     PointerSize pointer_size = InstructionSetPointerSize(isa);
1395     if (kIsDebugBuild) {
1396       auto* runtime = Runtime::Current();
1397       if (runtime != nullptr) {
1398         CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1399       }
1400     }
1401     DCHECK_ALIGNED(frame_size, kStackAlignment);
1402     DCHECK_NE(reg, -1);
1403     int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1404         + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1405         + sizeof(uint32_t);  // Filler.
1406     int num_regs = code_item_accessor.RegistersSize() - code_item_accessor.InsSize();
1407     int temp_threshold = code_item_accessor.RegistersSize();
1408     const int max_num_special_temps = 1;
1409     if (reg == temp_threshold) {
1410       // The current method pointer corresponds to special location on stack.
1411       return 0;
1412     } else if (reg >= temp_threshold + max_num_special_temps) {
1413       /*
1414        * Special temporaries may have custom locations and the logic above deals with that.
1415        * However, non-special temporaries are placed relative to the outs.
1416        */
1417       int temps_start = code_item_accessor.OutsSize() * sizeof(uint32_t)
1418           + static_cast<size_t>(pointer_size) /* art method */;
1419       int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1420       return temps_start + relative_offset;
1421     } else if (reg < num_regs) {
1422       int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1423       return locals_start + (reg * sizeof(uint32_t));
1424     } else {
1425       // Handle ins.
1426       return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1427           + static_cast<size_t>(pointer_size) /* art method */;
1428     }
1429   }
1430 
DumpVregLocations(std::ostream & os,const OatFile::OatMethod & oat_method,const CodeItemDataAccessor & code_item_accessor)1431   void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1432                          const CodeItemDataAccessor& code_item_accessor) {
1433     if (code_item_accessor.HasCodeItem()) {
1434       size_t num_locals_ins = code_item_accessor.RegistersSize();
1435       size_t num_ins = code_item_accessor.InsSize();
1436       size_t num_locals = num_locals_ins - num_ins;
1437       size_t num_outs = code_item_accessor.OutsSize();
1438 
1439       os << "vr_stack_locations:";
1440       for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1441         // For readability, delimit the different kinds of VRs.
1442         if (reg == num_locals_ins) {
1443           os << "\n\tmethod*:";
1444         } else if (reg == num_locals && num_ins > 0) {
1445           os << "\n\tins:";
1446         } else if (reg == 0 && num_locals > 0) {
1447           os << "\n\tlocals:";
1448         }
1449 
1450         uint32_t offset = GetVRegOffsetFromQuickCode(code_item_accessor,
1451                                                      oat_method.GetCoreSpillMask(),
1452                                                      oat_method.GetFpSpillMask(),
1453                                                      oat_method.GetFrameSizeInBytes(),
1454                                                      reg,
1455                                                      GetInstructionSet());
1456         os << " v" << reg << "[sp + #" << offset << "]";
1457       }
1458 
1459       for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1460         if (out_reg == 0) {
1461           os << "\n\touts:";
1462         }
1463 
1464         uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
1465         os << " v" << out_reg << "[sp + #" << offset << "]";
1466       }
1467 
1468       os << "\n";
1469     }
1470   }
1471 
1472   // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1473   // the optimizing compiler?
IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod & oat_method,const CodeItemDataAccessor & code_item_accessor)1474   static bool IsMethodGeneratedByOptimizingCompiler(
1475       const OatFile::OatMethod& oat_method,
1476       const CodeItemDataAccessor& code_item_accessor) {
1477     // If the native GC map is null and the Dex `code_item` is not
1478     // null, then this method has been compiled with the optimizing
1479     // compiler.
1480     return oat_method.GetQuickCode() != nullptr &&
1481            oat_method.GetVmapTable() != nullptr &&
1482            code_item_accessor.HasCodeItem();
1483   }
1484 
DumpVerifier(VariableIndentationOutputStream * vios,ScopedObjectAccess & soa,StackHandleScope<1> * hs,uint32_t dex_method_idx,const DexFile * dex_file,const dex::ClassDef & class_def,const dex::CodeItem * code_item,uint32_t method_access_flags)1485   verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
1486                                          ScopedObjectAccess& soa,
1487                                          StackHandleScope<1>* hs,
1488                                          uint32_t dex_method_idx,
1489                                          const DexFile* dex_file,
1490                                          const dex::ClassDef& class_def,
1491                                          const dex::CodeItem* code_item,
1492                                          uint32_t method_access_flags)
1493       REQUIRES_SHARED(Locks::mutator_lock_) {
1494     if ((method_access_flags & kAccNative) == 0) {
1495       Runtime* const runtime = Runtime::Current();
1496       DCHECK(options_.class_loader_ != nullptr);
1497       Handle<mirror::DexCache> dex_cache = hs->NewHandle(
1498           runtime->GetClassLinker()->RegisterDexFile(*dex_file, options_.class_loader_->Get()));
1499       CHECK(dex_cache != nullptr);
1500       ArtMethod* method = runtime->GetClassLinker()->ResolveMethodWithoutInvokeType(
1501           dex_method_idx, dex_cache, *options_.class_loader_);
1502       if (method == nullptr) {
1503         soa.Self()->ClearException();
1504         return nullptr;
1505       }
1506       return verifier::MethodVerifier::VerifyMethodAndDump(
1507           soa.Self(),
1508           vios,
1509           dex_method_idx,
1510           dex_file,
1511           dex_cache,
1512           *options_.class_loader_,
1513           class_def,
1514           code_item,
1515           method_access_flags,
1516           /* api_level= */ 0);
1517     }
1518 
1519     return nullptr;
1520   }
1521 
DumpCode(VariableIndentationOutputStream * vios,const OatFile::OatMethod & oat_method,const CodeItemDataAccessor & code_item_accessor,bool bad_input,size_t code_size)1522   void DumpCode(VariableIndentationOutputStream* vios,
1523                 const OatFile::OatMethod& oat_method,
1524                 const CodeItemDataAccessor& code_item_accessor,
1525                 bool bad_input, size_t code_size) {
1526     const void* quick_code = oat_method.GetQuickCode();
1527 
1528     if (code_size == 0) {
1529       code_size = oat_method.GetQuickCodeSize();
1530     }
1531     if (code_size == 0 || quick_code == nullptr) {
1532       vios->Stream() << "NO CODE!\n";
1533       return;
1534     } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method,
1535                                                                    code_item_accessor)) {
1536       // The optimizing compiler outputs its CodeInfo data in the vmap table.
1537       CodeInfo code_info(oat_method.GetVmapTable());
1538       if (AddStatsObject(oat_method.GetVmapTable())) {
1539         code_info.CollectSizeStats(oat_method.GetVmapTable(), stats_["CodeInfo"]);
1540       }
1541       std::unordered_map<uint32_t, std::vector<StackMap>> stack_maps;
1542       for (const StackMap& it : code_info.GetStackMaps()) {
1543         stack_maps[it.GetNativePcOffset(instruction_set_)].push_back(it);
1544       }
1545 
1546       const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1547       size_t offset = 0;
1548       while (offset < code_size) {
1549         offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1550         auto it = stack_maps.find(offset);
1551         if (it != stack_maps.end()) {
1552           ScopedIndentation indent1(vios);
1553           for (StackMap stack_map : it->second) {
1554             stack_map.Dump(vios, code_info, oat_method.GetCodeOffset(), instruction_set_);
1555           }
1556           stack_maps.erase(it);
1557         }
1558       }
1559       DCHECK_EQ(stack_maps.size(), 0u);  // Check that all stack maps have been printed.
1560     } else {
1561       const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1562       size_t offset = 0;
1563       while (offset < code_size) {
1564         offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1565       }
1566     }
1567   }
1568 
GetBootImageLiveObjectsDataRange(gc::Heap * heap) const1569   std::pair<const uint8_t*, const uint8_t*> GetBootImageLiveObjectsDataRange(gc::Heap* heap) const
1570       REQUIRES_SHARED(Locks::mutator_lock_) {
1571     const std::vector<gc::space::ImageSpace*>& boot_image_spaces = heap->GetBootImageSpaces();
1572     const ImageHeader& main_header = boot_image_spaces[0]->GetImageHeader();
1573     ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects =
1574         ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(
1575             main_header.GetImageRoot<kWithoutReadBarrier>(ImageHeader::kBootImageLiveObjects));
1576     DCHECK(boot_image_live_objects != nullptr);
1577     DCHECK(heap->ObjectIsInBootImageSpace(boot_image_live_objects));
1578     const uint8_t* boot_image_live_objects_address =
1579         reinterpret_cast<const uint8_t*>(boot_image_live_objects.Ptr());
1580     uint32_t begin_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(0).Uint32Value();
1581     uint32_t end_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(
1582         boot_image_live_objects->GetLength()).Uint32Value();
1583     return std::make_pair(boot_image_live_objects_address + begin_offset,
1584                           boot_image_live_objects_address + end_offset);
1585   }
1586 
DumpDataImgRelRoEntries(std::ostream & os)1587   void DumpDataImgRelRoEntries(std::ostream& os) {
1588     os << ".data.img.rel.ro: ";
1589     if (oat_file_.GetBootImageRelocations().empty()) {
1590       os << "empty.\n\n";
1591       return;
1592     }
1593 
1594     os << oat_file_.GetBootImageRelocations().size() << " entries.\n";
1595     Runtime* runtime = Runtime::Current();
1596     if (runtime != nullptr && !runtime->GetHeap()->GetBootImageSpaces().empty()) {
1597       const std::vector<gc::space::ImageSpace*>& boot_image_spaces =
1598           runtime->GetHeap()->GetBootImageSpaces();
1599       ScopedObjectAccess soa(Thread::Current());
1600       auto live_objects = GetBootImageLiveObjectsDataRange(runtime->GetHeap());
1601       const uint8_t* live_objects_begin = live_objects.first;
1602       const uint8_t* live_objects_end = live_objects.second;
1603       for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1604         uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1605         uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1606         os << StringPrintf("  0x%x: 0x%08x", entry_offset, object_offset);
1607         uint8_t* address = boot_image_spaces[0]->Begin() + object_offset;
1608         bool found = false;
1609         for (gc::space::ImageSpace* space : boot_image_spaces) {
1610           uint64_t local_offset = address - space->Begin();
1611           if (local_offset < space->GetImageHeader().GetImageSize()) {
1612             if (space->GetImageHeader().GetObjectsSection().Contains(local_offset)) {
1613               if (address >= live_objects_begin && address < live_objects_end) {
1614                 size_t index =
1615                     (address - live_objects_begin) / sizeof(mirror::HeapReference<mirror::Object>);
1616                 os << StringPrintf("   0x%08x BootImageLiveObject[%zu]",
1617                                    object_offset,
1618                                    index);
1619               } else {
1620                 ObjPtr<mirror::Object> o = reinterpret_cast<mirror::Object*>(address);
1621                 if (o->IsString()) {
1622                   os << "   String: " << o->AsString()->ToModifiedUtf8();
1623                 } else if (o->IsClass()) {
1624                   os << "   Class: " << o->AsClass()->PrettyDescriptor();
1625                 } else {
1626                   os << StringPrintf("   0x%08x %s",
1627                                      object_offset,
1628                                      o->GetClass()->PrettyDescriptor().c_str());
1629                 }
1630               }
1631             } else if (space->GetImageHeader().GetMethodsSection().Contains(local_offset)) {
1632               ArtMethod* m = reinterpret_cast<ArtMethod*>(address);
1633               os << "   ArtMethod: " << m->PrettyMethod();
1634             } else {
1635               os << StringPrintf("   0x%08x <unexpected section in %s>",
1636                                  object_offset,
1637                                  space->GetImageFilename().c_str());
1638             }
1639             found = true;
1640             break;
1641           }
1642         }
1643         if (!found) {
1644           os << StringPrintf("   0x%08x <outside boot image spaces>", object_offset);
1645         }
1646         os << "\n";
1647       }
1648     } else {
1649       for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1650         uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1651         uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1652         os << StringPrintf("  0x%x: 0x%08x\n", entry_offset, object_offset);
1653       }
1654     }
1655     os << "\n";
1656   }
1657 
1658   template <typename NameGetter>
DumpBssEntries(std::ostream & os,const char * slot_type,const IndexBssMapping * mapping,uint32_t number_of_indexes,size_t slot_size,NameGetter name)1659   void DumpBssEntries(std::ostream& os,
1660                       const char* slot_type,
1661                       const IndexBssMapping* mapping,
1662                       uint32_t number_of_indexes,
1663                       size_t slot_size,
1664                       NameGetter name) {
1665     os << ".bss mapping for " << slot_type << ": ";
1666     if (mapping == nullptr) {
1667       os << "empty.\n";
1668       return;
1669     }
1670     size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
1671     size_t num_valid_indexes = 0u;
1672     for (const IndexBssMappingEntry& entry : *mapping) {
1673       num_valid_indexes += 1u + POPCOUNT(entry.GetMask(index_bits));
1674     }
1675     os << mapping->size() << " entries for " << num_valid_indexes << " valid indexes.\n";
1676     os << std::hex;
1677     for (const IndexBssMappingEntry& entry : *mapping) {
1678       uint32_t index = entry.GetIndex(index_bits);
1679       uint32_t mask = entry.GetMask(index_bits);
1680       size_t bss_offset = entry.bss_offset - POPCOUNT(mask) * slot_size;
1681       for (uint32_t n : LowToHighBits(mask)) {
1682         size_t current_index = index - (32u - index_bits) + n;
1683         os << "  0x" << bss_offset << ": " << slot_type << ": " << name(current_index) << "\n";
1684         bss_offset += slot_size;
1685       }
1686       DCHECK_EQ(bss_offset, entry.bss_offset);
1687       os << "  0x" << bss_offset << ": " << slot_type << ": " << name(index) << "\n";
1688     }
1689     os << std::dec;
1690   }
1691 
DumpBssMappings(std::ostream & os,const DexFile * dex_file,const IndexBssMapping * method_bss_mapping,const IndexBssMapping * type_bss_mapping,const IndexBssMapping * public_type_bss_mapping,const IndexBssMapping * package_type_bss_mapping,const IndexBssMapping * string_bss_mapping,const IndexBssMapping * method_type_bss_mapping)1692   void DumpBssMappings(std::ostream& os,
1693                        const DexFile* dex_file,
1694                        const IndexBssMapping* method_bss_mapping,
1695                        const IndexBssMapping* type_bss_mapping,
1696                        const IndexBssMapping* public_type_bss_mapping,
1697                        const IndexBssMapping* package_type_bss_mapping,
1698                        const IndexBssMapping* string_bss_mapping,
1699                        const IndexBssMapping* method_type_bss_mapping) {
1700     DumpBssEntries(os,
1701                    "ArtMethod",
1702                    method_bss_mapping,
1703                    dex_file->NumMethodIds(),
1704                    static_cast<size_t>(GetInstructionSetPointerSize(instruction_set_)),
1705                    [=](uint32_t index) { return dex_file->PrettyMethod(index); });
1706     DumpBssEntries(os,
1707                    "Class",
1708                    type_bss_mapping,
1709                    dex_file->NumTypeIds(),
1710                    sizeof(GcRoot<mirror::Class>),
1711                    [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
1712     DumpBssEntries(os,
1713                    "Public Class",
1714                    public_type_bss_mapping,
1715                    dex_file->NumTypeIds(),
1716                    sizeof(GcRoot<mirror::Class>),
1717                    [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
1718     DumpBssEntries(os,
1719                    "Package Class",
1720                    package_type_bss_mapping,
1721                    dex_file->NumTypeIds(),
1722                    sizeof(GcRoot<mirror::Class>),
1723                    [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
1724     DumpBssEntries(
1725         os,
1726         "String",
1727         string_bss_mapping,
1728         dex_file->NumStringIds(),
1729         sizeof(GcRoot<mirror::Class>),
1730         [=](uint32_t index) { return dex_file->GetStringData(dex::StringIndex(index)); });
1731     DumpBssEntries(os,
1732                    "MethodType",
1733                    method_type_bss_mapping,
1734                    dex_file->NumProtoIds(),
1735                    sizeof(GcRoot<mirror::MethodType>),
1736                    [=](uint32_t index) {
1737                      const dex::ProtoId& proto_id = dex_file->GetProtoId(dex::ProtoIndex(index));
1738                      return dex_file->GetProtoSignature(proto_id).ToString();
1739                    });
1740   }
1741 
DumpBssOffsets(std::ostream & os,const char * slot_type,const IndexBssMapping * mapping)1742   void DumpBssOffsets(std::ostream& os, const char* slot_type, const IndexBssMapping* mapping) {
1743     os << ".bss offset for " << slot_type << ": ";
1744     if (mapping == nullptr) {
1745       os << "empty.\n";
1746       return;
1747     }
1748 
1749     os << "Mapping size: " << mapping->size() << "\n";
1750     for (size_t i = 0; i < mapping->size(); ++i) {
1751       os << "Entry[" << i << "]: index_and_mask: "
1752          << mapping->At(i).index_and_mask
1753          << ", bss_offset: "
1754          << mapping->At(i).bss_offset << "\n";
1755     }
1756 
1757     // TODO(solanes, 154012332): We are dumping the raw values but we could make assumptions about
1758     // ordering of the entries and deconstruct even the `index_and_mask`. This would allow us to use
1759     // DumpBssEntries and dump more information. The size and alignment of the entry (ArtMethod*
1760     // depends on instruction set but Class and String references are 32-bit) and the difference
1761     // from the previous `bss_offset` (or from the "oatbss" symbol for the first item) tell us how
1762     // many .bss entries a single `IndexBssMappingEntry` should describe. So we know how many most
1763     // significant set bits represent the mask and the rest is the actual index. And the position of
1764     // the mask bits would allow reconstructing the other indexes.
1765   }
1766 
1767   const OatFile& oat_file_;
1768   const std::vector<const OatDexFile*> oat_dex_files_;
1769   const OatDumperOptions& options_;
1770   uint32_t resolved_addr2instr_;
1771   const InstructionSet instruction_set_;
1772   std::set<uintptr_t> offsets_;
1773   Disassembler* disassembler_;
1774   Stats stats_;
1775   std::unordered_set<const void*> seen_stats_objects_;
1776 };
1777 
1778 class ImageDumper {
1779  public:
ImageDumper(std::ostream * os,gc::space::ImageSpace & image_space,const ImageHeader & image_header,OatDumperOptions * oat_dumper_options)1780   ImageDumper(std::ostream* os,
1781               gc::space::ImageSpace& image_space,
1782               const ImageHeader& image_header,
1783               OatDumperOptions* oat_dumper_options)
1784       : os_(os),
1785         vios_(os),
1786         indent1_(&vios_),
1787         image_space_(image_space),
1788         image_header_(image_header),
1789         oat_dumper_options_(oat_dumper_options) {}
1790 
Dump()1791   bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
1792     std::ostream& os = *os_;
1793     std::ostream& indent_os = vios_.Stream();
1794 
1795     os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
1796 
1797     os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1798 
1799     os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n";
1800     os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n";
1801     os << "IMAGE CHECKSUM: " << std::hex << image_header_.GetImageChecksum() << std::dec << "\n\n";
1802 
1803     os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum()) << "\n";
1804     os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n";
1805     os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n";
1806     os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n";
1807     os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
1808 
1809     os << "BOOT IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetBootImageBegin())
1810         << "\n";
1811     os << "BOOT IMAGE SIZE: " << image_header_.GetBootImageSize() << "\n\n";
1812 
1813     for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1814       auto section = static_cast<ImageHeader::ImageSections>(i);
1815       os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1816     }
1817 
1818     {
1819       os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots().Ptr()) << "\n";
1820       static_assert(arraysize(image_roots_descriptions_) ==
1821           static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
1822       DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1823       for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
1824         ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1825         const char* image_root_description = image_roots_descriptions_[i];
1826         ObjPtr<mirror::Object> image_root_object = image_header_.GetImageRoot(image_root);
1827         indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object.Ptr());
1828         if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
1829           ObjPtr<mirror::ObjectArray<mirror::Object>> image_root_object_array
1830               = image_root_object->AsObjectArray<mirror::Object>();
1831           ScopedIndentation indent2(&vios_);
1832           for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1833             ObjPtr<mirror::Object> value = image_root_object_array->Get(j);
1834             size_t run = 0;
1835             for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1836               if (value == image_root_object_array->Get(k)) {
1837                 run++;
1838               } else {
1839                 break;
1840               }
1841             }
1842             if (run == 0) {
1843               indent_os << StringPrintf("%d: ", j);
1844             } else {
1845               indent_os << StringPrintf("%d to %zd: ", j, j + run);
1846               j = j + run;
1847             }
1848             if (value != nullptr) {
1849               PrettyObjectValue(indent_os, value->GetClass(), value);
1850             } else {
1851               indent_os << j << ": null\n";
1852             }
1853           }
1854         }
1855       }
1856     }
1857 
1858     {
1859       os << "METHOD ROOTS\n";
1860       static_assert(arraysize(image_methods_descriptions_) ==
1861           static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1862       for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1863         auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1864         const char* description = image_methods_descriptions_[i];
1865         auto* image_method = image_header_.GetImageMethod(image_root);
1866         indent_os << StringPrintf("%s: %p\n", description, image_method);
1867       }
1868     }
1869     os << "\n";
1870 
1871     Runtime* const runtime = Runtime::Current();
1872     std::string image_filename = image_space_.GetImageFilename();
1873     std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
1874     os << "OAT LOCATION: " << oat_location;
1875     os << "\n";
1876     std::string error_msg;
1877     const OatFile* oat_file = image_space_.GetOatFile();
1878     if (oat_file == nullptr) {
1879       oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1880     }
1881     if (oat_file == nullptr) {
1882       oat_file = OatFile::Open(/*zip_fd=*/ -1,
1883                                oat_location,
1884                                oat_location,
1885                                /*executable=*/ false,
1886                                /*low_4gb=*/ false,
1887                                &error_msg);
1888     }
1889     if (oat_file == nullptr) {
1890       os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1891       return EXIT_FAILURE;
1892     }
1893     os << "\n";
1894 
1895     stats_.oat_file_bytes = oat_file->Size();
1896     stats_.oat_file_stats.AddBytes(oat_file->Size());
1897 
1898     oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
1899 
1900     for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
1901       CHECK(oat_dex_file != nullptr);
1902       stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1903                                                          oat_dex_file->FileSize()));
1904     }
1905 
1906     os << "OBJECTS:\n" << std::flush;
1907 
1908     // Loop through the image space and dump its objects.
1909     gc::Heap* heap = runtime->GetHeap();
1910     Thread* self = Thread::Current();
1911     {
1912       {
1913         WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1914         heap->FlushAllocStack();
1915       }
1916       // Since FlushAllocStack() above resets the (active) allocation
1917       // stack. Need to revoke the thread-local allocation stacks that
1918       // point into it.
1919       ScopedThreadSuspension sts(self, ThreadState::kNative);
1920       ScopedSuspendAll ssa(__FUNCTION__);
1921       heap->RevokeAllThreadLocalAllocationStacks(self);
1922     }
1923     {
1924       auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1925         DumpObject(obj);
1926       };
1927       ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
1928       // Dump the normal objects before ArtMethods.
1929       image_space_.GetLiveBitmap()->Walk(dump_visitor);
1930       indent_os << "\n";
1931       // TODO: Dump fields.
1932       // Dump methods after.
1933       image_header_.VisitPackedArtMethods([&](ArtMethod& method)
1934           REQUIRES_SHARED(Locks::mutator_lock_) {
1935         std::ostream& indent_os = vios_.Stream();
1936         indent_os << &method << " " << " ArtMethod: " << method.PrettyMethod() << "\n";
1937         DumpMethod(&method, indent_os);
1938         indent_os << "\n";
1939       },  image_space_.Begin(), image_header_.GetPointerSize());
1940       // Dump the large objects separately.
1941       heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
1942       indent_os << "\n";
1943     }
1944     os << "STATS:\n" << std::flush;
1945     std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
1946     size_t data_size = image_header_.GetDataSize();  // stored size in file.
1947     if (file == nullptr) {
1948       LOG(WARNING) << "Failed to find image in " << image_filename;
1949     } else {
1950       size_t file_bytes = file->GetLength();
1951       // If the image is compressed, adjust to decompressed size.
1952       size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1953       if (!image_header_.HasCompressedBlock()) {
1954         DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1955       }
1956       file_bytes += uncompressed_size - data_size;
1957       stats_.art_file_stats.AddBytes(file_bytes);
1958       stats_.art_file_stats["Header"].AddBytes(sizeof(ImageHeader));
1959     }
1960 
1961     size_t pointer_size = static_cast<size_t>(image_header_.GetPointerSize());
1962     CHECK_ALIGNED(image_header_.GetFieldsSection().Offset(), 4);
1963     CHECK_ALIGNED_PARAM(image_header_.GetMethodsSection().Offset(), pointer_size);
1964     CHECK_ALIGNED(image_header_.GetInternedStringsSection().Offset(), 8);
1965     CHECK_ALIGNED(image_header_.GetImageBitmapSection().Offset(), kElfSegmentAlignment);
1966 
1967     for (size_t i = 0; i < ImageHeader::ImageSections::kSectionCount; i++) {
1968       ImageHeader::ImageSections index = ImageHeader::ImageSections(i);
1969       const char* name = ImageHeader::GetImageSectionName(index);
1970       stats_.art_file_stats[name].AddBytes(image_header_.GetImageSection(index).Size());
1971     }
1972 
1973     stats_.object_stats.AddBytes(image_header_.GetObjectsSection().Size());
1974     stats_.Dump(os);
1975     os << "\n";
1976 
1977     os << std::flush;
1978 
1979     return oat_dumper_->Dump(os);
1980   }
1981 
1982  private:
PrettyObjectValue(std::ostream & os,ObjPtr<mirror::Class> type,ObjPtr<mirror::Object> value)1983   static void PrettyObjectValue(std::ostream& os,
1984                                 ObjPtr<mirror::Class> type,
1985                                 ObjPtr<mirror::Object> value)
1986       REQUIRES_SHARED(Locks::mutator_lock_) {
1987     CHECK(type != nullptr);
1988     if (value == nullptr) {
1989       os << StringPrintf("null   %s\n", type->PrettyDescriptor().c_str());
1990     } else if (type->IsStringClass()) {
1991       ObjPtr<mirror::String> string = value->AsString();
1992       os << StringPrintf("%p   String: %s\n",
1993                          string.Ptr(),
1994                          PrintableString(string->ToModifiedUtf8().c_str()).c_str());
1995     } else if (type->IsClassClass()) {
1996       ObjPtr<mirror::Class> klass = value->AsClass();
1997       os << StringPrintf("%p   Class: %s\n",
1998                          klass.Ptr(),
1999                          mirror::Class::PrettyDescriptor(klass).c_str());
2000     } else {
2001       os << StringPrintf("%p   %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
2002     }
2003   }
2004 
PrintField(std::ostream & os,ArtField * field,ObjPtr<mirror::Object> obj)2005   static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
2006       REQUIRES_SHARED(Locks::mutator_lock_) {
2007     os << StringPrintf("%s: ", field->GetName());
2008     switch (field->GetTypeAsPrimitiveType()) {
2009       case Primitive::kPrimLong:
2010         os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
2011         break;
2012       case Primitive::kPrimDouble:
2013         os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
2014         break;
2015       case Primitive::kPrimFloat:
2016         os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
2017         break;
2018       case Primitive::kPrimInt:
2019         os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
2020         break;
2021       case Primitive::kPrimChar:
2022         os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
2023         break;
2024       case Primitive::kPrimShort:
2025         os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
2026         break;
2027       case Primitive::kPrimBoolean:
2028         os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
2029             field->GetBoolean(obj));
2030         break;
2031       case Primitive::kPrimByte:
2032         os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
2033         break;
2034       case Primitive::kPrimNot: {
2035         // Get the value, don't compute the type unless it is non-null as we don't want
2036         // to cause class loading.
2037         ObjPtr<mirror::Object> value = field->GetObj(obj);
2038         if (value == nullptr) {
2039           os << StringPrintf("null   %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2040         } else {
2041           // Grab the field type without causing resolution.
2042           ObjPtr<mirror::Class> field_type = field->LookupResolvedType();
2043           if (field_type != nullptr) {
2044             PrettyObjectValue(os, field_type, value);
2045           } else {
2046             os << StringPrintf("%p   %s\n",
2047                                value.Ptr(),
2048                                PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2049           }
2050         }
2051         break;
2052       }
2053       default:
2054         os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2055         break;
2056     }
2057   }
2058 
DumpFields(std::ostream & os,mirror::Object * obj,ObjPtr<mirror::Class> klass)2059   static void DumpFields(std::ostream& os, mirror::Object* obj, ObjPtr<mirror::Class> klass)
2060       REQUIRES_SHARED(Locks::mutator_lock_) {
2061     ObjPtr<mirror::Class> super = klass->GetSuperClass();
2062     if (super != nullptr) {
2063       DumpFields(os, obj, super);
2064     }
2065     for (ArtField& field : klass->GetIFields()) {
2066       PrintField(os, &field, obj);
2067     }
2068   }
2069 
InDumpSpace(const mirror::Object * object)2070   bool InDumpSpace(const mirror::Object* object) {
2071     return image_space_.Contains(object);
2072   }
2073 
GetQuickOatCodeBegin(ArtMethod * m)2074   const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
2075     const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
2076         image_header_.GetPointerSize());
2077     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2078     if (class_linker->IsQuickResolutionStub(quick_code) ||
2079         class_linker->IsQuickToInterpreterBridge(quick_code) ||
2080         class_linker->IsNterpTrampoline(quick_code) ||
2081         class_linker->IsQuickGenericJniStub(quick_code) ||
2082         class_linker->IsJniDlsymLookupStub(quick_code) ||
2083         class_linker->IsJniDlsymLookupCriticalStub(quick_code)) {
2084       quick_code = oat_dumper_->GetQuickOatCode(m);
2085     }
2086     if (oat_dumper_->GetInstructionSet() == InstructionSet::kThumb2) {
2087       quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
2088     }
2089     return quick_code;
2090   }
2091 
GetQuickOatCodeSize(ArtMethod * m)2092   uint32_t GetQuickOatCodeSize(ArtMethod* m)
2093       REQUIRES_SHARED(Locks::mutator_lock_) {
2094     const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2095     if (oat_code_begin == nullptr) {
2096       return 0;
2097     }
2098     OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2099         reinterpret_cast<uintptr_t>(oat_code_begin) - sizeof(OatQuickMethodHeader));
2100     return method_header->GetCodeSize();
2101   }
2102 
GetQuickOatCodeEnd(ArtMethod * m)2103   const void* GetQuickOatCodeEnd(ArtMethod* m)
2104       REQUIRES_SHARED(Locks::mutator_lock_) {
2105     const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
2106     if (oat_code_begin == nullptr) {
2107       return nullptr;
2108     }
2109     return oat_code_begin + GetQuickOatCodeSize(m);
2110   }
2111 
DumpObject(mirror::Object * obj)2112   void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
2113     DCHECK(obj != nullptr);
2114     if (!InDumpSpace(obj)) {
2115       return;
2116     }
2117 
2118     std::ostream& os = vios_.Stream();
2119 
2120     ObjPtr<mirror::Class> obj_class = obj->GetClass();
2121     if (obj_class->IsArrayClass()) {
2122       os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
2123                          obj->AsArray()->GetLength());
2124     } else if (obj_class->IsClassClass()) {
2125       ObjPtr<mirror::Class> klass = obj->AsClass();
2126       os << StringPrintf("%p: java.lang.Class \"%s\" (",
2127                          obj,
2128                          mirror::Class::PrettyDescriptor(klass).c_str())
2129          << klass->GetStatus() << ")\n";
2130     } else if (obj_class->IsStringClass()) {
2131       os << StringPrintf("%p: java.lang.String %s\n",
2132                          obj,
2133                          PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
2134     } else {
2135       os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
2136     }
2137     ScopedIndentation indent1(&vios_);
2138     DumpFields(os, obj, obj_class);
2139     if (obj->IsObjectArray()) {
2140       ObjPtr<mirror::ObjectArray<mirror::Object>> obj_array = obj->AsObjectArray<mirror::Object>();
2141       for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
2142         ObjPtr<mirror::Object> value = obj_array->Get(i);
2143         size_t run = 0;
2144         for (int32_t j = i + 1; j < length; j++) {
2145           if (value == obj_array->Get(j)) {
2146             run++;
2147           } else {
2148             break;
2149           }
2150         }
2151         if (run == 0) {
2152           os << StringPrintf("%d: ", i);
2153         } else {
2154           os << StringPrintf("%d to %zd: ", i, i + run);
2155           i = i + run;
2156         }
2157         ObjPtr<mirror::Class> value_class =
2158             (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
2159         PrettyObjectValue(os, value_class, value);
2160       }
2161     } else if (obj_class->IsClassClass()) {
2162       ObjPtr<mirror::Class> klass = obj->AsClass();
2163 
2164       if (kBitstringSubtypeCheckEnabled) {
2165         os << "SUBTYPE_CHECK_BITS: ";
2166         SubtypeCheck<ObjPtr<mirror::Class>>::Dump(klass, os);
2167         os << "\n";
2168       }
2169 
2170       if (klass->ShouldHaveEmbeddedVTable()) {
2171         os << "EMBEDDED VTABLE:\n";
2172         ScopedIndentation indent2(&vios_);
2173         const PointerSize pointer_size = image_header_.GetPointerSize();
2174         for (size_t i = 0, length = klass->GetEmbeddedVTableLength(); i != length; ++i) {
2175           os << i << ": "
2176              << ArtMethod::PrettyMethod(klass->GetEmbeddedVTableEntry(i, pointer_size)) << '\n';
2177         }
2178       }
2179 
2180       if (klass->NumStaticFields() != 0) {
2181         os << "STATICS:\n";
2182         ScopedIndentation indent2(&vios_);
2183         for (ArtField& field : klass->GetSFields()) {
2184           PrintField(os, &field, field.GetDeclaringClass());
2185         }
2186       }
2187     }
2188     std::string temp;
2189     const char* desc = obj_class->GetDescriptor(&temp);
2190     desc = stats_.descriptors.emplace(desc).first->c_str();  // Dedup and keep alive.
2191     stats_.object_stats[desc].AddBytes(obj->SizeOf());
2192   }
2193 
DumpMethod(ArtMethod * method,std::ostream & indent_os)2194   void DumpMethod(ArtMethod* method, std::ostream& indent_os)
2195       REQUIRES_SHARED(Locks::mutator_lock_) {
2196     DCHECK(method != nullptr);
2197     const PointerSize pointer_size = image_header_.GetPointerSize();
2198     if (method->IsNative()) {
2199       const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2200       bool first_occurrence;
2201       uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2202       ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2203       if (first_occurrence) {
2204         stats_.oat_file_stats["native_code"].AddBytes(quick_oat_code_size);
2205       }
2206       if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2207           image_header_.GetPointerSize())) {
2208         indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
2209       }
2210     } else if (method->IsAbstract() || method->IsClassInitializer()) {
2211       // Don't print information for these.
2212     } else if (method->IsRuntimeMethod()) {
2213       if (method == Runtime::Current()->GetResolutionMethod()) {
2214         const void* resolution_trampoline =
2215             method->GetEntryPointFromQuickCompiledCodePtrSize(image_header_.GetPointerSize());
2216         indent_os << StringPrintf("Resolution trampoline: %p\n", resolution_trampoline);
2217         const void* critical_native_resolution_trampoline =
2218             method->GetEntryPointFromJniPtrSize(image_header_.GetPointerSize());
2219         indent_os << StringPrintf("Resolution trampoline for @CriticalNative: %p\n",
2220                                   critical_native_resolution_trampoline);
2221       } else {
2222         ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2223         if (table != nullptr) {
2224           indent_os << "IMT conflict table " << table << " method: ";
2225           for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
2226             indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2227                       << " ";
2228           }
2229         }
2230       }
2231     } else {
2232       CodeItemDataAccessor code_item_accessor(method->DexInstructionData());
2233       size_t dex_instruction_bytes = code_item_accessor.InsnsSizeInCodeUnits() * 2;
2234       stats_.dex_instruction_bytes += dex_instruction_bytes;
2235 
2236       const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2237       const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
2238 
2239       bool first_occurrence;
2240       size_t vmap_table_bytes = 0u;
2241       if (quick_oat_code_begin != nullptr) {
2242         OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2243             reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
2244         vmap_table_bytes = ComputeOatSize(method_header->GetOptimizedCodeInfoPtr(),
2245                                           &first_occurrence);
2246         if (first_occurrence) {
2247           stats_.vmap_table_bytes += vmap_table_bytes;
2248         }
2249       }
2250 
2251       uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2252       ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2253       if (first_occurrence) {
2254         stats_.managed_code_bytes += quick_oat_code_size;
2255         art::Stats& managed_code_stats = stats_.oat_file_stats["managed_code"];
2256         managed_code_stats.AddBytes(quick_oat_code_size);
2257         if (method->IsConstructor()) {
2258           if (method->IsStatic()) {
2259             managed_code_stats["class_initializer"].AddBytes(quick_oat_code_size);
2260           } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
2261             managed_code_stats["large_initializer"].AddBytes(quick_oat_code_size);
2262           }
2263         } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
2264           managed_code_stats["large_method"].AddBytes(quick_oat_code_size);
2265         }
2266       }
2267       stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
2268 
2269       uint32_t method_access_flags = method->GetAccessFlags();
2270 
2271       indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
2272       indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2273                                 dex_instruction_bytes,
2274                                 vmap_table_bytes,
2275                                 method_access_flags);
2276 
2277       size_t total_size = dex_instruction_bytes +
2278           vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
2279 
2280       double expansion =
2281       static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
2282       stats_.ComputeOutliers(total_size, expansion, method);
2283     }
2284   }
2285 
2286   std::set<const void*> already_seen_;
2287   // Compute the size of the given data within the oat file and whether this is the first time
2288   // this data has been requested
ComputeOatSize(const void * oat_data,bool * first_occurrence)2289   size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
2290     if (already_seen_.count(oat_data) == 0) {
2291       *first_occurrence = true;
2292       already_seen_.insert(oat_data);
2293     } else {
2294       *first_occurrence = false;
2295     }
2296     return oat_dumper_->ComputeSize(oat_data);
2297   }
2298 
2299  public:
2300   struct Stats {
2301     art::Stats art_file_stats;
2302     art::Stats oat_file_stats;
2303     art::Stats object_stats;
2304     std::set<std::string> descriptors;
2305 
2306     size_t oat_file_bytes = 0u;
2307     size_t managed_code_bytes = 0u;
2308     size_t managed_code_bytes_ignoring_deduplication = 0u;
2309 
2310     size_t vmap_table_bytes = 0u;
2311 
2312     size_t dex_instruction_bytes = 0u;
2313 
2314     std::vector<ArtMethod*> method_outlier;
2315     std::vector<size_t> method_outlier_size;
2316     std::vector<double> method_outlier_expansion;
2317     std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
2318 
Statsart::ImageDumper::Stats2319     Stats() {}
2320 
PercentOfOatBytesart::ImageDumper::Stats2321     double PercentOfOatBytes(size_t size) {
2322       return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2323     }
2324 
ComputeOutliersart::ImageDumper::Stats2325     void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
2326       method_outlier_size.push_back(total_size);
2327       method_outlier_expansion.push_back(expansion);
2328       method_outlier.push_back(method);
2329     }
2330 
DumpOutliersart::ImageDumper::Stats2331     void DumpOutliers(std::ostream& os)
2332         REQUIRES_SHARED(Locks::mutator_lock_) {
2333       size_t sum_of_sizes = 0;
2334       size_t sum_of_sizes_squared = 0;
2335       size_t sum_of_expansion = 0;
2336       size_t sum_of_expansion_squared = 0;
2337       size_t n = method_outlier_size.size();
2338       if (n <= 1) {
2339         return;
2340       }
2341       for (size_t i = 0; i < n; i++) {
2342         size_t cur_size = method_outlier_size[i];
2343         sum_of_sizes += cur_size;
2344         sum_of_sizes_squared += cur_size * cur_size;
2345         double cur_expansion = method_outlier_expansion[i];
2346         sum_of_expansion += cur_expansion;
2347         sum_of_expansion_squared += cur_expansion * cur_expansion;
2348       }
2349       size_t size_mean = sum_of_sizes / n;
2350       size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2351       double expansion_mean = sum_of_expansion / n;
2352       double expansion_variance =
2353           (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2354 
2355       // Dump methods whose size is a certain number of standard deviations from the mean
2356       size_t dumped_values = 0;
2357       size_t skipped_values = 0;
2358       for (size_t i = 100; i > 0; i--) {  // i is the current number of standard deviations
2359         size_t cur_size_variance = i * i * size_variance;
2360         bool first = true;
2361         for (size_t j = 0; j < n; j++) {
2362           size_t cur_size = method_outlier_size[j];
2363           if (cur_size > size_mean) {
2364             size_t cur_var = cur_size - size_mean;
2365             cur_var = cur_var * cur_var;
2366             if (cur_var > cur_size_variance) {
2367               if (dumped_values > 20) {
2368                 if (i == 1) {
2369                   skipped_values++;
2370                 } else {
2371                   i = 2;  // jump to counting for 1 standard deviation
2372                   break;
2373                 }
2374               } else {
2375                 if (first) {
2376                   os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
2377                   first = false;
2378                 }
2379                 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
2380                     << PrettySize(cur_size) << "\n";
2381                 method_outlier_size[j] = 0;  // don't consider this method again
2382                 dumped_values++;
2383               }
2384             }
2385           }
2386         }
2387       }
2388       if (skipped_values > 0) {
2389         os << "... skipped " << skipped_values
2390            << " methods with size > 1 standard deviation from the norm\n";
2391       }
2392       os << std::flush;
2393 
2394       // Dump methods whose expansion is a certain number of standard deviations from the mean
2395       dumped_values = 0;
2396       skipped_values = 0;
2397       for (size_t i = 10; i > 0; i--) {  // i is the current number of standard deviations
2398         double cur_expansion_variance = i * i * expansion_variance;
2399         bool first = true;
2400         for (size_t j = 0; j < n; j++) {
2401           double cur_expansion = method_outlier_expansion[j];
2402           if (cur_expansion > expansion_mean) {
2403             size_t cur_var = cur_expansion - expansion_mean;
2404             cur_var = cur_var * cur_var;
2405             if (cur_var > cur_expansion_variance) {
2406               if (dumped_values > 20) {
2407                 if (i == 1) {
2408                   skipped_values++;
2409                 } else {
2410                   i = 2;  // jump to counting for 1 standard deviation
2411                   break;
2412                 }
2413               } else {
2414                 if (first) {
2415                   os << "\nLarge expansion methods (size > " << i
2416                       << " standard deviations the norm):\n";
2417                   first = false;
2418                 }
2419                 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
2420                    << cur_expansion << "\n";
2421                 method_outlier_expansion[j] = 0.0;  // don't consider this method again
2422                 dumped_values++;
2423               }
2424             }
2425           }
2426         }
2427       }
2428       if (skipped_values > 0) {
2429         os << "... skipped " << skipped_values
2430            << " methods with expansion > 1 standard deviation from the norm\n";
2431       }
2432       os << "\n" << std::flush;
2433     }
2434 
Dumpart::ImageDumper::Stats2435     void Dump(std::ostream& os)
2436         REQUIRES_SHARED(Locks::mutator_lock_) {
2437       VariableIndentationOutputStream vios(&os);
2438       art_file_stats.DumpSizes(vios, "ArtFile");
2439       os << "\n" << std::flush;
2440       object_stats.DumpSizes(vios, "Objects");
2441       os << "\n" << std::flush;
2442       oat_file_stats.DumpSizes(vios, "OatFile");
2443       os << "\n" << std::flush;
2444 
2445       for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
2446         os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
2447                            oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2448                            PercentOfOatBytes(oat_dex_file_size.second));
2449       }
2450 
2451       os << "\n" << StringPrintf("vmap_table_bytes       = %7zd (%2.0f%% of oat file bytes)\n\n",
2452                                  vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
2453          << std::flush;
2454 
2455       os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2456          << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
2457                          static_cast<double>(managed_code_bytes) /
2458                              static_cast<double>(dex_instruction_bytes),
2459                          static_cast<double>(managed_code_bytes_ignoring_deduplication) /
2460                              static_cast<double>(dex_instruction_bytes))
2461          << std::flush;
2462 
2463       DumpOutliers(os);
2464     }
2465   } stats_;
2466 
2467  private:
2468   enum {
2469     // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2470     // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2471     kLargeConstructorDexBytes = 4000,
2472     // Number of bytes for a method to be considered large. Based on the 4000 basic block
2473     // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2474     kLargeMethodDexBytes = 16000
2475   };
2476 
2477   // For performance, use the *os_ directly for anything that doesn't need indentation
2478   // and prepare an indentation stream with default indentation 1.
2479   std::ostream* os_;
2480   VariableIndentationOutputStream vios_;
2481   ScopedIndentation indent1_;
2482 
2483   gc::space::ImageSpace& image_space_;
2484   const ImageHeader& image_header_;
2485   std::unique_ptr<OatDumper> oat_dumper_;
2486   OatDumperOptions* oat_dumper_options_;
2487 
2488   DISALLOW_COPY_AND_ASSIGN(ImageDumper);
2489 };
2490 
OpenOat(const std::string & oat_filename,const std::optional<std::string> & dex_filename,std::string * error_msg)2491 static std::unique_ptr<OatFile> OpenOat(const std::string& oat_filename,
2492                                         const std::optional<std::string>& dex_filename,
2493                                         std::string* error_msg) {
2494   if (!dex_filename.has_value()) {
2495     LOG(WARNING) << "No dex filename provided, "
2496                  << "oatdump might fail if the oat file does not contain the dex code.";
2497   }
2498   ArrayRef<const std::string> dex_filenames =
2499       dex_filename.has_value() ? ArrayRef<const std::string>(&dex_filename.value(), /*size=*/1) :
2500                                  ArrayRef<const std::string>();
2501   return std::unique_ptr<OatFile>(OatFile::Open(/*zip_fd=*/-1,
2502                                                 oat_filename,
2503                                                 oat_filename,
2504                                                 /*executable=*/false,
2505                                                 /*low_4gb=*/false,
2506                                                 dex_filenames,
2507                                                 /*dex_files=*/{},
2508                                                 /*reservation=*/nullptr,
2509                                                 error_msg));
2510 }
2511 
DumpImage(gc::space::ImageSpace * image_space,OatDumperOptions * options,std::ostream * os)2512 static int DumpImage(gc::space::ImageSpace* image_space,
2513                      OatDumperOptions* options,
2514                      std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
2515   const ImageHeader& image_header = image_space->GetImageHeader();
2516   if (!image_header.IsValid()) {
2517     LOG(ERROR) << "Invalid image header " << image_space->GetImageLocation();
2518     return EXIT_FAILURE;
2519   }
2520   ImageDumper image_dumper(os, *image_space, image_header, options);
2521   if (!image_dumper.Dump()) {
2522     return EXIT_FAILURE;
2523   }
2524   return EXIT_SUCCESS;
2525 }
2526 
DumpImages(Runtime * runtime,OatDumperOptions * options,std::ostream * os)2527 static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
2528   // Dumping the image, no explicit class loader.
2529   ScopedNullHandle<mirror::ClassLoader> null_class_loader;
2530   options->class_loader_ = &null_class_loader;
2531 
2532   ScopedObjectAccess soa(Thread::Current());
2533   if (options->app_image_ != nullptr) {
2534     if (!options->oat_filename_.has_value()) {
2535       LOG(ERROR) << "Can not dump app image without app oat file";
2536       return EXIT_FAILURE;
2537     }
2538     // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2539     // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2540     // pointers into 32 bit pointer sized ArtMethods.
2541     std::string error_msg;
2542     std::unique_ptr<OatFile> oat_file =
2543         OpenOat(*options->oat_filename_, options->dex_filename_, &error_msg);
2544     if (oat_file == nullptr) {
2545       LOG(ERROR) << "Failed to open oat file " << *options->oat_filename_ << " with error "
2546                  << error_msg;
2547       return EXIT_FAILURE;
2548     }
2549     std::unique_ptr<gc::space::ImageSpace> space(
2550         gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2551     if (space == nullptr) {
2552       LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2553                  << error_msg;
2554       return EXIT_FAILURE;
2555     }
2556     // Open dex files for the image.
2557     std::vector<std::unique_ptr<const DexFile>> dex_files;
2558     if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2559       LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2560                  << error_msg;
2561       return EXIT_FAILURE;
2562     }
2563     // Dump the actual image.
2564     return DumpImage(space.get(), options, os);
2565   }
2566 
2567   gc::Heap* heap = runtime->GetHeap();
2568   if (!heap->HasBootImageSpace()) {
2569     LOG(ERROR) << "No image spaces";
2570     return EXIT_FAILURE;
2571   }
2572   for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2573     int result = DumpImage(image_space, options, os);
2574     if (result != EXIT_SUCCESS) {
2575       return result;
2576     }
2577   }
2578   return EXIT_SUCCESS;
2579 }
2580 
InstallOatFile(Runtime * runtime,std::unique_ptr<OatFile> oat_file,std::vector<const DexFile * > * class_path)2581 static jobject InstallOatFile(Runtime* runtime,
2582                               std::unique_ptr<OatFile> oat_file,
2583                               std::vector<const DexFile*>* class_path)
2584     REQUIRES_SHARED(Locks::mutator_lock_) {
2585   Thread* self = Thread::Current();
2586   CHECK(self != nullptr);
2587   // Need well-known-classes.
2588   WellKnownClasses::Init(self->GetJniEnv());
2589 
2590   // Open dex files.
2591   OatFile* oat_file_ptr = oat_file.get();
2592   ClassLinker* class_linker = runtime->GetClassLinker();
2593   runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2594   for (const OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
2595     std::string error_msg;
2596     const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
2597     CHECK(dex_file != nullptr) << error_msg;
2598     class_path->push_back(dex_file);
2599   }
2600 
2601   // Need a class loader. Fake that we're a compiler.
2602   // Note: this will run initializers through the unstarted runtime, so make sure it's
2603   //       initialized.
2604   interpreter::UnstartedRuntime::Initialize();
2605 
2606   jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2607 
2608   // Need to register dex files to get a working dex cache.
2609   for (const DexFile* dex_file : *class_path) {
2610     ObjPtr<mirror::DexCache> dex_cache = class_linker->RegisterDexFile(
2611         *dex_file, self->DecodeJObject(class_loader)->AsClassLoader());
2612     CHECK(dex_cache != nullptr);
2613   }
2614 
2615   return class_loader;
2616 }
2617 
DumpOatWithRuntime(Runtime * runtime,std::unique_ptr<OatFile> oat_file,OatDumperOptions * options,std::ostream * os)2618 static int DumpOatWithRuntime(Runtime* runtime,
2619                               std::unique_ptr<OatFile> oat_file,
2620                               OatDumperOptions* options,
2621                               std::ostream* os) {
2622   CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2623   ScopedObjectAccess soa(Thread::Current());
2624 
2625   OatFile* oat_file_ptr = oat_file.get();
2626   std::vector<const DexFile*> class_path;
2627   jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
2628 
2629   // Use the class loader while dumping.
2630   StackHandleScope<1> scope(soa.Self());
2631   Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2632       soa.Decode<mirror::ClassLoader>(class_loader));
2633   options->class_loader_ = &loader_handle;
2634 
2635   OatDumper oat_dumper(*oat_file_ptr, *options);
2636   bool success = oat_dumper.Dump(*os);
2637   return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2638 }
2639 
DumpOatWithoutRuntime(OatFile * oat_file,OatDumperOptions * options,std::ostream * os)2640 static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
2641   CHECK(oat_file != nullptr && options != nullptr);
2642   // No image = no class loader.
2643   ScopedNullHandle<mirror::ClassLoader> null_class_loader;
2644   options->class_loader_ = &null_class_loader;
2645 
2646   OatDumper oat_dumper(*oat_file, *options);
2647   bool success = oat_dumper.Dump(*os);
2648   return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2649 }
2650 
DumpOat(Runtime * runtime,OatDumperOptions * options,std::ostream * os)2651 static int DumpOat(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
2652   std::string error_msg;
2653   std::unique_ptr<OatFile> oat_file =
2654       OpenOat(*options->oat_filename_, options->dex_filename_, &error_msg);
2655   if (oat_file == nullptr) {
2656     LOG(ERROR) << "Failed to open oat file from '" << *options->oat_filename_ << "': " << error_msg;
2657     return EXIT_FAILURE;
2658   }
2659 
2660   if (runtime != nullptr) {
2661     return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
2662   } else {
2663     return DumpOatWithoutRuntime(oat_file.get(), options, os);
2664   }
2665 }
2666 
SymbolizeOat(const char * oat_filename,const char * dex_filename,std::string & output_name,bool no_bits)2667 static int SymbolizeOat(const char* oat_filename,
2668                         const char* dex_filename,
2669                         std::string& output_name,
2670                         bool no_bits) {
2671   std::string error_msg;
2672   std::unique_ptr<OatFile> oat_file =
2673       OpenOat(oat_filename,
2674               dex_filename != nullptr ? std::make_optional(dex_filename) : std::nullopt,
2675               &error_msg);
2676   if (oat_file == nullptr) {
2677     LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
2678     return EXIT_FAILURE;
2679   }
2680 
2681   bool result;
2682   // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2683   // files for 64-bit code in the past.
2684   if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
2685     OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
2686     result = oat_symbolizer.Symbolize();
2687   } else {
2688     OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
2689     result = oat_symbolizer.Symbolize();
2690   }
2691   if (!result) {
2692     LOG(ERROR) << "Failed to symbolize";
2693     return EXIT_FAILURE;
2694   }
2695 
2696   return EXIT_SUCCESS;
2697 }
2698 
2699 class IMTDumper {
2700  public:
Dump(Runtime * runtime,const std::string & imt_file,bool dump_imt_stats,const char * oat_filename,const char * dex_filename)2701   static bool Dump(Runtime* runtime,
2702                    const std::string& imt_file,
2703                    bool dump_imt_stats,
2704                    const char* oat_filename,
2705                    const char* dex_filename) {
2706     Thread* self = Thread::Current();
2707 
2708     ScopedObjectAccess soa(self);
2709     StackHandleScope<1> scope(self);
2710     MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2711     std::vector<const DexFile*> class_path;
2712 
2713     if (oat_filename != nullptr) {
2714       std::string error_msg;
2715       std::unique_ptr<OatFile> oat_file =
2716           OpenOat(oat_filename,
2717                   dex_filename != nullptr ? std::make_optional(dex_filename) : std::nullopt,
2718                   &error_msg);
2719       if (oat_file == nullptr) {
2720         LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
2721         return false;
2722       }
2723 
2724       class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2725           InstallOatFile(runtime, std::move(oat_file), &class_path)));
2726     } else {
2727       class_loader.Assign(nullptr);  // Boot classloader. Just here for explicit documentation.
2728       class_path = runtime->GetClassLinker()->GetBootClassPath();
2729     }
2730 
2731     if (!imt_file.empty()) {
2732       return DumpImt(runtime, imt_file, class_loader);
2733     }
2734 
2735     if (dump_imt_stats) {
2736       return DumpImtStats(runtime, class_path, class_loader);
2737     }
2738 
2739     LOG(FATAL) << "Should not reach here";
2740     UNREACHABLE();
2741   }
2742 
2743  private:
DumpImt(Runtime * runtime,const std::string & imt_file,Handle<mirror::ClassLoader> h_class_loader)2744   static bool DumpImt(Runtime* runtime,
2745                       const std::string& imt_file,
2746                       Handle<mirror::ClassLoader> h_class_loader)
2747       REQUIRES_SHARED(Locks::mutator_lock_) {
2748     std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2749     std::unordered_set<std::string> prepared;
2750 
2751     for (const std::string& line : lines) {
2752       // A line should be either a class descriptor, in which case we will dump the complete IMT,
2753       // or a class descriptor and an interface method, in which case we will lookup the method,
2754       // determine its IMT slot, and check the class' IMT.
2755       size_t first_space = line.find(' ');
2756       if (first_space == std::string::npos) {
2757         DumpIMTForClass(runtime, line, h_class_loader, &prepared);
2758       } else {
2759         DumpIMTForMethod(runtime,
2760                          line.substr(0, first_space),
2761                          line.substr(first_space + 1, std::string::npos),
2762                          h_class_loader,
2763                          &prepared);
2764       }
2765       std::cerr << std::endl;
2766     }
2767 
2768     return true;
2769   }
2770 
DumpImtStats(Runtime * runtime,const std::vector<const DexFile * > & dex_files,Handle<mirror::ClassLoader> h_class_loader)2771   static bool DumpImtStats(Runtime* runtime,
2772                            const std::vector<const DexFile*>& dex_files,
2773                            Handle<mirror::ClassLoader> h_class_loader)
2774       REQUIRES_SHARED(Locks::mutator_lock_) {
2775     size_t without_imt = 0;
2776     size_t with_imt = 0;
2777     std::map<size_t, size_t> histogram;
2778 
2779     ClassLinker* class_linker = runtime->GetClassLinker();
2780     const PointerSize pointer_size = class_linker->GetImagePointerSize();
2781     std::unordered_set<std::string> prepared;
2782 
2783     Thread* self = Thread::Current();
2784     StackHandleScope<1> scope(self);
2785     MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
2786 
2787     for (const DexFile* dex_file : dex_files) {
2788       for (uint32_t class_def_index = 0;
2789            class_def_index != dex_file->NumClassDefs();
2790            ++class_def_index) {
2791         const dex::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
2792         const char* descriptor = dex_file->GetClassDescriptor(class_def);
2793         h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
2794         if (h_klass == nullptr) {
2795           std::cerr << "Warning: could not load " << descriptor << std::endl;
2796           continue;
2797         }
2798 
2799         if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
2800           without_imt++;
2801           continue;
2802         }
2803 
2804         ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
2805         if (im_table == nullptr) {
2806           // Should not happen, but accept.
2807           without_imt++;
2808           continue;
2809         }
2810 
2811         with_imt++;
2812         for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
2813           ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
2814           if (ptr->IsRuntimeMethod()) {
2815             if (ptr->IsImtUnimplementedMethod()) {
2816               histogram[0]++;
2817             } else {
2818               ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2819               histogram[current_table->NumEntries(pointer_size)]++;
2820             }
2821           } else {
2822             histogram[1]++;
2823           }
2824         }
2825       }
2826     }
2827 
2828     std::cerr << "IMT stats:"
2829               << std::endl << std::endl;
2830 
2831     std::cerr << "  " << with_imt << " classes with IMT."
2832               << std::endl << std::endl;
2833     std::cerr << "  " << without_imt << " classes without IMT (or copy from Object)."
2834               << std::endl << std::endl;
2835 
2836     double sum_one = 0;
2837     size_t count_one = 0;
2838 
2839     std::cerr << "  " << "IMT histogram" << std::endl;
2840     for (auto& bucket : histogram) {
2841       std::cerr << "    " << bucket.first << " " << bucket.second << std::endl;
2842       if (bucket.first > 0) {
2843         sum_one += bucket.second * bucket.first;
2844         count_one += bucket.second;
2845       }
2846     }
2847 
2848     double count_zero = count_one + histogram[0];
2849     std::cerr << "   Stats:" << std::endl;
2850     std::cerr << "     Average depth (including empty): " << (sum_one / count_zero) << std::endl;
2851     std::cerr << "     Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
2852 
2853     return true;
2854   }
2855 
2856   // Return whether the given class has no IMT (or the one shared with java.lang.Object).
HasNoIMT(Runtime * runtime,Handle<mirror::Class> klass,const PointerSize pointer_size,std::unordered_set<std::string> * prepared)2857   static bool HasNoIMT(Runtime* runtime,
2858                        Handle<mirror::Class> klass,
2859                        const PointerSize pointer_size,
2860                        std::unordered_set<std::string>* prepared)
2861       REQUIRES_SHARED(Locks::mutator_lock_) {
2862     if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
2863       return true;
2864     }
2865 
2866     if (klass->GetImt(pointer_size) == nullptr) {
2867       PrepareClass(runtime, klass, prepared);
2868     }
2869 
2870     ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>();
2871     DCHECK(object_class->IsObjectClass());
2872 
2873     bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
2874 
2875     if (klass->GetIfTable()->Count() == 0) {
2876       DCHECK(result);
2877     }
2878 
2879     return result;
2880   }
2881 
PrintTable(ImtConflictTable * table,PointerSize pointer_size)2882   static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
2883       REQUIRES_SHARED(Locks::mutator_lock_) {
2884     if (table == nullptr) {
2885       std::cerr << "    <No IMT?>" << std::endl;
2886       return;
2887     }
2888     size_t table_index = 0;
2889     for (;;) {
2890       ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
2891       if (ptr == nullptr) {
2892         return;
2893       }
2894       table_index++;
2895       std::cerr << "    " << ptr->PrettyMethod(true) << std::endl;
2896     }
2897   }
2898 
PrepareAndGetImTable(Runtime * runtime,Thread * self,Handle<mirror::ClassLoader> h_loader,const std::string & class_name,const PointerSize pointer_size,ObjPtr<mirror::Class> * klass_out,std::unordered_set<std::string> * prepared)2899   static ImTable* PrepareAndGetImTable(Runtime* runtime,
2900                                        Thread* self,
2901                                        Handle<mirror::ClassLoader> h_loader,
2902                                        const std::string& class_name,
2903                                        const PointerSize pointer_size,
2904                                        /*out*/ ObjPtr<mirror::Class>* klass_out,
2905                                        /*inout*/ std::unordered_set<std::string>* prepared)
2906       REQUIRES_SHARED(Locks::mutator_lock_) {
2907     if (class_name.empty()) {
2908       return nullptr;
2909     }
2910 
2911     std::string descriptor;
2912     if (class_name[0] == 'L') {
2913       descriptor = class_name;
2914     } else {
2915       descriptor = DotToDescriptor(class_name.c_str());
2916     }
2917 
2918     ObjPtr<mirror::Class> klass =
2919         runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
2920 
2921     if (klass == nullptr) {
2922       self->ClearException();
2923       std::cerr << "Did not find " <<  class_name << std::endl;
2924       *klass_out = nullptr;
2925       return nullptr;
2926     }
2927 
2928     StackHandleScope<1> scope(Thread::Current());
2929     Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
2930 
2931     ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
2932     *klass_out = h_klass.Get();
2933     return ret;
2934   }
2935 
PrepareAndGetImTable(Runtime * runtime,Handle<mirror::Class> h_klass,const PointerSize pointer_size,std::unordered_set<std::string> * prepared)2936   static ImTable* PrepareAndGetImTable(Runtime* runtime,
2937                                        Handle<mirror::Class> h_klass,
2938                                        const PointerSize pointer_size,
2939                                        /*inout*/ std::unordered_set<std::string>* prepared)
2940       REQUIRES_SHARED(Locks::mutator_lock_) {
2941     PrepareClass(runtime, h_klass, prepared);
2942     return h_klass->GetImt(pointer_size);
2943   }
2944 
DumpIMTForClass(Runtime * runtime,const std::string & class_name,Handle<mirror::ClassLoader> h_loader,std::unordered_set<std::string> * prepared)2945   static void DumpIMTForClass(Runtime* runtime,
2946                               const std::string& class_name,
2947                               Handle<mirror::ClassLoader> h_loader,
2948                               std::unordered_set<std::string>* prepared)
2949       REQUIRES_SHARED(Locks::mutator_lock_) {
2950     const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
2951     ObjPtr<mirror::Class> klass;
2952     ImTable* imt = PrepareAndGetImTable(runtime,
2953                                         Thread::Current(),
2954                                         h_loader,
2955                                         class_name,
2956                                         pointer_size,
2957                                         &klass,
2958                                         prepared);
2959     if (imt == nullptr) {
2960       return;
2961     }
2962 
2963     std::cerr << class_name << std::endl << " IMT:" << std::endl;
2964     for (size_t index = 0; index < ImTable::kSize; ++index) {
2965       std::cerr << "  " << index << ":" << std::endl;
2966       ArtMethod* ptr = imt->Get(index, pointer_size);
2967       if (ptr->IsRuntimeMethod()) {
2968         if (ptr->IsImtUnimplementedMethod()) {
2969           std::cerr << "    <empty>" << std::endl;
2970         } else {
2971           ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2972           PrintTable(current_table, pointer_size);
2973         }
2974       } else {
2975         std::cerr << "    " << ptr->PrettyMethod(true) << std::endl;
2976       }
2977     }
2978 
2979     std::cerr << " Interfaces:" << std::endl;
2980     // Run through iftable, find methods that slot here, see if they fit.
2981     ObjPtr<mirror::IfTable> if_table = klass->GetIfTable();
2982     for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
2983       ObjPtr<mirror::Class> iface = if_table->GetInterface(i);
2984       std::string iface_name;
2985       std::cerr << "  " << iface->GetDescriptor(&iface_name) << std::endl;
2986 
2987       for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
2988         uint32_t class_hash, name_hash, signature_hash;
2989         ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
2990         uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
2991         std::cerr << "    " << iface_method.PrettyMethod(true)
2992             << " slot=" << imt_slot
2993             << std::hex
2994             << " class_hash=0x" << class_hash
2995             << " name_hash=0x" << name_hash
2996             << " signature_hash=0x" << signature_hash
2997             << std::dec
2998             << std::endl;
2999       }
3000     }
3001   }
3002 
DumpIMTForMethod(Runtime * runtime,const std::string & class_name,const std::string & method,Handle<mirror::ClassLoader> h_loader,std::unordered_set<std::string> * prepared)3003   static void DumpIMTForMethod(Runtime* runtime,
3004                                const std::string& class_name,
3005                                const std::string& method,
3006                                Handle<mirror::ClassLoader> h_loader,
3007                                /*inout*/ std::unordered_set<std::string>* prepared)
3008       REQUIRES_SHARED(Locks::mutator_lock_) {
3009     const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3010     ObjPtr<mirror::Class> klass;
3011     ImTable* imt = PrepareAndGetImTable(runtime,
3012                                         Thread::Current(),
3013                                         h_loader,
3014                                         class_name,
3015                                         pointer_size,
3016                                         &klass,
3017                                         prepared);
3018     if (imt == nullptr) {
3019       return;
3020     }
3021 
3022     std::cerr << class_name << " <" << method << ">" << std::endl;
3023     for (size_t index = 0; index < ImTable::kSize; ++index) {
3024       ArtMethod* ptr = imt->Get(index, pointer_size);
3025       if (ptr->IsRuntimeMethod()) {
3026         if (ptr->IsImtUnimplementedMethod()) {
3027           continue;
3028         }
3029 
3030         ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3031         if (current_table == nullptr) {
3032           continue;
3033         }
3034 
3035         size_t table_index = 0;
3036         for (;;) {
3037           ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3038           if (ptr2 == nullptr) {
3039             break;
3040           }
3041           table_index++;
3042 
3043           std::string p_name = ptr2->PrettyMethod(true);
3044           if (p_name.starts_with(method)) {
3045             std::cerr << "  Slot "
3046                       << index
3047                       << " ("
3048                       << current_table->NumEntries(pointer_size)
3049                       << ")"
3050                       << std::endl;
3051             PrintTable(current_table, pointer_size);
3052             return;
3053           }
3054         }
3055       } else {
3056         std::string p_name = ptr->PrettyMethod(true);
3057         if (p_name.starts_with(method)) {
3058           std::cerr << "  Slot " << index << " (1)" << std::endl;
3059           std::cerr << "    " << p_name << std::endl;
3060         } else {
3061           // Run through iftable, find methods that slot here, see if they fit.
3062           ObjPtr<mirror::IfTable> if_table = klass->GetIfTable();
3063           for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3064             ObjPtr<mirror::Class> iface = if_table->GetInterface(i);
3065             size_t num_methods = iface->NumDeclaredVirtualMethods();
3066             if (num_methods > 0) {
3067               for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3068                 if (ImTable::GetImtIndex(&iface_method) == index) {
3069                   std::string i_name = iface_method.PrettyMethod(true);
3070                   if (i_name.starts_with(method)) {
3071                     std::cerr << "  Slot " << index << " (1)" << std::endl;
3072                     std::cerr << "    " << p_name << " (" << i_name << ")" << std::endl;
3073                   }
3074                 }
3075               }
3076             }
3077           }
3078         }
3079       }
3080     }
3081   }
3082 
3083   // Read lines from the given stream, dropping comments and empty lines
ReadCommentedInputStream(std::istream & in_stream)3084   static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3085     std::vector<std::string> output;
3086     while (in_stream.good()) {
3087       std::string dot;
3088       std::getline(in_stream, dot);
3089       if (dot.starts_with("#") || dot.empty()) {
3090         continue;
3091       }
3092       output.push_back(dot);
3093     }
3094     return output;
3095   }
3096 
3097   // Read lines from the given file, dropping comments and empty lines.
ReadCommentedInputFromFile(const std::string & input_filename)3098   static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3099     std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3100     if (input_file.get() == nullptr) {
3101       LOG(ERROR) << "Failed to open input file " << input_filename;
3102       return std::vector<std::string>();
3103     }
3104     std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3105     input_file->close();
3106     return result;
3107   }
3108 
3109   // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3110   // and note in the given set that the work was done.
PrepareClass(Runtime * runtime,Handle<mirror::Class> h_klass,std::unordered_set<std::string> * done)3111   static void PrepareClass(Runtime* runtime,
3112                            Handle<mirror::Class> h_klass,
3113                            /*inout*/ std::unordered_set<std::string>* done)
3114       REQUIRES_SHARED(Locks::mutator_lock_) {
3115     if (!h_klass->ShouldHaveImt()) {
3116       return;
3117     }
3118 
3119     std::string name;
3120     name = h_klass->GetDescriptor(&name);
3121 
3122     if (done->find(name) != done->end()) {
3123       return;
3124     }
3125     done->insert(name);
3126 
3127     if (h_klass->HasSuperClass()) {
3128       StackHandleScope<1> h(Thread::Current());
3129       PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3130     }
3131 
3132     if (!h_klass->IsTemp()) {
3133       runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3134     }
3135   }
3136 };
3137 
3138 enum class OatDumpMode {
3139   kSymbolize,
3140   kDumpImt,
3141   kDumpImage,
3142   kDumpOat,
3143 };
3144 
3145 struct OatdumpArgs : public CmdlineArgs {
3146  protected:
3147   using Base = CmdlineArgs;
3148 
ParseCustomart::OatdumpArgs3149   ParseStatus ParseCustom(const char* raw_option,
3150                           size_t raw_option_length,
3151                           std::string* error_msg) override {
3152     DCHECK_EQ(strlen(raw_option), raw_option_length);
3153     {
3154       ParseStatus base_parse = Base::ParseCustom(raw_option, raw_option_length, error_msg);
3155       if (base_parse != kParseUnknownArgument) {
3156         return base_parse;
3157       }
3158     }
3159 
3160     std::string_view option(raw_option, raw_option_length);
3161     if (option.starts_with("--oat-file=")) {
3162       oat_filename_ = raw_option + strlen("--oat-file=");
3163     } else if (option.starts_with("--dex-file=")) {
3164       dex_filename_ = raw_option + strlen("--dex-file=");
3165     } else if (option.starts_with("--image=")) {
3166       image_location_ = raw_option + strlen("--image=");
3167     } else if (option == "--no-dump:vmap") {
3168       dump_vmap_ = false;
3169     } else if (option =="--dump:code_info_stack_maps") {
3170       dump_code_info_stack_maps_ = true;
3171     } else if (option == "--no-disassemble") {
3172       disassemble_code_ = false;
3173     } else if (option =="--header-only") {
3174       dump_header_only_ = true;
3175     } else if (option.starts_with("--symbolize=")) {
3176       oat_filename_ = raw_option + strlen("--symbolize=");
3177       symbolize_ = true;
3178     } else if (option.starts_with("--only-keep-debug")) {
3179       only_keep_debug_ = true;
3180     } else if (option.starts_with("--class-filter=")) {
3181       class_filter_ = raw_option + strlen("--class-filter=");
3182     } else if (option.starts_with("--method-filter=")) {
3183       method_filter_ = raw_option + strlen("--method-filter=");
3184     } else if (option.starts_with("--list-classes")) {
3185       list_classes_ = true;
3186     } else if (option.starts_with("--list-methods")) {
3187       list_methods_ = true;
3188     } else if (option.starts_with("--export-dex-to=")) {
3189       export_dex_location_ = raw_option + strlen("--export-dex-to=");
3190     } else if (option.starts_with("--addr2instr=")) {
3191       if (!android::base::ParseUint(raw_option + strlen("--addr2instr="), &addr2instr_)) {
3192         *error_msg = "Address conversion failed";
3193         return kParseError;
3194       }
3195     } else if (option.starts_with("--app-image=")) {
3196       app_image_ = raw_option + strlen("--app-image=");
3197     } else if (option.starts_with("--app-oat=")) {
3198       app_oat_ = raw_option + strlen("--app-oat=");
3199     } else if (option.starts_with("--dump-imt=")) {
3200       imt_dump_ = std::string(option.substr(strlen("--dump-imt=")));
3201     } else if (option == "--dump-imt-stats") {
3202       imt_stat_dump_ = true;
3203     } else if (option == "--dump-method-and-offset-as-json") {
3204       dump_method_and_offset_as_json = true;
3205     } else {
3206       return kParseUnknownArgument;
3207     }
3208 
3209     return kParseOk;
3210   }
3211 
ParseChecksart::OatdumpArgs3212   ParseStatus ParseChecks(std::string* error_msg) override {
3213     if (image_location_ != nullptr) {
3214       if (!boot_image_locations_.empty()) {
3215         std::cerr << "Warning: Invalid combination of --boot-image and --image\n";
3216         std::cerr << "Use --image alone to dump boot image(s)\n";
3217         std::cerr << "Ignoring --boot-image\n";
3218         std::cerr << "\n";
3219         boot_image_locations_.clear();
3220       }
3221       Split(image_location_, ':', &boot_image_locations_);
3222     }
3223 
3224     // Perform the parent checks.
3225     ParseStatus parent_checks = Base::ParseChecks(error_msg);
3226     if (parent_checks != kParseOk) {
3227       return parent_checks;
3228     }
3229 
3230     // Perform our own checks.
3231     if (image_location_ == nullptr && app_image_ == nullptr && oat_filename_ == nullptr) {
3232       *error_msg = "Either --image, --app-image, --oat-file, or --symbolize must be specified";
3233       return kParseError;
3234     }
3235 
3236     if (app_image_ != nullptr && image_location_ != nullptr) {
3237       std::cerr << "Warning: Combining --app-image with --image is no longer supported\n";
3238       std::cerr << "Use --app-image alone to dump an app image, and optionally pass --boot-image "
3239                    "to specify the boot image that the app image is based on\n";
3240       std::cerr << "Use --image alone to dump boot image(s)\n";
3241       std::cerr << "Ignoring --image\n";
3242       std::cerr << "\n";
3243       image_location_ = nullptr;
3244     }
3245 
3246     if (image_location_ != nullptr && oat_filename_ != nullptr) {
3247       *error_msg =
3248           "--image and --oat-file must not be specified together\n"
3249           "Use --image alone to dump both boot image(s) and their oat file(s)\n"
3250           "Use --oat-file alone to dump an oat file";
3251       return kParseError;
3252     }
3253 
3254     if (app_oat_ != nullptr) {
3255       std::cerr << "Warning: --app-oat is deprecated. Use --oat-file instead\n";
3256       std::cerr << "\n";
3257       oat_filename_ = app_oat_;
3258     }
3259 
3260     if (boot_image_locations_.empty() && app_image_ != nullptr) {
3261       // At this point, boot image inference is impossible or has failed, and the user has been
3262       // warned about the failure.
3263       // When dumping an app image, we need at least one valid boot image, so we have to stop.
3264       // When dumping other things, we can continue to start the runtime in imageless mode.
3265       *error_msg = "--boot-image must be specified";
3266       return kParseError;
3267     }
3268 
3269     return kParseOk;
3270   }
3271 
GetUsageart::OatdumpArgs3272   std::string GetUsage() const override {
3273     std::string usage;
3274 
3275     usage += R"(
3276 Usage: oatdump [options] ...
3277 
3278 Examples:
3279 - Dump a primary boot image with its oat file.
3280     oatdump --image=/system/framework/boot.art
3281 
3282 - Dump a primary boot image and extension(s) with their oat files.
3283     oatdump --image=/system/framework/boot.art:/system/framework/boot-framework-adservices.art
3284 
3285 - Dump an app image with its oat file.
3286     oatdump --app-image=app.art --oat-file=app.odex [--dex-file=app.apk] [--boot-image=boot.art]
3287 
3288 - Dump an app oat file.
3289     oatdump --oat-file=app.odex [--dex-file=app.apk] [--boot-image=boot.art]
3290 
3291 - Dump IMT collisions. (See --dump-imt for details.)
3292     oatdump --oat-file=app.odex --dump-imt=imt.txt [--dex-file=app.apk] [--boot-image=boot.art]
3293         [--dump-imt-stats]
3294 
3295 - Symbolize an oat file. (See --symbolize for details.)
3296     oatdump --symbolize=app.odex [--dex-file=app.apk] [--only-keep-debug]
3297 
3298 Options:
3299   --oat-file=<file.oat>: dumps an oat file with the given filename.
3300       Example: --oat-file=/system/framework/arm64/boot.oat
3301 
3302   --image=<file.art>: dumps boot image(s) specified at the given location.
3303       Example: --image=/system/framework/boot.art
3304 
3305   --app-image=<file.art>: dumps an app image with the given filename.
3306       Must also have a specified app oat file (with --oat-file).
3307       Example: --app-image=app.art
3308 
3309   --app-oat=<file.odex>: deprecated. Use --oat-file instead.
3310 
3311 )";
3312 
3313     usage += Base::GetUsage();
3314 
3315     usage +=  // Optional.
3316         "  --no-dump:vmap may be used to disable vmap dumping.\n"
3317         "      Example: --no-dump:vmap\n"
3318         "\n"
3319         "  --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3320         "      Example: --dump:code_info_stack_maps\n"
3321         "\n"
3322         "  --no-disassemble may be used to disable disassembly.\n"
3323         "      Example: --no-disassemble\n"
3324         "\n"
3325         "  --header-only may be used to print only the oat header.\n"
3326         "      Example: --header-only\n"
3327         "\n"
3328         "  --list-classes may be used to list target file classes (can be used with filters).\n"
3329         "      Example: --list-classes\n"
3330         "      Example: --list-classes --class-filter=com.example.foo\n"
3331         "\n"
3332         "  --list-methods may be used to list target file methods (can be used with filters).\n"
3333         "      Example: --list-methods\n"
3334         "      Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3335         "\n"
3336         "  --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3337         "      Example: --symbolize=/system/framework/boot.oat\n"
3338         "\n"
3339         "  --only-keep-debug: modifies the behaviour of --symbolize so that\n"
3340         "      .rodata and .text sections are omitted in the output file to save space.\n"
3341         "      Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3342         "\n"
3343         "  --class-filter=<class name>: only dumps classes that contain the filter.\n"
3344         "      Example: --class-filter=com.example.foo\n"
3345         "\n"
3346         "  --method-filter=<method name>: only dumps methods that contain the filter.\n"
3347         "      Example: --method-filter=foo\n"
3348         "\n"
3349         "  --dump-method-and-offset-as-json: dumps fully qualified method names and\n"
3350         "                                    signatures ONLY, in a standard json format.\n"
3351         "      Example: --dump-method-and-offset-as-json\n"
3352         "\n"
3353         "  --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3354         "      Example: --export-dex-to=/data/local/tmp\n"
3355         "\n"
3356         "  --addr2instr=<address>: output matching method disassembled code from relative\n"
3357         "                          address (e.g. PC from crash dump)\n"
3358         "      Example: --addr2instr=0x00001a3b\n"
3359         "\n"
3360         "  --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3361         "                         types and interface methods in the given file. The file\n"
3362         "                         is read line-wise, where each line should either be a class\n"
3363         "                         name or descriptor, or a class name/descriptor and a prefix\n"
3364         "                         of a complete method name (separated by a whitespace).\n"
3365         "      Example: --dump-imt=imt.txt\n"
3366         "\n"
3367         "  --dump-imt-stats: modifies the behavior of --dump-imt to also output IMT statistics\n"
3368         "      for the boot image.\n"
3369         "      Example: --dump-imt-stats"
3370         "\n";
3371 
3372     return usage;
3373   }
3374 
3375  public:
GetModeart::OatdumpArgs3376   OatDumpMode GetMode() {
3377     // Keep the order of precedence for backward compatibility.
3378     if (symbolize_) {
3379       return OatDumpMode::kSymbolize;
3380     }
3381     if (!imt_dump_.empty()) {
3382       return OatDumpMode::kDumpImt;
3383     }
3384     if (image_location_ != nullptr || app_image_ != nullptr) {
3385       return OatDumpMode::kDumpImage;
3386     }
3387     CHECK_NE(oat_filename_, nullptr);
3388     return OatDumpMode::kDumpOat;
3389   }
3390 
3391   const char* oat_filename_ = nullptr;
3392   const char* dex_filename_ = nullptr;
3393   const char* class_filter_ = "";
3394   const char* method_filter_ = "";
3395   const char* image_location_ = nullptr;
3396   std::string elf_filename_prefix_;
3397   std::string imt_dump_;
3398   bool dump_vmap_ = true;
3399   bool dump_code_info_stack_maps_ = false;
3400   bool disassemble_code_ = true;
3401   bool symbolize_ = false;
3402   bool only_keep_debug_ = false;
3403   bool list_classes_ = false;
3404   bool list_methods_ = false;
3405   bool dump_header_only_ = false;
3406   bool imt_stat_dump_ = false;
3407   bool dump_method_and_offset_as_json = false;
3408   uint32_t addr2instr_ = 0;
3409   const char* export_dex_location_ = nullptr;
3410   const char* app_image_ = nullptr;
3411   const char* app_oat_ = nullptr;
3412 };
3413 
3414 struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
NeedsRuntimeart::OatdumpMain3415   bool NeedsRuntime() override {
3416     CHECK(args_ != nullptr);
3417 
3418     OatDumpMode mode = args_->GetMode();
3419 
3420     // Only enable absolute_addresses for image dumping.
3421     bool absolute_addresses = mode == OatDumpMode::kDumpImage;
3422 
3423     oat_dumper_options_.reset(new OatDumperOptions(args_->dump_vmap_,
3424                                                    args_->dump_code_info_stack_maps_,
3425                                                    args_->disassemble_code_,
3426                                                    absolute_addresses,
3427                                                    args_->class_filter_,
3428                                                    args_->method_filter_,
3429                                                    args_->list_classes_,
3430                                                    args_->list_methods_,
3431                                                    args_->dump_header_only_,
3432                                                    args_->dump_method_and_offset_as_json,
3433                                                    args_->export_dex_location_,
3434                                                    args_->app_image_,
3435                                                    args_->oat_filename_,
3436                                                    args_->dex_filename_,
3437                                                    args_->addr2instr_));
3438 
3439     switch (mode) {
3440       case OatDumpMode::kDumpImt:
3441       case OatDumpMode::kDumpImage:
3442         return true;
3443       case OatDumpMode::kSymbolize:
3444         return false;
3445       case OatDumpMode::kDumpOat:
3446         std::string error_msg;
3447         if (CanDumpWithRuntime(&error_msg)) {
3448           LOG(INFO) << "Dumping oat file with runtime";
3449           return true;
3450         } else {
3451           LOG(INFO) << ART_FORMAT("Cannot dump oat file with runtime: {}. Dumping without runtime",
3452                                   error_msg);
3453           return false;
3454         }
3455     }
3456   }
3457 
ExecuteWithoutRuntimeart::OatdumpMain3458   bool ExecuteWithoutRuntime() override {
3459     CHECK(args_ != nullptr);
3460 
3461     OatDumpMode mode = args_->GetMode();
3462     CHECK(mode == OatDumpMode::kSymbolize || mode == OatDumpMode::kDumpOat);
3463 
3464     MemMap::Init();
3465 
3466     if (mode == OatDumpMode::kSymbolize) {
3467       // ELF has special kind of section called SHT_NOBITS which allows us to create
3468       // sections which exist but their data is omitted from the ELF file to save space.
3469       // This is what "strip --only-keep-debug" does when it creates separate ELF file
3470       // with only debug data. We use it in similar way to exclude .rodata and .text.
3471       bool no_bits = args_->only_keep_debug_;
3472       return SymbolizeOat(
3473                  args_->oat_filename_, args_->dex_filename_, args_->output_name_, no_bits) ==
3474              EXIT_SUCCESS;
3475     }
3476 
3477     return DumpOat(nullptr, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
3478   }
3479 
ExecuteWithRuntimeart::OatdumpMain3480   bool ExecuteWithRuntime(Runtime* runtime) override {
3481     CHECK(args_ != nullptr);
3482     OatDumpMode mode = args_->GetMode();
3483     CHECK(mode == OatDumpMode::kDumpImt || mode == OatDumpMode::kDumpImage ||
3484           mode == OatDumpMode::kDumpOat);
3485 
3486     if (mode == OatDumpMode::kDumpImt) {
3487       return IMTDumper::Dump(runtime,
3488                              args_->imt_dump_,
3489                              args_->imt_stat_dump_,
3490                              args_->oat_filename_,
3491                              args_->dex_filename_);
3492     }
3493 
3494     if (mode == OatDumpMode::kDumpOat) {
3495       return DumpOat(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
3496     }
3497 
3498     return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
3499   }
3500 
CanDumpWithRuntimeart::OatdumpMain3501   bool CanDumpWithRuntime(std::string* error_msg) {
3502     std::unique_ptr<OatFileAssistantContext> ofa_context =
3503         args_->GetOatFileAssistantContext(error_msg);
3504     if (ofa_context == nullptr) {
3505       return false;
3506     }
3507 
3508     std::unique_ptr<OatFile> oat_file =
3509         OpenOat(*oat_dumper_options_->oat_filename_, oat_dumper_options_->dex_filename_, error_msg);
3510     if (oat_file == nullptr) {
3511       *error_msg = ART_FORMAT(
3512           "Failed to open oat file from '{}': {}", *oat_dumper_options_->oat_filename_, *error_msg);
3513       return false;
3514     }
3515 
3516     const std::vector<const OatDexFile*>& dex_files = oat_file->GetOatDexFiles();
3517     if (dex_files.empty()) {
3518       // Dump header only. Don't need a runtime.
3519       *error_msg = "No dex code";
3520       return false;
3521     }
3522 
3523     OatFileAssistant oat_file_assistant(dex_files[0]->GetLocation().c_str(),
3524                                         args_->instruction_set_,
3525                                         /*context=*/nullptr,
3526                                         /*load_executable=*/false,
3527                                         /*only_load_trusted_executable=*/false,
3528                                         ofa_context.get());
3529 
3530     if (!oat_file_assistant.ValidateBootClassPathChecksums(*oat_file)) {
3531       *error_msg = "BCP checksum check failed";
3532       return false;
3533     }
3534 
3535     return true;
3536   }
3537 
3538   std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3539 };
3540 
3541 }  // namespace art
3542 
main(int argc,char ** argv)3543 int main(int argc, char** argv) {
3544   // Output all logging to stderr.
3545   android::base::SetLogger(android::base::StderrLogger);
3546 
3547   art::OatdumpMain main;
3548   return main.Main(argc, argv);
3549 }
3550