1 /* 2 * Copyright (c) 2024 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 "abc2program_entity_container.h" 17 #include "abc_file_utils.h" 18 #include "file-inl.h" 19 #include "method_data_accessor-inl.h" 20 #include "mangling.h" 21 #include "os/file.h" 22 23 namespace panda::abc2program { 24 GetAbcFile() const25const panda_file::File &Abc2ProgramEntityContainer::GetAbcFile() const 26 { 27 return file_; 28 } 29 GetProgram() const30pandasm::Program &Abc2ProgramEntityContainer::GetProgram() const 31 { 32 return program_; 33 } 34 GetDebugInfoExtractor() const35const panda_file::DebugInfoExtractor &Abc2ProgramEntityContainer::GetDebugInfoExtractor() const 36 { 37 return debug_info_extractor_; 38 } 39 GetStringById(const panda_file::File::EntityId & entity_id) const40std::string Abc2ProgramEntityContainer::GetStringById(const panda_file::File::EntityId &entity_id) const 41 { 42 panda_file::File::StringData sd = file_.GetStringData(entity_id); 43 return (reinterpret_cast<const char *>(sd.data)); 44 } 45 GetFullRecordNameById(const panda_file::File::EntityId & class_id)46std::string Abc2ProgramEntityContainer::GetFullRecordNameById(const panda_file::File::EntityId &class_id) 47 { 48 uint32_t class_id_offset = class_id.GetOffset(); 49 auto it = record_full_name_map_.find(class_id_offset); 50 if (it != record_full_name_map_.end()) { 51 return it->second; 52 } 53 std::string name = GetStringById(class_id); 54 pandasm::Type type = pandasm::Type::FromDescriptor(name); 55 std::string record_full_name = type.GetName(); 56 ModifyRecordName(record_full_name); 57 record_full_name_map_.emplace(class_id_offset, record_full_name); 58 return record_full_name; 59 } 60 61 /** 62 * Support the inter-app hsp dependents bytecode har. 63 * The record name format: <bundleName>&<normalizedImportPath>&<version> 64 * The ohmurl specs that must have bundleName in inter-app package. The records need compile into the inter-app 65 * hsp package. So the recordNames need to add bundleName in front when the abc-file as input for the 66 * inter-app hsp package. 67 */ ModifyRecordName(std::string & record_name)68void Abc2ProgramEntityContainer::ModifyRecordName(std::string &record_name) 69 { 70 if (bundle_name_.empty()) { 71 return; 72 } 73 if (IsSourceFileRecord(record_name)) { 74 record_name = bundle_name_ + record_name; 75 } 76 } 77 IsSourceFileRecord(const std::string & record_name)78bool Abc2ProgramEntityContainer::IsSourceFileRecord(const std::string& record_name) 79 { 80 return record_name.find(NORMALIZED_OHMURL_SEPARATOR) == 0; 81 } 82 GetFullMethodNameById(const panda_file::File::EntityId & method_id)83std::string Abc2ProgramEntityContainer::GetFullMethodNameById(const panda_file::File::EntityId &method_id) 84 { 85 auto method_id_offset = method_id.GetOffset(); 86 auto it = method_full_name_map_.find(method_id_offset); 87 if (it != method_full_name_map_.end()) { 88 return it->second; 89 } 90 std::string full_method_name = ConcatFullMethodNameById(method_id); 91 method_full_name_map_.emplace(method_id_offset, full_method_name); 92 return full_method_name; 93 } 94 ConcatFullMethodNameById(const panda_file::File::EntityId & method_id)95std::string Abc2ProgramEntityContainer::ConcatFullMethodNameById(const panda_file::File::EntityId &method_id) 96 { 97 panda::panda_file::MethodDataAccessor method_data_accessor(file_, method_id); 98 std::string method_name_raw = GetStringById(method_data_accessor.GetNameId()); 99 std::string record_name = GetFullRecordNameById(method_data_accessor.GetClassId()); 100 std::stringstream ss; 101 if (AbcFileUtils::IsSystemTypeName(record_name)) { 102 ss << DOT; 103 } else { 104 ss << record_name << DOT; 105 } 106 ss << method_name_raw; 107 return ss.str(); 108 } 109 GetMouleLiteralArrayIdSet() const110const std::unordered_set<uint32_t> &Abc2ProgramEntityContainer::GetMouleLiteralArrayIdSet() const 111 { 112 return module_literal_array_id_set_; 113 } 114 GetModuleRequestPhaseIdSet() const115const std::unordered_set<uint32_t> &Abc2ProgramEntityContainer::GetModuleRequestPhaseIdSet() const 116 { 117 return module_request_phase_id_set_; 118 } 119 GetUnnestedLiteralArrayIdSet() const120const std::unordered_set<uint32_t> &Abc2ProgramEntityContainer::GetUnnestedLiteralArrayIdSet() const 121 { 122 return unnested_literal_array_id_set_; 123 } 124 GetUnprocessedNestedLiteralArrayIdSet()125std::unordered_set<uint32_t> &Abc2ProgramEntityContainer::GetUnprocessedNestedLiteralArrayIdSet() 126 { 127 return unprocessed_nested_literal_array_id_set_; 128 } 129 AddModuleLiteralArrayId(uint32_t module_literal_array_id)130void Abc2ProgramEntityContainer::AddModuleLiteralArrayId(uint32_t module_literal_array_id) 131 { 132 module_literal_array_id_set_.insert(module_literal_array_id); 133 } 134 135 AddModuleRequestPhaseId(uint32_t module_request_phase_id)136void Abc2ProgramEntityContainer::AddModuleRequestPhaseId(uint32_t module_request_phase_id) 137 { 138 module_request_phase_id_set_.insert(module_request_phase_id); 139 } 140 AddUnnestedLiteralArrayId(uint32_t literal_array_id)141void Abc2ProgramEntityContainer::AddUnnestedLiteralArrayId(uint32_t literal_array_id) 142 { 143 unnested_literal_array_id_set_.insert(literal_array_id); 144 } 145 AddProcessedNestedLiteralArrayId(uint32_t nested_literal_array_id)146void Abc2ProgramEntityContainer::AddProcessedNestedLiteralArrayId(uint32_t nested_literal_array_id) 147 { 148 processed_nested_literal_array_id_set_.insert(nested_literal_array_id); 149 } 150 TryAddUnprocessedNestedLiteralArrayId(uint32_t nested_literal_array_id)151void Abc2ProgramEntityContainer::TryAddUnprocessedNestedLiteralArrayId(uint32_t nested_literal_array_id) 152 { 153 if (unnested_literal_array_id_set_.count(nested_literal_array_id)) { 154 return; 155 } 156 if (processed_nested_literal_array_id_set_.count(nested_literal_array_id)) { 157 return; 158 } 159 unprocessed_nested_literal_array_id_set_.insert(nested_literal_array_id); 160 } 161 GetLiteralArrayIdName(uint32_t literal_array_id)162std::string Abc2ProgramEntityContainer::GetLiteralArrayIdName(uint32_t literal_array_id) 163 { 164 std::stringstream name; 165 auto cur_record_name = GetFullRecordNameById(panda_file::File::EntityId(current_class_id_)); 166 name << cur_record_name << UNDERLINE << literal_array_id; 167 return name.str(); 168 } 169 AddProgramString(const std::string & str) const170void Abc2ProgramEntityContainer::AddProgramString(const std::string &str) const 171 { 172 program_.strings.insert(str); 173 } 174 175 } // namespace panda::abc2program