David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | * Header file of the dexlayout utility. |
| 17 | * |
| 18 | * This is a tool to read dex files into an internal representation, |
| 19 | * reorganize the representation, and emit dex files with a better |
| 20 | * file layout. |
| 21 | */ |
| 22 | |
| 23 | #ifndef ART_DEXLAYOUT_DEXLAYOUT_H_ |
| 24 | #define ART_DEXLAYOUT_DEXLAYOUT_H_ |
| 25 | |
| 26 | #include <stdint.h> |
| 27 | #include <stdio.h> |
Vladimir Marko | 6c70224 | 2019-02-07 16:17:33 +0000 | [diff] [blame] | 28 | |
| 29 | #include <set> |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 30 | #include <unordered_map> |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 31 | |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 32 | #include "dex/compact_dex_level.h" |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 33 | #include "dex_container.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 34 | #include "dex/dex_file_layout.h" |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 35 | #include "dex_ir.h" |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 36 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 37 | namespace art { |
| 38 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 39 | class DexFile; |
| 40 | class Instruction; |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 41 | class ProfileCompilationInfo; |
| 42 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 43 | /* Supported output formats. */ |
| 44 | enum OutputFormat { |
| 45 | kOutputPlain = 0, // default |
| 46 | kOutputXml, // XML-style |
| 47 | }; |
| 48 | |
| 49 | /* Command-line options. */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 50 | class Options { |
| 51 | public: |
| 52 | Options() = default; |
| 53 | |
| 54 | bool dump_ = false; |
| 55 | bool build_dex_ir_ = false; |
| 56 | bool checksum_only_ = false; |
| 57 | bool disassemble_ = false; |
| 58 | bool exports_only_ = false; |
| 59 | bool ignore_bad_checksum_ = false; |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 60 | bool output_to_container_ = false; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 61 | bool show_annotations_ = false; |
| 62 | bool show_file_headers_ = false; |
| 63 | bool show_section_headers_ = false; |
David Sehr | 9335749 | 2017-03-09 08:02:44 -0800 | [diff] [blame] | 64 | bool show_section_statistics_ = false; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 65 | bool verbose_ = false; |
Mathieu Chartier | 2ef3b88 | 2017-10-20 19:50:39 -0700 | [diff] [blame] | 66 | bool verify_output_ = kIsDebugBuild; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 67 | bool visualize_pattern_ = false; |
Mathieu Chartier | 2c4b084 | 2017-12-13 11:49:51 -0800 | [diff] [blame] | 68 | bool update_checksum_ = false; |
Mathieu Chartier | 603ccab | 2017-10-20 14:34:28 -0700 | [diff] [blame] | 69 | CompactDexLevel compact_dex_level_ = CompactDexLevel::kCompactDexLevelNone; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 70 | OutputFormat output_format_ = kOutputPlain; |
| 71 | const char* output_dex_directory_ = nullptr; |
| 72 | const char* output_file_name_ = nullptr; |
| 73 | const char* profile_file_name_ = nullptr; |
Mathieu Chartier | 807b21b | 2018-01-29 05:18:24 -0800 | [diff] [blame] | 74 | // Filter that removes classes that don't have a matching descriptor (during IR creation). |
| 75 | // This speeds up cases when the output only requires a single class. |
Mathieu Chartier | 7517555 | 2018-01-25 11:23:01 -0800 | [diff] [blame] | 76 | std::set<std::string> class_filter_; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 79 | // Hotness info |
| 80 | class DexLayoutHotnessInfo { |
| 81 | public: |
| 82 | // Store layout information so that the offset calculation can specify the section sizes. |
| 83 | std::unordered_map<dex_ir::CodeItem*, LayoutType> code_item_layout_; |
| 84 | }; |
| 85 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 86 | class DexLayout { |
| 87 | public: |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 88 | class VectorOutputContainer { |
| 89 | public: |
| 90 | // Begin is not necessarily aligned (for now). |
| 91 | uint8_t* Begin() { |
| 92 | return &data_[0]; |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | std::vector<uint8_t> data_; |
| 97 | }; |
| 98 | |
| 99 | |
Mathieu Chartier | 2c4b084 | 2017-12-13 11:49:51 -0800 | [diff] [blame] | 100 | // Setting this to false disables class def layout entirely, which is stronger than strictly |
| 101 | // necessary to ensure the partial order w.r.t. class derivation. TODO: Re-enable (b/68317550). |
Nicolas Geoffray | 84caad6 | 2022-01-10 09:48:55 +0000 | [diff] [blame] | 102 | // This should never be set for a device build, as changing class defs ids |
| 103 | // conflict with profiles and verification passed by Play. |
Mathieu Chartier | 2c4b084 | 2017-12-13 11:49:51 -0800 | [diff] [blame] | 104 | static constexpr bool kChangeClassDefOrder = false; |
Nicolas Geoffray | 84caad6 | 2022-01-10 09:48:55 +0000 | [diff] [blame] | 105 | static_assert(!(kIsTargetBuild && kChangeClassDefOrder), "Never set class reordering on target"); |
Mathieu Chartier | 2c4b084 | 2017-12-13 11:49:51 -0800 | [diff] [blame] | 106 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 107 | DexLayout(Options& options, |
| 108 | ProfileCompilationInfo* info, |
| 109 | FILE* out_file, |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 110 | dex_ir::Header* header) |
| 111 | : options_(options), |
| 112 | info_(info), |
| 113 | out_file_(out_file), |
| 114 | header_(header) { } |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 115 | |
| 116 | int ProcessFile(const char* file_name); |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 117 | bool ProcessDexFile(const char* file_name, |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 118 | const DexFile* dex_file, |
| 119 | size_t dex_file_index, |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 120 | std::unique_ptr<DexContainer>* dex_container, |
| 121 | std::string* error_msg); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 122 | |
| 123 | dex_ir::Header* GetHeader() const { return header_; } |
| 124 | void SetHeader(dex_ir::Header* header) { header_ = header; } |
| 125 | |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 126 | DexLayoutSections& GetSections() { |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 127 | return dex_sections_; |
| 128 | } |
| 129 | |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 130 | const DexLayoutHotnessInfo& LayoutHotnessInfo() const { |
| 131 | return layout_hotness_info_; |
| 132 | } |
| 133 | |
Mathieu Chartier | 2c4b084 | 2017-12-13 11:49:51 -0800 | [diff] [blame] | 134 | const Options& GetOptions() const { |
| 135 | return options_; |
| 136 | } |
| 137 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 138 | private: |
| 139 | void DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item); |
| 140 | void DumpBytecodes(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset); |
| 141 | void DumpCatches(const dex_ir::CodeItem* code); |
| 142 | void DumpClass(int idx, char** last_package); |
| 143 | void DumpClassAnnotations(int idx); |
| 144 | void DumpClassDef(int idx); |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 145 | void DumpCode(uint32_t idx, |
| 146 | const dex_ir::CodeItem* code, |
| 147 | uint32_t code_offset, |
| 148 | const char* declaring_class_descriptor, |
| 149 | const char* method_name, |
| 150 | bool is_static, |
| 151 | const dex_ir::ProtoId* proto); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 152 | void DumpEncodedAnnotation(dex_ir::EncodedAnnotation* annotation); |
| 153 | void DumpEncodedValue(const dex_ir::EncodedValue* data); |
| 154 | void DumpFileHeader(); |
David Brazdil | 20c765f | 2018-10-27 21:45:15 +0000 | [diff] [blame] | 155 | void DumpIField(uint32_t idx, uint32_t flags, uint32_t hiddenapi_flags, int i); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 156 | void DumpInstruction(const dex_ir::CodeItem* code, |
| 157 | uint32_t code_offset, |
| 158 | uint32_t insn_idx, |
| 159 | uint32_t insn_width, |
| 160 | const Instruction* dec_insn); |
| 161 | void DumpInterface(const dex_ir::TypeId* type_item, int i); |
| 162 | void DumpLocalInfo(const dex_ir::CodeItem* code); |
David Brazdil | 20c765f | 2018-10-27 21:45:15 +0000 | [diff] [blame] | 163 | void DumpMethod(uint32_t idx, |
| 164 | uint32_t flags, |
| 165 | uint32_t hiddenapi_flags, |
| 166 | const dex_ir::CodeItem* code, |
| 167 | int i); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 168 | void DumpPositionInfo(const dex_ir::CodeItem* code); |
David Brazdil | 20c765f | 2018-10-27 21:45:15 +0000 | [diff] [blame] | 169 | void DumpSField(uint32_t idx, |
| 170 | uint32_t flags, |
| 171 | uint32_t hiddenapi_flags, |
| 172 | int i, |
| 173 | dex_ir::EncodedValue* init); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 174 | void DumpDexFile(); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 175 | |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 176 | void LayoutClassDefsAndClassData(const DexFile* dex_file); |
| 177 | void LayoutCodeItems(const DexFile* dex_file); |
| 178 | void LayoutStringData(const DexFile* dex_file); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 179 | |
| 180 | // Creates a new layout for the dex file based on profile info. |
| 181 | // Currently reorders ClassDefs, ClassDataItems, and CodeItems. |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 182 | void LayoutOutputFile(const DexFile* dex_file); |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 183 | bool OutputDexFile(const DexFile* input_dex_file, |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 184 | bool compute_offsets, |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 185 | std::unique_ptr<DexContainer>* dex_container, |
| 186 | std::string* error_msg); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 187 | |
| 188 | void DumpCFG(const DexFile* dex_file, int idx); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 189 | void DumpCFG(const DexFile* dex_file, uint32_t dex_method_idx, const dex::CodeItem* code); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 190 | |
| 191 | Options& options_; |
| 192 | ProfileCompilationInfo* info_; |
| 193 | FILE* out_file_; |
| 194 | dex_ir::Header* header_; |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 195 | DexLayoutSections dex_sections_; |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 196 | // Layout hotness information is only calculated when dexlayout is enabled. |
| 197 | DexLayoutHotnessInfo layout_hotness_info_; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 198 | |
| 199 | DISALLOW_COPY_AND_ASSIGN(DexLayout); |
| 200 | }; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 201 | |
| 202 | } // namespace art |
| 203 | |
| 204 | #endif // ART_DEXLAYOUT_DEXLAYOUT_H_ |