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 "property.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 #include "graph_analyzer.h"
23
24 namespace {
25 using OpcodeList = std::vector<panda::pandasm::Opcode>;
26
27 constexpr std::string_view TAG = "[Property]";
28 constexpr std::string_view STATE_VARIABLE_PREFIX = "__";
29
30 const OpcodeList PROPERTY_TYPE_LIST_NORMAL = {
31 panda::pandasm::Opcode::STOBJBYNAME, panda::pandasm::Opcode::STSUPERBYNAME,
32 panda::pandasm::Opcode::DEFINEPROPERTYBYNAME, panda::pandasm::Opcode::STOBJBYVALUE,
33 panda::pandasm::Opcode::STSUPERBYVALUE,
34 };
35
IsGetLdaStrPropertyIns(const panda::guard::InstructionInfo & info)36 bool IsGetLdaStrPropertyIns(const panda::guard::InstructionInfo &info)
37 {
38 return ((info.ins_->opcode == panda::pandasm::Opcode::STOBJBYVALUE) ||
39 (info.ins_->opcode == panda::pandasm::Opcode::STSUPERBYVALUE));
40 }
41 } // namespace
42
IsPropertyIns(const panda::guard::InstructionInfo & info)43 bool panda::guard::Property::IsPropertyIns(const panda::guard::InstructionInfo &info)
44 {
45 return std::any_of(PROPERTY_TYPE_LIST_NORMAL.begin(), PROPERTY_TYPE_LIST_NORMAL.end(),
46 [&](panda::pandasm::Opcode opcode) -> bool { return info.ins_->opcode == opcode; });
47 }
48
GetPropertyNameInfo(const InstructionInfo & info,InstructionInfo & nameInfo)49 void panda::guard::Property::GetPropertyNameInfo(const InstructionInfo &info, InstructionInfo &nameInfo)
50 {
51 if (!IsGetLdaStrPropertyIns(info)) {
52 nameInfo = info;
53 return;
54 }
55
56 // this.['property']
57 GraphAnalyzer::GetLdaStr(info, nameInfo);
58 }
59
ExtractNames(std::set<std::string> & strings) const60 void panda::guard::Property::ExtractNames(std::set<std::string> &strings) const
61 {
62 strings.emplace(this->name_);
63 }
64
Update()65 void panda::guard::Property::Update()
66 {
67 PANDA_GUARD_ASSERT_PRINT(!this->defineInsList_[0].IsValid() || !this->nameInfo_.IsValid(), TAG,
68 ErrorCode::GENERIC_ERROR, "get bad insInfo for ins" << this->name_);
69
70 if (!StringUtil::IsPrefixMatched(this->name_, STATE_VARIABLE_PREFIX.data())) {
71 this->obfName_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->name_);
72 } else {
73 auto tmpName = this->name_.substr(STATE_VARIABLE_PREFIX.size());
74 auto obfTmpName = GuardContext::GetInstance()->GetNameMapping()->GetName(tmpName);
75 this->obfName_ = STATE_VARIABLE_PREFIX;
76 this->obfName_ += obfTmpName;
77 GuardContext::GetInstance()->GetNameMapping()->AddNameMapping(this->name_, this->obfName_);
78 }
79
80 this->nameInfo_.ins_->GetId(0) = this->obfName_;
81 this->program_->prog_->strings.emplace(this->obfName_);
82
83 for (auto &defineIns : this->defineInsList_) {
84 if (!defineIns.IsValid()) {
85 LOG(INFO, PANDAGUARD) << TAG << "no bind define ins, ignore update define";
86 continue;
87 }
88
89 if (!IsGetLdaStrPropertyIns(defineIns)) {
90 defineIns.ins_->GetId(0) = this->obfName_;
91 }
92 }
93 }