Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "compact_dex_writer.h" |
| 18 | |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 19 | #include "android-base/stringprintf.h" |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 20 | #include "base/logging.h" |
| 21 | #include "base/time_utils.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 22 | #include "dex/compact_dex_file.h" |
Mathieu Chartier | 5e3cfa2 | 2018-02-20 16:53:37 -0800 | [diff] [blame] | 23 | #include "dex/compact_offset_table.h" |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 24 | #include "dexlayout.h" |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 28 | CompactDexWriter::CompactDexWriter(DexLayout* dex_layout) |
| 29 | : DexWriter(dex_layout, /*compute_offsets*/ true) { |
| 30 | CHECK(GetCompactDexLevel() != CompactDexLevel::kCompactDexLevelNone); |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 31 | } |
| 32 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 33 | CompactDexLevel CompactDexWriter::GetCompactDexLevel() const { |
| 34 | return dex_layout_->GetOptions().compact_dex_level_; |
| 35 | } |
| 36 | |
| 37 | CompactDexWriter::Container::Container(bool dedupe_code_items) |
Mathieu Chartier | b81ecad | 2018-01-23 22:08:26 -0800 | [diff] [blame] | 38 | : code_item_dedupe_(dedupe_code_items, &data_section_), |
| 39 | data_item_dedupe_(/*dedupe*/ true, &data_section_) {} |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 40 | |
| 41 | uint32_t CompactDexWriter::WriteDebugInfoOffsetTable(Stream* stream) { |
| 42 | const uint32_t start_offset = stream->Tell(); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 43 | // Debug offsets for method indexes. 0 means no debug info. |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 44 | std::vector<uint32_t> debug_info_offsets(header_->MethodIds().Size(), 0u); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 45 | |
| 46 | static constexpr InvokeType invoke_types[] = { |
| 47 | kDirect, |
| 48 | kVirtual |
| 49 | }; |
| 50 | |
| 51 | for (InvokeType invoke_type : invoke_types) { |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 52 | for (auto& class_def : header_->ClassDefs()) { |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 53 | // Skip classes that are not defined in this dex file. |
| 54 | dex_ir::ClassData* class_data = class_def->GetClassData(); |
| 55 | if (class_data == nullptr) { |
| 56 | continue; |
| 57 | } |
| 58 | for (auto& method : *(invoke_type == InvokeType::kDirect |
| 59 | ? class_data->DirectMethods() |
| 60 | : class_data->VirtualMethods())) { |
David Sehr | d83437c | 2018-06-11 14:06:23 -0700 | [diff] [blame] | 61 | const dex_ir::MethodId* method_id = method.GetMethodId(); |
| 62 | dex_ir::CodeItem* code_item = method.GetCodeItem(); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 63 | if (code_item != nullptr && code_item->DebugInfo() != nullptr) { |
| 64 | const uint32_t debug_info_offset = code_item->DebugInfo()->GetOffset(); |
| 65 | const uint32_t method_idx = method_id->GetIndex(); |
| 66 | if (debug_info_offsets[method_idx] != 0u) { |
| 67 | CHECK_EQ(debug_info_offset, debug_info_offsets[method_idx]); |
| 68 | } |
| 69 | debug_info_offsets[method_idx] = debug_info_offset; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | std::vector<uint8_t> data; |
| 76 | debug_info_base_ = 0u; |
| 77 | debug_info_offsets_table_offset_ = 0u; |
Mathieu Chartier | 5e3cfa2 | 2018-02-20 16:53:37 -0800 | [diff] [blame] | 78 | CompactOffsetTable::Build(debug_info_offsets, |
| 79 | &data, |
| 80 | &debug_info_base_, |
| 81 | &debug_info_offsets_table_offset_); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 82 | // Align the table and write it out. |
Mathieu Chartier | 5e3cfa2 | 2018-02-20 16:53:37 -0800 | [diff] [blame] | 83 | stream->AlignTo(CompactOffsetTable::kAlignment); |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 84 | debug_info_offsets_pos_ = stream->Tell(); |
| 85 | stream->Write(data.data(), data.size()); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 86 | |
| 87 | // Verify that the whole table decodes as expected and measure average performance. |
| 88 | const bool kMeasureAndTestOutput = dex_layout_->GetOptions().verify_output_; |
| 89 | if (kMeasureAndTestOutput && !debug_info_offsets.empty()) { |
| 90 | uint64_t start_time = NanoTime(); |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 91 | stream->Begin(); |
Mathieu Chartier | 5e3cfa2 | 2018-02-20 16:53:37 -0800 | [diff] [blame] | 92 | CompactOffsetTable::Accessor accessor(stream->Begin() + debug_info_offsets_pos_, |
| 93 | debug_info_base_, |
| 94 | debug_info_offsets_table_offset_); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 95 | |
| 96 | for (size_t i = 0; i < debug_info_offsets.size(); ++i) { |
Mathieu Chartier | 5e3cfa2 | 2018-02-20 16:53:37 -0800 | [diff] [blame] | 97 | CHECK_EQ(accessor.GetOffset(i), debug_info_offsets[i]); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 98 | } |
| 99 | uint64_t end_time = NanoTime(); |
| 100 | VLOG(dex) << "Average lookup time (ns) for debug info offsets: " |
| 101 | << (end_time - start_time) / debug_info_offsets.size(); |
| 102 | } |
| 103 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 104 | return stream->Tell() - start_offset; |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Mathieu Chartier | b81ecad | 2018-01-23 22:08:26 -0800 | [diff] [blame] | 107 | CompactDexWriter::ScopedDataSectionItem::ScopedDataSectionItem(Stream* stream, |
| 108 | dex_ir::Item* item, |
| 109 | size_t alignment, |
| 110 | Deduper* deduper) |
| 111 | : stream_(stream), |
| 112 | item_(item), |
| 113 | alignment_(alignment), |
| 114 | deduper_(deduper), |
| 115 | start_offset_(stream->Tell()) { |
| 116 | stream_->AlignTo(alignment_); |
| 117 | } |
| 118 | |
| 119 | CompactDexWriter::ScopedDataSectionItem::~ScopedDataSectionItem() { |
| 120 | // After having written, maybe dedupe the whole code item (excluding padding). |
| 121 | const uint32_t deduped_offset = deduper_->Dedupe(start_offset_, |
| 122 | stream_->Tell(), |
| 123 | item_->GetOffset()); |
Mathieu Chartier | 807b21b | 2018-01-29 05:18:24 -0800 | [diff] [blame] | 124 | // If we deduped, only use the deduped offset if the alignment matches the required alignment. |
| 125 | // Otherwise, return without deduping. |
Mathieu Chartier | b81ecad | 2018-01-23 22:08:26 -0800 | [diff] [blame] | 126 | if (deduped_offset != Deduper::kDidNotDedupe && IsAlignedParam(deduped_offset, alignment_)) { |
Mathieu Chartier | 807b21b | 2018-01-29 05:18:24 -0800 | [diff] [blame] | 127 | // Update the IR offset to the offset of the deduped item. |
Mathieu Chartier | b81ecad | 2018-01-23 22:08:26 -0800 | [diff] [blame] | 128 | item_->SetOffset(deduped_offset); |
Mathieu Chartier | 807b21b | 2018-01-29 05:18:24 -0800 | [diff] [blame] | 129 | // Clear the written data for the item so that the stream write doesn't abort in the future. |
Mathieu Chartier | b81ecad | 2018-01-23 22:08:26 -0800 | [diff] [blame] | 130 | stream_->Clear(start_offset_, stream_->Tell() - start_offset_); |
Mathieu Chartier | 807b21b | 2018-01-29 05:18:24 -0800 | [diff] [blame] | 131 | // Since we deduped, restore the offset to the original position. |
Mathieu Chartier | b81ecad | 2018-01-23 22:08:26 -0800 | [diff] [blame] | 132 | stream_->Seek(start_offset_); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | size_t CompactDexWriter::ScopedDataSectionItem::Written() const { |
| 137 | return stream_->Tell() - start_offset_; |
| 138 | } |
| 139 | |
| 140 | void CompactDexWriter::WriteCodeItem(Stream* stream, |
| 141 | dex_ir::CodeItem* code_item, |
| 142 | bool reserve_only) { |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 143 | DCHECK(code_item != nullptr); |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 144 | DCHECK(!reserve_only) << "Not supported because of deduping."; |
Mathieu Chartier | b81ecad | 2018-01-23 22:08:26 -0800 | [diff] [blame] | 145 | ScopedDataSectionItem data_item(stream, |
| 146 | code_item, |
| 147 | CompactDexFile::CodeItem::kAlignment, |
| 148 | code_item_dedupe_); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 149 | |
| 150 | CompactDexFile::CodeItem disk_code_item; |
Mathieu Chartier | 8740c66 | 2018-01-11 14:50:02 -0800 | [diff] [blame] | 151 | |
| 152 | uint16_t preheader_storage[CompactDexFile::CodeItem::kMaxPreHeaderSize] = {}; |
| 153 | uint16_t* preheader_end = preheader_storage + CompactDexFile::CodeItem::kMaxPreHeaderSize; |
| 154 | const uint16_t* preheader = disk_code_item.Create( |
| 155 | code_item->RegistersSize(), |
| 156 | code_item->InsSize(), |
| 157 | code_item->OutsSize(), |
| 158 | code_item->TriesSize(), |
| 159 | code_item->InsnsSize(), |
| 160 | preheader_end); |
| 161 | const size_t preheader_bytes = (preheader_end - preheader) * sizeof(preheader[0]); |
| 162 | |
| 163 | static constexpr size_t kPayloadInstructionRequiredAlignment = 4; |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 164 | const uint32_t current_code_item_start = stream->Tell() + preheader_bytes; |
Mathieu Chartier | a27af08 | 2018-02-03 16:12:23 -0800 | [diff] [blame] | 165 | if (!IsAlignedParam(current_code_item_start, kPayloadInstructionRequiredAlignment) || |
| 166 | kIsDebugBuild) { |
Mathieu Chartier | 8740c66 | 2018-01-11 14:50:02 -0800 | [diff] [blame] | 167 | // If the preheader is going to make the code unaligned, consider adding 2 bytes of padding |
| 168 | // before if required. |
Mathieu Chartier | a27af08 | 2018-02-03 16:12:23 -0800 | [diff] [blame] | 169 | IterationRange<DexInstructionIterator> instructions = code_item->Instructions(); |
| 170 | SafeDexInstructionIterator it(instructions.begin(), instructions.end()); |
| 171 | for (; !it.IsErrorState() && it < instructions.end(); ++it) { |
| 172 | // In case the instruction goes past the end of the code item, make sure to not process it. |
| 173 | if (std::next(it).IsErrorState()) { |
| 174 | break; |
| 175 | } |
| 176 | const Instruction::Code opcode = it->Opcode(); |
Mathieu Chartier | 8740c66 | 2018-01-11 14:50:02 -0800 | [diff] [blame] | 177 | // Payload instructions possibly require special alignment for their data. |
| 178 | if (opcode == Instruction::FILL_ARRAY_DATA || |
| 179 | opcode == Instruction::PACKED_SWITCH || |
| 180 | opcode == Instruction::SPARSE_SWITCH) { |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 181 | stream->Skip( |
| 182 | RoundUp(current_code_item_start, kPayloadInstructionRequiredAlignment) - |
| 183 | current_code_item_start); |
Mathieu Chartier | 8740c66 | 2018-01-11 14:50:02 -0800 | [diff] [blame] | 184 | break; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // Write preheader first. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 190 | stream->Write(reinterpret_cast<const uint8_t*>(preheader), preheader_bytes); |
Mathieu Chartier | 8740c66 | 2018-01-11 14:50:02 -0800 | [diff] [blame] | 191 | // Registered offset is after the preheader. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 192 | ProcessOffset(stream, code_item); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 193 | // Avoid using sizeof so that we don't write the fake instruction array at the end of the code |
| 194 | // item. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 195 | stream->Write(&disk_code_item, OFFSETOF_MEMBER(CompactDexFile::CodeItem, insns_)); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 196 | // Write the instructions. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 197 | stream->Write(code_item->Insns(), code_item->InsnsSize() * sizeof(uint16_t)); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 198 | // Write the post instruction data. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 199 | WriteCodeItemPostInstructionData(stream, code_item, reserve_only); |
Mathieu Chartier | b81ecad | 2018-01-23 22:08:26 -0800 | [diff] [blame] | 200 | } |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 201 | |
Mathieu Chartier | b81ecad | 2018-01-23 22:08:26 -0800 | [diff] [blame] | 202 | void CompactDexWriter::WriteDebugInfoItem(Stream* stream, dex_ir::DebugInfoItem* debug_info) { |
| 203 | ScopedDataSectionItem data_item(stream, |
| 204 | debug_info, |
| 205 | SectionAlignment(DexFile::kDexTypeDebugInfoItem), |
| 206 | data_item_dedupe_); |
| 207 | ProcessOffset(stream, debug_info); |
| 208 | stream->Write(debug_info->GetDebugInfo(), debug_info->GetDebugInfoSize()); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 211 | |
| 212 | CompactDexWriter::Deduper::Deduper(bool enabled, DexContainer::Section* section) |
| 213 | : enabled_(enabled), |
| 214 | dedupe_map_(/*bucket_count*/ 32, |
| 215 | HashedMemoryRange::HashEqual(section), |
| 216 | HashedMemoryRange::HashEqual(section)) {} |
| 217 | |
| 218 | uint32_t CompactDexWriter::Deduper::Dedupe(uint32_t data_start, |
| 219 | uint32_t data_end, |
| 220 | uint32_t item_offset) { |
| 221 | if (!enabled_) { |
| 222 | return kDidNotDedupe; |
| 223 | } |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 224 | HashedMemoryRange range {data_start, data_end - data_start}; |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 225 | auto existing = dedupe_map_.emplace(range, item_offset); |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 226 | if (!existing.second) { |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 227 | // Failed to insert means we deduped, return the existing item offset. |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 228 | return existing.first->second; |
| 229 | } |
| 230 | return kDidNotDedupe; |
| 231 | } |
| 232 | |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 233 | void CompactDexWriter::SortDebugInfosByMethodIndex() { |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 234 | static constexpr InvokeType invoke_types[] = { |
| 235 | kDirect, |
| 236 | kVirtual |
| 237 | }; |
| 238 | std::map<const dex_ir::DebugInfoItem*, uint32_t> method_idx_map; |
| 239 | for (InvokeType invoke_type : invoke_types) { |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 240 | for (auto& class_def : header_->ClassDefs()) { |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 241 | // Skip classes that are not defined in this dex file. |
| 242 | dex_ir::ClassData* class_data = class_def->GetClassData(); |
| 243 | if (class_data == nullptr) { |
| 244 | continue; |
| 245 | } |
| 246 | for (auto& method : *(invoke_type == InvokeType::kDirect |
| 247 | ? class_data->DirectMethods() |
| 248 | : class_data->VirtualMethods())) { |
David Sehr | d83437c | 2018-06-11 14:06:23 -0700 | [diff] [blame] | 249 | const dex_ir::MethodId* method_id = method.GetMethodId(); |
| 250 | dex_ir::CodeItem* code_item = method.GetCodeItem(); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 251 | if (code_item != nullptr && code_item->DebugInfo() != nullptr) { |
| 252 | const dex_ir::DebugInfoItem* debug_item = code_item->DebugInfo(); |
| 253 | method_idx_map.insert(std::make_pair(debug_item, method_id->GetIndex())); |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | } |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 258 | std::sort(header_->DebugInfoItems().begin(), |
| 259 | header_->DebugInfoItems().end(), |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 260 | [&](const std::unique_ptr<dex_ir::DebugInfoItem>& a, |
| 261 | const std::unique_ptr<dex_ir::DebugInfoItem>& b) { |
| 262 | auto it_a = method_idx_map.find(a.get()); |
| 263 | auto it_b = method_idx_map.find(b.get()); |
| 264 | uint32_t idx_a = it_a != method_idx_map.end() ? it_a->second : 0u; |
| 265 | uint32_t idx_b = it_b != method_idx_map.end() ? it_b->second : 0u; |
| 266 | return idx_a < idx_b; |
| 267 | }); |
| 268 | } |
| 269 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 270 | void CompactDexWriter::WriteHeader(Stream* stream) { |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 271 | CompactDexFile::Header header; |
| 272 | CompactDexFile::WriteMagic(&header.magic_[0]); |
| 273 | CompactDexFile::WriteCurrentVersion(&header.magic_[0]); |
| 274 | header.checksum_ = header_->Checksum(); |
| 275 | std::copy_n(header_->Signature(), DexFile::kSha1DigestSize, header.signature_); |
| 276 | header.file_size_ = header_->FileSize(); |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 277 | // Since we are not necessarily outputting the same format as the input, avoid using the stored |
| 278 | // header size. |
| 279 | header.header_size_ = GetHeaderSize(); |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 280 | header.endian_tag_ = header_->EndianTag(); |
| 281 | header.link_size_ = header_->LinkSize(); |
| 282 | header.link_off_ = header_->LinkOffset(); |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 283 | header.map_off_ = header_->MapListOffset(); |
| 284 | header.string_ids_size_ = header_->StringIds().Size(); |
| 285 | header.string_ids_off_ = header_->StringIds().GetOffset(); |
| 286 | header.type_ids_size_ = header_->TypeIds().Size(); |
| 287 | header.type_ids_off_ = header_->TypeIds().GetOffset(); |
| 288 | header.proto_ids_size_ = header_->ProtoIds().Size(); |
| 289 | header.proto_ids_off_ = header_->ProtoIds().GetOffset(); |
| 290 | header.field_ids_size_ = header_->FieldIds().Size(); |
| 291 | header.field_ids_off_ = header_->FieldIds().GetOffset(); |
| 292 | header.method_ids_size_ = header_->MethodIds().Size(); |
| 293 | header.method_ids_off_ = header_->MethodIds().GetOffset(); |
| 294 | header.class_defs_size_ = header_->ClassDefs().Size(); |
| 295 | header.class_defs_off_ = header_->ClassDefs().GetOffset(); |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 296 | header.data_size_ = header_->DataSize(); |
| 297 | header.data_off_ = header_->DataOffset(); |
Mathieu Chartier | c17b7d8 | 2018-03-14 14:00:04 -0700 | [diff] [blame] | 298 | header.owned_data_begin_ = owned_data_begin_; |
| 299 | header.owned_data_end_ = owned_data_end_; |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 300 | |
| 301 | // Compact dex specific flags. |
| 302 | header.debug_info_offsets_pos_ = debug_info_offsets_pos_; |
| 303 | header.debug_info_offsets_table_offset_ = debug_info_offsets_table_offset_; |
| 304 | header.debug_info_base_ = debug_info_base_; |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 305 | header.feature_flags_ = 0u; |
| 306 | // In cases where apps are converted to cdex during install, maintain feature flags so that |
| 307 | // the verifier correctly verifies apps that aren't targetting default methods. |
| 308 | if (header_->SupportDefaultMethods()) { |
| 309 | header.feature_flags_ |= static_cast<uint32_t>(CompactDexFile::FeatureFlags::kDefaultMethods); |
| 310 | } |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 311 | stream->Seek(0); |
| 312 | stream->Overwrite(reinterpret_cast<uint8_t*>(&header), sizeof(header)); |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 315 | size_t CompactDexWriter::GetHeaderSize() const { |
| 316 | return sizeof(CompactDexFile::Header); |
| 317 | } |
| 318 | |
Mathieu Chartier | b81ecad | 2018-01-23 22:08:26 -0800 | [diff] [blame] | 319 | void CompactDexWriter::WriteStringData(Stream* stream, dex_ir::StringData* string_data) { |
| 320 | ScopedDataSectionItem data_item(stream, |
| 321 | string_data, |
| 322 | SectionAlignment(DexFile::kDexTypeStringDataItem), |
| 323 | data_item_dedupe_); |
| 324 | ProcessOffset(stream, string_data); |
| 325 | stream->WriteUleb128(CountModifiedUtf8Chars(string_data->Data())); |
| 326 | stream->Write(string_data->Data(), strlen(string_data->Data())); |
| 327 | // Skip null terminator (already zeroed out, no need to write). |
| 328 | stream->Skip(1); |
| 329 | } |
| 330 | |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 331 | bool CompactDexWriter::CanGenerateCompactDex(std::string* error_msg) { |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 332 | static constexpr InvokeType invoke_types[] = { |
| 333 | kDirect, |
| 334 | kVirtual |
| 335 | }; |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 336 | std::vector<bool> saw_method_id(header_->MethodIds().Size(), false); |
| 337 | std::vector<dex_ir::CodeItem*> method_id_code_item(header_->MethodIds().Size(), nullptr); |
| 338 | std::vector<dex_ir::DebugInfoItem*> method_id_debug_info(header_->MethodIds().Size(), nullptr); |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 339 | for (InvokeType invoke_type : invoke_types) { |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 340 | for (auto& class_def : header_->ClassDefs()) { |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 341 | // Skip classes that are not defined in this dex file. |
| 342 | dex_ir::ClassData* class_data = class_def->GetClassData(); |
| 343 | if (class_data == nullptr) { |
| 344 | continue; |
| 345 | } |
| 346 | for (auto& method : *(invoke_type == InvokeType::kDirect |
| 347 | ? class_data->DirectMethods() |
| 348 | : class_data->VirtualMethods())) { |
David Sehr | d83437c | 2018-06-11 14:06:23 -0700 | [diff] [blame] | 349 | const uint32_t idx = method.GetMethodId()->GetIndex(); |
| 350 | dex_ir::CodeItem* code_item = method.GetCodeItem(); |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 351 | dex_ir:: DebugInfoItem* debug_info_item = nullptr; |
| 352 | if (code_item != nullptr) { |
| 353 | debug_info_item = code_item->DebugInfo(); |
| 354 | } |
| 355 | if (saw_method_id[idx]) { |
| 356 | if (method_id_code_item[idx] != code_item) { |
| 357 | *error_msg = android::base::StringPrintf("Conflicting code item for method id %u", |
| 358 | idx); |
| 359 | // Conflicting info, abort generation. |
| 360 | return false; |
| 361 | } |
| 362 | if (method_id_debug_info[idx] != debug_info_item) { |
| 363 | *error_msg = android::base::StringPrintf("Conflicting debug info for method id %u", |
| 364 | idx); |
| 365 | // Conflicting info, abort generation. |
| 366 | return false; |
| 367 | } |
| 368 | } |
| 369 | method_id_code_item[idx] = code_item; |
| 370 | method_id_debug_info[idx] = debug_info_item; |
| 371 | saw_method_id[idx] = true; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | return true; |
| 376 | } |
| 377 | |
| 378 | bool CompactDexWriter::Write(DexContainer* output, std::string* error_msg) { |
| 379 | DCHECK(error_msg != nullptr); |
Mathieu Chartier | 9b302bf | 2018-01-25 13:08:08 -0800 | [diff] [blame] | 380 | CHECK(compute_offsets_); |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 381 | CHECK(output->IsCompactDexContainer()); |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 382 | |
| 383 | if (!CanGenerateCompactDex(error_msg)) { |
| 384 | return false; |
| 385 | } |
| 386 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 387 | Container* const container = down_cast<Container*>(output); |
| 388 | // For now, use the same stream for both data and metadata. |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 389 | Stream temp_main_stream(output->GetMainSection()); |
Mathieu Chartier | b81ecad | 2018-01-23 22:08:26 -0800 | [diff] [blame] | 390 | CHECK_EQ(output->GetMainSection()->Size(), 0u); |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 391 | Stream temp_data_stream(output->GetDataSection()); |
| 392 | Stream* main_stream = &temp_main_stream; |
| 393 | Stream* data_stream = &temp_data_stream; |
| 394 | |
| 395 | // We want offset 0 to be reserved for null, seek to the data section alignment or the end of the |
| 396 | // section. |
| 397 | data_stream->Seek(std::max( |
| 398 | static_cast<uint32_t>(output->GetDataSection()->Size()), |
| 399 | kDataSectionAlignment)); |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 400 | code_item_dedupe_ = &container->code_item_dedupe_; |
Mathieu Chartier | b81ecad | 2018-01-23 22:08:26 -0800 | [diff] [blame] | 401 | data_item_dedupe_ = &container->data_item_dedupe_; |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 402 | |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 403 | // Starting offset is right after the header. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 404 | main_stream->Seek(GetHeaderSize()); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 405 | |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 406 | // Based on: https://source.android.com/devices/tech/dalvik/dex-format |
| 407 | // Since the offsets may not be calculated already, the writing must be done in the correct order. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 408 | const uint32_t string_ids_offset = main_stream->Tell(); |
| 409 | WriteStringIds(main_stream, /*reserve_only*/ true); |
| 410 | WriteTypeIds(main_stream); |
| 411 | const uint32_t proto_ids_offset = main_stream->Tell(); |
| 412 | WriteProtoIds(main_stream, /*reserve_only*/ true); |
| 413 | WriteFieldIds(main_stream); |
| 414 | WriteMethodIds(main_stream); |
| 415 | const uint32_t class_defs_offset = main_stream->Tell(); |
| 416 | WriteClassDefs(main_stream, /*reserve_only*/ true); |
| 417 | const uint32_t call_site_ids_offset = main_stream->Tell(); |
| 418 | WriteCallSiteIds(main_stream, /*reserve_only*/ true); |
| 419 | WriteMethodHandles(main_stream); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 420 | |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 421 | if (compute_offsets_) { |
| 422 | // Data section. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 423 | data_stream->AlignTo(kDataSectionAlignment); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 424 | } |
Mathieu Chartier | c17b7d8 | 2018-03-14 14:00:04 -0700 | [diff] [blame] | 425 | owned_data_begin_ = data_stream->Tell(); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 426 | |
| 427 | // Write code item first to minimize the space required for encoded methods. |
| 428 | // For cdex, the code items don't depend on the debug info. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 429 | WriteCodeItems(data_stream, /*reserve_only*/ false); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 430 | |
| 431 | // Sort the debug infos by method index order, this reduces size by ~0.1% by reducing the size of |
| 432 | // the debug info offset table. |
| 433 | SortDebugInfosByMethodIndex(); |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 434 | WriteDebugInfoItems(data_stream); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 435 | |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 436 | WriteEncodedArrays(data_stream); |
| 437 | WriteAnnotations(data_stream); |
| 438 | WriteAnnotationSets(data_stream); |
| 439 | WriteAnnotationSetRefs(data_stream); |
| 440 | WriteAnnotationsDirectories(data_stream); |
| 441 | WriteTypeLists(data_stream); |
| 442 | WriteClassDatas(data_stream); |
| 443 | WriteStringDatas(data_stream); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 444 | |
| 445 | // Write delayed id sections that depend on data sections. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 446 | { |
| 447 | Stream::ScopedSeek seek(main_stream, string_ids_offset); |
| 448 | WriteStringIds(main_stream, /*reserve_only*/ false); |
| 449 | } |
| 450 | { |
| 451 | Stream::ScopedSeek seek(main_stream, proto_ids_offset); |
| 452 | WriteProtoIds(main_stream, /*reserve_only*/ false); |
| 453 | } |
| 454 | { |
| 455 | Stream::ScopedSeek seek(main_stream, class_defs_offset); |
| 456 | WriteClassDefs(main_stream, /*reserve_only*/ false); |
| 457 | } |
| 458 | { |
| 459 | Stream::ScopedSeek seek(main_stream, call_site_ids_offset); |
| 460 | WriteCallSiteIds(main_stream, /*reserve_only*/ false); |
| 461 | } |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 462 | |
| 463 | // Write the map list. |
| 464 | if (compute_offsets_) { |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 465 | data_stream->AlignTo(SectionAlignment(DexFile::kDexTypeMapList)); |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 466 | header_->SetMapListOffset(data_stream->Tell()); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 467 | } else { |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 468 | data_stream->Seek(header_->MapListOffset()); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 469 | } |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 470 | |
| 471 | // Map items are included in the data section. |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 472 | GenerateAndWriteMapItems(data_stream); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 473 | |
| 474 | // Write link data if it exists. |
David Sehr | 2b5a38f | 2018-06-14 15:13:04 -0700 | [diff] [blame] | 475 | const std::vector<uint8_t>& link_data = header_->LinkData(); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 476 | if (link_data.size() > 0) { |
| 477 | CHECK_EQ(header_->LinkSize(), static_cast<uint32_t>(link_data.size())); |
| 478 | if (compute_offsets_) { |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 479 | header_->SetLinkOffset(data_stream->Tell()); |
| 480 | } else { |
| 481 | data_stream->Seek(header_->LinkOffset()); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 482 | } |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 483 | data_stream->Write(&link_data[0], link_data.size()); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | // Write debug info offset table last to make dex file verifier happy. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 487 | WriteDebugInfoOffsetTable(data_stream); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 488 | |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 489 | data_stream->AlignTo(kDataSectionAlignment); |
Mathieu Chartier | c17b7d8 | 2018-03-14 14:00:04 -0700 | [diff] [blame] | 490 | owned_data_end_ = data_stream->Tell(); |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 491 | if (compute_offsets_) { |
| 492 | header_->SetDataSize(data_stream->Tell()); |
| 493 | if (header_->DataSize() != 0) { |
| 494 | // Offset must be zero when the size is zero. |
| 495 | main_stream->AlignTo(kDataSectionAlignment); |
| 496 | // For now, default to saying the data is right after the main stream. |
| 497 | header_->SetDataOffset(main_stream->Tell()); |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 498 | } else { |
| 499 | header_->SetDataOffset(0u); |
| 500 | } |
| 501 | } |
| 502 | |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 503 | // Write header last. |
| 504 | if (compute_offsets_) { |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 505 | header_->SetFileSize(main_stream->Tell()); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 506 | } |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 507 | WriteHeader(main_stream); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 508 | |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 509 | // Trim sections to make sure they are sized properly. |
| 510 | output->GetMainSection()->Resize(header_->FileSize()); |
| 511 | output->GetDataSection()->Resize(data_stream->Tell()); |
| 512 | |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 513 | if (dex_layout_->GetOptions().update_checksum_) { |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 514 | // Compute the cdex section (also covers the used part of the data section). |
| 515 | header_->SetChecksum(CompactDexFile::CalculateChecksum(output->GetMainSection()->Begin(), |
| 516 | output->GetMainSection()->Size(), |
| 517 | output->GetDataSection()->Begin(), |
| 518 | output->GetDataSection()->Size())); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 519 | // Rewrite the header with the calculated checksum. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 520 | WriteHeader(main_stream); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 521 | } |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 522 | |
| 523 | // Clear the dedupe to prevent interdex code item deduping. This does not currently work well with |
| 524 | // dex2oat's class unloading. The issue is that verification encounters quickened opcodes after |
| 525 | // the first dex gets unloaded. |
| 526 | code_item_dedupe_->Clear(); |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 527 | |
| 528 | return true; |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | std::unique_ptr<DexContainer> CompactDexWriter::CreateDexContainer() const { |
| 532 | return std::unique_ptr<DexContainer>( |
| 533 | new CompactDexWriter::Container(dex_layout_->GetOptions().dedupe_code_items_)); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 534 | } |
| 535 | |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 536 | } // namespace art |