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 "graph_analyzer.h"
22
23 namespace {
24 using OpcodeList = std::vector<panda::pandasm::Opcode>;
25
26 constexpr std::string_view TAG = "[Property]";
27
28 const OpcodeList PROPERTY_TYPE_LIST_NORMAL = {
29 panda::pandasm::Opcode::STOBJBYNAME, panda::pandasm::Opcode::STSUPERBYNAME,
30 panda::pandasm::Opcode::DEFINEPROPERTYBYNAME, panda::pandasm::Opcode::STOBJBYVALUE,
31 panda::pandasm::Opcode::STSUPERBYVALUE,
32 };
33 } // namespace
34
IsPropertyIns(const panda::guard::InstructionInfo & info)35 bool panda::guard::Property::IsPropertyIns(const panda::guard::InstructionInfo &info)
36 {
37 return std::any_of(PROPERTY_TYPE_LIST_NORMAL.begin(), PROPERTY_TYPE_LIST_NORMAL.end(),
38 [&](panda::pandasm::Opcode opcode) -> bool { return info.ins_->opcode == opcode; });
39 }
40
GetPropertyNameInfo(const InstructionInfo & info,InstructionInfo & nameInfo)41 void panda::guard::Property::GetPropertyNameInfo(const InstructionInfo &info, InstructionInfo &nameInfo)
42 {
43 if (info.ins_->opcode != pandasm::Opcode::STOBJBYVALUE && info.ins_->opcode != pandasm::Opcode::STSUPERBYVALUE) {
44 nameInfo = info;
45 return;
46 }
47
48 // this.['property']
49 GraphAnalyzer::GetLdaStr(info, nameInfo);
50 }
51
ExtractNames(std::set<std::string> & strings) const52 void panda::guard::Property::ExtractNames(std::set<std::string> &strings) const
53 {
54 strings.emplace(this->name_);
55 }
56
Update()57 void panda::guard::Property::Update()
58 {
59 PANDA_GUARD_ASSERT_PRINT(!this->defineInsList_[0].IsValid() || !this->nameInfo_.IsValid(), TAG,
60 ErrorCode::GENERIC_ERROR, "get bad insInfo for ins" << this->name_);
61
62 this->obfName_ = GuardContext::GetInstance()->GetNameMapping()->GetName(this->name_);
63 this->nameInfo_.ins_->ids[0] = this->obfName_;
64 this->program_->prog_->strings.emplace(this->obfName_);
65 }