• 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 
16 #ifndef DISASSEMBLER_DISASSEMBLER_H
17 #define DISASSEMBLER_DISASSEMBLER_H
18 
19 #include "macros.h"
20 #include "utils/type_helpers.h"
21 #include "utils/span.h"
22 
23 #include "class_data_accessor-inl.h"
24 #include "code_data_accessor-inl.h"
25 #include "debug_data_accessor-inl.h"
26 #include "debug_info_extractor.h"
27 #include "field_data_accessor-inl.h"
28 #include "method_data_accessor-inl.h"
29 #include "literal_data_accessor-inl.h"
30 #include "libpandafile/module_data_accessor-inl.h"
31 #include "param_annotations_data_accessor.h"
32 #include "annotation_data_accessor.h"
33 #include "proto_data_accessor-inl.h"
34 #include "file-inl.h"
35 #include "file.h"
36 #include "os/file.h"
37 
38 #include "assembly-program.h"
39 #include "assembly-ins.h"
40 
41 #include "bytecode_instruction-inl.h"
42 #include "libpandabase/macros.h"
43 
44 #include <map>
45 #include <memory>
46 #include <string>
47 
48 #include "accumulators.h"
49 
50 namespace panda::disasm {
51 class Disassembler {
52 public:
53     NO_COPY_SEMANTIC(Disassembler);
54     DEFAULT_MOVE_SEMANTIC(Disassembler);
55 
56     Disassembler() = default;
57     ~Disassembler() = default;
58 
59     void Disassemble(const std::string &filename_in, const bool quiet = false, const bool skip_strings = false);
60     void CollectInfo();
61     void Serialize(std::ostream &os, bool add_separators = false, bool print_information = false) const;
62 
63     static inline bool IsPandasmFriendly(const char c);
64     static inline bool IsSystemType(const std::string &type_name);
65 
66     void GetRecord(pandasm::Record *record, const panda_file::File::EntityId &record_id);
67     void AddMethodToTables(const panda_file::File::EntityId &method_id);
68     void GetMethod(pandasm::Function *method, const panda_file::File::EntityId &method_id);
69     void GetLiteralArray(pandasm::LiteralArray *lit_array, size_t index) const;
70     std::optional<std::vector<std::string>> GetAnnotationByMethodName(const std::string &method_name) const;
71     std::vector<std::string> GetStrings() const;
72     std::vector<std::string> GetModuleLiterals() const;
73     template <typename T>
74     void FillLiteralArrayData(pandasm::LiteralArray *lit_array, const panda_file::LiteralTag &tag,
75                               const panda_file::LiteralDataAccessor::LiteralValue &value) const;
76 
GetProgInfo()77     const ProgInfo &GetProgInfo() const
78     {
79         return prog_info_;
80     }
81 
82     std::vector<size_t> GetColumnNumber();
83     std::vector<size_t> GetLineNumber();
84 
85 private:
86     void GetLiteralArrays();
87     void FillLiteralData(pandasm::LiteralArray *lit_array, const panda_file::LiteralDataAccessor::LiteralValue &value,
88                          const panda_file::LiteralTag &tag) const;
89     void GetRecords();
90     void GetFields(pandasm::Record *record, const panda_file::File::EntityId &record_id);
91 
92     void GetMethods(const panda_file::File::EntityId &record_id);
93     void GetMethodAnnotations(pandasm::Function &method, const panda_file::File::EntityId &method_id);
94     void CreateAnnotationElement(panda_file::AnnotationDataAccessor &ada, pandasm::Function &method,
95                                  const std::string &ann_name, const std::string &ann_elem_name,
96                                  const std::string &ann_elem_index);
97     void AddAnnotationElement(pandasm::Function &method, const std::string &annotation_name,
98                               const std::string &key, const uint32_t &value);
99     void GetParams(pandasm::Function *method, const panda_file::File::EntityId &proto_id) const;
100     IdList GetInstructions(pandasm::Function *method, panda_file::File::EntityId method_id,
101                            panda_file::File::EntityId code_id) const;
102     LabelTable GetExceptions(pandasm::Function *method, panda_file::File::EntityId method_id,
103                              panda_file::File::EntityId code_id) const;
104     bool LocateTryBlock(const BytecodeInstruction &bc_ins, const BytecodeInstruction &bc_ins_last,
105                         const panda_file::CodeDataAccessor::TryBlock &try_block,
106                         pandasm::Function::CatchBlock *catch_block_pa, LabelTable *label_table, size_t try_idx) const;
107     bool LocateCatchBlock(const BytecodeInstruction &bc_ins, const BytecodeInstruction &bc_ins_last,
108                           const panda_file::CodeDataAccessor::CatchBlock &catch_block,
109                           pandasm::Function::CatchBlock *catch_block_pa, LabelTable *label_table, size_t try_idx,
110                           size_t catch_idx) const;
111 
112     void GetMetaData(pandasm::Record *record, const panda_file::File::EntityId &record_id) const;
113     void GetMetaData(pandasm::Function *method, const panda_file::File::EntityId &method_id) const;
114     void GetMetaData(pandasm::Field *field, const panda_file::File::EntityId &field_id);
115 
116     void GetLanguageSpecificMetadata();
117 
118     std::string AnnotationTagToString(const char tag) const;
119 
120     std::string ScalarValueToString(const panda_file::ScalarValue &value, const std::string &type);
121     std::string ArrayValueToString(const panda_file::ArrayValue &value, const std::string &type, const size_t idx);
122 
123     std::string GetFullMethodName(const panda_file::File::EntityId &method_id) const;
124     std::string GetMethodSignature(const panda_file::File::EntityId &method_id) const;
125     std::string GetFullRecordName(const panda_file::File::EntityId &class_id) const;
126 
127     void GetRecordInfo(const panda_file::File::EntityId &record_id, RecordInfo *record_info) const;
128     void GetMethodInfo(const panda_file::File::EntityId &method_id, MethodInfo *method_info) const;
129     void GetInsInfo(const panda_file::File::EntityId &code_id, MethodInfo *method_info) const;
130 
131     template <typename T>
132     void SerializeValues(const pandasm::LiteralArray &lit_array, T &os) const;
133     std::string SerializeLiteralArray(const pandasm::LiteralArray &lit_array) const;
134     void Serialize(const std::string &key, const pandasm::LiteralArray &lit_array, std::ostream &os) const;
135     void Serialize(const std::string &module_offset, const std::vector<std::string> &module_array,
136                    std::ostream &os) const;
137     template <typename T>
138     void SerializeLiterals(const pandasm::LiteralArray &lit_array, T &os) const;
139     std::string LiteralTagToString(const panda_file::LiteralTag &tag) const;
140     void Serialize(const pandasm::Record &record, std::ostream &os, bool print_information = false) const;
141     void SerializeFields(const pandasm::Record &record, std::ostream &os, bool print_information) const;
142     void Serialize(const pandasm::Function &method, std::ostream &os, bool print_information = false) const;
143     void SerializeMethodAnnotations(const pandasm::Function &method, std::ostream &os) const;
144     void SerializeStrings(const panda_file::File::EntityId &offset, const std::string &name_value,
145                           std::ostream &os) const;
146     void Serialize(const pandasm::Function::CatchBlock &catch_block, std::ostream &os) const;
147     void Serialize(const pandasm::ItemMetadata &meta, const AnnotationList &ann_list, std::ostream &os) const;
148     void SerializeLineNumberTable(const panda_file::LineNumberTable &line_number_table, std::ostream &os) const;
149     void SerializeColumnNumberTable(const panda_file::ColumnNumberTable &column_number_table,
150                                     std::ostream &os) const;
151     void SerializeLocalVariableTable(const panda_file::LocalVariableTable &local_variable_table,
152                                      const pandasm::Function &method, std::ostream &os) const;
153     bool IsModuleLiteralOffset(const panda_file::File::EntityId &id) const;
SerializeLanguage(std::ostream & os)154     inline void SerializeLanguage(std::ostream &os) const
155     {
156         os << ".language " << panda::panda_file::LanguageToString(file_language_) << "\n\n";
157     }
158 
159     pandasm::Type PFTypeToPandasmType(const panda_file::Type &type, panda_file::ProtoDataAccessor &pda,
160                                       size_t &ref_idx) const;
161 
162     pandasm::Type FieldTypeToPandasmType(const uint32_t &type) const;
163 
StringDataToString(panda_file::File::StringData sd)164     static inline std::string StringDataToString(panda_file::File::StringData sd)
165     {
166         return std::string(utf::Mutf8AsCString(sd.data));
167     }
168 
169     pandasm::Opcode BytecodeOpcodeToPandasmOpcode(BytecodeInstruction::Opcode o) const;
170     pandasm::Opcode BytecodeOpcodeToPandasmOpcode(uint8_t o) const;
171 
172     pandasm::Ins BytecodeInstructionToPandasmInstruction(BytecodeInstruction bc_ins,
173                                                          panda_file::File::EntityId method_id) const;
174     std::string IDToString(BytecodeInstruction bc_ins, panda_file::File::EntityId method_id, size_t idx) const;
175 
176     panda::panda_file::SourceLang GetRecordLanguage(panda_file::File::EntityId class_id) const;
177     void GetLiteralArrayByOffset(pandasm::LiteralArray *lit_array, panda_file::File::EntityId offset) const;
178 
179     std::vector<std::string> GetModuleLiteralArray(panda_file::File::EntityId &module_id) const;
180     std::string SerializeModuleLiteralArray(const std::vector<std::string> &module_array) const;
181     std::string ModuleTagToString(panda_file::ModuleTag &tag) const;
182 
183     std::unique_ptr<const panda_file::File> file_;
184     pandasm::Program prog_;
185 
GetStringByOffset(uint32_t offset)186     inline std::string GetStringByOffset(uint32_t offset) const
187     {
188         const auto sd = file_->GetStringData(panda_file::File::EntityId(offset));
189         return std::string(utf::Mutf8AsCString(sd.data));
190     }
191 
IsValidOffset(uint32_t offset)192     inline bool IsValidOffset(uint32_t offset) const
193     {
194         return panda_file::File::EntityId(offset).IsValid() && offset < file_->GetHeader()->file_size;
195     }
196 
GetFileNameByAbsolutePath(const std::string & absolute_path)197     inline std::string GetFileNameByAbsolutePath(const std::string &absolute_path) const
198     {
199         size_t pos = absolute_path.find_last_of(panda::os::file::File::GetPathDelim());
200         ASSERT(pos != std::string::npos);
201         std::string file_name = absolute_path.substr(pos + 1);
202         return file_name;
203     }
204 
205     panda::panda_file::SourceLang file_language_ = panda::panda_file::SourceLang::PANDA_ASSEMBLY;
206 
207     std::map<std::string, panda_file::File::EntityId> record_name_to_id_;
208     std::map<std::string, panda_file::File::EntityId> method_name_to_id_;
209     std::map<std::string, std::vector<std::string>> modulearray_table_;
210     mutable std::map<panda_file::File::EntityId, std::string> string_offset_to_name_;
211 
212     ProgAnnotations prog_ann_;
213 
214     ProgInfo prog_info_;
215 
216     std::unique_ptr<panda_file::DebugInfoExtractor> debug_info_extractor_;
217 
218     bool quiet_;
219     bool skip_strings_;
220     std::unordered_set<uint32_t> module_literals_;
221 #include "disasm_plugins.inc"
222 };
223 }  // namespace panda::disasm
224 
225 #endif  // DISASSEMBLER_DISASSEMBLER_H
226