Lines Matching refs:dex
27 namespace dex {
29 Reader::Reader(const dex::u1* image, size_t size) : image_(image), size_(size) {
31 header_ = ptr<dex::Header>(0);
34 // start with an "empty" .dex IR
36 dex_ir_->magic = slicer::MemView(header_, sizeof(dex::Header::magic));
39 slicer::ArrayView<const dex::ClassDef> Reader::ClassDefs() const {
40 return section<dex::ClassDef>(header_->class_defs_off,
44 slicer::ArrayView<const dex::StringId> Reader::StringIds() const {
45 return section<dex::StringId>(header_->string_ids_off,
49 slicer::ArrayView<const dex::TypeId> Reader::TypeIds() const {
50 return section<dex::TypeId>(header_->type_ids_off,
54 slicer::ArrayView<const dex::FieldId> Reader::FieldIds() const {
55 return section<dex::FieldId>(header_->field_ids_off,
59 slicer::ArrayView<const dex::MethodId> Reader::MethodIds() const {
60 return section<dex::MethodId>(header_->method_ids_off,
64 slicer::ArrayView<const dex::ProtoId> Reader::ProtoIds() const {
65 return section<dex::ProtoId>(header_->proto_ids_off,
69 const dex::MapList* Reader::DexMapList() const {
70 return dataPtr<dex::MapList>(header_->map_off);
73 const char* Reader::GetStringMUTF8(dex::u4 index) const {
74 if (index == dex::kNoIndex) {
77 const dex::u1* strData = GetStringData(index);
78 dex::ReadULeb128(&strData);
89 void Reader::CreateClassIr(dex::u4 index) {
96 dex::u4 Reader::FindClassIndex(const char* class_descriptor) const {
99 for (dex::u4 i = 0; i < classes.size(); ++i) {
106 return dex::kNoIndex;
109 // map a .dex index to corresponding .dex IR node
113 // .dex IR nodes is 1:1
121 ir::Class* Reader::GetClass(dex::u4 index) {
122 SLICER_CHECK(index != dex::kNoIndex);
136 // map a .dex index to corresponding .dex IR node
138 ir::Type* Reader::GetType(dex::u4 index) {
139 SLICER_CHECK(index != dex::kNoIndex);
153 // map a .dex index to corresponding .dex IR node
155 ir::FieldDecl* Reader::GetFieldDecl(dex::u4 index) {
156 SLICER_CHECK(index != dex::kNoIndex);
170 // map a .dex index to corresponding .dex IR node
172 ir::MethodDecl* Reader::GetMethodDecl(dex::u4 index) {
173 SLICER_CHECK(index != dex::kNoIndex);
187 // map a .dex index to corresponding .dex IR node
189 ir::Proto* Reader::GetProto(dex::u4 index) {
190 SLICER_CHECK(index != dex::kNoIndex);
204 // map a .dex index to corresponding .dex IR node
206 ir::String* Reader::GetString(dex::u4 index) {
207 SLICER_CHECK(index != dex::kNoIndex);
221 ir::Class* Reader::ParseClass(dex::u4 index) {
232 if (dex_class_def.superclass_idx != dex::kNoIndex) {
236 if (dex_class_def.source_file_idx != dex::kNoIndex) {
241 const dex::u1* class_data = dataPtr<dex::u1>(dex_class_def.class_data_off);
243 dex::u4 static_fields_count = dex::ReadULeb128(&class_data);
244 dex::u4 instance_fields_count = dex::ReadULeb128(&class_data);
245 dex::u4 direct_methods_count = dex::ReadULeb128(&class_data);
246 dex::u4 virtual_methods_count = dex::ReadULeb128(&class_data);
248 dex::u4 base_index = dex::kNoIndex;
249 for (dex::u4 i = 0; i < static_fields_count; ++i) {
254 base_index = dex::kNoIndex;
255 for (dex::u4 i = 0; i < instance_fields_count; ++i) {
260 base_index = dex::kNoIndex;
261 for (dex::u4 i = 0; i < direct_methods_count; ++i) {
266 base_index = dex::kNoIndex;
267 for (dex::u4 i = 0; i < virtual_methods_count; ++i) {
280 ir::AnnotationsDirectory* Reader::ExtractAnnotations(dex::u4 offset) {
292 auto dex_annotations = dataPtr<dex::AnnotationsDirectoryItem>(offset);
297 const dex::u1* ptr = reinterpret_cast<const dex::u1*>(dex_annotations + 1);
299 for (dex::u4 i = 0; i < dex_annotations->fields_size; ++i) {
303 for (dex::u4 i = 0; i < dex_annotations->methods_size; ++i) {
307 for (dex::u4 i = 0; i < dex_annotations->parameters_size; ++i) {
314 ir::Annotation* Reader::ExtractAnnotationItem(dex::u4 offset) {
320 auto dexAnnotationItem = dataPtr<dex::AnnotationItem>(offset);
321 const dex::u1* ptr = dexAnnotationItem->annotation;
328 ir::AnnotationSet* Reader::ExtractAnnotationSet(dex::u4 offset) {
340 auto dex_annotation_set = dataPtr<dex::AnnotationSetItem>(offset);
341 for (dex::u4 i = 0; i < dex_annotation_set->size; ++i) {
350 ir::AnnotationSetRefList* Reader::ExtractAnnotationSetRefList(dex::u4 offset) {
353 auto dex_annotation_set_ref_list = dataPtr<dex::AnnotationSetRefList>(offset);
356 for (dex::u4 i = 0; i < dex_annotation_set_ref_list->size; ++i) {
357 dex::u4 entry_offset = dex_annotation_set_ref_list->list[i].annotations_off;
368 ir::FieldAnnotation* Reader::ParseFieldAnnotation(const dex::u1** pptr) {
369 auto dex_field_annotation = reinterpret_cast<const dex::FieldAnnotationsItem*>(*pptr);
378 *pptr += sizeof(dex::FieldAnnotationsItem);
382 ir::MethodAnnotation* Reader::ParseMethodAnnotation(const dex::u1** pptr) {
384 reinterpret_cast<const dex::MethodAnnotationsItem*>(*pptr);
393 *pptr += sizeof(dex::MethodAnnotationsItem);
397 ir::ParamAnnotation* Reader::ParseParamAnnotation(const dex::u1** pptr) {
399 reinterpret_cast<const dex::ParameterAnnotationsItem*>(*pptr);
408 *pptr += sizeof(dex::ParameterAnnotationsItem);
412 ir::EncodedField* Reader::ParseEncodedField(const dex::u1** pptr, dex::u4* base_index) {
415 auto field_index = dex::ReadULeb128(pptr);
416 SLICER_CHECK(field_index != dex::kNoIndex);
417 if (*base_index != dex::kNoIndex) {
424 ir_encoded_field->access_flags = dex::ReadULeb128(pptr);
432 static T ParseIntValue(const dex::u1** pptr, size_t size) {
455 static T ParseFloatValue(const dex::u1** pptr, size_t size) {
461 for (dex::u1* p = reinterpret_cast<dex::u1*>(&value) + start_byte; size > 0;
468 ir::EncodedValue* Reader::ParseEncodedValue(const dex::u1** pptr) {
473 dex::u1 header = *(*pptr)++;
474 dex::u1 type = header & dex::kEncodedValueTypeMask;
475 dex::u1 arg = header >> dex::kEncodedValueArgShift;
480 case dex::kEncodedByte:
484 case dex::kEncodedShort:
488 case dex::kEncodedChar:
492 case dex::kEncodedInt:
496 case dex::kEncodedLong:
500 case dex::kEncodedFloat:
504 case dex::kEncodedDouble:
508 case dex
509 dex::u4 index = ParseIntValue<dex::u4>(pptr, arg + 1);
513 case dex::kEncodedType: {
514 dex::u4 index = ParseIntValue<dex::u4>(pptr, arg + 1);
518 case dex::kEncodedField: {
519 dex::u4 index = ParseIntValue<dex::u4>(pptr, arg + 1);
523 case dex::kEncodedMethod: {
524 dex::u4 index = ParseIntValue<dex::u4>(pptr, arg + 1);
528 case dex::kEncodedEnum: {
529 dex::u4 index = ParseIntValue<dex::u4>(pptr, arg + 1);
533 case dex::kEncodedArray:
538 case dex::kEncodedAnnotation:
543 case dex::kEncodedNull:
547 case dex::kEncodedBoolean:
561 ir::Annotation* Reader::ParseAnnotation(const dex::u1** pptr) {
564 dex::u4 type_index = dex::ReadULeb128(pptr);
565 dex::u4 elements_count = dex::ReadULeb128(pptr);
568 ir_annotation->visibility = dex::kVisibilityEncoded;
570 for (dex::u4 i = 0; i < elements_count; ++i) {
573 ir_element->name = GetString(dex::ReadULeb128(pptr));
582 ir::EncodedArray* Reader::ParseEncodedArray(const dex::u1** pptr) {
585 dex::u4 count = dex::ReadULeb128(pptr);
586 for (dex::u4 i = 0; i < count; ++i) {
593 ir::EncodedArray* Reader::ExtractEncodedArray(dex::u4 offset) {
601 auto ptr = dataPtr<dex::u1>(offset);
607 ir::DebugInfo* Reader::ExtractDebugInfo(dex::u4 offset) {
613 const dex::u1* ptr = dataPtr<dex::u1>(offset);
615 ir_debug_info->line_start = dex::ReadULeb128(&ptr);
618 dex::u4 param_count = dex::ReadULeb128(&ptr);
619 for (dex::u4 i = 0; i < param_count; ++i) {
620 dex::u4 name_index = dex::ReadULeb128(&ptr) - 1;
622 (name_index == dex::kNoIndex) ? nullptr : GetString(name_index);
633 dex::u1 opcode = 0;
634 while ((opcode = *ptr++) != dex::DBG_END_SEQUENCE) {
636 case dex::DBG_ADVANCE_PC:
638 dex::ReadULeb128(&ptr);
641 case dex::DBG_ADVANCE_LINE:
643 dex::ReadSLeb128(&ptr);
646 case dex::DBG_START_LOCAL: {
648 dex::ReadULeb128(&ptr);
650 dex::u4 name_index = dex::ReadULeb128(&ptr) - 1;
651 if (name_index != dex::kNoIndex) {
655 dex::u4 type_index = dex::ReadULeb128(&ptr) - 1;
656 if (type_index != dex::kNoIndex) {
661 case dex::DBG_START_LOCAL_EXTENDED: {
663 dex::ReadULeb128(&ptr);
665 dex::u4 name_index = dex::ReadULeb128(&ptr) - 1;
666 if (name_index != dex::kNoIndex) {
670 dex::u4 type_index = dex::ReadULeb128(&ptr) - 1;
671 if (type_index != dex::kNoIndex) {
675 dex::u4 sig_index = dex::ReadULeb128(&ptr) - 1;
676 if (sig_index != dex::kNoIndex) {
681 case dex::DBG_END_LOCAL:
682 case dex::DBG_RESTART_LOCAL:
684 dex::ReadULeb128(&ptr);
687 case dex::DBG_SET_FILE: {
688 dex::u4 name_index = dex::ReadULeb128(&ptr) - 1;
689 if (name_index != dex::kNoIndex) {
701 ir::Code* Reader::ExtractCode(dex::u4 offset) {
708 auto dex_code = dataPtr<dex::Code>(offset);
717 slicer::ArrayView<const dex::u2>(dex_code->insns, dex_code->insns_size);
728 dex::u4 aligned_count = (dex_code->insns_size + 1) / 2 * 2;
730 reinterpret_cast<const dex::TryBlock*>(dex_code->insns + aligned_count);
732 reinterpret_cast<const dex::u1*>(tries + dex_code->tries_size);
735 slicer::ArrayView<const dex::TryBlock>(tries, dex_code->tries_size);
740 dex::u4 handlers_count = dex::ReadULeb128(&ptr);
743 for (dex::u4 handler_index = 0; handler_index < handlers_count; ++handler_index) {
744 int catch_count = dex::ReadSLeb128(&ptr);
747 dex::u4 type_index = dex::ReadULeb128(&ptr);
751 dex::ReadULeb128(&ptr);
756 dex::ReadULeb128(&ptr);
768 ir::EncodedMethod* Reader::ParseEncodedMethod(const dex::u1** pptr, dex::u4* base_index) {
771 auto method_index = dex::ReadULeb128(pptr);
772 SLICER_CHECK(method_index != dex::kNoIndex);
773 if (*base_index != dex::kNoIndex) {
780 ir_encoded_method->access_flags = dex::ReadULeb128(pptr);
782 dex::u4 code_offset = dex::ReadULeb128(pptr);
791 ir::Type* Reader::ParseType(dex::u4 index) {
801 ir::FieldDecl* Reader::ParseFieldDecl(dex::u4 index) {
813 ir::MethodDecl* Reader::ParseMethodDecl(dex::u4 index) {
825 ir::TypeList* Reader::ExtractTypeList(dex::u4 offset) {
835 auto dex_type_list = dataPtr<dex::TypeList>(offset);
838 for (dex::u4 i = 0; i < dex_type_list->size; ++i) {
846 ir::Proto* Reader::ParseProto(dex::u4 index) {
861 ir::String* Reader::ParseString(dex::u4 index) {
866 dex::ReadULeb128(&cstr);
878 void Reader::ParseInstructions(slicer::ArrayView<const dex::u2> code) {
879 const dex::u2* ptr = code.begin();
881 auto dex_instr = dex::DecodeInstruction(ptr);
883 dex::u4 index = dex::kNoIndex;
884 switch (dex::GetFormatFromOpcode(dex_instr.opcode)) {
885 case dex::kFmt20bc:
886 case dex::kFmt21c:
887 case dex::kFmt31c:
888 case dex::kFmt35c:
889 case dex::kFmt3rc:
893 case dex::kFmt22c:
902 case dex::kIndexStringRef:
906 case dex::kIndexTypeRef:
910 case dex::kIndexFieldRef:
914 case dex::kIndexMethodRef:
922 auto isize = dex::GetWidthFromBytecode(ptr);
929 // Basic .dex header structural checks
931 SLICER_CHECK(size_ > sizeof(dex::Header));
936 SLICER_CHECK(header_->header_size == sizeof(dex::Header));
937 SLICER_CHECK(header_->endian_tag == dex::kEndianConstant);
957 // we seem to have .dex files with extra bytes at the end ...
971 auto map_list = ptr<dex::MapList>(header_->map_off);
974 sizeof(dex::u4) + sizeof(dex::MapItem) * map_list->size;
978 } // namespace dex