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 const auto &options = GuardContext::GetInstance()->GetGuardOptions();
137 if (!options->IsPropertyObfEnabled() && !options->IsExportObfEnabled()) {
138 return;
139 }
140
141 auto nameCache = GuardContext::GetInstance()->GetNameCache();
142 nameCache->AddObfPropertyName(this->localName_, this->obfLocalName_);
143 nameCache->AddObfPropertyName(this->importName_, this->obfImportName_);
144 }
145
Update()146 void panda::guard::NameSpaceImportItem::Update()
147 {
148 auto &literalArrayTable = this->program_->prog_->literalarray_table;
149 auto it = literalArrayTable.find(this->literalArrayIdx_);
150 PANDA_GUARD_ASSERT_PRINT(it == literalArrayTable.end(), TAG, ErrorCode::GENERIC_ERROR,
151 "get bad literalArrayIdx:" << literalArrayIdx_);
152
153 this->obfLocalName_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->localName_);
154 it->second.literals_[this->localNameIndex_].value_ = this->obfLocalName_;
155 }
156
ExtractNames(std::set<std::string> & strings) const157 void panda::guard::NameSpaceImportItem::ExtractNames(std::set<std::string> &strings) const
158 {
159 strings.emplace(this->localName_);
160 }
161
RefreshNeedUpdate()162 void panda::guard::NameSpaceImportItem::RefreshNeedUpdate()
163 {
164 this->needUpdate_ = GuardContext::GetInstance()->GetGuardOptions()->IsToplevelObfEnabled() && !remoteFile_;
165 if (!this->needUpdate_) {
166 GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->localName_);
167 }
168 }
169
WriteFileCache(const std::string & filePath)170 void panda::guard::NameSpaceImportItem::WriteFileCache(const std::string &filePath)
171 {
172 auto nameCache = GuardContext::GetInstance()->GetNameCache();
173 if (this->localName_ != this->obfLocalName_) {
174 nameCache->AddObfIdentifierName(filePath, SCOPE_DELIMITER.data() + this->localName_, this->obfLocalName_);
175 }
176 }
177
WritePropertyCache()178 void panda::guard::NameSpaceImportItem::WritePropertyCache()
179 {
180 const auto &options = GuardContext::GetInstance()->GetGuardOptions();
181 if (!options->IsPropertyObfEnabled() && !options->IsExportObfEnabled()) {
182 return;
183 }
184 GuardContext::GetInstance()->GetNameCache()->AddObfPropertyName(this->localName_, this->obfLocalName_);
185 }
186
Update()187 void panda::guard::LocalExportItem::Update()
188 {
189 auto &literalArrayTable = this->program_->prog_->literalarray_table;
190 auto it = literalArrayTable.find(this->literalArrayIdx_);
191 PANDA_GUARD_ASSERT_PRINT(it == literalArrayTable.end(), TAG, ErrorCode::GENERIC_ERROR,
192 "get bad literalArrayIdx:" << literalArrayIdx_);
193
194 if (GuardContext::GetInstance()->GetGuardOptions()->IsExportObfEnabled()) {
195 this->obfLocalName_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->localName_);
196 it->second.literals_[this->localNameIndex_].value_ = this->obfLocalName_;
197
198 this->obfExportName_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->exportName_);
199 it->second.literals_[this->exportNameIndex_].value_ = this->obfExportName_;
200 return;
201 }
202
203 if (this->localName_ != this->exportName_) {
204 this->obfLocalName_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->localName_);
205 it->second.literals_[this->localNameIndex_].value_ = this->obfLocalName_;
206 }
207 }
208
ExtractNames(std::set<std::string> & strings) const209 void panda::guard::LocalExportItem::ExtractNames(std::set<std::string> &strings) const
210 {
211 strings.emplace(this->localName_);
212 strings.emplace(this->exportName_);
213 }
214
RefreshNeedUpdate()215 void panda::guard::LocalExportItem::RefreshNeedUpdate()
216 {
217 const auto &options = GuardContext::GetInstance()->GetGuardOptions();
218 if (!options->IsToplevelObfEnabled()) {
219 this->needUpdate_ = false;
220 GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->localName_);
221 GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->exportName_);
222 return;
223 }
224 if (options->IsExportObfEnabled()) {
225 this->needUpdate_ = true;
226 return;
227 }
228 if (this->localName_ != this->exportName_) {
229 this->needUpdate_ = true;
230 GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->exportName_);
231 return;
232 }
233 this->needUpdate_ = false;
234 GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->localName_);
235 GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->exportName_);
236 }
237
WriteFileCache(const std::string & filePath)238 void panda::guard::LocalExportItem::WriteFileCache(const std::string &filePath)
239 {
240 auto nameCache = GuardContext::GetInstance()->GetNameCache();
241 if (this->localName_ != this->obfLocalName_) {
242 nameCache->AddObfIdentifierName(filePath, SCOPE_DELIMITER.data() + this->localName_, this->obfLocalName_);
243 }
244 if ((this->exportName_ != this->localName_) && (this->exportName_ != this->obfExportName_)) {
245 nameCache->AddObfIdentifierName(filePath, SCOPE_DELIMITER.data() + this->exportName_, this->obfExportName_);
246 }
247 }
248
WritePropertyCache()249 void panda::guard::LocalExportItem::WritePropertyCache()
250 {
251 const auto &options = GuardContext::GetInstance()->GetGuardOptions();
252 if (!options->IsPropertyObfEnabled() && !options->IsExportObfEnabled()) {
253 return;
254 }
255
256 auto nameCache = GuardContext::GetInstance()->GetNameCache();
257 nameCache->AddObfPropertyName(this->localName_, this->obfLocalName_);
258 nameCache->AddObfPropertyName(this->exportName_, this->obfExportName_);
259 }
260
Update()261 void panda::guard::IndirectExportItem::Update()
262 {
263 auto &literalArrayTable = this->program_->prog_->literalarray_table;
264 auto it = literalArrayTable.find(this->literalArrayIdx_);
265 PANDA_GUARD_ASSERT_PRINT(it == literalArrayTable.end(), TAG, ErrorCode::GENERIC_ERROR,
266 "get bad literalArrayIdx:" << literalArrayIdx_);
267
268 this->obfImportName_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->importName_);
269 it->second.literals_[this->importNameIndex_].value_ = this->obfImportName_;
270
271 this->obfExportName_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->exportName_);
272 it->second.literals_[this->exportNameIndex_].value_ = this->obfExportName_;
273 }
274
ExtractNames(std::set<std::string> & strings) const275 void panda::guard::IndirectExportItem::ExtractNames(std::set<std::string> &strings) const
276 {
277 strings.emplace(this->importName_);
278 strings.emplace(this->exportName_);
279 }
280
RefreshNeedUpdate()281 void panda::guard::IndirectExportItem::RefreshNeedUpdate()
282 {
283 this->needUpdate_ = GuardContext::GetInstance()->GetGuardOptions()->IsExportObfEnabled() && !remoteFile_;
284 if (!this->needUpdate_) {
285 GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->importName_);
286 GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->exportName_);
287 }
288 }
289
WriteFileCache(const std::string & filePath)290 void panda::guard::IndirectExportItem::WriteFileCache(const std::string &filePath)
291 {
292 auto nameCache = GuardContext::GetInstance()->GetNameCache();
293 if (this->importName_ != this->obfImportName_) {
294 nameCache->AddObfIdentifierName(filePath, SCOPE_DELIMITER.data() + this->importName_, this->obfImportName_);
295 }
296 if ((this->exportName_ != this->importName_) && (this->exportName_ != this->obfExportName_)) {
297 nameCache->AddObfIdentifierName(filePath, SCOPE_DELIMITER.data() + this->exportName_, this->obfExportName_);
298 }
299 }
300
WritePropertyCache()301 void panda::guard::IndirectExportItem::WritePropertyCache()
302 {
303 const auto &options = GuardContext::GetInstance()->GetGuardOptions();
304 if (!options->IsPropertyObfEnabled() && !options->IsExportObfEnabled()) {
305 return;
306 }
307
308 auto nameCache = GuardContext::GetInstance()->GetNameCache();
309 nameCache->AddObfPropertyName(this->importName_, this->obfImportName_);
310 nameCache->AddObfPropertyName(this->exportName_, this->obfExportName_);
311 }
312
Build()313 void panda::guard::ModuleRecord::Build()
314 {
315 auto recordItem = program_->prog_->record_table.find(this->name_);
316 PANDA_GUARD_ASSERT_PRINT(recordItem == program_->prog_->record_table.end(), TAG, ErrorCode::GENERIC_ERROR,
317 "name:" << this->name_ << "not find in record_table");
318
319 for (const auto &field : recordItem->second.field_list) {
320 if (field.name == MODULE_RECORD_IDX) {
321 const auto &value = field.metadata->GetValue();
322 this->literalArrayIdx_ = value->GetValue<std::string>();
323 break;
324 }
325 }
326
327 if (this->literalArrayIdx_.empty()) {
328 // commonJs module don't have MODULE_RECORD_IDX
329 LOG(INFO, PANDAGUARD) << TAG << "no MODULE_RECORD_IDX in:" << this->name_;
330 return;
331 }
332
333 auto &literalArray = program_->prog_->literalarray_table.at(literalArrayIdx_);
334 CreateModuleVar(literalArray);
335 }
336
ExtractNames(std::set<std::string> & strings) const337 void panda::guard::ModuleRecord::ExtractNames(std::set<std::string> &strings) const
338 {
339 for (const auto &item : this->filePathList_) {
340 item.ExtractNames(strings);
341 }
342
343 for (const auto &item : this->regularImportList_) {
344 item.ExtractNames(strings);
345 }
346
347 for (const auto &item : this->nameSpaceImportList_) {
348 item.ExtractNames(strings);
349 }
350
351 for (const auto &item : this->localExportList_) {
352 item.ExtractNames(strings);
353 }
354
355 for (const auto &item : this->indirectExportList_) {
356 item.ExtractNames(strings);
357 }
358 }
359
IsExportVar(const std::string & var)360 bool panda::guard::ModuleRecord::IsExportVar(const std::string &var)
361 {
362 bool exportVar = std::any_of(localExportList_.begin(), localExportList_.end(), [&](const LocalExportItem &item) {
363 return (item.localName_ == var) || (item.exportName_ == var);
364 });
365 if (exportVar) {
366 return true;
367 }
368 return std::any_of(indirectExportList_.begin(), indirectExportList_.end(), [&](const IndirectExportItem &item) {
369 return (item.importName_ == var) || (item.exportName_ == var);
370 });
371 }
372
Update()373 void panda::guard::ModuleRecord::Update()
374 {
375 LOG(INFO, PANDAGUARD) << TAG << "update for " << this->literalArrayIdx_ << " start";
376
377 for (auto &item : this->regularImportList_) {
378 item.Obfuscate();
379 }
380
381 for (auto &item : this->nameSpaceImportList_) {
382 item.Obfuscate();
383 }
384
385 for (auto &item : this->localExportList_) {
386 item.Obfuscate();
387 }
388
389 for (auto &item : this->indirectExportList_) {
390 item.Obfuscate();
391 }
392 LOG(INFO, PANDAGUARD) << TAG << "update for " << this->literalArrayIdx_ << " end";
393 }
394
WriteNameCache(const std::string & filePath)395 void panda::guard::ModuleRecord::WriteNameCache(const std::string &filePath)
396 {
397 if (!this->obfuscated_) {
398 return;
399 }
400
401 for (auto &item : this->filePathList_) {
402 item.WriteNameCache(filePath);
403 }
404
405 for (auto &item : this->regularImportList_) {
406 item.WriteNameCache(filePath);
407 }
408
409 for (auto &item : this->nameSpaceImportList_) {
410 item.WriteNameCache(filePath);
411 }
412
413 for (auto &item : this->localExportList_) {
414 item.WriteNameCache(filePath);
415 }
416
417 for (auto &item : this->indirectExportList_) {
418 item.WriteNameCache(filePath);
419 }
420 }
421
CreateModuleVar(const pandasm::LiteralArray & literalArray)422 void panda::guard::ModuleRecord::CreateModuleVar(const pandasm::LiteralArray &literalArray)
423 {
424 uint32_t offset = 0;
425 CreateFilePathList(literalArray.literals_, offset);
426 CreateRegularImportList(literalArray.literals_, offset);
427 CreateNameSpaceImportList(literalArray.literals_, offset);
428 CreateLocalExportList(literalArray.literals_, offset);
429 CreateIndirectExportList(literalArray.literals_, offset);
430
431 Print();
432 }
433
CreateFilePathList(const std::vector<pandasm::LiteralArray::Literal> & literals,uint32_t & offset)434 void panda::guard::ModuleRecord::CreateFilePathList(const std::vector<pandasm::LiteralArray::Literal> &literals,
435 uint32_t &offset)
436 {
437 uint32_t num = GetIntegerValueByOffset(literals, offset);
438 for (uint32_t idx = 0; idx < num; idx++) {
439 FilePathItem item(this->program_, this->literalArrayIdx_);
440 item.refFilePath_.filePathIndex_ = offset;
441 item.refFilePath_.SetFilePath(GetStringValueByOffset(literals, offset));
442 item.Create();
443 this->filePathList_.emplace_back(item);
444 }
445 }
446
CreateRegularImportList(const std::vector<pandasm::LiteralArray::Literal> & literals,uint32_t & offset)447 void panda::guard::ModuleRecord::CreateRegularImportList(const std::vector<pandasm::LiteralArray::Literal> &literals,
448 uint32_t &offset)
449 {
450 uint32_t num = GetIntegerValueByOffset(literals, offset);
451 for (uint32_t idx = 0; idx < num; idx++) {
452 RegularImportItem item(this->program_, this->literalArrayIdx_);
453 item.localNameIndex_ = offset;
454 item.localName_ = GetStringValueByOffset(literals, offset);
455 item.obfLocalName_ = item.localName_;
456 item.importNameIndex_ = offset;
457 item.importName_ = GetStringValueByOffset(literals, offset);
458 item.obfImportName_ = item.importName_;
459 uint16_t filePathItemIndex = GetMethodAffiliateValueByOffset(literals, offset);
460 PANDA_GUARD_ASSERT_PRINT(filePathItemIndex >= this->filePathList_.size(), TAG, ErrorCode::GENERIC_ERROR,
461 "filePathItem index overflow");
462 item.remoteFile_ = this->filePathList_[filePathItemIndex].refFilePath_.isRemoteFile_;
463 this->regularImportList_.emplace_back(item);
464 }
465 }
466
CreateNameSpaceImportList(const std::vector<pandasm::LiteralArray::Literal> & literals,uint32_t & offset)467 void panda::guard::ModuleRecord::CreateNameSpaceImportList(const std::vector<pandasm::LiteralArray::Literal> &literals,
468 uint32_t &offset)
469 {
470 uint32_t num = GetIntegerValueByOffset(literals, offset);
471 for (uint32_t idx = 0; idx < num; idx++) {
472 NameSpaceImportItem item(this->program_, this->literalArrayIdx_);
473 item.localNameIndex_ = offset;
474 item.localName_ = GetStringValueByOffset(literals, offset);
475 item.obfLocalName_ = item.localName_;
476 uint16_t filePathItemIndex = GetMethodAffiliateValueByOffset(literals, offset);
477 PANDA_GUARD_ASSERT_PRINT(filePathItemIndex >= this->filePathList_.size(), TAG, ErrorCode::GENERIC_ERROR,
478 "filePathItem index overflow");
479 item.remoteFile_ = this->filePathList_[filePathItemIndex].refFilePath_.isRemoteFile_;
480 this->nameSpaceImportList_.emplace_back(item);
481 }
482 }
483
CreateLocalExportList(const std::vector<pandasm::LiteralArray::Literal> & literals,uint32_t & offset)484 void panda::guard::ModuleRecord::CreateLocalExportList(const std::vector<pandasm::LiteralArray::Literal> &literals,
485 uint32_t &offset)
486 {
487 uint32_t num = GetIntegerValueByOffset(literals, offset);
488 for (uint32_t idx = 0; idx < num; idx++) {
489 LocalExportItem item(this->program_, this->literalArrayIdx_);
490 item.localNameIndex_ = offset;
491 item.localName_ = GetStringValueByOffset(literals, offset);
492 item.obfLocalName_ = item.localName_;
493 item.exportNameIndex_ = offset;
494 item.exportName_ = GetStringValueByOffset(literals, offset);
495 item.obfExportName_ = item.exportName_;
496 this->localExportList_.emplace_back(item);
497 }
498 }
499
CreateIndirectExportList(const std::vector<pandasm::LiteralArray::Literal> & literals,uint32_t & offset)500 void panda::guard::ModuleRecord::CreateIndirectExportList(const std::vector<pandasm::LiteralArray::Literal> &literals,
501 uint32_t &offset)
502 {
503 uint32_t num = GetIntegerValueByOffset(literals, offset);
504 for (uint32_t idx = 0; idx < num; idx++) {
505 IndirectExportItem item(this->program_, this->literalArrayIdx_);
506 item.exportNameIndex_ = offset;
507 item.exportName_ = GetStringValueByOffset(literals, offset);
508 item.obfExportName_ = item.exportName_;
509 item.importNameIndex_ = offset;
510 item.importName_ = GetStringValueByOffset(literals, offset);
511 item.obfImportName_ = item.importName_;
512 uint16_t filePathItemIndex = GetMethodAffiliateValueByOffset(literals, offset);
513 PANDA_GUARD_ASSERT_PRINT(filePathItemIndex >= this->filePathList_.size(), TAG, ErrorCode::GENERIC_ERROR,
514 "filePathItem index overflow");
515 item.remoteFile_ = this->filePathList_[filePathItemIndex].refFilePath_.isRemoteFile_;
516 this->indirectExportList_.emplace_back(item);
517 }
518 }
519
Print()520 void panda::guard::ModuleRecord::Print()
521 {
522 LOG(INFO, PANDAGUARD) << TAG << "literalArrayIdx_:" << this->literalArrayIdx_;
523
524 for (const auto &item : this->filePathList_) {
525 LOG(INFO, PANDAGUARD) << TAG << "filePathList file:" << item.refFilePath_.filePath_;
526 }
527
528 for (const auto &item : this->regularImportList_) {
529 LOG(INFO, PANDAGUARD) << TAG << "regularImport local name:" << item.localName_;
530 LOG(INFO, PANDAGUARD) << TAG << "regularImport import name:" << item.importName_;
531 }
532
533 for (const auto &item : this->nameSpaceImportList_) {
534 LOG(INFO, PANDAGUARD) << TAG << "NameSpaceImport local name:" << item.localName_;
535 }
536
537 for (const auto &item : this->localExportList_) {
538 LOG(INFO, PANDAGUARD) << TAG << "localExport local name:" << item.localName_;
539 LOG(INFO, PANDAGUARD) << TAG << "localExport export name:" << item.exportName_;
540 }
541
542 for (const auto &item : this->indirectExportList_) {
543 LOG(INFO, PANDAGUARD) << TAG << "indirectExport local name:" << item.importName_;
544 LOG(INFO, PANDAGUARD) << TAG << "indirectExport export name:" << item.exportName_;
545 }
546 }
547
RefreshNeedUpdate()548 void panda::guard::ModuleRecord::RefreshNeedUpdate()
549 {
550 for (auto &item : this->regularImportList_) {
551 item.RefreshNeedUpdate();
552 }
553
554 for (auto &item : this->nameSpaceImportList_) {
555 item.RefreshNeedUpdate();
556 }
557
558 for (auto &item : this->localExportList_) {
559 item.RefreshNeedUpdate();
560 }
561
562 for (auto &item : this->indirectExportList_) {
563 item.RefreshNeedUpdate();
564 }
565 }
566
GetLocalExportName(uint32_t index)567 std::string panda::guard::ModuleRecord::GetLocalExportName(uint32_t index)
568 {
569 PANDA_GUARD_ASSERT_PRINT(index >= this->localExportList_.size(), TAG, ErrorCode::GENERIC_ERROR, "index is invalid");
570 return this->localExportList_[index].exportName_;
571 }
572
UpdateFileNameReferences()573 void panda::guard::ModuleRecord::UpdateFileNameReferences()
574 {
575 LOG(INFO, PANDAGUARD) << TAG << "update FileNameReferences for " << this->literalArrayIdx_ << " start";
576 for (auto &item : this->filePathList_) {
577 item.Obfuscate();
578 }
579 LOG(INFO, PANDAGUARD) << TAG << "update FileNameReferences for " << this->literalArrayIdx_ << " start";
580 }
581
UpdateLiteralArrayIdx(const std::string & literalArrayIdx)582 void panda::guard::ModuleRecord::UpdateLiteralArrayIdx(const std::string &literalArrayIdx)
583 {
584 this->literalArrayIdx_ = literalArrayIdx;
585
586 for (auto &item : this->filePathList_) {
587 item.literalArrayIdx_ = literalArrayIdx;
588 }
589
590 for (auto &item : this->regularImportList_) {
591 item.literalArrayIdx_ = literalArrayIdx;
592 }
593
594 for (auto &item : this->nameSpaceImportList_) {
595 item.literalArrayIdx_ = literalArrayIdx;
596 }
597
598 for (auto &item : this->localExportList_) {
599 item.literalArrayIdx_ = literalArrayIdx;
600 }
601
602 for (auto &item : this->indirectExportList_) {
603 item.literalArrayIdx_ = literalArrayIdx;
604 }
605 }
606