1 /** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef LIBPANDAFILE_FILE_READER_H 17 #define LIBPANDAFILE_FILE_READER_H 18 19 #include <type_traits> 20 #include "annotation_data_accessor.h" 21 #include "bytecode_instruction.h" 22 #include "class_data_accessor.h" 23 #include "code_data_accessor.h" 24 #include "debug_data_accessor.h" 25 #include "field_data_accessor.h" 26 #include "file_item_container.h" 27 #include "libpandafile/helpers.h" 28 #include "literal_data_accessor.h" 29 #include "method_data_accessor.h" 30 #include "method_handle_data_accessor.h" 31 #include "os/file.h" 32 #include "param_annotations_data_accessor.h" 33 #include "proto_data_accessor.h" 34 #include "utils/pandargs.h" 35 #include "utils/span.h" 36 #include "utils/type_helpers.h" 37 #include "utils/leb128.h" 38 #if !PANDA_TARGET_WINDOWS 39 #include "securec.h" 40 #endif 41 42 #include <cstdint> 43 #include <cerrno> 44 45 #include <limits> 46 #include <vector> 47 48 namespace panda::panda_file { 49 50 class FileReader { 51 public: 52 // default methods FileReader(std::unique_ptr<const File> && file)53 explicit FileReader(std::unique_ptr<const File> &&file) : file_(std::move(file)) {} 54 virtual ~FileReader() = default; 55 56 bool ReadContainer(); 57 GetContainerPtr()58 ItemContainer *GetContainerPtr() 59 { 60 return &container_; 61 } 62 63 void ComputeLayoutAndUpdateIndices(); 64 65 NO_COPY_SEMANTIC(FileReader); 66 NO_MOVE_SEMANTIC(FileReader); 67 68 private: 69 bool ReadLiteralArrayItems(); 70 bool ReadIndexHeaders(); 71 bool ReadClasses(); 72 73 bool CreateLiteralArrayItem(const LiteralDataAccessor::LiteralValue &lit_value, const LiteralTag &tag, 74 File::EntityId array_id); 75 AnnotationItem *CreateAnnotationItem(File::EntityId ann_id); 76 MethodItem *CreateMethodItem(ClassItem *cls, File::EntityId method_id); 77 ForeignMethodItem *CreateForeignMethodItem(BaseClassItem *fcls, File::EntityId method_id); 78 FieldItem *CreateFieldItem(ClassItem *cls, File::EntityId field_id); 79 ForeignFieldItem *CreateForeignFieldItem(BaseClassItem *fcls, File::EntityId field_id); 80 ClassItem *CreateClassItem(File::EntityId class_id); 81 ForeignClassItem *CreateForeignClassItem(File::EntityId class_id); 82 MethodHandleItem *CreateMethodHandleItem(File::EntityId mh_id); 83 TypeItem *CreateParamTypeItem(ProtoDataAccessor *proto_acc, size_t param_num, size_t reference_num); 84 std::vector<MethodParamItem> CreateMethodParamItems(ProtoDataAccessor *proto_acc, MethodDataAccessor *method_acc, 85 size_t reference_num); 86 DebugInfoItem *CreateDebugInfoItem(File::EntityId debug_info_id); 87 void UpdateDebugInfoDependecies(File::EntityId debug_info_id); 88 void UpdateDebugInfo(DebugInfoItem *debug_info_item, File::EntityId debug_info_id); 89 90 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true> SetIntegerFieldValue(FieldDataAccessor * field_acc,FieldItem * field_item)91 void SetIntegerFieldValue(FieldDataAccessor *field_acc, FieldItem *field_item) 92 { 93 auto value = field_acc->GetValue<T>(); 94 95 if (!value) { 96 return; 97 } 98 99 // NOLINTNEXTLINE(readability-braces-around-statements) 100 if constexpr (is_same_v<T, int64_t> || is_same_v<T, uint64_t>) { 101 auto *value_item = container_.GetOrCreateLongValueItem(value.value()); 102 field_item->SetValue(value_item); 103 // NOLINTNEXTLINE(readability-misleading-indentation) 104 } else { 105 auto *value_item = container_.GetOrCreateIntegerValueItem(value.value()); 106 field_item->SetValue(value_item); 107 } 108 } 109 110 template <typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true> SetFloatFieldValue(FieldDataAccessor * field_acc,FieldItem * field_item)111 void SetFloatFieldValue(FieldDataAccessor *field_acc, FieldItem *field_item) 112 { 113 auto value = field_acc->GetValue<T>(); 114 115 if (!value) { 116 return; 117 } 118 119 // NOLINTNEXTLINE(readability-braces-around-statements) 120 if constexpr (is_same_v<T, double>) { 121 auto *value_item = container_.GetOrCreateDoubleValueItem(value.value()); 122 field_item->SetValue(value_item); 123 // NOLINTNEXTLINE(readability-misleading-indentation) 124 } else { 125 auto *value_item = container_.GetOrCreateFloatValueItem(value.value()); 126 field_item->SetValue(value_item); 127 } 128 } 129 SetStringFieldValue(FieldDataAccessor * field_acc,FieldItem * field_item)130 void SetStringFieldValue(FieldDataAccessor *field_acc, FieldItem *field_item) 131 { 132 auto value = field_acc->GetValue<uint32_t>(); 133 134 if (value) { 135 panda_file::File::EntityId string_id(value.value()); 136 auto data = file_->GetStringData(string_id); 137 std::string string_data(reinterpret_cast<const char *>(data.data)); 138 auto *string_item = container_.GetOrCreateStringItem(string_data); 139 auto *value_item = container_.GetOrCreateIdValueItem(string_item); 140 field_item->SetValue(value_item); 141 } 142 } 143 144 // Creates foreign or non-foreign method item CreateGenericMethodItem(BaseClassItem * class_item,File::EntityId method_id)145 inline BaseItem *CreateGenericMethodItem(BaseClassItem *class_item, File::EntityId method_id) 146 { 147 if (file_->IsExternal(method_id)) { 148 return CreateForeignMethodItem(class_item, method_id); 149 } 150 return CreateMethodItem(static_cast<ClassItem *>(class_item), method_id); 151 } 152 153 // Creates foreign or non-foreign field item CreateGenericFieldItem(BaseClassItem * class_item,File::EntityId field_id)154 inline BaseItem *CreateGenericFieldItem(BaseClassItem *class_item, File::EntityId field_id) 155 { 156 if (file_->IsExternal(field_id)) { 157 return CreateForeignFieldItem(class_item, field_id); 158 } 159 return CreateFieldItem(static_cast<ClassItem *>(class_item), field_id); 160 } 161 162 // Creates foreign or non-foreign class item CreateGenericClassItem(File::EntityId class_id)163 inline BaseClassItem *CreateGenericClassItem(File::EntityId class_id) 164 { 165 if (file_->IsExternal(class_id)) { 166 return CreateForeignClassItem(class_id); 167 } 168 return CreateClassItem(class_id); 169 } 170 171 void UpdateCodeAndDebugInfoDependencies(const std::map<BaseItem *, File::EntityId> &reverse_done); 172 173 std::unique_ptr<const File> file_; 174 ItemContainer container_; 175 std::map<File::EntityId, BaseItem *> items_done_; 176 }; 177 178 } // namespace panda::panda_file 179 180 #endif // LIBPANDAFILE_FILE_READER_H 181