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 "entity.h"
17
18 #include "configs/guard_context.h"
19 #include "program.h"
20
21 namespace {
22 constexpr std::string_view TAG = "[Entity]";
23 constexpr std::string_view SCOPE_DELIMITER = "#";
24 } // namespace
25
Create()26 void panda::guard::Entity::Create()
27 {
28 this->Build();
29 this->RefreshNeedUpdate();
30
31 LOG(INFO, PANDAGUARD) << TAG << "needUpdate:" << (this->needUpdate_ ? "true" : "false");
32 }
33
Obfuscate()34 void panda::guard::Entity::Obfuscate()
35 {
36 if (!this->needUpdate_) {
37 this->WriteNameCache();
38 return;
39 }
40
41 this->Update();
42
43 this->obfuscated_ = true;
44 }
45
Build()46 void panda::guard::Entity::Build() {}
47
RefreshNeedUpdate()48 void panda::guard::Entity::RefreshNeedUpdate() {}
49
Update()50 void panda::guard::Entity::Update() {}
51
WriteNameCache(const std::string & filePath)52 void panda::guard::Entity::WriteNameCache(const std::string &filePath)
53 {
54 this->WriteFileCache(filePath);
55 this->WritePropertyCache();
56 }
57
WriteNameCache()58 void panda::guard::Entity::WriteNameCache() {}
59
SetNameCacheScope(const std::string & name)60 void panda::guard::Entity::SetNameCacheScope(const std::string &name)
61 {
62 if (this->scope_ == TOP_LEVEL) {
63 this->nameCacheScope_ = name;
64 } else {
65 if (!this->defineInsList_.empty()) {
66 this->nameCacheScope_ = this->defineInsList_[0].function_->nameCacheScope_ + SCOPE_DELIMITER.data() + name;
67 }
68 }
69 }
70
GetNameCacheScope() const71 std::string panda::guard::Entity::GetNameCacheScope() const
72 {
73 return this->scope_ == TOP_LEVEL ? SCOPE_DELIMITER.data() + this->nameCacheScope_ : this->nameCacheScope_;
74 }
75
UpdateLiteralArrayTableIdx(const std::string & originIdx,const std::string & updatedIdx) const76 void panda::guard::Entity::UpdateLiteralArrayTableIdx(const std::string &originIdx, const std::string &updatedIdx) const
77 {
78 auto entry = this->program_->prog_->literalarray_table.extract(originIdx);
79 entry.key() = updatedIdx;
80 this->program_->prog_->literalarray_table.insert(std::move(entry));
81 }
82
SetExportAndRefreshNeedUpdate(bool isExport)83 void panda::guard::Entity::SetExportAndRefreshNeedUpdate(bool isExport)
84 {
85 this->export_ = isExport;
86 this->RefreshNeedUpdate();
87 }
88
IsExport() const89 bool panda::guard::Entity::IsExport() const
90 {
91 return this->export_;
92 }
93
WriteFileCache(const std::string & filePath)94 void panda::guard::Entity::WriteFileCache(const std::string &filePath) {}
95
WritePropertyCache()96 void panda::guard::Entity::WritePropertyCache() {}
97
GetName() const98 std::string panda::guard::Entity::GetName() const
99 {
100 return this->name_;
101 }
102
GetObfName() const103 std::string panda::guard::Entity::GetObfName() const
104 {
105 return this->obfName_;
106 }
107
NeedUpdate(const Entity & entity)108 bool panda::guard::TopLevelOptionEntity::NeedUpdate(const Entity &entity)
109 {
110 bool needUpdate = true;
111 do {
112 const auto &options = GuardContext::GetInstance()->GetGuardOptions();
113 if (entity.IsExport()) {
114 needUpdate = options->IsExportObfEnabled() && options->IsToplevelObfEnabled();
115 break;
116 }
117
118 if (entity.scope_ == TOP_LEVEL) {
119 needUpdate = options->IsToplevelObfEnabled();
120 break;
121 }
122 } while (false);
123
124 if (!needUpdate) {
125 GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(entity.name_);
126 }
127
128 return needUpdate;
129 }
130
RefreshNeedUpdate()131 void panda::guard::TopLevelOptionEntity::RefreshNeedUpdate()
132 {
133 this->needUpdate_ = NeedUpdate(*this);
134 }
135
WritePropertyCache(const panda::guard::Entity & entity)136 void panda::guard::TopLevelOptionEntity::WritePropertyCache(const panda::guard::Entity &entity)
137 {
138 if (!entity.obfuscated_ || entity.GetName().empty() || entity.GetObfName().empty() ||
139 (entity.scope_ != TOP_LEVEL)) {
140 return;
141 }
142
143 const auto &options = GuardContext::GetInstance()->GetGuardOptions();
144 if (!options->IsToplevelObfEnabled()) {
145 return;
146 }
147 if (!options->IsPropertyObfEnabled() && !options->IsExportObfEnabled()) {
148 return;
149 }
150
151 if (entity.IsExport()) {
152 if (options->IsExportObfEnabled()) {
153 GuardContext::GetInstance()->GetNameCache()->AddObfPropertyName(entity.GetName(), entity.GetObfName());
154 }
155 } else {
156 GuardContext::GetInstance()->GetNameCache()->AddObfPropertyName(entity.GetName(), entity.GetObfName());
157 }
158 }
159
WritePropertyCache()160 void panda::guard::TopLevelOptionEntity::WritePropertyCache()
161 {
162 return WritePropertyCache(*this);
163 }
164
NeedUpdate(const Entity & entity)165 bool panda::guard::PropertyOptionEntity::NeedUpdate(const Entity &entity)
166 {
167 const auto &options = GuardContext::GetInstance()->GetGuardOptions();
168 bool needUpdate;
169 if (entity.IsExport()) {
170 needUpdate = options->IsExportObfEnabled() && options->IsPropertyObfEnabled();
171 } else {
172 needUpdate = options->IsPropertyObfEnabled();
173 }
174 if (!needUpdate) {
175 GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(entity.name_);
176 }
177 return needUpdate;
178 }
179
RefreshNeedUpdate()180 void panda::guard::PropertyOptionEntity::RefreshNeedUpdate()
181 {
182 this->needUpdate_ = NeedUpdate(*this);
183 }
184
WritePropertyCache(const panda::guard::Entity & entity)185 void panda::guard::PropertyOptionEntity::WritePropertyCache(const panda::guard::Entity &entity)
186 {
187 if (!entity.obfuscated_ || entity.GetName().empty() || entity.GetObfName().empty()) {
188 return;
189 }
190
191 const auto &options = GuardContext::GetInstance()->GetGuardOptions();
192 if (!options->IsPropertyObfEnabled() && !options->IsExportObfEnabled()) {
193 return;
194 }
195
196 if (entity.IsExport()) {
197 if (options->IsExportObfEnabled()) {
198 GuardContext::GetInstance()->GetNameCache()->AddObfPropertyName(entity.GetName(), entity.GetObfName());
199 }
200 } else {
201 GuardContext::GetInstance()->GetNameCache()->AddObfPropertyName(entity.GetName(), entity.GetObfName());
202 }
203 }
204
WritePropertyCache()205 void panda::guard::PropertyOptionEntity::WritePropertyCache()
206 {
207 return WritePropertyCache(*this);
208 }