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 "method.h"
17
18 #include "utils/logger.h"
19
20 #include "configs/guard_context.h"
21 #include "program.h"
22 #include "util/assert_util.h"
23
24 namespace {
25 constexpr std::string_view TAG = "[Method]";
26 }
27
InitNameCacheScope()28 void panda::guard::Method::InitNameCacheScope()
29 {
30 this->nameCacheScope_ = this->name_;
31 }
32
RefreshNeedUpdate()33 void panda::guard::Method::RefreshNeedUpdate()
34 {
35 this->contentNeedUpdate_ = PropertyOptionEntity::NeedUpdate(*this);
36 this->nameNeedUpdate_ = this->contentNeedUpdate_ && !IsWhiteListOrAnonymousFunction(this->idx_);
37
38 LOG(INFO, PANDAGUARD) << TAG << "Method contentNeedUpdate: " << (this->contentNeedUpdate_ ? "true" : "false");
39 LOG(INFO, PANDAGUARD) << TAG << "Method nameNeedUpdate: " << (this->nameNeedUpdate_ ? "true" : "false");
40 }
41
UpdateDefine() const42 void panda::guard::Method::UpdateDefine() const
43 {
44 auto &literalArrayTable = program_->prog_->literalarray_table;
45 auto it = literalArrayTable.find(this->literalArrayIdx_);
46 PANDA_GUARD_ASSERT_PRINT(it == literalArrayTable.end(), TAG, ErrorCode::GENERIC_ERROR,
47 "get bad literalArrayIdx:" << literalArrayIdx_);
48
49 it->second.literals_[this->idxIndex_].value_ = this->obfIdx_;
50 it->second.literals_[this->nameIndex_].value_ = this->obfName_;
51 }
52
WriteFileCache(const std::string & filePath)53 void panda::guard::Method::WriteFileCache(const std::string &filePath)
54 {
55 GuardContext::GetInstance()->GetNameCache()->AddObfMemberMethodName(
56 filePath, this->nameCacheScope_ + this->GetLines(), this->obfName_);
57 }
58
WritePropertyCache()59 void panda::guard::Method::WritePropertyCache()
60 {
61 PropertyOptionEntity::WritePropertyCache(*this);
62 }
63
RefreshNeedUpdate()64 void panda::guard::OuterMethod::RefreshNeedUpdate()
65 {
66 this->contentNeedUpdate_ = PropertyOptionEntity::NeedUpdate(*this);
67 this->nameNeedUpdate_ = this->contentNeedUpdate_ && !IsWhiteListOrAnonymousFunction(this->idx_);
68
69 LOG(INFO, PANDAGUARD) << TAG
70 << "Refresh outerMethod contentNeedUpdate: " << (this->contentNeedUpdate_ ? "true" : "false");
71 LOG(INFO, PANDAGUARD) << TAG
72 << "Refresh outerMethod nameNeedUpdate: " << (this->nameNeedUpdate_ ? "true" : "false");
73 }
74
WriteFileCache(const std::string & filePath)75 void panda::guard::OuterMethod::WriteFileCache(const std::string &filePath)
76 {
77 GuardContext::GetInstance()->GetNameCache()->AddObfMemberMethodName(filePath, this->nameDefine_ + this->GetLines(),
78 this->obfNameDefine_);
79 }
80
WritePropertyCache()81 void panda::guard::OuterMethod::WritePropertyCache()
82 {
83 PropertyOptionEntity::WritePropertyCache(*this);
84 }
85
SetContentNeedUpdate(bool toUpdate)86 void panda::guard::OuterMethod::SetContentNeedUpdate(bool toUpdate)
87 {
88 this->contentNeedUpdate_ = toUpdate && this->contentNeedUpdate_;
89 this->nameNeedUpdate_ = toUpdate && this->nameNeedUpdate_;
90
91 LOG(INFO, PANDAGUARD) << TAG
92 << "Set outerMethod contentNeedUpdate: " << (this->contentNeedUpdate_ ? "true" : "false");
93 LOG(INFO, PANDAGUARD) << TAG << "Set outerMethod nameNeedUpdate: " << (this->nameNeedUpdate_ ? "true" : "false");
94 }
95
GetName() const96 std::string panda::guard::OuterMethod::GetName() const
97 {
98 return this->nameDefine_;
99 }
100
GetObfName() const101 std::string panda::guard::OuterMethod::GetObfName() const
102 {
103 return this->obfNameDefine_;
104 }
105
Build()106 void panda::guard::OuterMethod::Build()
107 {
108 Function::Build();
109 if (this->nameInfo_.IsValid()) {
110 this->nameDefine_ = this->nameInfo_.ins_->ids[0];
111 } else {
112 this->nameDefine_ = this->name_;
113 }
114 }
115
Update()116 void panda::guard::OuterMethod::Update()
117 {
118 Function::Update();
119 if (this->contentNeedUpdate_) {
120 this->UpdateNameDefine();
121 }
122 }
123
UpdateNameDefine()124 void panda::guard::OuterMethod::UpdateNameDefine()
125 {
126 if (this->nameInfo_.IsValid()) {
127 this->obfNameDefine_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->nameDefine_);
128 this->nameInfo_.ins_->ids[0] = this->obfNameDefine_;
129 this->program_->prog_->strings.emplace(this->obfNameDefine_);
130 } else {
131 this->obfNameDefine_ = this->obfName_;
132 }
133 }
134
IsNameObfuscated() const135 bool panda::guard::OuterMethod::IsNameObfuscated() const
136 {
137 return Function::IsNameObfuscated() || this->nameInfo_.IsValid();
138 }
139
RefreshNeedUpdate()140 void panda::guard::PropertyMethod::RefreshNeedUpdate() {}
141
SetContentNeedUpdate(bool toUpdate)142 void panda::guard::PropertyMethod::SetContentNeedUpdate(bool toUpdate)
143 {
144 this->contentNeedUpdate_ = toUpdate && this->contentNeedUpdate_;
145 this->nameNeedUpdate_ = toUpdate && this->nameNeedUpdate_;
146 }
147