Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | * Implementation file of the dexlayout utility. |
| 17 | * |
| 18 | * This is a tool to read dex files into an internal representation, |
| 19 | * reorganize the representation, and emit dex files with a better |
| 20 | * file layout. |
| 21 | */ |
| 22 | |
| 23 | #include "dex_ir.h" |
David Sehr | d1e44e2 | 2016-10-06 17:09:32 -0700 | [diff] [blame] | 24 | #include "dex_instruction-inl.h" |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 25 | #include "dex_ir_builder.h" |
| 26 | |
| 27 | namespace art { |
| 28 | namespace dex_ir { |
| 29 | |
| 30 | static uint64_t ReadVarWidth(const uint8_t** data, uint8_t length, bool sign_extend) { |
| 31 | uint64_t value = 0; |
| 32 | for (uint32_t i = 0; i <= length; i++) { |
| 33 | value |= static_cast<uint64_t>(*(*data)++) << (i * 8); |
| 34 | } |
| 35 | if (sign_extend) { |
| 36 | int shift = (7 - length) * 8; |
| 37 | return (static_cast<int64_t>(value) << shift) >> shift; |
| 38 | } |
| 39 | return value; |
| 40 | } |
| 41 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 42 | static uint32_t GetDebugInfoStreamSize(const uint8_t* debug_info_stream) { |
| 43 | const uint8_t* stream = debug_info_stream; |
| 44 | DecodeUnsignedLeb128(&stream); // line_start |
| 45 | uint32_t parameters_size = DecodeUnsignedLeb128(&stream); |
| 46 | for (uint32_t i = 0; i < parameters_size; ++i) { |
| 47 | DecodeUnsignedLeb128P1(&stream); // Parameter name. |
| 48 | } |
| 49 | |
| 50 | for (;;) { |
| 51 | uint8_t opcode = *stream++; |
| 52 | switch (opcode) { |
| 53 | case DexFile::DBG_END_SEQUENCE: |
| 54 | return stream - debug_info_stream; // end of stream. |
| 55 | case DexFile::DBG_ADVANCE_PC: |
| 56 | DecodeUnsignedLeb128(&stream); // addr_diff |
| 57 | break; |
| 58 | case DexFile::DBG_ADVANCE_LINE: |
| 59 | DecodeSignedLeb128(&stream); // line_diff |
| 60 | break; |
| 61 | case DexFile::DBG_START_LOCAL: |
| 62 | DecodeUnsignedLeb128(&stream); // register_num |
| 63 | DecodeUnsignedLeb128P1(&stream); // name_idx |
| 64 | DecodeUnsignedLeb128P1(&stream); // type_idx |
| 65 | break; |
| 66 | case DexFile::DBG_START_LOCAL_EXTENDED: |
| 67 | DecodeUnsignedLeb128(&stream); // register_num |
| 68 | DecodeUnsignedLeb128P1(&stream); // name_idx |
| 69 | DecodeUnsignedLeb128P1(&stream); // type_idx |
| 70 | DecodeUnsignedLeb128P1(&stream); // sig_idx |
| 71 | break; |
| 72 | case DexFile::DBG_END_LOCAL: |
| 73 | case DexFile::DBG_RESTART_LOCAL: |
| 74 | DecodeUnsignedLeb128(&stream); // register_num |
| 75 | break; |
| 76 | case DexFile::DBG_SET_PROLOGUE_END: |
| 77 | case DexFile::DBG_SET_EPILOGUE_BEGIN: |
| 78 | break; |
| 79 | case DexFile::DBG_SET_FILE: { |
| 80 | DecodeUnsignedLeb128P1(&stream); // name_idx |
| 81 | break; |
| 82 | } |
| 83 | default: { |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
David Sehr | d1e44e2 | 2016-10-06 17:09:32 -0700 | [diff] [blame] | 90 | static bool GetIdFromInstruction(Collections& collections, |
| 91 | const Instruction* dec_insn, |
| 92 | std::vector<TypeId*>* type_ids, |
| 93 | std::vector<StringId*>* string_ids, |
| 94 | std::vector<MethodId*>* method_ids, |
| 95 | std::vector<FieldId*>* field_ids) { |
| 96 | // Determine index and width of the string. |
| 97 | uint32_t index = 0; |
| 98 | switch (Instruction::FormatOf(dec_insn->Opcode())) { |
| 99 | // SOME NOT SUPPORTED: |
| 100 | // case Instruction::k20bc: |
| 101 | case Instruction::k21c: |
| 102 | case Instruction::k35c: |
| 103 | // case Instruction::k35ms: |
| 104 | case Instruction::k3rc: |
| 105 | // case Instruction::k3rms: |
| 106 | // case Instruction::k35mi: |
| 107 | // case Instruction::k3rmi: |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 108 | case Instruction::k45cc: |
| 109 | case Instruction::k4rcc: |
David Sehr | d1e44e2 | 2016-10-06 17:09:32 -0700 | [diff] [blame] | 110 | index = dec_insn->VRegB(); |
| 111 | break; |
| 112 | case Instruction::k31c: |
| 113 | index = dec_insn->VRegB(); |
| 114 | break; |
| 115 | case Instruction::k22c: |
| 116 | // case Instruction::k22cs: |
| 117 | index = dec_insn->VRegC(); |
| 118 | break; |
| 119 | default: |
| 120 | break; |
| 121 | } // switch |
| 122 | |
| 123 | // Determine index type, and add reference to the appropriate collection. |
| 124 | switch (Instruction::IndexTypeOf(dec_insn->Opcode())) { |
| 125 | case Instruction::kIndexTypeRef: |
| 126 | if (index < collections.TypeIdsSize()) { |
| 127 | type_ids->push_back(collections.GetTypeId(index)); |
| 128 | return true; |
| 129 | } |
| 130 | break; |
| 131 | case Instruction::kIndexStringRef: |
| 132 | if (index < collections.StringIdsSize()) { |
| 133 | string_ids->push_back(collections.GetStringId(index)); |
| 134 | return true; |
| 135 | } |
| 136 | break; |
| 137 | case Instruction::kIndexMethodRef: |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 138 | case Instruction::kIndexMethodAndProtoRef: |
David Sehr | d1e44e2 | 2016-10-06 17:09:32 -0700 | [diff] [blame] | 139 | if (index < collections.MethodIdsSize()) { |
| 140 | method_ids->push_back(collections.GetMethodId(index)); |
| 141 | return true; |
| 142 | } |
| 143 | break; |
| 144 | case Instruction::kIndexFieldRef: |
| 145 | if (index < collections.FieldIdsSize()) { |
| 146 | field_ids->push_back(collections.GetFieldId(index)); |
| 147 | return true; |
| 148 | } |
| 149 | break; |
| 150 | case Instruction::kIndexUnknown: |
| 151 | case Instruction::kIndexNone: |
| 152 | case Instruction::kIndexVtableOffset: |
| 153 | case Instruction::kIndexFieldOffset: |
| 154 | default: |
| 155 | break; |
| 156 | } // switch |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Get all the types, strings, methods, and fields referred to from bytecode. |
| 162 | */ |
| 163 | static bool GetIdsFromByteCode(Collections& collections, |
| 164 | const CodeItem* code, |
| 165 | std::vector<TypeId*>* type_ids, |
| 166 | std::vector<StringId*>* string_ids, |
| 167 | std::vector<MethodId*>* method_ids, |
| 168 | std::vector<FieldId*>* field_ids) { |
| 169 | bool has_id = false; |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 170 | IterationRange<DexInstructionIterator> instructions = code->Instructions(); |
| 171 | SafeDexInstructionIterator it(instructions.begin(), instructions.end()); |
| 172 | for (; !it.IsErrorState() && it < instructions.end(); ++it) { |
| 173 | // In case the instruction goes past the end of the code item, make sure to not process it. |
| 174 | SafeDexInstructionIterator next = it; |
| 175 | ++next; |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 176 | if (next.IsErrorState()) { |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 177 | break; |
| 178 | } |
David Sehr | d1e44e2 | 2016-10-06 17:09:32 -0700 | [diff] [blame] | 179 | has_id |= GetIdFromInstruction(collections, |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 180 | &it.Inst(), |
David Sehr | d1e44e2 | 2016-10-06 17:09:32 -0700 | [diff] [blame] | 181 | type_ids, |
| 182 | string_ids, |
| 183 | method_ids, |
| 184 | field_ids); |
David Sehr | d1e44e2 | 2016-10-06 17:09:32 -0700 | [diff] [blame] | 185 | } // for |
| 186 | return has_id; |
| 187 | } |
| 188 | |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 189 | EncodedValue* Collections::ReadEncodedValue(const uint8_t** data) { |
| 190 | const uint8_t encoded_value = *(*data)++; |
| 191 | const uint8_t type = encoded_value & 0x1f; |
| 192 | EncodedValue* item = new EncodedValue(type); |
| 193 | ReadEncodedValue(data, type, encoded_value >> 5, item); |
| 194 | return item; |
| 195 | } |
| 196 | |
| 197 | EncodedValue* Collections::ReadEncodedValue(const uint8_t** data, uint8_t type, uint8_t length) { |
| 198 | EncodedValue* item = new EncodedValue(type); |
| 199 | ReadEncodedValue(data, type, length, item); |
| 200 | return item; |
| 201 | } |
| 202 | |
| 203 | void Collections::ReadEncodedValue( |
| 204 | const uint8_t** data, uint8_t type, uint8_t length, EncodedValue* item) { |
| 205 | switch (type) { |
| 206 | case DexFile::kDexAnnotationByte: |
| 207 | item->SetByte(static_cast<int8_t>(ReadVarWidth(data, length, false))); |
| 208 | break; |
| 209 | case DexFile::kDexAnnotationShort: |
| 210 | item->SetShort(static_cast<int16_t>(ReadVarWidth(data, length, true))); |
| 211 | break; |
| 212 | case DexFile::kDexAnnotationChar: |
| 213 | item->SetChar(static_cast<uint16_t>(ReadVarWidth(data, length, false))); |
| 214 | break; |
| 215 | case DexFile::kDexAnnotationInt: |
| 216 | item->SetInt(static_cast<int32_t>(ReadVarWidth(data, length, true))); |
| 217 | break; |
| 218 | case DexFile::kDexAnnotationLong: |
| 219 | item->SetLong(static_cast<int64_t>(ReadVarWidth(data, length, true))); |
| 220 | break; |
| 221 | case DexFile::kDexAnnotationFloat: { |
| 222 | // Fill on right. |
| 223 | union { |
| 224 | float f; |
| 225 | uint32_t data; |
| 226 | } conv; |
| 227 | conv.data = static_cast<uint32_t>(ReadVarWidth(data, length, false)) << (3 - length) * 8; |
| 228 | item->SetFloat(conv.f); |
| 229 | break; |
| 230 | } |
| 231 | case DexFile::kDexAnnotationDouble: { |
| 232 | // Fill on right. |
| 233 | union { |
| 234 | double d; |
| 235 | uint64_t data; |
| 236 | } conv; |
| 237 | conv.data = ReadVarWidth(data, length, false) << (7 - length) * 8; |
| 238 | item->SetDouble(conv.d); |
| 239 | break; |
| 240 | } |
Jeff Hao | 5daee90 | 2017-04-27 18:00:38 -0700 | [diff] [blame] | 241 | case DexFile::kDexAnnotationMethodType: { |
| 242 | const uint32_t proto_index = static_cast<uint32_t>(ReadVarWidth(data, length, false)); |
| 243 | item->SetProtoId(GetProtoId(proto_index)); |
| 244 | break; |
| 245 | } |
| 246 | case DexFile::kDexAnnotationMethodHandle: { |
| 247 | const uint32_t method_handle_index = static_cast<uint32_t>(ReadVarWidth(data, length, false)); |
| 248 | item->SetMethodHandle(GetMethodHandle(method_handle_index)); |
| 249 | break; |
| 250 | } |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 251 | case DexFile::kDexAnnotationString: { |
| 252 | const uint32_t string_index = static_cast<uint32_t>(ReadVarWidth(data, length, false)); |
| 253 | item->SetStringId(GetStringId(string_index)); |
| 254 | break; |
| 255 | } |
| 256 | case DexFile::kDexAnnotationType: { |
| 257 | const uint32_t string_index = static_cast<uint32_t>(ReadVarWidth(data, length, false)); |
| 258 | item->SetTypeId(GetTypeId(string_index)); |
| 259 | break; |
| 260 | } |
| 261 | case DexFile::kDexAnnotationField: |
| 262 | case DexFile::kDexAnnotationEnum: { |
| 263 | const uint32_t field_index = static_cast<uint32_t>(ReadVarWidth(data, length, false)); |
| 264 | item->SetFieldId(GetFieldId(field_index)); |
| 265 | break; |
| 266 | } |
| 267 | case DexFile::kDexAnnotationMethod: { |
| 268 | const uint32_t method_index = static_cast<uint32_t>(ReadVarWidth(data, length, false)); |
| 269 | item->SetMethodId(GetMethodId(method_index)); |
| 270 | break; |
| 271 | } |
| 272 | case DexFile::kDexAnnotationArray: { |
| 273 | EncodedValueVector* values = new EncodedValueVector(); |
| 274 | const uint32_t size = DecodeUnsignedLeb128(data); |
| 275 | // Decode all elements. |
| 276 | for (uint32_t i = 0; i < size; i++) { |
| 277 | values->push_back(std::unique_ptr<EncodedValue>(ReadEncodedValue(data))); |
| 278 | } |
| 279 | item->SetEncodedArray(new EncodedArrayItem(values)); |
| 280 | break; |
| 281 | } |
| 282 | case DexFile::kDexAnnotationAnnotation: { |
| 283 | AnnotationElementVector* elements = new AnnotationElementVector(); |
| 284 | const uint32_t type_idx = DecodeUnsignedLeb128(data); |
| 285 | const uint32_t size = DecodeUnsignedLeb128(data); |
| 286 | // Decode all name=value pairs. |
| 287 | for (uint32_t i = 0; i < size; i++) { |
| 288 | const uint32_t name_index = DecodeUnsignedLeb128(data); |
| 289 | elements->push_back(std::unique_ptr<AnnotationElement>( |
| 290 | new AnnotationElement(GetStringId(name_index), ReadEncodedValue(data)))); |
| 291 | } |
| 292 | item->SetEncodedAnnotation(new EncodedAnnotation(GetTypeId(type_idx), elements)); |
| 293 | break; |
| 294 | } |
| 295 | case DexFile::kDexAnnotationNull: |
| 296 | break; |
| 297 | case DexFile::kDexAnnotationBoolean: |
| 298 | item->SetBoolean(length != 0); |
| 299 | break; |
| 300 | default: |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void Collections::CreateStringId(const DexFile& dex_file, uint32_t i) { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 306 | const DexFile::StringId& disk_string_id = dex_file.GetStringId(dex::StringIndex(i)); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 307 | StringData* string_data = new StringData(dex_file.GetStringData(disk_string_id)); |
| 308 | string_datas_.AddItem(string_data, disk_string_id.string_data_off_); |
| 309 | |
| 310 | StringId* string_id = new StringId(string_data); |
| 311 | string_ids_.AddIndexedItem(string_id, StringIdsOffset() + i * StringId::ItemSize(), i); |
| 312 | } |
| 313 | |
| 314 | void Collections::CreateTypeId(const DexFile& dex_file, uint32_t i) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 315 | const DexFile::TypeId& disk_type_id = dex_file.GetTypeId(dex::TypeIndex(i)); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 316 | TypeId* type_id = new TypeId(GetStringId(disk_type_id.descriptor_idx_.index_)); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 317 | type_ids_.AddIndexedItem(type_id, TypeIdsOffset() + i * TypeId::ItemSize(), i); |
| 318 | } |
| 319 | |
| 320 | void Collections::CreateProtoId(const DexFile& dex_file, uint32_t i) { |
| 321 | const DexFile::ProtoId& disk_proto_id = dex_file.GetProtoId(i); |
| 322 | const DexFile::TypeList* type_list = dex_file.GetProtoParameters(disk_proto_id); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 323 | TypeList* parameter_type_list = CreateTypeList(type_list, disk_proto_id.parameters_off_); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 324 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 325 | ProtoId* proto_id = new ProtoId(GetStringId(disk_proto_id.shorty_idx_.index_), |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 326 | GetTypeId(disk_proto_id.return_type_idx_.index_), |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 327 | parameter_type_list); |
| 328 | proto_ids_.AddIndexedItem(proto_id, ProtoIdsOffset() + i * ProtoId::ItemSize(), i); |
| 329 | } |
| 330 | |
| 331 | void Collections::CreateFieldId(const DexFile& dex_file, uint32_t i) { |
| 332 | const DexFile::FieldId& disk_field_id = dex_file.GetFieldId(i); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 333 | FieldId* field_id = new FieldId(GetTypeId(disk_field_id.class_idx_.index_), |
| 334 | GetTypeId(disk_field_id.type_idx_.index_), |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 335 | GetStringId(disk_field_id.name_idx_.index_)); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 336 | field_ids_.AddIndexedItem(field_id, FieldIdsOffset() + i * FieldId::ItemSize(), i); |
| 337 | } |
| 338 | |
| 339 | void Collections::CreateMethodId(const DexFile& dex_file, uint32_t i) { |
| 340 | const DexFile::MethodId& disk_method_id = dex_file.GetMethodId(i); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 341 | MethodId* method_id = new MethodId(GetTypeId(disk_method_id.class_idx_.index_), |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 342 | GetProtoId(disk_method_id.proto_idx_), |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 343 | GetStringId(disk_method_id.name_idx_.index_)); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 344 | method_ids_.AddIndexedItem(method_id, MethodIdsOffset() + i * MethodId::ItemSize(), i); |
| 345 | } |
| 346 | |
| 347 | void Collections::CreateClassDef(const DexFile& dex_file, uint32_t i) { |
| 348 | const DexFile::ClassDef& disk_class_def = dex_file.GetClassDef(i); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 349 | const TypeId* class_type = GetTypeId(disk_class_def.class_idx_.index_); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 350 | uint32_t access_flags = disk_class_def.access_flags_; |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 351 | const TypeId* superclass = GetTypeIdOrNullPtr(disk_class_def.superclass_idx_.index_); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 352 | |
| 353 | const DexFile::TypeList* type_list = dex_file.GetInterfacesList(disk_class_def); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 354 | TypeList* interfaces_type_list = CreateTypeList(type_list, disk_class_def.interfaces_off_); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 355 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 356 | const StringId* source_file = GetStringIdOrNullPtr(disk_class_def.source_file_idx_.index_); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 357 | // Annotations. |
| 358 | AnnotationsDirectoryItem* annotations = nullptr; |
| 359 | const DexFile::AnnotationsDirectoryItem* disk_annotations_directory_item = |
| 360 | dex_file.GetAnnotationsDirectory(disk_class_def); |
| 361 | if (disk_annotations_directory_item != nullptr) { |
| 362 | annotations = CreateAnnotationsDirectoryItem( |
| 363 | dex_file, disk_annotations_directory_item, disk_class_def.annotations_off_); |
| 364 | } |
| 365 | // Static field initializers. |
| 366 | const uint8_t* static_data = dex_file.GetEncodedStaticFieldValuesArray(disk_class_def); |
| 367 | EncodedArrayItem* static_values = |
| 368 | CreateEncodedArrayItem(static_data, disk_class_def.static_values_off_); |
| 369 | ClassData* class_data = CreateClassData( |
| 370 | dex_file, dex_file.GetClassData(disk_class_def), disk_class_def.class_data_off_); |
| 371 | ClassDef* class_def = new ClassDef(class_type, access_flags, superclass, interfaces_type_list, |
| 372 | source_file, annotations, static_values, class_data); |
| 373 | class_defs_.AddIndexedItem(class_def, ClassDefsOffset() + i * ClassDef::ItemSize(), i); |
| 374 | } |
| 375 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 376 | TypeList* Collections::CreateTypeList(const DexFile::TypeList* dex_type_list, uint32_t offset) { |
| 377 | if (dex_type_list == nullptr) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 378 | return nullptr; |
| 379 | } |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 380 | auto found_type_list = TypeLists().find(offset); |
| 381 | if (found_type_list != TypeLists().end()) { |
| 382 | return found_type_list->second.get(); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 383 | } |
| 384 | TypeIdVector* type_vector = new TypeIdVector(); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 385 | uint32_t size = dex_type_list->Size(); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 386 | for (uint32_t index = 0; index < size; ++index) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 387 | type_vector->push_back(GetTypeId(dex_type_list->GetTypeItem(index).type_idx_.index_)); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 388 | } |
| 389 | TypeList* new_type_list = new TypeList(type_vector); |
| 390 | type_lists_.AddItem(new_type_list, offset); |
| 391 | return new_type_list; |
| 392 | } |
| 393 | |
| 394 | EncodedArrayItem* Collections::CreateEncodedArrayItem(const uint8_t* static_data, uint32_t offset) { |
| 395 | if (static_data == nullptr) { |
| 396 | return nullptr; |
| 397 | } |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 398 | auto found_encoded_array_item = EncodedArrayItems().find(offset); |
| 399 | if (found_encoded_array_item != EncodedArrayItems().end()) { |
| 400 | return found_encoded_array_item->second.get(); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 401 | } |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 402 | uint32_t size = DecodeUnsignedLeb128(&static_data); |
| 403 | EncodedValueVector* values = new EncodedValueVector(); |
| 404 | for (uint32_t i = 0; i < size; ++i) { |
| 405 | values->push_back(std::unique_ptr<EncodedValue>(ReadEncodedValue(&static_data))); |
| 406 | } |
| 407 | // TODO: Calculate the size of the encoded array. |
| 408 | EncodedArrayItem* encoded_array_item = new EncodedArrayItem(values); |
| 409 | encoded_array_items_.AddItem(encoded_array_item, offset); |
| 410 | return encoded_array_item; |
| 411 | } |
| 412 | |
Mathieu Chartier | 24066ec | 2017-10-21 16:01:08 -0700 | [diff] [blame] | 413 | void Collections::AddAnnotationsFromMapListSection(const DexFile& dex_file, |
| 414 | uint32_t start_offset, |
| 415 | uint32_t count) { |
| 416 | uint32_t current_offset = start_offset; |
| 417 | for (size_t i = 0; i < count; ++i) { |
| 418 | // Annotation that we didn't process already, add it to the set. |
| 419 | const DexFile::AnnotationItem* annotation = dex_file.GetAnnotationItemAtOffset(current_offset); |
| 420 | AnnotationItem* annotation_item = CreateAnnotationItem(dex_file, annotation); |
| 421 | DCHECK(annotation_item != nullptr); |
| 422 | current_offset += annotation_item->GetSize(); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | AnnotationItem* Collections::CreateAnnotationItem(const DexFile& dex_file, |
| 427 | const DexFile::AnnotationItem* annotation) { |
| 428 | const uint8_t* const start_data = reinterpret_cast<const uint8_t*>(annotation); |
| 429 | const uint32_t offset = start_data - dex_file.Begin(); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 430 | auto found_annotation_item = AnnotationItems().find(offset); |
| 431 | if (found_annotation_item != AnnotationItems().end()) { |
| 432 | return found_annotation_item->second.get(); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 433 | } |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 434 | uint8_t visibility = annotation->visibility_; |
| 435 | const uint8_t* annotation_data = annotation->annotation_; |
Andreas Gampe | 2ae2dda | 2017-04-26 22:01:01 -0700 | [diff] [blame] | 436 | std::unique_ptr<EncodedValue> encoded_value( |
| 437 | ReadEncodedValue(&annotation_data, DexFile::kDexAnnotationAnnotation, 0)); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 438 | AnnotationItem* annotation_item = |
| 439 | new AnnotationItem(visibility, encoded_value->ReleaseEncodedAnnotation()); |
Mathieu Chartier | 24066ec | 2017-10-21 16:01:08 -0700 | [diff] [blame] | 440 | annotation_item->SetOffset(offset); |
| 441 | annotation_item->SetSize(annotation_data - start_data); |
| 442 | annotation_items_.AddItem(annotation_item, annotation_item->GetOffset()); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 443 | return annotation_item; |
| 444 | } |
| 445 | |
| 446 | |
| 447 | AnnotationSetItem* Collections::CreateAnnotationSetItem(const DexFile& dex_file, |
Jeff Hao | be9b44b | 2017-02-16 13:34:38 -0800 | [diff] [blame] | 448 | const DexFile::AnnotationSetItem* disk_annotations_item, uint32_t offset) { |
| 449 | if (disk_annotations_item == nullptr || (disk_annotations_item->size_ == 0 && offset == 0)) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 450 | return nullptr; |
| 451 | } |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 452 | auto found_anno_set_item = AnnotationSetItems().find(offset); |
| 453 | if (found_anno_set_item != AnnotationSetItems().end()) { |
| 454 | return found_anno_set_item->second.get(); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 455 | } |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 456 | std::vector<AnnotationItem*>* items = new std::vector<AnnotationItem*>(); |
Jeff Hao | be9b44b | 2017-02-16 13:34:38 -0800 | [diff] [blame] | 457 | for (uint32_t i = 0; i < disk_annotations_item->size_; ++i) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 458 | const DexFile::AnnotationItem* annotation = |
Jeff Hao | be9b44b | 2017-02-16 13:34:38 -0800 | [diff] [blame] | 459 | dex_file.GetAnnotationItem(disk_annotations_item, i); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 460 | if (annotation == nullptr) { |
| 461 | continue; |
| 462 | } |
Mathieu Chartier | 24066ec | 2017-10-21 16:01:08 -0700 | [diff] [blame] | 463 | AnnotationItem* annotation_item = CreateAnnotationItem(dex_file, annotation); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 464 | items->push_back(annotation_item); |
| 465 | } |
| 466 | AnnotationSetItem* annotation_set_item = new AnnotationSetItem(items); |
| 467 | annotation_set_items_.AddItem(annotation_set_item, offset); |
| 468 | return annotation_set_item; |
| 469 | } |
| 470 | |
| 471 | AnnotationsDirectoryItem* Collections::CreateAnnotationsDirectoryItem(const DexFile& dex_file, |
| 472 | const DexFile::AnnotationsDirectoryItem* disk_annotations_item, uint32_t offset) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 473 | auto found_anno_dir_item = AnnotationsDirectoryItems().find(offset); |
| 474 | if (found_anno_dir_item != AnnotationsDirectoryItems().end()) { |
| 475 | return found_anno_dir_item->second.get(); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 476 | } |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 477 | const DexFile::AnnotationSetItem* class_set_item = |
| 478 | dex_file.GetClassAnnotationSet(disk_annotations_item); |
| 479 | AnnotationSetItem* class_annotation = nullptr; |
| 480 | if (class_set_item != nullptr) { |
David Sehr | 7639cdc | 2017-04-15 10:06:21 -0700 | [diff] [blame] | 481 | uint32_t item_offset = disk_annotations_item->class_annotations_off_; |
| 482 | class_annotation = CreateAnnotationSetItem(dex_file, class_set_item, item_offset); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 483 | } |
| 484 | const DexFile::FieldAnnotationsItem* fields = |
| 485 | dex_file.GetFieldAnnotations(disk_annotations_item); |
| 486 | FieldAnnotationVector* field_annotations = nullptr; |
| 487 | if (fields != nullptr) { |
| 488 | field_annotations = new FieldAnnotationVector(); |
| 489 | for (uint32_t i = 0; i < disk_annotations_item->fields_size_; ++i) { |
| 490 | FieldId* field_id = GetFieldId(fields[i].field_idx_); |
| 491 | const DexFile::AnnotationSetItem* field_set_item = |
| 492 | dex_file.GetFieldAnnotationSetItem(fields[i]); |
| 493 | uint32_t annotation_set_offset = fields[i].annotations_off_; |
| 494 | AnnotationSetItem* annotation_set_item = |
Jeff Hao | be9b44b | 2017-02-16 13:34:38 -0800 | [diff] [blame] | 495 | CreateAnnotationSetItem(dex_file, field_set_item, annotation_set_offset); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 496 | field_annotations->push_back(std::unique_ptr<FieldAnnotation>( |
| 497 | new FieldAnnotation(field_id, annotation_set_item))); |
| 498 | } |
| 499 | } |
| 500 | const DexFile::MethodAnnotationsItem* methods = |
| 501 | dex_file.GetMethodAnnotations(disk_annotations_item); |
| 502 | MethodAnnotationVector* method_annotations = nullptr; |
| 503 | if (methods != nullptr) { |
| 504 | method_annotations = new MethodAnnotationVector(); |
| 505 | for (uint32_t i = 0; i < disk_annotations_item->methods_size_; ++i) { |
| 506 | MethodId* method_id = GetMethodId(methods[i].method_idx_); |
| 507 | const DexFile::AnnotationSetItem* method_set_item = |
| 508 | dex_file.GetMethodAnnotationSetItem(methods[i]); |
| 509 | uint32_t annotation_set_offset = methods[i].annotations_off_; |
| 510 | AnnotationSetItem* annotation_set_item = |
Jeff Hao | be9b44b | 2017-02-16 13:34:38 -0800 | [diff] [blame] | 511 | CreateAnnotationSetItem(dex_file, method_set_item, annotation_set_offset); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 512 | method_annotations->push_back(std::unique_ptr<MethodAnnotation>( |
| 513 | new MethodAnnotation(method_id, annotation_set_item))); |
| 514 | } |
| 515 | } |
| 516 | const DexFile::ParameterAnnotationsItem* parameters = |
| 517 | dex_file.GetParameterAnnotations(disk_annotations_item); |
| 518 | ParameterAnnotationVector* parameter_annotations = nullptr; |
| 519 | if (parameters != nullptr) { |
| 520 | parameter_annotations = new ParameterAnnotationVector(); |
| 521 | for (uint32_t i = 0; i < disk_annotations_item->parameters_size_; ++i) { |
| 522 | MethodId* method_id = GetMethodId(parameters[i].method_idx_); |
| 523 | const DexFile::AnnotationSetRefList* list = |
| 524 | dex_file.GetParameterAnnotationSetRefList(¶meters[i]); |
| 525 | parameter_annotations->push_back(std::unique_ptr<ParameterAnnotation>( |
| 526 | GenerateParameterAnnotation(dex_file, method_id, list, parameters[i].annotations_off_))); |
| 527 | } |
| 528 | } |
| 529 | // TODO: Calculate the size of the annotations directory. |
| 530 | AnnotationsDirectoryItem* annotations_directory_item = new AnnotationsDirectoryItem( |
| 531 | class_annotation, field_annotations, method_annotations, parameter_annotations); |
| 532 | annotations_directory_items_.AddItem(annotations_directory_item, offset); |
| 533 | return annotations_directory_item; |
| 534 | } |
| 535 | |
| 536 | ParameterAnnotation* Collections::GenerateParameterAnnotation( |
| 537 | const DexFile& dex_file, MethodId* method_id, |
| 538 | const DexFile::AnnotationSetRefList* annotation_set_ref_list, uint32_t offset) { |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 539 | AnnotationSetRefList* set_ref_list = nullptr; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 540 | auto found_set_ref_list = AnnotationSetRefLists().find(offset); |
| 541 | if (found_set_ref_list != AnnotationSetRefLists().end()) { |
| 542 | set_ref_list = found_set_ref_list->second.get(); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 543 | } |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 544 | if (set_ref_list == nullptr) { |
| 545 | std::vector<AnnotationSetItem*>* annotations = new std::vector<AnnotationSetItem*>(); |
| 546 | for (uint32_t i = 0; i < annotation_set_ref_list->size_; ++i) { |
| 547 | const DexFile::AnnotationSetItem* annotation_set_item = |
| 548 | dex_file.GetSetRefItemItem(&annotation_set_ref_list->list_[i]); |
| 549 | uint32_t set_offset = annotation_set_ref_list->list_[i].annotations_off_; |
Jeff Hao | be9b44b | 2017-02-16 13:34:38 -0800 | [diff] [blame] | 550 | annotations->push_back(CreateAnnotationSetItem(dex_file, annotation_set_item, set_offset)); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 551 | } |
| 552 | set_ref_list = new AnnotationSetRefList(annotations); |
| 553 | annotation_set_ref_lists_.AddItem(set_ref_list, offset); |
| 554 | } |
| 555 | return new ParameterAnnotation(method_id, set_ref_list); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | CodeItem* Collections::CreateCodeItem(const DexFile& dex_file, |
| 559 | const DexFile::CodeItem& disk_code_item, uint32_t offset) { |
| 560 | uint16_t registers_size = disk_code_item.registers_size_; |
| 561 | uint16_t ins_size = disk_code_item.ins_size_; |
| 562 | uint16_t outs_size = disk_code_item.outs_size_; |
| 563 | uint32_t tries_size = disk_code_item.tries_size_; |
| 564 | |
| 565 | // TODO: Calculate the size of the debug info. |
| 566 | const uint8_t* debug_info_stream = dex_file.GetDebugInfoStream(&disk_code_item); |
| 567 | DebugInfoItem* debug_info = nullptr; |
| 568 | if (debug_info_stream != nullptr) { |
Mathieu Chartier | a2973d7 | 2017-02-14 17:12:20 -0800 | [diff] [blame] | 569 | debug_info = debug_info_items_.GetExistingObject(disk_code_item.debug_info_off_); |
| 570 | if (debug_info == nullptr) { |
| 571 | uint32_t debug_info_size = GetDebugInfoStreamSize(debug_info_stream); |
| 572 | uint8_t* debug_info_buffer = new uint8_t[debug_info_size]; |
| 573 | memcpy(debug_info_buffer, debug_info_stream, debug_info_size); |
| 574 | debug_info = new DebugInfoItem(debug_info_size, debug_info_buffer); |
| 575 | debug_info_items_.AddItem(debug_info, disk_code_item.debug_info_off_); |
| 576 | } |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | uint32_t insns_size = disk_code_item.insns_size_in_code_units_; |
| 580 | uint16_t* insns = new uint16_t[insns_size]; |
| 581 | memcpy(insns, disk_code_item.insns_, insns_size * sizeof(uint16_t)); |
| 582 | |
| 583 | TryItemVector* tries = nullptr; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 584 | CatchHandlerVector* handler_list = nullptr; |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 585 | if (tries_size > 0) { |
| 586 | tries = new TryItemVector(); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 587 | handler_list = new CatchHandlerVector(); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 588 | for (uint32_t i = 0; i < tries_size; ++i) { |
| 589 | const DexFile::TryItem* disk_try_item = dex_file.GetTryItems(disk_code_item, i); |
| 590 | uint32_t start_addr = disk_try_item->start_addr_; |
| 591 | uint16_t insn_count = disk_try_item->insn_count_; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 592 | uint16_t handler_off = disk_try_item->handler_off_; |
| 593 | const CatchHandler* handlers = nullptr; |
| 594 | for (std::unique_ptr<const CatchHandler>& existing_handlers : *handler_list) { |
| 595 | if (handler_off == existing_handlers->GetListOffset()) { |
| 596 | handlers = existing_handlers.get(); |
Jeff Hao | 44652a3 | 2017-02-22 14:20:41 -0800 | [diff] [blame] | 597 | break; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | if (handlers == nullptr) { |
| 601 | bool catch_all = false; |
| 602 | TypeAddrPairVector* addr_pairs = new TypeAddrPairVector(); |
| 603 | for (CatchHandlerIterator it(disk_code_item, *disk_try_item); it.HasNext(); it.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 604 | const dex::TypeIndex type_index = it.GetHandlerTypeIndex(); |
| 605 | const TypeId* type_id = GetTypeIdOrNullPtr(type_index.index_); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 606 | catch_all |= type_id == nullptr; |
| 607 | addr_pairs->push_back(std::unique_ptr<const TypeAddrPair>( |
| 608 | new TypeAddrPair(type_id, it.GetHandlerAddress()))); |
| 609 | } |
| 610 | handlers = new CatchHandler(catch_all, handler_off, addr_pairs); |
| 611 | handler_list->push_back(std::unique_ptr<const CatchHandler>(handlers)); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 612 | } |
| 613 | TryItem* try_item = new TryItem(start_addr, insn_count, handlers); |
| 614 | tries->push_back(std::unique_ptr<const TryItem>(try_item)); |
| 615 | } |
Jeff Hao | 44652a3 | 2017-02-22 14:20:41 -0800 | [diff] [blame] | 616 | // Manually walk catch handlers list and add any missing handlers unreferenced by try items. |
| 617 | const uint8_t* handlers_base = DexFile::GetCatchHandlerData(disk_code_item, 0); |
| 618 | const uint8_t* handlers_data = handlers_base; |
| 619 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_data); |
| 620 | while (handlers_size > handler_list->size()) { |
| 621 | bool already_added = false; |
| 622 | uint16_t handler_off = handlers_data - handlers_base; |
| 623 | for (std::unique_ptr<const CatchHandler>& existing_handlers : *handler_list) { |
| 624 | if (handler_off == existing_handlers->GetListOffset()) { |
| 625 | already_added = true; |
| 626 | break; |
| 627 | } |
| 628 | } |
| 629 | int32_t size = DecodeSignedLeb128(&handlers_data); |
Jeff Hao | ac46271 | 2017-03-02 10:59:43 -0800 | [diff] [blame] | 630 | bool has_catch_all = size <= 0; |
Jeff Hao | 44652a3 | 2017-02-22 14:20:41 -0800 | [diff] [blame] | 631 | if (has_catch_all) { |
| 632 | size = -size; |
| 633 | } |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 634 | if (already_added) { |
Jeff Hao | 44652a3 | 2017-02-22 14:20:41 -0800 | [diff] [blame] | 635 | for (int32_t i = 0; i < size; i++) { |
| 636 | DecodeUnsignedLeb128(&handlers_data); |
| 637 | DecodeUnsignedLeb128(&handlers_data); |
| 638 | } |
| 639 | if (has_catch_all) { |
| 640 | DecodeUnsignedLeb128(&handlers_data); |
| 641 | } |
| 642 | continue; |
| 643 | } |
| 644 | TypeAddrPairVector* addr_pairs = new TypeAddrPairVector(); |
| 645 | for (int32_t i = 0; i < size; i++) { |
| 646 | const TypeId* type_id = GetTypeIdOrNullPtr(DecodeUnsignedLeb128(&handlers_data)); |
| 647 | uint32_t addr = DecodeUnsignedLeb128(&handlers_data); |
| 648 | addr_pairs->push_back( |
| 649 | std::unique_ptr<const TypeAddrPair>(new TypeAddrPair(type_id, addr))); |
| 650 | } |
| 651 | if (has_catch_all) { |
| 652 | uint32_t addr = DecodeUnsignedLeb128(&handlers_data); |
| 653 | addr_pairs->push_back( |
| 654 | std::unique_ptr<const TypeAddrPair>(new TypeAddrPair(nullptr, addr))); |
| 655 | } |
| 656 | const CatchHandler* handler = new CatchHandler(has_catch_all, handler_off, addr_pairs); |
| 657 | handler_list->push_back(std::unique_ptr<const CatchHandler>(handler)); |
| 658 | } |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 659 | } |
Jeff Hao | 44652a3 | 2017-02-22 14:20:41 -0800 | [diff] [blame] | 660 | |
Bharadwaj Kalandhabhatta | 043c908 | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 661 | uint32_t size = DexFile::GetCodeItemSize(disk_code_item); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 662 | CodeItem* code_item = new CodeItem( |
| 663 | registers_size, ins_size, outs_size, debug_info, insns_size, insns, tries, handler_list); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 664 | code_item->SetSize(size); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 665 | code_items_.AddItem(code_item, offset); |
David Sehr | d1e44e2 | 2016-10-06 17:09:32 -0700 | [diff] [blame] | 666 | // Add "fixup" references to types, strings, methods, and fields. |
| 667 | // This is temporary, as we will probably want more detailed parsing of the |
| 668 | // instructions here. |
| 669 | std::unique_ptr<std::vector<TypeId*>> type_ids(new std::vector<TypeId*>()); |
| 670 | std::unique_ptr<std::vector<StringId*>> string_ids(new std::vector<StringId*>()); |
| 671 | std::unique_ptr<std::vector<MethodId*>> method_ids(new std::vector<MethodId*>()); |
| 672 | std::unique_ptr<std::vector<FieldId*>> field_ids(new std::vector<FieldId*>()); |
| 673 | if (GetIdsFromByteCode(*this, |
| 674 | code_item, |
| 675 | type_ids.get(), |
| 676 | string_ids.get(), |
| 677 | method_ids.get(), |
| 678 | field_ids.get())) { |
| 679 | CodeFixups* fixups = new CodeFixups(type_ids.release(), |
| 680 | string_ids.release(), |
| 681 | method_ids.release(), |
| 682 | field_ids.release()); |
| 683 | code_item->SetCodeFixups(fixups); |
| 684 | } |
| 685 | |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 686 | return code_item; |
| 687 | } |
| 688 | |
| 689 | MethodItem* Collections::GenerateMethodItem(const DexFile& dex_file, ClassDataItemIterator& cdii) { |
Jeff Hao | 9804e9e | 2017-06-15 14:04:51 -0700 | [diff] [blame] | 690 | MethodId* method_id = GetMethodId(cdii.GetMemberIndex()); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 691 | uint32_t access_flags = cdii.GetRawMemberAccessFlags(); |
| 692 | const DexFile::CodeItem* disk_code_item = cdii.GetMethodCodeItem(); |
Jeff Hao | 9804e9e | 2017-06-15 14:04:51 -0700 | [diff] [blame] | 693 | CodeItem* code_item = code_items_.GetExistingObject(cdii.GetMethodCodeItemOffset()); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 694 | DebugInfoItem* debug_info = nullptr; |
| 695 | if (disk_code_item != nullptr) { |
Jeff Hao | d212d5b | 2017-04-26 12:09:06 -0700 | [diff] [blame] | 696 | if (code_item == nullptr) { |
| 697 | code_item = CreateCodeItem(dex_file, *disk_code_item, cdii.GetMethodCodeItemOffset()); |
| 698 | } |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 699 | debug_info = code_item->DebugInfo(); |
| 700 | } |
Jeff Hao | 9804e9e | 2017-06-15 14:04:51 -0700 | [diff] [blame] | 701 | return new MethodItem(access_flags, method_id, code_item); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | ClassData* Collections::CreateClassData( |
| 705 | const DexFile& dex_file, const uint8_t* encoded_data, uint32_t offset) { |
| 706 | // Read the fields and methods defined by the class, resolving the circular reference from those |
| 707 | // to classes by setting class at the same time. |
Mathieu Chartier | a2973d7 | 2017-02-14 17:12:20 -0800 | [diff] [blame] | 708 | ClassData* class_data = class_datas_.GetExistingObject(offset); |
| 709 | if (class_data == nullptr && encoded_data != nullptr) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 710 | ClassDataItemIterator cdii(dex_file, encoded_data); |
| 711 | // Static fields. |
| 712 | FieldItemVector* static_fields = new FieldItemVector(); |
Jeff Hao | 9804e9e | 2017-06-15 14:04:51 -0700 | [diff] [blame] | 713 | for (; cdii.HasNextStaticField(); cdii.Next()) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 714 | FieldId* field_item = GetFieldId(cdii.GetMemberIndex()); |
| 715 | uint32_t access_flags = cdii.GetRawMemberAccessFlags(); |
| 716 | static_fields->push_back(std::unique_ptr<FieldItem>(new FieldItem(access_flags, field_item))); |
| 717 | } |
| 718 | // Instance fields. |
| 719 | FieldItemVector* instance_fields = new FieldItemVector(); |
Jeff Hao | 9804e9e | 2017-06-15 14:04:51 -0700 | [diff] [blame] | 720 | for (; cdii.HasNextInstanceField(); cdii.Next()) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 721 | FieldId* field_item = GetFieldId(cdii.GetMemberIndex()); |
| 722 | uint32_t access_flags = cdii.GetRawMemberAccessFlags(); |
| 723 | instance_fields->push_back( |
| 724 | std::unique_ptr<FieldItem>(new FieldItem(access_flags, field_item))); |
| 725 | } |
| 726 | // Direct methods. |
| 727 | MethodItemVector* direct_methods = new MethodItemVector(); |
Jeff Hao | 9804e9e | 2017-06-15 14:04:51 -0700 | [diff] [blame] | 728 | for (; cdii.HasNextDirectMethod(); cdii.Next()) { |
| 729 | direct_methods->push_back(std::unique_ptr<MethodItem>(GenerateMethodItem(dex_file, cdii))); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 730 | } |
| 731 | // Virtual methods. |
| 732 | MethodItemVector* virtual_methods = new MethodItemVector(); |
Jeff Hao | 9804e9e | 2017-06-15 14:04:51 -0700 | [diff] [blame] | 733 | for (; cdii.HasNextVirtualMethod(); cdii.Next()) { |
| 734 | virtual_methods->push_back(std::unique_ptr<MethodItem>(GenerateMethodItem(dex_file, cdii))); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 735 | } |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 736 | class_data = new ClassData(static_fields, instance_fields, direct_methods, virtual_methods); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 737 | class_data->SetSize(cdii.EndDataPointer() - encoded_data); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 738 | class_datas_.AddItem(class_data, offset); |
| 739 | } |
| 740 | return class_data; |
| 741 | } |
| 742 | |
Jeff Hao | 5daee90 | 2017-04-27 18:00:38 -0700 | [diff] [blame] | 743 | void Collections::CreateCallSitesAndMethodHandles(const DexFile& dex_file) { |
| 744 | // Iterate through the map list and set the offset of the CallSiteIds and MethodHandleItems. |
| 745 | const DexFile::MapList* map = |
| 746 | reinterpret_cast<const DexFile::MapList*>(dex_file.Begin() + MapListOffset()); |
| 747 | for (uint32_t i = 0; i < map->size_; ++i) { |
| 748 | const DexFile::MapItem* item = map->list_ + i; |
| 749 | switch (item->type_) { |
| 750 | case DexFile::kDexTypeCallSiteIdItem: |
| 751 | SetCallSiteIdsOffset(item->offset_); |
| 752 | break; |
| 753 | case DexFile::kDexTypeMethodHandleItem: |
| 754 | SetMethodHandleItemsOffset(item->offset_); |
| 755 | break; |
| 756 | default: |
| 757 | break; |
| 758 | } |
| 759 | } |
| 760 | // Populate MethodHandleItems first (CallSiteIds may depend on them). |
| 761 | for (uint32_t i = 0; i < dex_file.NumMethodHandles(); i++) { |
| 762 | CreateMethodHandleItem(dex_file, i); |
| 763 | } |
| 764 | // Populate CallSiteIds. |
| 765 | for (uint32_t i = 0; i < dex_file.NumCallSiteIds(); i++) { |
| 766 | CreateCallSiteId(dex_file, i); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | void Collections::CreateCallSiteId(const DexFile& dex_file, uint32_t i) { |
| 771 | const DexFile::CallSiteIdItem& disk_call_site_id = dex_file.GetCallSiteId(i); |
| 772 | const uint8_t* disk_call_item_ptr = dex_file.Begin() + disk_call_site_id.data_off_; |
| 773 | EncodedArrayItem* call_site_item = |
| 774 | CreateEncodedArrayItem(disk_call_item_ptr, disk_call_site_id.data_off_); |
| 775 | |
| 776 | CallSiteId* call_site_id = new CallSiteId(call_site_item); |
| 777 | call_site_ids_.AddIndexedItem(call_site_id, CallSiteIdsOffset() + i * CallSiteId::ItemSize(), i); |
| 778 | } |
| 779 | |
| 780 | void Collections::CreateMethodHandleItem(const DexFile& dex_file, uint32_t i) { |
| 781 | const DexFile::MethodHandleItem& disk_method_handle = dex_file.GetMethodHandle(i); |
| 782 | uint16_t index = disk_method_handle.field_or_method_idx_; |
| 783 | DexFile::MethodHandleType type = |
| 784 | static_cast<DexFile::MethodHandleType>(disk_method_handle.method_handle_type_); |
| 785 | bool is_invoke = type == DexFile::MethodHandleType::kInvokeStatic || |
| 786 | type == DexFile::MethodHandleType::kInvokeInstance || |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 787 | type == DexFile::MethodHandleType::kInvokeConstructor || |
| 788 | type == DexFile::MethodHandleType::kInvokeDirect || |
| 789 | type == DexFile::MethodHandleType::kInvokeInterface; |
| 790 | static_assert(DexFile::MethodHandleType::kLast == DexFile::MethodHandleType::kInvokeInterface, |
Jeff Hao | 5daee90 | 2017-04-27 18:00:38 -0700 | [diff] [blame] | 791 | "Unexpected method handle types."); |
| 792 | IndexedItem* field_or_method_id; |
| 793 | if (is_invoke) { |
| 794 | field_or_method_id = GetMethodId(index); |
| 795 | } else { |
| 796 | field_or_method_id = GetFieldId(index); |
| 797 | } |
| 798 | MethodHandleItem* method_handle = new MethodHandleItem(type, field_or_method_id); |
| 799 | method_handle_items_.AddIndexedItem( |
| 800 | method_handle, MethodHandleItemsOffset() + i * MethodHandleItem::ItemSize(), i); |
| 801 | } |
| 802 | |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 803 | static uint32_t HeaderOffset(const dex_ir::Collections& collections ATTRIBUTE_UNUSED) { |
| 804 | return 0; |
| 805 | } |
| 806 | |
| 807 | static uint32_t HeaderSize(const dex_ir::Collections& collections ATTRIBUTE_UNUSED) { |
| 808 | // Size is in elements, so there is only one header. |
| 809 | return 1; |
| 810 | } |
| 811 | |
| 812 | // The description of each dex file section type. |
| 813 | struct FileSectionDescriptor { |
| 814 | public: |
| 815 | std::string name; |
| 816 | uint16_t type; |
| 817 | // A function that when applied to a collection object, gives the size of the section. |
| 818 | std::function<uint32_t(const dex_ir::Collections&)> size_fn; |
| 819 | // A function that when applied to a collection object, gives the offset of the section. |
| 820 | std::function<uint32_t(const dex_ir::Collections&)> offset_fn; |
| 821 | }; |
| 822 | |
David Sehr | 7639cdc | 2017-04-15 10:06:21 -0700 | [diff] [blame] | 823 | static const FileSectionDescriptor kFileSectionDescriptors[] = { |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 824 | { |
| 825 | "Header", |
| 826 | DexFile::kDexTypeHeaderItem, |
| 827 | &HeaderSize, |
| 828 | &HeaderOffset, |
| 829 | }, { |
| 830 | "StringId", |
| 831 | DexFile::kDexTypeStringIdItem, |
| 832 | &dex_ir::Collections::StringIdsSize, |
| 833 | &dex_ir::Collections::StringIdsOffset |
| 834 | }, { |
| 835 | "TypeId", |
| 836 | DexFile::kDexTypeTypeIdItem, |
| 837 | &dex_ir::Collections::TypeIdsSize, |
| 838 | &dex_ir::Collections::TypeIdsOffset |
| 839 | }, { |
| 840 | "ProtoId", |
| 841 | DexFile::kDexTypeProtoIdItem, |
| 842 | &dex_ir::Collections::ProtoIdsSize, |
| 843 | &dex_ir::Collections::ProtoIdsOffset |
| 844 | }, { |
| 845 | "FieldId", |
| 846 | DexFile::kDexTypeFieldIdItem, |
| 847 | &dex_ir::Collections::FieldIdsSize, |
| 848 | &dex_ir::Collections::FieldIdsOffset |
| 849 | }, { |
| 850 | "MethodId", |
| 851 | DexFile::kDexTypeMethodIdItem, |
| 852 | &dex_ir::Collections::MethodIdsSize, |
| 853 | &dex_ir::Collections::MethodIdsOffset |
| 854 | }, { |
| 855 | "ClassDef", |
| 856 | DexFile::kDexTypeClassDefItem, |
| 857 | &dex_ir::Collections::ClassDefsSize, |
| 858 | &dex_ir::Collections::ClassDefsOffset |
| 859 | }, { |
Jeff Hao | 5daee90 | 2017-04-27 18:00:38 -0700 | [diff] [blame] | 860 | "CallSiteId", |
| 861 | DexFile::kDexTypeCallSiteIdItem, |
| 862 | &dex_ir::Collections::CallSiteIdsSize, |
| 863 | &dex_ir::Collections::CallSiteIdsOffset |
| 864 | }, { |
| 865 | "MethodHandle", |
| 866 | DexFile::kDexTypeMethodHandleItem, |
| 867 | &dex_ir::Collections::MethodHandleItemsSize, |
| 868 | &dex_ir::Collections::MethodHandleItemsOffset |
| 869 | }, { |
David Sehr | 9037a3a | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 870 | "StringData", |
| 871 | DexFile::kDexTypeStringDataItem, |
| 872 | &dex_ir::Collections::StringDatasSize, |
| 873 | &dex_ir::Collections::StringDatasOffset |
| 874 | }, { |
| 875 | "TypeList", |
| 876 | DexFile::kDexTypeTypeList, |
| 877 | &dex_ir::Collections::TypeListsSize, |
| 878 | &dex_ir::Collections::TypeListsOffset |
| 879 | }, { |
| 880 | "EncArr", |
| 881 | DexFile::kDexTypeEncodedArrayItem, |
| 882 | &dex_ir::Collections::EncodedArrayItemsSize, |
| 883 | &dex_ir::Collections::EncodedArrayItemsOffset |
| 884 | }, { |
| 885 | "Annotation", |
| 886 | DexFile::kDexTypeAnnotationItem, |
| 887 | &dex_ir::Collections::AnnotationItemsSize, |
| 888 | &dex_ir::Collections::AnnotationItemsOffset |
| 889 | }, { |
| 890 | "AnnoSet", |
| 891 | DexFile::kDexTypeAnnotationSetItem, |
| 892 | &dex_ir::Collections::AnnotationSetItemsSize, |
| 893 | &dex_ir::Collections::AnnotationSetItemsOffset |
| 894 | }, { |
| 895 | "AnnoSetRL", |
| 896 | DexFile::kDexTypeAnnotationSetRefList, |
| 897 | &dex_ir::Collections::AnnotationSetRefListsSize, |
| 898 | &dex_ir::Collections::AnnotationSetRefListsOffset |
| 899 | }, { |
| 900 | "AnnoDir", |
| 901 | DexFile::kDexTypeAnnotationsDirectoryItem, |
| 902 | &dex_ir::Collections::AnnotationsDirectoryItemsSize, |
| 903 | &dex_ir::Collections::AnnotationsDirectoryItemsOffset |
| 904 | }, { |
| 905 | "DebugInfo", |
| 906 | DexFile::kDexTypeDebugInfoItem, |
| 907 | &dex_ir::Collections::DebugInfoItemsSize, |
| 908 | &dex_ir::Collections::DebugInfoItemsOffset |
| 909 | }, { |
| 910 | "CodeItem", |
| 911 | DexFile::kDexTypeCodeItem, |
| 912 | &dex_ir::Collections::CodeItemsSize, |
| 913 | &dex_ir::Collections::CodeItemsOffset |
| 914 | }, { |
| 915 | "ClassData", |
| 916 | DexFile::kDexTypeClassDataItem, |
| 917 | &dex_ir::Collections::ClassDatasSize, |
| 918 | &dex_ir::Collections::ClassDatasOffset |
| 919 | } |
| 920 | }; |
| 921 | |
| 922 | std::vector<dex_ir::DexFileSection> GetSortedDexFileSections(dex_ir::Header* header, |
| 923 | dex_ir::SortDirection direction) { |
| 924 | const dex_ir::Collections& collections = header->GetCollections(); |
| 925 | std::vector<dex_ir::DexFileSection> sorted_sections; |
| 926 | // Build the table that will map from offset to color |
| 927 | for (const FileSectionDescriptor& s : kFileSectionDescriptors) { |
| 928 | sorted_sections.push_back(dex_ir::DexFileSection(s.name, |
| 929 | s.type, |
| 930 | s.size_fn(collections), |
| 931 | s.offset_fn(collections))); |
| 932 | } |
| 933 | // Sort by offset. |
| 934 | std::sort(sorted_sections.begin(), |
| 935 | sorted_sections.end(), |
| 936 | [=](dex_ir::DexFileSection& a, dex_ir::DexFileSection& b) { |
| 937 | if (direction == SortDirection::kSortDescending) { |
| 938 | return a.offset > b.offset; |
| 939 | } else { |
| 940 | return a.offset < b.offset; |
| 941 | } |
| 942 | }); |
| 943 | return sorted_sections; |
| 944 | } |
| 945 | |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 946 | } // namespace dex_ir |
| 947 | } // namespace art |