• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "abc_file.h"
17 #include "libpandabase/mem/pool_manager.h"
18 #include "libpandafile/class_data_accessor-inl.h"
19 #include "libpandafile/code_data_accessor-inl.h"
20 #include "libpandafile/method_data_accessor-inl.h"
21 #include "libpandafile/literal_data_accessor-inl.h"
22 #include "libpandafile/field_data_accessor-inl.h"
23 #include "libpandafile/module_data_accessor-inl.h"
24 #include "compiler/optimizer/ir_builder/ir_builder.h"
25 #include "bytecode_optimizer/common.h"
26 #include "bytecode_optimizer/runtime_adapter.h"
27 #include "callee_info.h"
28 #include "class.h"
29 #include "function.h"
30 #include "module_record.h"
31 
32 namespace panda::defect_scan_aux {
33 using EntityId = panda_file::File::EntityId;
34 using StringData = panda_file::StringData;
35 using LiteralTag = panda_file::LiteralTag;
36 using ModuleDataAccessor = panda_file::ModuleDataAccessor;
37 using ModuleTag = panda_file::ModuleTag;
38 
AbcFile(std::string_view filename,std::unique_ptr<const panda_file::File> && panda_file)39 AbcFile::AbcFile(std::string_view filename, std::unique_ptr<const panda_file::File> &&panda_file)
40     : filename_(filename), panda_file_(std::forward<std::unique_ptr<const panda_file::File>>(panda_file))
41 {
42     PoolManager::Initialize(PoolType::MALLOC);
43     allocator_ = std::make_unique<ArenaAllocator>(SpaceType::SPACE_TYPE_COMPILER);
44     local_allocator_ = std::make_unique<ArenaAllocator>(SpaceType::SPACE_TYPE_COMPILER, nullptr, true);
45 }
46 
~AbcFile()47 AbcFile::~AbcFile()
48 {
49     PoolManager::Finalize();
50 }
51 
Open(std::string_view abc_filename)52 std::unique_ptr<const AbcFile> AbcFile::Open(std::string_view abc_filename)
53 {
54     auto panda_file = panda_file::OpenPandaFile(abc_filename);
55     if (panda_file == nullptr) {
56         LOG(ERROR, DEFECT_SCAN_AUX) << "Can not open binary file '" << abc_filename << "'";
57         return nullptr;
58     }
59 
60     std::unique_ptr<AbcFile> abc_file(new (std::nothrow) AbcFile(abc_filename, std::move(panda_file)));
61     if (abc_file == nullptr) {
62         LOG(ERROR, DEFECT_SCAN_AUX) << "Can not create AbcFile instance for '" << abc_filename << "'";
63         return nullptr;
64     }
65 
66     abc_file->ExtractDebugInfo();
67     abc_file->ExtractModuleInfo();
68     abc_file->InitializeAllDefinedFunction();
69     abc_file->ExtractDefinedClassAndFunctionInfo();
70     abc_file->ExtractClassAndFunctionExportList();
71     return abc_file;
72 }
73 
IsModule(std::string_view record_name) const74 bool AbcFile::IsModule(std::string_view record_name) const
75 {
76     if (IsMergeAbc() && record_name == "") {
77         LOG(FATAL, DEFECT_SCAN_AUX) <<
78             "For merge abc, need to specify record name to check if it has module info";
79     }
80     return GetModuleRecordByName(std::string(record_name)) != nullptr;
81 }
82 
IsMergeAbc() const83 bool AbcFile::IsMergeAbc() const
84 {
85     return is_merge_abc_;
86 }
87 
GetAbcFileName() const88 const std::string &AbcFile::GetAbcFileName() const
89 {
90     return filename_;
91 }
92 
GetDefinedFunctionCount() const93 size_t AbcFile::GetDefinedFunctionCount() const
94 {
95     if (IsMergeAbc()) {
96         return merged_def_func_list_.size();
97     }
98     return def_func_list_.size();
99 }
100 
GetDefinedClassCount() const101 size_t AbcFile::GetDefinedClassCount() const
102 {
103     if (IsMergeAbc()) {
104         return merged_def_class_list_.size();
105     }
106     return def_class_list_.size();
107 }
108 
GetClassList() const109 const std::vector<std::shared_ptr<Class>> &AbcFile::GetClassList() const
110 {
111     if (IsMergeAbc()) {
112         return merged_def_class_list_;
113     }
114     return def_class_list_;
115 }
116 
GetDefinedFunctionByIndex(size_t index) const117 const Function *AbcFile::GetDefinedFunctionByIndex(size_t index) const
118 {
119     if (IsMergeAbc()) {
120         ASSERT(index < merged_def_func_list_.size());
121         return merged_def_func_list_[index].get();
122     }
123     ASSERT(index < def_func_list_.size());
124     return def_func_list_[index].get();
125 }
126 
GetFunctionByName(std::string_view func_name) const127 const Function *AbcFile::GetFunctionByName(std::string_view func_name) const
128 {
129     return GetFunctionByNameImpl(func_name);
130 }
131 
GetExportFunctionByExportName(std::string_view export_func_name,std::string_view record_name) const132 const Function *AbcFile::GetExportFunctionByExportName(std::string_view export_func_name,
133                                                        std::string_view record_name) const
134 {
135     if (!IsModule(record_name)) {
136         return nullptr;
137     }
138 
139     if (IsMergeAbc() && record_name == "") {
140         LOG(FATAL, DEFECT_SCAN_AUX) <<
141             "Failed to GetExportFunctionByExportName from merge abc, need to specify record name";
142     }
143     std::string inter_func_name = GetLocalNameByExportName(export_func_name, record_name);
144     auto export_func_list = export_func_list_;
145     if (IsMergeAbc()) {
146         const std::string record_name_str = std::string(record_name);
147         auto it = merge_export_func_map_.find(record_name_str);
148         if (it == merge_export_func_map_.end()) {
149             return nullptr;
150         }
151         export_func_list = it->second;
152     }
153     for (auto &export_func : export_func_list) {
154         const std::string &ex_func_name = export_func->GetFunctionName();
155         std::string_view no_hashtag_name = GetNameWithoutHashtag(ex_func_name, record_name);
156         if (no_hashtag_name == inter_func_name) {
157             return export_func;
158         }
159     }
160     return nullptr;
161 }
162 
GetDefinedClassByIndex(size_t index) const163 const Class *AbcFile::GetDefinedClassByIndex(size_t index) const
164 {
165     if (IsMergeAbc()) {
166         ASSERT(index < merged_def_class_list_.size());
167         return merged_def_class_list_[index].get();
168     }
169     ASSERT(index < def_class_list_.size());
170     return def_class_list_[index].get();
171 }
172 
GetClassByName(std::string_view class_name) const173 const Class *AbcFile::GetClassByName(std::string_view class_name) const
174 {
175     return GetClassByNameImpl(class_name);
176 }
177 
GetExportClassByExportName(std::string_view export_class_name,std::string_view record_name) const178 const Class *AbcFile::GetExportClassByExportName(std::string_view export_class_name,
179                                                  std::string_view record_name) const
180 {
181     if (!IsModule(record_name)) {
182         return nullptr;
183     }
184 
185     if (IsMergeAbc() && record_name == "") {
186         LOG(FATAL, DEFECT_SCAN_AUX) <<
187             "Failed to GetExportClassByExportName from merge abc, need to specify record name";
188     }
189     if (!IsMergeAbc()) {
190         record_name = std::string(MODULE_CLASS);
191     }
192     std::string inter_class_name = GetLocalNameByExportName(export_class_name, record_name);
193     auto export_class_list = export_class_list_;
194     if (IsMergeAbc()) {
195         const std::string record_name_str = std::string(record_name);
196         auto it = merge_export_class_map_.find(record_name_str);
197         if (it == merge_export_class_map_.end()) {
198             return nullptr;
199         }
200         export_class_list = it->second;
201     }
202     for (auto export_class : export_class_list) {
203         const std::string &ex_class_name = export_class->GetClassName();
204         std::string_view no_hashtag_name = GetNameWithoutHashtag(ex_class_name, record_name);
205         if (no_hashtag_name == inter_class_name) {
206             return export_class;
207         }
208     }
209     return nullptr;
210 }
211 
GetLineNumberByInst(const Function * func,const Inst & inst) const212 ssize_t AbcFile::GetLineNumberByInst(const Function *func, const Inst &inst) const
213 {
214     auto &line_number_table = debug_info_->GetLineNumberTable(func->GetMethodId());
215     if (!line_number_table.empty()) {
216         uint32_t inst_pc = inst.GetPc();
217         // line_number_table is in ascending order, find the element that satisfies e1.pc <= inst_pc < e2.pc
218         auto comp = [](size_t value, const panda_file::LineTableEntry &entry) { return value >= entry.offset; };
219         auto iter = std::upper_bound(line_number_table.rbegin(), line_number_table.rend(), inst_pc, comp);
220         if (iter != line_number_table.rend()) {
221             // line number written in a .abc file starts from 0
222             return iter->line + 1;
223         }
224     }
225     return -1;
226 }
227 
GetFileRecordList() const228 const std::set<std::string> AbcFile::GetFileRecordList() const
229 {
230     return record_name_set_;
231 }
232 
GetFileRecordCount() const233 size_t AbcFile::GetFileRecordCount() const
234 {
235     return record_name_set_.size();
236 }
237 
GetLocalNameByExportName(std::string_view export_name,std::string_view record_name) const238 std::string AbcFile::GetLocalNameByExportName(std::string_view export_name, std::string_view record_name) const
239 {
240     if (!IsModule(record_name)) {
241         return EMPTY_STR;
242     }
243     if (IsMergeAbc() && record_name == "") {
244         LOG(FATAL, DEFECT_SCAN_AUX) << "Failed to GetLocalNameByExportName from merge abc, need to specify record name";
245     }
246     if (!IsMergeAbc()) {
247         record_name = std::string(MODULE_CLASS);
248     }
249     auto module_record = GetModuleRecordByName(std::string(record_name));
250     return module_record->GetLocalNameByExportName(export_name);
251 }
252 
GetImportNameByExportName(std::string_view export_name,std::string_view record_name) const253 std::string AbcFile::GetImportNameByExportName(std::string_view export_name, std::string_view record_name) const
254 {
255     if (!IsModule(record_name)) {
256         return EMPTY_STR;
257     }
258     if (IsMergeAbc() && record_name == "") {
259         LOG(FATAL, DEFECT_SCAN_AUX)
260             << "Failed to GetImportNameByExportName from merge abc, need to specify record name";
261     }
262     if (!IsMergeAbc()) {
263         record_name = std::string(MODULE_CLASS);
264     }
265     auto module_record = GetModuleRecordByName(std::string(record_name));
266     return  module_record->GetImportNameByExportName(export_name);
267 }
268 
GetModuleNameByExportName(std::string_view export_name,std::string_view record_name) const269 std::string AbcFile::GetModuleNameByExportName(std::string_view export_name, std::string_view record_name) const
270 {
271     if (!IsModule(record_name)) {
272         return EMPTY_STR;
273     }
274     if (IsMergeAbc() && record_name == "") {
275         LOG(FATAL, DEFECT_SCAN_AUX)
276             << "Failed to GetModuleNameByExportName from merge abc, need to specify record name";
277     }
278     if (!IsMergeAbc()) {
279         record_name = std::string(MODULE_CLASS);
280     }
281     auto module_record = GetModuleRecordByName(std::string(record_name));
282     return module_record->GetModuleNameByExportName(export_name);
283 }
284 
GetModuleNameByLocalName(std::string_view local_name,std::string_view record_name) const285 std::string AbcFile::GetModuleNameByLocalName(std::string_view local_name, std::string_view record_name) const
286 {
287     if (!IsModule(record_name)) {
288         return EMPTY_STR;
289     }
290     if (IsMergeAbc() && record_name == "") {
291         LOG(FATAL, DEFECT_SCAN_AUX) << "Failed to GetModuleNameByLocalName from merge abc, need to specify record name";
292     }
293     if (!IsMergeAbc()) {
294         record_name = std::string(MODULE_CLASS);
295     }
296     auto module_record = GetModuleRecordByName(std::string(record_name));
297     return module_record->GetModuleNameByLocalName(local_name);
298 }
299 
GetImportNameByLocalName(std::string_view local_name,std::string_view record_name) const300 std::string AbcFile::GetImportNameByLocalName(std::string_view local_name, std::string_view record_name) const
301 {
302     if (!IsModule(record_name)) {
303         return EMPTY_STR;
304     }
305     if (IsMergeAbc() && record_name == "") {
306         LOG(FATAL, DEFECT_SCAN_AUX) << "Failed to GetImportNameByLocalName from merge abc, need to specify record name";
307     }
308     if (!IsMergeAbc()) {
309         record_name = std::string(MODULE_CLASS);
310     }
311     auto module_record = GetModuleRecordByName(std::string(record_name));
312     return module_record->GetImportNameByLocalName(local_name);
313 }
314 
GetNameWithoutHashtag(std::string_view full_name,std::string_view record_name) const315 std::string_view AbcFile::GetNameWithoutHashtag(std::string_view full_name, std::string_view record_name) const
316 {
317     size_t pos = full_name.find(record_name);
318     std::string_view name = pos == std::string::npos ? full_name : full_name.substr(pos + record_name.length());
319     if (name[0] == '#') {
320         size_t sec_hashtag_idx = name.find_first_of('#', 1);
321         if (sec_hashtag_idx != std::string::npos && (sec_hashtag_idx + 1) <= name.size()) {
322             return name.substr(sec_hashtag_idx + 1);
323         }
324     }
325     return name;
326 }
327 
GetStringByInst(const Inst & inst) const328 std::string AbcFile::GetStringByInst(const Inst &inst) const
329 {
330     auto type = inst.GetType();
331     switch (type) {
332         case InstType::DEFINEFUNC_IMM8_ID16_IMM8:
333         case InstType::DEFINEFUNC_IMM16_ID16_IMM8:
334         case InstType::DEFINEMETHOD_IMM8_ID16_IMM8:
335         case InstType::DEFINEMETHOD_IMM16_ID16_IMM8:
336         case InstType::DEFINECLASSWITHBUFFER_IMM8_ID16_ID16_IMM16_V8:
337         case InstType::DEFINECLASSWITHBUFFER_IMM16_ID16_ID16_IMM16_V8: {
338             uint32_t method_id = inst.GetImms()[1];
339             return GetStringByMethodId(EntityId(method_id));
340         }
341         case InstType::TRYLDGLOBALBYNAME_IMM8_ID16:
342         case InstType::TRYSTGLOBALBYNAME_IMM8_ID16:
343         case InstType::TRYLDGLOBALBYNAME_IMM16_ID16:
344         case InstType::TRYSTGLOBALBYNAME_IMM16_ID16:
345         case InstType::STCONSTTOGLOBALRECORD_IMM16_ID16:
346         case InstType::STTOGLOBALRECORD_IMM16_ID16:
347         case InstType::LDGLOBALVAR_IMM16_ID16:
348         case InstType::STGLOBALVAR_IMM16_ID16:
349         case InstType::LDOBJBYNAME_IMM8_ID16:
350         case InstType::LDOBJBYNAME_IMM16_ID16:
351         case InstType::STOBJBYNAME_IMM8_ID16_V8:
352         case InstType::STOBJBYNAME_IMM16_ID16_V8:
353         case InstType::LDSUPERBYNAME_IMM8_ID16:
354         case InstType::LDSUPERBYNAME_IMM16_ID16:
355         case InstType::STSUPERBYNAME_IMM8_ID16_V8:
356         case InstType::STSUPERBYNAME_IMM16_ID16_V8:
357         case InstType::LDTHISBYNAME_IMM8_ID16:
358         case InstType::LDTHISBYNAME_IMM16_ID16:
359         case InstType::STTHISBYNAME_IMM8_ID16:
360         case InstType::STTHISBYNAME_IMM16_ID16:
361         case InstType::STOWNBYNAME_IMM8_ID16_V8:
362         case InstType::STOWNBYNAME_IMM16_ID16_V8:
363         case InstType::STOWNBYNAMEWITHNAMESET_IMM8_ID16_V8:
364         case InstType::STOWNBYNAMEWITHNAMESET_IMM16_ID16_V8: {
365             uint32_t string_id = inst.GetImms()[1];
366             return GetStringByStringId(EntityId(string_id));
367         }
368         default:
369             return EMPTY_STR;
370     }
371 }
372 
373 // TODO(wangyantian): may match multiple stlex inst when considering control flow
GetStLexInstByLdLexInst(FuncInstPair func_inst_pair) const374 std::optional<FuncInstPair> AbcFile::GetStLexInstByLdLexInst(FuncInstPair func_inst_pair) const
375 {
376     Function *func = func_inst_pair.first;
377     const Inst &ld_lex_inst = func_inst_pair.second;
378     if (func == nullptr || !ld_lex_inst.IsInstLdLexVar()) {
379         return std::nullopt;
380     }
381 
382     auto ld_imms = ld_lex_inst.GetImms();
383     uint32_t ld_level = ld_imms[0];
384     uint32_t ld_slot_id = ld_imms[1];
385     Function *cur_func = func;
386     uint32_t i = 0;
387     while (true) {
388         bool has_new_lexenv = false;
389         const auto &graph = cur_func->GetGraph();
390         // TODO: Multiple newlexenv instructions and poplexenv instruction are not correctly supported in
391         //  the following code. Future fixes are required.
392         graph.VisitAllInstructions([&has_new_lexenv](const Inst &inst) {
393             if (inst.GetType() == InstType::NEWLEXENV_IMM8 || inst.GetType() == InstType::WIDE_NEWLEXENV_PREF_IMM16) {
394                 has_new_lexenv = true;
395             }
396         });
397         if (has_new_lexenv) {
398             i++;
399         }
400         if (i == ld_level + 1) {
401             break;
402         }
403         cur_func = cur_func->GetParentFunction();
404         if (cur_func == nullptr) {
405             return std::nullopt;
406         }
407     }
408     auto &graph = cur_func->GetGraph();
409     Inst st_lex_inst = ld_lex_inst;
410     bool is_same_func = (cur_func == func_inst_pair.first);
411     graph.VisitAllInstructions([is_same_func, ld_lex_inst, ld_slot_id, &st_lex_inst](const Inst &inst) {
412         if (inst.IsInstStLexVar()) {
413             auto st_imms = inst.GetImms();
414             uint32_t st_level = st_imms[0];
415             uint32_t st_slot_id = st_imms[1];
416             if (st_level == 0 && st_slot_id == ld_slot_id) {
417                 // Best effort to avoid the case where ld_lex_inst is some input of st_lex_inst
418                 // Current heuristics for choosing a valid st_lex_inst (or relationship between conditions):
419                 // 1. If no valid st_lex_inst is found (which is necessary to make the search process more sound).
420                 // 2. If they are not in the same function.
421                 // 3. If they are in the same function, but current st_lex_inst appears before ld_lex_inst (by PC).
422                 // More advanced heuristics (e.g. preforming dfs on all inputs of st_lex_inst) should be considered.
423                 if (st_lex_inst == ld_lex_inst || !is_same_func || inst.GetPc() < ld_lex_inst.GetPc()) {
424                     st_lex_inst = inst;
425                 }
426             }
427         }
428     });
429     if (st_lex_inst != ld_lex_inst) {
430         return FuncInstPair(cur_func, st_lex_inst);
431     }
432 
433     return std::nullopt;
434 }
435 
GetStGlobalInstByLdGlobalInst(FuncInstPair func_inst_pair) const436 std::optional<FuncInstPair> AbcFile::GetStGlobalInstByLdGlobalInst(FuncInstPair func_inst_pair) const
437 {
438     const Function *func = func_inst_pair.first;
439     const Inst &ld_global_inst = func_inst_pair.second;
440     if (func == nullptr || !ld_global_inst.IsInstLdGlobal()) {
441         return std::nullopt;
442     }
443 
444     uint32_t ld_str_id = ld_global_inst.GetImms()[0];
445     std::string record_name = func->GetRecordName();
446     Function *func_main;
447     // TODO(wangyantian): only consider that func_main_0 has StGlobal inst for now, what about other cases?
448     if (IsMergeAbc()) {
449         ASSERT(merge_def_func_map_.find(record_name) != merge_def_func_map_.end());
450         func_main = merge_def_func_map_.find(record_name)->second[0].get();
451     } else {
452         func_main = def_func_list_[0].get();
453     }
454     auto &graph = func_main->GetGraph();
455     Inst st_global_inst = ld_global_inst;
456     graph.VisitAllInstructions([ld_str_id, &st_global_inst](const Inst &inst) {
457         if (inst.IsInstStGlobal()) {
458             uint32_t st_str_id = inst.GetImms()[0];
459             if (st_str_id == ld_str_id) {
460                 st_global_inst = inst;
461             }
462         }
463     });
464     if (st_global_inst != ld_global_inst) {
465         return FuncInstPair(func_main, st_global_inst);
466     }
467 
468     return std::nullopt;
469 }
470 
ExtractDebugInfo()471 void AbcFile::ExtractDebugInfo()
472 {
473     debug_info_ = std::make_unique<const panda_file::DebugInfoExtractor>(panda_file_.get());
474     if (debug_info_ == nullptr) {
475         LOG(FATAL, DEFECT_SCAN_AUX) << "Failed to extract debug info";
476     }
477 }
478 
ExtractModuleInfo()479 void AbcFile::ExtractModuleInfo()
480 {
481     int module_offset = -1;
482     is_merge_abc_ = true;
483     for (uint32_t id : panda_file_->GetClasses()) {
484         EntityId class_id(id);
485         if (panda_file_->IsExternal(class_id)) {
486             continue;
487         }
488         panda_file::ClassDataAccessor cda(*panda_file_, class_id);
489         const char *desc = utf::Mutf8AsCString(cda.GetDescriptor());
490         if (std::strcmp(MODULE_CLASS, desc) == 0) {
491             is_merge_abc_ = false;
492             cda.EnumerateFields([&](panda_file::FieldDataAccessor &field_accessor) -> void {
493                 EntityId field_name_id = field_accessor.GetNameId();
494                 StringData sd = panda_file_->GetStringData(field_name_id);
495                 if (std::strcmp(utf::Mutf8AsCString(sd.data), filename_.data())) {
496                     module_offset = field_accessor.GetValue<int32_t>().value();
497                     return;
498                 }
499             });
500             break;
501         }
502     }
503     if (is_merge_abc_) {
504         ExtractMergeAbcModuleInfo();
505         return;
506     } else if (module_offset == -1) {
507         return;
508     }
509 
510     std::unique_ptr<ModuleRecord> module_record = std::make_unique<ModuleRecord>(filename_);
511     if (module_record == nullptr) {
512         LOG(FATAL, DEFECT_SCAN_AUX) << "Can not create ModuleRecord instance for '" << filename_ << "'";
513     }
514     ExtractModuleRecord(EntityId(module_offset), module_record);
515     AddModuleRecord(MODULE_CLASS, std::move(module_record));
516 }
517 
ExtractMergeAbcModuleInfo()518 void AbcFile::ExtractMergeAbcModuleInfo()
519 {
520     for (uint32_t id : panda_file_->GetClasses()) {
521         EntityId class_id(id);
522         if (panda_file_->IsExternal(class_id)) {
523             continue;
524         }
525         panda_file::ClassDataAccessor cda(*panda_file_, class_id);
526         const char *desc = utf::Mutf8AsCString(cda.GetDescriptor());
527         cda.EnumerateFields([&](panda_file::FieldDataAccessor &field_accessor) -> void {
528             EntityId field_name_id = field_accessor.GetNameId();
529             StringData sd = panda_file_->GetStringData(field_name_id);
530             if (std::strcmp(utf::Mutf8AsCString(sd.data), MODULE_IDX_FIELD_NAME) != 0) {
531                 return;
532             }
533             auto module_offset = field_accessor.GetValue<int32_t>().value();
534             std::unique_ptr<ModuleRecord> module_record = std::make_unique<ModuleRecord>(desc);
535             ASSERT(module_record != nullptr);
536             ExtractModuleRecord(EntityId(module_offset), module_record);
537             AddModuleRecord(std::string(desc), std::move(module_record));
538         });
539     }
540 }
541 
ExtractModuleRecord(EntityId module_id,std::unique_ptr<ModuleRecord> & module_record)542 void AbcFile::ExtractModuleRecord(EntityId module_id, std::unique_ptr<ModuleRecord> &module_record)
543 {
544     ModuleDataAccessor mda(*panda_file_, module_id);
545     const std::vector<uint32_t> &request_modules_idx = mda.getRequestModules();
546     std::vector<std::string> request_modules;
547     for (size_t idx = 0; idx < request_modules_idx.size(); ++idx) {
548         request_modules.push_back(GetStringByStringId(EntityId(request_modules_idx[idx])));
549     }
550     module_record->SetRequestModules(request_modules);
551 
552     size_t regular_import_num = 0;
553     size_t local_export_num = 0;
554     mda.EnumerateModuleRecord([&](const ModuleTag &tag, uint32_t export_name_offset, uint32_t module_request_idx,
555                                   uint32_t import_name_offset, uint32_t local_name_offset) {
556         size_t request_num = request_modules.size();
557         ASSERT(request_num == 0 || module_request_idx < request_num);
558         std::string module_request = EMPTY_STR;
559         if (request_num != 0) {
560             module_request = request_modules[module_request_idx];
561         }
562         switch (tag) {
563             case ModuleTag::REGULAR_IMPORT: {
564                 ++regular_import_num;
565                 std::string local_name = GetStringByStringId(EntityId(local_name_offset));
566                 std::string import_name = GetStringByStringId(EntityId(import_name_offset));
567                 module_record->AddImportEntry({module_request, import_name, local_name});
568                 LOG(DEBUG, DEFECT_SCAN_AUX) << "ModuleRecord adds a regular import: [" << module_request << ", "
569                                             << import_name << ", " << local_name << "]";
570                 break;
571             }
572             case ModuleTag::NAMESPACE_IMPORT: {
573                 std::string local_name = GetStringByStringId(EntityId(local_name_offset));
574                 module_record->AddImportEntry({module_request, "*", local_name});
575                 LOG(DEBUG, DEFECT_SCAN_AUX)
576                     << "ModuleRecord adds a namespace import: [" << module_request << ", *, " << local_name << "]";
577                 break;
578             }
579             case ModuleTag::LOCAL_EXPORT: {
580                 ++local_export_num;
581                 std::string local_name = GetStringByStringId(EntityId(local_name_offset));
582                 std::string export_name = GetStringByStringId(EntityId(export_name_offset));
583                 module_record->AddExportEntry({export_name, EMPTY_STR, EMPTY_STR, local_name});
584                 LOG(DEBUG, DEFECT_SCAN_AUX)
585                     << "ModuleRecord adds a local export: [" << export_name << ", null, null, " << local_name << "]";
586                 break;
587             }
588             case ModuleTag::INDIRECT_EXPORT: {
589                 std::string export_name = GetStringByStringId(EntityId(export_name_offset));
590                 std::string import_name = GetStringByStringId(EntityId(import_name_offset));
591                 module_record->AddExportEntry({export_name, module_request, import_name, EMPTY_STR});
592                 LOG(DEBUG, DEFECT_SCAN_AUX) << "ModuleRecord adds an indirect export: [" << export_name << ", "
593                                             << module_request << ", " << import_name << ", null]";
594                 break;
595             }
596             case ModuleTag::STAR_EXPORT: {
597                 module_record->AddExportEntry({EMPTY_STR, module_request, "*", EMPTY_STR});
598                 LOG(DEBUG, DEFECT_SCAN_AUX) << "ModuleRecord adds a start export: ["
599                                             << "null, " << module_request << "*, null]";
600                 break;
601             }
602             default: {
603                 UNREACHABLE();
604                 break;
605             }
606         }
607     });
608     module_record->SetRegularImportNum(regular_import_num);
609     module_record->SetLocalExportNum(local_export_num);
610 }
611 
AddModuleRecord(std::string record_name,std::unique_ptr<ModuleRecord> && module_record)612 void AbcFile::AddModuleRecord(std::string record_name, std::unique_ptr<ModuleRecord> &&module_record)
613 {
614     ASSERT(module_record_map_.find(record_name) == module_record_map_.end());
615     module_record_map_[record_name] = module_record.get();
616     module_record_list_.emplace_back(std::move(module_record));
617 }
618 
InitializeAllDefinedFunction()619 void AbcFile::InitializeAllDefinedFunction()
620 {
621     for (uint32_t id : panda_file_->GetClasses()) {
622         EntityId class_id {id};
623         if (panda_file_->IsExternal(class_id)) {
624             continue;
625         }
626 
627         panda_file::ClassDataAccessor cda {*panda_file_, class_id};
628         cda.EnumerateMethods([&](panda_file::MethodDataAccessor &mda) {
629             if (!mda.IsExternal()) {
630                 std::string func_name = GetStringByStringId(mda.GetNameId());
631                 std::string record_name = "";
632                 std::string name = func_name;
633                 if (IsMergeAbc()) {
634                     record_name = std::string(utf::Mutf8AsCString(cda.GetName().data));
635                     record_name_set_.insert(record_name);
636                     func_name = record_name + func_name;
637                 }
638                 EntityId m_id = mda.GetMethodId();
639                 panda_file::CodeDataAccessor cda {*panda_file_, mda.GetCodeId().value()};
640                 uint32_t arg_count = cda.GetNumArgs();
641                 compiler::Graph *graph = GenerateFunctionGraph(mda, func_name);
642                 if (graph == nullptr) {
643                     return;
644                 }
645                 std::unique_ptr<Function> func =
646                     std::make_unique<Function>(record_name, func_name, m_id, arg_count, Graph(graph), this);
647                 if (func == nullptr) {
648                     LOG(FATAL, DEFECT_SCAN_AUX) << "Can not allocate memory when processing '" << filename_ << "'";
649                 }
650                 LOG(DEBUG, DEFECT_SCAN_AUX) << "Create a new function: " << func_name;
651                 if (IsMergeAbc()) {
652                     AddMergedDefinedFunction(std::move(func));
653                 } else {
654                     AddDefinedFunction(std::move(func));
655                 }
656             }
657         });
658     }
659 }
660 
ExtractDefinedClassAndFunctionInfo()661 void AbcFile::ExtractDefinedClassAndFunctionInfo()
662 {
663     if (IsMergeAbc()) {
664         ExtractMergedDefinedClassAndFunctionInfo();
665     } else {
666         ExtractSingleDefinedClassAndFunctionInfo();
667     }
668 }
669 
ExtractMergedDefinedClassAndFunctionInfo()670 void AbcFile::ExtractMergedDefinedClassAndFunctionInfo()
671 {
672     for (auto &merge_def_func_pair : merge_def_func_map_) {
673         for (auto &func : merge_def_func_pair.second) {
674             ExtractMergedClassAndFunctionInfo(func.get());
675         }
676     }
677     std::unordered_set<const Function *> processed_func;
678     for (auto &merge_def_class_pair : merge_def_class_map_) {
679         for (auto &def_class : merge_def_class_pair.second) {
680             Function *def_func = def_class->GetDefiningFunction();
681             if (def_func != nullptr && processed_func.count(def_func) == 0) {
682                 ExtractClassInheritInfo(def_func);
683                 processed_func.insert(def_func);
684             }
685         }
686     }
687     for (auto &merge_def_func_pair : merge_def_func_map_) {
688         for (auto &func : merge_def_func_pair.second) {
689             ExtractFunctionCalleeInfo(func.get());
690         }
691     }
692 }
693 
ExtractSingleDefinedClassAndFunctionInfo()694 void AbcFile::ExtractSingleDefinedClassAndFunctionInfo()
695 {
696     for (auto &func : def_func_list_) {
697         ExtractClassAndFunctionInfo(func.get());
698     }
699 
700     std::unordered_set<const Function *> processed_func;
701     for (auto &def_class : def_class_list_) {
702         Function *def_func = def_class->GetDefiningFunction();
703         if (def_func != nullptr && processed_func.count(def_func) == 0) {
704             ExtractClassInheritInfo(def_func);
705             processed_func.insert(def_func);
706         }
707     }
708 
709     for (auto &func : def_func_list_) {
710         ExtractFunctionCalleeInfo(func.get());
711     }
712 }
713 
ExtractClassAndFunctionInfo(Function * func)714 void AbcFile::ExtractClassAndFunctionInfo(Function *func)
715 {
716     auto &graph = func->GetGraph();
717     graph.VisitAllInstructions([&](const Inst &inst) {
718         auto type = inst.GetType();
719         switch (type) {
720             case InstType::DEFINECLASSWITHBUFFER_IMM8_ID16_ID16_IMM16_V8:
721             case InstType::DEFINECLASSWITHBUFFER_IMM16_ID16_ID16_IMM16_V8: {
722                 auto def_class = ResolveDefineClassWithBufferInst(func, inst);
723                 AddDefinedClass(std::move(def_class));
724                 break;
725             }
726             case InstType::DEFINEFUNC_IMM8_ID16_IMM8:
727             case InstType::DEFINEFUNC_IMM16_ID16_IMM8: {
728                 Function *def_func = ResolveDefineFuncInstCommon(func, inst);
729                 BuildFunctionDefineChain(func, def_func);
730                 break;
731             }
732             case InstType::DEFINEMETHOD_IMM8_ID16_IMM8:
733             case InstType::DEFINEMETHOD_IMM16_ID16_IMM8: {
734                 auto member_func = ResolveDefineFuncInstCommon(func, inst);
735                 BuildFunctionDefineChain(func, member_func);
736                 // resolve the class where it's defined
737                 ResolveDefineMethodInst(member_func, inst);
738                 break;
739             }
740             default:
741                 break;
742         }
743     });
744 }
745 
ExtractMergedClassAndFunctionInfo(Function * func)746 void AbcFile::ExtractMergedClassAndFunctionInfo(Function *func)
747 {
748     auto &graph = func->GetGraph();
749     std::string record_name = func->GetRecordName();
750     graph.VisitAllInstructions([&](const Inst &inst) {
751         auto type = inst.GetType();
752         switch (type) {
753             case InstType::DEFINECLASSWITHBUFFER_IMM8_ID16_ID16_IMM16_V8:
754             case InstType::DEFINECLASSWITHBUFFER_IMM16_ID16_ID16_IMM16_V8: {
755                 auto def_class = ResolveDefineClassWithBufferInst(func, inst);
756                 AddMergedDefinedClass(std::move(def_class), record_name);
757                 break;
758             }
759             case InstType::DEFINEFUNC_IMM8_ID16_IMM8:
760             case InstType::DEFINEFUNC_IMM16_ID16_IMM8: {
761                 Function *def_func = ResolveDefineFuncInstCommon(func, inst);
762                 BuildFunctionDefineChain(func, def_func);
763                 break;
764             }
765             case InstType::DEFINEMETHOD_IMM8_ID16_IMM8:
766             case InstType::DEFINEMETHOD_IMM16_ID16_IMM8: {
767                 auto member_func = ResolveDefineFuncInstCommon(func, inst);
768                 BuildFunctionDefineChain(func, member_func);
769                 // resolve the class where it's defined
770                 ResolveDefineMethodInst(member_func, inst);
771                 break;
772             }
773             default:
774                 break;
775         }
776     });
777 }
778 
ExtractClassInheritInfo(Function * func) const779 void AbcFile::ExtractClassInheritInfo(Function *func) const
780 {
781     auto &graph = func->GetGraph();
782     std::string record_name = func->GetRecordName();
783     graph.VisitAllInstructions([&](const Inst &inst) {
784         if (inst.GetType() != InstType::DEFINECLASSWITHBUFFER_IMM8_ID16_ID16_IMM16_V8 &&
785             inst.GetType() != InstType::DEFINECLASSWITHBUFFER_IMM16_ID16_ID16_IMM16_V8) {
786             return;
787         }
788 
789         Class *cur_class = GetClassByNameImpl(record_name + GetStringByInst(inst));
790         ASSERT(cur_class != nullptr);
791         Inst def_class_input1 = inst.GetInputInsts()[0];
792         auto [ret_ptr, ret_sym, ret_type] = ResolveInstCommon(func, def_class_input1);
793         if (ret_ptr != nullptr && ret_type == ResolveType::CLASS_OBJECT) {
794             auto par_class = reinterpret_cast<const Class *>(ret_ptr);
795             cur_class->SetParentClass(par_class);
796             return;
797         }
798         size_t first_delim_idx = ret_sym.find_first_of(DELIM);
799         size_t last_delim_idx = ret_sym.find_last_of(DELIM);
800         std::string par_class_name = ret_sym;
801         std::string var_name = EMPTY_STR;
802         if (last_delim_idx != std::string::npos) {
803             par_class_name = ret_sym.substr(last_delim_idx + 1);
804             var_name = ret_sym.substr(0, first_delim_idx);
805             cur_class->SetParentClassName(record_name + par_class_name);
806         }
807         std::string record_name = func->GetRecordName();
808         if (ret_type == ResolveType::UNRESOLVED_MODULE) {
809             std::string imp_par_class_name = GetImportNameByLocalName(par_class_name, record_name);
810             if (!imp_par_class_name.empty()) {
811                 cur_class->SetParentClassName(record_name + imp_par_class_name);
812             }
813             std::string inter_name = var_name.empty() ? par_class_name : var_name;
814             std::string module_name = GetModuleNameByLocalName(inter_name, record_name);
815             if (!module_name.empty()) {
816                 cur_class->SetParClassExternalModuleName(module_name);
817             }
818         }
819         if (ret_type == ResolveType::UNRESOLVED_GLOBAL_VAR) {
820             cur_class->SetParentClassName(record_name + par_class_name);
821             var_name = var_name.empty() ? var_name : ret_sym.substr(0, last_delim_idx);
822             cur_class->SetParClassGlobalVarName(var_name);
823         }
824     });
825 }
826 
ExtractFunctionCalleeInfo(Function * func)827 void AbcFile::ExtractFunctionCalleeInfo(Function *func)
828 {
829     auto &graph = func->GetGraph();
830     graph.VisitAllInstructions([&](const Inst &inst) {
831         std::unique_ptr<CalleeInfo> callee_info {nullptr};
832         switch (inst.GetType()) {
833             case InstType::CALLARG0_IMM8: {
834                 callee_info = ResolveCallInstCommon(func, inst);
835                 callee_info->SetCalleeArgCount(0);
836                 break;
837             }
838             case InstType::CALLARG1_IMM8_V8: {
839                 callee_info = ResolveCallInstCommon(func, inst, 1);
840                 callee_info->SetCalleeArgCount(1);
841                 break;
842             }
843             case InstType::CALLARGS2_IMM8_V8_V8: {
844                 constexpr int ARG_COUNT = 2;
845                 callee_info = ResolveCallInstCommon(func, inst, ARG_COUNT);
846                 callee_info->SetCalleeArgCount(ARG_COUNT);
847                 break;
848             }
849             case InstType::CALLARGS3_IMM8_V8_V8_V8: {
850                 constexpr int ARG_COUNT = 3;
851                 callee_info = ResolveCallInstCommon(func, inst, ARG_COUNT);
852                 callee_info->SetCalleeArgCount(ARG_COUNT);
853                 break;
854             }
855             case InstType::CALLRANGE_IMM8_IMM8_V8: {
856                 uint32_t arg_count = inst.GetImms()[1];
857                 callee_info = ResolveCallInstCommon(func, inst, arg_count);
858                 callee_info->SetCalleeArgCount(arg_count);
859                 break;
860             }
861             case InstType::WIDE_CALLRANGE_PREF_IMM16_V8: {
862                 uint32_t arg_count = inst.GetImms()[0];
863                 callee_info = ResolveCallInstCommon(func, inst, arg_count);
864                 callee_info->SetCalleeArgCount(arg_count);
865                 break;
866             }
867             case InstType::SUPERCALLSPREAD_IMM8_V8: {
868                 callee_info = ResolveCallInstCommon(func, inst, 1);
869                 break;
870             }
871             case InstType::APPLY_IMM8_V8_V8: {
872                 constexpr uint32_t FUNC_OBJ_INDEX = 2;
873                 callee_info = ResolveCallInstCommon(func, inst, FUNC_OBJ_INDEX);
874                 break;
875             }
876             case InstType::CALLTHIS0_IMM8_V8: {
877                 callee_info = ResolveCallInstCommon(func, inst, 1);
878                 callee_info->SetCalleeArgCount(0);
879                 break;
880             }
881             case InstType::CALLTHIS1_IMM8_V8_V8: {
882                 constexpr int ARG_COUNT = 1;
883                 // 1 represents the this pointer
884                 callee_info = ResolveCallInstCommon(func, inst, ARG_COUNT + 1);
885                 callee_info->SetCalleeArgCount(ARG_COUNT);
886                 break;
887             }
888             case InstType::CALLTHIS2_IMM8_V8_V8_V8: {
889                 constexpr int ARG_COUNT = 2;
890                 callee_info = ResolveCallInstCommon(func, inst, ARG_COUNT + 1);
891                 callee_info->SetCalleeArgCount(ARG_COUNT);
892                 break;
893             }
894             case InstType::CALLTHIS3_IMM8_V8_V8_V8_V8: {
895                 constexpr int ARG_COUNT = 3;
896                 callee_info = ResolveCallInstCommon(func, inst, ARG_COUNT + 1);
897                 callee_info->SetCalleeArgCount(ARG_COUNT);
898                 break;
899             }
900             case InstType::CALLTHISRANGE_IMM8_IMM8_V8: {
901                 uint32_t arg_count = inst.GetImms()[1];
902                 callee_info = ResolveCallInstCommon(func, inst, arg_count + 1);
903                 callee_info->SetCalleeArgCount(arg_count);
904                 break;
905             }
906             case InstType::WIDE_CALLTHISRANGE_PREF_IMM16_V8: {
907                 uint32_t arg_count = inst.GetImms()[0];
908                 callee_info = ResolveCallInstCommon(func, inst, arg_count + 1);
909                 callee_info->SetCalleeArgCount(arg_count);
910                 break;
911             }
912             case InstType::SUPERCALLTHISRANGE_IMM8_IMM8_V8:
913             case InstType::SUPERCALLARROWRANGE_IMM8_IMM8_V8: {
914                 uint32_t arg_count = inst.GetImms()[1];
915                 callee_info = ResolveSuperCallInst(func, inst);
916                 callee_info->SetCalleeArgCount(arg_count);
917                 break;
918             }
919             case InstType::WIDE_SUPERCALLTHISRANGE_PREF_IMM16_V8:
920             case InstType::WIDE_SUPERCALLARROWRANGE_PREF_IMM16_V8: {
921                 uint32_t arg_count = inst.GetImms()[0];
922                 callee_info = ResolveSuperCallInst(func, inst);
923                 callee_info->SetCalleeArgCount(arg_count);
924                 break;
925             }
926             default:
927                 break;
928         }
929         if (callee_info != nullptr) {
930             AddCalleeInfo(std::move(callee_info));
931         }
932     });
933 }
934 
BuildFunctionDefineChain(Function * parent_func,Function * child_func) const935 void AbcFile::BuildFunctionDefineChain(Function *parent_func, Function *child_func) const
936 {
937     if (parent_func == nullptr || child_func == nullptr || child_func->GetParentFunction() == parent_func) {
938         return;
939     }
940     child_func->SetParentFunction(parent_func);
941     parent_func->AddDefinedFunction(child_func);
942 }
943 
BuildClassAndMemberFuncRelation(Class * clazz,Function * member_func) const944 void AbcFile::BuildClassAndMemberFuncRelation(Class *clazz, Function *member_func) const
945 {
946     if (clazz == nullptr || member_func == nullptr || member_func->GetClass() == clazz) {
947         return;
948     }
949     clazz->AddMemberFunction(member_func);
950     member_func->SetClass(clazz);
951 }
952 
ExtractClassAndFunctionExportList()953 void AbcFile::ExtractClassAndFunctionExportList()
954 {
955     if (IsMergeAbc()) {
956         ExtractMergedClassAndFunctionExportList();
957     } else {
958         ExtractSingleClassAndFunctionExportList();
959     }
960 }
961 
ExtractMergedClassAndFunctionExportList()962 void AbcFile::ExtractMergedClassAndFunctionExportList()
963 {
964     if (merge_def_func_map_.empty()) {
965         return;
966     }
967 
968     for (auto &merge_def_func_pair : merge_def_func_map_) {
969         if (merge_def_func_pair.second.empty()) {
970             continue;
971         }
972         const Function *func_main = merge_def_func_pair.second[0].get();
973         auto &graph = func_main->GetGraph();
974         graph.VisitAllInstructions([&](const Inst &inst) {
975             auto type = inst.GetType();
976             if (type == InstType::STMODULEVAR_IMM8 || type == InstType::WIDE_STMODULEVAR_PREF_IMM16) {
977                 AddExportListForMerge(func_main, inst);
978             }
979         });
980     }
981 }
982 
AddExportListForMerge(const Function * func_main,const Inst & inst)983 void AbcFile::AddExportListForMerge(const Function *func_main, const Inst &inst)
984 {
985     [[maybe_unused]] auto type = inst.GetType();
986     ASSERT(type == InstType::STMODULEVAR_IMM8 || type == InstType::WIDE_STMODULEVAR_PREF_IMM16);
987 
988     std::string record_name = func_main->GetRecordName();
989     Inst st_module_input0 = inst.GetInputInsts()[0];
990     switch (st_module_input0.GetType()) {
991         case InstType::DEFINEFUNC_IMM8_ID16_IMM8:
992         case InstType::DEFINEFUNC_IMM16_ID16_IMM8: {
993             auto export_func = ResolveDefineFuncInstCommon(func_main, st_module_input0);
994             ASSERT(export_func != nullptr);
995             if (merge_export_func_map_.find(record_name) == merge_export_func_map_.end()) {
996                 merge_export_func_map_.emplace(record_name, std::vector<const Function *>());
997             }
998             merge_export_func_map_[record_name].push_back(export_func);
999             break;
1000         }
1001         case InstType::DEFINECLASSWITHBUFFER_IMM8_ID16_ID16_IMM16_V8:
1002         case InstType::DEFINECLASSWITHBUFFER_IMM16_ID16_ID16_IMM16_V8: {
1003             Class *export_clazz = GetClassByNameImpl(record_name + GetStringByInst(st_module_input0));
1004             ASSERT(export_clazz != nullptr);
1005             if (merge_export_class_map_.find(record_name) == merge_export_class_map_.end()) {
1006                 merge_export_class_map_.emplace(record_name, std::vector<const Class *>());
1007             }
1008             merge_export_class_map_[record_name].push_back(export_clazz);
1009             break;
1010         }
1011         default:
1012             break;
1013     }
1014 }
1015 
ExtractSingleClassAndFunctionExportList()1016 void AbcFile::ExtractSingleClassAndFunctionExportList()
1017 {
1018     if (!IsModule() || def_func_list_.empty()) {
1019         return;
1020     }
1021     const Function *func_main = def_func_list_[0].get();
1022     ASSERT(func_main->GetFunctionName() == ENTRY_FUNCTION_NAME);
1023     auto &graph = func_main->GetGraph();
1024     std::string record_name = func_main->GetRecordName();
1025     graph.VisitAllInstructions([&](const Inst &inst) {
1026         auto type = inst.GetType();
1027         if (type == InstType::STMODULEVAR_IMM8 || type == InstType::WIDE_STMODULEVAR_PREF_IMM16) {
1028             AddExportListForSingle(func_main, inst);
1029         }
1030     });
1031 }
1032 
AddExportListForSingle(const Function * func_main,const Inst & inst)1033 void AbcFile::AddExportListForSingle(const Function *func_main, const Inst &inst)
1034 {
1035     [[maybe_unused]] auto type = inst.GetType();
1036     ASSERT(type == InstType::STMODULEVAR_IMM8 || type == InstType::WIDE_STMODULEVAR_PREF_IMM16);
1037     Inst st_module_input0 = inst.GetInputInsts()[0];
1038     switch (st_module_input0.GetType()) {
1039         case InstType::DEFINEFUNC_IMM8_ID16_IMM8:
1040         case InstType::DEFINEFUNC_IMM16_ID16_IMM8: {
1041             auto export_func = ResolveDefineFuncInstCommon(func_main, st_module_input0);
1042             ASSERT(export_func != nullptr);
1043             export_func_list_.push_back(export_func);
1044             break;
1045         }
1046         case InstType::DEFINECLASSWITHBUFFER_IMM8_ID16_ID16_IMM16_V8:
1047         case InstType::DEFINECLASSWITHBUFFER_IMM16_ID16_ID16_IMM16_V8: {
1048             Class *export_clazz = GetClassByNameImpl(GetStringByInst(st_module_input0));
1049             ASSERT(export_clazz != nullptr);
1050             export_class_list_.push_back(export_clazz);
1051             break;
1052         }
1053         default:
1054             break;
1055     }
1056 }
1057 
GenerateFunctionGraph(const panda_file::MethodDataAccessor & mda,std::string_view func_name)1058 compiler::Graph *AbcFile::GenerateFunctionGraph(const panda_file::MethodDataAccessor &mda, std::string_view func_name)
1059 {
1060     panda::BytecodeOptimizerRuntimeAdapter adapter(mda.GetPandaFile());
1061     auto method_ptr = reinterpret_cast<compiler::RuntimeInterface::MethodPtr>(mda.GetMethodId().GetOffset());
1062     compiler::options.SetCompilerUseSafepoint(false);
1063     compiler::options.SetCompilerMaxBytecodeSize(bytecodeopt::MAX_BYTECODE_SIZE);
1064     compiler::Graph *graph = allocator_->New<compiler::Graph>(allocator_.get(), local_allocator_.get(), Arch::NONE,
1065                                                               method_ptr, &adapter, false, nullptr, true, true);
1066     if ((graph == nullptr) || !graph->RunPass<compiler::IrBuilder>()) {
1067         LOG(FATAL, DEFECT_SCAN_AUX) << "Cannot generate graph for function '" << func_name << "'";
1068     }
1069     return graph;
1070 }
1071 
ResolveInstCommon(Function * func,Inst inst) const1072 ResolveResult AbcFile::ResolveInstCommon(Function *func, Inst inst) const
1073 {
1074     auto type = inst.GetType();
1075     std::string record_name = func->GetRecordName();
1076     switch (type) {
1077         case InstType::DEFINEFUNC_IMM8_ID16_IMM8:
1078         case InstType::DEFINEFUNC_IMM16_ID16_IMM8: {
1079             std::string func_name = record_name + GetStringByInst(inst);
1080             const Function *func = GetFunctionByName(func_name);
1081             ASSERT(func != nullptr);
1082             return std::make_tuple(func, EMPTY_STR, ResolveType::FUNCTION_OBJECT);
1083         }
1084         case InstType::DEFINECLASSWITHBUFFER_IMM8_ID16_ID16_IMM16_V8:
1085         case InstType::DEFINECLASSWITHBUFFER_IMM16_ID16_ID16_IMM16_V8: {
1086             std::string class_name = record_name + GetStringByInst(inst);
1087             const Class *clazz = GetClassByName(class_name);
1088             ASSERT(clazz != nullptr);
1089             return std::make_tuple(clazz, EMPTY_STR, ResolveType::CLASS_OBJECT);
1090         }
1091         case InstType::NEWOBJAPPLY_IMM8_V8:
1092         case InstType::NEWOBJAPPLY_IMM16_V8:
1093         case InstType::NEWOBJRANGE_IMM8_IMM8_V8:
1094         case InstType::NEWOBJRANGE_IMM16_IMM8_V8:
1095         case InstType::WIDE_NEWOBJRANGE_PREF_IMM16_V8: {
1096             Inst newobj_input0 = inst.GetInputInsts()[0];
1097             auto resolve_res = ResolveInstCommon(func, newobj_input0);
1098             return HandleNewObjInstResolveResultCommon(resolve_res);
1099         }
1100         case InstType::LDOBJBYNAME_IMM8_ID16:
1101         case InstType::LDOBJBYNAME_IMM16_ID16: {
1102             Inst ld_obj_input0 = inst.GetInputInsts()[0];
1103             auto resolve_res = ResolveInstCommon(func, ld_obj_input0);
1104             return HandleLdObjByNameInstResolveResult(inst, resolve_res, record_name);
1105         }
1106         case InstType::LDLEXVAR_IMM4_IMM4:
1107         case InstType::LDLEXVAR_IMM8_IMM8:
1108         case InstType::WIDE_LDLEXVAR_PREF_IMM16_IMM16: {
1109             auto p = GetStLexInstByLdLexInst({func, inst});
1110             if (p == std::nullopt) {
1111                 return std::make_tuple(nullptr, EMPTY_STR, ResolveType::UNRESOLVED_OTHER);
1112             }
1113             return ResolveInstCommon(p.value().first, p.value().second);
1114         }
1115         case InstType::STLEXVAR_IMM4_IMM4:
1116         case InstType::STLEXVAR_IMM8_IMM8:
1117         case InstType::WIDE_STLEXVAR_PREF_IMM16_IMM16: {
1118             Inst stlex_input0 = inst.GetInputInsts()[0];
1119             return ResolveInstCommon(func, stlex_input0);
1120         }
1121         case InstType::LDLOCALMODULEVAR_IMM8:
1122         case InstType::WIDE_LDLOCALMODULEVAR_PREF_IMM16: {
1123             size_t index = inst.GetImms()[0];
1124             auto module_record = GetModuleRecordByName(record_name);
1125             const std::string &export_name = module_record->GetExportNameByIndex(index);
1126             const Function *func = GetExportFunctionByExportName(export_name, record_name);
1127             if (func != nullptr) {
1128                 return std::make_tuple(func, EMPTY_STR, ResolveType::FUNCTION_OBJECT);
1129             }
1130             const Class *clazz = GetExportClassByExportName(export_name, record_name);
1131             if (clazz != nullptr) {
1132                 return std::make_tuple(clazz, EMPTY_STR, ResolveType::CLASS_OBJECT);
1133             }
1134             return std::make_tuple(nullptr, EMPTY_STR, ResolveType::UNRESOLVED_OTHER);
1135         }
1136         case InstType::LDEXTERNALMODULEVAR_IMM8:
1137         case InstType::WIDE_LDEXTERNALMODULEVAR_PREF_IMM16: {
1138             size_t index = inst.GetImms()[0];
1139             auto module_record = GetModuleRecordByName(func->GetRecordName());
1140             const std::string &inter_name = module_record->GetImportLocalNameByIndex(index);
1141             return std::make_tuple(nullptr, inter_name, ResolveType::UNRESOLVED_MODULE);
1142         }
1143         case InstType::GETMODULENAMESPACE_IMM8:
1144         case InstType::WIDE_GETMODULENAMESPACE_PREF_IMM16: {
1145             size_t index = inst.GetImms()[0];
1146             auto module_record = GetModuleRecordByName(func->GetRecordName());
1147             const std::string &str = module_record->GetImportNamespaceNameByIndex(index);
1148             return std::make_tuple(nullptr, str, ResolveType::UNRESOLVED_MODULE);
1149         }
1150         case InstType::LDGLOBAL: {
1151             // TODO(wangyantian): load a specific global variable, namely 'globalThis'
1152             return std::make_tuple(nullptr, EMPTY_STR, ResolveType::UNRESOLVED_OTHER);
1153         }
1154         case InstType::LDGLOBALVAR_IMM16_ID16:
1155         case InstType::TRYLDGLOBALBYNAME_IMM8_ID16:
1156         case InstType::TRYLDGLOBALBYNAME_IMM16_ID16: {
1157             std::string str = GetStringByInst(inst);
1158             auto p = GetStGlobalInstByLdGlobalInst({func, inst});
1159             if (p == std::nullopt) {
1160                 return std::make_tuple(nullptr, str, ResolveType::UNRESOLVED_GLOBAL_VAR);
1161             }
1162             auto [ret_ptr, ret_sym, ret_type] = ResolveInstCommon(p.value().first, p.value().second);
1163             if (ret_ptr != nullptr) {
1164                 return std::make_tuple(ret_ptr, str, ret_type);
1165             }
1166             return std::make_tuple(nullptr, str, ResolveType::UNRESOLVED_GLOBAL_VAR);
1167         }
1168         case InstType::TRYSTGLOBALBYNAME_IMM8_ID16:
1169         case InstType::TRYSTGLOBALBYNAME_IMM16_ID16:
1170         case InstType::STGLOBALVAR_IMM16_ID16:
1171         case InstType::STCONSTTOGLOBALRECORD_IMM16_ID16:
1172         case InstType::STTOGLOBALRECORD_IMM16_ID16: {
1173             Inst stglobal_input0 = inst.GetInputInsts()[0];
1174             return ResolveInstCommon(func, stglobal_input0);
1175         }
1176         case InstType::OPCODE_PHI: {
1177             // TODO: only the next unvisited path is considered for now, what about other paths?
1178             // TODO: when all inputs of the phi instruction contain a path to the phi instruction itself,
1179             //  the current solution still causes infinite recursion and stack overflow. However, this case
1180             //  should not occur in real-world applications. However, the following part needs to be redesigned
1181             //  when encountering such scenarios.
1182             auto phi_input_idx = func->GetAndUpdateToVisitInputForInst(inst);
1183             return ResolveInstCommon(func, inst.GetInputInsts()[phi_input_idx]);
1184         }
1185         // don't deal with the situation that func obj comes from parameter or the output of another call inst
1186         default: {
1187             return std::make_tuple(nullptr, EMPTY_STR, ResolveType::UNRESOLVED_OTHER);
1188         }
1189     }
1190 }
1191 
HandleLdObjByNameInstResolveResult(const Inst & ldobjbyname_inst,const ResolveResult & resolve_res,const std::string record_name) const1192 ResolveResult AbcFile::HandleLdObjByNameInstResolveResult(const Inst &ldobjbyname_inst,
1193                                                           const ResolveResult &resolve_res,
1194                                                           const std::string record_name) const
1195 {
1196     auto &[ret_ptr, ret_sym, ret_type] = resolve_res;
1197     std::string name = GetStringByInst(ldobjbyname_inst);
1198     switch (ret_type) {
1199         case ResolveType::UNRESOLVED_MODULE:
1200         case ResolveType::UNRESOLVED_GLOBAL_VAR: {
1201             return std::make_tuple(nullptr, ret_sym + "." + name, ret_type);
1202         }
1203         case ResolveType::FUNCTION_OBJECT: {
1204             ASSERT(ret_ptr != nullptr);
1205             if (name == CALL || name == APPLY) {
1206                 return std::make_tuple(ret_ptr, EMPTY_STR, ResolveType::FUNCTION_OBJECT);
1207             }
1208             return std::make_tuple(nullptr, ret_sym + "." + name, ResolveType::UNRESOLVED_OTHER);
1209         }
1210         case ResolveType::CLASS_OBJECT:
1211         case ResolveType::CLASS_INSTANCE: {
1212             ASSERT(ret_ptr != nullptr);
1213             // TODO(wangyantian): distinguish static func from member func in a class
1214             const void *member_func =
1215                 reinterpret_cast<const Class *>(ret_ptr)->GetMemberFunctionByName(record_name + name);
1216             if (member_func != nullptr) {
1217                 return std::make_tuple(member_func, name, ResolveType::FUNCTION_OBJECT);
1218             }
1219             return std::make_tuple(nullptr, ret_sym + "." + name, ResolveType::UNRESOLVED_OTHER);
1220         }
1221         default: {
1222             return std::make_tuple(nullptr, ret_sym + "." + name, ResolveType::UNRESOLVED_OTHER);
1223         }
1224     }
1225 }
1226 
HandleNewObjInstResolveResultCommon(const ResolveResult & resolve_res) const1227 ResolveResult AbcFile::HandleNewObjInstResolveResultCommon(const ResolveResult &resolve_res) const
1228 {
1229     auto &[ret_ptr, ret_sym, ret_type] = resolve_res;
1230     switch (ret_type) {
1231         case ResolveType::CLASS_OBJECT: {
1232             ASSERT(ret_ptr != nullptr);
1233             return std::make_tuple(ret_ptr, EMPTY_STR, ResolveType::CLASS_INSTANCE);
1234         }
1235         case ResolveType::UNRESOLVED_GLOBAL_VAR:
1236         case ResolveType::UNRESOLVED_MODULE: {
1237             return std::make_tuple(nullptr, ret_sym, ret_type);
1238         }
1239         default: {
1240             return std::make_tuple(nullptr, ret_sym, ResolveType::UNRESOLVED_OTHER);
1241         }
1242     }
1243 }
1244 
ResolveDefineFuncInstCommon(const Function * func,const Inst & def_func_inst) const1245 Function *AbcFile::ResolveDefineFuncInstCommon(const Function *func, const Inst &def_func_inst) const
1246 {
1247     std::string record_name = func->GetRecordName();
1248     std::string def_func_name = record_name + GetStringByInst(def_func_inst);
1249     Function *def_func = GetFunctionByNameImpl(def_func_name);
1250     ASSERT(def_func != nullptr);
1251     return def_func;
1252 }
1253 
ResolveDefineClassWithBufferInst(Function * func,const Inst & define_class_inst) const1254 std::unique_ptr<Class> AbcFile::ResolveDefineClassWithBufferInst(Function *func, const Inst &define_class_inst) const
1255 {
1256     auto imms = define_class_inst.GetImms();
1257     auto m_id = EntityId(imms[1]);
1258     std::string record_name = func->GetRecordName();
1259     std::string class_name = record_name + GetStringByMethodId(m_id);
1260     std::unique_ptr<Class> def_class = std::make_unique<Class>(class_name, record_name, this, func);
1261     if (def_class == nullptr) {
1262         LOG(FATAL, DEFECT_SCAN_AUX) << "Can not allocate memory when processing '" << filename_ << "'";
1263     }
1264     LOG(DEBUG, DEFECT_SCAN_AUX) << "Create a new class: " << class_name;
1265     func->AddDefinedClass(def_class.get());
1266 
1267     // handle ctor of the class
1268     std::string ctor_name = record_name + GetStringByInst(define_class_inst);
1269     HandleMemberFunctionFromClassBuf(ctor_name, func, def_class.get());
1270 
1271     auto literal_array_id = EntityId(imms[2]);
1272     panda_file::LiteralDataAccessor lit_array_accessor(*panda_file_, panda_file_->GetLiteralArraysId());
1273     lit_array_accessor.EnumerateLiteralVals(
1274         literal_array_id, [&](const panda_file::LiteralDataAccessor::LiteralValue &value, const LiteralTag &tag) {
1275             if (tag == LiteralTag::METHOD || tag == panda_file::LiteralTag::GETTER ||
1276                 tag == panda_file::LiteralTag::SETTER || tag == LiteralTag::GENERATORMETHOD ||
1277                 tag == LiteralTag::ASYNCGENERATORMETHOD) {
1278                 auto method_id = EntityId(std::get<uint32_t>(value));
1279                 std::string member_func_name = record_name + GetStringByMethodId(method_id);
1280                 HandleMemberFunctionFromClassBuf(member_func_name, func, def_class.get());
1281             }
1282         });
1283 
1284     return def_class;
1285 }
1286 
ResolveCallInstCommon(Function * func,const Inst & call_inst,uint32_t func_obj_idx) const1287 std::unique_ptr<CalleeInfo> AbcFile::ResolveCallInstCommon(Function *func, const Inst &call_inst,
1288                                                            uint32_t func_obj_idx) const
1289 {
1290     std::unique_ptr<CalleeInfo> callee_info = std::make_unique<CalleeInfo>(call_inst, func);
1291     if (callee_info == nullptr) {
1292         LOG(FATAL, DEFECT_SCAN_AUX) << "Can not allocate memory when processing '" << filename_ << "'";
1293     }
1294 
1295     Inst call_input0 = call_inst.GetInputInsts()[func_obj_idx];
1296     auto [ret_ptr, ret_sym, ret_type] = ResolveInstCommon(func, call_input0);
1297     std::string record_name = func->GetRecordName();
1298     if (ret_ptr != nullptr && ret_type == ResolveType::FUNCTION_OBJECT) {
1299         auto callee = reinterpret_cast<const Function *>(ret_ptr);
1300         callee_info->SetCallee(callee);
1301     } else {
1302         size_t first_delim_idx = ret_sym.find_first_of(DELIM);
1303         size_t last_delim_idx = ret_sym.find_last_of(DELIM);
1304         std::string callee_name = ret_sym;
1305         std::string var_name = EMPTY_STR;
1306         if (first_delim_idx != std::string::npos) {
1307             callee_name = ret_sym.substr(last_delim_idx + 1);
1308             var_name = ret_sym.substr(0, first_delim_idx);
1309             callee_info->SetFunctionName(record_name + callee_name);
1310         }
1311         if (ret_type == ResolveType::UNRESOLVED_MODULE) {
1312             std::string imp_callee_name = GetImportNameByLocalName(callee_name, record_name);
1313             if (!imp_callee_name.empty()) {
1314                 callee_info->SetFunctionName(record_name + imp_callee_name);
1315             }
1316             std::string inter_name = var_name.empty() ? callee_name : var_name;
1317             std::string module_name = GetModuleNameByLocalName(inter_name, record_name);
1318             if (!module_name.empty()) {
1319                 callee_info->SetExternalModuleName(module_name);
1320             }
1321         } else if (ret_type == ResolveType::UNRESOLVED_GLOBAL_VAR) {
1322             callee_info->SetFunctionName(record_name + callee_name);
1323             var_name = var_name.empty() ? var_name : ret_sym.substr(0, last_delim_idx);
1324             callee_info->SetGlobalVarName(var_name);
1325         }
1326     }
1327     func->AddCalleeInfo(callee_info.get());
1328     return callee_info;
1329 }
1330 
ResolveSuperCallInst(Function * func,const Inst & call_inst) const1331 std::unique_ptr<CalleeInfo> AbcFile::ResolveSuperCallInst(Function *func, const Inst &call_inst) const
1332 {
1333     std::unique_ptr<CalleeInfo> callee_info = std::make_unique<CalleeInfo>(call_inst, func);
1334     if (callee_info == nullptr) {
1335         LOG(FATAL, DEFECT_SCAN_AUX) << "Can not allocate memory when processing '" << filename_ << "'";
1336     }
1337     const Class *clazz = func->GetClass();
1338     if (clazz != nullptr && clazz->GetParentClass() != nullptr) {
1339         const std::string &parent_ctor_name = clazz->GetParentClass()->GetClassName();
1340         const Function *parent_ctor = GetFunctionByName(parent_ctor_name);
1341         ASSERT(parent_ctor != nullptr);
1342         callee_info->SetCallee(parent_ctor);
1343     }
1344     // TODO(wangyantian): deal with situations when above if doesn't hold
1345     func->AddCalleeInfo(callee_info.get());
1346     return callee_info;
1347 }
1348 
ResolveDefineMethodInst(Function * member_func,const Inst & define_method_inst)1349 void AbcFile::ResolveDefineMethodInst(Function *member_func, const Inst &define_method_inst)
1350 {
1351     std::string record_name = member_func->GetRecordName();
1352     Inst def_method_input0 = define_method_inst.GetInputInsts()[0];
1353     if ((def_method_input0.GetType() == InstType::LDOBJBYNAME_IMM8_ID16 ||
1354          def_method_input0.GetType() == InstType::LDOBJBYNAME_IMM16_ID16) &&
1355         GetStringByInst(def_method_input0) == PROTOTYPE) {
1356         Inst ld_obj_input0 = def_method_input0.GetInputInsts()[0];
1357         if (ld_obj_input0.GetType() == InstType::DEFINECLASSWITHBUFFER_IMM8_ID16_ID16_IMM16_V8 ||
1358             ld_obj_input0.GetType() == InstType::DEFINECLASSWITHBUFFER_IMM16_ID16_ID16_IMM16_V8) {
1359             auto clazz = GetClassByNameImpl(record_name + GetStringByInst(ld_obj_input0));
1360             if (clazz != nullptr) {
1361                 BuildClassAndMemberFuncRelation(clazz, member_func);
1362             }
1363         }
1364     }
1365 }
1366 
HandleMemberFunctionFromClassBuf(const std::string & func_name,Function * def_func,Class * def_class) const1367 void AbcFile::HandleMemberFunctionFromClassBuf(const std::string &func_name, Function *def_func, Class *def_class) const
1368 {
1369     Function *member_func = GetFunctionByNameImpl(func_name);
1370     ASSERT(member_func != nullptr);
1371     BuildFunctionDefineChain(def_func, member_func);
1372     BuildClassAndMemberFuncRelation(def_class, member_func);
1373 }
1374 
AddDefinedClass(std::shared_ptr<Class> && def_class)1375 void AbcFile::AddDefinedClass(std::shared_ptr<Class> &&def_class)
1376 {
1377     auto &class_name = def_class->GetClassName();
1378     ASSERT(def_class_map_.find(class_name) == def_class_map_.end());
1379     def_class_map_[class_name] = def_class.get();
1380     def_class_list_.emplace_back(std::move(def_class));
1381 }
1382 
AddMergedDefinedClass(std::shared_ptr<Class> && def_class,std::string record_name)1383 void AbcFile::AddMergedDefinedClass(std::shared_ptr<Class> &&def_class, std::string record_name)
1384 {
1385     auto &class_name = def_class->GetClassName();
1386     ASSERT(def_class_map_.find(class_name) == def_class_map_.end());
1387     def_class_map_[class_name] = def_class.get();
1388     if (merge_def_class_map_.find(record_name) == merge_def_class_map_.end()) {
1389         merge_def_class_map_.emplace(record_name, std::vector<std::shared_ptr<Class>>());
1390     }
1391     merge_def_class_map_[record_name].push_back(def_class);
1392     merged_def_class_list_.push_back(def_class);
1393 }
1394 
AddDefinedFunction(std::shared_ptr<Function> && def_func)1395 void AbcFile::AddDefinedFunction(std::shared_ptr<Function> &&def_func)
1396 {
1397     const std::string &func_name = def_func->GetFunctionName();
1398     ASSERT(def_func_map_.find(func_name) == def_func_map_.end());
1399     def_func_map_[func_name] = def_func.get();
1400     if (func_name != ENTRY_FUNCTION_NAME) {
1401         def_func_list_.emplace_back(std::move(def_func));
1402     } else {
1403         def_func_list_.insert(def_func_list_.begin(), std::move(def_func));
1404     }
1405 }
1406 
AddMergedDefinedFunction(std::shared_ptr<Function> && def_func)1407 void AbcFile::AddMergedDefinedFunction(std::shared_ptr<Function> &&def_func)
1408 {
1409     const std::string &record_name = def_func->GetRecordName();
1410     const std::string &func_name = def_func->GetFunctionName();
1411     ASSERT(def_func_map_.find(func_name) == def_func_map_.end());
1412     def_func_map_[func_name] = def_func.get();
1413     merged_def_func_list_.push_back(def_func);
1414     if (merge_def_func_map_.find(record_name) == merge_def_func_map_.end()) {
1415         merge_def_func_map_.emplace(record_name, std::vector<std::shared_ptr<Function>>());
1416     }
1417 
1418     if (func_name.find(ENTRY_FUNCTION_NAME) == std::string::npos) {
1419         merge_def_func_map_[record_name].push_back(def_func);
1420     } else {
1421         merge_def_func_map_[record_name].insert(merge_def_func_map_[record_name].begin(), def_func);
1422     }
1423 }
1424 
AddCalleeInfo(std::unique_ptr<CalleeInfo> && callee_info)1425 void AbcFile::AddCalleeInfo(std::unique_ptr<CalleeInfo> &&callee_info)
1426 {
1427     callee_info_list_.emplace_back(std::move(callee_info));
1428 }
1429 
GetFunctionByNameImpl(std::string_view func_name) const1430 Function *AbcFile::GetFunctionByNameImpl(std::string_view func_name) const
1431 {
1432     auto iter = def_func_map_.find(std::string(func_name));
1433     if (iter != def_func_map_.end()) {
1434         return iter->second;
1435     }
1436     return nullptr;
1437 }
1438 
GetModuleRecordByName(std::string record_name) const1439 const ModuleRecord *AbcFile::GetModuleRecordByName(std::string record_name) const
1440 {
1441     if (!IsMergeAbc()) {
1442         record_name = std::string(MODULE_CLASS);
1443     }
1444 
1445     auto iter = module_record_map_.find(record_name);
1446     if (iter != module_record_map_.end()) {
1447         return iter->second;
1448     }
1449     return nullptr;
1450 }
1451 
GetClassByNameImpl(std::string_view class_name) const1452 Class *AbcFile::GetClassByNameImpl(std::string_view class_name) const
1453 {
1454     auto iter = def_class_map_.find(std::string(class_name));
1455     if (iter != def_class_map_.end()) {
1456         return iter->second;
1457     }
1458     return nullptr;
1459 }
1460 
GetStringByMethodId(EntityId method_id) const1461 std::string AbcFile::GetStringByMethodId(EntityId method_id) const
1462 {
1463     panda_file::MethodDataAccessor mda {*panda_file_, method_id};
1464     return GetStringByStringId(mda.GetNameId());
1465 }
1466 
GetStringByStringId(EntityId string_id) const1467 std::string AbcFile::GetStringByStringId(EntityId string_id) const
1468 {
1469     StringData sd = panda_file_->GetStringData(string_id);
1470     // TODO(wangyantian): what if sd.is_ascii equals false?
1471     return std::string(utf::Mutf8AsCString(sd.data));
1472 }
1473 }  // namespace panda::defect_scan_aux
1474