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 | |
| 42 | static bool GetPositionsCb(void* context, const DexFile::PositionInfo& entry) { |
| 43 | DebugInfoItem* debug_info = reinterpret_cast<DebugInfoItem*>(context); |
| 44 | PositionInfoVector& positions = debug_info->GetPositionInfo(); |
| 45 | positions.push_back(std::unique_ptr<PositionInfo>(new PositionInfo(entry.address_, entry.line_))); |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | static void GetLocalsCb(void* context, const DexFile::LocalInfo& entry) { |
| 50 | DebugInfoItem* debug_info = reinterpret_cast<DebugInfoItem*>(context); |
| 51 | LocalInfoVector& locals = debug_info->GetLocalInfo(); |
| 52 | const char* name = entry.name_ != nullptr ? entry.name_ : "(null)"; |
| 53 | const char* signature = entry.signature_ != nullptr ? entry.signature_ : ""; |
| 54 | locals.push_back(std::unique_ptr<LocalInfo>( |
| 55 | new LocalInfo(name, entry.descriptor_, signature, entry.start_address_, |
| 56 | entry.end_address_, entry.reg_))); |
| 57 | } |
| 58 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 59 | static uint32_t GetDebugInfoStreamSize(const uint8_t* debug_info_stream) { |
| 60 | const uint8_t* stream = debug_info_stream; |
| 61 | DecodeUnsignedLeb128(&stream); // line_start |
| 62 | uint32_t parameters_size = DecodeUnsignedLeb128(&stream); |
| 63 | for (uint32_t i = 0; i < parameters_size; ++i) { |
| 64 | DecodeUnsignedLeb128P1(&stream); // Parameter name. |
| 65 | } |
| 66 | |
| 67 | for (;;) { |
| 68 | uint8_t opcode = *stream++; |
| 69 | switch (opcode) { |
| 70 | case DexFile::DBG_END_SEQUENCE: |
| 71 | return stream - debug_info_stream; // end of stream. |
| 72 | case DexFile::DBG_ADVANCE_PC: |
| 73 | DecodeUnsignedLeb128(&stream); // addr_diff |
| 74 | break; |
| 75 | case DexFile::DBG_ADVANCE_LINE: |
| 76 | DecodeSignedLeb128(&stream); // line_diff |
| 77 | break; |
| 78 | case DexFile::DBG_START_LOCAL: |
| 79 | DecodeUnsignedLeb128(&stream); // register_num |
| 80 | DecodeUnsignedLeb128P1(&stream); // name_idx |
| 81 | DecodeUnsignedLeb128P1(&stream); // type_idx |
| 82 | break; |
| 83 | case DexFile::DBG_START_LOCAL_EXTENDED: |
| 84 | DecodeUnsignedLeb128(&stream); // register_num |
| 85 | DecodeUnsignedLeb128P1(&stream); // name_idx |
| 86 | DecodeUnsignedLeb128P1(&stream); // type_idx |
| 87 | DecodeUnsignedLeb128P1(&stream); // sig_idx |
| 88 | break; |
| 89 | case DexFile::DBG_END_LOCAL: |
| 90 | case DexFile::DBG_RESTART_LOCAL: |
| 91 | DecodeUnsignedLeb128(&stream); // register_num |
| 92 | break; |
| 93 | case DexFile::DBG_SET_PROLOGUE_END: |
| 94 | case DexFile::DBG_SET_EPILOGUE_BEGIN: |
| 95 | break; |
| 96 | case DexFile::DBG_SET_FILE: { |
| 97 | DecodeUnsignedLeb128P1(&stream); // name_idx |
| 98 | break; |
| 99 | } |
| 100 | default: { |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
David Sehr | d1e44e2 | 2016-10-06 17:09:32 -0700 | [diff] [blame] | 107 | static bool GetIdFromInstruction(Collections& collections, |
| 108 | const Instruction* dec_insn, |
| 109 | std::vector<TypeId*>* type_ids, |
| 110 | std::vector<StringId*>* string_ids, |
| 111 | std::vector<MethodId*>* method_ids, |
| 112 | std::vector<FieldId*>* field_ids) { |
| 113 | // Determine index and width of the string. |
| 114 | uint32_t index = 0; |
| 115 | switch (Instruction::FormatOf(dec_insn->Opcode())) { |
| 116 | // SOME NOT SUPPORTED: |
| 117 | // case Instruction::k20bc: |
| 118 | case Instruction::k21c: |
| 119 | case Instruction::k35c: |
| 120 | // case Instruction::k35ms: |
| 121 | case Instruction::k3rc: |
| 122 | // case Instruction::k3rms: |
| 123 | // case Instruction::k35mi: |
| 124 | // case Instruction::k3rmi: |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 125 | case Instruction::k45cc: |
| 126 | case Instruction::k4rcc: |
David Sehr | d1e44e2 | 2016-10-06 17:09:32 -0700 | [diff] [blame] | 127 | index = dec_insn->VRegB(); |
| 128 | break; |
| 129 | case Instruction::k31c: |
| 130 | index = dec_insn->VRegB(); |
| 131 | break; |
| 132 | case Instruction::k22c: |
| 133 | // case Instruction::k22cs: |
| 134 | index = dec_insn->VRegC(); |
| 135 | break; |
| 136 | default: |
| 137 | break; |
| 138 | } // switch |
| 139 | |
| 140 | // Determine index type, and add reference to the appropriate collection. |
| 141 | switch (Instruction::IndexTypeOf(dec_insn->Opcode())) { |
| 142 | case Instruction::kIndexTypeRef: |
| 143 | if (index < collections.TypeIdsSize()) { |
| 144 | type_ids->push_back(collections.GetTypeId(index)); |
| 145 | return true; |
| 146 | } |
| 147 | break; |
| 148 | case Instruction::kIndexStringRef: |
| 149 | if (index < collections.StringIdsSize()) { |
| 150 | string_ids->push_back(collections.GetStringId(index)); |
| 151 | return true; |
| 152 | } |
| 153 | break; |
| 154 | case Instruction::kIndexMethodRef: |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 155 | case Instruction::kIndexMethodAndProtoRef: |
David Sehr | d1e44e2 | 2016-10-06 17:09:32 -0700 | [diff] [blame] | 156 | if (index < collections.MethodIdsSize()) { |
| 157 | method_ids->push_back(collections.GetMethodId(index)); |
| 158 | return true; |
| 159 | } |
| 160 | break; |
| 161 | case Instruction::kIndexFieldRef: |
| 162 | if (index < collections.FieldIdsSize()) { |
| 163 | field_ids->push_back(collections.GetFieldId(index)); |
| 164 | return true; |
| 165 | } |
| 166 | break; |
| 167 | case Instruction::kIndexUnknown: |
| 168 | case Instruction::kIndexNone: |
| 169 | case Instruction::kIndexVtableOffset: |
| 170 | case Instruction::kIndexFieldOffset: |
| 171 | default: |
| 172 | break; |
| 173 | } // switch |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Get all the types, strings, methods, and fields referred to from bytecode. |
| 179 | */ |
| 180 | static bool GetIdsFromByteCode(Collections& collections, |
| 181 | const CodeItem* code, |
| 182 | std::vector<TypeId*>* type_ids, |
| 183 | std::vector<StringId*>* string_ids, |
| 184 | std::vector<MethodId*>* method_ids, |
| 185 | std::vector<FieldId*>* field_ids) { |
| 186 | bool has_id = false; |
| 187 | // Iterate over all instructions. |
| 188 | const uint16_t* insns = code->Insns(); |
| 189 | for (uint32_t insn_idx = 0; insn_idx < code->InsnsSize();) { |
| 190 | const Instruction* instruction = Instruction::At(&insns[insn_idx]); |
| 191 | const uint32_t insn_width = instruction->SizeInCodeUnits(); |
| 192 | if (insn_width == 0) { |
| 193 | break; |
| 194 | } |
| 195 | has_id |= GetIdFromInstruction(collections, |
| 196 | instruction, |
| 197 | type_ids, |
| 198 | string_ids, |
| 199 | method_ids, |
| 200 | field_ids); |
| 201 | insn_idx += insn_width; |
| 202 | } // for |
| 203 | return has_id; |
| 204 | } |
| 205 | |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 206 | EncodedValue* Collections::ReadEncodedValue(const uint8_t** data) { |
| 207 | const uint8_t encoded_value = *(*data)++; |
| 208 | const uint8_t type = encoded_value & 0x1f; |
| 209 | EncodedValue* item = new EncodedValue(type); |
| 210 | ReadEncodedValue(data, type, encoded_value >> 5, item); |
| 211 | return item; |
| 212 | } |
| 213 | |
| 214 | EncodedValue* Collections::ReadEncodedValue(const uint8_t** data, uint8_t type, uint8_t length) { |
| 215 | EncodedValue* item = new EncodedValue(type); |
| 216 | ReadEncodedValue(data, type, length, item); |
| 217 | return item; |
| 218 | } |
| 219 | |
| 220 | void Collections::ReadEncodedValue( |
| 221 | const uint8_t** data, uint8_t type, uint8_t length, EncodedValue* item) { |
| 222 | switch (type) { |
| 223 | case DexFile::kDexAnnotationByte: |
| 224 | item->SetByte(static_cast<int8_t>(ReadVarWidth(data, length, false))); |
| 225 | break; |
| 226 | case DexFile::kDexAnnotationShort: |
| 227 | item->SetShort(static_cast<int16_t>(ReadVarWidth(data, length, true))); |
| 228 | break; |
| 229 | case DexFile::kDexAnnotationChar: |
| 230 | item->SetChar(static_cast<uint16_t>(ReadVarWidth(data, length, false))); |
| 231 | break; |
| 232 | case DexFile::kDexAnnotationInt: |
| 233 | item->SetInt(static_cast<int32_t>(ReadVarWidth(data, length, true))); |
| 234 | break; |
| 235 | case DexFile::kDexAnnotationLong: |
| 236 | item->SetLong(static_cast<int64_t>(ReadVarWidth(data, length, true))); |
| 237 | break; |
| 238 | case DexFile::kDexAnnotationFloat: { |
| 239 | // Fill on right. |
| 240 | union { |
| 241 | float f; |
| 242 | uint32_t data; |
| 243 | } conv; |
| 244 | conv.data = static_cast<uint32_t>(ReadVarWidth(data, length, false)) << (3 - length) * 8; |
| 245 | item->SetFloat(conv.f); |
| 246 | break; |
| 247 | } |
| 248 | case DexFile::kDexAnnotationDouble: { |
| 249 | // Fill on right. |
| 250 | union { |
| 251 | double d; |
| 252 | uint64_t data; |
| 253 | } conv; |
| 254 | conv.data = ReadVarWidth(data, length, false) << (7 - length) * 8; |
| 255 | item->SetDouble(conv.d); |
| 256 | break; |
| 257 | } |
| 258 | case DexFile::kDexAnnotationString: { |
| 259 | const uint32_t string_index = static_cast<uint32_t>(ReadVarWidth(data, length, false)); |
| 260 | item->SetStringId(GetStringId(string_index)); |
| 261 | break; |
| 262 | } |
| 263 | case DexFile::kDexAnnotationType: { |
| 264 | const uint32_t string_index = static_cast<uint32_t>(ReadVarWidth(data, length, false)); |
| 265 | item->SetTypeId(GetTypeId(string_index)); |
| 266 | break; |
| 267 | } |
| 268 | case DexFile::kDexAnnotationField: |
| 269 | case DexFile::kDexAnnotationEnum: { |
| 270 | const uint32_t field_index = static_cast<uint32_t>(ReadVarWidth(data, length, false)); |
| 271 | item->SetFieldId(GetFieldId(field_index)); |
| 272 | break; |
| 273 | } |
| 274 | case DexFile::kDexAnnotationMethod: { |
| 275 | const uint32_t method_index = static_cast<uint32_t>(ReadVarWidth(data, length, false)); |
| 276 | item->SetMethodId(GetMethodId(method_index)); |
| 277 | break; |
| 278 | } |
| 279 | case DexFile::kDexAnnotationArray: { |
| 280 | EncodedValueVector* values = new EncodedValueVector(); |
| 281 | const uint32_t size = DecodeUnsignedLeb128(data); |
| 282 | // Decode all elements. |
| 283 | for (uint32_t i = 0; i < size; i++) { |
| 284 | values->push_back(std::unique_ptr<EncodedValue>(ReadEncodedValue(data))); |
| 285 | } |
| 286 | item->SetEncodedArray(new EncodedArrayItem(values)); |
| 287 | break; |
| 288 | } |
| 289 | case DexFile::kDexAnnotationAnnotation: { |
| 290 | AnnotationElementVector* elements = new AnnotationElementVector(); |
| 291 | const uint32_t type_idx = DecodeUnsignedLeb128(data); |
| 292 | const uint32_t size = DecodeUnsignedLeb128(data); |
| 293 | // Decode all name=value pairs. |
| 294 | for (uint32_t i = 0; i < size; i++) { |
| 295 | const uint32_t name_index = DecodeUnsignedLeb128(data); |
| 296 | elements->push_back(std::unique_ptr<AnnotationElement>( |
| 297 | new AnnotationElement(GetStringId(name_index), ReadEncodedValue(data)))); |
| 298 | } |
| 299 | item->SetEncodedAnnotation(new EncodedAnnotation(GetTypeId(type_idx), elements)); |
| 300 | break; |
| 301 | } |
| 302 | case DexFile::kDexAnnotationNull: |
| 303 | break; |
| 304 | case DexFile::kDexAnnotationBoolean: |
| 305 | item->SetBoolean(length != 0); |
| 306 | break; |
| 307 | default: |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | void Collections::CreateStringId(const DexFile& dex_file, uint32_t i) { |
| 313 | const DexFile::StringId& disk_string_id = dex_file.GetStringId(i); |
| 314 | StringData* string_data = new StringData(dex_file.GetStringData(disk_string_id)); |
| 315 | string_datas_.AddItem(string_data, disk_string_id.string_data_off_); |
| 316 | |
| 317 | StringId* string_id = new StringId(string_data); |
| 318 | string_ids_.AddIndexedItem(string_id, StringIdsOffset() + i * StringId::ItemSize(), i); |
| 319 | } |
| 320 | |
| 321 | void Collections::CreateTypeId(const DexFile& dex_file, uint32_t i) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame^] | 322 | const DexFile::TypeId& disk_type_id = dex_file.GetTypeId(dex::TypeIndex(i)); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 323 | TypeId* type_id = new TypeId(GetStringId(disk_type_id.descriptor_idx_)); |
| 324 | type_ids_.AddIndexedItem(type_id, TypeIdsOffset() + i * TypeId::ItemSize(), i); |
| 325 | } |
| 326 | |
| 327 | void Collections::CreateProtoId(const DexFile& dex_file, uint32_t i) { |
| 328 | const DexFile::ProtoId& disk_proto_id = dex_file.GetProtoId(i); |
| 329 | const DexFile::TypeList* type_list = dex_file.GetProtoParameters(disk_proto_id); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 330 | TypeList* parameter_type_list = CreateTypeList(type_list, disk_proto_id.parameters_off_); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 331 | |
| 332 | ProtoId* proto_id = new ProtoId(GetStringId(disk_proto_id.shorty_idx_), |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame^] | 333 | GetTypeId(disk_proto_id.return_type_idx_.index_), |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 334 | parameter_type_list); |
| 335 | proto_ids_.AddIndexedItem(proto_id, ProtoIdsOffset() + i * ProtoId::ItemSize(), i); |
| 336 | } |
| 337 | |
| 338 | void Collections::CreateFieldId(const DexFile& dex_file, uint32_t i) { |
| 339 | const DexFile::FieldId& disk_field_id = dex_file.GetFieldId(i); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame^] | 340 | FieldId* field_id = new FieldId(GetTypeId(disk_field_id.class_idx_.index_), |
| 341 | GetTypeId(disk_field_id.type_idx_.index_), |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 342 | GetStringId(disk_field_id.name_idx_)); |
| 343 | field_ids_.AddIndexedItem(field_id, FieldIdsOffset() + i * FieldId::ItemSize(), i); |
| 344 | } |
| 345 | |
| 346 | void Collections::CreateMethodId(const DexFile& dex_file, uint32_t i) { |
| 347 | const DexFile::MethodId& disk_method_id = dex_file.GetMethodId(i); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame^] | 348 | MethodId* method_id = new MethodId(GetTypeId(disk_method_id.class_idx_.index_), |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 349 | GetProtoId(disk_method_id.proto_idx_), |
| 350 | GetStringId(disk_method_id.name_idx_)); |
| 351 | method_ids_.AddIndexedItem(method_id, MethodIdsOffset() + i * MethodId::ItemSize(), i); |
| 352 | } |
| 353 | |
| 354 | void Collections::CreateClassDef(const DexFile& dex_file, uint32_t i) { |
| 355 | const DexFile::ClassDef& disk_class_def = dex_file.GetClassDef(i); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame^] | 356 | const TypeId* class_type = GetTypeId(disk_class_def.class_idx_.index_); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 357 | uint32_t access_flags = disk_class_def.access_flags_; |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame^] | 358 | const TypeId* superclass = GetTypeIdOrNullPtr(disk_class_def.superclass_idx_.index_); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 359 | |
| 360 | const DexFile::TypeList* type_list = dex_file.GetInterfacesList(disk_class_def); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 361 | TypeList* interfaces_type_list = CreateTypeList(type_list, disk_class_def.interfaces_off_); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 362 | |
| 363 | const StringId* source_file = GetStringIdOrNullPtr(disk_class_def.source_file_idx_); |
| 364 | // Annotations. |
| 365 | AnnotationsDirectoryItem* annotations = nullptr; |
| 366 | const DexFile::AnnotationsDirectoryItem* disk_annotations_directory_item = |
| 367 | dex_file.GetAnnotationsDirectory(disk_class_def); |
| 368 | if (disk_annotations_directory_item != nullptr) { |
| 369 | annotations = CreateAnnotationsDirectoryItem( |
| 370 | dex_file, disk_annotations_directory_item, disk_class_def.annotations_off_); |
| 371 | } |
| 372 | // Static field initializers. |
| 373 | const uint8_t* static_data = dex_file.GetEncodedStaticFieldValuesArray(disk_class_def); |
| 374 | EncodedArrayItem* static_values = |
| 375 | CreateEncodedArrayItem(static_data, disk_class_def.static_values_off_); |
| 376 | ClassData* class_data = CreateClassData( |
| 377 | dex_file, dex_file.GetClassData(disk_class_def), disk_class_def.class_data_off_); |
| 378 | ClassDef* class_def = new ClassDef(class_type, access_flags, superclass, interfaces_type_list, |
| 379 | source_file, annotations, static_values, class_data); |
| 380 | class_defs_.AddIndexedItem(class_def, ClassDefsOffset() + i * ClassDef::ItemSize(), i); |
| 381 | } |
| 382 | |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 383 | TypeList* Collections::CreateTypeList(const DexFile::TypeList* dex_type_list, uint32_t offset) { |
| 384 | if (dex_type_list == nullptr) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 385 | return nullptr; |
| 386 | } |
| 387 | // TODO: Create more efficient lookup for existing type lists. |
| 388 | for (std::unique_ptr<TypeList>& type_list : TypeLists()) { |
| 389 | if (type_list->GetOffset() == offset) { |
| 390 | return type_list.get(); |
| 391 | } |
| 392 | } |
| 393 | TypeIdVector* type_vector = new TypeIdVector(); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 394 | uint32_t size = dex_type_list->Size(); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 395 | for (uint32_t index = 0; index < size; ++index) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame^] | 396 | 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] | 397 | } |
| 398 | TypeList* new_type_list = new TypeList(type_vector); |
| 399 | type_lists_.AddItem(new_type_list, offset); |
| 400 | return new_type_list; |
| 401 | } |
| 402 | |
| 403 | EncodedArrayItem* Collections::CreateEncodedArrayItem(const uint8_t* static_data, uint32_t offset) { |
| 404 | if (static_data == nullptr) { |
| 405 | return nullptr; |
| 406 | } |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 407 | for (std::unique_ptr<EncodedArrayItem>& existing_array_item : EncodedArrayItems()) { |
| 408 | if (existing_array_item->GetOffset() == offset) { |
| 409 | return existing_array_item.get(); |
| 410 | } |
| 411 | } |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 412 | uint32_t size = DecodeUnsignedLeb128(&static_data); |
| 413 | EncodedValueVector* values = new EncodedValueVector(); |
| 414 | for (uint32_t i = 0; i < size; ++i) { |
| 415 | values->push_back(std::unique_ptr<EncodedValue>(ReadEncodedValue(&static_data))); |
| 416 | } |
| 417 | // TODO: Calculate the size of the encoded array. |
| 418 | EncodedArrayItem* encoded_array_item = new EncodedArrayItem(values); |
| 419 | encoded_array_items_.AddItem(encoded_array_item, offset); |
| 420 | return encoded_array_item; |
| 421 | } |
| 422 | |
| 423 | AnnotationItem* Collections::CreateAnnotationItem(const DexFile::AnnotationItem* annotation, |
| 424 | uint32_t offset) { |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 425 | for (std::unique_ptr<AnnotationItem>& existing_annotation_item : AnnotationItems()) { |
| 426 | if (existing_annotation_item->GetOffset() == offset) { |
| 427 | return existing_annotation_item.get(); |
| 428 | } |
| 429 | } |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 430 | uint8_t visibility = annotation->visibility_; |
| 431 | const uint8_t* annotation_data = annotation->annotation_; |
| 432 | EncodedValue* encoded_value = |
| 433 | ReadEncodedValue(&annotation_data, DexFile::kDexAnnotationAnnotation, 0); |
| 434 | // TODO: Calculate the size of the annotation. |
| 435 | AnnotationItem* annotation_item = |
| 436 | new AnnotationItem(visibility, encoded_value->ReleaseEncodedAnnotation()); |
| 437 | annotation_items_.AddItem(annotation_item, offset); |
| 438 | return annotation_item; |
| 439 | } |
| 440 | |
| 441 | |
| 442 | AnnotationSetItem* Collections::CreateAnnotationSetItem(const DexFile& dex_file, |
| 443 | const DexFile::AnnotationSetItem& disk_annotations_item, uint32_t offset) { |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 444 | if (disk_annotations_item.size_ == 0 && offset == 0) { |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 445 | return nullptr; |
| 446 | } |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 447 | for (std::unique_ptr<AnnotationSetItem>& existing_anno_set_item : AnnotationSetItems()) { |
| 448 | if (existing_anno_set_item->GetOffset() == offset) { |
| 449 | return existing_anno_set_item.get(); |
| 450 | } |
| 451 | } |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 452 | std::vector<AnnotationItem*>* items = new std::vector<AnnotationItem*>(); |
| 453 | for (uint32_t i = 0; i < disk_annotations_item.size_; ++i) { |
| 454 | const DexFile::AnnotationItem* annotation = |
| 455 | dex_file.GetAnnotationItem(&disk_annotations_item, i); |
| 456 | if (annotation == nullptr) { |
| 457 | continue; |
| 458 | } |
| 459 | AnnotationItem* annotation_item = |
| 460 | CreateAnnotationItem(annotation, disk_annotations_item.entries_[i]); |
| 461 | items->push_back(annotation_item); |
| 462 | } |
| 463 | AnnotationSetItem* annotation_set_item = new AnnotationSetItem(items); |
| 464 | annotation_set_items_.AddItem(annotation_set_item, offset); |
| 465 | return annotation_set_item; |
| 466 | } |
| 467 | |
| 468 | AnnotationsDirectoryItem* Collections::CreateAnnotationsDirectoryItem(const DexFile& dex_file, |
| 469 | const DexFile::AnnotationsDirectoryItem* disk_annotations_item, uint32_t offset) { |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 470 | for (std::unique_ptr<AnnotationsDirectoryItem>& anno_dir_item : AnnotationsDirectoryItems()) { |
| 471 | if (anno_dir_item->GetOffset() == offset) { |
| 472 | return anno_dir_item.get(); |
| 473 | } |
| 474 | } |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 475 | const DexFile::AnnotationSetItem* class_set_item = |
| 476 | dex_file.GetClassAnnotationSet(disk_annotations_item); |
| 477 | AnnotationSetItem* class_annotation = nullptr; |
| 478 | if (class_set_item != nullptr) { |
| 479 | uint32_t offset = disk_annotations_item->class_annotations_off_; |
| 480 | class_annotation = CreateAnnotationSetItem(dex_file, *class_set_item, offset); |
| 481 | } |
| 482 | const DexFile::FieldAnnotationsItem* fields = |
| 483 | dex_file.GetFieldAnnotations(disk_annotations_item); |
| 484 | FieldAnnotationVector* field_annotations = nullptr; |
| 485 | if (fields != nullptr) { |
| 486 | field_annotations = new FieldAnnotationVector(); |
| 487 | for (uint32_t i = 0; i < disk_annotations_item->fields_size_; ++i) { |
| 488 | FieldId* field_id = GetFieldId(fields[i].field_idx_); |
| 489 | const DexFile::AnnotationSetItem* field_set_item = |
| 490 | dex_file.GetFieldAnnotationSetItem(fields[i]); |
| 491 | uint32_t annotation_set_offset = fields[i].annotations_off_; |
| 492 | AnnotationSetItem* annotation_set_item = |
| 493 | CreateAnnotationSetItem(dex_file, *field_set_item, annotation_set_offset); |
| 494 | field_annotations->push_back(std::unique_ptr<FieldAnnotation>( |
| 495 | new FieldAnnotation(field_id, annotation_set_item))); |
| 496 | } |
| 497 | } |
| 498 | const DexFile::MethodAnnotationsItem* methods = |
| 499 | dex_file.GetMethodAnnotations(disk_annotations_item); |
| 500 | MethodAnnotationVector* method_annotations = nullptr; |
| 501 | if (methods != nullptr) { |
| 502 | method_annotations = new MethodAnnotationVector(); |
| 503 | for (uint32_t i = 0; i < disk_annotations_item->methods_size_; ++i) { |
| 504 | MethodId* method_id = GetMethodId(methods[i].method_idx_); |
| 505 | const DexFile::AnnotationSetItem* method_set_item = |
| 506 | dex_file.GetMethodAnnotationSetItem(methods[i]); |
| 507 | uint32_t annotation_set_offset = methods[i].annotations_off_; |
| 508 | AnnotationSetItem* annotation_set_item = |
| 509 | CreateAnnotationSetItem(dex_file, *method_set_item, annotation_set_offset); |
| 510 | method_annotations->push_back(std::unique_ptr<MethodAnnotation>( |
| 511 | new MethodAnnotation(method_id, annotation_set_item))); |
| 512 | } |
| 513 | } |
| 514 | const DexFile::ParameterAnnotationsItem* parameters = |
| 515 | dex_file.GetParameterAnnotations(disk_annotations_item); |
| 516 | ParameterAnnotationVector* parameter_annotations = nullptr; |
| 517 | if (parameters != nullptr) { |
| 518 | parameter_annotations = new ParameterAnnotationVector(); |
| 519 | for (uint32_t i = 0; i < disk_annotations_item->parameters_size_; ++i) { |
| 520 | MethodId* method_id = GetMethodId(parameters[i].method_idx_); |
| 521 | const DexFile::AnnotationSetRefList* list = |
| 522 | dex_file.GetParameterAnnotationSetRefList(¶meters[i]); |
| 523 | parameter_annotations->push_back(std::unique_ptr<ParameterAnnotation>( |
| 524 | GenerateParameterAnnotation(dex_file, method_id, list, parameters[i].annotations_off_))); |
| 525 | } |
| 526 | } |
| 527 | // TODO: Calculate the size of the annotations directory. |
| 528 | AnnotationsDirectoryItem* annotations_directory_item = new AnnotationsDirectoryItem( |
| 529 | class_annotation, field_annotations, method_annotations, parameter_annotations); |
| 530 | annotations_directory_items_.AddItem(annotations_directory_item, offset); |
| 531 | return annotations_directory_item; |
| 532 | } |
| 533 | |
| 534 | ParameterAnnotation* Collections::GenerateParameterAnnotation( |
| 535 | const DexFile& dex_file, MethodId* method_id, |
| 536 | const DexFile::AnnotationSetRefList* annotation_set_ref_list, uint32_t offset) { |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 537 | AnnotationSetRefList* set_ref_list = nullptr; |
| 538 | for (std::unique_ptr<AnnotationSetRefList>& existing_set_ref_list : AnnotationSetRefLists()) { |
| 539 | if (existing_set_ref_list->GetOffset() == offset) { |
| 540 | set_ref_list = existing_set_ref_list.get(); |
| 541 | break; |
| 542 | } |
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_; |
| 550 | annotations->push_back(CreateAnnotationSetItem(dex_file, *annotation_set_item, set_offset)); |
| 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) { |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 569 | uint32_t debug_info_size = GetDebugInfoStreamSize(debug_info_stream); |
| 570 | uint8_t* debug_info_buffer = new uint8_t[debug_info_size]; |
| 571 | memcpy(debug_info_buffer, debug_info_stream, debug_info_size); |
| 572 | debug_info = new DebugInfoItem(debug_info_size, debug_info_buffer); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 573 | debug_info_items_.AddItem(debug_info, disk_code_item.debug_info_off_); |
| 574 | } |
| 575 | |
| 576 | uint32_t insns_size = disk_code_item.insns_size_in_code_units_; |
| 577 | uint16_t* insns = new uint16_t[insns_size]; |
| 578 | memcpy(insns, disk_code_item.insns_, insns_size * sizeof(uint16_t)); |
| 579 | |
| 580 | TryItemVector* tries = nullptr; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 581 | CatchHandlerVector* handler_list = nullptr; |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 582 | if (tries_size > 0) { |
| 583 | tries = new TryItemVector(); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 584 | handler_list = new CatchHandlerVector(); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 585 | for (uint32_t i = 0; i < tries_size; ++i) { |
| 586 | const DexFile::TryItem* disk_try_item = dex_file.GetTryItems(disk_code_item, i); |
| 587 | uint32_t start_addr = disk_try_item->start_addr_; |
| 588 | uint16_t insn_count = disk_try_item->insn_count_; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 589 | uint16_t handler_off = disk_try_item->handler_off_; |
| 590 | const CatchHandler* handlers = nullptr; |
| 591 | for (std::unique_ptr<const CatchHandler>& existing_handlers : *handler_list) { |
| 592 | if (handler_off == existing_handlers->GetListOffset()) { |
| 593 | handlers = existing_handlers.get(); |
| 594 | } |
| 595 | } |
| 596 | if (handlers == nullptr) { |
| 597 | bool catch_all = false; |
| 598 | TypeAddrPairVector* addr_pairs = new TypeAddrPairVector(); |
| 599 | 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^] | 600 | const dex::TypeIndex type_index = it.GetHandlerTypeIndex(); |
| 601 | const TypeId* type_id = GetTypeIdOrNullPtr(type_index.index_); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 602 | catch_all |= type_id == nullptr; |
| 603 | addr_pairs->push_back(std::unique_ptr<const TypeAddrPair>( |
| 604 | new TypeAddrPair(type_id, it.GetHandlerAddress()))); |
| 605 | } |
| 606 | handlers = new CatchHandler(catch_all, handler_off, addr_pairs); |
| 607 | handler_list->push_back(std::unique_ptr<const CatchHandler>(handlers)); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 608 | } |
| 609 | TryItem* try_item = new TryItem(start_addr, insn_count, handlers); |
| 610 | tries->push_back(std::unique_ptr<const TryItem>(try_item)); |
| 611 | } |
| 612 | } |
| 613 | // TODO: Calculate the size of the code item. |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 614 | CodeItem* code_item = new CodeItem( |
| 615 | registers_size, ins_size, outs_size, debug_info, insns_size, insns, tries, handler_list); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 616 | code_items_.AddItem(code_item, offset); |
David Sehr | d1e44e2 | 2016-10-06 17:09:32 -0700 | [diff] [blame] | 617 | // Add "fixup" references to types, strings, methods, and fields. |
| 618 | // This is temporary, as we will probably want more detailed parsing of the |
| 619 | // instructions here. |
| 620 | std::unique_ptr<std::vector<TypeId*>> type_ids(new std::vector<TypeId*>()); |
| 621 | std::unique_ptr<std::vector<StringId*>> string_ids(new std::vector<StringId*>()); |
| 622 | std::unique_ptr<std::vector<MethodId*>> method_ids(new std::vector<MethodId*>()); |
| 623 | std::unique_ptr<std::vector<FieldId*>> field_ids(new std::vector<FieldId*>()); |
| 624 | if (GetIdsFromByteCode(*this, |
| 625 | code_item, |
| 626 | type_ids.get(), |
| 627 | string_ids.get(), |
| 628 | method_ids.get(), |
| 629 | field_ids.get())) { |
| 630 | CodeFixups* fixups = new CodeFixups(type_ids.release(), |
| 631 | string_ids.release(), |
| 632 | method_ids.release(), |
| 633 | field_ids.release()); |
| 634 | code_item->SetCodeFixups(fixups); |
| 635 | } |
| 636 | |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 637 | return code_item; |
| 638 | } |
| 639 | |
| 640 | MethodItem* Collections::GenerateMethodItem(const DexFile& dex_file, ClassDataItemIterator& cdii) { |
| 641 | MethodId* method_item = GetMethodId(cdii.GetMemberIndex()); |
| 642 | uint32_t access_flags = cdii.GetRawMemberAccessFlags(); |
| 643 | const DexFile::CodeItem* disk_code_item = cdii.GetMethodCodeItem(); |
| 644 | CodeItem* code_item = nullptr; |
| 645 | DebugInfoItem* debug_info = nullptr; |
| 646 | if (disk_code_item != nullptr) { |
| 647 | code_item = CreateCodeItem(dex_file, *disk_code_item, cdii.GetMethodCodeItemOffset()); |
| 648 | debug_info = code_item->DebugInfo(); |
| 649 | } |
| 650 | if (debug_info != nullptr) { |
| 651 | bool is_static = (access_flags & kAccStatic) != 0; |
| 652 | dex_file.DecodeDebugLocalInfo( |
| 653 | disk_code_item, is_static, cdii.GetMemberIndex(), GetLocalsCb, debug_info); |
| 654 | dex_file.DecodeDebugPositionInfo(disk_code_item, GetPositionsCb, debug_info); |
| 655 | } |
| 656 | return new MethodItem(access_flags, method_item, code_item); |
| 657 | } |
| 658 | |
| 659 | ClassData* Collections::CreateClassData( |
| 660 | const DexFile& dex_file, const uint8_t* encoded_data, uint32_t offset) { |
| 661 | // Read the fields and methods defined by the class, resolving the circular reference from those |
| 662 | // to classes by setting class at the same time. |
| 663 | ClassData* class_data = nullptr; |
| 664 | if (encoded_data != nullptr) { |
| 665 | ClassDataItemIterator cdii(dex_file, encoded_data); |
| 666 | // Static fields. |
| 667 | FieldItemVector* static_fields = new FieldItemVector(); |
| 668 | for (uint32_t i = 0; cdii.HasNextStaticField(); i++, cdii.Next()) { |
| 669 | FieldId* field_item = GetFieldId(cdii.GetMemberIndex()); |
| 670 | uint32_t access_flags = cdii.GetRawMemberAccessFlags(); |
| 671 | static_fields->push_back(std::unique_ptr<FieldItem>(new FieldItem(access_flags, field_item))); |
| 672 | } |
| 673 | // Instance fields. |
| 674 | FieldItemVector* instance_fields = new FieldItemVector(); |
| 675 | for (uint32_t i = 0; cdii.HasNextInstanceField(); i++, cdii.Next()) { |
| 676 | FieldId* field_item = GetFieldId(cdii.GetMemberIndex()); |
| 677 | uint32_t access_flags = cdii.GetRawMemberAccessFlags(); |
| 678 | instance_fields->push_back( |
| 679 | std::unique_ptr<FieldItem>(new FieldItem(access_flags, field_item))); |
| 680 | } |
| 681 | // Direct methods. |
| 682 | MethodItemVector* direct_methods = new MethodItemVector(); |
| 683 | for (uint32_t i = 0; cdii.HasNextDirectMethod(); i++, cdii.Next()) { |
| 684 | direct_methods->push_back( |
| 685 | std::unique_ptr<MethodItem>(GenerateMethodItem(dex_file, cdii))); |
| 686 | } |
| 687 | // Virtual methods. |
| 688 | MethodItemVector* virtual_methods = new MethodItemVector(); |
| 689 | for (uint32_t i = 0; cdii.HasNextVirtualMethod(); i++, cdii.Next()) { |
| 690 | virtual_methods->push_back( |
| 691 | std::unique_ptr<MethodItem>(GenerateMethodItem(dex_file, cdii))); |
| 692 | } |
| 693 | // TODO: Calculate the size of the class data. |
| 694 | class_data = new ClassData(static_fields, instance_fields, direct_methods, virtual_methods); |
| 695 | class_datas_.AddItem(class_data, offset); |
| 696 | } |
| 697 | return class_data; |
| 698 | } |
| 699 | |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 700 | } // namespace dex_ir |
| 701 | } // namespace art |