blob: b1e66be3487d8da446d9beb2e584a63c501c5382 [file] [log] [blame]
Jeff Hao3ab96b42016-09-09 18:35:01 -07001/*
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 Sehrd1e44e22016-10-06 17:09:32 -070024#include "dex_instruction-inl.h"
Jeff Hao3ab96b42016-09-09 18:35:01 -070025#include "dex_ir_builder.h"
26
27namespace art {
28namespace dex_ir {
29
30static 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
42static 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
49static 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 Haoea7c6292016-11-14 18:10:16 -080059static uint32_t GetCodeItemSize(const DexFile& dex_file, const DexFile::CodeItem& disk_code_item) {
60 uintptr_t code_item_start = reinterpret_cast<uintptr_t>(&disk_code_item);
61 uint32_t insns_size = disk_code_item.insns_size_in_code_units_;
62 uint32_t tries_size = disk_code_item.tries_size_;
63 if (tries_size == 0) {
64 uintptr_t insns_end = reinterpret_cast<uintptr_t>(&disk_code_item.insns_[insns_size]);
65 return insns_end - code_item_start;
66 } else {
67 uint32_t last_handler_off = 0;
68 for (uint32_t i = 0; i < tries_size; ++i) {
69 // Iterate over the try items to find the last catch handler.
70 const DexFile::TryItem* disk_try_item = dex_file.GetTryItems(disk_code_item, i);
71 uint16_t handler_off = disk_try_item->handler_off_;
72 if (handler_off > last_handler_off) {
73 last_handler_off = handler_off;
74 }
75 }
76 // Decode the final handler to see where it ends.
77 const uint8_t* handler_data = DexFile::GetCatchHandlerData(disk_code_item, last_handler_off);
78 int32_t uleb128_count = DecodeSignedLeb128(&handler_data) * 2;
79 if (uleb128_count <= 0) {
80 uleb128_count = -uleb128_count + 1;
81 }
82 for (int32_t i = 0; i < uleb128_count; ++i) {
83 DecodeUnsignedLeb128(&handler_data);
84 }
85 return reinterpret_cast<uintptr_t>(handler_data) - code_item_start;
86 }
87}
88
Jeff Haoa8621002016-10-04 18:13:44 +000089static uint32_t GetDebugInfoStreamSize(const uint8_t* debug_info_stream) {
90 const uint8_t* stream = debug_info_stream;
91 DecodeUnsignedLeb128(&stream); // line_start
92 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
93 for (uint32_t i = 0; i < parameters_size; ++i) {
94 DecodeUnsignedLeb128P1(&stream); // Parameter name.
95 }
96
97 for (;;) {
98 uint8_t opcode = *stream++;
99 switch (opcode) {
100 case DexFile::DBG_END_SEQUENCE:
101 return stream - debug_info_stream; // end of stream.
102 case DexFile::DBG_ADVANCE_PC:
103 DecodeUnsignedLeb128(&stream); // addr_diff
104 break;
105 case DexFile::DBG_ADVANCE_LINE:
106 DecodeSignedLeb128(&stream); // line_diff
107 break;
108 case DexFile::DBG_START_LOCAL:
109 DecodeUnsignedLeb128(&stream); // register_num
110 DecodeUnsignedLeb128P1(&stream); // name_idx
111 DecodeUnsignedLeb128P1(&stream); // type_idx
112 break;
113 case DexFile::DBG_START_LOCAL_EXTENDED:
114 DecodeUnsignedLeb128(&stream); // register_num
115 DecodeUnsignedLeb128P1(&stream); // name_idx
116 DecodeUnsignedLeb128P1(&stream); // type_idx
117 DecodeUnsignedLeb128P1(&stream); // sig_idx
118 break;
119 case DexFile::DBG_END_LOCAL:
120 case DexFile::DBG_RESTART_LOCAL:
121 DecodeUnsignedLeb128(&stream); // register_num
122 break;
123 case DexFile::DBG_SET_PROLOGUE_END:
124 case DexFile::DBG_SET_EPILOGUE_BEGIN:
125 break;
126 case DexFile::DBG_SET_FILE: {
127 DecodeUnsignedLeb128P1(&stream); // name_idx
128 break;
129 }
130 default: {
131 break;
132 }
133 }
134 }
135}
136
David Sehrd1e44e22016-10-06 17:09:32 -0700137static bool GetIdFromInstruction(Collections& collections,
138 const Instruction* dec_insn,
139 std::vector<TypeId*>* type_ids,
140 std::vector<StringId*>* string_ids,
141 std::vector<MethodId*>* method_ids,
142 std::vector<FieldId*>* field_ids) {
143 // Determine index and width of the string.
144 uint32_t index = 0;
145 switch (Instruction::FormatOf(dec_insn->Opcode())) {
146 // SOME NOT SUPPORTED:
147 // case Instruction::k20bc:
148 case Instruction::k21c:
149 case Instruction::k35c:
150 // case Instruction::k35ms:
151 case Instruction::k3rc:
152 // case Instruction::k3rms:
153 // case Instruction::k35mi:
154 // case Instruction::k3rmi:
Orion Hodsonb34bb192016-10-18 17:02:58 +0100155 case Instruction::k45cc:
156 case Instruction::k4rcc:
David Sehrd1e44e22016-10-06 17:09:32 -0700157 index = dec_insn->VRegB();
158 break;
159 case Instruction::k31c:
160 index = dec_insn->VRegB();
161 break;
162 case Instruction::k22c:
163 // case Instruction::k22cs:
164 index = dec_insn->VRegC();
165 break;
166 default:
167 break;
168 } // switch
169
170 // Determine index type, and add reference to the appropriate collection.
171 switch (Instruction::IndexTypeOf(dec_insn->Opcode())) {
172 case Instruction::kIndexTypeRef:
173 if (index < collections.TypeIdsSize()) {
174 type_ids->push_back(collections.GetTypeId(index));
175 return true;
176 }
177 break;
178 case Instruction::kIndexStringRef:
179 if (index < collections.StringIdsSize()) {
180 string_ids->push_back(collections.GetStringId(index));
181 return true;
182 }
183 break;
184 case Instruction::kIndexMethodRef:
Orion Hodsonb34bb192016-10-18 17:02:58 +0100185 case Instruction::kIndexMethodAndProtoRef:
David Sehrd1e44e22016-10-06 17:09:32 -0700186 if (index < collections.MethodIdsSize()) {
187 method_ids->push_back(collections.GetMethodId(index));
188 return true;
189 }
190 break;
191 case Instruction::kIndexFieldRef:
192 if (index < collections.FieldIdsSize()) {
193 field_ids->push_back(collections.GetFieldId(index));
194 return true;
195 }
196 break;
197 case Instruction::kIndexUnknown:
198 case Instruction::kIndexNone:
199 case Instruction::kIndexVtableOffset:
200 case Instruction::kIndexFieldOffset:
201 default:
202 break;
203 } // switch
204 return false;
205}
206
207/*
208 * Get all the types, strings, methods, and fields referred to from bytecode.
209 */
210static bool GetIdsFromByteCode(Collections& collections,
211 const CodeItem* code,
212 std::vector<TypeId*>* type_ids,
213 std::vector<StringId*>* string_ids,
214 std::vector<MethodId*>* method_ids,
215 std::vector<FieldId*>* field_ids) {
216 bool has_id = false;
217 // Iterate over all instructions.
218 const uint16_t* insns = code->Insns();
219 for (uint32_t insn_idx = 0; insn_idx < code->InsnsSize();) {
220 const Instruction* instruction = Instruction::At(&insns[insn_idx]);
221 const uint32_t insn_width = instruction->SizeInCodeUnits();
222 if (insn_width == 0) {
223 break;
224 }
225 has_id |= GetIdFromInstruction(collections,
226 instruction,
227 type_ids,
228 string_ids,
229 method_ids,
230 field_ids);
231 insn_idx += insn_width;
232 } // for
233 return has_id;
234}
235
Jeff Hao3ab96b42016-09-09 18:35:01 -0700236EncodedValue* Collections::ReadEncodedValue(const uint8_t** data) {
237 const uint8_t encoded_value = *(*data)++;
238 const uint8_t type = encoded_value & 0x1f;
239 EncodedValue* item = new EncodedValue(type);
240 ReadEncodedValue(data, type, encoded_value >> 5, item);
241 return item;
242}
243
244EncodedValue* Collections::ReadEncodedValue(const uint8_t** data, uint8_t type, uint8_t length) {
245 EncodedValue* item = new EncodedValue(type);
246 ReadEncodedValue(data, type, length, item);
247 return item;
248}
249
250void Collections::ReadEncodedValue(
251 const uint8_t** data, uint8_t type, uint8_t length, EncodedValue* item) {
252 switch (type) {
253 case DexFile::kDexAnnotationByte:
254 item->SetByte(static_cast<int8_t>(ReadVarWidth(data, length, false)));
255 break;
256 case DexFile::kDexAnnotationShort:
257 item->SetShort(static_cast<int16_t>(ReadVarWidth(data, length, true)));
258 break;
259 case DexFile::kDexAnnotationChar:
260 item->SetChar(static_cast<uint16_t>(ReadVarWidth(data, length, false)));
261 break;
262 case DexFile::kDexAnnotationInt:
263 item->SetInt(static_cast<int32_t>(ReadVarWidth(data, length, true)));
264 break;
265 case DexFile::kDexAnnotationLong:
266 item->SetLong(static_cast<int64_t>(ReadVarWidth(data, length, true)));
267 break;
268 case DexFile::kDexAnnotationFloat: {
269 // Fill on right.
270 union {
271 float f;
272 uint32_t data;
273 } conv;
274 conv.data = static_cast<uint32_t>(ReadVarWidth(data, length, false)) << (3 - length) * 8;
275 item->SetFloat(conv.f);
276 break;
277 }
278 case DexFile::kDexAnnotationDouble: {
279 // Fill on right.
280 union {
281 double d;
282 uint64_t data;
283 } conv;
284 conv.data = ReadVarWidth(data, length, false) << (7 - length) * 8;
285 item->SetDouble(conv.d);
286 break;
287 }
288 case DexFile::kDexAnnotationString: {
289 const uint32_t string_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
290 item->SetStringId(GetStringId(string_index));
291 break;
292 }
293 case DexFile::kDexAnnotationType: {
294 const uint32_t string_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
295 item->SetTypeId(GetTypeId(string_index));
296 break;
297 }
298 case DexFile::kDexAnnotationField:
299 case DexFile::kDexAnnotationEnum: {
300 const uint32_t field_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
301 item->SetFieldId(GetFieldId(field_index));
302 break;
303 }
304 case DexFile::kDexAnnotationMethod: {
305 const uint32_t method_index = static_cast<uint32_t>(ReadVarWidth(data, length, false));
306 item->SetMethodId(GetMethodId(method_index));
307 break;
308 }
309 case DexFile::kDexAnnotationArray: {
310 EncodedValueVector* values = new EncodedValueVector();
311 const uint32_t size = DecodeUnsignedLeb128(data);
312 // Decode all elements.
313 for (uint32_t i = 0; i < size; i++) {
314 values->push_back(std::unique_ptr<EncodedValue>(ReadEncodedValue(data)));
315 }
316 item->SetEncodedArray(new EncodedArrayItem(values));
317 break;
318 }
319 case DexFile::kDexAnnotationAnnotation: {
320 AnnotationElementVector* elements = new AnnotationElementVector();
321 const uint32_t type_idx = DecodeUnsignedLeb128(data);
322 const uint32_t size = DecodeUnsignedLeb128(data);
323 // Decode all name=value pairs.
324 for (uint32_t i = 0; i < size; i++) {
325 const uint32_t name_index = DecodeUnsignedLeb128(data);
326 elements->push_back(std::unique_ptr<AnnotationElement>(
327 new AnnotationElement(GetStringId(name_index), ReadEncodedValue(data))));
328 }
329 item->SetEncodedAnnotation(new EncodedAnnotation(GetTypeId(type_idx), elements));
330 break;
331 }
332 case DexFile::kDexAnnotationNull:
333 break;
334 case DexFile::kDexAnnotationBoolean:
335 item->SetBoolean(length != 0);
336 break;
337 default:
338 break;
339 }
340}
341
342void Collections::CreateStringId(const DexFile& dex_file, uint32_t i) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800343 const DexFile::StringId& disk_string_id = dex_file.GetStringId(dex::StringIndex(i));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700344 StringData* string_data = new StringData(dex_file.GetStringData(disk_string_id));
345 string_datas_.AddItem(string_data, disk_string_id.string_data_off_);
346
347 StringId* string_id = new StringId(string_data);
348 string_ids_.AddIndexedItem(string_id, StringIdsOffset() + i * StringId::ItemSize(), i);
349}
350
351void Collections::CreateTypeId(const DexFile& dex_file, uint32_t i) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800352 const DexFile::TypeId& disk_type_id = dex_file.GetTypeId(dex::TypeIndex(i));
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800353 TypeId* type_id = new TypeId(GetStringId(disk_type_id.descriptor_idx_.index_));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700354 type_ids_.AddIndexedItem(type_id, TypeIdsOffset() + i * TypeId::ItemSize(), i);
355}
356
357void Collections::CreateProtoId(const DexFile& dex_file, uint32_t i) {
358 const DexFile::ProtoId& disk_proto_id = dex_file.GetProtoId(i);
359 const DexFile::TypeList* type_list = dex_file.GetProtoParameters(disk_proto_id);
Jeff Haoa8621002016-10-04 18:13:44 +0000360 TypeList* parameter_type_list = CreateTypeList(type_list, disk_proto_id.parameters_off_);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700361
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800362 ProtoId* proto_id = new ProtoId(GetStringId(disk_proto_id.shorty_idx_.index_),
Andreas Gampea5b09a62016-11-17 15:21:22 -0800363 GetTypeId(disk_proto_id.return_type_idx_.index_),
Jeff Hao3ab96b42016-09-09 18:35:01 -0700364 parameter_type_list);
365 proto_ids_.AddIndexedItem(proto_id, ProtoIdsOffset() + i * ProtoId::ItemSize(), i);
366}
367
368void Collections::CreateFieldId(const DexFile& dex_file, uint32_t i) {
369 const DexFile::FieldId& disk_field_id = dex_file.GetFieldId(i);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800370 FieldId* field_id = new FieldId(GetTypeId(disk_field_id.class_idx_.index_),
371 GetTypeId(disk_field_id.type_idx_.index_),
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800372 GetStringId(disk_field_id.name_idx_.index_));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700373 field_ids_.AddIndexedItem(field_id, FieldIdsOffset() + i * FieldId::ItemSize(), i);
374}
375
376void Collections::CreateMethodId(const DexFile& dex_file, uint32_t i) {
377 const DexFile::MethodId& disk_method_id = dex_file.GetMethodId(i);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800378 MethodId* method_id = new MethodId(GetTypeId(disk_method_id.class_idx_.index_),
Jeff Hao3ab96b42016-09-09 18:35:01 -0700379 GetProtoId(disk_method_id.proto_idx_),
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800380 GetStringId(disk_method_id.name_idx_.index_));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700381 method_ids_.AddIndexedItem(method_id, MethodIdsOffset() + i * MethodId::ItemSize(), i);
382}
383
384void Collections::CreateClassDef(const DexFile& dex_file, uint32_t i) {
385 const DexFile::ClassDef& disk_class_def = dex_file.GetClassDef(i);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800386 const TypeId* class_type = GetTypeId(disk_class_def.class_idx_.index_);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700387 uint32_t access_flags = disk_class_def.access_flags_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800388 const TypeId* superclass = GetTypeIdOrNullPtr(disk_class_def.superclass_idx_.index_);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700389
390 const DexFile::TypeList* type_list = dex_file.GetInterfacesList(disk_class_def);
Jeff Haoa8621002016-10-04 18:13:44 +0000391 TypeList* interfaces_type_list = CreateTypeList(type_list, disk_class_def.interfaces_off_);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700392
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800393 const StringId* source_file = GetStringIdOrNullPtr(disk_class_def.source_file_idx_.index_);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700394 // Annotations.
395 AnnotationsDirectoryItem* annotations = nullptr;
396 const DexFile::AnnotationsDirectoryItem* disk_annotations_directory_item =
397 dex_file.GetAnnotationsDirectory(disk_class_def);
398 if (disk_annotations_directory_item != nullptr) {
399 annotations = CreateAnnotationsDirectoryItem(
400 dex_file, disk_annotations_directory_item, disk_class_def.annotations_off_);
401 }
402 // Static field initializers.
403 const uint8_t* static_data = dex_file.GetEncodedStaticFieldValuesArray(disk_class_def);
404 EncodedArrayItem* static_values =
405 CreateEncodedArrayItem(static_data, disk_class_def.static_values_off_);
406 ClassData* class_data = CreateClassData(
407 dex_file, dex_file.GetClassData(disk_class_def), disk_class_def.class_data_off_);
408 ClassDef* class_def = new ClassDef(class_type, access_flags, superclass, interfaces_type_list,
409 source_file, annotations, static_values, class_data);
410 class_defs_.AddIndexedItem(class_def, ClassDefsOffset() + i * ClassDef::ItemSize(), i);
411}
412
Jeff Haoa8621002016-10-04 18:13:44 +0000413TypeList* Collections::CreateTypeList(const DexFile::TypeList* dex_type_list, uint32_t offset) {
414 if (dex_type_list == nullptr) {
Jeff Hao3ab96b42016-09-09 18:35:01 -0700415 return nullptr;
416 }
Jeff Haoea7c6292016-11-14 18:10:16 -0800417 auto found_type_list = TypeLists().find(offset);
418 if (found_type_list != TypeLists().end()) {
419 return found_type_list->second.get();
Jeff Hao3ab96b42016-09-09 18:35:01 -0700420 }
421 TypeIdVector* type_vector = new TypeIdVector();
Jeff Haoa8621002016-10-04 18:13:44 +0000422 uint32_t size = dex_type_list->Size();
Jeff Hao3ab96b42016-09-09 18:35:01 -0700423 for (uint32_t index = 0; index < size; ++index) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800424 type_vector->push_back(GetTypeId(dex_type_list->GetTypeItem(index).type_idx_.index_));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700425 }
426 TypeList* new_type_list = new TypeList(type_vector);
427 type_lists_.AddItem(new_type_list, offset);
428 return new_type_list;
429}
430
431EncodedArrayItem* Collections::CreateEncodedArrayItem(const uint8_t* static_data, uint32_t offset) {
432 if (static_data == nullptr) {
433 return nullptr;
434 }
Jeff Haoea7c6292016-11-14 18:10:16 -0800435 auto found_encoded_array_item = EncodedArrayItems().find(offset);
436 if (found_encoded_array_item != EncodedArrayItems().end()) {
437 return found_encoded_array_item->second.get();
Jeff Haoa8621002016-10-04 18:13:44 +0000438 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700439 uint32_t size = DecodeUnsignedLeb128(&static_data);
440 EncodedValueVector* values = new EncodedValueVector();
441 for (uint32_t i = 0; i < size; ++i) {
442 values->push_back(std::unique_ptr<EncodedValue>(ReadEncodedValue(&static_data)));
443 }
444 // TODO: Calculate the size of the encoded array.
445 EncodedArrayItem* encoded_array_item = new EncodedArrayItem(values);
446 encoded_array_items_.AddItem(encoded_array_item, offset);
447 return encoded_array_item;
448}
449
450AnnotationItem* Collections::CreateAnnotationItem(const DexFile::AnnotationItem* annotation,
451 uint32_t offset) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800452 auto found_annotation_item = AnnotationItems().find(offset);
453 if (found_annotation_item != AnnotationItems().end()) {
454 return found_annotation_item->second.get();
Jeff Haoa8621002016-10-04 18:13:44 +0000455 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700456 uint8_t visibility = annotation->visibility_;
457 const uint8_t* annotation_data = annotation->annotation_;
458 EncodedValue* encoded_value =
459 ReadEncodedValue(&annotation_data, DexFile::kDexAnnotationAnnotation, 0);
460 // TODO: Calculate the size of the annotation.
461 AnnotationItem* annotation_item =
462 new AnnotationItem(visibility, encoded_value->ReleaseEncodedAnnotation());
463 annotation_items_.AddItem(annotation_item, offset);
464 return annotation_item;
465}
466
467
468AnnotationSetItem* Collections::CreateAnnotationSetItem(const DexFile& dex_file,
469 const DexFile::AnnotationSetItem& disk_annotations_item, uint32_t offset) {
Jeff Haoa8621002016-10-04 18:13:44 +0000470 if (disk_annotations_item.size_ == 0 && offset == 0) {
Jeff Hao3ab96b42016-09-09 18:35:01 -0700471 return nullptr;
472 }
Jeff Haoea7c6292016-11-14 18:10:16 -0800473 auto found_anno_set_item = AnnotationSetItems().find(offset);
474 if (found_anno_set_item != AnnotationSetItems().end()) {
475 return found_anno_set_item->second.get();
Jeff Haoa8621002016-10-04 18:13:44 +0000476 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700477 std::vector<AnnotationItem*>* items = new std::vector<AnnotationItem*>();
478 for (uint32_t i = 0; i < disk_annotations_item.size_; ++i) {
479 const DexFile::AnnotationItem* annotation =
480 dex_file.GetAnnotationItem(&disk_annotations_item, i);
481 if (annotation == nullptr) {
482 continue;
483 }
484 AnnotationItem* annotation_item =
485 CreateAnnotationItem(annotation, disk_annotations_item.entries_[i]);
486 items->push_back(annotation_item);
487 }
488 AnnotationSetItem* annotation_set_item = new AnnotationSetItem(items);
489 annotation_set_items_.AddItem(annotation_set_item, offset);
490 return annotation_set_item;
491}
492
493AnnotationsDirectoryItem* Collections::CreateAnnotationsDirectoryItem(const DexFile& dex_file,
494 const DexFile::AnnotationsDirectoryItem* disk_annotations_item, uint32_t offset) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800495 auto found_anno_dir_item = AnnotationsDirectoryItems().find(offset);
496 if (found_anno_dir_item != AnnotationsDirectoryItems().end()) {
497 return found_anno_dir_item->second.get();
Jeff Haoa8621002016-10-04 18:13:44 +0000498 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700499 const DexFile::AnnotationSetItem* class_set_item =
500 dex_file.GetClassAnnotationSet(disk_annotations_item);
501 AnnotationSetItem* class_annotation = nullptr;
502 if (class_set_item != nullptr) {
503 uint32_t offset = disk_annotations_item->class_annotations_off_;
504 class_annotation = CreateAnnotationSetItem(dex_file, *class_set_item, offset);
505 }
506 const DexFile::FieldAnnotationsItem* fields =
507 dex_file.GetFieldAnnotations(disk_annotations_item);
508 FieldAnnotationVector* field_annotations = nullptr;
509 if (fields != nullptr) {
510 field_annotations = new FieldAnnotationVector();
511 for (uint32_t i = 0; i < disk_annotations_item->fields_size_; ++i) {
512 FieldId* field_id = GetFieldId(fields[i].field_idx_);
513 const DexFile::AnnotationSetItem* field_set_item =
514 dex_file.GetFieldAnnotationSetItem(fields[i]);
515 uint32_t annotation_set_offset = fields[i].annotations_off_;
516 AnnotationSetItem* annotation_set_item =
517 CreateAnnotationSetItem(dex_file, *field_set_item, annotation_set_offset);
518 field_annotations->push_back(std::unique_ptr<FieldAnnotation>(
519 new FieldAnnotation(field_id, annotation_set_item)));
520 }
521 }
522 const DexFile::MethodAnnotationsItem* methods =
523 dex_file.GetMethodAnnotations(disk_annotations_item);
524 MethodAnnotationVector* method_annotations = nullptr;
525 if (methods != nullptr) {
526 method_annotations = new MethodAnnotationVector();
527 for (uint32_t i = 0; i < disk_annotations_item->methods_size_; ++i) {
528 MethodId* method_id = GetMethodId(methods[i].method_idx_);
529 const DexFile::AnnotationSetItem* method_set_item =
530 dex_file.GetMethodAnnotationSetItem(methods[i]);
531 uint32_t annotation_set_offset = methods[i].annotations_off_;
532 AnnotationSetItem* annotation_set_item =
533 CreateAnnotationSetItem(dex_file, *method_set_item, annotation_set_offset);
534 method_annotations->push_back(std::unique_ptr<MethodAnnotation>(
535 new MethodAnnotation(method_id, annotation_set_item)));
536 }
537 }
538 const DexFile::ParameterAnnotationsItem* parameters =
539 dex_file.GetParameterAnnotations(disk_annotations_item);
540 ParameterAnnotationVector* parameter_annotations = nullptr;
541 if (parameters != nullptr) {
542 parameter_annotations = new ParameterAnnotationVector();
543 for (uint32_t i = 0; i < disk_annotations_item->parameters_size_; ++i) {
544 MethodId* method_id = GetMethodId(parameters[i].method_idx_);
545 const DexFile::AnnotationSetRefList* list =
546 dex_file.GetParameterAnnotationSetRefList(&parameters[i]);
547 parameter_annotations->push_back(std::unique_ptr<ParameterAnnotation>(
548 GenerateParameterAnnotation(dex_file, method_id, list, parameters[i].annotations_off_)));
549 }
550 }
551 // TODO: Calculate the size of the annotations directory.
552 AnnotationsDirectoryItem* annotations_directory_item = new AnnotationsDirectoryItem(
553 class_annotation, field_annotations, method_annotations, parameter_annotations);
554 annotations_directory_items_.AddItem(annotations_directory_item, offset);
555 return annotations_directory_item;
556}
557
558ParameterAnnotation* Collections::GenerateParameterAnnotation(
559 const DexFile& dex_file, MethodId* method_id,
560 const DexFile::AnnotationSetRefList* annotation_set_ref_list, uint32_t offset) {
Jeff Haoa8621002016-10-04 18:13:44 +0000561 AnnotationSetRefList* set_ref_list = nullptr;
Jeff Haoea7c6292016-11-14 18:10:16 -0800562 auto found_set_ref_list = AnnotationSetRefLists().find(offset);
563 if (found_set_ref_list != AnnotationSetRefLists().end()) {
564 set_ref_list = found_set_ref_list->second.get();
Jeff Hao3ab96b42016-09-09 18:35:01 -0700565 }
Jeff Haoa8621002016-10-04 18:13:44 +0000566 if (set_ref_list == nullptr) {
567 std::vector<AnnotationSetItem*>* annotations = new std::vector<AnnotationSetItem*>();
568 for (uint32_t i = 0; i < annotation_set_ref_list->size_; ++i) {
569 const DexFile::AnnotationSetItem* annotation_set_item =
570 dex_file.GetSetRefItemItem(&annotation_set_ref_list->list_[i]);
571 uint32_t set_offset = annotation_set_ref_list->list_[i].annotations_off_;
572 annotations->push_back(CreateAnnotationSetItem(dex_file, *annotation_set_item, set_offset));
573 }
574 set_ref_list = new AnnotationSetRefList(annotations);
575 annotation_set_ref_lists_.AddItem(set_ref_list, offset);
576 }
577 return new ParameterAnnotation(method_id, set_ref_list);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700578}
579
580CodeItem* Collections::CreateCodeItem(const DexFile& dex_file,
581 const DexFile::CodeItem& disk_code_item, uint32_t offset) {
582 uint16_t registers_size = disk_code_item.registers_size_;
583 uint16_t ins_size = disk_code_item.ins_size_;
584 uint16_t outs_size = disk_code_item.outs_size_;
585 uint32_t tries_size = disk_code_item.tries_size_;
586
587 // TODO: Calculate the size of the debug info.
588 const uint8_t* debug_info_stream = dex_file.GetDebugInfoStream(&disk_code_item);
589 DebugInfoItem* debug_info = nullptr;
590 if (debug_info_stream != nullptr) {
Jeff Haoa8621002016-10-04 18:13:44 +0000591 uint32_t debug_info_size = GetDebugInfoStreamSize(debug_info_stream);
592 uint8_t* debug_info_buffer = new uint8_t[debug_info_size];
593 memcpy(debug_info_buffer, debug_info_stream, debug_info_size);
594 debug_info = new DebugInfoItem(debug_info_size, debug_info_buffer);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700595 debug_info_items_.AddItem(debug_info, disk_code_item.debug_info_off_);
596 }
597
598 uint32_t insns_size = disk_code_item.insns_size_in_code_units_;
599 uint16_t* insns = new uint16_t[insns_size];
600 memcpy(insns, disk_code_item.insns_, insns_size * sizeof(uint16_t));
601
602 TryItemVector* tries = nullptr;
Jeff Haoa8621002016-10-04 18:13:44 +0000603 CatchHandlerVector* handler_list = nullptr;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700604 if (tries_size > 0) {
605 tries = new TryItemVector();
Jeff Haoa8621002016-10-04 18:13:44 +0000606 handler_list = new CatchHandlerVector();
Jeff Hao3ab96b42016-09-09 18:35:01 -0700607 for (uint32_t i = 0; i < tries_size; ++i) {
608 const DexFile::TryItem* disk_try_item = dex_file.GetTryItems(disk_code_item, i);
609 uint32_t start_addr = disk_try_item->start_addr_;
610 uint16_t insn_count = disk_try_item->insn_count_;
Jeff Haoa8621002016-10-04 18:13:44 +0000611 uint16_t handler_off = disk_try_item->handler_off_;
612 const CatchHandler* handlers = nullptr;
613 for (std::unique_ptr<const CatchHandler>& existing_handlers : *handler_list) {
614 if (handler_off == existing_handlers->GetListOffset()) {
615 handlers = existing_handlers.get();
616 }
617 }
618 if (handlers == nullptr) {
619 bool catch_all = false;
620 TypeAddrPairVector* addr_pairs = new TypeAddrPairVector();
621 for (CatchHandlerIterator it(disk_code_item, *disk_try_item); it.HasNext(); it.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800622 const dex::TypeIndex type_index = it.GetHandlerTypeIndex();
623 const TypeId* type_id = GetTypeIdOrNullPtr(type_index.index_);
Jeff Haoa8621002016-10-04 18:13:44 +0000624 catch_all |= type_id == nullptr;
625 addr_pairs->push_back(std::unique_ptr<const TypeAddrPair>(
626 new TypeAddrPair(type_id, it.GetHandlerAddress())));
627 }
628 handlers = new CatchHandler(catch_all, handler_off, addr_pairs);
629 handler_list->push_back(std::unique_ptr<const CatchHandler>(handlers));
Jeff Hao3ab96b42016-09-09 18:35:01 -0700630 }
631 TryItem* try_item = new TryItem(start_addr, insn_count, handlers);
632 tries->push_back(std::unique_ptr<const TryItem>(try_item));
633 }
634 }
Jeff Haoea7c6292016-11-14 18:10:16 -0800635 uint32_t size = GetCodeItemSize(dex_file, disk_code_item);
Jeff Haoa8621002016-10-04 18:13:44 +0000636 CodeItem* code_item = new CodeItem(
637 registers_size, ins_size, outs_size, debug_info, insns_size, insns, tries, handler_list);
Jeff Haoea7c6292016-11-14 18:10:16 -0800638 code_item->SetSize(size);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700639 code_items_.AddItem(code_item, offset);
David Sehrd1e44e22016-10-06 17:09:32 -0700640 // Add "fixup" references to types, strings, methods, and fields.
641 // This is temporary, as we will probably want more detailed parsing of the
642 // instructions here.
643 std::unique_ptr<std::vector<TypeId*>> type_ids(new std::vector<TypeId*>());
644 std::unique_ptr<std::vector<StringId*>> string_ids(new std::vector<StringId*>());
645 std::unique_ptr<std::vector<MethodId*>> method_ids(new std::vector<MethodId*>());
646 std::unique_ptr<std::vector<FieldId*>> field_ids(new std::vector<FieldId*>());
647 if (GetIdsFromByteCode(*this,
648 code_item,
649 type_ids.get(),
650 string_ids.get(),
651 method_ids.get(),
652 field_ids.get())) {
653 CodeFixups* fixups = new CodeFixups(type_ids.release(),
654 string_ids.release(),
655 method_ids.release(),
656 field_ids.release());
657 code_item->SetCodeFixups(fixups);
658 }
659
Jeff Hao3ab96b42016-09-09 18:35:01 -0700660 return code_item;
661}
662
663MethodItem* Collections::GenerateMethodItem(const DexFile& dex_file, ClassDataItemIterator& cdii) {
664 MethodId* method_item = GetMethodId(cdii.GetMemberIndex());
665 uint32_t access_flags = cdii.GetRawMemberAccessFlags();
666 const DexFile::CodeItem* disk_code_item = cdii.GetMethodCodeItem();
667 CodeItem* code_item = nullptr;
668 DebugInfoItem* debug_info = nullptr;
669 if (disk_code_item != nullptr) {
670 code_item = CreateCodeItem(dex_file, *disk_code_item, cdii.GetMethodCodeItemOffset());
671 debug_info = code_item->DebugInfo();
672 }
673 if (debug_info != nullptr) {
674 bool is_static = (access_flags & kAccStatic) != 0;
675 dex_file.DecodeDebugLocalInfo(
676 disk_code_item, is_static, cdii.GetMemberIndex(), GetLocalsCb, debug_info);
677 dex_file.DecodeDebugPositionInfo(disk_code_item, GetPositionsCb, debug_info);
678 }
679 return new MethodItem(access_flags, method_item, code_item);
680}
681
682ClassData* Collections::CreateClassData(
683 const DexFile& dex_file, const uint8_t* encoded_data, uint32_t offset) {
684 // Read the fields and methods defined by the class, resolving the circular reference from those
685 // to classes by setting class at the same time.
686 ClassData* class_data = nullptr;
687 if (encoded_data != nullptr) {
688 ClassDataItemIterator cdii(dex_file, encoded_data);
689 // Static fields.
690 FieldItemVector* static_fields = new FieldItemVector();
691 for (uint32_t i = 0; cdii.HasNextStaticField(); i++, cdii.Next()) {
692 FieldId* field_item = GetFieldId(cdii.GetMemberIndex());
693 uint32_t access_flags = cdii.GetRawMemberAccessFlags();
694 static_fields->push_back(std::unique_ptr<FieldItem>(new FieldItem(access_flags, field_item)));
695 }
696 // Instance fields.
697 FieldItemVector* instance_fields = new FieldItemVector();
698 for (uint32_t i = 0; cdii.HasNextInstanceField(); i++, cdii.Next()) {
699 FieldId* field_item = GetFieldId(cdii.GetMemberIndex());
700 uint32_t access_flags = cdii.GetRawMemberAccessFlags();
701 instance_fields->push_back(
702 std::unique_ptr<FieldItem>(new FieldItem(access_flags, field_item)));
703 }
704 // Direct methods.
705 MethodItemVector* direct_methods = new MethodItemVector();
706 for (uint32_t i = 0; cdii.HasNextDirectMethod(); i++, cdii.Next()) {
707 direct_methods->push_back(
708 std::unique_ptr<MethodItem>(GenerateMethodItem(dex_file, cdii)));
709 }
710 // Virtual methods.
711 MethodItemVector* virtual_methods = new MethodItemVector();
712 for (uint32_t i = 0; cdii.HasNextVirtualMethod(); i++, cdii.Next()) {
713 virtual_methods->push_back(
714 std::unique_ptr<MethodItem>(GenerateMethodItem(dex_file, cdii)));
715 }
Jeff Hao3ab96b42016-09-09 18:35:01 -0700716 class_data = new ClassData(static_fields, instance_fields, direct_methods, virtual_methods);
Jeff Haoea7c6292016-11-14 18:10:16 -0800717 class_data->SetSize(cdii.EndDataPointer() - encoded_data);
Jeff Hao3ab96b42016-09-09 18:35:01 -0700718 class_datas_.AddItem(class_data, offset);
719 }
720 return class_data;
721}
722
Jeff Hao3ab96b42016-09-09 18:35:01 -0700723} // namespace dex_ir
724} // namespace art