• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef LIBPANDAFILE_FILE_READER_H_
16 #define LIBPANDAFILE_FILE_READER_H_
17 
18 #include <type_traits>
19 #include "annotation_data_accessor.h"
20 #include "bytecode_instruction.h"
21 #include "class_data_accessor.h"
22 #include "code_data_accessor.h"
23 #include "debug_data_accessor.h"
24 #include "field_data_accessor.h"
25 #include "file_item_container.h"
26 #include "libpandafile/helpers.h"
27 #include "literal_data_accessor.h"
28 #include "method_data_accessor.h"
29 #include "method_handle_data_accessor.h"
30 #include "os/file.h"
31 #include "param_annotations_data_accessor.h"
32 #include "proto_data_accessor.h"
33 #include "utils/pandargs.h"
34 #include "utils/span.h"
35 #include "utils/type_helpers.h"
36 #include "utils/leb128.h"
37 #if !PANDA_TARGET_WINDOWS
38 #include "securec.h"
39 #endif
40 
41 #include <cstdint>
42 #include <cerrno>
43 
44 #include <limits>
45 #include <vector>
46 
47 namespace panda::panda_file {
48 
49 class FileReader {
50 public:
51     // default methods
FileReader(std::unique_ptr<const File> && file)52     explicit FileReader(std::unique_ptr<const File> &&file) : file_(std::move(file)) {}
53     virtual ~FileReader() = default;
54 
55     bool ReadContainer();
56 
GetContainerPtr()57     ItemContainer *GetContainerPtr()
58     {
59         return &container_;
60     }
61 
62     void ComputeLayoutAndUpdateIndices();
63 
64     NO_COPY_SEMANTIC(FileReader);
65     NO_MOVE_SEMANTIC(FileReader);
66 
67 private:
68     bool ReadLiteralArrayItems();
69     bool ReadIndexHeaders();
70     bool ReadClasses();
71 
72     bool CreateLiteralArrayItem(const LiteralDataAccessor::LiteralValue &lit_value, const LiteralTag &tag,
73                                 File::EntityId array_id);
74     AnnotationItem *CreateAnnotationItem(File::EntityId ann_id);
75     MethodItem *CreateMethodItem(ClassItem *cls, File::EntityId method_id);
76     ForeignMethodItem *CreateForeignMethodItem(BaseClassItem *fcls, File::EntityId method_id);
77     FieldItem *CreateFieldItem(ClassItem *cls, File::EntityId field_id);
78     ForeignFieldItem *CreateForeignFieldItem(BaseClassItem *fcls, File::EntityId field_id);
79     ClassItem *CreateClassItem(File::EntityId class_id);
80     ForeignClassItem *CreateForeignClassItem(File::EntityId class_id);
81     MethodHandleItem *CreateMethodHandleItem(File::EntityId mh_id);
82     TypeItem *CreateParamTypeItem(ProtoDataAccessor *proto_acc, size_t param_num, size_t reference_num);
83     std::vector<MethodParamItem> CreateMethodParamItems(ProtoDataAccessor *proto_acc, MethodDataAccessor *method_acc,
84                                                         size_t reference_num);
85     DebugInfoItem *CreateDebugInfoItem(File::EntityId debug_info_id);
86     void UpdateDebugInfoDependecies(File::EntityId debug_info_id);
87     void UpdateDebugInfo(DebugInfoItem *debug_info_item, File::EntityId debug_info_id);
88 
89     template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
SetIntegerFieldValue(FieldDataAccessor * field_acc,FieldItem * field_item)90     void SetIntegerFieldValue(FieldDataAccessor *field_acc, FieldItem *field_item)
91     {
92         auto value = field_acc->GetValue<T>();
93 
94         if (!value) {
95             return;
96         }
97 
98         // NOLINTNEXTLINE(readability-braces-around-statements)
99         if constexpr (is_same_v<T, int64_t> || is_same_v<T, uint64_t>) {
100             auto *value_item = container_.GetOrCreateLongValueItem(value.value());
101             field_item->SetValue(value_item);
102             // NOLINTNEXTLINE(readability-misleading-indentation)
103         } else {
104             auto *value_item = container_.GetOrCreateIntegerValueItem(value.value());
105             field_item->SetValue(value_item);
106         }
107     }
108 
109     template <typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
SetFloatFieldValue(FieldDataAccessor * field_acc,FieldItem * field_item)110     void SetFloatFieldValue(FieldDataAccessor *field_acc, FieldItem *field_item)
111     {
112         auto value = field_acc->GetValue<T>();
113 
114         if (!value) {
115             return;
116         }
117 
118         // NOLINTNEXTLINE(readability-braces-around-statements)
119         if constexpr (is_same_v<T, double>) {
120             auto *value_item = container_.GetOrCreateDoubleValueItem(value.value());
121             field_item->SetValue(value_item);
122             // NOLINTNEXTLINE(readability-misleading-indentation)
123         } else {
124             auto *value_item = container_.GetOrCreateFloatValueItem(value.value());
125             field_item->SetValue(value_item);
126         }
127     }
128 
SetStringFieldValue(FieldDataAccessor * field_acc,FieldItem * field_item)129     void SetStringFieldValue(FieldDataAccessor *field_acc, FieldItem *field_item)
130     {
131         auto value = field_acc->GetValue<uint32_t>();
132 
133         if (value) {
134             panda_file::File::EntityId string_id(value.value());
135             auto data = file_->GetStringData(string_id);
136             std::string string_data(reinterpret_cast<const char *>(data.data));
137             auto *string_item = container_.GetOrCreateStringItem(string_data);
138             auto *value_item = container_.GetOrCreateIdValueItem(string_item);
139             field_item->SetValue(value_item);
140         }
141     }
142 
143     // Creates foreign or non-foreign method item
CreateGenericMethodItem(BaseClassItem * class_item,File::EntityId method_id)144     inline BaseItem *CreateGenericMethodItem(BaseClassItem *class_item, File::EntityId method_id)
145     {
146         if (file_->IsExternal(method_id)) {
147             return CreateForeignMethodItem(class_item, method_id);
148         }
149         return CreateMethodItem(static_cast<ClassItem *>(class_item), method_id);
150     }
151 
152     // Creates foreign or non-foreign field item
CreateGenericFieldItem(BaseClassItem * class_item,File::EntityId field_id)153     inline BaseItem *CreateGenericFieldItem(BaseClassItem *class_item, File::EntityId field_id)
154     {
155         if (file_->IsExternal(field_id)) {
156             return CreateForeignFieldItem(class_item, field_id);
157         }
158         return CreateFieldItem(static_cast<ClassItem *>(class_item), field_id);
159     }
160 
161     // Creates foreign or non-foreign class item
CreateGenericClassItem(File::EntityId class_id)162     inline BaseClassItem *CreateGenericClassItem(File::EntityId class_id)
163     {
164         if (file_->IsExternal(class_id)) {
165             return CreateForeignClassItem(class_id);
166         }
167         return CreateClassItem(class_id);
168     }
169 
170     void UpdateCodeAndDebugInfoDependencies(const std::map<BaseItem *, File::EntityId> &reverse_done);
171 
172     std::unique_ptr<const File> file_;
173     ItemContainer container_;
174     std::map<File::EntityId, BaseItem *> items_done_;
175 };
176 
177 }  // namespace panda::panda_file
178 
179 #endif  // LIBPANDAFILE_FILE_READER_H_
180