• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "inst_obf.h"
17 
18 #include "configs/guard_context.h"
19 #include "graph_analyzer.h"
20 #include "program.h"
21 
22 using namespace panda::guard;
23 namespace {
24 using FunctionProcessor = void(InstructionInfo &inst);
25 using ProcessorMap = std::map<panda::pandasm::Opcode, const std::function<FunctionProcessor>>;
26 
__anon79f322830202(InstructionInfo &inst) 27 constexpr auto PROCESSOR_DEFAULT = [](InstructionInfo &inst) { inst.UpdateInsName(false); };
28 
__anon79f322830302(InstructionInfo &inst) 29 constexpr auto PROCESSOR_DEFAULT_LDA_STR = [](InstructionInfo &inst) {
30     InstructionInfo outInfo;
31     GraphAnalyzer::GetLdaStr(inst, outInfo);
32     if (!outInfo.IsValid()) {
33         return;
34     }
35     outInfo.UpdateInsName(false);
36 };
37 
__anon79f322830402(InstructionInfo &inst) 38 constexpr auto PROCESSOR_FILE_PATH_LDA_STR = [](InstructionInfo &inst) {
39     InstructionInfo outInfo;
40     GraphAnalyzer::GetLdaStr(inst, outInfo);
41     if (!outInfo.IsValid()) {
42         return;
43     }
44     outInfo.UpdateInsFileName();
45 };
46 
47 const ProcessorMap INST_PROCESSOR_MAP = {
48     {panda::pandasm::Opcode::LDOBJBYNAME, PROCESSOR_DEFAULT},
49     {panda::pandasm::Opcode::THROW_UNDEFINEDIFHOLEWITHNAME, PROCESSOR_DEFAULT},
50     {panda::pandasm::Opcode::LDSUPERBYNAME, PROCESSOR_DEFAULT},
51     {panda::pandasm::Opcode::LDOBJBYVALUE, PROCESSOR_DEFAULT_LDA_STR},
52     {panda::pandasm::Opcode::ISIN, PROCESSOR_DEFAULT_LDA_STR},
53     {panda::pandasm::Opcode::LDSUPERBYVALUE, PROCESSOR_DEFAULT_LDA_STR},
54     {panda::pandasm::Opcode::DYNAMICIMPORT, PROCESSOR_FILE_PATH_LDA_STR},
55 };
56 }  // namespace
57 
UpdateInst(InstructionInfo & inst)58 void InstObf::UpdateInst(InstructionInfo &inst)
59 {
60     auto it = INST_PROCESSOR_MAP.find(inst.ins_->opcode);
61     if (it == INST_PROCESSOR_MAP.end()) {
62         return;
63     }
64     it->second(inst);
65 }
66