• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2025 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 "annotation.h"
17 
18 #include "utils/logger.h"
19 
20 #include "configs/guard_context.h"
21 #include "program.h"
22 
23 namespace {
24 constexpr std::string_view TAG = "[Annotation]";
25 constexpr std::string_view ANNOTATION_RECORD_DELIMITER = ".";
26 constexpr std::string_view SCOPE_DELIMITER = "#";
27 const std::vector<std::string> SYS_ANNOTATIONS = {"_ESConcurrentModuleRequestsAnnotation",
28                                                   "_ESExpectedPropertyCountAnnotation", "_ESSlotNumberAnnotation"};
29 }  // namespace
30 
IsWhiteListAnnotation(const std::string & name)31 bool panda::guard::Annotation::IsWhiteListAnnotation(const std::string &name)
32 {
33     return std::find(SYS_ANNOTATIONS.begin(), SYS_ANNOTATIONS.end(), name) != SYS_ANNOTATIONS.end();
34 }
35 
WriteNameCache(const std::string & filePath)36 void panda::guard::Annotation::WriteNameCache(const std::string &filePath)
37 {
38     if (!this->needUpdate_) {
39         return;
40     }
41     this->WriteFileCache(filePath);
42     this->WritePropertyCache();
43 }
44 
Update()45 void panda::guard::Annotation::Update()
46 {
47     LOG(INFO, PANDAGUARD) << TAG << "annotation update for:" << this->name_ << " start";
48     this->obfName_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->name_);
49 
50     const auto node = this->program_->nodeTable_.at(this->nodeName_);
51     auto obfRecordName = node->obfName_ + ANNOTATION_RECORD_DELIMITER.data() + this->obfName_;
52     LOG(INFO, PANDAGUARD) << TAG << "annotation obfName:" << obfRecordName;
53 
54     auto entry = this->program_->prog_->record_table.extract(this->recordName_);
55     entry.key() = obfRecordName;
56     entry.mapped().name = obfRecordName;
57     this->program_->prog_->record_table.insert(std::move(entry));
58 
59     recordName_ = obfRecordName;
60     LOG(INFO, PANDAGUARD) << TAG << "annotation update for:" << this->name_ << " end";
61 }
62 
RefreshNeedUpdate()63 void panda::guard::Annotation::RefreshNeedUpdate()
64 {
65     // when skipping obfuscated, the needUpdate_ field will be set to false
66     this->needUpdate_ = this->needUpdate_ && TopLevelOptionEntity::NeedUpdate(*this);
67 }
68 
WriteFileCache(const std::string & filePath)69 void panda::guard::Annotation::WriteFileCache(const std::string &filePath)
70 {
71     GuardContext::GetInstance()->GetNameCache()->AddObfIdentifierName(filePath, SCOPE_DELIMITER.data() + this->name_,
72                                                                       this->obfName_);
73 }
74 
WritePropertyCache()75 void panda::guard::Annotation::WritePropertyCache()
76 {
77     TopLevelOptionEntity::WritePropertyCache(*this);
78 }
79