• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "module_record.h"
17 
18 #include "configs/guard_context.h"
19 #include "program.h"
20 #include "util/assert_util.h"
21 #include "util/string_util.h"
22 
23 namespace {
24 constexpr std::string_view TAG = "[Module_Record]";
25 constexpr std::string_view MODULE_RECORD_IDX = "moduleRecordIdx";
26 constexpr std::string_view SCOPE_DELIMITER = "#";
27 constexpr std::string_view PATH_DELIMITER = "/";
28 
GetMethodAffiliateValueByOffset(const std::vector<panda::pandasm::LiteralArray::Literal> & literals,uint32_t & offset)29 uint16_t GetMethodAffiliateValueByOffset(const std::vector<panda::pandasm::LiteralArray::Literal> &literals,
30                                          uint32_t &offset)
31 {
32     PANDA_GUARD_ASSERT_PRINT(offset >= literals.size(), TAG, panda::guard::ErrorCode::GENERIC_ERROR, "offset overflow");
33     PANDA_GUARD_ASSERT_PRINT(literals[offset].tag_ != panda::panda_file::LiteralTag::METHODAFFILIATE, TAG,
34                              panda::guard::ErrorCode::GENERIC_ERROR, "not method affiliate value");
35     return std::get<uint16_t>(literals[offset++].value_);
36 }
37 
GetIntegerValueByOffset(const std::vector<panda::pandasm::LiteralArray::Literal> & literals,uint32_t & offset)38 uint32_t GetIntegerValueByOffset(const std::vector<panda::pandasm::LiteralArray::Literal> &literals, uint32_t &offset)
39 {
40     PANDA_GUARD_ASSERT_PRINT(offset >= literals.size(), TAG, panda::guard::ErrorCode::GENERIC_ERROR, "offset overflow");
41     PANDA_GUARD_ASSERT_PRINT(!literals[offset].IsIntegerValue(), TAG, panda::guard::ErrorCode::GENERIC_ERROR,
42                              "not integer value");
43     return std::get<uint32_t>(literals[offset++].value_);
44 }
45 
GetStringValueByOffset(const std::vector<panda::pandasm::LiteralArray::Literal> & literals,uint32_t & offset)46 std::string GetStringValueByOffset(const std::vector<panda::pandasm::LiteralArray::Literal> &literals, uint32_t &offset)
47 {
48     PANDA_GUARD_ASSERT_PRINT(offset >= literals.size(), TAG, panda::guard::ErrorCode::GENERIC_ERROR, "offset overflow");
49     PANDA_GUARD_ASSERT_PRINT(!literals[offset].IsStringValue(), TAG, panda::guard::ErrorCode::GENERIC_ERROR,
50                              "not string value");
51     return std::get<std::string>(literals[offset++].value_);
52 }
53 }  // namespace
54 
Update()55 void panda::guard::FilePathItem::Update()
56 {
57     this->refFilePath_.Update();
58     if (this->refFilePath_.filePath_ == this->refFilePath_.obfFilePath_) {
59         return;
60     }
61 
62     auto &literalArrayTable = program_->prog_->literalarray_table;
63     auto it = literalArrayTable.find(literalArrayIdx_);
64     PANDA_GUARD_ASSERT_PRINT(it == literalArrayTable.end(), TAG, ErrorCode::GENERIC_ERROR,
65                              "get bad literalArrayIdx:" << literalArrayIdx_);
66     it->second.literals_[this->refFilePath_.filePathIndex_].value_ = this->refFilePath_.obfFilePath_;
67 }
68 
ExtractNames(std::set<std::string> & strings) const69 void panda::guard::FilePathItem::ExtractNames(std::set<std::string> &strings) const
70 {
71     auto parts = StringUtil::Split(this->refFilePath_.GetRawPath(), PATH_DELIMITER.data());
72     for (const auto &part : parts) {
73         strings.emplace(part);
74     }
75 }
76 
RefreshNeedUpdate()77 void panda::guard::FilePathItem::RefreshNeedUpdate()
78 {
79     this->needUpdate = GuardContext::GetInstance()->GetGuardOptions()->IsFileNameObfEnabled() &&
80                        this->refFilePath_.pathType_ != FilePathType::EXTERNAL_DEPENDENCE;
81     if (!this->needUpdate) {
82         auto parts = StringUtil::Split(this->refFilePath_.GetRawPath(), PATH_DELIMITER.data());
83         for (const auto &part : parts) {
84             GuardContext::GetInstance()->GetNameMapping()->AddFileNameMapping(part);
85         }
86     }
87 }
88 
Build()89 void panda::guard::FilePathItem::Build()
90 {
91     this->refFilePath_.Init();
92 }
93 
Update()94 void panda::guard::RegularImportItem::Update()
95 {
96     auto &literalArrayTable = this->program_->prog_->literalarray_table;
97     auto it = literalArrayTable.find(this->literalArrayIdx_);
98     PANDA_GUARD_ASSERT_PRINT(it == literalArrayTable.end(), TAG, ErrorCode::GENERIC_ERROR,
99                              "get bad literalArrayIdx:" << literalArrayIdx_);
100 
101     this->obfLocalName_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->localName_);
102     it->second.literals_[this->localNameIndex_].value_ = this->obfLocalName_;
103 
104     this->obfImportName_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->importName_);
105     it->second.literals_[this->importNameIndex_].value_ = this->obfImportName_;
106 }
107 
ExtractNames(std::set<std::string> & strings) const108 void panda::guard::RegularImportItem::ExtractNames(std::set<std::string> &strings) const
109 {
110     strings.emplace(this->localName_);
111     strings.emplace(this->importName_);
112 }
113 
RefreshNeedUpdate()114 void panda::guard::RegularImportItem::RefreshNeedUpdate()
115 {
116     this->needUpdate = GuardContext::GetInstance()->GetGuardOptions()->IsExportObfEnabled() && !remoteFile_;
117     if (!this->needUpdate) {
118         GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->localName_);
119         GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->importName_);
120     }
121 }
122 
WriteFileCache(const std::string & filePath)123 void panda::guard::RegularImportItem::WriteFileCache(const std::string &filePath)
124 {
125     auto nameCache = GuardContext::GetInstance()->GetNameCache();
126     if (this->localName_ != this->obfLocalName_) {
127         nameCache->AddObfIdentifierName(filePath, SCOPE_DELIMITER.data() + this->localName_, this->obfLocalName_);
128     }
129     if ((this->importName_ != this->localName_) && (this->importName_ != this->obfImportName_)) {
130         nameCache->AddObfIdentifierName(filePath, SCOPE_DELIMITER.data() + this->importName_, this->obfImportName_);
131     }
132 }
133 
WritePropertyCache()134 void panda::guard::RegularImportItem::WritePropertyCache()
135 {
136     if (!GuardContext::GetInstance()->GetGuardOptions()->IsPropertyObfEnabled()) {
137         return;
138     }
139 
140     auto nameCache = GuardContext::GetInstance()->GetNameCache();
141     nameCache->AddObfPropertyName(this->localName_, this->obfLocalName_);
142     nameCache->AddObfPropertyName(this->importName_, this->obfImportName_);
143 }
144 
Update()145 void panda::guard::NameSpaceImportItem::Update()
146 {
147     auto &literalArrayTable = this->program_->prog_->literalarray_table;
148     auto it = literalArrayTable.find(this->literalArrayIdx_);
149     PANDA_GUARD_ASSERT_PRINT(it == literalArrayTable.end(), TAG, ErrorCode::GENERIC_ERROR,
150                              "get bad literalArrayIdx:" << literalArrayIdx_);
151 
152     this->obfLocalName_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->localName_);
153     it->second.literals_[this->localNameIndex_].value_ = this->obfLocalName_;
154 }
155 
ExtractNames(std::set<std::string> & strings) const156 void panda::guard::NameSpaceImportItem::ExtractNames(std::set<std::string> &strings) const
157 {
158     strings.emplace(this->localName_);
159 }
160 
RefreshNeedUpdate()161 void panda::guard::NameSpaceImportItem::RefreshNeedUpdate()
162 {
163     this->needUpdate = GuardContext::GetInstance()->GetGuardOptions()->IsToplevelObfEnabled() && !remoteFile_;
164     if (!this->needUpdate) {
165         GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->localName_);
166     }
167 }
168 
WriteFileCache(const std::string & filePath)169 void panda::guard::NameSpaceImportItem::WriteFileCache(const std::string &filePath)
170 {
171     auto nameCache = GuardContext::GetInstance()->GetNameCache();
172     if (this->localName_ != this->obfLocalName_) {
173         nameCache->AddObfIdentifierName(filePath, SCOPE_DELIMITER.data() + this->localName_, this->obfLocalName_);
174     }
175 }
176 
WritePropertyCache()177 void panda::guard::NameSpaceImportItem::WritePropertyCache()
178 {
179     if (!GuardContext::GetInstance()->GetGuardOptions()->IsPropertyObfEnabled()) {
180         return;
181     }
182     GuardContext::GetInstance()->GetNameCache()->AddObfPropertyName(this->localName_, this->obfLocalName_);
183 }
184 
Update()185 void panda::guard::LocalExportItem::Update()
186 {
187     auto &literalArrayTable = this->program_->prog_->literalarray_table;
188     auto it = literalArrayTable.find(this->literalArrayIdx_);
189     PANDA_GUARD_ASSERT_PRINT(it == literalArrayTable.end(), TAG, ErrorCode::GENERIC_ERROR,
190                              "get bad literalArrayIdx:" << literalArrayIdx_);
191 
192     if (GuardContext::GetInstance()->GetGuardOptions()->IsExportObfEnabled()) {
193         this->obfLocalName_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->localName_);
194         it->second.literals_[this->localNameIndex_].value_ = this->obfLocalName_;
195 
196         this->obfExportName_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->exportName_);
197         it->second.literals_[this->exportNameIndex_].value_ = this->obfExportName_;
198         return;
199     }
200 
201     if (this->localName_ != this->exportName_) {
202         this->obfLocalName_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->localName_);
203         it->second.literals_[this->localNameIndex_].value_ = this->obfLocalName_;
204     }
205 }
206 
ExtractNames(std::set<std::string> & strings) const207 void panda::guard::LocalExportItem::ExtractNames(std::set<std::string> &strings) const
208 {
209     strings.emplace(this->localName_);
210     strings.emplace(this->exportName_);
211 }
212 
RefreshNeedUpdate()213 void panda::guard::LocalExportItem::RefreshNeedUpdate()
214 {
215     auto options = GuardContext::GetInstance()->GetGuardOptions();
216     if (!options->IsToplevelObfEnabled()) {
217         this->needUpdate = false;
218         GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->localName_);
219         GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->exportName_);
220         return;
221     }
222     if (options->IsExportObfEnabled()) {
223         this->needUpdate = true;
224         return;
225     }
226     if (this->localName_ != this->exportName_) {
227         this->needUpdate = true;
228         GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->exportName_);
229         return;
230     }
231     this->needUpdate = false;
232     GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->localName_);
233     GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->exportName_);
234 }
235 
WriteFileCache(const std::string & filePath)236 void panda::guard::LocalExportItem::WriteFileCache(const std::string &filePath)
237 {
238     auto nameCache = GuardContext::GetInstance()->GetNameCache();
239     if (this->localName_ != this->obfLocalName_) {
240         nameCache->AddObfIdentifierName(filePath, SCOPE_DELIMITER.data() + this->localName_, this->obfLocalName_);
241     }
242     if ((this->exportName_ != this->localName_) && (this->exportName_ != this->obfExportName_)) {
243         nameCache->AddObfIdentifierName(filePath, SCOPE_DELIMITER.data() + this->exportName_, this->obfExportName_);
244     }
245 }
246 
WritePropertyCache()247 void panda::guard::LocalExportItem::WritePropertyCache()
248 {
249     if (!GuardContext::GetInstance()->GetGuardOptions()->IsPropertyObfEnabled()) {
250         return;
251     }
252 
253     auto nameCache = GuardContext::GetInstance()->GetNameCache();
254     nameCache->AddObfPropertyName(this->localName_, this->obfLocalName_);
255     nameCache->AddObfPropertyName(this->exportName_, this->obfExportName_);
256 }
257 
Update()258 void panda::guard::IndirectExportItem::Update()
259 {
260     auto &literalArrayTable = this->program_->prog_->literalarray_table;
261     auto it = literalArrayTable.find(this->literalArrayIdx_);
262     PANDA_GUARD_ASSERT_PRINT(it == literalArrayTable.end(), TAG, ErrorCode::GENERIC_ERROR,
263                              "get bad literalArrayIdx:" << literalArrayIdx_);
264 
265     this->obfImportName_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->importName_);
266     it->second.literals_[this->importNameIndex_].value_ = this->obfImportName_;
267 
268     this->obfExportName_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->exportName_);
269     it->second.literals_[this->exportNameIndex_].value_ = this->obfExportName_;
270 }
271 
ExtractNames(std::set<std::string> & strings) const272 void panda::guard::IndirectExportItem::ExtractNames(std::set<std::string> &strings) const
273 {
274     strings.emplace(this->importName_);
275     strings.emplace(this->exportName_);
276 }
277 
RefreshNeedUpdate()278 void panda::guard::IndirectExportItem::RefreshNeedUpdate()
279 {
280     this->needUpdate = GuardContext::GetInstance()->GetGuardOptions()->IsExportObfEnabled() && !remoteFile_;
281     if (!this->needUpdate) {
282         GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->importName_);
283         GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->exportName_);
284     }
285 }
286 
WriteFileCache(const std::string & filePath)287 void panda::guard::IndirectExportItem::WriteFileCache(const std::string &filePath)
288 {
289     auto nameCache = GuardContext::GetInstance()->GetNameCache();
290     if (this->importName_ != this->obfImportName_) {
291         nameCache->AddObfIdentifierName(filePath, SCOPE_DELIMITER.data() + this->importName_, this->obfImportName_);
292     }
293     if ((this->exportName_ != this->importName_) && (this->exportName_ != this->obfExportName_)) {
294         nameCache->AddObfIdentifierName(filePath, SCOPE_DELIMITER.data() + this->exportName_, this->obfExportName_);
295     }
296 }
297 
WritePropertyCache()298 void panda::guard::IndirectExportItem::WritePropertyCache()
299 {
300     if (!GuardContext::GetInstance()->GetGuardOptions()->IsPropertyObfEnabled()) {
301         return;
302     }
303 
304     auto nameCache = GuardContext::GetInstance()->GetNameCache();
305     nameCache->AddObfPropertyName(this->importName_, this->obfImportName_);
306     nameCache->AddObfPropertyName(this->exportName_, this->obfExportName_);
307 }
308 
Build()309 void panda::guard::ModuleRecord::Build()
310 {
311     auto recordItem = program_->prog_->record_table.find(this->name_);
312     PANDA_GUARD_ASSERT_PRINT(recordItem == program_->prog_->record_table.end(), TAG, ErrorCode::GENERIC_ERROR,
313                              "name:" << this->name_ << "not find in record_table");
314 
315     for (const auto &field : recordItem->second.field_list) {
316         if (field.name == MODULE_RECORD_IDX) {
317             const auto &value = field.metadata->GetValue();
318             this->literalArrayIdx_ = value->GetValue<std::string>();
319             break;
320         }
321     }
322 
323     if (this->literalArrayIdx_.empty()) {
324         // commonJs module don't have MODULE_RECORD_IDX
325         LOG(INFO, PANDAGUARD) << TAG << "no MODULE_RECORD_IDX in:" << this->name_;
326         return;
327     }
328 
329     auto &literalArray = program_->prog_->literalarray_table.at(literalArrayIdx_);
330     CreateModuleVar(literalArray);
331 }
332 
ExtractNames(std::set<std::string> & strings) const333 void panda::guard::ModuleRecord::ExtractNames(std::set<std::string> &strings) const
334 {
335     for (const auto &item : this->filePathList_) {
336         item.ExtractNames(strings);
337     }
338 
339     for (const auto &item : this->regularImportList_) {
340         item.ExtractNames(strings);
341     }
342 
343     for (const auto &item : this->nameSpaceImportList_) {
344         item.ExtractNames(strings);
345     }
346 
347     for (const auto &item : this->localExportList_) {
348         item.ExtractNames(strings);
349     }
350 
351     for (const auto &item : this->indirectExportList_) {
352         item.ExtractNames(strings);
353     }
354 }
355 
IsExportVar(const std::string & var)356 bool panda::guard::ModuleRecord::IsExportVar(const std::string &var)
357 {
358     bool exportVar = std::any_of(localExportList_.begin(), localExportList_.end(), [&](const LocalExportItem &item) {
359         return (item.localName_ == var) || (item.exportName_ == var);
360     });
361     if (exportVar) {
362         return true;
363     }
364     return std::any_of(indirectExportList_.begin(), indirectExportList_.end(), [&](const IndirectExportItem &item) {
365         return (item.importName_ == var) || (item.exportName_ == var);
366     });
367 }
368 
Update()369 void panda::guard::ModuleRecord::Update()
370 {
371     LOG(INFO, PANDAGUARD) << TAG << "update for " << this->literalArrayIdx_ << " start";
372 
373     for (auto &item : this->regularImportList_) {
374         item.Obfuscate();
375     }
376 
377     for (auto &item : this->nameSpaceImportList_) {
378         item.Obfuscate();
379     }
380 
381     for (auto &item : this->localExportList_) {
382         item.Obfuscate();
383     }
384 
385     for (auto &item : this->indirectExportList_) {
386         item.Obfuscate();
387     }
388     LOG(INFO, PANDAGUARD) << TAG << "update for " << this->literalArrayIdx_ << " end";
389 }
390 
WriteNameCache(const std::string & filePath)391 void panda::guard::ModuleRecord::WriteNameCache(const std::string &filePath)
392 {
393     if (!this->obfuscated) {
394         return;
395     }
396 
397     for (auto &item : this->filePathList_) {
398         item.WriteNameCache(filePath);
399     }
400 
401     for (auto &item : this->regularImportList_) {
402         item.WriteNameCache(filePath);
403     }
404 
405     for (auto &item : this->nameSpaceImportList_) {
406         item.WriteNameCache(filePath);
407     }
408 
409     for (auto &item : this->localExportList_) {
410         item.WriteNameCache(filePath);
411     }
412 
413     for (auto &item : this->indirectExportList_) {
414         item.WriteNameCache(filePath);
415     }
416 }
417 
CreateModuleVar(const pandasm::LiteralArray & literalArray)418 void panda::guard::ModuleRecord::CreateModuleVar(const pandasm::LiteralArray &literalArray)
419 {
420     uint32_t offset = 0;
421     CreateFilePathList(literalArray.literals_, offset);
422     CreateRegularImportList(literalArray.literals_, offset);
423     CreateNameSpaceImportList(literalArray.literals_, offset);
424     CreateLocalExportList(literalArray.literals_, offset);
425     CreateIndirectExportList(literalArray.literals_, offset);
426 
427     Print();
428 }
429 
CreateFilePathList(const std::vector<pandasm::LiteralArray::Literal> & literals,uint32_t & offset)430 void panda::guard::ModuleRecord::CreateFilePathList(const std::vector<pandasm::LiteralArray::Literal> &literals,
431                                                     uint32_t &offset)
432 {
433     uint32_t num = GetIntegerValueByOffset(literals, offset);
434     for (uint32_t idx = 0; idx < num; idx++) {
435         FilePathItem item(this->program_, this->literalArrayIdx_);
436         item.refFilePath_.filePathIndex_ = offset;
437         item.refFilePath_.SetFilePath(GetStringValueByOffset(literals, offset));
438         item.Create();
439         this->filePathList_.emplace_back(item);
440     }
441 }
442 
CreateRegularImportList(const std::vector<pandasm::LiteralArray::Literal> & literals,uint32_t & offset)443 void panda::guard::ModuleRecord::CreateRegularImportList(const std::vector<pandasm::LiteralArray::Literal> &literals,
444                                                          uint32_t &offset)
445 {
446     uint32_t num = GetIntegerValueByOffset(literals, offset);
447     for (uint32_t idx = 0; idx < num; idx++) {
448         RegularImportItem item(this->program_, this->literalArrayIdx_);
449         item.localNameIndex_ = offset;
450         item.localName_ = GetStringValueByOffset(literals, offset);
451         item.obfLocalName_ = item.localName_;
452         item.importNameIndex_ = offset;
453         item.importName_ = GetStringValueByOffset(literals, offset);
454         item.obfImportName_ = item.importName_;
455         uint16_t filePathItemIndex = GetMethodAffiliateValueByOffset(literals, offset);
456         PANDA_GUARD_ASSERT_PRINT(filePathItemIndex >= this->filePathList_.size(), TAG, ErrorCode::GENERIC_ERROR,
457                                  "filePathItem index overflow");
458         item.remoteFile_ = this->filePathList_[filePathItemIndex].refFilePath_.isRemoteFile_;
459         this->regularImportList_.emplace_back(item);
460     }
461 }
462 
CreateNameSpaceImportList(const std::vector<pandasm::LiteralArray::Literal> & literals,uint32_t & offset)463 void panda::guard::ModuleRecord::CreateNameSpaceImportList(const std::vector<pandasm::LiteralArray::Literal> &literals,
464                                                            uint32_t &offset)
465 {
466     uint32_t num = GetIntegerValueByOffset(literals, offset);
467     for (uint32_t idx = 0; idx < num; idx++) {
468         NameSpaceImportItem item(this->program_, this->literalArrayIdx_);
469         item.localNameIndex_ = offset;
470         item.localName_ = GetStringValueByOffset(literals, offset);
471         item.obfLocalName_ = item.localName_;
472         uint16_t filePathItemIndex = GetMethodAffiliateValueByOffset(literals, offset);
473         PANDA_GUARD_ASSERT_PRINT(filePathItemIndex >= this->filePathList_.size(), TAG, ErrorCode::GENERIC_ERROR,
474                                  "filePathItem index overflow");
475         item.remoteFile_ = this->filePathList_[filePathItemIndex].refFilePath_.isRemoteFile_;
476         this->nameSpaceImportList_.emplace_back(item);
477     }
478 }
479 
CreateLocalExportList(const std::vector<pandasm::LiteralArray::Literal> & literals,uint32_t & offset)480 void panda::guard::ModuleRecord::CreateLocalExportList(const std::vector<pandasm::LiteralArray::Literal> &literals,
481                                                        uint32_t &offset)
482 {
483     uint32_t num = GetIntegerValueByOffset(literals, offset);
484     for (uint32_t idx = 0; idx < num; idx++) {
485         LocalExportItem item(this->program_, this->literalArrayIdx_);
486         item.localNameIndex_ = offset;
487         item.localName_ = GetStringValueByOffset(literals, offset);
488         item.obfLocalName_ = item.localName_;
489         item.exportNameIndex_ = offset;
490         item.exportName_ = GetStringValueByOffset(literals, offset);
491         item.obfExportName_ = item.exportName_;
492         this->localExportList_.emplace_back(item);
493     }
494 }
495 
CreateIndirectExportList(const std::vector<pandasm::LiteralArray::Literal> & literals,uint32_t & offset)496 void panda::guard::ModuleRecord::CreateIndirectExportList(const std::vector<pandasm::LiteralArray::Literal> &literals,
497                                                           uint32_t &offset)
498 {
499     uint32_t num = GetIntegerValueByOffset(literals, offset);
500     for (uint32_t idx = 0; idx < num; idx++) {
501         IndirectExportItem item(this->program_, this->literalArrayIdx_);
502         item.exportNameIndex_ = offset;
503         item.exportName_ = GetStringValueByOffset(literals, offset);
504         item.obfExportName_ = item.exportName_;
505         item.importNameIndex_ = offset;
506         item.importName_ = GetStringValueByOffset(literals, offset);
507         item.obfImportName_ = item.importName_;
508         uint16_t filePathItemIndex = GetMethodAffiliateValueByOffset(literals, offset);
509         PANDA_GUARD_ASSERT_PRINT(filePathItemIndex >= this->filePathList_.size(), TAG, ErrorCode::GENERIC_ERROR,
510                                  "filePathItem index overflow");
511         item.remoteFile_ = this->filePathList_[filePathItemIndex].refFilePath_.isRemoteFile_;
512         this->indirectExportList_.emplace_back(item);
513     }
514 }
515 
Print()516 void panda::guard::ModuleRecord::Print()
517 {
518     LOG(INFO, PANDAGUARD) << TAG << "literalArrayIdx_:" << this->literalArrayIdx_;
519 
520     for (const auto &item : this->filePathList_) {
521         LOG(INFO, PANDAGUARD) << TAG << "filePathList file:" << item.refFilePath_.filePath_;
522     }
523 
524     for (const auto &item : this->regularImportList_) {
525         LOG(INFO, PANDAGUARD) << TAG << "regularImport local name:" << item.localName_;
526         LOG(INFO, PANDAGUARD) << TAG << "regularImport import name:" << item.importName_;
527     }
528 
529     for (const auto &item : this->nameSpaceImportList_) {
530         LOG(INFO, PANDAGUARD) << TAG << "NameSpaceImport local name:" << item.localName_;
531     }
532 
533     for (const auto &item : this->localExportList_) {
534         LOG(INFO, PANDAGUARD) << TAG << "localExport local name:" << item.localName_;
535         LOG(INFO, PANDAGUARD) << TAG << "localExport export name:" << item.exportName_;
536     }
537 
538     for (const auto &item : this->indirectExportList_) {
539         LOG(INFO, PANDAGUARD) << TAG << "indirectExport local name:" << item.importName_;
540         LOG(INFO, PANDAGUARD) << TAG << "indirectExport export name:" << item.exportName_;
541     }
542 }
543 
RefreshNeedUpdate()544 void panda::guard::ModuleRecord::RefreshNeedUpdate()
545 {
546     for (auto &item : this->regularImportList_) {
547         item.RefreshNeedUpdate();
548     }
549 
550     for (auto &item : this->nameSpaceImportList_) {
551         item.RefreshNeedUpdate();
552     }
553 
554     for (auto &item : this->localExportList_) {
555         item.RefreshNeedUpdate();
556     }
557 
558     for (auto &item : this->indirectExportList_) {
559         item.RefreshNeedUpdate();
560     }
561 }
562 
GetLocalExportName(uint32_t index)563 std::string panda::guard::ModuleRecord::GetLocalExportName(uint32_t index)
564 {
565     PANDA_GUARD_ASSERT_PRINT(index >= this->localExportList_.size(), TAG, ErrorCode::GENERIC_ERROR, "index is invalid");
566     return this->localExportList_[index].exportName_;
567 }
568 
UpdateFileNameReferences()569 void panda::guard::ModuleRecord::UpdateFileNameReferences()
570 {
571     LOG(INFO, PANDAGUARD) << TAG << "update FileNameReferences for " << this->literalArrayIdx_ << " start";
572     for (auto &item : this->filePathList_) {
573         item.Obfuscate();
574     }
575     LOG(INFO, PANDAGUARD) << TAG << "update FileNameReferences for " << this->literalArrayIdx_ << " start";
576 }
577